1 /***************************************************************************
2 * Copyright (C) 2005-2018 by the Quassel Project *
3 * devel@quassel-irc.org *
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. *
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. *
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 ***************************************************************************/
21 #include "syncableobject.h"
24 #include <QMetaProperty>
26 #include "signalproxy.h"
29 SyncableObject::SyncableObject(QObject* parent)
33 SyncableObject::SyncableObject(const QString& objectName, QObject* parent)
36 setObjectName(objectName);
39 SyncableObject::SyncableObject(const SyncableObject& other, QObject* parent)
41 , _initialized(other._initialized)
42 , _allowClientUpdates(other._allowClientUpdates)
45 SyncableObject::~SyncableObject()
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);
55 SyncableObject& SyncableObject::operator=(const SyncableObject& other)
60 _initialized = other._initialized;
61 _allowClientUpdates = other._allowClientUpdates;
65 bool SyncableObject::isInitialized() const
70 void SyncableObject::setInitialized()
76 QVariantMap SyncableObject::toVariantMap()
78 QVariantMap properties;
80 const QMetaObject* meta = metaObject();
82 // we collect data from properties
85 for (int i = 0; i < meta->propertyCount(); i++) {
86 prop = meta->property(i);
87 propName = QString(prop.name());
88 if (propName == "objectName")
90 properties[propName] = prop.read(this);
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"))
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());
107 QVariant value(variantType, (const void*)nullptr);
108 QGenericReturnArgument genericvalue = QGenericReturnArgument(method.typeName(), value.data());
109 QMetaObject::invokeMethod(this, methodname.toLatin1(), genericvalue);
111 properties[SignalProxy::ExtendedMetaObject::methodBaseName(method)] = value;
116 void SyncableObject::fromVariantMap(const QVariantMap& properties)
118 const QMetaObject* meta = metaObject();
120 QVariantMap::const_iterator iterator = properties.constBegin();
122 while (iterator != properties.constEnd()) {
123 propName = iterator.key();
124 if (propName == "objectName") {
129 int propertyIndex = meta->indexOfProperty(propName.toLatin1());
131 if (propertyIndex == -1 || !meta->property(propertyIndex).isWritable())
132 setInitValue(propName, iterator.value());
134 setProperty(propName.toLatin1(), iterator.value());
135 // qDebug() << "<<< SYNC:" << name << iterator.value();
140 bool SyncableObject::setInitValue(const QString& property, const QVariant& value)
142 QString handlername = QString("initSet") + property;
143 handlername[7] = handlername[7].toUpper();
145 QString methodSignature = QString("%1(%2)").arg(handlername).arg(value.typeName());
146 int methodIdx = metaObject()->indexOfMethod(methodSignature.toLatin1().constData());
148 QByteArray normedMethodName = QMetaObject::normalizedSignature(methodSignature.toLatin1().constData());
149 methodIdx = metaObject()->indexOfMethod(normedMethodName.constData());
155 QGenericArgument param(value.typeName(), value.constData());
156 return QMetaObject::invokeMethod(this, handlername.toLatin1(), param);
159 void SyncableObject::renameObject(const QString& newName)
161 const QString oldName = objectName();
162 if (oldName != newName) {
163 setObjectName(newName);
164 foreach (SignalProxy* proxy, _signalProxies) {
165 proxy->renameObject(this, newName, oldName);
170 void SyncableObject::update(const QVariantMap& properties)
172 fromVariantMap(properties);
173 SYNC(ARG(properties))
177 void SyncableObject::requestUpdate(const QVariantMap& properties)
179 if (allowClientUpdates()) {
182 REQUEST(ARG(properties))
185 void SyncableObject::sync_call__(SignalProxy::ProxyMode modeType, const char* funcname, ...) const
187 // qDebug() << Q_FUNC_INFO << modeType << funcname;
188 foreach (SignalProxy* proxy, _signalProxies) {
190 va_start(ap, funcname);
191 proxy->sync_call__(this, modeType, funcname, ap);
196 void SyncableObject::synchronize(SignalProxy* proxy)
198 if (_signalProxies.contains(proxy))
200 _signalProxies << proxy;
203 void SyncableObject::stopSynchronize(SignalProxy* proxy)
205 for (int i = 0; i < _signalProxies.count(); i++) {
206 if (_signalProxies[i] == proxy) {
207 _signalProxies.removeAt(i);