1 /***************************************************************************
2 * Copyright (C) 2005-09 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 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
21 #include <QMetaProperty>
25 #include "syncableobject.h"
27 #include "signalproxy.h"
30 INIT_SYNCABLE_OBJECT(SyncableObject)
31 SyncableObject::SyncableObject(QObject *parent)
34 _allowClientUpdates(false)
38 SyncableObject::SyncableObject(const QString &objectName, QObject *parent)
41 _allowClientUpdates(false)
43 setObjectName(objectName);
46 SyncableObject::SyncableObject(const SyncableObject &other, QObject *parent)
48 _initialized(other._initialized),
49 _allowClientUpdates(other._allowClientUpdates)
53 SyncableObject::~SyncableObject() {
54 QList<SignalProxy *>::iterator proxyIter = _signalProxies.begin();
55 while(proxyIter != _signalProxies.end()) {
56 SignalProxy *proxy = (*proxyIter);
57 proxyIter = _signalProxies.erase(proxyIter);
58 proxy->stopSynchronize(this);
62 SyncableObject &SyncableObject::operator=(const SyncableObject &other) {
66 _initialized = other._initialized;
67 _allowClientUpdates = other._allowClientUpdates;
71 bool SyncableObject::isInitialized() const {
75 void SyncableObject::setInitialized() {
80 QVariantMap SyncableObject::toVariantMap() {
81 QVariantMap properties;
83 const QMetaObject* meta = metaObject();
85 // we collect data from properties
88 for(int i = 0; i < meta->propertyCount(); i++) {
89 prop = meta->property(i);
90 propName = QString(prop.name());
91 if(propName == "objectName")
93 properties[propName] = prop.read(this);
96 // ...as well as methods, which have names starting with "init"
97 for(int i = 0; i < meta->methodCount(); i++) {
98 QMetaMethod method = meta->method(i);
99 QString methodname(SignalProxy::ExtendedMetaObject::methodName(method));
100 if(!methodname.startsWith("init") || methodname.startsWith("initSet") || methodname.startsWith("initDone"))
103 QVariant::Type variantType = QVariant::nameToType(method.typeName());
104 if(variantType == QVariant::Invalid && !QByteArray(method.typeName()).isEmpty()) {
105 qWarning() << "SyncableObject::toVariantMap(): cannot fetch init data for:" << this << method.signature() << "- Returntype is unknown to Qt's MetaSystem:" << QByteArray(method.typeName());
109 QVariant value(variantType, (const void *) 0);
110 QGenericReturnArgument genericvalue = QGenericReturnArgument(method.typeName(), value.data());
111 QMetaObject::invokeMethod(this, methodname.toAscii(), genericvalue);
113 properties[SignalProxy::ExtendedMetaObject::methodBaseName(method)] = value;
118 void SyncableObject::fromVariantMap(const QVariantMap &properties) {
119 const QMetaObject *meta = metaObject();
121 QVariantMap::const_iterator iterator = properties.constBegin();
123 while(iterator != properties.constEnd()) {
124 propName = iterator.key();
125 if(propName == "objectName") {
130 int propertyIndex = meta->indexOfProperty(propName.toAscii());
132 if(propertyIndex == -1 || !meta->property(propertyIndex).isWritable())
133 setInitValue(propName, iterator.value());
135 setProperty(propName.toAscii(), iterator.value());
136 // qDebug() << "<<< SYNC:" << name << iterator.value();
141 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.toAscii().constData());
148 QByteArray normedMethodName = QMetaObject::normalizedSignature(methodSignature.toAscii().constData());
149 methodIdx = metaObject()->indexOfMethod(normedMethodName.constData());
155 QGenericArgument param(value.typeName(), value.constData());
156 return QMetaObject::invokeMethod(this, handlername.toAscii(), param);
159 void SyncableObject::renameObject(const QString &newName) {
160 const QString oldName = objectName();
161 if(oldName != newName) {
162 setObjectName(newName);
163 foreach(SignalProxy *proxy, _signalProxies) {
164 proxy->renameObject(this, newName, oldName);
169 void SyncableObject::update(const QVariantMap &properties) {
170 fromVariantMap(properties);
171 SYNC(ARG(properties))
175 void SyncableObject::requestUpdate(const QVariantMap &properties) {
176 if(allowClientUpdates()) {
179 REQUEST(ARG(properties))
182 void SyncableObject::sync_call__(SignalProxy::ProxyMode modeType, const char *funcname, ...) const {
183 //qDebug() << Q_FUNC_INFO << modeType << funcname;
184 foreach(SignalProxy *proxy, _signalProxies) {
186 va_start(ap, funcname);
187 proxy->sync_call__(this, modeType, funcname, ap);
192 void SyncableObject::synchronize(SignalProxy *proxy) {
193 if(_signalProxies.contains(proxy))
195 _signalProxies << proxy;
198 void SyncableObject::stopSynchronize(SignalProxy *proxy) {
199 for(int i = 0; i < _signalProxies.count(); i++) {
200 if(_signalProxies[i] == proxy) {
201 _signalProxies.removeAt(i);