kjsembed
kjscmd.cpp
Go to the documentation of this file.
00001 /* This file is part of the KDE libraries 00002 Copyright (C) 2004, 2005, 2006 Ian Reinhart Geiser <geiseri@kde.org> 00003 Copyright (C) 2004, 2005, 2006 Matt Broadstone <mbroadst@gmail.com> 00004 Copyright (C) 2004, 2005, 2006 Richard J. Moore <rich@kde.org> 00005 Copyright (C) 2004, 2005, 2006 Erik L. Bunce <kde@bunce.us> 00006 00007 This library is free software; you can redistribute it and/or 00008 modify it under the terms of the GNU Library General Public 00009 License as published by the Free Software Foundation; either 00010 version 2 of the License, or (at your option) any later version. 00011 00012 This library is distributed in the hope that it will be useful, 00013 but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 Library General Public License for more details. 00016 00017 You should have received a copy of the GNU Library General Public License 00018 along with this library; see the file COPYING.LIB. If not, write to 00019 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00020 Boston, MA 02110-1301, USA. 00021 */ 00022 00023 00024 #include <QtGui/QApplication> 00025 #include <QtCore/QDebug> 00026 #include <QtCore/QStringList> 00027 00028 #ifndef QT_ONLY 00029 #include <kapplication.h> 00030 #include <kaboutdata.h> 00031 #include <kcmdlineargs.h> 00032 #include <kdeversion.h> 00033 #endif // QT_ONLY 00034 00035 #include <kjs/interpreter.h> 00036 #include <kjs/ustring.h> 00037 00038 #include <kjsembed/kjseglobal.h> 00039 #include <kjsembed/kjsembed.h> 00040 00041 #include <QtCore/QDate> 00042 00043 using namespace KJSEmbed; 00044 00045 void printUsage(QString appName) 00046 { 00047 (*KJSEmbed::conerr()) << "Usage: " << appName << " [options] [file]" << endl 00048 << "Options:" << endl 00049 << " -e, --exec execute script without gui support." << endl 00050 << " -i, --interactive start interactive kjs interpreter." << endl 00051 #ifndef QT_ONLY 00052 << " -n, --no-kde start without KDE KApplication support." << endl 00053 #endif 00054 << endl; 00055 } 00056 00057 #ifndef QT_ONLY 00058 00059 #endif // QT_ONLY 00060 00061 int main( int argc, char **argv ) 00062 { 00063 QTime time; 00064 time.start(); 00065 00066 #ifdef _WIN32 00067 # ifdef CONSOLEIO 00068 RedirectIOToConsole(); 00069 # endif 00070 #endif 00071 00072 // Handle arguments 00073 QString appName = argv[0]; 00074 QStringList args; 00075 for (int i = 1; i < argc; i++ ) 00076 { 00077 args << argv[i]; 00078 } 00079 00080 QString script; 00081 KJS::List scriptArgs; 00082 bool gui = true; 00083 #ifndef QT_ONLY 00084 /* 00085 #ifdef __GNUC__ 00086 #warning "KDE Support enabled" 00087 #endif 00088 */ 00089 bool kde = true; 00090 #else 00091 /* 00092 #ifdef __GNUC__ 00093 #warning "KDE Support disabled" 00094 #endif 00095 */ 00096 #endif 00097 00098 if (argc > 1) 00099 { 00100 while (!args.isEmpty()) 00101 { 00102 QString arg = args.takeFirst(); 00103 if (arg.contains('-')) 00104 { 00105 if ((arg == "--version") || (arg == "-v")) 00106 { 00107 printf("Qt: %s\n", qVersion()); 00108 #ifndef QT_ONLY 00109 printf("KDE: %s\n", KDE_VERSION_STRING); 00110 #endif 00111 return 0; 00112 } 00113 if ((arg == "--exec") || (arg == "-e")) 00114 { 00115 gui = false; 00116 } 00117 else if ((arg == "--interactive") || (arg == "-i")) 00118 (*KJSEmbed::conout()) << "Interactive"; 00119 #ifndef QT_ONLY 00120 else if ((arg == "-n") || (arg == "--no-kde")) 00121 { 00122 kde = false; 00123 } 00124 #endif 00125 else 00126 { 00127 printUsage(appName); 00128 return 0; 00129 } 00130 } 00131 else 00132 { 00133 if (!script.isEmpty()) 00134 scriptArgs.append(KJS::jsString(arg)); 00135 else 00136 script = arg; 00137 } 00138 } 00139 } 00140 else 00141 { 00142 printUsage(appName); 00143 return 0; 00144 } 00145 00146 // Setup QApplication 00147 QCoreApplication *app; 00148 00149 #ifndef QT_ONLY 00150 if (kde) 00151 { 00152 KAboutData aboutData( "kjscmd", 0, ki18n("KJSCmd"), "0.2", 00153 ki18n("" 00154 "Utility for running KJSEmbed scripts \n" ), 00155 KAboutData::License_LGPL, 00156 ki18n("(C) 2005-2006 The KJSEmbed Authors") ); 00157 00158 KCmdLineOptions options; 00159 options.add("e", ki18n("Execute script without gui support")); 00160 options.add("exec", ki18n("Execute script without gui support")); 00161 options.add("i", ki18n("start interactive kjs interpreter")); 00162 options.add("interactive", ki18n("start interactive kjs interpreter")); 00163 options.add("n", ki18n("start without KDE KApplication support.")); 00164 options.add("no-kde", ki18n("start without KDE KApplication support.")); 00165 options.add("!+command", ki18n("Script to execute")); 00166 00167 KCmdLineArgs::addCmdLineOptions( options ); // Add our own options. 00168 KCmdLineArgs::init( argc, argv, &aboutData ); 00169 00170 app = new KApplication(gui); 00171 } 00172 else 00173 #endif 00174 if (gui) 00175 { 00176 qDebug("no KDE"); 00177 app = new QApplication( argc, argv ); 00178 dynamic_cast<QApplication*>(app)->connect( app, SIGNAL( lastWindowClosed() ), SLOT(quit()) ); 00179 } 00180 else 00181 { 00182 qDebug("no GUI"); 00183 app = new QCoreApplication(argc, argv); 00184 } 00185 qDebug(" New %s %dms", app->metaObject()->className(), time.elapsed()); 00186 00187 app->setApplicationName( appName ); 00188 00189 // Setup Interpreter 00190 time.restart(); 00191 Engine kernel; 00192 qDebug(" New engine %dms", time.elapsed()); 00193 time.restart(); 00194 00195 KJS::Interpreter *js = kernel.interpreter(); 00196 js->setShouldPrintExceptions(true); 00197 KJS::ExecState *exec = js->globalExec(); 00198 00199 // Publish bindings 00200 KJS::JSObject *appObject = kernel.addObject( app, "Application" ); 00201 KJS::JSObject *argObject = js->builtinArray()->construct( exec, scriptArgs ); 00202 appObject->put( exec, "args", argObject ); 00203 Engine::ExitStatus result = Engine::Failure; 00204 00205 if (!script.isEmpty()) 00206 { 00207 result = kernel.runFile(toUString(script)); 00208 } 00209 else // exec shell 00210 { 00211 result = kernel.runFile( ":/console.js" ); 00212 } 00213 00214 if ( result != Engine::Success ) 00215 { 00216 KJS::Completion jsres = kernel.completion(); 00217 (*KJSEmbed::conerr()) << toQString(jsres.value()->toString(exec)) << endl; 00218 } 00219 return (int)result; 00220 } 00221
KDE 4.6 API Reference