Kross
main.cpp
Go to the documentation of this file.
00001 /*************************************************************************** 00002 * main.cpp 00003 * This file is part of the KDE project 00004 * copyright (C)2006 by Sebastian Sauer (mail@dipe.org) 00005 * 00006 * This program is free software; you can redistribute it and/or 00007 * modify it under the terms of the GNU Library General Public 00008 * License as published by the Free Software Foundation; either 00009 * version 2 of the License, or (at your option) any later version. 00010 * This program is distributed in the hope that it will be useful, 00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 * Library General Public License for more details. 00014 * You should have received a copy of the GNU Library General Public License 00015 * along with this program; see the file COPYING. If not, write to 00016 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00017 * Boston, MA 02110-1301, USA. 00018 ***************************************************************************/ 00019 00020 // for std namespace 00021 #include <iostream> 00022 00023 // Qt 00024 00025 #include <QtCore/QFile> 00026 00027 // KDE 00028 #include <kapplication.h> 00029 #include <kcmdlineargs.h> 00030 #include <kaboutdata.h> 00031 #include <kurl.h> 00032 00033 // Kross 00034 #include "../core/manager.h" 00035 #include "../core/action.h" 00036 #include "../core/interpreter.h" 00037 00038 #define ERROR_OK 0 00039 #define ERROR_HELP -1 00040 #define ERROR_NOSUCHFILE -2 00041 #define ERROR_OPENFAILED -3 00042 #define ERROR_NOINTERPRETER -4 00043 #define ERROR_EXCEPTION -6 00044 00045 KApplication* app = 0; 00046 00047 int runScriptFile(const QString& scriptfile) 00048 { 00049 // Read the scriptfile 00050 QFile f(scriptfile); 00051 if(! f.exists()) { 00052 std::cerr << "No such scriptfile: " << scriptfile.toLatin1().data() << std::endl; 00053 return ERROR_NOSUCHFILE; 00054 } 00055 if(! f.open(QIODevice::ReadOnly)) { 00056 std::cerr << "Failed to open scriptfile: " << scriptfile.toLatin1().data() << std::endl; 00057 return ERROR_OPENFAILED; 00058 } 00059 QByteArray scriptcode = f.readAll(); 00060 f.close(); 00061 00062 // Determinate the matching interpreter 00063 Kross::InterpreterInfo* interpreterinfo = Kross::Manager::self().interpreterInfo( Kross::Manager::self().interpreternameForFile(scriptfile) ); 00064 if(! interpreterinfo) { 00065 std::cerr << "No interpreter for file: " << scriptfile.toLatin1().data() << std::endl; 00066 return ERROR_NOINTERPRETER; 00067 } 00068 00069 // First we need a Action and fill it. 00070 Kross::Action* action = new Kross::Action(0 /*no parent*/, KUrl(scriptfile)); 00071 action->setInterpreter( interpreterinfo->interpreterName() ); 00072 action->setCode(scriptcode); 00073 00074 // Now execute the Action. 00075 action->trigger(); 00076 00077 if(action->hadError()) { 00078 // We had an exception. 00079 std::cerr << QString("%2\n%1").arg(action->errorTrace()).arg(action->errorMessage()).toLatin1().data() << std::endl; 00080 delete action; 00081 return ERROR_EXCEPTION; 00082 } 00083 00084 delete action; 00085 return ERROR_OK; 00086 } 00087 00088 int main(int argc, char **argv) 00089 { 00090 int result = ERROR_OK; 00091 00092 KAboutData about("kross", 00093 "kdelibs4", 00094 ki18n("Kross"), 00095 "0.1", 00096 ki18n("KDE application to run Kross scripts."), 00097 KAboutData::License_LGPL, 00098 ki18n("(C) 2006 Sebastian Sauer"), 00099 ki18n("Run Kross scripts."), 00100 "http://kross.dipe.org", 00101 "kross@dipe.org"); 00102 about.addAuthor(ki18n("Sebastian Sauer"), ki18n("Author"), "mail@dipe.org"); 00103 00104 // Initialize command line args 00105 KCmdLineArgs::init(argc, argv, &about); 00106 // Tell which options are supported and parse them. 00107 KCmdLineOptions options; 00108 options.add("+file", ki18n("Scriptfile")); 00109 00110 KCmdLineArgs::addCmdLineOptions(options); 00111 KCmdLineArgs *args = KCmdLineArgs::parsedArgs(); 00112 00113 // If no options are defined. 00114 if(args->count() < 1) { 00115 std::cout << "Syntax: " << KCmdLineArgs::appName().toLocal8Bit().constData() << " scriptfile1 [scriptfile2] [scriptfile3] ..." << std::endl; 00116 return ERROR_HELP; 00117 } 00118 00119 // Create KApplication instance. 00120 app = new KApplication( /* GUIenabled */ true ); 00121 00122 // Each argument is a scriptfile to open 00123 for(int i = 0; i < args->count(); i++) { 00124 result = runScriptFile(args->arg(i)); 00125 if(result != ERROR_OK) 00126 break; 00127 } 00128 00129 // Free the KApplication instance and exit the program. 00130 delete app; 00131 Kross::Manager::self().deleteModules(); 00132 return result; 00133 }
KDE 4.6 API Reference