• 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
  • console
console/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 // for std namespace
21 #include <iostream>
22 
23 // Qt
24 
25 #include <QtCore/QFile>
26 
27 // KDE
28 #include <kapplication.h>
29 #include <kcmdlineargs.h>
30 #include <kaboutdata.h>
31 #include <kurl.h>
32 
33 // Kross
34 #include "../core/manager.h"
35 #include "../core/action.h"
36 #include "../core/interpreter.h"
37 
38 #define ERROR_OK 0
39 #define ERROR_HELP -1
40 #define ERROR_NOSUCHFILE -2
41 #define ERROR_OPENFAILED -3
42 #define ERROR_NOINTERPRETER -4
43 #define ERROR_EXCEPTION -6
44 
45 KApplication* app = 0;
46 
47 int runScriptFile(const QString& scriptfile)
48 {
49  // Read the scriptfile
50  QFile f(scriptfile);
51  if(! f.exists()) {
52  std::cerr << "No such scriptfile: " << scriptfile.toLatin1().data() << std::endl;
53  return ERROR_NOSUCHFILE;
54  }
55  if(! f.open(QIODevice::ReadOnly)) {
56  std::cerr << "Failed to open scriptfile: " << scriptfile.toLatin1().data() << std::endl;
57  return ERROR_OPENFAILED;
58  }
59  QByteArray scriptcode = f.readAll();
60  f.close();
61 
62  // Determinate the matching interpreter
63  Kross::InterpreterInfo* interpreterinfo = Kross::Manager::self().interpreterInfo( Kross::Manager::self().interpreternameForFile(scriptfile) );
64  if(! interpreterinfo) {
65  std::cerr << "No interpreter for file: " << scriptfile.toLatin1().data() << std::endl;
66  return ERROR_NOINTERPRETER;
67  }
68 
69  // First we need a Action and fill it.
70  Kross::Action* action = new Kross::Action(0 /*no parent*/, KUrl(scriptfile));
71  action->setInterpreter( interpreterinfo->interpreterName() );
72  action->setCode(scriptcode);
73 
74  // Now execute the Action.
75  action->trigger();
76 
77  if(action->hadError()) {
78  // We had an exception.
79  std::cerr << QString("%2\n%1").arg(action->errorTrace()).arg(action->errorMessage()).toLatin1().data() << std::endl;
80  delete action;
81  return ERROR_EXCEPTION;
82  }
83 
84  delete action;
85  return ERROR_OK;
86 }
87 
88 int main(int argc, char **argv)
89 {
90  int result = ERROR_OK;
91 
92  KAboutData about("kross",
93  "kdelibs4",
94  ki18n("Kross"),
95  "0.1",
96  ki18n("KDE application to run Kross scripts."),
97  KAboutData::License_LGPL,
98  ki18n("(C) 2006 Sebastian Sauer"),
99  ki18n("Run Kross scripts."),
100  "http://kross.dipe.org",
101  "kross@dipe.org");
102  about.addAuthor(ki18n("Sebastian Sauer"), ki18n("Author"), "mail@dipe.org");
103 
104  // Initialize command line args
105  KCmdLineArgs::init(argc, argv, &about);
106  // Tell which options are supported and parse them.
107  KCmdLineOptions options;
108  options.add("+file", ki18n("Scriptfile"));
109 
110  KCmdLineArgs::addCmdLineOptions(options);
111  KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
112 
113  // If no options are defined.
114  if(args->count() < 1) {
115  std::cout << "Syntax: " << KCmdLineArgs::appName().toLocal8Bit().constData() << " scriptfile1 [scriptfile2] [scriptfile3] ..." << std::endl;
116  return ERROR_HELP;
117  }
118 
119  // Create KApplication instance.
120  app = new KApplication( /* GUIenabled */ true );
121 
122  // Each argument is a scriptfile to open
123  for(int i = 0; i < args->count(); i++) {
124  result = runScriptFile(args->arg(i));
125  if(result != ERROR_OK)
126  break;
127  }
128 
129  // Free the KApplication instance and exit the program.
130  delete app;
131  Kross::Manager::self().deleteModules();
132  return result;
133 }
Kross::ErrorInterface::hadError
bool hadError() const
Definition: errorinterface.h:48
KApplication
KCmdLineArgs::addCmdLineOptions
static void addCmdLineOptions(const KCmdLineOptions &options, const KLocalizedString &name=KLocalizedString(), const QByteArray &id=QByteArray(), const QByteArray &afterId=QByteArray())
KAboutData::addAuthor
KAboutData & addAuthor(const KLocalizedString &name, const KLocalizedString &task=KLocalizedString(), const QByteArray &emailAddress=QByteArray(), const QByteArray &webAddress=QByteArray())
kapplication.h
kurl.h
ERROR_HELP
#define ERROR_HELP
Definition: console/main.cpp:39
KCmdLineOptions::add
KCmdLineOptions & add(const QByteArray &name, const KLocalizedString &description=KLocalizedString(), const QByteArray &defaultValue=QByteArray())
QByteArray
ki18n
KLocalizedString ki18n(const char *msg)
KCmdLineArgs::parsedArgs
static KCmdLineArgs * parsedArgs(const QByteArray &id=QByteArray())
KCmdLineArgs
ERROR_OPENFAILED
#define ERROR_OPENFAILED
Definition: console/main.cpp:41
Kross::Manager::interpreterInfo
InterpreterInfo * interpreterInfo(const QString &interpretername) const
Definition: manager.cpp:250
QFile::exists
bool exists() const
KCmdLineArgs::arg
QString arg(int n) const
QFile
KUrl
KCmdLineArgs::count
int count() const
QAction::trigger
void trigger()
Kross::InterpreterInfo
The InterpreterInfo class provides abstract information about a Interpreter before the interpreter-ba...
Definition: core/interpreter.h:43
ERROR_NOINTERPRETER
#define ERROR_NOINTERPRETER
Definition: console/main.cpp:42
Kross::ErrorInterface::errorMessage
const QString errorMessage() const
Definition: errorinterface.h:53
app
KApplication * app
Definition: console/main.cpp:45
Kross::ErrorInterface::errorTrace
const QString errorTrace() const
Definition: errorinterface.h:58
KCmdLineArgs::appName
static QString appName()
kcmdlineargs.h
QByteArray::constData
const char * constData() const
QIODevice::readAll
QByteArray readAll()
KAboutData::License_LGPL
KAboutData
runScriptFile
int runScriptFile(const QString &scriptfile)
Definition: console/main.cpp:47
QString
QFile::open
virtual bool open(QFlags< QIODevice::OpenModeFlag > mode)
QString::toLocal8Bit
QByteArray toLocal8Bit() const
QFile::close
virtual void close()
main
int main(int argc, char **argv)
Definition: console/main.cpp:88
QString::toLatin1
QByteArray toLatin1() const
Kross::Manager::self
static Manager & self()
Return the Manager instance.
Definition: manager.cpp:73
ERROR_EXCEPTION
#define ERROR_EXCEPTION
Definition: console/main.cpp:43
Kross::Action::setInterpreter
void setInterpreter(const QString &interpretername)
Set the name of the interpreter (javascript, python or ruby).
Definition: action.cpp:338
Kross::Action::setCode
void setCode(const QByteArray &code)
Set the scriptcode code this Action should execute.
Definition: action.cpp:323
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))
QByteArray::data
char * data()
Kross::InterpreterInfo::interpreterName
const QString interpreterName() const
Definition: core/interpreter.cpp:76
Kross::Action
The Action class is an abstract container to deal with scripts like a single standalone script file...
Definition: action.h:94
kaboutdata.h
ERROR_OK
#define ERROR_OK
Definition: console/main.cpp:38
KCmdLineOptions
QString::data
QChar * data()
QString::arg
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const
ERROR_NOSUCHFILE
#define ERROR_NOSUCHFILE
Definition: console/main.cpp:40
Kross::Manager::deleteModules
void deleteModules()
External modules are dynamically loadable and are normally deleted when the kross library is unloaded...
Definition: manager.cpp:364
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