Skip to content
Snippets Groups Projects
Select Git revision
  • 6b3bf5149fcd35b3e881151faa92ad9c57cc85cb
  • master default protected
  • nightly/20250722.0
  • beta/202507211539
  • stable/20250718.0
  • nightly/20250718.0
  • nightly/20250714.0
  • beta/202507141552
  • beta/202506161038
  • stable/20250613.0
  • nightly/20250613.0
  • beta/202506101658
  • stable/20250610.0
  • nightly/20250610.0
  • beta/202506091027
  • beta/202506061543
  • nightly/20250605.0
  • beta/202506051039
  • beta/202506051002
  • beta/202506041611
  • beta/202506041335
  • beta/202505231812
22 results

qmlregister.cpp

Blame
  • Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    qmlregister.cpp 10.72 KiB
    /*
     * Copyright (C) 2020 by Savoir-faire Linux
     * Author: Andreas Traczyk <andreas.traczyk@savoirfairelinux.com>
     *
     * This program is free software; you can redistribute it and/or modify
     * it under the terms of the GNU General Public License as published by
     * the Free Software Foundation; either version 3 of the License, or
     * (at your option) any later version.
     *
     * This program is distributed in the hope that it will be useful,
     * but WITHOUT ANY WARRANTY; without even the implied warranty of
     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     * GNU General Public License for more details.
     *
     * You should have received a copy of the GNU General Public License
     * along with this program.  If not, see <https://www.gnu.org/licenses/>.
     */
    
    #include "qmlregister.h"
    
    #include "accountadapter.h"
    #include "avadapter.h"
    #include "calladapter.h"
    #include "contactadapter.h"
    #include "pluginadapter.h"
    #include "messagesadapter.h"
    #include "settingsadapter.h"
    #include "utilsadapter.h"
    #include "conversationsadapter.h"
    #include "currentconversation.h"
    
    #include "accountlistmodel.h"
    #include "accountstomigratelistmodel.h"
    #include "mediacodeclistmodel.h"
    #include "audiodevicemodel.h"
    #include "audiomanagerlistmodel.h"
    #include "bannedlistmodel.h"
    #include "moderatorlistmodel.h"
    #include "deviceitemlistmodel.h"
    #include "smartlistmodel.h"
    #include "conversationlistmodelbase.h"
    #include "filestosendlistmodel.h"
    
    #include "qrimageprovider.h"
    #include "avatarimageprovider.h"
    #include "avatarregistry.h"
    #include "appsettingsmanager.h"
    #include "mainapplication.h"
    #include "distantrenderer.h"
    #include "namedirectory.h"
    #include "updatemanager.h"
    #include "pluginlistpreferencemodel.h"
    #include "previewrenderer.h"
    #include "version.h"
    #include "videoformatfpsmodel.h"
    #include "videoformatresolutionmodel.h"
    #include "videoinputdevicemodel.h"
    #include "wizardviewstepmodel.h"
    
    #include "api/peerdiscoverymodel.h"
    #include "api/newcodecmodel.h"
    #include "api/newdevicemodel.h"
    #include "api/datatransfermodel.h"
    #include "api/pluginmodel.h"
    #include "api/conversation.h"
    
    #include <QMetaType>
    #include <QQmlEngine>
    
    // clang-format off
    // TODO: remove this
    #define QML_REGISTERSINGLETONTYPE_WITH_INSTANCE(T) \
        qmlRegisterSingletonType<T>(NS_MODELS, VER_MAJ, 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);
    
    #define QML_REGISTERTYPE(NS, T) qmlRegisterType<T>(NS, VER_MAJ, VER_MIN, #T);
    
    #define QML_REGISTERNAMESPACE(NS, T, NAME) \
        qmlRegisterUncreatableMetaObject(T, NS, VER_MAJ, 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);
    
    #define QML_REGISTERUNCREATABLE_IN_NAMESPACE(T, NAMESPACE) \
        qmlRegisterUncreatableType<NAMESPACE::T>(NS_MODELS, \
                                                 VER_MAJ, VER_MIN, #T, \
                                                 "Don't try to add to a qml definition of " #T);
    
    namespace Utils {
    
    /*!
     * This function will expose custom types to the QML engine.
     */
    void
    registerTypes(QQmlEngine* engine,
                  SystemTray* systemTray,
                  LRCInstance* lrcInstance,
                  AppSettingsManager* appSettingsManager,
                  ScreenInfo* screenInfo,
                  QObject* parent)
    {
        // setup the adapters (their lifetimes are that of MainApplication)
        auto callAdapter = new CallAdapter(systemTray, lrcInstance, parent);
        auto messagesAdapter = new MessagesAdapter(appSettingsManager, lrcInstance, parent);
        auto conversationsAdapter = new ConversationsAdapter(systemTray, lrcInstance, parent);
        auto avAdapter = new AvAdapter(lrcInstance, parent);
        auto contactAdapter = new ContactAdapter(lrcInstance, parent);
        auto accountAdapter = new AccountAdapter(appSettingsManager, lrcInstance, parent);
        auto utilsAdapter = new UtilsAdapter(systemTray, lrcInstance, parent);
        auto settingsAdapter = new SettingsAdapter(appSettingsManager, lrcInstance, parent);
        auto pluginAdapter = new PluginAdapter(lrcInstance, parent);
        auto currentConversation = new CurrentConversation(lrcInstance, parent);
    
        // qml adapter registration
        QML_REGISTERSINGLETONTYPE_POBJECT(NS_ADAPTERS, callAdapter, "CallAdapter");
        QML_REGISTERSINGLETONTYPE_POBJECT(NS_ADAPTERS, messagesAdapter, "MessagesAdapter");
        QML_REGISTERSINGLETONTYPE_POBJECT(NS_ADAPTERS, conversationsAdapter, "ConversationsAdapter");
        QML_REGISTERSINGLETONTYPE_POBJECT(NS_ADAPTERS, avAdapter, "AvAdapter");
        QML_REGISTERSINGLETONTYPE_POBJECT(NS_ADAPTERS, contactAdapter, "ContactAdapter");
        QML_REGISTERSINGLETONTYPE_POBJECT(NS_ADAPTERS, accountAdapter, "AccountAdapter");
        QML_REGISTERSINGLETONTYPE_POBJECT(NS_ADAPTERS, utilsAdapter, "UtilsAdapter");
        QML_REGISTERSINGLETONTYPE_POBJECT(NS_ADAPTERS, settingsAdapter, "SettingsAdapter");
        QML_REGISTERSINGLETONTYPE_POBJECT(NS_ADAPTERS, pluginAdapter, "PluginAdapter");
        QML_REGISTERSINGLETONTYPE_POBJECT(NS_ADAPTERS, currentConversation, "CurrentConversation");
    
        // TODO: remove these
        QML_REGISTERSINGLETONTYPE_CUSTOM(NS_MODELS, AVModel, &lrcInstance->avModel())
        QML_REGISTERSINGLETONTYPE_CUSTOM(NS_MODELS, PluginModel, &lrcInstance->pluginModel())
        QML_REGISTERSINGLETONTYPE_CUSTOM(NS_HELPERS, UpdateManager, lrcInstance->getUpdateManager())
    
        // Hack for QtCreator autocomplete (part 2)
        // https://bugreports.qt.io/browse/QTCREATORBUG-20569
        // Use a dummy object to register the import namespace.
        // This occurs when we register from within MainApplication
        QML_REGISTERNAMESPACE(NS_MODELS, dummy::staticMetaObject, "");
        QML_REGISTERNAMESPACE(NS_ADAPTERS, dummy::staticMetaObject, "");
        QML_REGISTERNAMESPACE(NS_CONSTANTS, dummy::staticMetaObject, "");
        QML_REGISTERNAMESPACE(NS_HELPERS, dummy::staticMetaObject, "");
        QML_REGISTERNAMESPACE(NS_ENUMS, dummy::staticMetaObject, "");
    
        // QAbstractListModels
        QML_REGISTERTYPE(NS_MODELS, DeviceItemListModel);
        QML_REGISTERTYPE(NS_MODELS, BannedListModel);
        QML_REGISTERTYPE(NS_MODELS, ModeratorListModel);
        QML_REGISTERTYPE(NS_MODELS, MediaCodecListModel);
        QML_REGISTERTYPE(NS_MODELS, AccountsToMigrateListModel);
        QML_REGISTERTYPE(NS_MODELS, AudioDeviceModel);
        QML_REGISTERTYPE(NS_MODELS, AudioManagerListModel);
        QML_REGISTERTYPE(NS_MODELS, VideoInputDeviceModel);
        QML_REGISTERTYPE(NS_MODELS, VideoFormatResolutionModel);
        QML_REGISTERTYPE(NS_MODELS, VideoFormatFpsModel);
        QML_REGISTERTYPE(NS_MODELS, PluginListPreferenceModel);
        QML_REGISTERTYPE(NS_MODELS, FilesToSendListModel);
        QML_REGISTERTYPE(NS_MODELS, SmartListModel);
    
        // Roles & type enums for models
        QML_REGISTERNAMESPACE(NS_MODELS, AccountList::staticMetaObject, "AccountList");
        QML_REGISTERNAMESPACE(NS_MODELS, ConversationList::staticMetaObject, "ConversationList");
        QML_REGISTERNAMESPACE(NS_MODELS, ContactList::staticMetaObject, "ContactList");
        QML_REGISTERNAMESPACE(NS_MODELS, FilesToSend::staticMetaObject, "FilesToSend");
    
        // QQuickItems
        QML_REGISTERTYPE(NS_MODELS, PreviewRenderer);
        QML_REGISTERTYPE(NS_MODELS, VideoCallPreviewRenderer);
        QML_REGISTERTYPE(NS_MODELS, DistantRenderer);
        QML_REGISTERTYPE(NS_MODELS, PhotoboothPreviewRender)
    
        // Qml singleton components
        QML_REGISTERSINGLETONTYPE_URL(NS_CONSTANTS, "qrc:/src/constant/JamiTheme.qml", JamiTheme);
        QML_REGISTERSINGLETONTYPE_URL(NS_MODELS, "qrc:/src/constant/JamiQmlUtils.qml", JamiQmlUtils);
        QML_REGISTERSINGLETONTYPE_URL(NS_CONSTANTS, "qrc:/src/constant/JamiStrings.qml", JamiStrings);
        QML_REGISTERSINGLETONTYPE_URL(NS_CONSTANTS, "qrc:/src/constant/JamiResources.qml", JamiResources);
    
        QML_REGISTERSINGLETONTYPE_POBJECT(NS_CONSTANTS, screenInfo, "ScreenInfo")
        QML_REGISTERSINGLETONTYPE_POBJECT(NS_CONSTANTS, lrcInstance, "LRCInstance")
        QML_REGISTERSINGLETONTYPE_POBJECT(NS_CONSTANTS, appSettingsManager, "AppSettingsManager")
    
        auto avatarRegistry = new AvatarRegistry(lrcInstance, parent);
        auto wizardViewStepModel = new WizardViewStepModel(lrcInstance, accountAdapter, appSettingsManager, parent);
        QML_REGISTERSINGLETONTYPE_POBJECT(NS_HELPERS, avatarRegistry, "AvatarRegistry");
        QML_REGISTERSINGLETONTYPE_POBJECT(NS_MODELS, wizardViewStepModel, "WizardViewStepModel")
    
        // C++ singletons
        // TODO: remove this
        QML_REGISTERSINGLETONTYPE_WITH_INSTANCE(NameDirectory);
    
        // Lrc namespaces, models, and singletons
        QML_REGISTERNAMESPACE(NS_MODELS, lrc::api::staticMetaObject, "Lrc");
        QML_REGISTERNAMESPACE(NS_MODELS, lrc::api::account::staticMetaObject, "Account");
        QML_REGISTERNAMESPACE(NS_MODELS, lrc::api::call::staticMetaObject, "Call");
        QML_REGISTERNAMESPACE(NS_MODELS, lrc::api::datatransfer::staticMetaObject, "Datatransfer");
        QML_REGISTERNAMESPACE(NS_MODELS, lrc::api::interaction::staticMetaObject, "Interaction");
        QML_REGISTERNAMESPACE(NS_MODELS, lrc::api::video::staticMetaObject, "Video");
        QML_REGISTERNAMESPACE(NS_MODELS, lrc::api::profile::staticMetaObject, "Profile");
        QML_REGISTERNAMESPACE(NS_MODELS, lrc::api::conversation::staticMetaObject, "Conversation");
    
        // Same as QML_REGISTERUNCREATABLE but omit the namespace in Qml
        QML_REGISTERUNCREATABLE_IN_NAMESPACE(NewAccountModel, lrc::api);
        QML_REGISTERUNCREATABLE_IN_NAMESPACE(BehaviorController, lrc::api);
        QML_REGISTERUNCREATABLE_IN_NAMESPACE(DataTransferModel, lrc::api);
        QML_REGISTERUNCREATABLE_IN_NAMESPACE(ContactModel, lrc::api);
        QML_REGISTERUNCREATABLE_IN_NAMESPACE(ConversationModel, lrc::api);
        QML_REGISTERUNCREATABLE_IN_NAMESPACE(NewCallModel, lrc::api);
        QML_REGISTERUNCREATABLE_IN_NAMESPACE(NewDeviceModel, lrc::api);
        QML_REGISTERUNCREATABLE_IN_NAMESPACE(NewCodecModel, lrc::api);
        QML_REGISTERUNCREATABLE_IN_NAMESPACE(PeerDiscoveryModel, lrc::api);
    
        // Enums
        QML_REGISTERUNCREATABLE(NS_ENUMS, Settings);
        QML_REGISTERUNCREATABLE(NS_ENUMS, NetWorkManager);
        QML_REGISTERUNCREATABLE(NS_ENUMS, WizardViewStepModel)
    
        engine->addImageProvider(QLatin1String("qrImage"), new QrImageProvider(lrcInstance));
        engine->addImageProvider(QLatin1String("avatarImage"),
                                  new AvatarImageProvider(lrcInstance));
    
        engine->setObjectOwnership(&lrcInstance->avModel(), QQmlEngine::CppOwnership);
        engine->setObjectOwnership(&lrcInstance->pluginModel(), QQmlEngine::CppOwnership);
        engine->setObjectOwnership(lrcInstance->getUpdateManager(), QQmlEngine::CppOwnership);
        engine->setObjectOwnership(&NameDirectory::instance(), QQmlEngine::CppOwnership);
    }
    // clang-format on
    } // namespace Utils