1 /***************************************************************************
2 * Copyright (C) 2005-08 by the Quassel IRC Team *
3 * devel@quassel-irc.org *
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. *
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. *
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 ***************************************************************************/
21 #include <QMetaProperty>
23 #include "syncableobject.h"
25 #include "signalproxy.h"
28 SyncableObject::SyncableObject(QObject *parent) : QObject(parent) {
32 SyncableObject::SyncableObject(const SyncableObject &other, QObject *parent) : QObject(parent) {
33 _initialized = other._initialized;
37 bool SyncableObject::isInitialized() const {
41 void SyncableObject::setInitialized() {
46 QVariantMap SyncableObject::toVariantMap() {
47 QVariantMap properties;
49 const QMetaObject* meta = metaObject();
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);
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"))
64 QVariant value = QVariant(QVariant::nameToType(method.typeName()));
65 QGenericReturnArgument genericvalue = QGenericReturnArgument(method.typeName(), &value);
66 QMetaObject::invokeMethod(this, methodname.toAscii(), genericvalue);
68 properties[SignalProxy::methodBaseName(method)] = value;
69 // qDebug() << ">>> SYNC:" << methodBaseName(method) << value;
71 // properties["Payload"] = QByteArray(10000000, 'a'); // for testing purposes
76 void SyncableObject::fromVariantMap(const QVariantMap &properties) {
77 const QMetaObject *meta = metaObject();
79 QVariantMap::const_iterator iterator = properties.constBegin();
80 while(iterator != properties.constEnd()) {
81 QString name = iterator.key();
82 int propertyIndex = meta->indexOfProperty(name.toAscii());
84 if(propertyIndex == -1 || !meta->property(propertyIndex).isWritable())
85 setInitValue(name, iterator.value());
87 setProperty(name.toAscii(), iterator.value());
88 // qDebug() << "<<< SYNC:" << name << iterator.value();
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);
100 void SyncableObject::renameObject(const QString &newName) {
101 const QString oldName = objectName();
102 if(oldName != newName) {
103 setObjectName(newName);
104 emit objectRenamed(newName, oldName);