1 /***************************************************************************
2 * Copyright (C) 2005-2020 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)
30 : SyncableObject(QString{}, parent)
33 SyncableObject::SyncableObject(const QString& objectName, QObject* parent)
36 _objectName = objectName;
37 setObjectName(objectName);
39 connect(this, &QObject::objectNameChanged, this, [this](auto&& newName) {
40 for (auto&& proxy : _signalProxies) {
41 proxy->renameObject(this, newName, _objectName);
43 _objectName = newName;
47 SyncableObject::~SyncableObject()
49 QList<SignalProxy*>::iterator proxyIter = _signalProxies.begin();
50 while (proxyIter != _signalProxies.end()) {
51 SignalProxy* proxy = (*proxyIter);
52 proxyIter = _signalProxies.erase(proxyIter);
53 proxy->stopSynchronize(this);
57 bool SyncableObject::isInitialized() const
62 void SyncableObject::setInitialized()
68 QVariantMap SyncableObject::toVariantMap()
70 QVariantMap properties;
72 const QMetaObject* meta = metaObject();
74 // we collect data from properties
77 for (int i = 0; i < meta->propertyCount(); i++) {
78 prop = meta->property(i);
79 propName = QString(prop.name());
80 if (propName == "objectName")
82 properties[propName] = prop.read(this);
85 // ...as well as methods, which have names starting with "init"
86 for (int i = 0; i < meta->methodCount(); i++) {
87 QMetaMethod method = meta->method(i);
88 QString methodname(SignalProxy::ExtendedMetaObject::methodName(method));
89 if (!methodname.startsWith("init") || methodname.startsWith("initSet") || methodname.startsWith("initDone"))
92 QVariant::Type variantType = QVariant::nameToType(method.typeName());
93 if (variantType == QVariant::Invalid && !QByteArray(method.typeName()).isEmpty()) {
94 qWarning() << "SyncableObject::toVariantMap(): cannot fetch init data for:" << this << method.methodSignature()
95 << "- Returntype is unknown to Qt's MetaSystem:" << QByteArray(method.typeName());
99 QVariant value(variantType, (const void*)nullptr);
100 QGenericReturnArgument genericvalue = QGenericReturnArgument(method.typeName(), value.data());
101 QMetaObject::invokeMethod(this, methodname.toLatin1(), genericvalue);
103 properties[SignalProxy::ExtendedMetaObject::methodBaseName(method)] = value;
108 void SyncableObject::fromVariantMap(const QVariantMap& properties)
110 const QMetaObject* meta = metaObject();
112 QVariantMap::const_iterator iterator = properties.constBegin();
114 while (iterator != properties.constEnd()) {
115 propName = iterator.key();
116 if (propName == "objectName") {
121 int propertyIndex = meta->indexOfProperty(propName.toLatin1());
123 if (propertyIndex == -1 || !meta->property(propertyIndex).isWritable())
124 setInitValue(propName, iterator.value());
126 setProperty(propName.toLatin1(), iterator.value());
127 // qDebug() << "<<< SYNC:" << name << iterator.value();
132 bool SyncableObject::setInitValue(const QString& property, const QVariant& value)
134 QString handlername = QString("initSet") + property;
135 handlername[7] = handlername[7].toUpper();
137 QString methodSignature = QString("%1(%2)").arg(handlername).arg(value.typeName());
138 int methodIdx = metaObject()->indexOfMethod(methodSignature.toLatin1().constData());
140 QByteArray normedMethodName = QMetaObject::normalizedSignature(methodSignature.toLatin1().constData());
141 methodIdx = metaObject()->indexOfMethod(normedMethodName.constData());
147 QGenericArgument param(value.typeName(), value.constData());
148 return QMetaObject::invokeMethod(this, handlername.toLatin1(), param);
151 void SyncableObject::update(const QVariantMap& properties)
153 fromVariantMap(properties);
154 SYNC(ARG(properties))
158 void SyncableObject::requestUpdate(const QVariantMap& properties)
160 if (allowClientUpdates()) {
163 REQUEST(ARG(properties))
166 void SyncableObject::sync_call__(SignalProxy::ProxyMode modeType, const char* funcname, ...) const
168 // qDebug() << Q_FUNC_INFO << modeType << funcname;
169 foreach (SignalProxy* proxy, _signalProxies) {
171 va_start(ap, funcname);
172 proxy->sync_call__(this, modeType, funcname, ap);
177 void SyncableObject::synchronize(SignalProxy* proxy)
179 if (_signalProxies.contains(proxy))
181 _signalProxies << proxy;
184 void SyncableObject::stopSynchronize(SignalProxy* proxy)
186 for (int i = 0; i < _signalProxies.count(); i++) {
187 if (_signalProxies[i] == proxy) {
188 _signalProxies.removeAt(i);