summaryrefslogtreecommitdiffstats
path: root/ksirc/puke/controller.h
blob: d1d71675fd679ba2f8e1febdf191959a1826b231 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
 * Main controller for:
 *
 * PUKE = Perl gUi Kontrol Environment
 *
 */

#ifndef PUKE_CONTROLLER_H
#define PUKE_CONTROLLER_H

#include <tqobject.h>
#include <tqsocketnotifier.h>
#include <tqstring.h>
#include <tqdict.h>
#include <tqintdict.h>
#include <tqstrlist.h>

#include "pmessage.h"
#include "pobject.h"
#include "pwidget.h"
#include "commands.h"

class PukeController;
class KLibrary;

typedef struct {
  TQString server;
  bool writeable;
  TQSocketNotifier *sr,*sw;
} fdStatus;


struct commandStruct {
  void (PukeController::*cmd)(int, PukeMessage*);
  KLibrary *library;
};

typedef struct {
  PObject *pwidget; // The widget
  int type;         // The type so casting is "safer"
} WidgetS;          // WidgetStruct

typedef struct {
  PObject *(*wc)(CreateArgs &ca);
  KLibrary *library;
} widgetCreate;

class errorNoSuchWidget {
public:
  errorNoSuchWidget(widgetId &_wi)
  {
    wi = _wi;
  }

  widgetId &widgetIden() {
    return wi;
  }
private:
  widgetId wi;
};

class errorCommandFailed {
public:
    errorCommandFailed(int _command, int _iarg){
        __command = _command;
        __iarg = _iarg;
    }

    int command() { return __command; }
    int iarg() { return __iarg; }
    
private:
    int __command, __iarg;
};

#define INVALID_DEL_NO_CONTROL 100
#define INVALID_DEL_NO_SUCH_CONNECTION 101
#define INVALID_DEL_NO_SUCH_WIDGET 102

class PukeController : public PObject
{
  TQ_OBJECT
  
public:
  PukeController(TQString socket = "", TQObject *parent=0, const char *name=0);
  virtual ~PukeController();
  bool running;

  /**
   * Verifies the widgetId exists and is a valid widget.
   * True is valid, false if invalid.
   */
  bool checkWidgetId(widgetId *pwI);

  /**
   * id2pobject takes a window id and returns the reuired object
   * it throw an errorNoSuchWidget on failures
   */
  PObject *id2pobject(int fd, int iWinId);
  PObject *id2pobject(widgetId *pwi);
  /**
   * Return a PWidget if it's a widget, throws an exception if not found
   */
  PWidget *id2pwidget(widgetId *pwi);

  TQStrList allObjects();

signals:
  void PukeMessages(TQString server, int command, TQString args);
  void inserted(TQObject *);

public slots:
  void ServMessage(TQString, int, TQString);

protected slots:
  void Traffic(int);
  void Writeable(int);
  void NewConnect(int);
  void slotInserted(TQObject *obj);

  /**
   * When we delete a widget, this removes it from our internal
   * list of widgets.  We never remove a widget ourselfs, we call delete
   * and this function removes it.
   */
  void pobjectDestroyed();

  /**
   * Fd to write to
   * PukeMessage message  to be written, if null buffer is flushed.
   */
  void writeBuffer(int fd, PukeMessage *message);


private:
  TQString qsPukeSocket;
  int iListenFd;
  bool bClosing; // Set true if we are closing, we don't try and close twice at the same time.
  TQSocketNotifier *qsnListen;
  TQIntDict<fdStatus> qidConnectFd;

  /**
   * Controller ID is defined as 1
   */
  enum { ControllerWinId = PUKE_CONTROLLER };
      
  
  // List of widgets and the fle descriptors they belong too
  TQIntDict<TQIntDict<WidgetS> > WidgetList;
  // I use a char * key that's the %p (hex address) of the pwidget
  TQDict<widgetId> revWidgetList;
  enum { keySize = 10 };

  // Funtions used to create new widget
  TQIntDict<widgetCreate> widgetCF; // widgetCreatingFuntion List

  TQIntDict<commandStruct> qidCommandTable;

  void initHdlr();

  void closefd(int fd);

  void MessageDispatch(int fd, PukeMessage *pm);

  /**
   * WinId comes from a static unsigned int we increment for each new window
   */
  static uint uiBaseWinId;
  
  /**
   * Create new Widget, returns new iWinId for it.
   * Takes server fd and parent winid, and type as arguments
   */
  widgetId createWidget(widgetId wI, PukeMessage *pm);

  /**
   * Used to process messages going to controller, winId #1
   *
   */
  void messageHandler(int fd, PukeMessage *pm);

  /**
   * NOT APPLICAABLE
   */
  void setWidget(TQObject *) { }
  /**
   * NOT APPLICAABLE
   */
  virtual TQObject *widget() { return 0x0; }

  /**
   * Inserts a PObject into our internal list
   */
  void insertPObject(int fd, int iWinId, WidgetS *obj);

  /**
   * Closes a widget, checking for sanity
   */
//  void closeWidget(widgetId wI);
   
  // Message handlers
  void hdlrPukeSetup(int fd, PukeMessage *pm);
  void hdlrPukeInvalid(int fd, PukeMessage *pm);
  void hdlrPukeEcho(int fd, PukeMessage *pm);
  void hdlrPukeDumpTree(int fd, PukeMessage *pm);
  void hdlrPukeFetchWidget(int fd, PukeMessage *pm);
  void hdlrPukeDeleteWidget(int fd, PukeMessage *pm);

};

#endif