1 /***************************************************************************
2 * Copyright (C) 2005-09 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 SYNCABLEOBJECT_H
22 #define SYNCABLEOBJECT_H
24 #include <QDataStream>
27 #include <QVariantMap>
29 #include "signalproxy.h"
31 class SyncableObject : public QObject {
35 SyncableObject(QObject *parent = 0);
36 SyncableObject(const QString &objectName, QObject *parent = 0);
37 SyncableObject(const SyncableObject &other, QObject *parent = 0);
39 //! Stores the object's state into a QVariantMap.
40 /** The default implementation takes dynamic properties as well as getters that have
41 * names starting with "init" and stores them in a QVariantMap. Override this method in
42 * derived classes in order to store the object state in a custom form.
43 * \note This is used by SignalProxy to transmit the state of the object to clients
44 * that request the initial object state. Later updates use a different mechanism
45 * and assume that the state is completely covered by properties and init* getters.
46 * DO NOT OVERRIDE THIS unless you know exactly what you do!
48 * \return The object's state in a QVariantMap
50 virtual QVariantMap toVariantMap();
52 //! Initialize the object's state from a given QVariantMap.
53 /** \see toVariantMap() for important information concerning this method.
55 virtual void fromVariantMap(const QVariantMap &properties);
57 virtual bool isInitialized() const;
59 virtual const QMetaObject *syncMetaObject() const { return metaObject(); };
61 inline void setAllowClientUpdates(bool allow) { _allowClientUpdates = allow; }
62 inline bool allowClientUpdates() const { return _allowClientUpdates; }
65 virtual void setInitialized();
66 void requestUpdate(const QVariantMap &properties);
67 virtual void update(const QVariantMap &properties);
70 void sync_call__(SignalProxy::ProxyMode modeType, const char *funcname, ...) const;
72 void renameObject(const QString &newName);
73 SyncableObject &operator=(const SyncableObject &other);
77 void updatedRemotely();
78 void updated(const QVariantMap &properties);
79 void updateRequested(const QVariantMap &properties);
80 void objectRenamed(QString newName, QString oldName);
83 void synchronize(SignalProxy *proxy);
85 bool setInitValue(const QString &property, const QVariant &value);
88 bool _allowClientUpdates;
90 QList<SignalProxy *> _signalProxies;
92 friend class SignalProxy;
95 #define SYNCABLE_OBJECT static const int _classNameOffset__;
96 #define INIT_SYNCABLE_OBJECT(x) const int x ::_classNameOffset__ = QByteArray(staticMetaObject.className()).length() + 2;
99 # define SYNC(...) sync_call__(SignalProxy::Server, (__FUNCTION__ + _classNameOffset__), __VA_ARGS__);
100 # define REQUEST(...) sync_call__(SignalProxy::Client, (__FUNCTION__ + _classNameOffset__) , __VA_ARGS__);
102 # define SYNC(...) sync_call__(SignalProxy::Server, __func__, __VA_ARGS__);
103 # define SYNC_OTHER(x, ...) sync_call__(SignalProxy::Server, #x, __VA_ARGS__);
104 # define REQUEST(...) sync_call__(SignalProxy::Client, __func__, __VA_ARGS__);
105 # define REQUEST_OTHER(x, ...) sync_call__(SignalProxy::Client, #x, __VA_ARGS__);
109 #define ARG(x) const_cast<void *>(reinterpret_cast<const void*>(&x))