You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
133 lines
3.9 KiB
133 lines
3.9 KiB
15 years ago
|
#include "scriptmanager.h"
|
||
12 years ago
|
#include <tdeparts/part.h>
|
||
|
#include <tdeparts/componentfactory.h>
|
||
15 years ago
|
#include <kapplication.h>
|
||
|
#include <kdesktopfile.h>
|
||
|
#include <kstandarddirs.h>
|
||
|
|
||
|
#include <klocale.h>
|
||
|
#include <kmessagebox.h>
|
||
|
#include <kdebug.h>
|
||
|
|
||
|
//using namespace KScriptInterface;
|
||
|
class ScriptInfo
|
||
|
{
|
||
|
public:
|
||
15 years ago
|
TQString scriptType;
|
||
|
TQString scriptFile;
|
||
|
TQString scriptMethod;
|
||
15 years ago
|
ScriptInfo();
|
||
|
~ScriptInfo(){}
|
||
|
};
|
||
|
ScriptInfo::ScriptInfo()
|
||
|
{
|
||
|
scriptType = "";
|
||
|
scriptFile = "";
|
||
|
scriptMethod = "";
|
||
|
}
|
||
15 years ago
|
KScriptManager::KScriptManager(TQObject *parent, const char *name) :
|
||
|
TQObject(parent,name), KScriptClientInterface()
|
||
15 years ago
|
{
|
||
|
|
||
|
}
|
||
|
KScriptManager::~KScriptManager()
|
||
|
{
|
||
|
m_scripts.setAutoDelete(true);
|
||
|
m_scriptCache.setAutoDelete(true);
|
||
|
|
||
|
}
|
||
15 years ago
|
bool KScriptManager::addScript( const TQString &scriptDesktopFile)
|
||
15 years ago
|
{
|
||
|
//m_scriptNames.append(scriptName);
|
||
|
// lets get some information about the script we are going to run...
|
||
|
bool success = false;
|
||
15 years ago
|
TQString tmpScriptType = "";
|
||
|
TQString tmpScriptFile = "";
|
||
|
TQString tmpScriptMethod = "";
|
||
15 years ago
|
// 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();
|
||
13 years ago
|
TQString localpath = TQString(kapp->name()) + "/scripts/" + desktop.readEntry("X-TDE-ScriptName", "");
|
||
15 years ago
|
m_scripts[desktop.readName()]->scriptFile = locate("data", localpath);
|
||
|
// m_scripts[desktop.readName()]->scriptMethod = tmpScriptMethod;
|
||
|
success = true;
|
||
|
}
|
||
|
return success;
|
||
|
}
|
||
15 years ago
|
bool KScriptManager::removeScript( const TQString &scriptName )
|
||
15 years ago
|
{
|
||
|
bool result = m_scriptCache.remove(scriptName);
|
||
|
result = m_scripts.remove(scriptName);
|
||
|
return result;
|
||
|
}
|
||
15 years ago
|
TQStringList KScriptManager::scripts()
|
||
15 years ago
|
{
|
||
15 years ago
|
TQDictIterator<ScriptInfo> it( m_scripts );
|
||
15 years ago
|
// return m_scriptNames;
|
||
15 years ago
|
TQStringList scriptList;
|
||
15 years ago
|
while ( it.current() )
|
||
|
{
|
||
|
scriptList.append(it.currentKey());
|
||
|
++it;
|
||
|
}
|
||
|
return scriptList;
|
||
|
}
|
||
|
void KScriptManager::clear()
|
||
|
{
|
||
|
m_scriptCache.clear();
|
||
|
m_scripts.clear();
|
||
|
}
|
||
15 years ago
|
void KScriptManager::runScript( const TQString &scriptName, TQObject *context, const TQVariant &arg)
|
||
15 years ago
|
{
|
||
|
ScriptInfo *newScript = m_scripts[scriptName];
|
||
|
if (newScript)
|
||
|
{
|
||
13 years ago
|
TQString scriptType = "([X-TDE-Script-Runner] == '" + newScript->scriptType + "')";
|
||
15 years ago
|
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...
|
||
15 years ago
|
// currently i am thinking a TQTimer that will throw a signal in 10 minutes
|
||
15 years ago
|
// 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
|
||
14 years ago
|
KMessageBox::sorry(0, i18n("Unable find script \"%1\".").arg(scriptName), i18n("KScript Error"));
|
||
15 years ago
|
}
|
||
|
#include "scriptmanager.moc"
|
||
|
#include "scriptinterface.moc"
|