From 0521bd21f7309188a1f278eb5559c8d0d7ac412d Mon Sep 17 00:00:00 2001
From: Andreas Traczyk <andreas.traczyk@savoirfairelinux.com>
Date: Mon, 23 Aug 2021 12:43:31 -0400
Subject: [PATCH] qmlengine: homogenize qml module registration

Make sure all the calls to register are using the same versions
from qmlregister.h.

Change-Id: I1b6d2b168778fa91d16342765c93ad732de596e0
---
 src/mainapplication.cpp | 17 +++--------------
 src/qmlregister.cpp     | 12 ++++++------
 src/qmlregister.h       | 19 ++++++++++---------
 3 files changed, 19 insertions(+), 29 deletions(-)

diff --git a/src/mainapplication.cpp b/src/mainapplication.cpp
index 7b27b8cbd..70146ef4a 100644
--- a/src/mainapplication.cpp
+++ b/src/mainapplication.cpp
@@ -195,21 +195,10 @@ MainApplication::init()
             results[opts::MUTEDAEMON].toBool());
 
 #ifdef Q_OS_UNIX
-    GlobalInstances::setDBusErrorHandler(std::make_unique<Interfaces::DBusErrorHandler>());
+    using namespace Interfaces;
+    GlobalInstances::setDBusErrorHandler(std::make_unique<DBusErrorHandler>());
     auto dBusErrorHandlerQObject = dynamic_cast<QObject*>(&GlobalInstances::dBusErrorHandler());
-    qmlRegisterSingletonType<Interfaces::DBusErrorHandler>("net.jami.Models",
-                                                           1,
-                                                           0,
-                                                           "DBusErrorHandler",
-                                                           [dBusErrorHandlerQObject](QQmlEngine* e,
-                                                                                     QJSEngine* se)
-                                                               -> QObject* {
-                                                               Q_UNUSED(e)
-                                                               Q_UNUSED(se)
-                                                               return dBusErrorHandlerQObject;
-                                                           });
-    engine_->setObjectOwnership(dBusErrorHandlerQObject, QQmlEngine::CppOwnership);
-
+    QML_REGISTERSINGLETONTYPE_CUSTOM(NS_MODELS, DBusErrorHandler, dBusErrorHandlerQObject);
     if ((!lrc::api::Lrc::isConnected()) || (!lrc::api::Lrc::dbusIsValid())) {
         engine_->load(QUrl(QStringLiteral("qrc:/src/DaemonReconnectWindow.qml")));
         exec();
diff --git a/src/qmlregister.cpp b/src/qmlregister.cpp
index de7c8e6c8..dc6e6501c 100644
--- a/src/qmlregister.cpp
+++ b/src/qmlregister.cpp
@@ -71,26 +71,26 @@
 // clang-format off
 // TODO: remove this
 #define QML_REGISTERSINGLETONTYPE_WITH_INSTANCE(T) \
-    qmlRegisterSingletonType<T>(NS_MODELS, VER_MAJ, VER_MIN, #T, \
+    qmlRegisterSingletonType<T>(NS_MODELS, MODULE_VER_MAJ, MODULE_VER_MIN, #T, \
                                 [](QQmlEngine* e, QJSEngine* se) -> QObject* { \
                                     Q_UNUSED(e); Q_UNUSED(se); \
                                     return &(T::instance()); \
                                 });
 
 #define QML_REGISTERSINGLETONTYPE_URL(NS, URL, T) \
-    qmlRegisterSingletonType(QUrl(QStringLiteral(URL)), NS, VER_MAJ, VER_MIN, #T);
+    qmlRegisterSingletonType(QUrl(QStringLiteral(URL)), NS, MODULE_VER_MAJ, MODULE_VER_MIN, #T);
 
-#define QML_REGISTERTYPE(NS, T) qmlRegisterType<T>(NS, VER_MAJ, VER_MIN, #T);
+#define QML_REGISTERTYPE(NS, T) qmlRegisterType<T>(NS, MODULE_VER_MAJ, MODULE_VER_MIN, #T);
 
 #define QML_REGISTERNAMESPACE(NS, T, NAME) \
-    qmlRegisterUncreatableMetaObject(T, NS, VER_MAJ, VER_MIN, NAME, "")
+    qmlRegisterUncreatableMetaObject(T, NS, MODULE_VER_MAJ, MODULE_VER_MIN, NAME, "")
 
 #define QML_REGISTERUNCREATABLE(N, T) \
-    qmlRegisterUncreatableType<T>(N, VER_MAJ, VER_MIN, #T, "Don't try to add to a qml definition of " #T);
+    qmlRegisterUncreatableType<T>(N, MODULE_VER_MAJ, MODULE_VER_MIN, #T, "Don't try to add to a qml definition of " #T);
 
 #define QML_REGISTERUNCREATABLE_IN_NAMESPACE(T, NAMESPACE) \
     qmlRegisterUncreatableType<NAMESPACE::T>(NS_MODELS, \
-                                             VER_MAJ, VER_MIN, #T, \
+                                             MODULE_VER_MAJ, MODULE_VER_MIN, #T, \
                                              "Don't try to add to a qml definition of " #T);
 
 namespace Utils {
diff --git a/src/qmlregister.h b/src/qmlregister.h
index 02d8b35df..60de5504a 100644
--- a/src/qmlregister.h
+++ b/src/qmlregister.h
@@ -22,13 +22,13 @@
 #include <QQmlEngine>
 #include <QObject>
 
-#define NS_MODELS    "net.jami.Models"
-#define NS_ADAPTERS  "net.jami.Adapters"
-#define NS_CONSTANTS "net.jami.Constants"
-#define NS_HELPERS   "net.jami.Helpers"
-#define NS_ENUMS     "net.jami.Enums"
-#define VER_MAJ      1
-#define VER_MIN      0
+#define NS_MODELS      "net.jami.Models"
+#define NS_ADAPTERS    "net.jami.Adapters"
+#define NS_CONSTANTS   "net.jami.Constants"
+#define NS_HELPERS     "net.jami.Helpers"
+#define NS_ENUMS       "net.jami.Enums"
+#define MODULE_VER_MAJ 1
+#define MODULE_VER_MIN 0
 
 class SystemTray;
 class LRCInstance;
@@ -46,12 +46,13 @@ Q_CLASSINFO("RegisterEnumClassesUnscoped", "false")
 #define QML_REGISTERSINGLETONTYPE_POBJECT(NS, I, N) \
     QQmlEngine::setObjectOwnership(I, QQmlEngine::CppOwnership); \
     { using T = std::remove_reference<decltype(*I)>::type; \
-    qmlRegisterSingletonType<T>(NS, VER_MAJ, VER_MIN, N, \
+    qmlRegisterSingletonType<T>(NS, MODULE_VER_MAJ, MODULE_VER_MIN, N, \
                                 [i=I](QQmlEngine*, QJSEngine*) -> QObject* { \
                                     return i; }); }
 
 #define QML_REGISTERSINGLETONTYPE_CUSTOM(NS, T, P) \
-    qmlRegisterSingletonType<T>(NS, VER_MAJ, VER_MIN, #T, \
+    QQmlEngine::setObjectOwnership(P, QQmlEngine::CppOwnership); \
+    qmlRegisterSingletonType<T>(NS, MODULE_VER_MAJ, MODULE_VER_MIN, #T, \
                                 [p=P](QQmlEngine*, QJSEngine*) -> QObject* { \
                                     return p; \
                                 });
-- 
GitLab