scriptmanager.cpp
#include "scriptmanager.h"
#include <kparts/part.h>
#include <kparts/componentfactory.h>
#include <kapplication.h>
#include <kdesktopfile.h>
#include <kstandarddirs.h>
#include <klocale.h>
#include <kmessagebox.h>
#include <kdebug.h>
//using namespace KScriptInterface;
class ScriptInfo
{
public:
QString scriptType;
QString scriptFile;
QString scriptMethod;
ScriptInfo();
~ScriptInfo(){}
};
ScriptInfo::ScriptInfo()
{
scriptType = "";
scriptFile = "";
scriptMethod = "";
}
KScriptManager::KScriptManager(QObject *parent, const char *name) :
QObject(parent,name), KScriptClientInterface()
{
}
KScriptManager::~KScriptManager()
{
m_scripts.setAutoDelete(true);
m_scriptCache.setAutoDelete(true);
}
bool KScriptManager::addScript( const QString &scriptDesktopFile)
{
//m_scriptNames.append(scriptName);
// lets get some information about the script we are going to run...
bool success = false;
QString tmpScriptType = "";
QString tmpScriptFile = "";
QString tmpScriptMethod = "";
// Read the desktop file
if(KDesktopFile::isDesktopFile(scriptDesktopFile))
{
KDesktopFile desktop(scriptDesktopFile, true);
m_scripts.insert(desktop.readName(), new ScriptInfo());
m_scripts[desktop.readName()]->scriptType = desktop.readType();
QString localpath = QString(kapp->name()) + "/scripts/" + desktop.readEntry("X-KDE-ScriptName", "");
m_scripts[desktop.readName()]->scriptFile = locate("data", localpath);
// m_scripts[desktop.readName()]->scriptMethod = tmpScriptMethod;
success = true;
}
return success;
}
bool KScriptManager::removeScript( const QString &scriptName )
{
bool result = m_scriptCache.remove(scriptName);
result = m_scripts.remove(scriptName);
return result;
}
QStringList KScriptManager::scripts()
{
QDictIterator<ScriptInfo> it( m_scripts );
// return m_scriptNames;
QStringList scriptList;
while ( it.current() )
{
scriptList.append(it.currentKey());
++it;
}
return scriptList;
}
void KScriptManager::clear()
{
m_scriptCache.clear();
m_scripts.clear();
}
void KScriptManager::runScript( const QString &scriptName, QObject *context, const QVariant &arg)
{
ScriptInfo *newScript = m_scripts[scriptName];
if (newScript)
{
QString scriptType = "([X-KDE-Script-Runner] == '" + newScript->scriptType + "')";
kdDebug()<<"running script, type = '"<<scriptType<<"'"<<endl;
// See if the script is already cached...
if ( !m_scriptCache[scriptName] )
{
// via some magic we will let the old script engine go away after
// some minutes...
// currently i am thinking a QTimer that will throw a signal in 10 minutes
// to remove m_scriptCache[m_currentScript]
KScriptInterface *ksif = KParts::ComponentFactory::createInstanceFromQuery<KScriptInterface>( "KScriptRunner/KScriptRunner", scriptType, this );
if ( ksif )
{
m_scriptCache.insert( scriptName, ksif );
}
else
{
KMessageBox::sorry(0, i18n("Unable to get KScript Runner for type \"%1\".").arg(newScript->scriptType), i18n("KScript Error"));
return;
}
}
m_currentScript = scriptName;
if ( m_scriptCache[m_currentScript] )
{
m_scriptCache[m_currentScript]->ScriptClientInterface = this;
if (!newScript->scriptMethod.isEmpty())
m_scriptCache[m_currentScript]->setScript( newScript->scriptFile, newScript->scriptMethod );
else
m_scriptCache[m_currentScript]->setScript( newScript->scriptFile );
m_scriptCache[m_currentScript]->run(context, arg);
}
else
{
// Dialog and say we cant go on...
// This is also a broken script so we need to remove it
m_scriptCache.remove(m_currentScript);
}
}
else
KMessageBox::sorry(0, i18n("Unable find script \"%1\".").arg(scriptName), i18n("KScript Error"));
}
#include "scriptmanager.moc"
#include "scriptinterface.moc"
locate
QString locate(const char *type, const QString &filename, const KInstance *instance=KGlobal::instance())
KScriptManager::~KScriptManager
virtual ~KScriptManager()
Destroy the current script engine.
Definition: scriptmanager.cpp:33
KScriptManager::removeScript
bool removeScript(const QString &scriptName)
Remove a script instance from the script engine.
Definition: scriptmanager.cpp:61
KScriptManager::runScript
void runScript(const QString &scriptName, QObject *context=0, const QVariant &arg=0)
Run the selected script.
Definition: scriptmanager.cpp:84
KScriptManager::KScriptManager
KScriptManager(QObject *parent, const char *name)
Create a new instance of the script engine.
Definition: scriptmanager.cpp:28
kdDebug
kdbgstream kdDebug(int area=0)
klocale.h
KMessageBox::sorry
static void sorry(QWidget *parent, const QString &text, const QString &caption=QString::null, int options=Notify)
KDesktopFile::readType
QString readType() const
KScriptClientInterface
This class is used for allowing feedback to the main system.
Definition: scriptclientinterface.h:73
KScriptManager::scripts
QStringList scripts()
Access the names of script instances from the script engine.
Definition: scriptmanager.cpp:67
KDesktopFile
KScriptManager::addScript
bool addScript(const QString &scriptDesktopFile)
Add a new script instance to the script engine.
Definition: scriptmanager.cpp:39
KDesktopFile::isDesktopFile
static bool isDesktopFile(const QString &path)
KConfig::readEntry
QString readEntry(const QString &pKey, const QString &aDefault=QString::null) const
KScriptManager::clear
void clear()
Clear all script intstances in memory.
Definition: scriptmanager.cpp:79
endl
kndbgstream & endl(kndbgstream &s)
KScriptInterface
This class is the base for all script engines.
Definition: scriptinterface.h:47
KDesktopFile::readName
QString readName() const

KDE Home | KDE Accessibility Home | Description of Access Keys