Prettified SignalProxy some more, updated license headers.
[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  *   SignalProxy has been inspired by QxtRPCPeer, part of libqxt,          *
21  *   the Qt eXTension Library <http://www.libqxt.org>. We would like to    *
22  *   thank Arvid "aep" Picciani and Adam "ahigerd" Higerd for providing    *
23  *   QxtRPCPeer, valuable input and the genius idea to (ab)use Qt's        *
24  *   Meta Object System for transmitting signals over the network.         *
25  *                                                                         *
26  *   To make contribution back into libqxt possible, redistribution and    *
27  *   modification of this file is additionally allowed under the terms of  *
28  *   the Common Public License, version 1.0, as published by IBM.          *
29  ***************************************************************************/
30
31 #ifndef _SIGNALPROXY_H_
32 #define _SIGNALPROXY_H_
33
34 #include <QList>
35 #include <QHash>
36 #include <QVariant>
37 #include <QPair>
38 #include <QString>
39 #include <QByteArray>
40
41 class SignalRelay;
42
43 class SignalProxy : public QObject {
44   Q_OBJECT
45
46 public:
47   enum ProxyMode {
48     Server,
49     Client
50   };
51
52   SignalProxy(QObject* parent);
53   SignalProxy(ProxyMode mode, QObject* parent);
54   SignalProxy(ProxyMode mode, QIODevice* device, QObject* parent);
55   virtual ~SignalProxy();
56
57   void setProxyMode(ProxyMode mode);
58   ProxyMode proxyMode() const;
59
60   bool maxPeersReached();
61   
62   bool addPeer(QIODevice *iodev);
63   void removePeer(QIODevice *iodev = 0);
64
65   bool attachSignal(QObject* sender, const char* signal, const QByteArray& sigName = QByteArray());
66   bool attachSlot(const QByteArray& sigName, QObject* recv, const char* slot);
67
68   void detachObject(QObject *obj);
69   void detachSignals(QObject *sender);
70   void detachSlots(QObject *receiver);
71   
72   void call(const char *signal , QVariant p1, QVariant p2, QVariant p3, QVariant p4,
73             QVariant p5, QVariant p6, QVariant p7, QVariant p8, QVariant p9);
74   void call(const QByteArray &funcName, const QVariantList &params);
75
76   static void writeDataToDevice(QIODevice *dev, const QVariant &item);
77   static bool readDataFromDevice(QIODevice *dev, quint32 &blockSize, QVariant &item);
78   
79   const QList<int> &argTypes(QObject* obj, int methodId);
80   const QByteArray &methodName(QObject* obj, int methodId);
81   
82   typedef QHash<int, QList<int> > ArgHash;
83   typedef QHash<int, QByteArray> MethodNameHash;
84   struct ClassInfo {
85     ArgHash argTypes;
86     MethodNameHash methodNames;
87     // QHash<int, int> syncMap
88   };
89   
90 private slots:
91   void dataAvailable();
92   void detachSender();
93   void removePeerBySender();
94
95 signals:
96   void peerRemoved(QIODevice* obj);
97   void connected();
98   void disconnected();
99   
100 private:
101   void createClassInfo(QObject *obj);
102   void setArgTypes(QObject* obj, int methodId);
103   void setMethodName(QObject* obj, int methodId);
104
105   void receivePeerSignal(const QVariant &packedFunc);
106
107   void _detachSignals(QObject *sender);
108   void _detachSlots(QObject *receiver);
109
110   // containg a list of argtypes for fast access
111   QHash<QByteArray, ClassInfo*> _classInfo;
112
113   // we use one SignalRelay per QObject
114   QHash<QObject*, SignalRelay *> _relayHash;
115
116   // RPC function -> (object, slot ID)
117   typedef QPair<QObject*, int> MethodId;
118   typedef QMultiHash<QByteArray, MethodId> SlotHash;
119   SlotHash _attachedSlots;
120   
121
122   // Hash of used QIODevices
123   QHash<QIODevice*, quint32> _peerByteCount;
124
125   ProxyMode _proxyMode;
126   int _maxClients;
127 };
128
129
130
131
132 #endif