ssl: Use QSslSocket directly to avoid redundant qobject_casts
[quassel.git] / src / common / syncableobject.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2020 by the Quassel Project                        *
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  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
19  ***************************************************************************/
20
21 #include "syncableobject.h"
22
23 #include <QDebug>
24 #include <QMetaProperty>
25
26 #include "signalproxy.h"
27 #include "util.h"
28
29 SyncableObject::SyncableObject(QObject* parent)
30     : SyncableObject(QString{}, parent)
31 {}
32
33 SyncableObject::SyncableObject(const QString& objectName, QObject* parent)
34     : QObject(parent)
35 {
36     _objectName = objectName;
37     setObjectName(objectName);
38
39     connect(this, &QObject::objectNameChanged, this, [this](auto&& newName) {
40         for (auto&& proxy : _signalProxies) {
41             proxy->renameObject(this, newName, _objectName);
42         }
43         _objectName = newName;
44     });
45 }
46
47 SyncableObject::~SyncableObject()
48 {
49     QList<SignalProxy*>::iterator proxyIter = _signalProxies.begin();
50     while (proxyIter != _signalProxies.end()) {
51         SignalProxy* proxy = (*proxyIter);
52         proxyIter = _signalProxies.erase(proxyIter);
53         proxy->stopSynchronize(this);
54     }
55 }
56
57 bool SyncableObject::isInitialized() const
58 {
59     return _initialized;
60 }
61
62 void SyncableObject::setInitialized()
63 {
64     _initialized = true;
65     emit initDone();
66 }
67
68 QVariantMap SyncableObject::toVariantMap()
69 {
70     QVariantMap properties;
71
72     const QMetaObject* meta = metaObject();
73
74     // we collect data from properties
75     QMetaProperty prop;
76     QString propName;
77     for (int i = 0; i < meta->propertyCount(); i++) {
78         prop = meta->property(i);
79         propName = QString(prop.name());
80         if (propName == "objectName")
81             continue;
82         properties[propName] = prop.read(this);
83     }
84
85     // ...as well as methods, which have names starting with "init"
86     for (int i = 0; i < meta->methodCount(); i++) {
87         QMetaMethod method = meta->method(i);
88         QString methodname(SignalProxy::ExtendedMetaObject::methodName(method));
89         if (!methodname.startsWith("init") || methodname.startsWith("initSet") || methodname.startsWith("initDone"))
90             continue;
91
92         QVariant::Type variantType = QVariant::nameToType(method.typeName());
93         if (variantType == QVariant::Invalid && !QByteArray(method.typeName()).isEmpty()) {
94             qWarning() << "SyncableObject::toVariantMap(): cannot fetch init data for:" << this << method.methodSignature()
95                        << "- Returntype is unknown to Qt's MetaSystem:" << QByteArray(method.typeName());
96             continue;
97         }
98
99         QVariant value(variantType, (const void*)nullptr);
100         QGenericReturnArgument genericvalue = QGenericReturnArgument(method.typeName(), value.data());
101         QMetaObject::invokeMethod(this, methodname.toLatin1(), genericvalue);
102
103         properties[SignalProxy::ExtendedMetaObject::methodBaseName(method)] = value;
104     }
105     return properties;
106 }
107
108 void SyncableObject::fromVariantMap(const QVariantMap& properties)
109 {
110     const QMetaObject* meta = metaObject();
111
112     QVariantMap::const_iterator iterator = properties.constBegin();
113     QString propName;
114     while (iterator != properties.constEnd()) {
115         propName = iterator.key();
116         if (propName == "objectName") {
117             ++iterator;
118             continue;
119         }
120
121         int propertyIndex = meta->indexOfProperty(propName.toLatin1());
122
123         if (propertyIndex == -1 || !meta->property(propertyIndex).isWritable())
124             setInitValue(propName, iterator.value());
125         else
126             setProperty(propName.toLatin1(), iterator.value());
127         // qDebug() << "<<< SYNC:" << name << iterator.value();
128         ++iterator;
129     }
130 }
131
132 bool SyncableObject::setInitValue(const QString& property, const QVariant& value)
133 {
134     QString handlername = QString("initSet") + property;
135     handlername[7] = handlername[7].toUpper();
136
137     QString methodSignature = QString("%1(%2)").arg(handlername).arg(value.typeName());
138     int methodIdx = metaObject()->indexOfMethod(methodSignature.toLatin1().constData());
139     if (methodIdx < 0) {
140         QByteArray normedMethodName = QMetaObject::normalizedSignature(methodSignature.toLatin1().constData());
141         methodIdx = metaObject()->indexOfMethod(normedMethodName.constData());
142     }
143     if (methodIdx < 0) {
144         return false;
145     }
146
147     QGenericArgument param(value.typeName(), value.constData());
148     return QMetaObject::invokeMethod(this, handlername.toLatin1(), param);
149 }
150
151 void SyncableObject::update(const QVariantMap& properties)
152 {
153     fromVariantMap(properties);
154     SYNC(ARG(properties))
155     emit updated();
156 }
157
158 void SyncableObject::requestUpdate(const QVariantMap& properties)
159 {
160     if (allowClientUpdates()) {
161         update(properties);
162     }
163     REQUEST(ARG(properties))
164 }
165
166 void SyncableObject::sync_call__(SignalProxy::ProxyMode modeType, const char* funcname, ...) const
167 {
168     // qDebug() << Q_FUNC_INFO << modeType << funcname;
169     foreach (SignalProxy* proxy, _signalProxies) {
170         va_list ap;
171         va_start(ap, funcname);
172         proxy->sync_call__(this, modeType, funcname, ap);
173         va_end(ap);
174     }
175 }
176
177 void SyncableObject::synchronize(SignalProxy* proxy)
178 {
179     if (_signalProxies.contains(proxy))
180         return;
181     _signalProxies << proxy;
182 }
183
184 void SyncableObject::stopSynchronize(SignalProxy* proxy)
185 {
186     for (int i = 0; i < _signalProxies.count(); i++) {
187         if (_signalProxies[i] == proxy) {
188             _signalProxies.removeAt(i);
189             break;
190         }
191     }
192 }