fda437f38ba008b556aa71f88377e9175a9be8e2
[quassel.git] / src / common / syncableobject.h
1 /***************************************************************************
2  *   Copyright (C) 2005-09 by the Quassel Project                          *
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) version 3.                                           *
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
21 #ifndef SYNCABLEOBJECT_H
22 #define SYNCABLEOBJECT_H
23
24 #include <QDataStream>
25 #include <QMetaType>
26 #include <QObject>
27 #include <QVariantMap>
28
29 #include "signalproxy.h"
30
31 class SyncableObject : public QObject {
32   SYNCABLE_OBJECT
33   Q_OBJECT
34
35 public:
36   SyncableObject(QObject *parent = 0);
37   SyncableObject(const QString &objectName, QObject *parent = 0);
38   SyncableObject(const SyncableObject &other, QObject *parent = 0);
39   ~SyncableObject();
40
41   //! Stores the object's state into a QVariantMap.
42   /** The default implementation takes dynamic properties as well as getters that have
43    *  names starting with "init" and stores them in a QVariantMap. Override this method in
44    *  derived classes in order to store the object state in a custom form.
45    *  \note  This is used by SignalProxy to transmit the state of the object to clients
46    *         that request the initial object state. Later updates use a different mechanism
47    *         and assume that the state is completely covered by properties and init* getters.
48    *         DO NOT OVERRIDE THIS unless you know exactly what you do!
49    *
50    *  \return The object's state in a QVariantMap
51    */
52   virtual QVariantMap toVariantMap();
53
54   //! Initialize the object's state from a given QVariantMap.
55   /** \see toVariantMap() for important information concerning this method.
56    */
57   virtual void fromVariantMap(const QVariantMap &properties);
58
59   virtual bool isInitialized() const;
60
61   virtual const QMetaObject *syncMetaObject() const { return metaObject(); };
62
63   inline void setAllowClientUpdates(bool allow) { _allowClientUpdates = allow; }
64   inline bool allowClientUpdates() const { return _allowClientUpdates; }
65
66 public slots:
67   virtual void setInitialized();
68   void requestUpdate(const QVariantMap &properties);
69   virtual void update(const QVariantMap &properties);
70
71 protected:
72   void sync_call__(SignalProxy::ProxyMode modeType, const char *funcname, ...) const;
73
74   void renameObject(const QString &newName);
75   SyncableObject &operator=(const SyncableObject &other);
76
77 signals:
78   void initDone();
79   void updatedRemotely();
80   void updated();
81
82 private:
83   void synchronize(SignalProxy *proxy);
84   void stopSynchronize(SignalProxy *proxy);
85
86   bool setInitValue(const QString &property, const QVariant &value);
87
88   bool _initialized;
89   bool _allowClientUpdates;
90
91   QList<SignalProxy *> _signalProxies;
92
93   friend class SignalProxy;
94 };
95
96 #define SYNCABLE_OBJECT static const int _classNameOffset__;
97 #define INIT_SYNCABLE_OBJECT(x) const int x ::_classNameOffset__ = QByteArray(staticMetaObject.className()).length() + 2;
98
99 #ifdef Q_WS_WIN
100 #    define SYNC(...) sync_call__(SignalProxy::Server, (__FUNCTION__ + _classNameOffset__), __VA_ARGS__);
101 #    define REQUEST(...) sync_call__(SignalProxy::Client, (__FUNCTION__ + _classNameOffset__) , __VA_ARGS__);
102 #else
103 #    define SYNC(...) sync_call__(SignalProxy::Server, __func__, __VA_ARGS__);
104 #    define REQUEST(...) sync_call__(SignalProxy::Client, __func__, __VA_ARGS__);
105 #endif //Q_WS_WIN
106
107 #define SYNC_OTHER(x, ...) sync_call__(SignalProxy::Server, #x, __VA_ARGS__);
108 #define REQUEST_OTHER(x, ...) sync_call__(SignalProxy::Client, #x, __VA_ARGS__);
109
110
111 #define ARG(x) const_cast<void *>(reinterpret_cast<const void*>(&x))
112 #define NO_ARG 0
113
114 #endif