Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[nemo-systemsettings] Add count and maximumCount to UserModel. Contri…
…butes to JB#49948

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 <tomi.leppanen@jolla.com>
  • Loading branch information
Tomin1 committed May 27, 2020
1 parent f1c8f1c commit 5d8fc0a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
27 changes: 25 additions & 2 deletions src/usermodel.cpp
Expand Up @@ -57,6 +57,7 @@ const QHash<const QString, int> 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 },
Expand Down Expand Up @@ -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)
Expand All @@ -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<int, QByteArray> UserModel::roleNames() const
{
static const QHash<int, QByteArray> roles = {
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -385,6 +406,7 @@ void UserModel::onUserRemoved(uint uid)
iter.value() -= 1;
}
endRemoveRows();
emit countChanged();
}

void UserModel::onCurrentUserChanged(uint uid)
Expand Down Expand Up @@ -544,4 +566,5 @@ void UserModel::add(UserInfo &user)
m_uidsToRows.insert(user.uid(), row);
endInsertRows();
}
emit countChanged();
}
8 changes: 7 additions & 1 deletion src/usermodel.h
Expand Up @@ -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 {
Expand Down Expand Up @@ -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<int, QByteArray> roleNames() const;
int rowCount(const QModelIndex &parent = QModelIndex()) const;
Expand All @@ -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);
Expand Down

0 comments on commit 5d8fc0a

Please sign in to comment.