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