Quassel now handles even huge ChatScenes without slowing to a crawl.
[quassel.git] / src / common / syncableobject.h
1 /***************************************************************************
2  *   Copyright (C) 2005-08 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) 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 class SyncableObject : public QObject {
30   Q_OBJECT
31
32 public:
33   SyncableObject(QObject *parent = 0);
34   SyncableObject(const SyncableObject &other, QObject *parent = 0);
35
36   //! Stores the object's state into a QVariantMap.
37   /** The default implementation takes dynamic properties as well as getters that have
38    *  names starting with "init" and stores them in a QVariantMap. Override this method in
39    *  derived classes in order to store the object state in a custom form.
40    *  \note  This is used by SignalProxy to transmit the state of the object to clients
41    *         that request the initial object state. Later updates use a different mechanism
42    *         and assume that the state is completely covered by properties and init* getters.
43    *         DO NOT OVERRIDE THIS unless you know exactly what you do!
44    *
45    *  \return The object's state in a QVariantMap
46    */
47   virtual QVariantMap toVariantMap();
48   
49   //! Initialize the object's state from a given QVariantMap.
50   /** \see toVariantMap() for important information concerning this method.
51    */
52   virtual void fromVariantMap(const QVariantMap &properties);
53
54   virtual bool isInitialized() const;
55
56   virtual const QMetaObject *syncMetaObject() const { return metaObject(); };
57
58   inline void setAllowClientUpdates(bool allow) { _allowClientUpdates = allow; }
59   inline bool allowClientUpdates() const { return _allowClientUpdates; }
60
61 public slots:
62   virtual void setInitialized();
63   void requestUpdate(const QVariantMap &properties);
64   void update(const QVariantMap &properties);
65
66 protected:
67   void renameObject(const QString &newName);
68
69 signals:
70   void initDone();
71   void updatedRemotely();
72   void updated(const QVariantMap &properties);
73   void updateRequested(const QVariantMap &properties);
74   void objectRenamed(QString newName, QString oldName);
75
76 private:
77   bool setInitValue(const QString &property, const QVariant &value);
78   
79   bool _initialized;
80   bool _allowClientUpdates;
81
82 };
83
84 #endif