File indexing completed on 2024-04-28 04:03:00

0001 /***************************************************************************
0002                           crunninglist.h  -  list of running scripts
0003                              -------------------
0004     begin                : Ne dec 22 2002
0005     copyright            : (C) 2002-2009 by Tomas Mecir
0006     email                : kmuddy@kmuddy.com
0007  ***************************************************************************/
0008 
0009 /***************************************************************************
0010  *                                                                         *
0011  *   This program is free software; you can redistribute it and/or modify  *
0012  *   it under the terms of the GNU General Public License as published by  *
0013  *   the Free Software Foundation; either version 2 of the License, or     *
0014  *   (at your option) any later version.                                   *
0015  *                                                                         *
0016  ***************************************************************************/
0017 
0018 #ifndef CRUNNINGLIST_H
0019 #define CRUNNINGLIST_H
0020 
0021 class cRunningScript;
0022 
0023 using namespace std;
0024 
0025 #include "cactionbase.h"
0026 
0027 #include <qobject.h>
0028 #include <list>
0029 #include <map>
0030 #include <qcolor.h>
0031 
0032 class QSocketNotifier;
0033 class QAbstractItemModel;
0034 class cRunningModel;
0035 
0036 /**
0037 Manages all running scripts.
0038 
0039   *@author Tomas Mecir
0040   */
0041 
0042 class cRunningList: public QObject, public cActionBase {
0043 Q_OBJECT
0044 public: 
0045   cRunningList (int sess);
0046   virtual ~cRunningList ();
0047 
0048   QAbstractItemModel *getModel ();
0049 
0050   /** add a new script to my list */
0051   void addScript (cRunningScript *script);
0052 
0053   /** flow control state, i.e. how many lines are waiting in the
0054   flow control's queue */
0055   int fcState () { return textQueue.size (); };
0056   
0057   void killAll ();
0058 
0059   /** get script name */
0060   const QString name (int id);
0061   /** try to terminate the script */
0062   void terminate (int id);
0063   /** kill the script NOW! */
0064   void kill (int id);
0065 
0066   /** attempt to give a write-lock to a script */
0067   bool requestLock (cRunningScript *script, const QString &varname);
0068   /** release a lock */
0069   void releaseLock (cRunningScript *script, const QString &varname);
0070   /** look if the variable can be modified by a script (returns true
0071   if the variable is not locked or if this script has the lock) */
0072   bool canModify (cRunningScript *script, const QString &varname);
0073 private:
0074   QColor * getColor(QString s);
0075   QColor m_currentFgColor;
0076   QColor m_currentBkColor;
0077 
0078 signals:
0079   void stateChanged ();
0080 protected:
0081   virtual void eventStringHandler (QString event, int, QString &par1, const QString &);
0082   /** remove one script from my list */
0083   void removeScript (cRunningScript *script);
0084   void sendThisNow (const QString &text, int type, bool noFC = false);
0085   /** send input to flow-controlled scripts */
0086   void sendToFlowControlled(const QString &text, int type);
0087   /** send user command to all scripts */
0088   void sendCommand (const QString &text);
0089   /** send server output to all scripts */
0090   void sendServerOutput (const QString &text);
0091   /** send server prompt to all scripts */
0092   void sendPrompt (const QString &prompt);
0093 
0094   /** list of all running scripts */
0095   map<int, cRunningScript *> scripts;
0096   /** last used script ID */
0097   int lastid;
0098   
0099   cRunningScript *getRunningScript (int id);
0100   /** text sending synchronization */
0101   int waitCounter;
0102   /** text sending operation in progress */
0103   bool waitLock;
0104   /** text strings waiting to be sent to scripts */
0105   list<QString> textQueue;
0106   /** types of those strings */
0107   list<int> typeQueue;
0108   map<QString, cRunningScript *> locks;
0109 
0110   cRunningModel *model;
0111   friend class cRunningModel;
0112 protected slots:
0113   /** send a text from a script to the server */
0114   void sendText (const QString &text);
0115   /** display a text from the script */
0116   void displayText (const QString &text);
0117   
0118   /** one script has finished succesfully */
0119   void scriptFinished (cRunningScript *script, int returnValue);
0120   /** one script has been killed */
0121   void scriptKilled (cRunningScript *script);
0122   /** one script couldn't be started */
0123   void scriptFailed (cRunningScript *script);
0124   /** cRunningScript has accepted the text (but not sent to script yet) */
0125   void scriptTextAccepted ();
0126   /** cRunningScript has received the sent text */
0127   void scriptTextSent ();
0128 };
0129 
0130 #endif