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