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 initialized(SyncableObject *obj);
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 bool hasUpdateSignal(QObject *obj);
95 const QByteArray &methodName(QObject *obj, int methodId);
96 const QHash<QByteArray, int> &syncMap(SyncableObject *obj);
98 typedef QHash<int, QList<int> > ArgHash;
99 typedef QHash<int, QByteArray> MethodNameHash;
102 MethodNameHash methodNames;
103 QHash<QByteArray, int> syncMap;
104 bool hasUpdateSignal;
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);
125 void createClassInfo(QObject *obj);
126 void setArgTypes(QObject *obj, int methodId);
127 void setMethodName(QObject *obj, int methodId);
128 void setSyncMap(SyncableObject *obj);
130 bool methodsMatch(const QMetaMethod &signal, const QMetaMethod &slot) const;
132 void dispatchSignal(QIODevice *receiver, const QVariant &identifier, const QVariantList ¶ms);
133 void dispatchSignal(const QVariant &identifier, const QVariantList ¶ms);
135 void receivePeerSignal(QIODevice *sender, const QVariant &packedFunc);
136 void handleSync(QVariantList params);
137 void handleInitRequest(QIODevice *sender, const QVariantList ¶ms);
138 void handleInitData(QIODevice *sender, const QVariantList ¶ms);
139 void handleSignal(const QByteArray &funcName, const QVariantList ¶ms);
141 bool invokeSlot(QObject *receiver, int methodId, const QVariantList ¶ms);
143 QVariantMap initData(SyncableObject *obj) const;
144 void setInitData(SyncableObject *obj, const QVariantMap &properties);
146 void _detachSignals(QObject *sender);
147 void _detachSlots(QObject *receiver);
148 void _stopSync(SyncableObject *obj);
150 void dumpSyncMap(SyncableObject *object);
152 // Hash of used QIODevices
153 QHash<QIODevice*, quint32> _peerByteCount;
155 // containg a list of argtypes for fast access
156 QHash<const QMetaObject *, ClassInfo*> _classInfo;
158 // we use one SignalRelay per QObject
159 QHash<QObject*, SignalRelay *> _relayHash;
161 // RPC function -> (object, slot ID)
162 typedef QPair<QObject*, int> MethodId;
163 typedef QMultiHash<QByteArray, MethodId> SlotHash;
164 SlotHash _attachedSlots;
167 typedef QHash<QString, SyncableObject *> ObjectId;
168 QHash<QByteArray, ObjectId> _syncSlave;
171 ProxyMode _proxyMode;
173 // the slaveMutex protects both containers:
174 // - _syncSlaves for sync and init calls
178 friend class SignalRelay;