From 5d8fc0afce98d6a9a8ea1e8727ca34d8f6ab2594 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomi=20Lepp=C3=A4nen?= Date: Mon, 25 May 2020 09:59:24 +0300 Subject: [PATCH] [nemo-systemsettings] Add count and maximumCount to UserModel. Contributes to JB#49948 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Expose number of existing users and maximum number of users and also add new error enum for trying to go over maximum number of users. Remove a useless comment line and make placeholder() const too. Signed-off-by: Tomi Leppänen --- src/usermodel.cpp | 27 +++++++++++++++++++++++++-- src/usermodel.h | 8 +++++++- 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/src/usermodel.cpp b/src/usermodel.cpp index b59f417..943d292 100644 --- a/src/usermodel.cpp +++ b/src/usermodel.cpp @@ -57,6 +57,7 @@ const QHash errorTypeMap = { { QStringLiteral(SailfishUserManagerErrorHomeRemoveFailed), UserModel::HomeRemoveFailed }, { QStringLiteral(SailfishUserManagerErrorGroupCreateFailed), UserModel::GroupCreateFailed }, { QStringLiteral(SailfishUserManagerErrorUserAddFailed), UserModel::UserAddFailed }, + { QStringLiteral(SailfishUserManagerErrorMaxUsersReached), UserModel::MaximumNumberOfUsersReached }, { QStringLiteral(SailfishUserManagerErrorUserModifyFailed), UserModel::UserModifyFailed }, { QStringLiteral(SailfishUserManagerErrorUserRemoveFailed), UserModel::UserRemoveFailed }, { QStringLiteral(SailfishUserManagerErrorGetUidFailed), UserModel::GetUidFailed }, @@ -106,7 +107,7 @@ UserModel::~UserModel() { } -bool UserModel::placeholder() +bool UserModel::placeholder() const { // Placeholder is always last and the only item that can be invalid if (m_users.count() == 0) @@ -133,6 +134,27 @@ void UserModel::setPlaceholder(bool value) emit placeholderChanged(); } +/* + * Number of existing users + * + * If placeholder = false, then this is the same as rowCount. + */ +int UserModel::count() const +{ + return (placeholder()) ? m_users.count()-1 : m_users.count(); +} + +/* + * Maximum number of users that can be created + * + * If more users are created after count reaches this, + * MaximumNumberOfUsersReached may be thrown and user creation fails. + */ +int UserModel::maximumCount() const +{ + return SAILFISH_USERMANAGER_MAX_USERS; +} + QHash UserModel::roleNames() const { static const QHash roles = { @@ -222,7 +244,6 @@ QModelIndex UserModel::index(int row, int column, const QModelIndex &parent) con if (row < 0 || row >= m_users.count() || column != 0) return QModelIndex(); - // create index return createIndex(row, 0, row); } @@ -385,6 +406,7 @@ void UserModel::onUserRemoved(uint uid) iter.value() -= 1; } endRemoveRows(); + emit countChanged(); } void UserModel::onCurrentUserChanged(uint uid) @@ -544,4 +566,5 @@ void UserModel::add(UserInfo &user) m_uidsToRows.insert(user.uid(), row); endInsertRows(); } + emit countChanged(); } diff --git a/src/usermodel.h b/src/usermodel.h index ab9dcde..cf776fa 100644 --- a/src/usermodel.h +++ b/src/usermodel.h @@ -50,6 +50,8 @@ class SYSTEMSETTINGS_EXPORT UserModel: public QAbstractListModel { Q_OBJECT Q_PROPERTY(bool placeholder READ placeholder WRITE setPlaceholder NOTIFY placeholderChanged) + Q_PROPERTY(int count READ count NOTIFY countChanged) + Q_PROPERTY(int maximumCount READ maximumCount CONSTANT) public: enum Roles { @@ -84,14 +86,17 @@ class SYSTEMSETTINGS_EXPORT UserModel: public QAbstractListModel UserNotFound, AddToGroupFailed, RemoveFromGroupFailed, + MaximumNumberOfUsersReached, }; Q_ENUM(ErrorType) explicit UserModel(QObject *parent = 0); ~UserModel(); - bool placeholder(); + bool placeholder() const; void setPlaceholder(bool value); + int count() const; + int maximumCount() const; QHash roleNames() const; int rowCount(const QModelIndex &parent = QModelIndex()) const; @@ -113,6 +118,7 @@ class SYSTEMSETTINGS_EXPORT UserModel: public QAbstractListModel signals: void placeholderChanged(); + void countChanged(); void userGroupsChanged(int row); void userAddFailed(int error); void userModifyFailed(int row, int error);