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>
38 class SignalProxy : public QObject {
53 SignalProxy(QObject *parent);
54 SignalProxy(ProxyMode mode, QObject *parent);
55 SignalProxy(ProxyMode mode, QIODevice *device, QObject *parent);
56 virtual ~SignalProxy();
58 void setProxyMode(ProxyMode mode);
59 ProxyMode proxyMode() const;
61 bool addPeer(QIODevice *iodev);
62 void removePeer(QIODevice *iodev = 0);
64 bool attachSignal(QObject *sender, const char *signal, const QByteArray& sigName = QByteArray());
65 bool attachSlot(const QByteArray& sigName, QObject *recv, const char *slot);
67 void synchronize(SyncableObject *obj);
69 void setInitialized(SyncableObject *obj);
70 bool isInitialized(SyncableObject *obj) const;
71 void requestInit(SyncableObject *obj);
73 void detachObject(QObject *obj);
74 void detachSignals(QObject *sender);
75 void detachSlots(QObject *receiver);
76 void stopSync(SyncableObject *obj);
78 //! Writes a QVariant to a device.
79 /** The data item is prefixed with the resulting blocksize,
80 * so the corresponding function readDataFromDevice() can check if enough data is available
81 * at the device to reread the item.
83 static void writeDataToDevice(QIODevice *dev, const QVariant &item);
85 //! Reads a data item from a device that has been written by writeDataToDevice().
86 /** If not enough data bytes are available, the function returns false and the QVariant reference
89 static bool readDataFromDevice(QIODevice *dev, quint32 &blockSize, QVariant &item);
91 static QString methodBaseName(const QMetaMethod &method);
93 const QList<int> &argTypes(QObject *obj, int methodId);
94 const QByteArray &methodName(QObject *obj, int methodId);
95 const QHash<QByteArray, int> &syncMap(SyncableObject *obj);
97 typedef QHash<int, QList<int> > ArgHash;
98 typedef QHash<int, QByteArray> MethodNameHash;
101 MethodNameHash methodNames;
102 QHash<QByteArray, int> syncMap;
105 void dumpProxyStats();
108 void dataAvailable();
110 void removePeerBySender();
111 void objectRenamed(QString oldname, QString newname);
112 void objectRenamed(QByteArray classname, QString oldname, QString newname);
115 void peerRemoved(QIODevice *obj);
118 void objectInitialized(SyncableObject *);
124 void createClassInfo(QObject *obj);
125 void setArgTypes(QObject *obj, int methodId);
126 void setMethodName(QObject *obj, int methodId);
127 void setSyncMap(SyncableObject *obj);
129 bool methodsMatch(const QMetaMethod &signal, const QMetaMethod &slot) const;
131 void dispatchSignal(QIODevice *receiver, const QVariant &identifier, const QVariantList ¶ms);
132 void dispatchSignal(const QVariant &identifier, const QVariantList ¶ms);
134 void receivePeerSignal(QIODevice *sender, const QVariant &packedFunc);
135 void handleSync(QVariantList params);
136 void handleInitRequest(QIODevice *sender, const QVariantList ¶ms);
137 void handleInitData(QIODevice *sender, const QVariantList ¶ms);
138 void handleSignal(const QByteArray &funcName, const QVariantList ¶ms);
140 bool invokeSlot(QObject *receiver, int methodId, const QVariantList ¶ms);
142 QVariantMap initData(SyncableObject *obj) const;
143 void setInitData(SyncableObject *obj, const QVariantMap &properties);
145 void _detachSignals(QObject *sender);
146 void _detachSlots(QObject *receiver);
147 void _stopSync(SyncableObject *obj);
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 // the slaveMutex protects both containers:
175 // - _syncSlaves for sync and init calls
179 friend class SignalRelay;