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 <QMetaProperty>
25 #include "syncableobject.h"
27 #include "signalproxy.h"
30 SyncableObject::SyncableObject(QObject *parent)
36 SyncableObject::SyncableObject(const QString &objectName, QObject *parent)
39 setObjectName(objectName);
43 SyncableObject::SyncableObject(const SyncableObject &other, QObject *parent)
45 _initialized(other._initialized),
46 _allowClientUpdates(other._allowClientUpdates)
51 SyncableObject::~SyncableObject()
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);
62 SyncableObject &SyncableObject::operator=(const SyncableObject &other)
67 _initialized = other._initialized;
68 _allowClientUpdates = other._allowClientUpdates;
73 bool SyncableObject::isInitialized() const
79 void SyncableObject::setInitialized()
86 QVariantMap SyncableObject::toVariantMap()
88 QVariantMap properties;
90 const QMetaObject *meta = metaObject();
92 // we collect data from properties
95 for (int i = 0; i < meta->propertyCount(); i++) {
96 prop = meta->property(i);
97 propName = QString(prop.name());
98 if (propName == "objectName")
100 properties[propName] = prop.read(this);
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"))
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());
116 QVariant value(variantType, (const void *)nullptr);
117 QGenericReturnArgument genericvalue = QGenericReturnArgument(method.typeName(), value.data());
118 QMetaObject::invokeMethod(this, methodname.toLatin1(), genericvalue);
120 properties[SignalProxy::ExtendedMetaObject::methodBaseName(method)] = value;
126 void SyncableObject::fromVariantMap(const QVariantMap &properties)
128 const QMetaObject *meta = metaObject();
130 QVariantMap::const_iterator iterator = properties.constBegin();
132 while (iterator != properties.constEnd()) {
133 propName = iterator.key();
134 if (propName == "objectName") {
139 int propertyIndex = meta->indexOfProperty(propName.toLatin1());
141 if (propertyIndex == -1 || !meta->property(propertyIndex).isWritable())
142 setInitValue(propName, iterator.value());
144 setProperty(propName.toLatin1(), iterator.value());
145 // qDebug() << "<<< SYNC:" << name << iterator.value();
151 bool SyncableObject::setInitValue(const QString &property, const QVariant &value)
153 QString handlername = QString("initSet") + property;
154 handlername[7] = handlername[7].toUpper();
156 QString methodSignature = QString("%1(%2)").arg(handlername).arg(value.typeName());
157 int methodIdx = metaObject()->indexOfMethod(methodSignature.toLatin1().constData());
159 QByteArray normedMethodName = QMetaObject::normalizedSignature(methodSignature.toLatin1().constData());
160 methodIdx = metaObject()->indexOfMethod(normedMethodName.constData());
166 QGenericArgument param(value.typeName(), value.constData());
167 return QMetaObject::invokeMethod(this, handlername.toLatin1(), param);
171 void SyncableObject::renameObject(const QString &newName)
173 const QString oldName = objectName();
174 if (oldName != newName) {
175 setObjectName(newName);
176 foreach(SignalProxy *proxy, _signalProxies) {
177 proxy->renameObject(this, newName, oldName);
183 void SyncableObject::update(const QVariantMap &properties)
185 fromVariantMap(properties);
186 SYNC(ARG(properties))
191 void SyncableObject::requestUpdate(const QVariantMap &properties)
193 if (allowClientUpdates()) {
196 REQUEST(ARG(properties))
200 void SyncableObject::sync_call__(SignalProxy::ProxyMode modeType, const char *funcname, ...) const
202 //qDebug() << Q_FUNC_INFO << modeType << funcname;
203 foreach(SignalProxy *proxy, _signalProxies) {
205 va_start(ap, funcname);
206 proxy->sync_call__(this, modeType, funcname, ap);
212 void SyncableObject::synchronize(SignalProxy *proxy)
214 if (_signalProxies.contains(proxy))
216 _signalProxies << proxy;
220 void SyncableObject::stopSynchronize(SignalProxy *proxy)
222 for (int i = 0; i < _signalProxies.count(); i++) {
223 if (_signalProxies[i] == proxy) {
224 _signalProxies.removeAt(i);