- fixed crashes caused by return types of init methods that were unknown to Qt's...
[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) : QObject(parent) {
31   _initialized = false;
32 }
33
34 SyncableObject::SyncableObject(const SyncableObject &other, QObject *parent) : QObject(parent) {
35   _initialized = other._initialized;
36
37 }
38
39 bool SyncableObject::isInitialized() const {
40   return _initialized;
41 }
42
43 void SyncableObject::setInitialized() {
44   _initialized = true;
45   emit initDone();
46 }
47
48 QVariantMap SyncableObject::toVariantMap() {
49   QVariantMap properties;
50
51   const QMetaObject* meta = metaObject();
52
53   // we collect data from properties
54   for(int i = 0; i < meta->propertyCount(); i++) {
55     QMetaProperty prop = meta->property(i);
56     properties[QString(prop.name())] = prop.read(this);
57   }
58
59   // ...as well as methods, which have names starting with "init"
60   for(int i = 0; i < meta->methodCount(); i++) {
61     QMetaMethod method = meta->method(i);
62     QString methodname(::methodName(method));
63     if(!methodname.startsWith("init") || methodname.startsWith("initSet") || methodname.startsWith("initDone"))
64       continue;
65
66     QVariant::Type variantType = QVariant::nameToType(method.typeName());
67     if(variantType == QVariant::Invalid && !QByteArray(method.typeName()).isEmpty()) {
68       qWarning() << "SyncableObject::toVariantMap(): cannot fetch init data for:" << this << method.signature() << "- Returntype is unknown to Qt's MetaSystem:" << QByteArray(method.typeName());
69       continue;
70     }
71     QVariant value = QVariant(variantType);
72     QGenericReturnArgument genericvalue = QGenericReturnArgument(method.typeName(), &value);
73     QMetaObject::invokeMethod(this, methodname.toAscii(), genericvalue);
74
75     properties[SignalProxy::methodBaseName(method)] = value;
76     // qDebug() << ">>> SYNC:" << methodBaseName(method) << value;
77   }
78   // properties["Payload"] = QByteArray(10000000, 'a');  // for testing purposes
79   return properties;
80
81 }
82
83 void SyncableObject::fromVariantMap(const QVariantMap &properties) {
84   const QMetaObject *meta = metaObject();
85
86   QVariantMap::const_iterator iterator = properties.constBegin();
87   while(iterator != properties.constEnd()) {
88     QString name = iterator.key();
89     int propertyIndex = meta->indexOfProperty(name.toAscii());
90
91     if(propertyIndex == -1 || !meta->property(propertyIndex).isWritable())
92       setInitValue(name, iterator.value());
93     else
94       setProperty(name.toAscii(), iterator.value());
95     // qDebug() << "<<< SYNC:" << name << iterator.value();
96     iterator++;
97   }
98 }
99
100 bool SyncableObject::setInitValue(const QString &property, const QVariant &value) {
101   QString handlername = QString("initSet") + property;
102   handlername[7] = handlername[7].toUpper();
103   QGenericArgument param(value.typeName(), value.constData());
104   return QMetaObject::invokeMethod(this, handlername.toAscii(), param);
105 }
106
107 void SyncableObject::renameObject(const QString &newName) {
108   const QString oldName = objectName();
109   if(oldName != newName) {
110     setObjectName(newName);
111     emit objectRenamed(newName, oldName);
112   }
113 }