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) {
37 QVariantMap SyncableObject::toVariantMap() {
38 QVariantMap properties;
40 const QMetaObject* meta = metaObject();
42 // we collect data from properties
43 for(int i = 0; i < meta->propertyCount(); i++) {
44 QMetaProperty prop = meta->property(i);
45 properties[QString(prop.name())] = prop.read(this);
48 // ...as well as methods, which have names starting with "init"
49 for(int i = 0; i < meta->methodCount(); i++) {
50 QMetaMethod method = meta->method(i);
51 QString methodname(::methodName(method));
52 if(!methodname.startsWith("init") || methodname.startsWith("initSet"))
55 QVariant value = QVariant(QVariant::nameToType(method.typeName()));
56 QGenericReturnArgument genericvalue = QGenericReturnArgument(method.typeName(), &value);
57 QMetaObject::invokeMethod(this, methodname.toAscii(), genericvalue);
59 properties[SignalProxy::methodBaseName(method)] = value;
60 // qDebug() << ">>> SYNC:" << methodBaseName(method) << value;
62 // properties["Payload"] = QByteArray(10000000, 'a'); // for testing purposes
67 void SyncableObject::fromVariantMap(const QVariantMap &properties) {
68 const QMetaObject *meta = metaObject();
70 QVariantMap::const_iterator iterator = properties.constBegin();
71 while(iterator != properties.constEnd()) {
72 QString name = iterator.key();
73 int propertyIndex = meta->indexOfProperty(name.toAscii());
75 if(propertyIndex == -1 || !meta->property(propertyIndex).isWritable())
76 setInitValue(name, iterator.value());
78 setProperty(name.toAscii(), iterator.value());
79 // qDebug() << "<<< SYNC:" << name << iterator.value();
84 bool SyncableObject::setInitValue(const QString &property, const QVariant &value) {
85 QString handlername = QString("initSet") + property;
86 handlername[7] = handlername[7].toUpper();
87 QGenericArgument param(value.typeName(), value.constData());
88 return QMetaObject::invokeMethod(this, handlername.toAscii(), param);
92 QDataStream &operator<<(QDataStream &out, SyncableObject object) {
93 out << object.toVariantMap();
97 QDataStream &operator>>(QDataStream &in, SyncableObject &object) {
100 object.fromVariantMap(map);