1e013f7c0e08e8ab080f412a3d461f1227a57cbf
[quassel.git] / src / common / signalproxy.h
1 /****************************************************************************
2  **
3  ** Copyright (C) Qxt Foundation. Some rights reserved.
4  **
5  ** This file is part of the QxtNetwork module of the Qt eXTension library
6  **
7  ** This library is free software; you can redistribute it and/or modify it
8  ** under the terms of th Common Public License, version 1.0, as published by
9  ** IBM.
10  **
11  ** This file is provided "AS IS", without WARRANTIES OR CONDITIONS OF ANY
12  ** KIND, EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, ANY
13  ** WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY OR
14  ** FITNESS FOR A PARTICULAR PURPOSE.
15  **
16  ** You should have received a copy of the CPL along with this file.
17  ** See the LICENSE file and the cpl1.0.txt file included with the source
18  ** distribution for more information. If you did not receive a copy of the
19  ** license, contact the Qxt Foundation.
20  **
21  ** <http://libqxt.sourceforge.net>  <foundation@libqxt.org>
22  **
23  ****************************************************************************/
24
25 #ifndef _SIGNALPROXY_H_
26 #define _SIGNALPROXY_H_
27
28 #include <QObject>
29 #include <QList>
30 #include <QHash>
31 #include <QVariant>
32 #include <QPair>
33 #include <QString>
34 #include <QByteArray>
35
36 class ClassIntrospector;
37
38 class SignalProxy : public QObject {
39   Q_OBJECT
40
41 public:
42   enum RPCTypes {
43     Server,
44     Client,
45     Peer
46   };
47
48   SignalProxy(QObject* parent);
49   SignalProxy(RPCTypes type, QObject* parent);
50   SignalProxy(RPCTypes type, QIODevice* device, QObject* parent);
51   virtual ~SignalProxy();
52
53   void setRPCType(RPCTypes type);
54   RPCTypes rpcType() const;
55
56   bool maxPeersReached();
57   
58   bool addPeer(QIODevice *iodev);
59   void removePeer(QIODevice *iodev = 0);
60
61   bool attachSignal(QObject* sender, const char* signal, const QByteArray& rpcFunction = QByteArray());
62   bool attachSlot(const QByteArray& rpcFunction, QObject* recv, const char* slot);
63
64   void detachObject(QObject *obj);
65   void detachSignals(QObject *sender);
66   void detachSlots(QObject *receiver);
67   
68   void call(const char *signal , QVariant p1, QVariant p2, QVariant p3, QVariant p4,
69             QVariant p5, QVariant p6, QVariant p7, QVariant p8, QVariant p9);
70   void call(const QByteArray &funcName, const QVariantList &params);
71
72   static void writeDataToDevice(QIODevice *dev, const QVariant &item);
73   static bool readDataFromDevice(QIODevice *dev, quint32 &blockSize, QVariant &item);
74   
75   const QList<int> &argTypes(QObject* obj, int methodId);
76   const QByteArray &methodName(QObject* obj, int methodId);
77   
78   typedef QHash<int, QList<int> > ArgHash;
79   typedef QHash<int, QByteArray> MethodNameHash;
80   struct ClassInfo {
81     ArgHash argTypes;
82     MethodNameHash methodNames;
83     // QHash<int, int> syncMap
84   };
85   
86 private slots:
87   void dataAvailable();
88   void detachSender();
89   void removePeerBySender();
90
91 signals:
92   void peerRemoved(QIODevice* obj);
93   void connected();
94   void disconnected();
95   
96 private:
97   void createClassInfo(QObject *obj);
98   void setArgTypes(QObject* obj, int methodId);
99   void setMethodName(QObject* obj, int methodId);
100   
101   void receivePeerSignal(const QVariant &packedFunc);
102
103   void _detachSignals(QObject *sender);
104   void _detachSlots(QObject *receiver);
105
106   // containg a list of argtypes for fast access
107   QHash<QByteArray, ClassInfo*> _classInfo;
108   
109   // we use one introSpector per QObject
110   QHash<QObject*, ClassIntrospector*> _specHash;
111
112   // RPC function -> (object, slot ID)
113   typedef QPair<QObject*, int> MethodId;
114   typedef QMultiHash<QByteArray, MethodId> SlotHash;
115   SlotHash _attachedSlots;
116   
117
118   // Hash of used QIODevices
119   QHash<QIODevice*, quint32> _peerByteCount;
120
121   int _rpcType;
122   int _maxClients;
123 };
124
125
126
127
128 #endif