Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[nemo-systemsettings] Add ErrorType enum to UserModel. Contributes to…
… JB#49466

Remove name argument from failure signals as it wasn't used anyway and
that information can be obtained by knowing which row is affected.

Signed-off-by: Tomi Leppänen <tomi.leppanen@jolla.com>
  • Loading branch information
Tomin1 committed Mar 31, 2020
1 parent b138822 commit 437de5d
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 16 deletions.
47 changes: 35 additions & 12 deletions src/usermodel.cpp
Expand Up @@ -42,13 +42,32 @@
#include <QString>
#include <functional>
#include <sailfishusermanagerinterface.h>
#include <grp.h>
#include <sys/types.h>
#include <grp.h>

namespace {
const auto UserManagerService = QStringLiteral(SAILFISH_USERMANAGER_DBUS_INTERFACE);
const auto UserManagerPath = QStringLiteral(SAILFISH_USERMANAGER_DBUS_OBJECT_PATH);
const auto UserManagerInterface = QStringLiteral(SAILFISH_USERMANAGER_DBUS_INTERFACE);

const QHash<const QString, int> errorTypeMap = {
{ QStringLiteral(SailfishUserManagerErrorBusy), UserModel::Busy },
{ QStringLiteral(SailfishUserManagerErrorHomeCreateFailed), UserModel::HomeCreateFailed },
{ QStringLiteral(SailfishUserManagerErrorHomeRemoveFailed), UserModel::HomeRemoveFailed },
{ QStringLiteral(SailfishUserManagerErrorGroupCreateFailed), UserModel::GroupCreateFailed },
{ QStringLiteral(SailfishUserManagerErrorUserAddFailed), UserModel::UserAddFailed },
{ QStringLiteral(SailfishUserManagerErrorUserModifyFailed), UserModel::UserModifyFailed },
{ QStringLiteral(SailfishUserManagerErrorUserRemoveFailed), UserModel::UserRemoveFailed },
{ QStringLiteral(SailfishUserManagerErrorGetUidFailed), UserModel::GetUidFailed },
};

int getErrorType(QDBusError &error)
{
if (error.type() != QDBusError::Other)
return error.type();

return errorTypeMap.value(error.name(), UserModel::OtherError);
}
}

UserModel::UserModel(QObject *parent)
Expand Down Expand Up @@ -93,12 +112,12 @@ void UserModel::setPlaceholder(bool value)
return;

if (value) {
auto row = m_users.count();
int row = m_users.count();
beginInsertRows(QModelIndex(), row, row);
m_users.append(UserInfo::placeholder());
endInsertRows();
} else {
auto row = m_users.count()-1;
int row = m_users.count()-1;
beginRemoveRows(QModelIndex(), row, row);
m_users.remove(row);
endRemoveRows();
Expand Down Expand Up @@ -325,16 +344,17 @@ void UserModel::onCurrentUserChangeFailed(uint uid)
{
if (m_uidsToRows.contains(uid)) {
int row = m_uidsToRows[uid];
emit setCurrentUserFailed(row, m_users[row].name());
emit setCurrentUserFailed(row, Failure);
}
}

void UserModel::userAddFinished(QDBusPendingCallWatcher *call, int row)
{
QDBusPendingReply<uint> reply = *call;
if (reply.isError()) {
emit userAddFailed();
qWarning() << "Adding user with usermanager failed:" << reply.error();
auto error = reply.error();
emit userAddFailed(getErrorType(error));
qCWarning(lcUsersLog) << "Adding user with usermanager failed:" << error;
} else {
uint uid = reply.value();
// Check that this was not just added to the list by onUserAdded
Expand All @@ -354,8 +374,9 @@ void UserModel::userModifyFinished(QDBusPendingCallWatcher *call, int row)
{
QDBusPendingReply<void> reply = *call;
if (reply.isError()) {
emit userModifyFailed(row, m_users.at(row).name());
qWarning() << "Modifying user with usermanager failed:" << reply.error();
auto error = reply.error();
emit userModifyFailed(row, getErrorType(error));
qCWarning(lcUsersLog) << "Modifying user with usermanager failed:" << error;
reset(row);
} // else awesome! (data was changed already)
call->deleteLater();
Expand All @@ -365,8 +386,9 @@ void UserModel::userRemoveFinished(QDBusPendingCallWatcher *call, int row)
{
QDBusPendingReply<void> reply = *call;
if (reply.isError()) {
emit userRemoveFailed(row, m_users.at(row).name());
qWarning() << "Removing user with usermanager failed:" << reply.error();
auto error = reply.error();
emit userRemoveFailed(row, getErrorType(error));
qCWarning(lcUsersLog) << "Removing user with usermanager failed:" << error;
} // else awesome! (waiting for signal to alter data)
call->deleteLater();
}
Expand All @@ -375,8 +397,9 @@ void UserModel::setCurrentUserFinished(QDBusPendingCallWatcher *call, int row)
{
QDBusPendingReply<void> reply = *call;
if (reply.isError()) {
emit setCurrentUserFailed(row, m_users.at(row).name());
qWarning() << "Switching user with usermanager failed:" << reply.error();
auto error = reply.error();
emit setCurrentUserFailed(row, getErrorType(error));
qCWarning(lcUsersLog) << "Switching user with usermanager failed:" << error;
} // else user switching was initiated successfully
call->deleteLater();
}
Expand Down
24 changes: 20 additions & 4 deletions src/usermodel.h
Expand Up @@ -33,6 +33,7 @@
#define USERMODEL_H

#include <QAbstractListModel>
#include <QDBusError>
#include <QHash>
#include <QVector>

Expand Down Expand Up @@ -66,6 +67,21 @@ class SYSTEMSETTINGS_EXPORT UserModel: public QAbstractListModel
};
Q_ENUM(UserType)

enum ErrorType {
Failure = QDBusError::Failed,
OtherError = QDBusError::Other,
InvalidArgs = QDBusError::InvalidArgs,
Busy = 100,
HomeCreateFailed,
HomeRemoveFailed,
GroupCreateFailed,
UserAddFailed,
UserModifyFailed,
UserRemoveFailed,
GetUidFailed,
};
Q_ENUM(ErrorType)

explicit UserModel(QObject *parent = 0);
~UserModel();

Expand All @@ -86,10 +102,10 @@ class SYSTEMSETTINGS_EXPORT UserModel: public QAbstractListModel

signals:
void placeholderChanged();
void userAddFailed();
void userModifyFailed(int row, const QString &name);
void userRemoveFailed(int row, const QString &name);
void setCurrentUserFailed(int row, const QString &name);
void userAddFailed(int error);
void userModifyFailed(int row, int error);
void userRemoveFailed(int row, int error);
void setCurrentUserFailed(int row, int error);

private slots:
void onUserAdded(const SailfishUserManagerEntry &entry);
Expand Down

0 comments on commit 437de5d

Please sign in to comment.