interfaces Library API Documentation

scriptmanager.cpp

00001 #include "scriptmanager.h"
00002 #include <kparts/part.h>
00003 #include <kparts/componentfactory.h>
00004 #include <kapplication.h>
00005 #include <kdesktopfile.h>
00006 #include <kstandarddirs.h>
00007 
00008 #include <klocale.h>
00009 #include <kmessagebox.h>
00010 #include <kdebug.h>
00011 
00012 //using namespace KScriptInterface;
00013 class ScriptInfo
00014 {
00015         public:
00016                 QString scriptType;
00017                 QString scriptFile;
00018                 QString scriptMethod;
00019                 ScriptInfo();
00020                 ~ScriptInfo(){};
00021 };
00022 ScriptInfo::ScriptInfo()
00023 {
00024         scriptType = "";
00025         scriptFile = "";
00026         scriptMethod = "";
00027 }
00028 KScriptManager::KScriptManager(QObject *parent, const char *name) :
00029         QObject(parent,name), KScriptClientInterface()
00030 {
00031 
00032 }
00033 KScriptManager::~KScriptManager()
00034 {
00035     m_scripts.setAutoDelete(true);
00036     m_scriptCache.setAutoDelete(true);
00037 
00038 }
00039 bool KScriptManager::addScript( const QString &scriptDesktopFile)
00040 {
00041         //m_scriptNames.append(scriptName);
00042         // lets get some information about the script we are going to run...
00043         bool success = false;
00044         QString tmpScriptType = "";
00045         QString tmpScriptFile = "";
00046         QString tmpScriptMethod = "";
00047         // Read the desktop file
00048 
00049         if(KDesktopFile::isDesktopFile(scriptDesktopFile))
00050         {
00051                 KDesktopFile desktop(scriptDesktopFile, true);
00052                 m_scripts.insert(desktop.readName(), new ScriptInfo());
00053                 m_scripts[desktop.readName()]->scriptType = desktop.readType();
00054                 QString localpath = QString(kapp->name()) + "/scripts/" + desktop.readEntry("X-KDE-ScriptName", "");
00055                 m_scripts[desktop.readName()]->scriptFile = locate("data", localpath);
00056 //              m_scripts[desktop.readName()]->scriptMethod = tmpScriptMethod;
00057                 success = true;
00058         }
00059         return success;
00060 }
00061 bool KScriptManager::removeScript( const QString &scriptName )
00062 {
00063         bool result = m_scriptCache.remove(scriptName);
00064         result = m_scripts.remove(scriptName);
00065         return result;
00066 }
00067 QStringList KScriptManager::scripts()
00068 {
00069         QDictIterator<ScriptInfo> it( m_scripts );
00070 //      return m_scriptNames;
00071         QStringList scriptList;
00072         while ( it.current() )
00073         {
00074                 scriptList.append(it.currentKey());
00075                 ++it;
00076         }
00077         return scriptList;
00078 }
00079 void KScriptManager::clear()
00080 {
00081         m_scriptCache.clear();
00082         m_scripts.clear();
00083 }
00084 void KScriptManager::runScript( const QString &scriptName, QObject *context, const QVariant &arg)
00085 {
00086         ScriptInfo *newScript = m_scripts[scriptName];
00087         QString scriptType = "([X-KDE-Script-Runner] == '" + newScript->scriptType + "')";
00088         kdDebug()<<"running script, type = '"<<scriptType<<"'"<<endl;
00089         if (newScript)
00090         {
00091                 // See if the script is allready cached...
00092                 if ( !m_scriptCache[scriptName] )
00093                 {
00094                         // via some magic we will let the old script engine go away after
00095                         // some minutes...
00096                         // currently i am thinking a QTimer that will throw a signal in 10 minutes
00097                         // to remove m_scriptCache[m_currentScript]
00098                         KScriptInterface *ksif = KParts::ComponentFactory::createInstanceFromQuery<KScriptInterface>( "KScriptRunner/KScriptRunner", scriptType, this );
00099                         if ( ksif ) 
00100                         {
00101                           m_scriptCache.insert( scriptName, ksif );
00102                           
00103                         }
00104                         else
00105                         {
00106                           KMessageBox::sorry(0, i18n("Unable to get KScript Runner for type ") + newScript->scriptType, i18n("KScript error"));
00107                           return;
00108                         }
00109                 }
00110                 m_currentScript = scriptName;
00111 
00112                 if ( m_scriptCache[m_currentScript] )
00113                 {
00114                         m_scriptCache[m_currentScript]->ScriptClientInterface = this;
00115                         if (newScript->scriptMethod != "")
00116                                 m_scriptCache[m_currentScript]->setScript( newScript->scriptFile, newScript->scriptMethod );
00117                         else
00118                                 m_scriptCache[m_currentScript]->setScript( newScript->scriptFile );
00119                         m_scriptCache[m_currentScript]->run(context, arg);
00120                 }
00121                 else
00122                 {
00123                         // Dialog and say we cant go on...
00124                         // This is also a broken script so we need to remove it
00125                         m_scriptCache.remove(m_currentScript);
00126                 }
00127         }
00128 }
00129 #include "scriptmanager.moc"
00130 #include "scriptinterface.moc"
KDE Logo
This file is part of the documentation for kdelibs Version 3.1.5.
Documentation copyright © 1996-2002 the KDE developers.
Generated on Wed Jan 28 13:30:50 2004 by doxygen 1.3.4 written by Dimitri van Heesch, © 1997-2001