File indexing completed on 2024-03-24 15:37:23

0001 /***************************************************************************
0002  * main.cpp
0003  * This file is part of the KDE project
0004  * copyright (C)2006 by Sebastian Sauer (mail@dipe.org)
0005  *
0006  * This program is free software; you can redistribute it and/or
0007  * modify it under the terms of the GNU Library General Public
0008  * License as published by the Free Software Foundation; either
0009  * version 2 of the License, or (at your option) any later version.
0010  * This program is distributed in the hope that it will be useful,
0011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0013  * Library General Public License for more details.
0014  * You should have received a copy of the GNU Library General Public License
0015  * along with this program; see the file COPYING.  If not, write to
0016  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017  * Boston, MA 02110-1301, USA.
0018  ***************************************************************************/
0019 
0020 #include <QFile>
0021 #include <QStringList>
0022 #include <QScriptEngine>
0023 #include <QLibraryInfo>
0024 #include <QDebug>
0025 #include <QApplication>
0026 
0027 #include <qcommandlineparser.h>
0028 #include <qcommandlineoption.h>
0029 
0030 bool runScriptFile(QScriptEngine *engine, const QString &scriptfile)
0031 {
0032     // Read the scriptfile
0033     QFile f(scriptfile);
0034     if (! f.exists()) {
0035         qWarning() << "No such scriptfile:" << scriptfile;
0036         return false;
0037     }
0038     if (! f.open(QIODevice::ReadOnly)) {
0039         qWarning() << "Failed to open scriptfile:" << scriptfile;
0040         return false;
0041     }
0042     QByteArray scriptcode = f.readAll();
0043     f.close();
0044 
0045     // Execute the javascript code.
0046     qDebug() << "Execute scriptfile:" << scriptfile;
0047     QScriptValue v = engine->evaluate(scriptcode);
0048     qDebug() << "Execute done. Result:" << v.toString();
0049 
0050     return true;
0051 }
0052 
0053 int main(int argc, char **argv)
0054 {
0055     QApplication *app = new QApplication(argc, argv);
0056     app->setApplicationName("kross");
0057     app->setApplicationVersion("0.1");
0058     app->setOrganizationDomain("dipe.org");
0059 
0060     QScriptEngine *engine = new QScriptEngine();
0061     engine->installTranslatorFunctions();
0062 
0063     /*K4AboutData about("kross",0,ki18n("Kross"),"0.1",
0064                      ki18n("KDE application to run Kross scripts."),
0065                      K4AboutData::License_LGPL,
0066                      ki18n("(C) 2006 Sebastian Sauer"),
0067                      ki18n("Run Kross scripts."),
0068                      "http://kross.dipe.org","kross@dipe.org");*/
0069     //about.addAuthor(ki18n("Sebastian Sauer"), ki18n("Author"), "mail@dipe.org");
0070 
0071     // Initialize command line args
0072     // Tell which options are supported and parse them.
0073     QCommandLineParser parser;
0074     parser.setApplicationDescription(QCoreApplication::translate("main", "KDE application to run Kross scripts."));
0075     parser.addHelpOption();
0076     parser.addOption(QCommandLineOption(QStringList() << "+file", QCoreApplication::translate("main", "Scriptfile")));
0077     parser.process(*app);
0078 
0079     const QStringList args = parser.positionalArguments();
0080     // If no options are defined.
0081     if (args.count() < 1) {
0082         parser.showHelp();
0083         //std::cout << "Syntax: " << "kross" << " scriptfile1 [scriptfile2] [scriptfile3] ..." << std::endl;
0084         return -1;
0085     }
0086 
0087     QScriptValue global = engine->globalObject();
0088 
0089     //qDebug()<<"QLibraryInfo::PluginsPath="<<QLibraryInfo::location(QLibraryInfo::PluginsPath);
0090 
0091     engine->importExtension("kross").toString();
0092 
0093     foreach (const QString &file, args) {
0094         runScriptFile(engine, file);
0095     }
0096 
0097     delete engine;
0098     delete app;
0099     return 0;
0100 }