scriptmanager.cpp
00001 #include "scriptmanager.h" 00002 #include <tdeparts/part.h> 00003 #include <tdeparts/componentfactory.h> 00004 #include <tdeapplication.h> 00005 #include <kdesktopfile.h> 00006 #include <kstandarddirs.h> 00007 00008 #include <tdelocale.h> 00009 #include <tdemessagebox.h> 00010 #include <kdebug.h> 00011 00012 //using namespace KScriptInterface; 00013 class ScriptInfo 00014 { 00015 public: 00016 TQString scriptType; 00017 TQString scriptFile; 00018 TQString scriptMethod; 00019 ScriptInfo(); 00020 ~ScriptInfo(){} 00021 }; 00022 ScriptInfo::ScriptInfo() 00023 { 00024 scriptType = ""; 00025 scriptFile = ""; 00026 scriptMethod = ""; 00027 } 00028 KScriptManager::KScriptManager(TQObject *parent, const char *name) : 00029 TQObject(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 TQString &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 TQString tmpScriptType = ""; 00045 TQString tmpScriptFile = ""; 00046 TQString 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 TQString localpath = TQString(kapp->name()) + "/scripts/" + desktop.readEntry("X-TDE-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 TQString &scriptName ) 00062 { 00063 bool result = m_scriptCache.remove(scriptName); 00064 result = m_scripts.remove(scriptName); 00065 return result; 00066 } 00067 TQStringList KScriptManager::scripts() 00068 { 00069 TQDictIterator<ScriptInfo> it( m_scripts ); 00070 // return m_scriptNames; 00071 TQStringList 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 TQString &scriptName, TQObject *context, const TQVariant &arg) 00085 { 00086 ScriptInfo *newScript = m_scripts[scriptName]; 00087 if (newScript) 00088 { 00089 TQString scriptType = "([X-TDE-Script-Runner] == '" + newScript->scriptType + "')"; 00090 kdDebug()<<"running script, type = '"<<scriptType<<"'"<<endl; 00091 // See if the script is already 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 TQTimer 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 \"%1\".").arg(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.isEmpty()) 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 else 00129 KMessageBox::sorry(0, i18n("Unable find script \"%1\".").arg(scriptName), i18n("KScript Error")); 00130 } 00131 #include "scriptmanager.moc" 00132 #include "scriptinterface.moc"