TDE core libraries
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.

scriptmanager.cpp 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #include "scriptmanager.h"
  2. #include <tdeparts/part.h>
  3. #include <tdeparts/componentfactory.h>
  4. #include <tdeapplication.h>
  5. #include <kdesktopfile.h>
  6. #include <kstandarddirs.h>
  7. #include <tdelocale.h>
  8. #include <tdemessagebox.h>
  9. #include <kdebug.h>
  10. //using namespace KScriptInterface;
  11. class ScriptInfo
  12. {
  13. public:
  14. TQString scriptType;
  15. TQString scriptFile;
  16. TQString scriptMethod;
  17. ScriptInfo();
  18. ~ScriptInfo(){}
  19. };
  20. ScriptInfo::ScriptInfo()
  21. {
  22. scriptType = "";
  23. scriptFile = "";
  24. scriptMethod = "";
  25. }
  26. KScriptManager::KScriptManager(TQObject *parent, const char *name) :
  27. TQObject(parent,name), KScriptClientInterface()
  28. {
  29. }
  30. KScriptManager::~KScriptManager()
  31. {
  32. m_scripts.setAutoDelete(true);
  33. m_scriptCache.setAutoDelete(true);
  34. }
  35. bool KScriptManager::addScript( const TQString &scriptDesktopFile)
  36. {
  37. //m_scriptNames.append(scriptName);
  38. // lets get some information about the script we are going to run...
  39. bool success = false;
  40. TQString tmpScriptType = "";
  41. TQString tmpScriptFile = "";
  42. TQString tmpScriptMethod = "";
  43. // Read the desktop file
  44. if(KDesktopFile::isDesktopFile(scriptDesktopFile))
  45. {
  46. KDesktopFile desktop(scriptDesktopFile, true);
  47. m_scripts.insert(desktop.readName(), new ScriptInfo());
  48. m_scripts[desktop.readName()]->scriptType = desktop.readType();
  49. TQString localpath = TQString(kapp->name()) + "/scripts/" + desktop.readEntry("X-TDE-ScriptName", "");
  50. m_scripts[desktop.readName()]->scriptFile = locate("data", localpath);
  51. // m_scripts[desktop.readName()]->scriptMethod = tmpScriptMethod;
  52. success = true;
  53. }
  54. return success;
  55. }
  56. bool KScriptManager::removeScript( const TQString &scriptName )
  57. {
  58. bool result = m_scriptCache.remove(scriptName);
  59. result = m_scripts.remove(scriptName);
  60. return result;
  61. }
  62. TQStringList KScriptManager::scripts()
  63. {
  64. TQDictIterator<ScriptInfo> it( m_scripts );
  65. // return m_scriptNames;
  66. TQStringList scriptList;
  67. while ( it.current() )
  68. {
  69. scriptList.append(it.currentKey());
  70. ++it;
  71. }
  72. return scriptList;
  73. }
  74. void KScriptManager::clear()
  75. {
  76. m_scriptCache.clear();
  77. m_scripts.clear();
  78. }
  79. void KScriptManager::runScript( const TQString &scriptName, TQObject *context, const TQVariant &arg)
  80. {
  81. ScriptInfo *newScript = m_scripts[scriptName];
  82. if (newScript)
  83. {
  84. TQString scriptType = "([X-TDE-Script-Runner] == '" + newScript->scriptType + "')";
  85. kdDebug()<<"running script, type = '"<<scriptType<<"'"<<endl;
  86. // See if the script is already cached...
  87. if ( !m_scriptCache[scriptName] )
  88. {
  89. // via some magic we will let the old script engine go away after
  90. // some minutes...
  91. // currently i am thinking a TQTimer that will throw a signal in 10 minutes
  92. // to remove m_scriptCache[m_currentScript]
  93. KScriptInterface *ksif = KParts::ComponentFactory::createInstanceFromQuery<KScriptInterface>( "KScriptRunner/KScriptRunner", scriptType, this );
  94. if ( ksif )
  95. {
  96. m_scriptCache.insert( scriptName, ksif );
  97. }
  98. else
  99. {
  100. KMessageBox::sorry(0, i18n("Unable to get KScript Runner for type \"%1\".").arg(newScript->scriptType), i18n("KScript Error"));
  101. return;
  102. }
  103. }
  104. m_currentScript = scriptName;
  105. if ( m_scriptCache[m_currentScript] )
  106. {
  107. m_scriptCache[m_currentScript]->ScriptClientInterface = this;
  108. if (!newScript->scriptMethod.isEmpty())
  109. m_scriptCache[m_currentScript]->setScript( newScript->scriptFile, newScript->scriptMethod );
  110. else
  111. m_scriptCache[m_currentScript]->setScript( newScript->scriptFile );
  112. m_scriptCache[m_currentScript]->run(context, arg);
  113. }
  114. else
  115. {
  116. // Dialog and say we cant go on...
  117. // This is also a broken script so we need to remove it
  118. m_scriptCache.remove(m_currentScript);
  119. }
  120. }
  121. else
  122. KMessageBox::sorry(0, i18n("Unable find script \"%1\".").arg(scriptName), i18n("KScript Error"));
  123. }
  124. #include "scriptmanager.moc"
  125. #include "scriptinterface.moc"