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

Kross

  • sources
  • kde-4.14
  • kdelibs
  • kross
  • qts
qts/main.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * main.cpp
3  * This file is part of the KDE project
4  * copyright (C)2006 by Sebastian Sauer (mail@dipe.org)
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Library General Public License for more details.
14  * You should have received a copy of the GNU Library General Public License
15  * along with this program; see the file COPYING. If not, write to
16  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  ***************************************************************************/
19 
20 #include <QFile>
21 #include <QScriptEngine>
22 #include <QLibraryInfo>
23 #include <QDebug>
24 
25 #include <kapplication.h>
26 #include <kcmdlineargs.h>
27 #include <kaboutdata.h>
28 // #include <kurl.h>
29 
30 KApplication* app = 0;
31 
32 bool runScriptFile(QScriptEngine* engine, const QString& scriptfile)
33 {
34  // Read the scriptfile
35  QFile f(scriptfile);
36  if(! f.exists()) {
37  qWarning() << "No such scriptfile:" << scriptfile;
38  return false;
39  }
40  if(! f.open(QIODevice::ReadOnly)) {
41  qWarning() << "Failed to open scriptfile:" << scriptfile;
42  return false;
43  }
44  QByteArray scriptcode = f.readAll();
45  f.close();
46 
47  // Execute the javascript code.
48  qDebug() << "Execute scriptfile:" << scriptfile;
49  QScriptValue v = engine->evaluate(scriptcode);
50  qDebug() << "Execute done. Result:" << v.toString();
51 
52  return true;
53 }
54 
55 int main(int argc, char **argv)
56 {
57  KAboutData about("kross",0,ki18n("Kross"),"0.1",
58  ki18n("KDE application to run Kross scripts."),
59  KAboutData::License_LGPL,
60  ki18n("(C) 2006 Sebastian Sauer"),
61  ki18n("Run Kross scripts."),
62  "http://kross.dipe.org","kross@dipe.org");
63  about.addAuthor(ki18n("Sebastian Sauer"), ki18n("Author"), "mail@dipe.org");
64 
65  // Initialize command line args
66  KCmdLineArgs::init(argc, argv, &about);
67  // Tell which options are supported and parse them.
68  KCmdLineOptions options;
69  options.add("+file", ki18n("Scriptfile"));
70  KCmdLineArgs::addCmdLineOptions(options);
71  KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
72 
73  QStringList files;
74  for(int i = 0; i < args->count(); ++i)
75  files << args->arg(i);
76 
77  // If no options are defined.
78  if(files.count() < 1) {
79  qWarning() << "Syntax:" << KCmdLineArgs::appName() << "scriptfile1 [scriptfile2] [scriptfile3] ...";
80  return -1;
81  }
82 
83 
84  app = new KApplication( /* GUIenabled */ true );
85  QScriptEngine* engine = new QScriptEngine();
86  engine->installTranslatorFunctions();
87  QScriptValue global = engine->globalObject();
88 
89  //qDebug()<<"QLibraryInfo::PluginsPath="<<QLibraryInfo::location(QLibraryInfo::PluginsPath);
90  //app->addLibraryPath("/home/kde4/kde4/lib/kde4/");
91 
92  engine->importExtension("kross").toString();
93 
94  foreach(const QString &file, files)
95  runScriptFile(engine, file);
96 
97  delete engine;
98  delete app;
99  return 0;
100 }
KApplication
KCmdLineArgs::addCmdLineOptions
static void addCmdLineOptions(const KCmdLineOptions &options, const KLocalizedString &name=KLocalizedString(), const QByteArray &id=QByteArray(), const QByteArray &afterId=QByteArray())
QScriptEngine::importExtension
QScriptValue importExtension(const QString &extension)
KAboutData::addAuthor
KAboutData & addAuthor(const KLocalizedString &name, const KLocalizedString &task=KLocalizedString(), const QByteArray &emailAddress=QByteArray(), const QByteArray &webAddress=QByteArray())
kapplication.h
KCmdLineOptions::add
KCmdLineOptions & add(const QByteArray &name, const KLocalizedString &description=KLocalizedString(), const QByteArray &defaultValue=QByteArray())
QByteArray
ki18n
KLocalizedString ki18n(const char *msg)
runScriptFile
bool runScriptFile(QScriptEngine *engine, const QString &scriptfile)
Definition: qts/main.cpp:32
KCmdLineArgs::parsedArgs
static KCmdLineArgs * parsedArgs(const QByteArray &id=QByteArray())
QScriptValue
KCmdLineArgs
QScriptEngine::installTranslatorFunctions
void installTranslatorFunctions(const QScriptValue &object)
QScriptEngine::evaluate
QScriptValue evaluate(const QString &program, const QString &fileName, int lineNumber)
QFile::exists
bool exists() const
KCmdLineArgs::arg
QString arg(int n) const
QScriptValue::toString
QString toString() const
QFile
KCmdLineArgs::count
int count() const
app
KApplication * app
Definition: qts/main.cpp:30
QScriptEngine
QList::count
int count(const T &value) const
KCmdLineArgs::appName
static QString appName()
kcmdlineargs.h
main
int main(int argc, char **argv)
Definition: qts/main.cpp:55
QIODevice::readAll
QByteArray readAll()
KAboutData::License_LGPL
KAboutData
QString
QFile::open
virtual bool open(QFlags< QIODevice::OpenModeFlag > mode)
QStringList
QFile::close
virtual void close()
KCmdLineArgs::init
static void init(int argc, char **argv, const QByteArray &appname, const QByteArray &catalog, const KLocalizedString &programName, const QByteArray &version, const KLocalizedString &description=KLocalizedString(), StdCmdLineArgs stdargs=StdCmdLineArgs(CmdLineArgQt|CmdLineArgKDE))
kaboutdata.h
QScriptEngine::globalObject
QScriptValue globalObject() const
KCmdLineOptions
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:24:44 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

Kross

Skip menu "Kross"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

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