1 /***************************************************************************
2 * Copyright (C) 2005-07 by the Quassel IRC Team *
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>
35 class SignalProxy : public QObject {
50 SignalProxy(QObject *parent);
51 SignalProxy(ProxyMode mode, QObject *parent);
52 SignalProxy(ProxyMode mode, QIODevice *device, QObject *parent);
53 virtual ~SignalProxy();
55 void setProxyMode(ProxyMode mode);
56 ProxyMode proxyMode() const;
58 bool addPeer(QIODevice *iodev);
59 void removePeer(QIODevice *iodev = 0);
61 bool attachSignal(QObject *sender, const char *signal, const QByteArray& sigName = QByteArray());
62 bool attachSlot(const QByteArray& sigName, QObject *recv, const char *slot);
64 void synchronize(QObject *obj);
65 void synchronizeAsMaster(QObject *obj);
66 void synchronizeAsSlave(QObject *obj);
68 void setInitialized(QObject *obj);
69 bool initialized(QObject *obj);
70 void requestInit(QObject *obj);
72 void detachObject(QObject *obj);
73 void detachSignals(QObject *sender);
74 void detachSlots(QObject *receiver);
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 QByteArray &methodName(QObject *obj, int methodId);
93 const QHash<int, int> &syncMap(QObject *obj);
95 typedef QHash<int, QList<int> > ArgHash;
96 typedef QHash<int, QByteArray> MethodNameHash;
99 MethodNameHash methodNames;
100 QHash<int, int> syncMap;
103 void dumpProxyStats();
106 void dataAvailable();
108 void removePeerBySender();
109 void objectRenamed(QString oldname, QString newname);
110 void objectRenamed(QByteArray classname, QString oldname, QString newname);
113 void peerRemoved(QIODevice *obj);
121 void createClassInfo(QObject *obj);
122 void setArgTypes(QObject *obj, int methodId);
123 void setMethodName(QObject *obj, int methodId);
124 void setSyncMap(QObject *obj);
126 bool methodsMatch(const QMetaMethod &signal, const QMetaMethod &slot) const;
128 void dispatchSignal(QIODevice *receiver, const QVariant &identifier, const QVariantList ¶ms);
129 void dispatchSignal(const QVariant &identifier, const QVariantList ¶ms);
131 void receivePeerSignal(QIODevice *sender, const QVariant &packedFunc);
132 void handleSync(QVariantList params);
133 void handleInitRequest(QIODevice *sender, const QVariantList ¶ms);
134 void handleInitData(QIODevice *sender, const QVariantList ¶ms);
135 void handleSignal(const QByteArray &funcName, const QVariantList ¶ms);
137 bool invokeSlot(QObject *receiver, int methodId, const QVariantList ¶ms);
139 QVariantMap initData(QObject *obj) const;
140 void setInitData(QObject *obj, const QVariantMap &properties);
141 bool setInitValue(QObject *obj, const QString &property, const QVariant &value);
143 void _detachSignals(QObject *sender);
144 void _detachSlots(QObject *receiver);
146 void dumpSyncMap(QObject *object);
148 // containg a list of argtypes for fast access
149 QHash<const QMetaObject *, ClassInfo*> _classInfo;
151 // we use one SignalRelay per QObject
152 QHash<QObject*, SignalRelay *> _relayHash;
154 // RPC function -> (object, slot ID)
155 typedef QPair<QObject*, int> MethodId;
156 typedef QMultiHash<QByteArray, MethodId> SlotHash;
157 SlotHash _attachedSlots;
160 typedef QHash<QString, QObject *> ObjectId;
161 QHash<QByteArray, ObjectId> _syncSlave;
163 // Hash of used QIODevices
164 QHash<QIODevice*, quint32> _peerByteCount;
166 ProxyMode _proxyMode;
168 friend class SignalRelay;