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