aab2857c668e590822a0439d0bbf478d6daabc60
[quassel.git] / src / common / syncableobject.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-09 by the Quassel Project                          *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
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.                                           *
9  *                                                                         *
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.                          *
14  *                                                                         *
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  ***************************************************************************/
20
21 #include <QMetaProperty>
22
23 #include <QDebug>
24
25 #include "syncableobject.h"
26
27 #include "signalproxy.h"
28 #include "util.h"
29
30 SyncableObject::SyncableObject(QObject *parent)
31   : QObject(parent),
32     _initialized(false),
33     _allowClientUpdates(false)
34 {
35 }
36
37 SyncableObject::SyncableObject(const QString &objectName, QObject *parent)
38   : QObject(parent),
39     _initialized(false),
40     _allowClientUpdates(false)
41 {
42   setObjectName(objectName);
43 }
44
45 SyncableObject::SyncableObject(const SyncableObject &other, QObject *parent)
46   : QObject(parent),
47     _initialized(other._initialized),
48     _allowClientUpdates(other._allowClientUpdates)
49 {
50 }
51
52 SyncableObject &SyncableObject::operator=(const SyncableObject &other) {
53   if(this == &other)
54     return *this;
55
56   _initialized = other._initialized;
57   _allowClientUpdates = other._allowClientUpdates;
58   return *this;
59 }
60
61 void SyncableObject::synchronize(SignalProxy *proxy) {
62   if(_signalProxies.contains(proxy))
63     return;
64   _signalProxies << proxy;
65 }
66
67 bool SyncableObject::isInitialized() const {
68   return _initialized;
69 }
70
71 void SyncableObject::setInitialized() {
72   _initialized = true;
73   emit initDone();
74 }
75
76 QVariantMap SyncableObject::toVariantMap() {
77   QVariantMap properties;
78
79   const QMetaObject* meta = metaObject();
80
81   // we collect data from properties
82   QMetaProperty prop;
83   QString propName;
84   for(int i = 0; i < meta->propertyCount(); i++) {
85     prop = meta->property(i);
86     propName = QString(prop.name());
87     if(propName == "objectName")
88       continue;
89     properties[propName] = prop.read(this);
90   }
91
92   // ...as well as methods, which have names starting with "init"
93   for(int i = 0; i < meta->methodCount(); i++) {
94     QMetaMethod method = meta->method(i);
95     QString methodname(SignalProxy::ExtendedMetaObject::methodName(method));
96     if(!methodname.startsWith("init") || methodname.startsWith("initSet") || methodname.startsWith("initDone"))
97       continue;
98
99     QVariant::Type variantType = QVariant::nameToType(method.typeName());
100     if(variantType == QVariant::Invalid && !QByteArray(method.typeName()).isEmpty()) {
101       qWarning() << "SyncableObject::toVariantMap(): cannot fetch init data for:" << this << method.signature() << "- Returntype is unknown to Qt's MetaSystem:" << QByteArray(method.typeName());
102       continue;
103     }
104     QVariant value = QVariant(variantType);
105     QGenericReturnArgument genericvalue = QGenericReturnArgument(method.typeName(), &value);
106     QMetaObject::invokeMethod(this, methodname.toAscii(), genericvalue);
107
108     properties[SignalProxy::ExtendedMetaObject::methodBaseName(method)] = value;
109   }
110   // properties["Payload"] = QByteArray(10000000, 'a');  // for testing purposes
111   return properties;
112
113 }
114
115 void SyncableObject::fromVariantMap(const QVariantMap &properties) {
116   const QMetaObject *meta = metaObject();
117
118   QVariantMap::const_iterator iterator = properties.constBegin();
119   QString propName;
120   while(iterator != properties.constEnd()) {
121     propName = iterator.key();
122     if(propName == "objectName") {
123       iterator++;
124       continue;
125     }
126
127     int propertyIndex = meta->indexOfProperty(propName.toAscii());
128
129     if(propertyIndex == -1 || !meta->property(propertyIndex).isWritable())
130       setInitValue(propName, iterator.value());
131     else
132       setProperty(propName.toAscii(), iterator.value());
133     // qDebug() << "<<< SYNC:" << name << iterator.value();
134     iterator++;
135   }
136 }
137
138 bool SyncableObject::setInitValue(const QString &property, const QVariant &value) {
139   QString handlername = QString("initSet") + property;
140   handlername[7] = handlername[7].toUpper();
141   QGenericArgument param(value.typeName(), value.constData());
142   return QMetaObject::invokeMethod(this, handlername.toAscii(), param);
143 }
144
145 void SyncableObject::renameObject(const QString &newName) {
146   const QString oldName = objectName();
147   if(oldName != newName) {
148     setObjectName(newName);
149     emit objectRenamed(newName, oldName);
150   }
151 }
152
153 void SyncableObject::update(const QVariantMap &properties) {
154   fromVariantMap(properties);
155   emit updated(properties);
156 }
157
158 void SyncableObject::requestUpdate(const QVariantMap &properties) {
159   if(allowClientUpdates()) {
160     update(properties);
161   }
162   emit updateRequested(properties);
163 }
164
165 void SyncableObject::sync_call__(SignalProxy::ProxyMode modeType, const char *funcname, ...) {
166   qDebug() << Q_FUNC_INFO << modeType << funcname;
167   foreach(SignalProxy *proxy, _signalProxies) {
168     va_list ap;
169     va_start(ap, funcname);
170     proxy->syncCall(this, modeType, funcname, ap);
171     va_end(ap);
172   }
173 }