• Skip to content
  • Skip to link menu
KDE 4.1 API Reference
  • KDE API Reference
  • kdelibs
  • Sitemap
  • Contact Us
 

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 #endif // QT_ONLY
00033 
00034 #include <kjs/interpreter.h>
00035 #include <kjs/ustring.h>
00036 
00037 #include <kjsembed/kjseglobal.h>
00038 #include <kjsembed/kjsembed.h>
00039 
00040 #include <QtCore/QDate>
00041 
00042 using namespace KJSEmbed;
00043 
00044 void printUsage(QString appName)
00045 {
00046     (*KJSEmbed::conerr()) << "Usage: " << appName << " [options] [file]" << endl
00047                           << "Options:" << endl
00048                           << "    -e, --exec            execute script without gui support." << endl 
00049                           << "    -i, --interactive     start interactive kjs interpreter." << endl
00050 #ifndef QT_ONLY
00051                           << "    -n, --no-kde          start without KDE KApplication support." << endl
00052 #endif
00053                           << endl;
00054 }
00055 
00056 #ifndef QT_ONLY
00057 
00058 #endif // QT_ONLY
00059 
00060 int main( int argc, char **argv )
00061 {
00062     QTime time;
00063     time.start();
00064 
00065 #ifdef _WIN32
00066 #   ifdef CONSOLEIO
00067     RedirectIOToConsole();
00068 #   endif
00069 #endif
00070 
00071     // Handle arguments
00072     QString appName = argv[0];
00073     QStringList args;
00074     for (int i = 1; i < argc; i++ )
00075     {
00076         args << argv[i];
00077     }
00078 
00079     QString script;
00080     KJS::List scriptArgs;
00081     bool gui = true;
00082 #ifndef QT_ONLY
00083     /*
00084     #ifdef __GNUC__
00085         #warning "KDE Support enabled"
00086     #endif
00087     */
00088     bool kde = true;
00089 #else
00090     /*
00091     #ifdef __GNUC__
00092         #warning "KDE Support disabled"
00093     #endif
00094     */
00095 #endif
00096 
00097     if (argc > 1)
00098     {
00099         while (!args.isEmpty())
00100         {
00101             QString arg = args.takeFirst();
00102             if (arg.contains('-'))
00103             {
00104                 if ((arg == "--exec") || (arg == "-e"))
00105         {
00106                     gui = false;
00107         }
00108                 else if ((arg == "--interactive") || (arg == "-i"))
00109                     (*KJSEmbed::conout()) << "Interactive";
00110 #ifndef QT_ONLY
00111                 else if ((arg == "-n") || (arg == "--no-kde"))
00112         {
00113             kde = false;
00114         }
00115 #endif
00116         else
00117                 {
00118                     printUsage(appName);
00119                     return 0;
00120                 }
00121             }
00122             else
00123             {
00124                 if (!script.isEmpty())
00125                     scriptArgs.append(KJS::jsString(arg));
00126                 else
00127                     script = arg;
00128             }
00129         }
00130     }
00131     else
00132     {
00133         printUsage(appName);
00134         return 0;
00135     }
00136 
00137     // Setup QApplication
00138     QCoreApplication *app;
00139 
00140 #ifndef QT_ONLY
00141     if (kde)
00142     {
00143         KAboutData aboutData( "kjscmd", 0, ki18n("KJSCmd"), "0.2",
00144             ki18n(""
00145             "Utility for running KJSEmbed scripts \n" ),
00146             KAboutData::License_LGPL,
00147             ki18n("(C) 2005-2006 The KJSEmbed Authors") );
00148 
00149         KCmdLineOptions options;
00150         options.add("e", ki18n("Execute script without gui support"));
00151         options.add("exec", ki18n("Execute script without gui support"));
00152         options.add("i", ki18n("start interactive kjs interpreter"));
00153         options.add("interactive", ki18n("start interactive kjs interpreter"));
00154         options.add("n", ki18n("start without KDE KApplication support."));
00155         options.add("no-kde", ki18n("start without KDE KApplication support."));
00156         options.add("!+command", ki18n("Script to execute"));
00157 
00158         KCmdLineArgs::addCmdLineOptions( options ); // Add our own options.
00159         KCmdLineArgs::init( argc, argv, &aboutData );
00160 
00161         app = new KApplication(gui);
00162     }
00163     else
00164 #endif
00165     if (gui)
00166     {
00167     qDebug("no KDE");
00168         app = new QApplication( argc, argv );
00169         dynamic_cast<QApplication*>(app)->connect( app, SIGNAL( lastWindowClosed() ), SLOT(quit()) );
00170     }
00171     else
00172     {
00173     qDebug("no GUI");
00174         app = new QCoreApplication(argc, argv);
00175     }
00176     qDebug(" New %s %dms", app->metaObject()->className(), time.elapsed());
00177 
00178     app->setApplicationName( appName );
00179     
00180     // Setup Interpreter
00181     time.restart();
00182     Engine kernel;
00183     qDebug(" New engine %dms", time.elapsed());
00184     time.restart();
00185 
00186     KJS::Interpreter *js = kernel.interpreter();
00187     js->setShouldPrintExceptions(true);
00188     KJS::ExecState *exec = js->globalExec();
00189 
00190     // Publish bindings
00191     KJS::JSObject *appObject = kernel.addObject( app, "Application" );
00192     KJS::JSObject *argObject = js->builtinArray()->construct( exec, scriptArgs );
00193     appObject->put( exec, "args", argObject );
00194     Engine::ExitStatus result = Engine::Failure;
00195 
00196     if (!script.isEmpty())
00197     {
00198         result = kernel.runFile(toUString(script));
00199     }
00200     else // exec shell
00201     {
00202         result = kernel.runFile( ":/console.js" );
00203     }
00204 
00205     if ( result != Engine::Success )
00206     {
00207         KJS::Completion jsres = kernel.completion();
00208        (*KJSEmbed::conerr()) << toQString(jsres.value()->toString(exec)) << endl;
00209     }
00210     return (int)result;
00211 }
00212 

kjsembed

Skip menu "kjsembed"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • KIO
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • Kross
  • KUtils
  • Nepomuk
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal