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