cipher implementation
[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 INIT_SYNCABLE_OBJECT(SyncableObject)
31 SyncableObject::SyncableObject(QObject *parent)
32   : QObject(parent),
33     _initialized(false),
34     _allowClientUpdates(false)
35 {
36 }
37
38 SyncableObject::SyncableObject(const QString &objectName, QObject *parent)
39   : QObject(parent),
40     _initialized(false),
41     _allowClientUpdates(false)
42 {
43   setObjectName(objectName);
44 }
45
46 SyncableObject::SyncableObject(const SyncableObject &other, QObject *parent)
47   : QObject(parent),
48     _initialized(other._initialized),
49     _allowClientUpdates(other._allowClientUpdates)
50 {
51 }
52
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);
59   }
60 }
61
62 SyncableObject &SyncableObject::operator=(const SyncableObject &other) {
63   if(this == &other)
64     return *this;
65
66   _initialized = other._initialized;
67   _allowClientUpdates = other._allowClientUpdates;
68   return *this;
69 }
70
71 bool SyncableObject::isInitialized() const {
72   return _initialized;
73 }
74
75 void SyncableObject::setInitialized() {
76   _initialized = true;
77   emit initDone();
78 }
79
80 QVariantMap SyncableObject::toVariantMap() {
81   QVariantMap properties;
82
83   const QMetaObject* meta = metaObject();
84
85   // we collect data from properties
86   QMetaProperty prop;
87   QString propName;
88   for(int i = 0; i < meta->propertyCount(); i++) {
89     prop = meta->property(i);
90     propName = QString(prop.name());
91     if(propName == "objectName")
92       continue;
93     properties[propName] = prop.read(this);
94   }
95
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"))
101       continue;
102
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());
106       continue;
107     }
108     QVariant value = QVariant(variantType);
109     QGenericReturnArgument genericvalue = QGenericReturnArgument(method.typeName(), &value);
110     QMetaObject::invokeMethod(this, methodname.toAscii(), genericvalue);
111
112     properties[SignalProxy::ExtendedMetaObject::methodBaseName(method)] = value;
113   }
114   // properties["Payload"] = QByteArray(10000000, 'a');  // for testing purposes
115   return properties;
116
117 }
118
119 void SyncableObject::fromVariantMap(const QVariantMap &properties) {
120   const QMetaObject *meta = metaObject();
121
122   QVariantMap::const_iterator iterator = properties.constBegin();
123   QString propName;
124   while(iterator != properties.constEnd()) {
125     propName = iterator.key();
126     if(propName == "objectName") {
127       iterator++;
128       continue;
129     }
130
131     int propertyIndex = meta->indexOfProperty(propName.toAscii());
132
133     if(propertyIndex == -1 || !meta->property(propertyIndex).isWritable())
134       setInitValue(propName, iterator.value());
135     else
136       setProperty(propName.toAscii(), iterator.value());
137     // qDebug() << "<<< SYNC:" << name << iterator.value();
138     iterator++;
139   }
140 }
141
142 bool SyncableObject::setInitValue(const QString &property, const QVariant &value) {
143   QString handlername = QString("initSet") + property;
144   handlername[7] = handlername[7].toUpper();
145
146   QString methodSignature = QString("%1(%2)").arg(handlername).arg(value.typeName());
147   int methodIdx = metaObject()->indexOfMethod(methodSignature.toAscii().constData());
148   if(methodIdx <  0) {
149     QByteArray normedMethodName = QMetaObject::normalizedSignature(methodSignature.toAscii().constData());
150     methodIdx = metaObject()->indexOfMethod(normedMethodName.constData());
151   }
152   if(methodIdx < 0) {
153     return false;
154   }
155
156   QGenericArgument param(value.typeName(), value.constData());
157   return QMetaObject::invokeMethod(this, handlername.toAscii(), param);
158 }
159
160 void SyncableObject::renameObject(const QString &newName) {
161   const QString oldName = objectName();
162   if(oldName != newName) {
163     setObjectName(newName);
164     foreach(SignalProxy *proxy, _signalProxies) {
165       proxy->renameObject(this, newName, oldName);
166     }
167   }
168 }
169
170 void SyncableObject::update(const QVariantMap &properties) {
171   fromVariantMap(properties);
172   SYNC(ARG(properties))
173   emit updated();
174 }
175
176 void SyncableObject::requestUpdate(const QVariantMap &properties) {
177   if(allowClientUpdates()) {
178     update(properties);
179   }
180   REQUEST(ARG(properties))
181 }
182
183 void SyncableObject::sync_call__(SignalProxy::ProxyMode modeType, const char *funcname, ...) const {
184   //qDebug() << Q_FUNC_INFO << modeType << funcname;
185   foreach(SignalProxy *proxy, _signalProxies) {
186     va_list ap;
187     va_start(ap, funcname);
188     proxy->sync_call__(this, modeType, funcname, ap);
189     va_end(ap);
190   }
191 }
192
193 void SyncableObject::synchronize(SignalProxy *proxy) {
194   if(_signalProxies.contains(proxy))
195     return;
196   _signalProxies << proxy;
197 }
198
199 void SyncableObject::stopSynchronize(SignalProxy *proxy) {
200   for(int i = 0; i < _signalProxies.count(); i++) {
201     if(_signalProxies[i] == proxy) {
202       _signalProxies.removeAt(i);
203       break;
204     }
205   }
206 }