Commit 5d8fc0af authored by Tomi Leppänen's avatar Tomi Leppänen

[nemo-systemsettings] Add count and maximumCount to UserModel. Contributes 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's avatarTomi Leppänen <tomi.leppanen@jolla.com>
parent f1c8f1c0
......@@ -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 },
......@@ -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<int, QByteArray> UserModel::roleNames() const
{
static const QHash<int, QByteArray> 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();
}
......@@ -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 @@ public:
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;
......@@ -113,6 +118,7 @@ public:
signals:
void placeholderChanged();
void countChanged();
void userGroupsChanged(int row);
void userAddFailed(int error);
void userModifyFailed(int row, int error);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment