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());
108 QVariant value = QVariant(variantType);
109 QGenericReturnArgument genericvalue = QGenericReturnArgument(method.typeName(), &value);
110 QMetaObject::invokeMethod(this, methodname.toAscii(), genericvalue);
112 properties[SignalProxy::ExtendedMetaObject::methodBaseName(method)] = value;
114 // properties["Payload"] = QByteArray(10000000, 'a'); // for testing purposes
119 void SyncableObject::fromVariantMap(const QVariantMap &properties) {
120 const QMetaObject *meta = metaObject();
122 QVariantMap::const_iterator iterator = properties.constBegin();
124 while(iterator != properties.constEnd()) {
125 propName = iterator.key();
126 if(propName == "objectName") {
131 int propertyIndex = meta->indexOfProperty(propName.toAscii());
133 if(propertyIndex == -1 || !meta->property(propertyIndex).isWritable())
134 setInitValue(propName, iterator.value());
136 setProperty(propName.toAscii(), iterator.value());
137 // qDebug() << "<<< SYNC:" << name << iterator.value();
142 bool SyncableObject::setInitValue(const QString &property, const QVariant &value) {
143 QString handlername = QString("initSet") + property;
144 handlername[7] = handlername[7].toUpper();
145 QGenericArgument param(value.typeName(), value.constData());
146 return QMetaObject::invokeMethod(this, handlername.toAscii(), param);
149 void SyncableObject::renameObject(const QString &newName) {
150 const QString oldName = objectName();
151 if(oldName != newName) {
152 setObjectName(newName);
153 foreach(SignalProxy *proxy, _signalProxies) {
154 proxy->renameObject(this, newName, oldName);
159 void SyncableObject::update(const QVariantMap &properties) {
160 fromVariantMap(properties);
161 SYNC(ARG(properties))
165 void SyncableObject::requestUpdate(const QVariantMap &properties) {
166 if(allowClientUpdates()) {
169 REQUEST(ARG(properties))
172 void SyncableObject::sync_call__(SignalProxy::ProxyMode modeType, const char *funcname, ...) const {
173 //qDebug() << Q_FUNC_INFO << modeType << funcname;
174 foreach(SignalProxy *proxy, _signalProxies) {
176 va_start(ap, funcname);
177 proxy->sync_call__(this, modeType, funcname, ap);
182 void SyncableObject::synchronize(SignalProxy *proxy) {
183 if(_signalProxies.contains(proxy))
185 _signalProxies << proxy;
188 void SyncableObject::stopSynchronize(SignalProxy *proxy) {
189 for(int i = 0; i < _signalProxies.count(); i++) {
190 if(_signalProxies[i] == proxy) {
191 _signalProxies.removeAt(i);