Don't show migration warnings if we don't have old settings to migrate
[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 bool SyncableObject::isInitialized() const {
62   return _initialized;
63 }
64
65 void SyncableObject::setInitialized() {
66   _initialized = true;
67   emit initDone();
68 }
69
70 QVariantMap SyncableObject::toVariantMap() {
71   QVariantMap properties;
72
73   const QMetaObject* meta = metaObject();
74
75   // we collect data from properties
76   QMetaProperty prop;
77   QString propName;
78   for(int i = 0; i < meta->propertyCount(); i++) {
79     prop = meta->property(i);
80     propName = QString(prop.name());
81     if(propName == "objectName")
82       continue;
83     properties[propName] = prop.read(this);
84   }
85
86   // ...as well as methods, which have names starting with "init"
87   for(int i = 0; i < meta->methodCount(); i++) {
88     QMetaMethod method = meta->method(i);
89     QString methodname(SignalProxy::ExtendedMetaObject::methodName(method));
90     if(!methodname.startsWith("init") || methodname.startsWith("initSet") || methodname.startsWith("initDone"))
91       continue;
92
93     QVariant::Type variantType = QVariant::nameToType(method.typeName());
94     if(variantType == QVariant::Invalid && !QByteArray(method.typeName()).isEmpty()) {
95       qWarning() << "SyncableObject::toVariantMap(): cannot fetch init data for:" << this << method.signature() << "- Returntype is unknown to Qt's MetaSystem:" << QByteArray(method.typeName());
96       continue;
97     }
98     QVariant value = QVariant(variantType);
99     QGenericReturnArgument genericvalue = QGenericReturnArgument(method.typeName(), &value);
100     QMetaObject::invokeMethod(this, methodname.toAscii(), genericvalue);
101
102     properties[SignalProxy::ExtendedMetaObject::methodBaseName(method)] = value;
103   }
104   // properties["Payload"] = QByteArray(10000000, 'a');  // for testing purposes
105   return properties;
106
107 }
108
109 void SyncableObject::fromVariantMap(const QVariantMap &properties) {
110   const QMetaObject *meta = metaObject();
111
112   QVariantMap::const_iterator iterator = properties.constBegin();
113   QString propName;
114   while(iterator != properties.constEnd()) {
115     propName = iterator.key();
116     if(propName == "objectName") {
117       iterator++;
118       continue;
119     }
120
121     int propertyIndex = meta->indexOfProperty(propName.toAscii());
122
123     if(propertyIndex == -1 || !meta->property(propertyIndex).isWritable())
124       setInitValue(propName, iterator.value());
125     else
126       setProperty(propName.toAscii(), iterator.value());
127     // qDebug() << "<<< SYNC:" << name << iterator.value();
128     iterator++;
129   }
130 }
131
132 bool SyncableObject::setInitValue(const QString &property, const QVariant &value) {
133   QString handlername = QString("initSet") + property;
134   handlername[7] = handlername[7].toUpper();
135   QGenericArgument param(value.typeName(), value.constData());
136   return QMetaObject::invokeMethod(this, handlername.toAscii(), param);
137 }
138
139 void SyncableObject::renameObject(const QString &newName) {
140   const QString oldName = objectName();
141   if(oldName != newName) {
142     setObjectName(newName);
143     emit objectRenamed(newName, oldName);
144   }
145 }
146
147 void SyncableObject::update(const QVariantMap &properties) {
148   fromVariantMap(properties);
149   emit updated(properties);
150 }
151
152 void SyncableObject::requestUpdate(const QVariantMap &properties) {
153   if(allowClientUpdates()) {
154     update(properties);
155   }
156   emit updateRequested(properties);
157 }