X-Git-Url: https://git.quassel-irc.org/?p=quassel.git;a=blobdiff_plain;f=src%2Fcommon%2Fsyncableobject.cpp;h=248ccc9f45bf58fc05d81101aa6b9ab91fe13fa3;hp=5bf68044a8cdd3e1e190cb7c29cce5c281c49f33;hb=b2efa6809ee09b70be19c0f66e0fcd6e57bf64bc;hpb=b797e5f581b10a517c30f78cb53f813af741e261 diff --git a/src/common/syncableobject.cpp b/src/common/syncableobject.cpp index 5bf68044..248ccc9f 100644 --- a/src/common/syncableobject.cpp +++ b/src/common/syncableobject.cpp @@ -20,18 +20,43 @@ #include +#include + #include "syncableobject.h" #include "signalproxy.h" #include "util.h" -SyncableObject::SyncableObject(QObject *parent) : QObject(parent) { +SyncableObject::SyncableObject(QObject *parent) + : QObject(parent), + _initialized(false), + _allowClientUpdates(false) +{ +} + +SyncableObject::SyncableObject(const SyncableObject &other, QObject *parent) + : QObject(parent), + _initialized(other._initialized), + _allowClientUpdates(other._allowClientUpdates) +{ +} + +SyncableObject &SyncableObject::operator=(const SyncableObject &other) { + if(this == &other) + return *this; + _initialized = other._initialized; + _allowClientUpdates = other._allowClientUpdates; + return *this; } -SyncableObject::SyncableObject(const SyncableObject &other, QObject *parent) : QObject(parent) { - Q_UNUSED(other); +bool SyncableObject::isInitialized() const { + return _initialized; +} +void SyncableObject::setInitialized() { + _initialized = true; + emit initDone(); } QVariantMap SyncableObject::toVariantMap() { @@ -40,19 +65,29 @@ QVariantMap SyncableObject::toVariantMap() { const QMetaObject* meta = metaObject(); // we collect data from properties + QMetaProperty prop; + QString propName; for(int i = 0; i < meta->propertyCount(); i++) { - QMetaProperty prop = meta->property(i); - properties[QString(prop.name())] = prop.read(this); + prop = meta->property(i); + propName = QString(prop.name()); + if(propName == "objectName") + continue; + properties[propName] = prop.read(this); } // ...as well as methods, which have names starting with "init" for(int i = 0; i < meta->methodCount(); i++) { QMetaMethod method = meta->method(i); QString methodname(::methodName(method)); - if(!methodname.startsWith("init") || methodname.startsWith("initSet")) + if(!methodname.startsWith("init") || methodname.startsWith("initSet") || methodname.startsWith("initDone")) continue; - QVariant value = QVariant(QVariant::nameToType(method.typeName())); + QVariant::Type variantType = QVariant::nameToType(method.typeName()); + if(variantType == QVariant::Invalid && !QByteArray(method.typeName()).isEmpty()) { + qWarning() << "SyncableObject::toVariantMap(): cannot fetch init data for:" << this << method.signature() << "- Returntype is unknown to Qt's MetaSystem:" << QByteArray(method.typeName()); + continue; + } + QVariant value = QVariant(variantType); QGenericReturnArgument genericvalue = QGenericReturnArgument(method.typeName(), &value); QMetaObject::invokeMethod(this, methodname.toAscii(), genericvalue); @@ -68,14 +103,20 @@ void SyncableObject::fromVariantMap(const QVariantMap &properties) { const QMetaObject *meta = metaObject(); QVariantMap::const_iterator iterator = properties.constBegin(); + QString propName; while(iterator != properties.constEnd()) { - QString name = iterator.key(); - int propertyIndex = meta->indexOfProperty(name.toAscii()); + propName = iterator.key(); + if(propName == "objectName") { + iterator++; + continue; + } + + int propertyIndex = meta->indexOfProperty(propName.toAscii()); if(propertyIndex == -1 || !meta->property(propertyIndex).isWritable()) - setInitValue(name, iterator.value()); + setInitValue(propName, iterator.value()); else - setProperty(name.toAscii(), iterator.value()); + setProperty(propName.toAscii(), iterator.value()); // qDebug() << "<<< SYNC:" << name << iterator.value(); iterator++; } @@ -88,15 +129,22 @@ bool SyncableObject::setInitValue(const QString &property, const QVariant &value return QMetaObject::invokeMethod(this, handlername.toAscii(), param); } -#include -QDataStream &operator<<(QDataStream &out, SyncableObject object) { - out << object.toVariantMap(); - return out; +void SyncableObject::renameObject(const QString &newName) { + const QString oldName = objectName(); + if(oldName != newName) { + setObjectName(newName); + emit objectRenamed(newName, oldName); + } +} + +void SyncableObject::update(const QVariantMap &properties) { + fromVariantMap(properties); + emit updated(properties); } -QDataStream &operator>>(QDataStream &in, SyncableObject &object) { - QVariantMap map; - in >> map; - object.fromVariantMap(map); - return in; +void SyncableObject::requestUpdate(const QVariantMap &properties) { + if(allowClientUpdates()) { + update(properties); + } + emit updateRequested(properties); }