scriptguiclient.cpp

00001 /***************************************************************************
00002  * scriptguiclient.cpp
00003  * This file is part of the KDE project
00004  * copyright (C) 2005 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 #include "scriptguiclient.h"
00021 #include "manager.h"
00022 #include "../api/interpreter.h"
00023 #include "wdgscriptsmanager.h"
00024 
00025 #include <kapplication.h>
00026 #include <kpopupmenu.h>
00027 #include <kstandarddirs.h>
00028 #include <kmimetype.h>
00029 #include <kmessagebox.h>
00030 #include <kfiledialog.h>
00031 #include <klocale.h>
00032 #include <kurl.h>
00033 #include <ktar.h>
00034 #include <kstandarddirs.h>
00035 #include <kdebug.h>
00036 
00037 #include <kio/netaccess.h>
00038 
00039 using namespace Kross::Api;
00040 
00041 namespace Kross { namespace Api {
00042 
00043     /// @internal
00044     class ScriptGUIClientPrivate
00045     {
00046         public:
00047             KXMLGUIClient* guiclient;
00048             QWidget* parent;
00049 
00050             QMap<QString, ScriptActionCollection*> collections;
00051     };
00052 
00053 }}
00054 
00055 ScriptGUIClient::ScriptGUIClient(KXMLGUIClient* guiclient, QWidget* parent)
00056     : QObject( parent )
00057     , KXMLGUIClient( guiclient )
00058     , d( new ScriptGUIClientPrivate() ) // initialize d-pointer class
00059 {
00060     kdDebug() << QString("ScriptGUIClient::ScriptGUIClient() Ctor") << endl;
00061 
00062     d->guiclient = guiclient;
00063     d->parent = parent;
00064 
00065     setInstance( ScriptGUIClient::instance() );
00066 
00067     // action to execute a scriptfile.
00068     new KAction(i18n("Execute Script File..."), 0, 0, this, SLOT(executeScriptFile()), actionCollection(), "executescriptfile");
00069 
00070     // acion to show the ScriptManagerGUI dialog.
00071     new KAction(i18n("Scripts Manager..."), 0, 0, this, SLOT(showScriptManager()), actionCollection(), "configurescripts");
00072 
00073     // The predefined ScriptActionCollection's this ScriptGUIClient provides.
00074     d->collections.replace("installedscripts",
00075         new ScriptActionCollection(i18n("Scripts"), actionCollection(), "installedscripts") );
00076     d->collections.replace("loadedscripts",
00077         new ScriptActionCollection(i18n("Loaded"), actionCollection(), "loadedscripts") );
00078     d->collections.replace("executedscripts",
00079         new ScriptActionCollection(i18n("History"), actionCollection(), "executedscripts") );
00080 
00081     reloadInstalledScripts();
00082 }
00083 
00084 ScriptGUIClient::~ScriptGUIClient()
00085 {
00086     kdDebug() << QString("ScriptGUIClient::~ScriptGUIClient() Dtor") << endl;
00087     for(QMap<QString, ScriptActionCollection*>::Iterator it = d->collections.begin(); it != d->collections.end(); ++it)
00088         delete it.data();
00089     delete d;
00090 }
00091 
00092 bool ScriptGUIClient::hasActionCollection(const QString& name)
00093 {
00094     return d->collections.contains(name);
00095 }
00096 
00097 ScriptActionCollection* ScriptGUIClient::getActionCollection(const QString& name)
00098 {
00099     return d->collections[name];
00100 }
00101 
00102 QMap<QString, ScriptActionCollection*> ScriptGUIClient::getActionCollections()
00103 {
00104     return d->collections;
00105 }
00106 
00107 void ScriptGUIClient::addActionCollection(const QString& name, ScriptActionCollection* collection)
00108 {
00109     removeActionCollection(name);
00110     d->collections.replace(name, collection);
00111 }
00112 
00113 bool ScriptGUIClient::removeActionCollection(const QString& name)
00114 {
00115     if(d->collections.contains(name)) {
00116         ScriptActionCollection* c = d->collections[name];
00117         d->collections.remove(name);
00118         delete c;
00119         return true;
00120     }
00121     return false;
00122 }
00123 
00124 void ScriptGUIClient::reloadInstalledScripts()
00125 {
00126     ScriptActionCollection* installedcollection = d->collections["installedscripts"];
00127     if(installedcollection)
00128         installedcollection->clear();
00129 
00130     QStringList files = KGlobal::dirs()->findAllResources("appdata", "scripts/*/*.rc");
00131     //files.sort();
00132     for(QStringList::iterator it = files.begin(); it != files.end(); ++it)
00133         loadScriptConfig(*it);
00134 }
00135 
00136 bool ScriptGUIClient::installScriptPackage(const QString& scriptpackagefile)
00137 {
00138     kdDebug() << QString("Install script package: %1").arg(scriptpackagefile) << endl;
00139     KTar archive( scriptpackagefile );
00140     if(! archive.open(IO_ReadOnly)) {
00141         KMessageBox::sorry(0, i18n("Could not read the package \"%1\".").arg(scriptpackagefile));
00142         return false;
00143     }
00144 
00145     QString destination = KGlobal::dirs()->saveLocation("appdata", "scripts", true);
00146     if(destination.isNull()) {
00147         kdWarning() << "ScriptGUIClient::installScriptPackage() Failed to determinate location where the scriptpackage should be installed to!" << endl;
00148         return false;
00149     }
00150 
00151     QString packagename = QFileInfo(scriptpackagefile).baseName();
00152     destination += packagename; // add the packagename to the name of the destination-directory.
00153 
00154     if( QDir(destination).exists() ) {
00155         if( KMessageBox::warningContinueCancel(0,
00156             i18n("A script package with the name \"%1\" already exists. Replace this package?" ).arg(packagename),
00157             i18n("Replace")) != KMessageBox::Continue )
00158                 return false;
00159 
00160         if(! KIO::NetAccess::del(destination, 0) ) {
00161             KMessageBox::sorry(0, i18n("Could not uninstall this script package. You may not have sufficient permissions to delete the folder \"%1\".").arg(destination));
00162             return false;
00163         }
00164     }
00165 
00166     const KArchiveDirectory* archivedir = archive.directory();
00167     archivedir->copyTo(destination, true);
00168 
00169     reloadInstalledScripts();
00170     return true;
00171 }
00172 
00173 bool ScriptGUIClient::uninstallScriptPackage(const QString& scriptpackagepath)
00174 {
00175     if(! KIO::NetAccess::del(scriptpackagepath, 0) ) {
00176         KMessageBox::sorry(0, i18n("Could not uninstall this script package. You may not have sufficient permissions to delete the folder \"%1\".").arg(scriptpackagepath));
00177         return false;
00178     }
00179     reloadInstalledScripts();
00180     return true;
00181 }
00182 
00183 bool ScriptGUIClient::loadScriptConfig(const QString& scriptconfigfile)
00184 {
00185     kdDebug() << "ScriptGUIClient::loadScriptConfig file=" << scriptconfigfile << endl;
00186 
00187     QDomDocument domdoc;
00188     QFile file(scriptconfigfile);
00189     if(! file.open(IO_ReadOnly)) {
00190         kdWarning() << "ScriptGUIClient::loadScriptConfig(): Failed to read scriptconfigfile: " << scriptconfigfile << endl;
00191         return false;
00192     }
00193     bool ok = domdoc.setContent(&file);
00194     file.close();
00195     if(! ok) {
00196         kdWarning() << "ScriptGUIClient::loadScriptConfig(): Failed to parse scriptconfigfile: " << scriptconfigfile << endl;
00197         return false;
00198     }
00199 
00200     ScriptActionCollection* installedcollection = d->collections["installedscripts"];
00201 
00202     // walk throuh the list of Scripts-elements.
00203     QDomNodeList nodelist = domdoc.elementsByTagName("ScriptAction");
00204     uint nodelistcount = nodelist.count();
00205     for(uint i = 0; i < nodelistcount; i++) {
00206         ScriptAction::Ptr action = new ScriptAction(scriptconfigfile, nodelist.item(i).toElement());
00207 
00208         if(installedcollection)
00209             installedcollection->attach( action );
00210 
00211         connect(action.data(), SIGNAL( failed(const QString&, const QString&) ),
00212                 this, SLOT( executionFailed(const QString&, const QString&) ));
00213         connect(action.data(), SIGNAL( success() ),
00214                 this, SLOT( successfullyExecuted() ));
00215     }
00216 
00217     emit collectionChanged(installedcollection);
00218     return true;
00219 }
00220 
00221 void ScriptGUIClient::successfullyExecuted()
00222 {
00223     const ScriptAction* action = dynamic_cast< const ScriptAction* >( QObject::sender() );
00224     if(action) {
00225         emit executionFinished(action);
00226         ScriptActionCollection* executedcollection = d->collections["executedscripts"];
00227         if(executedcollection) {
00228             ScriptAction* actionptr = const_cast< ScriptAction* >( action );
00229             executedcollection->detach(actionptr);
00230             executedcollection->attach(actionptr);
00231 
00232             emit collectionChanged(executedcollection);
00233         }
00234     }
00235 }
00236 
00237 void ScriptGUIClient::executionFailed(const QString& errormessage, const QString& tracedetails)
00238 {
00239     const ScriptAction* action = dynamic_cast< const ScriptAction* >( QObject::sender() );
00240     if(action)
00241         emit executionFinished(action);
00242     if(tracedetails.isEmpty())
00243         KMessageBox::error(0, errormessage);
00244     else
00245         KMessageBox::detailedError(0, errormessage, tracedetails);
00246 }
00247 
00248 KURL ScriptGUIClient::openScriptFile(const QString& caption)
00249 {
00250     QStringList mimetypes;
00251     QMap<QString, InterpreterInfo*> infos = Manager::scriptManager()->getInterpreterInfos();
00252     for(QMap<QString, InterpreterInfo*>::Iterator it = infos.begin(); it != infos.end(); ++it)
00253         mimetypes.append( it.data()->getMimeTypes().join(" ").stripWhiteSpace() );
00254 
00255     KFileDialog* filedialog = new KFileDialog(
00256         QString::null, // startdir
00257         mimetypes.join(" "), // filter
00258         0, // parent widget
00259         "ScriptGUIClientFileDialog", // name
00260         true // modal
00261     );
00262     if(! caption.isNull())
00263         filedialog->setCaption(caption);
00264     if( filedialog->exec() )
00265         return filedialog->selectedURL();
00266     return KURL();
00267 }
00268 
00269 bool ScriptGUIClient::loadScriptFile()
00270 {
00271     KURL url = openScriptFile( i18n("Load Script File") );
00272     if(url.isValid()) {
00273         ScriptActionCollection* loadedcollection = d->collections["loadedscripts"];
00274         if(loadedcollection) {
00275             ScriptAction::Ptr action = new ScriptAction( url.path() );
00276             connect(action.data(), SIGNAL( failed(const QString&, const QString&) ),
00277                     this, SLOT( executionFailed(const QString&, const QString&) ));
00278             connect(action.data(), SIGNAL( success() ),
00279                     this, SLOT( successfullyExecuted() ));
00280 
00281             loadedcollection->detach(action);
00282             loadedcollection->attach(action);
00283             return true;
00284         }
00285     }
00286     return false;
00287 }
00288 
00289 bool ScriptGUIClient::executeScriptFile()
00290 {
00291     KURL url = openScriptFile( i18n("Execute Script File") );
00292     if(url.isValid())
00293         return executeScriptFile( url.path() );
00294     return false;
00295 }
00296 
00297 bool ScriptGUIClient::executeScriptFile(const QString& file)
00298 {
00299     kdDebug() << QString("Kross::Api::ScriptGUIClient::executeScriptFile() file='%1'").arg(file) << endl;
00300 
00301     ScriptAction::Ptr action = new ScriptAction(file);
00302     return executeScriptAction(action);
00303 }
00304 
00305 bool ScriptGUIClient::executeScriptAction(ScriptAction::Ptr action)
00306 {
00307     connect(action.data(), SIGNAL( failed(const QString&, const QString&) ),
00308             this, SLOT( executionFailed(const QString&, const QString&) ));
00309     connect(action.data(), SIGNAL( success() ),
00310             this, SLOT( successfullyExecuted() ));
00311     action->activate();
00312     bool ok = action->hadException();
00313     action->finalize(); // execution is done.
00314     return ok;
00315 }
00316 
00317 void ScriptGUIClient::showScriptManager()
00318 {
00319     KDialogBase* dialog = new KDialogBase(d->parent, "", true, i18n("Scripts Manager"), KDialogBase::Close);
00320     WdgScriptsManager* wsm = new WdgScriptsManager(this, dialog);
00321     dialog->setMainWidget(wsm);
00322     dialog->resize( QSize(360, 320).expandedTo(dialog->minimumSizeHint()) );
00323     dialog->show();
00324 }
00325 
00326 #include "scriptguiclient.moc"

Generated on Thu Feb 9 17:59:11 2006 for Kross by  doxygen 1.4.6