b8c8d5ba492474e600079c927913431e7f838356
[quassel.git] / src / common / syncableobject.cpp
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 #include <QMetaProperty>
22
23 #include <QDebug>
24
25 #include "syncableobject.h"
26
27 #include "signalproxy.h"
28 #include "util.h"
29
30 SyncableObject::SyncableObject(QObject *parent)
31   : QObject(parent),
32     _initialized(false),
33     _allowClientUpdates(false)
34 {
35 }
36
37 SyncableObject::SyncableObject(const QString &objectName, QObject *parent)
38   : QObject(parent),
39     _initialized(false),
40     _allowClientUpdates(false)
41 {
42   setObjectName(objectName);
43 }
44
45 SyncableObject::SyncableObject(const SyncableObject &other, QObject *parent)
46   : QObject(parent),
47     _initialized(other._initialized),
48     _allowClientUpdates(other._allowClientUpdates)
49 {
50 }
51
52 SyncableObject::~SyncableObject() {
53   QList<SignalProxy *> proxies = _signalProxies;
54   _signalProxies.clear();
55   for(int i = 0; i < proxies.count(); i++) {
56     proxies[i]->stopSynchronize(this);
57   }
58 }
59
60 SyncableObject &SyncableObject::operator=(const SyncableObject &other) {
61   if(this == &other)
62     return *this;
63
64   _initialized = other._initialized;
65   _allowClientUpdates = other._allowClientUpdates;
66   return *this;
67 }
68
69 bool SyncableObject::isInitialized() const {
70   return _initialized;
71 }
72
73 void SyncableObject::setInitialized() {
74   _initialized = true;
75   emit initDone();
76 }
77
78 QVariantMap SyncableObject::toVariantMap() {
79   QVariantMap properties;
80
81   const QMetaObject* meta = metaObject();
82
83   // we collect data from properties
84   QMetaProperty prop;
85   QString propName;
86   for(int i = 0; i < meta->propertyCount(); i++) {
87     prop = meta->property(i);
88     propName = QString(prop.name());
89     if(propName == "objectName")
90       continue;
91     properties[propName] = prop.read(this);
92   }
93
94   // ...as well as methods, which have names starting with "init"
95   for(int i = 0; i < meta->methodCount(); i++) {
96     QMetaMethod method = meta->method(i);
97     QString methodname(SignalProxy::ExtendedMetaObject::methodName(method));
98     if(!methodname.startsWith("init") || methodname.startsWith("initSet") || methodname.startsWith("initDone"))
99       continue;
100
101     QVariant::Type variantType = QVariant::nameToType(method.typeName());
102     if(variantType == QVariant::Invalid && !QByteArray(method.typeName()).isEmpty()) {
103       qWarning() << "SyncableObject::toVariantMap(): cannot fetch init data for:" << this << method.signature() << "- Returntype is unknown to Qt's MetaSystem:" << QByteArray(method.typeName());
104       continue;
105     }
106     QVariant value = QVariant(variantType);
107     QGenericReturnArgument genericvalue = QGenericReturnArgument(method.typeName(), &value);
108     QMetaObject::invokeMethod(this, methodname.toAscii(), genericvalue);
109
110     properties[SignalProxy::ExtendedMetaObject::methodBaseName(method)] = value;
111   }
112   // properties["Payload"] = QByteArray(10000000, 'a');  // for testing purposes
113   return properties;
114
115 }
116
117 void SyncableObject::fromVariantMap(const QVariantMap &properties) {
118   const QMetaObject *meta = metaObject();
119
120   QVariantMap::const_iterator iterator = properties.constBegin();
121   QString propName;
122   while(iterator != properties.constEnd()) {
123     propName = iterator.key();
124     if(propName == "objectName") {
125       iterator++;
126       continue;
127     }
128
129     int propertyIndex = meta->indexOfProperty(propName.toAscii());
130
131     if(propertyIndex == -1 || !meta->property(propertyIndex).isWritable())
132       setInitValue(propName, iterator.value());
133     else
134       setProperty(propName.toAscii(), iterator.value());
135     // qDebug() << "<<< SYNC:" << name << iterator.value();
136     iterator++;
137   }
138 }
139
140 bool SyncableObject::setInitValue(const QString &property, const QVariant &value) {
141   QString handlername = QString("initSet") + property;
142   handlername[7] = handlername[7].toUpper();
143   QGenericArgument param(value.typeName(), value.constData());
144   return QMetaObject::invokeMethod(this, handlername.toAscii(), param);
145 }
146
147 void SyncableObject::renameObject(const QString &newName) {
148   const QString oldName = objectName();
149   if(oldName != newName) {
150     setObjectName(newName);
151     foreach(SignalProxy *proxy, _signalProxies) {
152       proxy->renameObject(this, newName, oldName);
153     }
154   }
155 }
156
157 void SyncableObject::update(const QVariantMap &properties) {
158   fromVariantMap(properties);
159   emit updated(properties);
160 }
161
162 void SyncableObject::requestUpdate(const QVariantMap &properties) {
163   if(allowClientUpdates()) {
164     update(properties);
165   }
166   emit updateRequested(properties);
167 }
168
169 void SyncableObject::sync_call__(SignalProxy::ProxyMode modeType, const char *funcname, ...) const {
170   //qDebug() << Q_FUNC_INFO << modeType << funcname;
171   foreach(SignalProxy *proxy, _signalProxies) {
172     va_list ap;
173     va_start(ap, funcname);
174     proxy->sync_call__(this, modeType, funcname, ap);
175     va_end(ap);
176   }
177 }
178
179 void SyncableObject::synchronize(SignalProxy *proxy) {
180   if(_signalProxies.contains(proxy))
181     return;
182   _signalProxies << proxy;
183 }
184
185 void SyncableObject::stopSynchronize(SignalProxy *proxy) {
186   for(int i = 0; i < _signalProxies.count(); i++) {
187     if(_signalProxies[i] == proxy) {
188       _signalProxies.removeAt(i);
189       break;
190     }
191   }
192 }