This fix of the SignalProxy features more backward compatibility
[quassel.git] / src / common / signalproxy.h
1 /***************************************************************************
2  *   Copyright (C) 2005-07 by the Quassel IRC Team                         *
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 #include <QMutex>
33
34 class SignalRelay;
35 class QMetaObject;
36
37 class SignalProxy : public QObject {
38   Q_OBJECT
39
40 public:
41   enum ProxyMode {
42     Server,
43     Client
44   };
45
46   enum RequestType {
47     Sync = 0,
48     InitRequest,
49     InitData
50   };
51
52   SignalProxy(QObject *parent);
53   SignalProxy(ProxyMode mode, QObject *parent);
54   SignalProxy(ProxyMode mode, QIODevice *device, QObject *parent);
55   virtual ~SignalProxy();
56
57   void setProxyMode(ProxyMode mode);
58   ProxyMode proxyMode() const;
59
60   bool addPeer(QIODevice *iodev);
61   void removePeer(QIODevice *iodev = 0);
62
63   bool attachSignal(QObject *sender, const char *signal, const QByteArray& sigName = QByteArray());
64   bool attachSlot(const QByteArray& sigName, QObject *recv, const char *slot);
65
66   void synchronize(QObject *obj);
67   void synchronizeAsMaster(QObject *obj);
68   void synchronizeAsSlave(QObject *obj);
69
70   void setInitialized(QObject *obj);
71   bool initialized(QObject *obj);
72   void requestInit(QObject *obj);
73
74   void detachObject(QObject *obj);
75   void detachSignals(QObject *sender);
76   void detachSlots(QObject *receiver);
77   void stopSync(QObject *obj);
78
79   //! Writes a QVariant to a device.
80   /** The data item is prefixed with the resulting blocksize,
81    *  so the corresponding function readDataFromDevice() can check if enough data is available
82    *  at the device to reread the item.
83    */
84   static void writeDataToDevice(QIODevice *dev, const QVariant &item);
85
86   //! Reads a data item from a device that has been written by writeDataToDevice().
87   /** If not enough data bytes are available, the function returns false and the QVariant reference
88    *  remains untouched.
89    */
90   static bool readDataFromDevice(QIODevice *dev, quint32 &blockSize, QVariant &item);
91
92   static QString methodBaseName(const QMetaMethod &method);
93   
94   const QList<int> &argTypes(QObject *obj, int methodId);
95   const QByteArray &methodName(QObject *obj, int methodId);
96   const QHash<QByteArray, int> &syncMap(QObject *obj);
97
98   typedef QHash<int, QList<int> > ArgHash;
99   typedef QHash<int, QByteArray> MethodNameHash;
100   struct ClassInfo {
101     ArgHash argTypes;
102     MethodNameHash methodNames;
103     QHash<QByteArray, int> syncMap;
104   };
105
106   void dumpProxyStats();
107   
108 private slots:
109   void dataAvailable();
110   void detachSender();
111   void removePeerBySender();
112   void objectRenamed(QString oldname, QString newname);
113   void objectRenamed(QByteArray classname, QString oldname, QString newname);
114
115 signals:
116   void peerRemoved(QIODevice *obj);
117   void connected();
118   void disconnected();
119   
120 private:
121   void initServer();
122   void initClient();
123   
124   void createClassInfo(QObject *obj);
125   void setArgTypes(QObject *obj, int methodId);
126   void setMethodName(QObject *obj, int methodId);
127   void setSyncMap(QObject *obj);
128
129   bool methodsMatch(const QMetaMethod &signal, const QMetaMethod &slot) const;
130
131   void dispatchSignal(QIODevice *receiver, const QVariant &identifier, const QVariantList &params);
132   void dispatchSignal(const QVariant &identifier, const QVariantList &params);
133   
134   void receivePeerSignal(QIODevice *sender, const QVariant &packedFunc);
135   void handleSync(QVariantList params);
136   void handleInitRequest(QIODevice *sender, const QVariantList &params);
137   void handleInitData(QIODevice *sender, const QVariantList &params);
138   void handleSignal(const QByteArray &funcName, const QVariantList &params);
139
140   bool invokeSlot(QObject *receiver, int methodId, const QVariantList &params);
141
142   QVariantMap initData(QObject *obj) const;
143   void setInitData(QObject *obj, const QVariantMap &properties);
144   bool setInitValue(QObject *obj, const QString &property, const QVariant &value);
145
146   void _detachSignals(QObject *sender);
147   void _detachSlots(QObject *receiver);
148   void _stopSync(QObject *obj);
149
150   void dumpSyncMap(QObject *object);
151   
152   // Hash of used QIODevices
153   QHash<QIODevice*, quint32> _peerByteCount;
154
155   // containg a list of argtypes for fast access
156   QHash<const QMetaObject *, ClassInfo*> _classInfo;
157
158   // we use one SignalRelay per QObject
159   QHash<QObject*, SignalRelay *> _relayHash;
160
161   // RPC function -> (object, slot ID)
162   typedef QPair<QObject*, int> MethodId;
163   typedef QMultiHash<QByteArray, MethodId> SlotHash;
164   SlotHash _attachedSlots;
165
166   // slaves for sync
167   typedef QHash<QString, QObject *> ObjectId;
168   QHash<QByteArray, ObjectId> _syncSlave;
169
170
171   ProxyMode _proxyMode;
172   
173   // the slaveMutex protects both containers:
174   //  - _syncSlaves for sync and init calls
175   //  - _attachedSlots
176   QMutex slaveMutex;
177   
178   friend class SignalRelay;
179 };
180
181 #endif