test/main.cpp

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 "../main/manager.h"
00021 
00022 #include "../api/object.h"
00023 #include "../api/class.h"
00024 #include "../api/module.h"
00025 //#include "../api/script.h"
00026 #include "../api/interpreter.h"
00027 
00028 #include "../main/scriptcontainer.h"
00029 
00030 //#include "../kexidb/kexidbmodule.h"
00031 
00032 #include "testobject.h"
00033 #include "testaction.h"
00034 #include "testwindow.h"
00035 #include "testplugin.h"
00036 
00037 // Qt
00038 #include <qstring.h>
00039 #include <qfile.h>
00040 
00041 // KDE
00042 #include <kdebug.h>
00043 #include <kinstance.h>
00044 #include <kapplication.h>
00045 #include <kcmdlineargs.h>
00046 #include <kaboutdata.h>
00047 #include <ksharedptr.h>
00048 
00049 // for std namespace
00050 #include <string>
00051 #include <iostream>
00052 
00053 KApplication *app = 0;
00054 
00055 static KCmdLineOptions options[] =
00056 {
00057     { "interpreter <interpretername>", I18N_NOOP("Name of the interpreter being used"), "python" },
00058     { "scriptfile <filename>", I18N_NOOP("Script file to execute with the defined interpreter"), "testcase.py" },
00059     { "gui", I18N_NOOP("Start the GUI; otherwise the command line application is used."), 0 },
00060 
00061     //{ "functionname <functioname>", I18N_NOOP("Execute the function in the defined script file."), "" },
00062     //{ "functionargs <functioarguments>", I18N_NOOP("List of arguments to pass to the function on execution."), "" },
00063     { 0, 0, 0 }
00064 };
00065 
00066 void runInterpreter(const QString& interpretername, const QString& scriptcode)
00067 {
00068     try {
00069 
00070         // Return the scriptingmanager instance. The manager is used as main
00071         // entry point to work with Kross.
00072         Kross::Api::Manager* manager = Kross::Api::Manager::scriptManager();
00073         if(! manager)
00074             Kross::Api::Exception::Ptr( new Kross::Api::Exception("Failed to get Kross::Api::Manager instance!") );
00075 
00076         // Add modules that should be accessible by scripting. Those
00077         // modules are wrappers around functionality you want to be
00078         // able to access from within scripts. You don't need to take
00079         // care of freeing them cause that will be done by Kross.
00080         // Modules are shared between the ScriptContainer instances.
00081         manager->addModule( new TestPluginModule("krosstestpluginmodule") );
00082 
00083         // To represent a script that should be executed Kross uses
00084         // the Script container class. You are able to fill them with
00085         // what is needed and just execute them.
00086         Kross::Api::ScriptContainer::Ptr scriptcontainer = manager->getScriptContainer("MyScriptName");
00087 
00088         //scriptcontainer->enableModule("KexiDB");
00089 
00090         scriptcontainer->setInterpreterName(interpretername);
00091         scriptcontainer->setCode(scriptcode);
00092 
00093         //TESTCASE
00094         TestObject* testobject = new TestObject(app, scriptcontainer);
00095         manager->addQObject( testobject );
00096 
00097         /*TestAction* testaction =*/ new TestAction(scriptcontainer);
00098         //manager->addQObject( testaction );
00099 
00100         /*Kross::Api::Object* o =*/ scriptcontainer->execute();
00101 
00102         // Call a function.
00103         //kdDebug()<<"--------------------------"<<endl;
00104         //scriptcontainer->callFunction("testobjectCallback" /*, Kross::Api::List* functionarguments */);
00105 
00106         // Call a class.
00107         /*
00108         kdDebug()<<"--------------------------"<<endl;
00109         Kross::Api::Object* testclassinstance = scriptcontainer->classInstance("testClass");
00110         if(testclassinstance) {
00111             QValueList<Kross::Api::Object*> ll;
00112             Kross::Api::Object* instancecallresult = testclassinstance->call("testClassFunction1", Kross::Api::List::create(ll));
00113             //kdDebug() << QString("testClass.testClassFunction1 returnvalue => '%1'").arg( instancecallresult.toString() ) << endl;
00114         }
00115         kdDebug()<<"--------------------------"<<endl;
00116         */
00117 
00118 
00119 /*
00120         // Connect QObject signal with scriptfunction.
00121         scriptcontainer->connect(testobject, SIGNAL(testSignal()), "testobjectCallback");
00122         scriptcontainer->connect(testobject, SIGNAL(testSignalString(const QString&)), "testobjectCallbackWithParams");
00123         // Call the testSlot to emit the testSignal.
00124         testobject->testSlot();
00125 */
00126     }
00127     catch(Kross::Api::Exception::Ptr e) {
00128         std::cout << QString("EXCEPTION %1").arg(e->toString()).latin1() << std::endl;
00129     }
00130 
00131 /*TESTCASE
00132     Kross::Api::ScriptContainer* sc2 = manager->getScriptContainer("MyScriptName222");
00133     sc2->setInterpreterName(interpretername);
00134     sc2->setCode(scriptcode);
00135     try {
00136         sc2->execute();
00137     }
00138     catch(Kross::Api::Exception& e) {
00139         kdDebug() << QString("EXCEPTION type='%1' description='%2'").arg(e.type()).arg(e.description()) << endl;
00140     }
00141     //delete sc2;
00142 */
00143 
00144     std::string s; std::cin >> s; // just wait.
00145 }
00146 
00147 int main(int argc, char **argv)
00148 {
00149     int result = 0;
00150 
00151     KAboutData about("krosstest",
00152                      "KrossTest",
00153                      "0.1",
00154                      "KDE application to test the Kross framework.",
00155                      KAboutData::License_LGPL,
00156                      "(C) 2005 Sebastian Sauer",
00157                      "Test the Kross framework!",
00158                      "http://www.dipe.org/kross",
00159                      "kross@dipe.org");
00160     about.addAuthor("Sebastian Sauer", "Author", "mail@dipe.org");
00161 
00162     KCmdLineArgs::init(argc, argv, &about);
00163     KCmdLineArgs::addCmdLineOptions(options);
00164 
00165     KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
00166     QString interpretername = args->getOption("interpreter");
00167     QString scriptfilename = args->getOption("scriptfile");
00168 
00169     QFile f(QFile::encodeName(scriptfilename));
00170     if(f.exists() && f.open(IO_ReadOnly)) {
00171         QString scriptcode = f.readAll();
00172         f.close();
00173 
00174         if( args->isSet("gui") ) {
00175             app = new KApplication();
00176             TestWindow *mainWin = new TestWindow(interpretername, scriptcode);
00177             app->setMainWidget(mainWin);
00178             mainWin->show();
00179             args->clear();
00180             result = app->exec();
00181         }
00182         else {
00183             app = new KApplication(true, true);
00184             runInterpreter(interpretername, scriptcode);
00185         }
00186     }
00187     else {
00188         kdWarning() << "Failed to load scriptfile: " << scriptfilename << endl;
00189         result = -1;
00190     }
00191 
00192     delete app;
00193     return result;
00194 }

Generated on Thu Feb 9 17:59:10 2006 for Kross by  doxygen 1.4.6