Kross::Api::ScriptGUIClient Class Reference

#include <scriptguiclient.h>

Inheritance diagram for Kross::Api::ScriptGUIClient:

Inheritance graph
[legend]
Collaboration diagram for Kross::Api::ScriptGUIClient:

Collaboration graph
[legend]
List of all members.

Detailed Description

The ScriptGUIClient class provides abstract access to scripting code used to extend an applications functionality.

Definition at line 43 of file scriptguiclient.h.

Public Types

typedef QPtrList< KAction > List
 List of KAction instances.

Public Slots

KURL openScriptFile (const QString &caption=QString::null)
bool loadScriptFile ()
bool executeScriptFile ()
bool executeScriptFile (const QString &file)
bool executeScriptAction (ScriptAction::Ptr action)
void showScriptManager ()

Signals

void collectionChanged (ScriptActionCollection *)
void executionFinished (const Kross::Api::ScriptAction *)
 This signal is emited when the execution of a script is finished.

Public Member Functions

 ScriptGUIClient (KXMLGUIClient *guiclient, QWidget *parent=0)
virtual ~ScriptGUIClient ()
bool hasActionCollection (const QString &name)
ScriptActionCollectiongetActionCollection (const QString &name)
QMap< QString, ScriptActionCollection * > getActionCollections ()
void addActionCollection (const QString &name, ScriptActionCollection *collection)
bool removeActionCollection (const QString &name)
void reloadInstalledScripts ()
bool installScriptPackage (const QString &scriptpackagefile)
bool uninstallScriptPackage (const QString &scriptpackagepath)
bool loadScriptConfig (const QString &scriptconfigfile)
 blockSignals (bool block)
 checkConnectArgs (const char *signal, const QObject *receiver, const char *member)
 child (const char *objName, const char *inheritsClass=0, bool recursiveSearch=TRUE)
 childEvent (QChildEvent *)
 children ()
 className ()
 connect (const QObject *sender, const char *signal, const QObject *receiver, const char *member)
 connect (const QObject *sender, const char *signal, const char *member)
 connectNotify (const char *signal)
 customEvent (QCustomEvent *)
 deleteLater ()
 destroyed ()
 destroyed (QObject *obj)
 disconnect (const QObject *sender, const char *signal, const QObject *receiver, const char *member)
 disconnect (const char *signal=0, const QObject *receiver=0, const char *member=0)
 disconnect (const QObject *receiver, const char *member=0)
 disconnectNotify (const char *signal)
 dumpObjectInfo ()
 dumpObjectTree ()
 event (QEvent *e)
 eventFilter (QObject *watched, QEvent *e)
 highPriority ()
 inherits (const char *clname)
 insertChild (QObject *obj)
 installEventFilter (const QObject *filterObj)
 isA (const char *clname)
 isWidgetType ()
 killTimer (int id)
 killTimers ()
 metaObject ()
 name ()
 name (const char *defaultName)
 normalizeSignalSlot (const char *signalSlot)
 objectTrees ()
 parent ()
 property (const char *name)
 queryList (const char *inheritsClass=0, const char *objName=0, bool regexpMatch=TRUE, bool recursiveSearch=TRUE)
 removeChild (QObject *obj)
 removeEventFilter (const QObject *obj)
 sender ()
 setName (const char *name)
 setProperty (const char *name, const QVariant &value)
 signalsBlocked ()
 startTimer (int interval)
 timerEvent (QTimerEvent *)
 tr (const char *sourceText, const char *comment)
 trUtf8 (const char *sourceText, const char *comment)


Constructor & Destructor Documentation

ScriptGUIClient::ScriptGUIClient KXMLGUIClient *  guiclient,
QWidget parent = 0
[explicit]
 

Constructor.

Parameters:
guiclient The KXMLGUIClient this ScriptGUIClient is a child of.
parent The parent QObject. If defined Qt will handle freeing this ScriptGUIClient instance else the caller has to take care of freeing this instance if not needed any longer.

Definition at line 55 of file scriptguiclient.cpp.

References Kross::Api::ScriptGUIClientPrivate::collections, executeScriptFile(), Kross::Api::ScriptGUIClientPrivate::guiclient, QObject::parent(), Kross::Api::ScriptGUIClientPrivate::parent, reloadInstalledScripts(), QMap::replace(), and showScriptManager().

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 }

ScriptGUIClient::~ScriptGUIClient  )  [virtual]
 

Destructor.

Definition at line 84 of file scriptguiclient.cpp.

References QMap::begin(), Kross::Api::ScriptGUIClientPrivate::collections, and QMap::end().

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 }


Member Function Documentation

void ScriptGUIClient::addActionCollection const QString name,
ScriptActionCollection collection
 

Add a new ScriptActionCollection with the name name to our map of actioncollections.

Definition at line 107 of file scriptguiclient.cpp.

References Kross::Api::ScriptGUIClientPrivate::collections, QObject::name(), removeActionCollection(), and QMap::replace().

00108 {
00109     removeActionCollection(name);
00110     d->collections.replace(name, collection);
00111 }

bool ScriptGUIClient::executeScriptAction ScriptAction::Ptr  action  )  [slot]
 

This method executes the ScriptAction action . Internaly we just call ScriptAction::activate and redirect the success/failed signals to our internal slots.

Definition at line 305 of file scriptguiclient.cpp.

References QObject::connect().

Referenced by executeScriptFile().

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 }

bool ScriptGUIClient::executeScriptFile const QString file  )  [slot]
 

Execute the scriptfile file . Internaly we try to use the defined filename to auto-detect the Interpreter which should be used for the execution.

Definition at line 297 of file scriptguiclient.cpp.

References executeScriptAction().

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 }

bool ScriptGUIClient::executeScriptFile  )  [slot]
 

A KFileDialog will be displayed to let the user choose the scriptfile that should be executed. The executed ScriptAction will be added to the ScriptActionCollection of executed scripts.

Definition at line 289 of file scriptguiclient.cpp.

References openScriptFile().

Referenced by ScriptGUIClient().

00290 {
00291     KURL url = openScriptFile( i18n("Execute Script File") );
00292     if(url.isValid())
00293         return executeScriptFile( url.path() );
00294     return false;
00295 }

ScriptActionCollection * ScriptGUIClient::getActionCollection const QString name  ) 
 

Returns:
the ScriptActionCollection which has the name name or NULL if there exists no such ScriptActionCollection .

Definition at line 97 of file scriptguiclient.cpp.

References Kross::Api::ScriptGUIClientPrivate::collections, and QObject::name().

Referenced by Kross::Api::WdgScriptsManager::slotSelectionChanged(), and Kross::Api::WdgScriptsManager::slotUninstallScript().

00098 {
00099     return d->collections[name];
00100 }

QMap< QString, ScriptActionCollection * > ScriptGUIClient::getActionCollections  ) 
 

Returns:
a map of all avaiable ScriptActionCollection instances this ScriptGUIClient knows about. Per default there are 2 collections avaiable; 1. "installedscripts" The installed collection of scripts. 2. "loadedscripts" The loaded scripts.

Definition at line 102 of file scriptguiclient.cpp.

References Kross::Api::ScriptGUIClientPrivate::collections.

00103 {
00104     return d->collections;
00105 }

bool ScriptGUIClient::hasActionCollection const QString name  ) 
 

Returns:
true if this ScriptGUIClient has a ScriptActionCollection with the name name else false is returned.

Definition at line 92 of file scriptguiclient.cpp.

References Kross::Api::ScriptGUIClientPrivate::collections, QMap::contains(), and QObject::name().

00093 {
00094     return d->collections.contains(name);
00095 }

bool ScriptGUIClient::installScriptPackage const QString scriptpackagefile  ) 
 

Install the packagefile scriptpackagefile . Those packagefile should be a tar.gz-archive which will be extracted and to the users script-directory.

Definition at line 136 of file scriptguiclient.cpp.

Referenced by Kross::Api::WdgScriptsManager::slotInstallScript().

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 }

bool ScriptGUIClient::loadScriptConfig const QString scriptconfigfile  ) 
 

Load the scriptpackage's configurationfile scriptconfigfile and add the defined ScriptAction instances that will be added to the list of installed scripts.

Definition at line 183 of file scriptguiclient.cpp.

References QFile::open().

Referenced by reloadInstalledScripts().

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 }

bool ScriptGUIClient::loadScriptFile  )  [slot]
 

A KFileDialog will be displayed to let the user choose a scriptfile that should be loaded. Those loaded ScriptAction will be added to the ScriptActionCollection of loaded scripts.

Definition at line 269 of file scriptguiclient.cpp.

References Kross::Api::ScriptGUIClientPrivate::collections, QObject::connect(), and openScriptFile().

Referenced by Kross::Api::WdgScriptsManager::slotLoadScript().

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 }

KURL ScriptGUIClient::openScriptFile const QString caption = QString::null  )  [slot]
 

A KFileDialog will be displayed to let the user choose a scriptfile. The choosen file will be returned as KURL.

Definition at line 248 of file scriptguiclient.cpp.

References QMap::begin(), QMap::end(), Kross::Api::Manager::getInterpreterInfos(), QString::isNull(), QStringList::join(), and Kross::Api::Manager::scriptManager().

Referenced by executeScriptFile(), and loadScriptFile().

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 }

void ScriptGUIClient::reloadInstalledScripts  ) 
 

Reload the list of installed scripts.

Definition at line 124 of file scriptguiclient.cpp.

References Kross::Api::ScriptActionCollection::clear(), Kross::Api::ScriptGUIClientPrivate::collections, and loadScriptConfig().

Referenced by ScriptGUIClient(), and uninstallScriptPackage().

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 }

bool ScriptGUIClient::removeActionCollection const QString name  ) 
 

Remove the ScriptActionCollection defined with name name.

Definition at line 113 of file scriptguiclient.cpp.

References Kross::Api::ScriptGUIClientPrivate::collections, QMap::contains(), QObject::name(), and QMap::remove().

Referenced by addActionCollection().

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 }

void ScriptGUIClient::showScriptManager  )  [slot]
 

The ScriptManagerGUI dialog will be displayed to let the user manage the scriptfiles.

Definition at line 317 of file scriptguiclient.cpp.

References Kross::Api::ScriptGUIClientPrivate::parent.

Referenced by ScriptGUIClient().

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 }

bool ScriptGUIClient::uninstallScriptPackage const QString scriptpackagepath  ) 
 

Uninstall the scriptpackage located in the path scriptpackagepath . This just deletes the whole directory.

Definition at line 173 of file scriptguiclient.cpp.

References reloadInstalledScripts().

Referenced by Kross::Api::WdgScriptsManager::slotUninstallScript().

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 }


The documentation for this class was generated from the following files:
Generated on Thu Feb 9 18:01:17 2006 for Kross by  doxygen 1.4.6