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

kjsembed

  • sources
  • kde-4.14
  • kdelibs
  • kjsembed
  • kjscmd
kjscmd.cpp
Go to the documentation of this file.
1 /* This file is part of the KDE libraries
2  Copyright (C) 2004, 2005, 2006 Ian Reinhart Geiser <geiseri@kde.org>
3  Copyright (C) 2004, 2005, 2006 Matt Broadstone <mbroadst@gmail.com>
4  Copyright (C) 2004, 2005, 2006 Richard J. Moore <rich@kde.org>
5  Copyright (C) 2004, 2005, 2006 Erik L. Bunce <kde@bunce.us>
6 
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Library General Public
9  License as published by the Free Software Foundation; either
10  version 2 of the License, or (at your option) any later version.
11 
12  This library is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  Library General Public License for more details.
16 
17  You should have received a copy of the GNU Library General Public License
18  along with this library; see the file COPYING.LIB. If not, write to
19  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20  Boston, MA 02110-1301, USA.
21 */
22 
23 
24 #include <QtGui/QApplication>
25 #include <QtCore/QDebug>
26 #include <QtCore/QStringList>
27 
28 #ifndef QT_ONLY
29 #include <kapplication.h>
30 #include <kaboutdata.h>
31 #include <kcmdlineargs.h>
32 #include <kdeversion.h>
33 #endif // QT_ONLY
34 
35 #include <kjs/interpreter.h>
36 #include <kjs/ustring.h>
37 
38 #include <kjsembed/kjseglobal.h>
39 #include <kjsembed/kjsembed.h>
40 
41 #include <QtCore/QDate>
42 
43 using namespace KJSEmbed;
44 
45 void printUsage(QString appName)
46 {
47  (*KJSEmbed::conerr()) << "Usage: " << appName << " [options] [file]" << endl
48  << "Options:" << endl
49  << " -e, --exec execute script without gui support." << endl
50  << " -i, --interactive start interactive kjs interpreter." << endl
51 #ifndef QT_ONLY
52  << " -n, --no-kde start without KDE KApplication support." << endl
53 #endif
54  << endl;
55 }
56 
57 #ifndef QT_ONLY
58 
59 #endif // QT_ONLY
60 
61 int main( int argc, char **argv )
62 {
63  QTime time;
64  time.start();
65 
66 #ifdef _WIN32
67 # ifdef CONSOLEIO
68  RedirectIOToConsole();
69 # endif
70 #endif
71 
72  // Handle arguments
73  QString appName = argv[0];
74  QStringList args;
75  for (int i = 1; i < argc; i++ )
76  {
77  args << argv[i];
78  }
79 
80  QString script;
81  KJS::List scriptArgs;
82  bool gui = true;
83 #ifndef QT_ONLY
84  /*
85  #ifdef __GNUC__
86  #warning "KDE Support enabled"
87  #endif
88  */
89  bool kde = true;
90 #else
91  /*
92  #ifdef __GNUC__
93  #warning "KDE Support disabled"
94  #endif
95  */
96 #endif
97 
98  if (argc > 1)
99  {
100  while (!args.isEmpty())
101  {
102  QString arg = args.takeFirst();
103  if (arg.contains('-'))
104  {
105  if ((arg == "--version") || (arg == "-v"))
106  {
107  printf("Qt: %s\n", qVersion());
108 #ifndef QT_ONLY
109  printf("KDE: %s\n", KDE_VERSION_STRING);
110 #endif
111  return 0;
112  }
113  if ((arg == "--exec") || (arg == "-e"))
114  {
115  gui = false;
116  }
117  else if ((arg == "--interactive") || (arg == "-i"))
118  (*KJSEmbed::conout()) << "Interactive";
119 #ifndef QT_ONLY
120  else if ((arg == "-n") || (arg == "--no-kde"))
121  {
122  kde = false;
123  }
124 #endif
125  else
126  {
127  printUsage(appName);
128  return 0;
129  }
130  }
131  else
132  {
133  if (!script.isEmpty())
134  scriptArgs.append(KJS::jsString(arg));
135  else
136  script = arg;
137  }
138  }
139  }
140  else
141  {
142  printUsage(appName);
143  return 0;
144  }
145 
146  // Setup QApplication
147  QCoreApplication *app;
148 
149 #ifndef QT_ONLY
150  if (kde)
151  {
152  KAboutData aboutData( "kjscmd", 0, ki18n("KJSCmd"), "0.2",
153  ki18n(""
154  "Utility for running KJSEmbed scripts \n" ),
155  KAboutData::License_LGPL,
156  ki18n("(C) 2005-2006 The KJSEmbed Authors") );
157 
158  KCmdLineOptions options;
159  options.add("e", ki18n("Execute script without gui support"));
160  options.add("exec", ki18n("Execute script without gui support"));
161  options.add("i", ki18n("start interactive kjs interpreter"));
162  options.add("interactive", ki18n("start interactive kjs interpreter"));
163  options.add("n", ki18n("start without KDE KApplication support."));
164  options.add("no-kde", ki18n("start without KDE KApplication support."));
165  options.add("!+command", ki18n("Script to execute"));
166 
167  KCmdLineArgs::addCmdLineOptions( options ); // Add our own options.
168  KCmdLineArgs::init( argc, argv, &aboutData );
169 
170  app = new KApplication(gui);
171  }
172  else
173 #endif
174  if (gui)
175  {
176  qDebug("no KDE");
177  app = new QApplication( argc, argv );
178  dynamic_cast<QApplication*>(app)->connect( app, SIGNAL(lastWindowClosed()), SLOT(quit()) );
179  }
180  else
181  {
182  qDebug("no GUI");
183  app = new QCoreApplication(argc, argv);
184  }
185  qDebug(" New %s %dms", app->metaObject()->className(), time.elapsed());
186 
187  app->setApplicationName( appName );
188 
189  // Setup Interpreter
190  time.restart();
191  Engine kernel;
192  qDebug(" New engine %dms", time.elapsed());
193  time.restart();
194 
195  KJS::Interpreter *js = kernel.interpreter();
196  js->setShouldPrintExceptions(true);
197  KJS::ExecState *exec = js->globalExec();
198 
199  // Publish bindings
200  KJS::JSObject *appObject = kernel.addObject( app, "Application" );
201  KJS::JSObject *argObject = js->builtinArray()->construct( exec, scriptArgs );
202  appObject->put( exec, "args", argObject );
203  Engine::ExitStatus result = Engine::Failure;
204 
205  if (!script.isEmpty())
206  {
207  result = kernel.runFile(toUString(script));
208  }
209  else // exec shell
210  {
211  result = kernel.runFile( ":/console.js" );
212  }
213 
214  if ( result != Engine::Success )
215  {
216  KJS::Completion jsres = kernel.completion();
217  (*KJSEmbed::conerr()) << toQString(jsres.value()->toString(exec)) << endl;
218  }
219  return (int)result;
220 }
221 
KJSEmbed::Engine::addObject
KJS::JSObject * addObject(QObject *obj, const KJS::UString &name=KJS::UString()) const
publishes a QObject to the global context of the javascript interpereter.
Definition: kjsembed.cpp:190
KJSEmbed::Engine::Failure
Definition: kjsembed.h:65
QCoreApplication
QApplication
QObject::metaObject
virtual const QMetaObject * metaObject() const
main
int main(int argc, char **argv)
Definition: kjscmd.cpp:61
KJSEmbed::Engine::ExitStatus
ExitStatus
Status codes for script execution.
Definition: kjsembed.h:65
KJSEmbed::Engine::interpreter
KJS::Interpreter * interpreter() const
Returns the current interpreter.
Definition: kjsembed.cpp:200
QTime
KJSEmbed::Engine::completion
KJS::Completion completion() const
Returns the Completion object for the last script executed.
Definition: kjsembed.cpp:195
KJSEmbed::Engine::runFile
ExitStatus runFile(const KJS::UString &file)
Execute the file with the specified name using the current interpreter.
Definition: kjsembed.cpp:233
QTime::elapsed
int elapsed() const
QList::isEmpty
bool isEmpty() const
QString::isEmpty
bool isEmpty() const
BrushNS::result
result
Definition: brush.cpp:48
QTime::restart
int restart()
KJSEmbed::conout
KJSEMBED_EXPORT QTextStream * conout()
Definition: kjseglobal.cpp:130
QString
QStringList
KJSEmbed::Engine::Success
Definition: kjsembed.h:65
QString::contains
bool contains(QChar ch, Qt::CaseSensitivity cs) const
QMetaObject::className
const char * className() const
kjseglobal.h
QList::takeFirst
T takeFirst()
KJSEmbed::Engine
The main interface for running embedded Javascript.
Definition: kjsembed.h:58
KJS::jsString
KJS::JSCell * jsString(const QString &s)
Definition: kjseglobal.h:73
List
Definition: variant_binding.cpp:130
KJSEmbed::toUString
KJS::UString toUString(const QString &qs)
Definition: kjseglobal.h:66
QTime::start
void start()
kjsembed.h
KJSEmbed::conerr
KJSEMBED_EXPORT QTextStream * conerr()
Definition: kjseglobal.cpp:143
printUsage
void printUsage(QString appName)
Definition: kjscmd.cpp:45
QCoreApplication::setApplicationName
void setApplicationName(const QString &application)
KJSEmbed::toQString
QString toQString(const KJS::UString &u)
Definition: kjseglobal.h:58
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:22:53 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kjsembed

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

kdelibs API Reference

Skip menu "kdelibs API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal