diff --git a/src/app/accountadapter.cpp b/src/app/accountadapter.cpp index 2475d0ef86483d14b6eb6eda6392d0ffbd32ee14..bc6e4aa098422ed160af31880447ea20fb62cd2e 100644 --- a/src/app/accountadapter.cpp +++ b/src/app/accountadapter.cpp @@ -32,8 +32,10 @@ AccountAdapter::AccountAdapter(AppSettingsManager* settingsManager, : QmlAdapterBase(instance, parent) , settingsManager_(settingsManager) , accountListModel_(new AccountListModel(instance)) + , deviceItemListModel_(new DeviceItemListModel(instance)) { QML_REGISTERSINGLETONTYPE_POBJECT(NS_MODELS, accountListModel_.get(), "AccountListModel"); + QML_REGISTERSINGLETONTYPE_POBJECT(NS_MODELS, deviceItemListModel_.get(), "DeviceItemListModel"); connect(&lrcInstance_->accountModel(), &AccountModel::accountStatusChanged, diff --git a/src/app/accountadapter.h b/src/app/accountadapter.h index 8794e38a1b43bfaaba22e388b777f8ac2d10817b..83052c4e6392132db75c8ed7f3ce2b4b278c3ff0 100644 --- a/src/app/accountadapter.h +++ b/src/app/accountadapter.h @@ -21,6 +21,7 @@ #include "qmladapterbase.h" #include "accountlistmodel.h" +#include "deviceitemlistmodel.h" #include "lrcinstance.h" #include "utils.h" @@ -103,5 +104,6 @@ private: AppSettingsManager* settingsManager_; QScopedPointer<AccountListModel> accountListModel_; + QScopedPointer<DeviceItemListModel> deviceItemListModel_; }; Q_DECLARE_METATYPE(AccountAdapter*) diff --git a/src/app/deviceitemlistmodel.cpp b/src/app/deviceitemlistmodel.cpp index 5f9ce3623e96812940a00a0f656b0fa614b66230..dcff6595d4c97eab7c3b54ee4db2e0d797beee96 100644 --- a/src/app/deviceitemlistmodel.cpp +++ b/src/app/deviceitemlistmodel.cpp @@ -26,49 +26,29 @@ #include "api/conversation.h" #include "api/devicemodel.h" -DeviceItemListModel::DeviceItemListModel(QObject* parent) +DeviceItemListModel::DeviceItemListModel(LRCInstance* instance, QObject* parent) : AbstractListModelBase(parent) { - connect(this, &AbstractListModelBase::lrcInstanceChanged, [this] { - if (lrcInstance_) { - if (!lrcInstance_->get_currentAccountId().isEmpty()) - onAccountChanged(); - connect(lrcInstance_, - &LRCInstance::currentAccountIdChanged, - this, - &DeviceItemListModel::onAccountChanged, - Qt::UniqueConnection); - } - }); -} + lrcInstance_ = instance; -DeviceItemListModel::~DeviceItemListModel() {} + connect(lrcInstance_, + &LRCInstance::currentAccountIdChanged, + this, + &DeviceItemListModel::connectAccount, + Qt::UniqueConnection); + + connectAccount(); +} int DeviceItemListModel::rowCount(const QModelIndex& parent) const { if (!parent.isValid() && lrcInstance_) { - /* - * Count. - */ return lrcInstance_->getCurrentAccountInfo().deviceModel->getAllDevices().size(); } - /* - * A valid QModelIndex returns 0 as no entry has sub-elements. - */ return 0; } -int -DeviceItemListModel::columnCount(const QModelIndex& parent) const -{ - Q_UNUSED(parent); - /* - * Only need one column. - */ - return 1; -} - QVariant DeviceItemListModel::data(const QModelIndex& index, int role) const { @@ -98,27 +78,6 @@ DeviceItemListModel::roleNames() const return roles; } -QModelIndex -DeviceItemListModel::index(int row, int column, const QModelIndex& parent) const -{ - Q_UNUSED(parent); - if (column != 0) { - return QModelIndex(); - } - - if (row >= 0 && row < rowCount()) { - return createIndex(row, column); - } - return QModelIndex(); -} - -QModelIndex -DeviceItemListModel::parent(const QModelIndex& child) const -{ - Q_UNUSED(child); - return QModelIndex(); -} - Qt::ItemFlags DeviceItemListModel::flags(const QModelIndex& index) const { @@ -143,8 +102,12 @@ DeviceItemListModel::revokeDevice(QString deviceId, QString password) } void -DeviceItemListModel::onAccountChanged() +DeviceItemListModel::connectAccount() { + if (lrcInstance_->get_currentAccountId().isEmpty()) { + return; + } + reset(); auto* deviceModel = lrcInstance_->getCurrentAccountInfo().deviceModel.get(); diff --git a/src/app/deviceitemlistmodel.h b/src/app/deviceitemlistmodel.h index 3082247de8adc0c8138b661e9e857fdccbd4ddfd..50b699085ff6f5e324fa0cc9c624fe3f66adca3f 100644 --- a/src/app/deviceitemlistmodel.h +++ b/src/app/deviceitemlistmodel.h @@ -21,9 +21,7 @@ #include "abstractlistmodelbase.h" -#include <QSortFilterProxyModel> - -class DeviceItemListModel : public AbstractListModelBase +class DeviceItemListModel final : public AbstractListModelBase { Q_OBJECT @@ -31,74 +29,18 @@ public: enum Role { DeviceName = Qt::UserRole + 1, DeviceID, IsCurrent }; Q_ENUM(Role) - explicit DeviceItemListModel(QObject* parent = nullptr); - ~DeviceItemListModel(); + explicit DeviceItemListModel(LRCInstance* instance, QObject* parent = nullptr); // QAbstractListModel override. int rowCount(const QModelIndex& parent = QModelIndex()) const override; - int columnCount(const QModelIndex& parent) const override; QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override; - - // Override role name as access point in qml. QHash<int, QByteArray> roleNames() const override; - QModelIndex index(int row, int column = 0, const QModelIndex& parent = QModelIndex()) const; - QModelIndex parent(const QModelIndex& child) const; - Qt::ItemFlags flags(const QModelIndex& index) const; + Qt::ItemFlags flags(const QModelIndex& index) const override; // This function is to reset the model when there's new account added. Q_INVOKABLE void reset(); - Q_INVOKABLE void revokeDevice(QString deviceId, QString password); public Q_SLOTS: - void onAccountChanged(); -}; - -class DeviceItemProxyModel : public QSortFilterProxyModel -{ - Q_OBJECT - Q_PROPERTY(LRCInstance* lrcInstance READ getLrcInstance WRITE setLrcInstance) - -public: - explicit DeviceItemProxyModel(QObject* parent = nullptr) - : QSortFilterProxyModel(parent) - { - sourceModel_ = new DeviceItemListModel(this); - - setSourceModel(sourceModel_); - setSortRole(DeviceItemListModel::Role::IsCurrent); - sort(0, Qt::DescendingOrder); - setFilterCaseSensitivity(Qt::CaseSensitivity::CaseInsensitive); - } - - bool lessThan(const QModelIndex& left, const QModelIndex& right) const override - { - QVariant leftIsCurrent = sourceModel()->data(left, DeviceItemListModel::Role::IsCurrent); - QVariant rightIsCurrent = sourceModel()->data(right, DeviceItemListModel::Role::IsCurrent); - - if (leftIsCurrent.toBool()) - return false; - if (rightIsCurrent.toBool()) - return true; - - QChar leftDeviceNameFirstChar - = sourceModel()->data(left, DeviceItemListModel::Role::DeviceName).toString().at(0); - QChar rightDeviceNameFirstChar - = sourceModel()->data(right, DeviceItemListModel::Role::DeviceName).toString().at(0); - - return leftDeviceNameFirstChar < rightDeviceNameFirstChar; - } - - LRCInstance* getLrcInstance() - { - return sourceModel_->property("lrcInstance").value<LRCInstance*>(); - } - - void setLrcInstance(LRCInstance* instance) - { - sourceModel_->setProperty("lrcInstance", QVariant::fromValue(instance)); - } - -private: - DeviceItemListModel* sourceModel_; + void connectAccount(); }; diff --git a/src/app/qmlregister.cpp b/src/app/qmlregister.cpp index 1fc0ea4c8d55ef36efce535d33743fc11991d923..e9a9317709ed47031ac8da1f63cd47bff0594f17 100644 --- a/src/app/qmlregister.cpp +++ b/src/app/qmlregister.cpp @@ -153,7 +153,6 @@ registerTypes(QQmlEngine* engine, QML_REGISTERNAMESPACE(NS_ENUMS, dummy::staticMetaObject, ""); // QAbstractListModels - QML_REGISTERTYPE(NS_MODELS, DeviceItemProxyModel); QML_REGISTERTYPE(NS_MODELS, BannedListModel); QML_REGISTERTYPE(NS_MODELS, ModeratorListModel); QML_REGISTERTYPE(NS_MODELS, MediaCodecListModel); diff --git a/src/app/settingsview/components/LinkedDevices.qml b/src/app/settingsview/components/LinkedDevices.qml index 7f6e962e469c73247ec347433f6a647e55dc53bc..9b6eb9d73a8d01014a8610e2f51e67b099c83868 100644 --- a/src/app/settingsview/components/LinkedDevices.qml +++ b/src/app/settingsview/components/LinkedDevices.qml @@ -20,6 +20,8 @@ import QtQuick import QtQuick.Controls import QtQuick.Layouts +import SortFilterProxyModel 0.2 + import net.jami.Models 1.1 import net.jami.Adapters 1.1 import net.jami.Constants 1.1 @@ -49,7 +51,7 @@ ColumnLayout { id: revokeDevicePasswordDialog onRevokeDeviceWithPassword: function(idOfDevice, password) { - deviceItemListModel.sourceModel.revokeDevice(idOfDevice, password) + DeviceItemListModel.revokeDevice(idOfDevice, password) } } @@ -64,7 +66,9 @@ ColumnLayout { buttonTitles: [JamiStrings.optionOk, JamiStrings.optionCancel] buttonStyles: [SimpleMessageDialog.ButtonStyle.TintedBlue, SimpleMessageDialog.ButtonStyle.TintedBlack] - buttonCallBacks: [function() {deviceItemListModel.sourceModel.revokeDevice(idOfDev, "")}] + buttonCallBacks: [ + function() { DeviceItemListModel.revokeDevice(idOfDev, "") } + ] } Label { @@ -83,10 +87,15 @@ ColumnLayout { Layout.fillWidth: true Layout.preferredHeight: 160 - model: DeviceItemProxyModel { - id: deviceItemListModel - - lrcInstance: LRCInstance + model: SortFilterProxyModel { + sourceModel: DeviceItemListModel + sorters: [ + RoleSorter { roleName: "IsCurrent"; sortOrder: Qt.DescendingOrder }, + StringSorter { + roleName: "DeviceName" + caseSensitivity: Qt.CaseInsensitive + } + ] } delegate: DeviceItemDelegate {