1 /***************************************************************************
2 * Copyright (C) 2005-08 by the Quassel Project *
3 * devel@quassel-irc.org *
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. *
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. *
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 ***************************************************************************/
21 #ifndef _SIGNALPROXY_H_
22 #define _SIGNALPROXY_H_
27 #include <QVariantMap>
36 class SignalProxy : public QObject {
51 SignalProxy(QObject *parent);
52 SignalProxy(ProxyMode mode, QObject *parent);
53 SignalProxy(ProxyMode mode, QIODevice *device, QObject *parent);
54 virtual ~SignalProxy();
56 void setProxyMode(ProxyMode mode);
57 ProxyMode proxyMode() const;
59 bool addPeer(QIODevice *iodev);
60 void removePeer(QIODevice *iodev = 0);
62 bool attachSignal(QObject *sender, const char *signal, const QByteArray& sigName = QByteArray());
63 bool attachSlot(const QByteArray& sigName, QObject *recv, const char *slot);
65 void synchronize(SyncableObject *obj);
67 void setInitialized(SyncableObject *obj);
68 bool isInitialized(SyncableObject *obj) const;
69 void requestInit(SyncableObject *obj);
71 void detachObject(QObject *obj);
72 void detachSignals(QObject *sender);
73 void detachSlots(QObject *receiver);
74 void stopSync(SyncableObject *obj);
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.
81 static void writeDataToDevice(QIODevice *dev, const QVariant &item);
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
87 static bool readDataFromDevice(QIODevice *dev, quint32 &blockSize, QVariant &item);
89 static QString methodBaseName(const QMetaMethod &method);
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);
97 typedef QHash<int, QList<int> > ArgHash;
98 typedef QHash<int, QByteArray> MethodNameHash;
101 QHash<int, int> minArgCount;
102 MethodNameHash methodNames;
103 int updatedRemotelyId; // id of the updatedRemotely() signal - makes things faster
104 QHash<QByteArray, int> syncMap;
107 void dumpProxyStats();
110 void dataAvailable();
112 void removePeerBySender();
113 void objectRenamed(QString oldname, QString newname);
114 void objectRenamed(QByteArray classname, QString oldname, QString newname);
117 void peerRemoved(QIODevice *obj);
120 void objectInitialized(SyncableObject *);
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);
133 bool methodsMatch(const QMetaMethod &signal, const QMetaMethod &slot) const;
135 void dispatchSignal(QIODevice *receiver, const QVariant &identifier, const QVariantList ¶ms);
136 void dispatchSignal(const QVariant &identifier, const QVariantList ¶ms);
138 void receivePeerSignal(QIODevice *sender, const QVariant &packedFunc);
139 void handleSync(QVariantList params);
140 void handleInitRequest(QIODevice *sender, const QVariantList ¶ms);
141 void handleInitData(QIODevice *sender, const QVariantList ¶ms);
142 void handleSignal(const QByteArray &funcName, const QVariantList ¶ms);
144 bool invokeSlot(QObject *receiver, int methodId, const QVariantList ¶ms = QVariantList());
146 QVariantMap initData(SyncableObject *obj) const;
147 void setInitData(SyncableObject *obj, const QVariantMap &properties);
150 void dumpSyncMap(SyncableObject *object);
153 // Hash of used QIODevices
154 QHash<QIODevice*, quint32> _peerByteCount;
156 // containg a list of argtypes for fast access
157 QHash<const QMetaObject *, ClassInfo*> _classInfo;
159 // we use one SignalRelay per QObject
160 QHash<QObject*, SignalRelay *> _relayHash;
162 // RPC function -> (object, slot ID)
163 typedef QPair<QObject*, int> MethodId;
164 typedef QMultiHash<QByteArray, MethodId> SlotHash;
165 SlotHash _attachedSlots;
168 typedef QHash<QString, SyncableObject *> ObjectId;
169 QHash<QByteArray, ObjectId> _syncSlave;
172 ProxyMode _proxyMode;
174 friend class SignalRelay;