Some cosmetic corrections and some minor work on the nick model. Hopefully I
[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) any later version.                                   *
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 QMetaObject;
34
35 class SignalProxy : public QObject {
36   Q_OBJECT
37
38 public:
39   enum ProxyMode {
40     Server,
41     Client
42   };
43
44   enum RequestType {
45     Sync = 0,
46     InitRequest,
47     InitData
48   };
49
50   SignalProxy(QObject *parent);
51   SignalProxy(ProxyMode mode, QObject *parent);
52   SignalProxy(ProxyMode mode, QIODevice *device, QObject *parent);
53   virtual ~SignalProxy();
54
55   void setProxyMode(ProxyMode mode);
56   ProxyMode proxyMode() const;
57
58   bool addPeer(QIODevice *iodev);
59   void removePeer(QIODevice *iodev = 0);
60
61   bool attachSignal(QObject *sender, const char *signal, const QByteArray& sigName = QByteArray());
62   bool attachSlot(const QByteArray& sigName, QObject *recv, const char *slot);
63
64   void synchronize(QObject *obj);
65   void synchronizeAsMaster(QObject *obj);
66   void synchronizeAsSlave(QObject *obj);
67
68   void setInitialized(QObject *obj);
69   bool initialized(QObject *obj);
70   void requestInit(QObject *obj);
71   
72   void detachObject(QObject *obj);
73   void detachSignals(QObject *sender);
74   void detachSlots(QObject *receiver);
75   
76   static void writeDataToDevice(QIODevice *dev, const QVariant &item);
77   static bool readDataFromDevice(QIODevice *dev, quint32 &blockSize, QVariant &item);
78
79   static QString methodBaseName(const QMetaMethod &method);
80   
81   const QList<int> &argTypes(QObject *obj, int methodId);
82   const QByteArray &methodName(QObject *obj, int methodId);
83   const QHash<int, int> &syncMap(QObject *obj);
84
85   typedef QHash<int, QList<int> > ArgHash;
86   typedef QHash<int, QByteArray> MethodNameHash;
87   struct ClassInfo {
88     ArgHash argTypes;
89     MethodNameHash methodNames;
90     QHash<int, int> syncMap;
91   };
92
93   void dumpProxyStats();
94   
95 private slots:
96   void dataAvailable();
97   void detachSender();
98   void removePeerBySender();
99   void objectRenamed(QString oldname, QString newname);
100   void objectRenamed(QByteArray classname, QString oldname, QString newname);
101
102 signals:
103   void peerRemoved(QIODevice *obj);
104   void connected();
105   void disconnected();
106   
107 private:
108   void initServer();
109   void initClient();
110   
111   void createClassInfo(QObject *obj);
112   void setArgTypes(QObject *obj, int methodId);
113   void setMethodName(QObject *obj, int methodId);
114   void setSyncMap(QObject *obj);
115
116   bool methodsMatch(const QMetaMethod &signal, const QMetaMethod &slot) const;
117
118   void dispatchSignal(QIODevice *receiver, const QVariant &identifier, const QVariantList &params);
119   void dispatchSignal(const QVariant &identifier, const QVariantList &params);
120   
121   void receivePeerSignal(QIODevice *sender, const QVariant &packedFunc);
122   void handleSync(QVariantList params);
123   void handleInitRequest(QIODevice *sender, const QVariantList &params);
124   void handleInitData(QIODevice *sender, const QVariantList &params);
125   void handleSignal(const QByteArray &funcName, const QVariantList &params);
126
127   bool invokeSlot(QObject *receiver, int methodId, const QVariantList &params);
128
129   QVariantMap initData(QObject *obj) const;
130   void setInitData(QObject *obj, const QVariantMap &properties);
131   bool setInitValue(QObject *obj, const QString &property, const QVariant &value);
132
133   void _detachSignals(QObject *sender);
134   void _detachSlots(QObject *receiver);
135
136   // containg a list of argtypes for fast access
137   QHash<const QMetaObject *, ClassInfo*> _classInfo;
138
139   // we use one SignalRelay per QObject
140   QHash<QObject*, SignalRelay *> _relayHash;
141
142   // RPC function -> (object, slot ID)
143   typedef QPair<QObject*, int> MethodId;
144   typedef QMultiHash<QByteArray, MethodId> SlotHash;
145   SlotHash _attachedSlots;
146
147   // slaves for sync
148   typedef QHash<QString, QObject *> ObjectId;
149   QHash<QByteArray, ObjectId> _syncSlave;
150
151   // Hash of used QIODevices
152   QHash<QIODevice*, quint32> _peerByteCount;
153
154   ProxyMode _proxyMode;
155
156   friend class SignalRelay;
157 };
158
159 #endif