From bed7cddf065790b63ba2d79021d755a17477c09c Mon Sep 17 00:00:00 2001 From: Ronan Abhamon Date: Wed, 21 Dec 2016 11:26:07 +0100 Subject: [PATCH] feat(app): - avoid the usage of a lot of singletons, use methods in `CoreManager` instead - create a `UnregisteredSipAddressesProxyModel` model - unstable --- tests/CMakeLists.txt | 2 ++ tests/src/app/App.cpp | 14 +++++----- .../components/contacts/ContactsListModel.cpp | 2 -- .../components/contacts/ContactsListModel.hpp | 15 +---------- .../contacts/ContactsListProxyModel.cpp | 8 +++--- .../contacts/ContactsListProxyModel.hpp | 2 +- tests/src/components/core/CoreManager.cpp | 10 +++++++ tests/src/components/core/CoreManager.hpp | 27 ++++++++++++++++--- .../sip-addresses/SipAddressesModel.cpp | 4 +-- .../sip-addresses/SipAddressesModel.hpp | 14 +--------- .../UnregisteredSipAddressesModel.cpp | 4 +-- .../UnregisteredSipAddressesProxyModel.cpp | 3 +++ .../UnregisteredSipAddressesProxyModel.hpp | 12 +++++++++ .../src/components/timeline/TimelineModel.cpp | 4 +-- tests/ui/views/App/MainWindow/MainWindow.qml | 7 ++--- 15 files changed, 73 insertions(+), 55 deletions(-) create mode 100644 tests/src/components/sip-addresses/UnregisteredSipAddressesProxyModel.cpp create mode 100644 tests/src/components/sip-addresses/UnregisteredSipAddressesProxyModel.hpp diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 7f553821a..538b0257a 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -68,6 +68,7 @@ set(SOURCES src/components/settings/SettingsModel.cpp src/components/sip-addresses/SipAddressesModel.cpp src/components/sip-addresses/UnregisteredSipAddressesModel.cpp + src/components/sip-addresses/UnregisteredSipAddressesProxyModel.hpp src/components/timeline/TimelineModel.cpp src/main.cpp ) @@ -92,6 +93,7 @@ set(HEADERS src/components/settings/SettingsModel.hpp src/components/sip-addresses/SipAddressesModel.hpp src/components/sip-addresses/UnregisteredSipAddressesModel.hpp + src/components/sip-addresses/UnregisteredSipAddressesProxyModel.hpp src/components/timeline/TimelineModel.hpp src/utils.hpp ) diff --git a/tests/src/app/App.cpp b/tests/src/app/App.cpp index d7c1edafd..d9dded5f9 100644 --- a/tests/src/app/App.cpp +++ b/tests/src/app/App.cpp @@ -12,6 +12,7 @@ #include "../components/notifier/Notifier.hpp" #include "../components/settings/AccountSettingsModel.hpp" #include "../components/sip-addresses/SipAddressesModel.hpp" +#include "../components/sip-addresses/UnregisteredSipAddressesProxyModel.hpp" #include "../components/timeline/TimelineModel.hpp" #include "App.hpp" @@ -64,10 +65,8 @@ App::App (int &argc, char **argv) : QApplication(argc, argv) { void App::initContentApp () { qInfo() << "Initializing core manager..."; - // Init core & contacts. + // Init core. CoreManager::init(); - ContactsListModel::init(); - SipAddressesModel::init(); // Register types and load context properties. registerTypes(); @@ -99,8 +98,6 @@ void App::registerTypes () { "Linphone", 1, 0, "Presence", "Presence is uncreatable" ); - qRegisterMetaType("ChatModel::EntryType"); - qmlRegisterSingletonType( "Linphone", 1, 0, "App", [](QQmlEngine *, QJSEngine *) -> QObject *{ @@ -118,14 +115,14 @@ void App::registerTypes () { qmlRegisterSingletonType( "Linphone", 1, 0, "ContactsListModel", [](QQmlEngine *, QJSEngine *) -> QObject *{ - return ContactsListModel::getInstance(); + return CoreManager::getInstance()->getContactsListModel(); } ); qmlRegisterSingletonType( "Linphone", 1, 0, "SipAddressesModel", [](QQmlEngine *, QJSEngine *) -> QObject *{ - return SipAddressesModel::getInstance(); + return CoreManager::getInstance()->getSipAddressesModel(); } ); @@ -147,6 +144,9 @@ void App::registerTypes () { qmlRegisterType("Linphone", 1, 0, "ContactsListProxyModel"); qmlRegisterType("Linphone", 1, 0, "ChatModel"); qmlRegisterType("Linphone", 1, 0, "ChatProxyModel"); + qmlRegisterType("Linphone", 1, 0, "UnregisteredSipAddressesProxyModel"); + + qRegisterMetaType("ChatModel::EntryType"); } void App::addContextProperties () { diff --git a/tests/src/components/contacts/ContactsListModel.cpp b/tests/src/components/contacts/ContactsListModel.cpp index f349e054a..66a43c4e1 100644 --- a/tests/src/components/contacts/ContactsListModel.cpp +++ b/tests/src/components/contacts/ContactsListModel.cpp @@ -10,8 +10,6 @@ using namespace std; // ============================================================================= -ContactsListModel *ContactsListModel::m_instance = nullptr; - ContactsListModel::ContactsListModel (QObject *parent) : QAbstractListModel(parent) { m_linphone_friends = CoreManager::getInstance()->getCore()->getFriendsLists().front(); diff --git a/tests/src/components/contacts/ContactsListModel.hpp b/tests/src/components/contacts/ContactsListModel.hpp index f90fb1411..133eaeb17 100644 --- a/tests/src/components/contacts/ContactsListModel.hpp +++ b/tests/src/components/contacts/ContactsListModel.hpp @@ -15,6 +15,7 @@ class ContactsListModel : public QAbstractListModel { friend class SipAddressesModel; public: + ContactsListModel (QObject *parent = Q_NULLPTR); ~ContactsListModel () = default; int rowCount (const QModelIndex &index = QModelIndex()) const override; @@ -25,27 +26,13 @@ public: bool removeRow (int row, const QModelIndex &parent = QModelIndex()); bool removeRows (int row, int count, const QModelIndex &parent = QModelIndex()) override; - static void init () { - if (!m_instance) { - m_instance = new ContactsListModel(); - } - } - - static ContactsListModel *getInstance () { - return m_instance; - } - public slots: ContactModel *addContact (VcardModel *vcard); void removeContact (ContactModel *contact); private: - ContactsListModel (QObject *parent = Q_NULLPTR); - QList m_list; std::shared_ptr m_linphone_friends; - - static ContactsListModel *m_instance; }; #endif // CONTACTS_LIST_MODEL_H_ diff --git a/tests/src/components/contacts/ContactsListProxyModel.cpp b/tests/src/components/contacts/ContactsListProxyModel.cpp index 60eea9722..ac24a6538 100644 --- a/tests/src/components/contacts/ContactsListProxyModel.cpp +++ b/tests/src/components/contacts/ContactsListProxyModel.cpp @@ -1,7 +1,7 @@ #include #include "../../utils.hpp" -#include "ContactsListModel.hpp" +#include "../core/CoreManager.hpp" #include "ContactsListProxyModel.hpp" @@ -32,15 +32,15 @@ const QRegExp ContactsListProxyModel::m_search_separators("^[^_.-;@ ][_.-;@ ]"); // ----------------------------------------------------------------------------- ContactsListProxyModel::ContactsListProxyModel (QObject *parent) : QSortFilterProxyModel(parent) { - m_list = ContactsListModel::getInstance(); + m_list = CoreManager::getInstance()->getContactsListModel(); setSourceModel(m_list); setFilterCaseSensitivity(Qt::CaseInsensitive); + setDynamicSortFilter(false); for (const ContactModel *contact : m_list->m_list) m_weights[contact] = 0; - setDynamicSortFilter(false); sort(0); } @@ -125,6 +125,6 @@ float ContactsListProxyModel::computeContactWeight (const ContactModel &contact) void ContactsListProxyModel::setConnectedFilter (bool use_connected_filter) { if (use_connected_filter != m_use_connected_filter) { m_use_connected_filter = use_connected_filter; - invalidate(); + invalidateFilter(); } } diff --git a/tests/src/components/contacts/ContactsListProxyModel.hpp b/tests/src/components/contacts/ContactsListProxyModel.hpp index 290ca01f6..9d257ffad 100644 --- a/tests/src/components/contacts/ContactsListProxyModel.hpp +++ b/tests/src/components/contacts/ContactsListProxyModel.hpp @@ -25,7 +25,7 @@ public: public slots: void setFilter (const QString &pattern) { setFilterFixedString(pattern); - sort(0); + invalidateFilter(); } protected: diff --git a/tests/src/components/core/CoreManager.cpp b/tests/src/components/core/CoreManager.cpp index c71931782..fedb91bff 100644 --- a/tests/src/components/core/CoreManager.cpp +++ b/tests/src/components/core/CoreManager.cpp @@ -13,6 +13,16 @@ CoreManager::CoreManager (QObject *parent) : QObject(parent), setDatabasesPaths(); } +void CoreManager::init () { + if (!m_instance) { + m_instance = new CoreManager(); + + m_instance->m_contacts_list_model = new ContactsListModel(m_instance); + m_instance->m_sip_addresses_model = new SipAddressesModel(m_instance); + m_instance->m_unregistered_sip_addresses_model = new UnregisteredSipAddressesModel(m_instance); + } +} + VcardModel *CoreManager::createDetachedVcardModel () { return new VcardModel(linphone::Factory::get()->createVcard()); } diff --git a/tests/src/components/core/CoreManager.hpp b/tests/src/components/core/CoreManager.hpp index d7f6dca9f..44c2e85db 100644 --- a/tests/src/components/core/CoreManager.hpp +++ b/tests/src/components/core/CoreManager.hpp @@ -2,6 +2,9 @@ #define CORE_MANAGER_H_ #include "../contact/VcardModel.hpp" +#include "../contacts/ContactsListModel.hpp" +#include "../sip-addresses/SipAddressesModel.hpp" +#include "../sip-addresses/UnregisteredSipAddressesModel.hpp" // ============================================================================= @@ -15,12 +18,24 @@ public: return m_core; } - static void init () { - if (!m_instance) { - m_instance = new CoreManager(); - } + // --------------------------------------------------------------------------- + // Singleton models. + // --------------------------------------------------------------------------- + + ContactsListModel *getContactsListModel () { + return m_contacts_list_model; } + SipAddressesModel *getSipAddressesModel () { + return m_sip_addresses_model; + } + + UnregisteredSipAddressesModel *getUnregisteredSipAddressesModel () { + return m_unregistered_sip_addresses_model; + } + + static void init (); + static CoreManager *getInstance () { return m_instance; } @@ -36,6 +51,10 @@ private: void setDatabasesPaths (); std::shared_ptr m_core; + ContactsListModel *m_contacts_list_model; + SipAddressesModel *m_sip_addresses_model; + UnregisteredSipAddressesModel *m_unregistered_sip_addresses_model; + static CoreManager *m_instance; }; diff --git a/tests/src/components/sip-addresses/SipAddressesModel.cpp b/tests/src/components/sip-addresses/SipAddressesModel.cpp index adaba93fd..2bac46ab0 100644 --- a/tests/src/components/sip-addresses/SipAddressesModel.cpp +++ b/tests/src/components/sip-addresses/SipAddressesModel.cpp @@ -9,8 +9,6 @@ // ============================================================================= -SipAddressesModel *SipAddressesModel::m_instance = nullptr; - SipAddressesModel::SipAddressesModel (QObject *parent) : QAbstractListModel(parent) { shared_ptr core = CoreManager::getInstance()->getCore(); @@ -51,7 +49,7 @@ SipAddressesModel::SipAddressesModel (QObject *parent) : QAbstractListModel(pare } // Get sip addresses from contacts. - for (auto &contact : ContactsListModel::getInstance()->m_list) { + for (auto &contact : CoreManager::getInstance()->getContactsListModel()->m_list) { for (const auto &sip_address : contact->getVcardModel()->getSipAddresses()) { auto it = m_sip_addresses.find(sip_address.toString()); diff --git a/tests/src/components/sip-addresses/SipAddressesModel.hpp b/tests/src/components/sip-addresses/SipAddressesModel.hpp index 22b8acea0..45109472f 100644 --- a/tests/src/components/sip-addresses/SipAddressesModel.hpp +++ b/tests/src/components/sip-addresses/SipAddressesModel.hpp @@ -11,6 +11,7 @@ class SipAddressesModel : public QAbstractListModel { Q_OBJECT; public: + SipAddressesModel (QObject *parent = Q_NULLPTR); ~SipAddressesModel () = default; int rowCount (const QModelIndex &index = QModelIndex()) const override; @@ -18,25 +19,12 @@ public: QHash roleNames () const override; QVariant data (const QModelIndex &index, int role) const override; - static void init () { - if (!m_instance) - m_instance = new SipAddressesModel(); - } - - static SipAddressesModel *getInstance () { - return m_instance; - } - public slots: ContactModel *mapSipAddressToContact (const QString &sip_address) const; private: - SipAddressesModel (QObject *parent = Q_NULLPTR); - QHash m_sip_addresses; QList m_refs; - - static SipAddressesModel *m_instance; }; #endif // SIP_ADDRESSES_MODEL_H_ diff --git a/tests/src/components/sip-addresses/UnregisteredSipAddressesModel.cpp b/tests/src/components/sip-addresses/UnregisteredSipAddressesModel.cpp index e8716a67f..610407d48 100644 --- a/tests/src/components/sip-addresses/UnregisteredSipAddressesModel.cpp +++ b/tests/src/components/sip-addresses/UnregisteredSipAddressesModel.cpp @@ -1,11 +1,11 @@ -#include "../sip-addresses/SipAddressesModel.hpp" +#include "../core/CoreManager.hpp" #include "UnregisteredSipAddressesModel.hpp" // ============================================================================= UnregisteredSipAddressesModel::UnregisteredSipAddressesModel (QObject *parent) : QSortFilterProxyModel(parent) { - setSourceModel(SipAddressesModel::getInstance()); + setSourceModel(CoreManager::getInstance()->getSipAddressesModel()); } bool UnregisteredSipAddressesModel::filterAcceptsRow (int source_row, const QModelIndex &source_parent) const { diff --git a/tests/src/components/sip-addresses/UnregisteredSipAddressesProxyModel.cpp b/tests/src/components/sip-addresses/UnregisteredSipAddressesProxyModel.cpp new file mode 100644 index 000000000..b7896163d --- /dev/null +++ b/tests/src/components/sip-addresses/UnregisteredSipAddressesProxyModel.cpp @@ -0,0 +1,3 @@ +#include "UnregisteredSipAddressesProxyModel.hpp" + +// ============================================================================= diff --git a/tests/src/components/sip-addresses/UnregisteredSipAddressesProxyModel.hpp b/tests/src/components/sip-addresses/UnregisteredSipAddressesProxyModel.hpp new file mode 100644 index 000000000..edd521c51 --- /dev/null +++ b/tests/src/components/sip-addresses/UnregisteredSipAddressesProxyModel.hpp @@ -0,0 +1,12 @@ +#ifndef UNREGISTERED_SIP_ADDRESSES_PROXY_MODEL_H_ +#define UNREGISTERED_SIP_ADDRESSES_PROXY_MODEL_H_ + +#include "UnregisteredSipAddressesModel.hpp" + +// ============================================================================= + +class UnregisteredSipAddressesProxyModel : public QSortFilterProxyModel { + Q_OBJECT; +}; + +#endif // UNREGISTERED_SIP_ADDRESSES_PROXY_MODEL_H_ diff --git a/tests/src/components/timeline/TimelineModel.cpp b/tests/src/components/timeline/TimelineModel.cpp index 88ae7f234..0d3421c9f 100644 --- a/tests/src/components/timeline/TimelineModel.cpp +++ b/tests/src/components/timeline/TimelineModel.cpp @@ -1,11 +1,11 @@ -#include "../sip-addresses/SipAddressesModel.hpp" +#include "../core/CoreManager.hpp" #include "TimelineModel.hpp" // ============================================================================= TimelineModel::TimelineModel (QObject *parent) : QSortFilterProxyModel(parent) { - setSourceModel(SipAddressesModel::getInstance()); + setSourceModel(CoreManager::getInstance()->getSipAddressesModel()); sort(0); } diff --git a/tests/ui/views/App/MainWindow/MainWindow.qml b/tests/ui/views/App/MainWindow/MainWindow.qml index 1d5a2eabe..54b697294 100644 --- a/tests/ui/views/App/MainWindow/MainWindow.qml +++ b/tests/ui/views/App/MainWindow/MainWindow.qml @@ -27,8 +27,7 @@ ApplicationWindow { title: MainWindowStyle.title visible: true - onActiveFocusItemChanged: activeFocusItem == null && - smartSearchBar.hideMenu() + onActiveFocusItemChanged: activeFocusItem == null && smartSearchBar.hideMenu() // --------------------------------------------------------------------------- // Toolbar properties. @@ -88,8 +87,10 @@ ApplicationWindow { Layout.fillWidth: true entryHeight: MainWindowStyle.searchBox.entryHeight maxMenuHeight: MainWindowStyle.searchBox.maxHeight - model: ContactsListProxyModel {} placeholderText: qsTr('mainSearchBarPlaceholder') + + contactsModel: ContactsListProxyModel {} + othersSipAddresses: UnregisteredSipAddressesProxyModel {} } } }