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