Prevent the core from restoring its state if it is unconfigured.
[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 "syncableobject.h"
24
25 #include "signalproxy.h"
26 #include "util.h"
27
28 SyncableObject::SyncableObject(QObject *parent) : QObject(parent) {
29   _initialized = false;
30 }
31
32 SyncableObject::SyncableObject(const SyncableObject &other, QObject *parent) : QObject(parent) {
33   _initialized = other._initialized;
34
35 }
36
37 bool SyncableObject::isInitialized() const {
38   return _initialized;
39 }
40
41 void SyncableObject::setInitialized() {
42   _initialized = true;
43   emit initDone();
44 }
45
46 QVariantMap SyncableObject::toVariantMap() {
47   QVariantMap properties;
48
49   const QMetaObject* meta = metaObject();
50
51   // we collect data from properties
52   for(int i = 0; i < meta->propertyCount(); i++) {
53     QMetaProperty prop = meta->property(i);
54     properties[QString(prop.name())] = prop.read(this);
55   }
56
57   // ...as well as methods, which have names starting with "init"
58   for(int i = 0; i < meta->methodCount(); i++) {
59     QMetaMethod method = meta->method(i);
60     QString methodname(::methodName(method));
61     if(!methodname.startsWith("init") || methodname.startsWith("initSet"))
62       continue;
63
64     QVariant value = QVariant(QVariant::nameToType(method.typeName()));
65     QGenericReturnArgument genericvalue = QGenericReturnArgument(method.typeName(), &value);
66     QMetaObject::invokeMethod(this, methodname.toAscii(), genericvalue);
67
68     properties[SignalProxy::methodBaseName(method)] = value;
69     // qDebug() << ">>> SYNC:" << methodBaseName(method) << value;
70   }
71   // properties["Payload"] = QByteArray(10000000, 'a');  // for testing purposes
72   return properties;
73
74 }
75
76 void SyncableObject::fromVariantMap(const QVariantMap &properties) {
77   const QMetaObject *meta = metaObject();
78
79   QVariantMap::const_iterator iterator = properties.constBegin();
80   while(iterator != properties.constEnd()) {
81     QString name = iterator.key();
82     int propertyIndex = meta->indexOfProperty(name.toAscii());
83
84     if(propertyIndex == -1 || !meta->property(propertyIndex).isWritable())
85       setInitValue(name, iterator.value());
86     else
87       setProperty(name.toAscii(), iterator.value());
88     // qDebug() << "<<< SYNC:" << name << iterator.value();
89     iterator++;
90   }
91 }
92
93 bool SyncableObject::setInitValue(const QString &property, const QVariant &value) {
94   QString handlername = QString("initSet") + property;
95   handlername[7] = handlername[7].toUpper();
96   QGenericArgument param(value.typeName(), value.constData());
97   return QMetaObject::invokeMethod(this, handlername.toAscii(), param);
98 }