Some new UI elements for configuring network reconnects. EgS, that should
[quassel.git] / src / common / signalproxy.h
1 /***************************************************************************
2  *   Copyright (C) 2005-08 by the Quassel Project                          *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) version 3.                                           *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  ***************************************************************************/
20
21 #ifndef _SIGNALPROXY_H_
22 #define _SIGNALPROXY_H_
23
24 #include <QList>
25 #include <QHash>
26 #include <QVariant>
27 #include <QVariantMap>
28 #include <QPair>
29 #include <QString>
30 #include <QByteArray>
31
32 class SignalRelay;
33 class SyncableObject;
34 class QMetaObject;
35
36 class SignalProxy : public QObject {
37   Q_OBJECT
38
39 public:
40   enum ProxyMode {
41     Server,
42     Client
43   };
44
45   enum RequestType {
46     Sync = 0,
47     InitRequest,
48     InitData
49   };
50
51   SignalProxy(QObject *parent);
52   SignalProxy(ProxyMode mode, QObject *parent);
53   SignalProxy(ProxyMode mode, QIODevice *device, QObject *parent);
54   virtual ~SignalProxy();
55
56   void setProxyMode(ProxyMode mode);
57   ProxyMode proxyMode() const;
58
59   bool addPeer(QIODevice *iodev);
60   void removePeer(QIODevice *iodev = 0);
61
62   bool attachSignal(QObject *sender, const char *signal, const QByteArray& sigName = QByteArray());
63   bool attachSlot(const QByteArray& sigName, QObject *recv, const char *slot);
64
65   void synchronize(SyncableObject *obj);
66
67   void setInitialized(SyncableObject *obj);
68   bool isInitialized(SyncableObject *obj) const;
69   void requestInit(SyncableObject *obj);
70
71   void detachObject(QObject *obj);
72   void detachSignals(QObject *sender);
73   void detachSlots(QObject *receiver);
74   void stopSync(SyncableObject *obj);
75
76   //! Writes a QVariant to a device.
77   /** The data item is prefixed with the resulting blocksize,
78    *  so the corresponding function readDataFromDevice() can check if enough data is available
79    *  at the device to reread the item.
80    */
81   static void writeDataToDevice(QIODevice *dev, const QVariant &item);
82
83   //! Reads a data item from a device that has been written by writeDataToDevice().
84   /** If not enough data bytes are available, the function returns false and the QVariant reference
85    *  remains untouched.
86    */
87   static bool readDataFromDevice(QIODevice *dev, quint32 &blockSize, QVariant &item);
88
89   static QString methodBaseName(const QMetaMethod &method);
90
91   const QList<int> &argTypes(QObject *obj, int methodId);
92   const int &minArgCount(QObject *obj, int methodId);
93   const QByteArray &methodName(QObject *obj, int methodId);
94   const QHash<QByteArray, int> &syncMap(SyncableObject *obj);
95   int updatedRemotelyId(SyncableObject *obj);
96
97   typedef QHash<int, QList<int> > ArgHash;
98   typedef QHash<int, QByteArray> MethodNameHash;
99   struct ClassInfo {
100     ArgHash argTypes;
101     QHash<int, int> minArgCount;
102     MethodNameHash methodNames;
103     int updatedRemotelyId; // id of the updatedRemotely() signal - makes things faster
104     QHash<QByteArray, int> syncMap;
105   };
106
107   void dumpProxyStats();
108   
109 private slots:
110   void dataAvailable();
111   void detachSender();
112   void removePeerBySender();
113   void objectRenamed(QString oldname, QString newname);
114   void objectRenamed(QByteArray classname, QString oldname, QString newname);
115
116 signals:
117   void peerRemoved(QIODevice *obj);
118   void connected();
119   void disconnected();
120   void objectInitialized(SyncableObject *);
121   
122 private:
123   void initServer();
124   void initClient();
125   
126   void createClassInfo(QObject *obj);
127   void setArgTypes(QObject *obj, int methodId);
128   void setMinArgCount(QObject *obj, int methodId);
129   void setMethodName(QObject *obj, int methodId);
130   void setSyncMap(SyncableObject *obj);
131   void setUpdatedRemotelyId(QObject *obj);
132
133   bool methodsMatch(const QMetaMethod &signal, const QMetaMethod &slot) const;
134
135   void dispatchSignal(QIODevice *receiver, const QVariant &identifier, const QVariantList &params);
136   void dispatchSignal(const QVariant &identifier, const QVariantList &params);
137   
138   void receivePeerSignal(QIODevice *sender, const QVariant &packedFunc);
139   void handleSync(QVariantList params);
140   void handleInitRequest(QIODevice *sender, const QVariantList &params);
141   void handleInitData(QIODevice *sender, const QVariantList &params);
142   void handleSignal(const QByteArray &funcName, const QVariantList &params);
143
144   bool invokeSlot(QObject *receiver, int methodId, const QVariantList &params = QVariantList());
145
146   QVariantMap initData(SyncableObject *obj) const;
147   void setInitData(SyncableObject *obj, const QVariantMap &properties);
148
149 public:
150   void dumpSyncMap(SyncableObject *object);
151   
152 private:
153   // Hash of used QIODevices
154   QHash<QIODevice*, quint32> _peerByteCount;
155
156   // containg a list of argtypes for fast access
157   QHash<const QMetaObject *, ClassInfo*> _classInfo;
158
159   // we use one SignalRelay per QObject
160   QHash<QObject*, SignalRelay *> _relayHash;
161
162   // RPC function -> (object, slot ID)
163   typedef QPair<QObject*, int> MethodId;
164   typedef QMultiHash<QByteArray, MethodId> SlotHash;
165   SlotHash _attachedSlots;
166
167   // slaves for sync
168   typedef QHash<QString, SyncableObject *> ObjectId;
169   QHash<QByteArray, ObjectId> _syncSlave;
170
171
172   ProxyMode _proxyMode;
173   
174   friend class SignalRelay;
175 };
176
177 #endif