diff options
author | David Edmundson <david@davidedmundson.co.uk> | 2011-11-11 01:39:46 +0000 |
---|---|---|
committer | David Edmundson <david@davidedmundson.co.uk> | 2011-11-11 01:39:46 +0000 |
commit | b31a9e8bd75fc3f3f68fb62a7c6ddc54e8a33972 (patch) | |
tree | 64f9e022f73176382c120a1775d100c71f69a678 /liblightdm-qt | |
parent | ff9840a78034159935b940e15eec9631338e8d1e (diff) | |
download | lightdm-b31a9e8bd75fc3f3f68fb62a7c6ddc54e8a33972.tar.gz |
Tidy users model
Diffstat (limited to 'liblightdm-qt')
-rw-r--r-- | liblightdm-qt/QLightDM/session.h | 5 | ||||
-rw-r--r-- | liblightdm-qt/QLightDM/user.h | 81 | ||||
-rw-r--r-- | liblightdm-qt/user.cpp | 126 |
3 files changed, 57 insertions, 155 deletions
diff --git a/liblightdm-qt/QLightDM/session.h b/liblightdm-qt/QLightDM/session.h index 83310bbc..e3343274 100644 --- a/liblightdm-qt/QLightDM/session.h +++ b/liblightdm-qt/QLightDM/session.h @@ -21,10 +21,13 @@ namespace QLightDM { { Q_OBJECT public: + enum SessionModelRoles { + IdRole = Qt::UserRole + }; + explicit SessionsModel(QObject *parent = 0); virtual ~SessionsModel(); - enum SessionModelRoles {IdRole = Qt::UserRole}; int rowCount(const QModelIndex &parent) const; QVariant data(const QModelIndex &index, int role=Qt::DisplayRole) const; diff --git a/liblightdm-qt/QLightDM/user.h b/liblightdm-qt/QLightDM/user.h index 08841fc1..136e2c1f 100644 --- a/liblightdm-qt/QLightDM/user.h +++ b/liblightdm-qt/QLightDM/user.h @@ -18,75 +18,34 @@ class UserPrivate; class UsersModelPrivate; +class UserItem; namespace QLightDM { - //public facing User class - /** Class storing user information. - This is an implicitly shared class. */ - - class Q_DECL_EXPORT User - { - public: - explicit User(); - User(const QString &name, const QString &realName, const QString &homeDirectory, const QString &image, bool isLoggedIn); - User(const User& other); - ~User(); - User &operator=(const User& other); - - bool update(const QString &realName, const QString &homeDirectory, const QString &image, bool isLoggedIn); - - /** The name to display (the real name if available, otherwise use the username */ - QString displayName() const; - - /** The username of the user*/ - QString name() const; - /** The user's real name, use this for displaying*/ - QString realName() const; - - /** Returns the home directory of this user*/ - QString homeDirectory() const; - - /** Returns the path to an avatar of this user*/ - QString image() const; - - /** Returns true if this user is already logged in on another session*/ - bool isLoggedIn() const; - - // LdmUser &operator=(const LdmUser user); - private: - QSharedDataPointer<UserPrivate> d; - }; - - class Q_DECL_EXPORT UsersModel : public QAbstractListModel - { - Q_OBJECT - public: - explicit UsersModel(QObject *parent = 0); - ~UsersModel(); - - enum UserModelRoles {NameRole = Qt::UserRole, - RealNameRole, - LoggedInRole}; - - int rowCount(const QModelIndex &parent) const; - QVariant data(const QModelIndex &index, int role) const; +class Q_DECL_EXPORT UsersModel : public QAbstractListModel +{ + Q_OBJECT +public: + explicit UsersModel(QObject *parent = 0); + ~UsersModel(); - signals: + enum UserModelRoles {NameRole = Qt::UserRole, + RealNameRole, + LoggedInRole}; - public slots: + int rowCount(const QModelIndex &parent) const; + QVariant data(const QModelIndex &index, int role) const; - private slots: - /** Updates the model with new changes in the password file*/ - void loadUsers(); +private slots: + /** Updates the model with new changes in the password file*/ + void loadUsers(); - private: - /** Returns a list of all users in the password file*/ - QList<User> getUsers(); - UsersModelPrivate *d; - }; +private: + UsersModelPrivate *d; + QList<UserItem> getUsers() const; +}; - UsersModel *users(); +UsersModel *users(); } #endif // QLIGHTDM_USER_H diff --git a/liblightdm-qt/user.cpp b/liblightdm-qt/user.cpp index f5adba70..fb1643b4 100644 --- a/liblightdm-qt/user.cpp +++ b/liblightdm-qt/user.cpp @@ -35,7 +35,8 @@ UsersModel *QLightDM::users() return user_model; } -class UserPrivate : public QSharedData + +class UserItem { public: QString name; @@ -43,91 +44,22 @@ public: QString homeDirectory; QString image; bool isLoggedIn; + QString displayName() const; }; -User::User(): - d(new UserPrivate) -{ -} - -User::User(const QString& name, const QString& realName, const QString& homeDirectory, const QString& image, bool isLoggedIn) : - d(new UserPrivate) -{ - d->name = name; - d->realName = realName; - d->homeDirectory = homeDirectory; - d->image = image; - d->isLoggedIn = isLoggedIn; -} - -User::User(const User &other) - : d(other.d) -{ -} - -User::~User() -{ -} - - -User& User::operator=(const User& other) -{ - d = other.d; - return *this; -} - -bool User::update(const QString& realName, const QString& homeDirectory, const QString& image, bool isLoggedIn) -{ - if (d->realName == realName && d->homeDirectory == homeDirectory && d->image == image && d->isLoggedIn == isLoggedIn) { - return false; - } - - d->realName = realName; - d->homeDirectory = homeDirectory; - d->image = image; - d->isLoggedIn = isLoggedIn; - - return true; -} - -QString User::displayName() const -{ - if (!d->realName.isEmpty()) { - return d->realName; +QString UserItem::displayName() const { + if (realName.isEmpty()){ + return name; } else { - return d->name; + return realName; } } -QString User::name() const -{ - return d->name; -} - -QString User::realName() const -{ - return d->realName; -} - -QString User::homeDirectory() const -{ - return d->homeDirectory; -} - -QString User::image() const -{ - return d->image; -} - -bool User::isLoggedIn() const -{ - return d->isLoggedIn; -} class UsersModelPrivate { public: - QList<User> users; + QList<UserItem> users; }; UsersModel::UsersModel(QObject *parent) : @@ -164,22 +96,23 @@ QVariant UsersModel::data(const QModelIndex &index, int role) const case Qt::DisplayRole: return d->users[row].displayName(); case Qt::DecorationRole: - return QPixmap(d->users[row].image()); + return QPixmap(d->users[row].image); case UsersModel::NameRole: - return d->users[row].name(); + return d->users[row].name; case UsersModel::RealNameRole: - return d->users[row].realName(); + return d->users[row].realName; case UsersModel::LoggedInRole: - return d->users[row].isLoggedIn(); + return d->users[row].isLoggedIn; } return QVariant(); } -QList<User> UsersModel::getUsers() +QList<UserItem> UsersModel::getUsers() const { - QString file = "/etc/lightdm/users.conf"; + QString file = "/etc/lightdm/users.conf"; //FIXME hardcoded path!! + qDebug() << "Loading user configuration from " << file; QSettings settings(file, QSettings::IniFormat); @@ -198,7 +131,7 @@ QList<User> UsersModel::getUsers() else { hiddenUsers = QStringList() << "nobody" << "nobody4" << "noaccess"; } - QList<User> users; + QList<UserItem> users; setpwent(); Q_FOREVER { @@ -226,18 +159,25 @@ QList<User> UsersModel::getUsers() QStringList tokens = QString(entry->pw_gecos).split(","); QString realName; - if(tokens.size() > 0 && tokens.at(0) != "") + if(tokens.size() > 0 && tokens.at(0) != "") { realName = tokens.at(0); + } QDir homeDir(entry->pw_dir); QString image = homeDir.filePath(".face"); if(!QFile::exists (image)) { image = homeDir.filePath(".face.icon"); - if(!QFile::exists (image)) + if(!QFile::exists (image)) { image = ""; + } } - User user(entry->pw_name, realName, entry->pw_dir, image, false); + UserItem user; + user.name = entry->pw_name; + user.realName = realName; + user.homeDirectory = entry->pw_dir; + user.image = image; + user.isLoggedIn = false; users.append(user); } @@ -252,19 +192,19 @@ QList<User> UsersModel::getUsers() void UsersModel::loadUsers() { - QList<User> usersToAdd; + QList<UserItem> usersToAdd; //might get rid of "User" object, keep as private object (like sessionsmodel) - or make it copyable. //loop through all the new list of users, if it's in the list already update it (or do nothing) otherwise append to list of new users - QList<User> newUserList = getUsers(); + QList<UserItem> newUserList = getUsers(); - foreach(const User &user, newUserList) { + Q_FOREACH(const UserItem &user, newUserList) { bool alreadyInList = false; for(int i=0; i < d->users.size(); i++) { - if (user.name() == d->users[i].name()) { + if (user.name == d->users[i].name) { alreadyInList = true; - d->users[i].update(user.name(), user.homeDirectory(), user.image(), user.isLoggedIn()); +// d->users[i].update(user.name(), user.homeDirectory(), user.image(), user.isLoggedIn()); QModelIndex index = createIndex(i,0); dataChanged(index, index); } @@ -280,8 +220,8 @@ void UsersModel::loadUsers() //FIXME this isn't perfect, looping like this in a mutating list - use mutable iterator. for (int i=0; i < d->users.size() ; i++) { bool found = false; - foreach(const User &user, newUserList) { - if (d->users[i].name() == user.name()) { + foreach(const UserItem &user, newUserList) { + if (d->users[i].name == user.name) { found = true; } } |