File indexing completed on 2024-12-01 06:51:48

0001 //
0002 // C++ Implementation: scriptingplugin
0003 //
0004 // Description: scriptingplugin
0005 //
0006 /*
0007 Copyright 2006-2011 Tomas Mecir <kmuddy@kmuddy.com>
0008 
0009 This program is free software; you can redistribute it and/or
0010 modify it under the terms of the GNU General Public License as
0011 published by the Free Software Foundation; either version 2 of 
0012 the License, or (at your option) any later version.
0013 
0014 This program is distributed in the hope that it will be useful,
0015 but WITHOUT ANY WARRANTY; without even the implied warranty of
0016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0017 GNU General Public License for more details.
0018 
0019 You should have received a copy of the GNU General Public License
0020 along with this program.  If not, see <http://www.gnu.org/licenses/>.
0021 */
0022 
0023 #include "scriptingplugin.h"
0024 
0025 #include "cactionmanager.h"
0026 #include "clistmanager.h"
0027 #include "cmacromanager.h"
0028 #include "cmenumanager.h"
0029 #include "cprofilemanager.h"
0030 #include "cprofilesettings.h"
0031 
0032 #include "cnotifymanager.h"
0033 #include "crunninglist.h"
0034 #include "cscriptlist.h"
0035 #include "dlgrunninglist.h"
0036 
0037 #include <QDir>
0038 #include <kaction.h>
0039 #include <kactioncollection.h>  
0040 #include <kapplication.h>
0041 #include <kmainwindow.h>
0042 #include <kpluginfactory.h>
0043 #include <kpluginloader.h>
0044 #include <KLocalizedString>
0045 #include <ktoggleaction.h>
0046 
0047 #include <map>
0048 
0049 K_PLUGIN_CLASS_WITH_JSON(cScriptingPlugin, "scriptingplugin.json")
0050 
0051 
0052 // Macros exec and notify:
0053 
0054 class cMacroExec : public cMacro {
0055   public:
0056     cMacroExec () : cMacro ("exec") {}
0057     virtual void eval (const QString &params, int sess, cCmdQueue *queue) {
0058       QString sname = params.section (' ', 0, 0, QString::SectionSkipEmpty);
0059       QString pars = params.section (' ', 1, -1, QString::SectionSkipEmpty);
0060       pars = expandVariables (pars, sess, queue);
0061       cScriptList *list = dynamic_cast<cScriptList *>(cListManager::self()->getList (sess, "scripts"));
0062       if (!list) {  // this shouldn't happen
0063         am->invokeEvent ("message", sess, i18n ("/exec: Script list is not available."));
0064         return;
0065       }
0066 
0067       list->runScript (sname, pars);
0068     }
0069 };
0070 
0071 class cMacroNotify : public cMacro {
0072   public:
0073     cMacroNotify () : cMacro ("notify") {
0074       notifymanager = new cNotifyManager;
0075     }
0076     ~cMacroNotify () {
0077       delete notifymanager;
0078     }
0079     virtual void eval (const QString &params, int sess, cCmdQueue *queue) {
0080       QString pars = expandVariables (params, sess, queue);
0081       QString ip_port = pars.section (' ', 0, 0, QString::SectionSkipEmpty);
0082       QString ip_data = pars.section (' ', 1, -1, QString::SectionSkipEmpty);
0083 
0084       bool convert_ok;
0085       int  port;
0086       port = ip_port.toInt(&convert_ok);
0087 // Send data to IP port
0088       if (convert_ok)
0089         notifymanager->doNotify (port, ip_data);
0090     }
0091   private:
0092     cNotifyManager *notifymanager;
0093 };
0094 
0095 
0096 
0097 struct ScriptingSessionData {
0098   cRunningList *runningList;
0099 };
0100 
0101 struct cScriptingPluginPrivate {
0102   QAction *showRunningScripts;
0103   dlgRunningList *rdlg;
0104 
0105   cMacroExec *mexec;
0106   cMacroNotify *mnotify;
0107   
0108   std::map<int, ScriptingSessionData> sessionData;
0109 };
0110 
0111 cScriptingPlugin::cScriptingPlugin (QObject *, const QVariantList &)
0112 {
0113   d = new cScriptingPluginPrivate;
0114 
0115   // set default values for our profile-based values
0116   cProfileSettings::setDefaultString ("script-directory", QDir::homePath());
0117   cProfileSettings::setDefaultString ("script-working-directory", QDir::homePath());
0118 
0119   d->mexec = new cMacroExec;
0120   d->mnotify = new cMacroNotify;
0121 
0122   cListManager *lm = cListManager::self();
0123   lm->registerType ("scripts", i18n ("Scripts"), cScriptList::newList);
0124 
0125   KMainWindow *mainWindow = cActionManager::self()->mainWindow ();
0126   d->rdlg = new dlgRunningList (mainWindow);
0127   d->rdlg->hide ();
0128   d->rdlg->setObjectName ("runningscripts");
0129   mainWindow->addDockWidget (Qt::RightDockWidgetArea, d->rdlg);
0130   d->rdlg->setFloating (true);
0131 
0132   KActionCollection *acol = cActionManager::self()->getACol ();
0133   d->showRunningScripts = d->rdlg->toggleViewAction ();
0134   acol->addAction ("ShowRunningScripts", d->showRunningScripts);
0135 
0136   // plug things into the menu
0137   cMenuManager *menu = cMenuManager::self();
0138   menu->plug (d->showRunningScripts, "view-profile");
0139 }
0140 
0141 cScriptingPlugin::~cScriptingPlugin()
0142 {
0143   cMenuManager *menu = cMenuManager::self();
0144   menu->unplug (d->showRunningScripts);
0145 
0146   cListManager *lm = cListManager::self();
0147   lm->unregisterType ("scripts");
0148 
0149   delete d->rdlg;
0150   delete d->mexec;
0151   delete d->mnotify;
0152   delete d;
0153 }
0154 
0155 void cScriptingPlugin::sessionSwitch (int)
0156 {
0157   updateRunningList ();
0158 }
0159 
0160 void cScriptingPlugin::connected (int sess)
0161 {
0162   ScriptingSessionData sd;
0163   sd.runningList = 0;
0164   if (cProfileManager::self()->settings (sess))  // this means that it's a profile connection
0165     sd.runningList = new cRunningList (sess);
0166   d->sessionData[sess] = sd;
0167   if (cActionManager::self()->activeSession() == sess)
0168     sessionSwitch (sess);
0169 }
0170 
0171 void cScriptingPlugin::disconnected (int sess)
0172 {
0173   if (d->sessionData.count (sess)) {
0174     delete d->sessionData[sess].runningList;
0175     d->sessionData[sess].runningList = 0;
0176   }
0177   d->sessionData.erase (sess);
0178 
0179   updateRunningList ();
0180 }
0181 
0182 void cScriptingPlugin::updateRunningList ()
0183 {
0184   if (!d->rdlg) return;
0185   cActionManager *am = cActionManager::self();
0186   int s = am->activeSession();
0187   cRunningList *rl = dynamic_cast<cRunningList *>(am->object ("runninglist", s));
0188   d->rdlg->switchRunningList (rl);
0189 }
0190 
0191 #include "scriptingplugin.moc"