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