F:/KPlato/koffice/libs/kross/test/main.cpp

Aller à la documentation de ce fichier.
00001 /***************************************************************************
00002  * main.cpp
00003  * This file is part of the KDE project
00004  * copyright (C)2004-2005 by Sebastian Sauer (mail@dipe.org)
00005  *
00006  * This program is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Library General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2 of the License, or (at your option) any later version.
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  * Library General Public License for more details.
00014  * You should have received a copy of the GNU Library General Public License
00015  * along with this program; see the file COPYING.  If not, write to
00016  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, USA.
00018  ***************************************************************************/
00019 
00020 #include "testobject.h"
00021 #include "testwindow.h"
00022 
00023 // Kross
00024 #include "../core/action.h"
00025 #include "../core/interpreter.h"
00026 #include "../core/manager.h"
00027 
00028 // Qt
00029 #include <QString>
00030 #include <QFile>
00031 #include <QMetaObject>
00032 #include <QMetaMethod>
00033 
00034 // KDE
00035 #include <kdebug.h>
00036 #include <kinstance.h>
00037 #include <kapplication.h>
00038 #include <kcmdlineargs.h>
00039 #include <kaboutdata.h>
00040 #include <ksharedptr.h>
00041 
00042 // for std namespace
00043 #include <string>
00044 #include <iostream>
00045 
00046 #define ERROR_OK 0
00047 #define ERROR_HELP -1
00048 #define ERROR_NOSUCHFILE -2
00049 #define ERROR_OPENFAILED -3
00050 #define ERROR_NOINTERPRETER -4
00051 #define ERROR_EXCEPTION -6
00052 
00053 KApplication *app = 0;
00054 
00055 static KCmdLineOptions options[] =
00056 {
00057     { "gui", "Start the GUI; otherwise the command line application is used.", 0 },
00058     { "+file", "Scriptfile", 0 },
00059 
00060     //{ "functionname <functioname>", I18N_NOOP("Execute the function in the defined script file."), "" },
00061     //{ "functionargs <functioarguments>", I18N_NOOP("List of arguments to pass to the function on execution."), "" },
00062     { 0, 0, 0 }
00063 };
00064 
00065 QString getInterpreterName(const QString& scriptfile)
00066 {
00067     Kross::InterpreterInfo* interpreterinfo = Kross::Manager::self().interpreterInfo( Kross::Manager::self().interpreternameForFile(scriptfile) );
00068     return interpreterinfo ? interpreterinfo->interpreterName() : "python";
00069 }
00070 
00071 int readFile(const QString& scriptfile, QString& content)
00072 {
00073     QFile f(QFile::encodeName(scriptfile));
00074     if(! f.exists()) {
00075         std::cerr << "No such scriptfile: " << scriptfile.toLatin1().data() << std::endl;
00076         return ERROR_NOSUCHFILE;
00077     }
00078     if(! f.open(QIODevice::ReadOnly)) {
00079         std::cerr << "Failed to open scriptfile: " << scriptfile.toLatin1().data() << std::endl;
00080         return ERROR_OPENFAILED;
00081     }
00082     content = f.readAll();
00083     f.close();
00084     return ERROR_OK;
00085 }
00086 
00087 static TestObject* testobj1 = 0;
00088 static TestObject* testobj2 = 0;
00089 
00090 void finishTestEnvironment()
00091 {
00092     delete testobj1; testobj1 = 0;
00093     delete testobj2; testobj2 = 0;
00094 }
00095 
00096 void initTestEnvironment()
00097 {
00098     // Create the testobject instances.
00099     testobj1 = new TestObject("TestObject1");
00100     testobj2 = new TestObject("TestObject2");
00101 
00102     // Publish both testobject instances.
00103     Kross::Manager::self().addObject( testobj1 );
00104     Kross::Manager::self().addObject( testobj2 );
00105 }
00106 
00107 int runScriptFile(const QString& scriptfile)
00108 {
00109     // Read the scriptfile
00110     QString scriptcode;
00111     int result = readFile(scriptfile, scriptcode);
00112     if(result != ERROR_OK)
00113         return result;
00114 
00115     // Determinate the matching interpreter
00116     QString interpretername = getInterpreterName(scriptfile);
00117 
00118     // Create the testobject instances.
00119     TestObject* testobj3 = new TestObject("TestObject3");
00120     TestObject* testobj4 = new TestObject("TestObject4");
00121 
00122     // First we need a Action and fill it.
00123     Kross::Action* action = new Kross::Action( scriptfile );
00124     action->setInterpreter( interpretername );
00125     action->setCode( scriptcode );
00126 
00127     // Publish other both testobject instance to the script.
00128     action->addObject( testobj3 );
00129     action->addObject( testobj4 );
00130 
00131     // Now execute the Action.
00132     std::cout << "Execute scriptfile " << scriptfile.toLatin1().data() << " now" << std::endl;
00133     action->trigger();
00134     std::cout << "Execution of scriptfile " << scriptfile.toLatin1().data() << " done" << std::endl;
00135 
00136     /*
00137     if(action->hadException()) {
00138         // We had an exception.
00139         QString errormessage = action->getException()->getError();
00140         QString tracedetails = action->getException()->getTrace();
00141         std::cerr << QString("%2\n%1").arg(tracedetails).arg(errormessage).toLatin1().data() << std::endl;
00142         return ERROR_EXCEPTION;
00143     }
00144     */
00145 
00146     delete action;
00147     delete testobj3;
00148     delete testobj4;
00149 
00150     return ERROR_OK;
00151 }
00152 
00153 int main(int argc, char **argv)
00154 {
00155     int result = 0;
00156 
00157     KAboutData about("krosstest",
00158                      "KrossTest",
00159                      "0.1",
00160                      "KDE application to test the Kross framework.",
00161                      KAboutData::License_LGPL,
00162                      "(C) 2005 Sebastian Sauer",
00163                      "Test the Kross framework!",
00164                      "http://kross.dipe.org",
00165                      "kross@dipe.org");
00166     about.addAuthor("Sebastian Sauer", "Author", "mail@dipe.org");
00167 
00168     KCmdLineArgs::init(argc, argv, &about);
00169     KCmdLineArgs::addCmdLineOptions(options);
00170 
00171     KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
00172 
00173     QStringList scriptfiles;
00174     for(int i = 0; i < args->count(); i++)
00175         scriptfiles.append( QFile::decodeName(args->arg(i)) );
00176 
00177     //testcase
00178     if(scriptfiles.count() < 1)
00179         scriptfiles.append("testcase.py");
00180 
00181     if(scriptfiles.count() < 1) {
00182         std::cerr << "No scriptfile to execute defined. See --help" << std::endl;
00183         return ERROR_NOSUCHFILE;
00184     }
00185 
00186     if( args->isSet("gui") ) {
00187         app = new KApplication();
00188 
00189         QString interpretername, scriptcode;
00190         if(scriptfiles.count() > 0) {
00191             result = readFile( scriptfiles[0], scriptcode );
00192             if(result == ERROR_OK)
00193                 interpretername = getInterpreterName( scriptfiles[0] );
00194         }
00195 
00196         if(result == ERROR_OK) {
00197             initTestEnvironment();
00198 
00199             TestWindow *mainWin = new TestWindow(interpretername, scriptcode);
00200             app->setMainWidget(mainWin);
00201             mainWin->show();
00202             args->clear();
00203             result = app->exec();
00204         }
00205     }
00206     else {
00207         app = new KApplication(true);
00208         initTestEnvironment();
00209 
00210         foreach(QString file, scriptfiles) {
00211             result = runScriptFile(file);
00212             if(result != ERROR_OK)
00213                 break;
00214         }
00215     }
00216 
00217     finishTestEnvironment();
00218     delete app;
00219 
00220     kDebug() << "DONE!!!" << endl;
00221     return result;
00222 }

Généré le Wed Nov 22 23:41:07 2006 pour KPlato par  doxygen 1.5.1-p1