making signalproxy direct connection thread safe
[quassel.git] / src / common / syncableobject.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-08 by the Quassel IRC Team                         *
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 SyncableObject &other, QObject *parent)
38   : QObject(parent),
39     _initialized(other._initialized),
40     _allowClientUpdates(other._allowClientUpdates)
41 {
42 }
43
44 SyncableObject &SyncableObject::operator=(const SyncableObject &other) {
45   if(this == &other)
46     return *this;
47
48   _initialized = other._initialized;
49   _allowClientUpdates = other._allowClientUpdates;
50   return *this;
51 }
52
53 bool SyncableObject::isInitialized() const {
54   return _initialized;
55 }
56
57 void SyncableObject::setInitialized() {
58   _initialized = true;
59   emit initDone();
60 }
61
62 QVariantMap SyncableObject::toVariantMap() {
63   QVariantMap properties;
64
65   const QMetaObject* meta = metaObject();
66
67   // we collect data from properties
68   QMetaProperty prop;
69   QString propName;
70   for(int i = 0; i < meta->propertyCount(); i++) {
71     prop = meta->property(i);
72     propName = QString(prop.name());
73     if(propName == "objectName")
74       continue;
75     properties[propName] = prop.read(this);
76   }
77
78   // ...as well as methods, which have names starting with "init"
79   for(int i = 0; i < meta->methodCount(); i++) {
80     QMetaMethod method = meta->method(i);
81     QString methodname(::methodName(method));
82     if(!methodname.startsWith("init") || methodname.startsWith("initSet") || methodname.startsWith("initDone"))
83       continue;
84
85     QVariant::Type variantType = QVariant::nameToType(method.typeName());
86     if(variantType == QVariant::Invalid && !QByteArray(method.typeName()).isEmpty()) {
87       qWarning() << "SyncableObject::toVariantMap(): cannot fetch init data for:" << this << method.signature() << "- Returntype is unknown to Qt's MetaSystem:" << QByteArray(method.typeName());
88       continue;
89     }
90     QVariant value = QVariant(variantType);
91     QGenericReturnArgument genericvalue = QGenericReturnArgument(method.typeName(), &value);
92     QMetaObject::invokeMethod(this, methodname.toAscii(), genericvalue);
93
94     properties[SignalProxy::methodBaseName(method)] = value;
95     // qDebug() << ">>> SYNC:" << methodBaseName(method) << value;
96   }
97   // properties["Payload"] = QByteArray(10000000, 'a');  // for testing purposes
98   return properties;
99
100 }
101
102 void SyncableObject::fromVariantMap(const QVariantMap &properties) {
103   const QMetaObject *meta = metaObject();
104
105   QVariantMap::const_iterator iterator = properties.constBegin();
106   QString propName;
107   while(iterator != properties.constEnd()) {
108     propName = iterator.key();
109     if(propName == "objectName") {
110       iterator++;
111       continue;
112     }
113
114     int propertyIndex = meta->indexOfProperty(propName.toAscii());
115
116     if(propertyIndex == -1 || !meta->property(propertyIndex).isWritable())
117       setInitValue(propName, iterator.value());
118     else
119       setProperty(propName.toAscii(), iterator.value());
120     // qDebug() << "<<< SYNC:" << name << iterator.value();
121     iterator++;
122   }
123 }
124
125 bool SyncableObject::setInitValue(const QString &property, const QVariant &value) {
126   QString handlername = QString("initSet") + property;
127   handlername[7] = handlername[7].toUpper();
128   QGenericArgument param(value.typeName(), value.constData());
129   return QMetaObject::invokeMethod(this, handlername.toAscii(), param);
130 }
131
132 void SyncableObject::renameObject(const QString &newName) {
133   const QString oldName = objectName();
134   if(oldName != newName) {
135     setObjectName(newName);
136     emit objectRenamed(newName, oldName);
137   }
138 }
139
140 void SyncableObject::update(const QVariantMap &properties) {
141   fromVariantMap(properties);
142   emit updated(properties);
143 }
144
145 void SyncableObject::requestUpdate(const QVariantMap &properties) {
146   if(allowClientUpdates()) {
147     update(properties);
148   }
149   emit updateRequested(properties);
150 }