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