X-Git-Url: https://git.quassel-irc.org/?p=quassel.git;a=blobdiff_plain;f=src%2Fcommon%2Fsyncableobject.cpp;h=ac62f0802522f320ec19602fe74aee4bf595d881;hp=668f61c607af133b88e04a0d141231720175f91d;hb=565743a41c93de874cb79fd145b22e2422bb754e;hpb=d1b6499b0b848d4287efae89107576548533502c diff --git a/src/common/syncableobject.cpp b/src/common/syncableobject.cpp index 668f61c6..ac62f080 100644 --- a/src/common/syncableobject.cpp +++ b/src/common/syncableobject.cpp @@ -1,5 +1,5 @@ /*************************************************************************** - * Copyright (C) 2005-08 by the Quassel IRC Team * + * Copyright (C) 2005-09 by the Quassel Project * * devel@quassel-irc.org * * * * This program is free software; you can redistribute it and/or modify * @@ -20,18 +20,52 @@ #include +#include + #include "syncableobject.h" #include "signalproxy.h" #include "util.h" -SyncableObject::SyncableObject(QObject *parent) : QObject(parent) { - _initialized = false; +INIT_SYNCABLE_OBJECT(SyncableObject) +SyncableObject::SyncableObject(QObject *parent) + : QObject(parent), + _initialized(false), + _allowClientUpdates(false) +{ } -SyncableObject::SyncableObject(const SyncableObject &other, QObject *parent) : QObject(parent) { - _initialized = other._initialized; +SyncableObject::SyncableObject(const QString &objectName, QObject *parent) + : QObject(parent), + _initialized(false), + _allowClientUpdates(false) +{ + setObjectName(objectName); +} + +SyncableObject::SyncableObject(const SyncableObject &other, QObject *parent) + : QObject(parent), + _initialized(other._initialized), + _allowClientUpdates(other._allowClientUpdates) +{ +} + +SyncableObject::~SyncableObject() { + QList::iterator proxyIter = _signalProxies.begin(); + while(proxyIter != _signalProxies.end()) { + SignalProxy *proxy = (*proxyIter); + proxyIter = _signalProxies.erase(proxyIter); + proxy->stopSynchronize(this); + } +} + +SyncableObject &SyncableObject::operator=(const SyncableObject &other) { + if(this == &other) + return *this; + _initialized = other._initialized; + _allowClientUpdates = other._allowClientUpdates; + return *this; } bool SyncableObject::isInitialized() const { @@ -49,42 +83,56 @@ 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")) + QString methodname(SignalProxy::ExtendedMetaObject::methodName(method)); + if(!methodname.startsWith("init") || methodname.startsWith("initSet") || methodname.startsWith("initDone")) continue; - QVariant value = QVariant(QVariant::nameToType(method.typeName())); - QGenericReturnArgument genericvalue = QGenericReturnArgument(method.typeName(), &value); + 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(variantType, (const void *) 0); + QGenericReturnArgument genericvalue = QGenericReturnArgument(method.typeName(), value.data()); QMetaObject::invokeMethod(this, methodname.toAscii(), genericvalue); - properties[SignalProxy::methodBaseName(method)] = value; - // qDebug() << ">>> SYNC:" << methodBaseName(method) << value; + properties[SignalProxy::ExtendedMetaObject::methodBaseName(method)] = value; } - // properties["Payload"] = QByteArray(10000000, 'a'); // for testing purposes return properties; - } 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++; } @@ -93,6 +141,65 @@ void SyncableObject::fromVariantMap(const QVariantMap &properties) { bool SyncableObject::setInitValue(const QString &property, const QVariant &value) { QString handlername = QString("initSet") + property; handlername[7] = handlername[7].toUpper(); + + QString methodSignature = QString("%1(%2)").arg(handlername).arg(value.typeName()); + int methodIdx = metaObject()->indexOfMethod(methodSignature.toAscii().constData()); + if(methodIdx < 0) { + QByteArray normedMethodName = QMetaObject::normalizedSignature(methodSignature.toAscii().constData()); + methodIdx = metaObject()->indexOfMethod(normedMethodName.constData()); + } + if(methodIdx < 0) { + return false; + } + QGenericArgument param(value.typeName(), value.constData()); return QMetaObject::invokeMethod(this, handlername.toAscii(), param); } + +void SyncableObject::renameObject(const QString &newName) { + const QString oldName = objectName(); + if(oldName != newName) { + setObjectName(newName); + foreach(SignalProxy *proxy, _signalProxies) { + proxy->renameObject(this, newName, oldName); + } + } +} + +void SyncableObject::update(const QVariantMap &properties) { + fromVariantMap(properties); + SYNC(ARG(properties)) + emit updated(); +} + +void SyncableObject::requestUpdate(const QVariantMap &properties) { + if(allowClientUpdates()) { + update(properties); + } + REQUEST(ARG(properties)) +} + +void SyncableObject::sync_call__(SignalProxy::ProxyMode modeType, const char *funcname, ...) const { + //qDebug() << Q_FUNC_INFO << modeType << funcname; + foreach(SignalProxy *proxy, _signalProxies) { + va_list ap; + va_start(ap, funcname); + proxy->sync_call__(this, modeType, funcname, ap); + va_end(ap); + } +} + +void SyncableObject::synchronize(SignalProxy *proxy) { + if(_signalProxies.contains(proxy)) + return; + _signalProxies << proxy; +} + +void SyncableObject::stopSynchronize(SignalProxy *proxy) { + for(int i = 0; i < _signalProxies.count(); i++) { + if(_signalProxies[i] == proxy) { + _signalProxies.removeAt(i); + break; + } + } +}