Kross
main.cpp
Go to the documentation of this file.
00001 /*************************************************************************** 00002 * main.cpp 00003 * This file is part of the KDE project 00004 * copyright (C)2006 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 <QFile> 00021 #include <QScriptEngine> 00022 #include <QLibraryInfo> 00023 #include <QDebug> 00024 00025 #include <kapplication.h> 00026 #include <kcmdlineargs.h> 00027 #include <kaboutdata.h> 00028 // #include <kurl.h> 00029 00030 KApplication* app = 0; 00031 00032 bool runScriptFile(QScriptEngine* engine, const QString& scriptfile) 00033 { 00034 // Read the scriptfile 00035 QFile f(scriptfile); 00036 if(! f.exists()) { 00037 qWarning() << "No such scriptfile:" << scriptfile; 00038 return false; 00039 } 00040 if(! f.open(QIODevice::ReadOnly)) { 00041 qWarning() << "Failed to open scriptfile:" << scriptfile; 00042 return false; 00043 } 00044 QByteArray scriptcode = f.readAll(); 00045 f.close(); 00046 00047 // Execute the javascript code. 00048 qDebug() << "Execute scriptfile:" << scriptfile; 00049 QScriptValue v = engine->evaluate(scriptcode); 00050 qDebug() << "Execute done. Result:" << v.toString(); 00051 00052 return true; 00053 } 00054 00055 int main(int argc, char **argv) 00056 { 00057 KAboutData about("kross",0,ki18n("Kross"),"0.1", 00058 ki18n("KDE application to run Kross scripts."), 00059 KAboutData::License_LGPL, 00060 ki18n("(C) 2006 Sebastian Sauer"), 00061 ki18n("Run Kross scripts."), 00062 "http://kross.dipe.org","kross@dipe.org"); 00063 about.addAuthor(ki18n("Sebastian Sauer"), ki18n("Author"), "mail@dipe.org"); 00064 00065 // Initialize command line args 00066 KCmdLineArgs::init(argc, argv, &about); 00067 // Tell which options are supported and parse them. 00068 KCmdLineOptions options; 00069 options.add("+file", ki18n("Scriptfile")); 00070 KCmdLineArgs::addCmdLineOptions(options); 00071 KCmdLineArgs *args = KCmdLineArgs::parsedArgs(); 00072 00073 QStringList files; 00074 for(int i = 0; i < args->count(); ++i) 00075 files << args->arg(i); 00076 00077 // If no options are defined. 00078 if(files.count() < 1) { 00079 qWarning() << "Syntax:" << KCmdLineArgs::appName() << "scriptfile1 [scriptfile2] [scriptfile3] ..."; 00080 return -1; 00081 } 00082 00083 00084 app = new KApplication( /* GUIenabled */ true ); 00085 QScriptEngine* engine = new QScriptEngine(); 00086 QScriptValue global = engine->globalObject(); 00087 00088 //qDebug()<<"QLibraryInfo::PluginsPath="<<QLibraryInfo::location(QLibraryInfo::PluginsPath); 00089 //app->addLibraryPath("/home/kde4/kde4/lib/kde4/"); 00090 00091 engine->importExtension("kross").toString(); 00092 00093 foreach(const QString &file, files) 00094 runScriptFile(engine, file); 00095 00096 delete engine; 00097 delete app; 00098 return 0; 00099 }
KDE 4.6 API Reference