From 917c8fe94754c9de57689f28176b2b12fe1a419b Mon Sep 17 00:00:00 2001 From: David Edmundson Date: Wed, 16 Nov 2011 00:35:26 +0000 Subject: User -> UsersModel --- liblightdm-qt/Makefile.am | 10 +- liblightdm-qt/QLightDM/User | 1 - liblightdm-qt/QLightDM/UsersModel | 1 + liblightdm-qt/QLightDM/greeter.h | 1 - liblightdm-qt/QLightDM/user.h | 50 -------- liblightdm-qt/QLightDM/usersmodel.h | 49 ++++++++ liblightdm-qt/user.cpp | 233 ------------------------------------ liblightdm-qt/usersmodel.cpp | 233 ++++++++++++++++++++++++++++++++++++ 8 files changed, 288 insertions(+), 290 deletions(-) delete mode 100644 liblightdm-qt/QLightDM/User create mode 100644 liblightdm-qt/QLightDM/UsersModel delete mode 100644 liblightdm-qt/QLightDM/user.h create mode 100644 liblightdm-qt/QLightDM/usersmodel.h delete mode 100644 liblightdm-qt/user.cpp create mode 100644 liblightdm-qt/usersmodel.cpp (limited to 'liblightdm-qt') diff --git a/liblightdm-qt/Makefile.am b/liblightdm-qt/Makefile.am index 11369c3f..4c1df547 100644 --- a/liblightdm-qt/Makefile.am +++ b/liblightdm-qt/Makefile.am @@ -6,8 +6,8 @@ greeter.cpp: QLightDM/greeter.h $(MOC) $< -o greeter_moc.cpp session.cpp: QLightDM/session.h $(MOC) $< -o session_moc.cpp -user.cpp: QLightDM/user.h - $(MOC) $< -o user_moc.cpp +usersmodel.cpp: QLightDM/usersmodel.h + $(MOC) $< -o usermodel_moc.cpp @@ -20,12 +20,12 @@ liblightdm_qt_1include_HEADERS = \ QLightDM/Power \ QLightDM/Session \ QLightDM/System \ - QLightDM/User \ + QLightDM/UsersModel \ QLightDM/greeter.h \ QLightDM/power.h \ QLightDM/session.h \ QLightDM/system.h \ - QLightDM/user.h + QLightDM/usersmodel.h liblightdm_qt_1includedir=$(includedir)/lightdm-qt-1/QLightDM @@ -34,7 +34,7 @@ liblightdm_qt_1_la_SOURCES = \ power.cpp \ session.cpp \ system.cpp \ - user.cpp \ + usersmodel.cpp \ $(liblightdm_qt_1include_HEADERS) pkgconfigdir = $(libdir)/pkgconfig diff --git a/liblightdm-qt/QLightDM/User b/liblightdm-qt/QLightDM/User deleted file mode 100644 index 968058f5..00000000 --- a/liblightdm-qt/QLightDM/User +++ /dev/null @@ -1 +0,0 @@ -#include "QLightDM/user.h" \ No newline at end of file diff --git a/liblightdm-qt/QLightDM/UsersModel b/liblightdm-qt/QLightDM/UsersModel new file mode 100644 index 00000000..95f2cf3f --- /dev/null +++ b/liblightdm-qt/QLightDM/UsersModel @@ -0,0 +1 @@ +#include "QLightDM/usersmodel.h" \ No newline at end of file diff --git a/liblightdm-qt/QLightDM/greeter.h b/liblightdm-qt/QLightDM/greeter.h index 8d1b4006..c0b89b41 100644 --- a/liblightdm-qt/QLightDM/greeter.h +++ b/liblightdm-qt/QLightDM/greeter.h @@ -15,7 +15,6 @@ #include #include -#include "QLightDM/User" class GreeterPrivate; diff --git a/liblightdm-qt/QLightDM/user.h b/liblightdm-qt/QLightDM/user.h deleted file mode 100644 index ea33a2c3..00000000 --- a/liblightdm-qt/QLightDM/user.h +++ /dev/null @@ -1,50 +0,0 @@ -/* - * Copyright (C) 2010-2011 David Edmundson. - * Author: David Edmundson - * - * This library is free software; you can redistribute it and/or modify it under - * the terms of the GNU Lesser General Public License as published by the Free - * Software Foundation; either version 3 of the License, or (at your option) any - * later version. See http://www.gnu.org/copyleft/lgpl.html the full text of the - * license. - */ - -#ifndef QLIGHTDM_USER_H -#define QLIGHTDM_USER_H - -#include -#include -#include - -class UserPrivate; -class UsersModelPrivate; -class UserItem; - -namespace QLightDM -{ -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; - -private slots: - /** Updates the model with new changes in the password file*/ - void loadUsers(); - -private: - UsersModelPrivate *d; - QList getUsers() const; -}; - -}; - -#endif // QLIGHTDM_USER_H diff --git a/liblightdm-qt/QLightDM/usersmodel.h b/liblightdm-qt/QLightDM/usersmodel.h new file mode 100644 index 00000000..498e0bce --- /dev/null +++ b/liblightdm-qt/QLightDM/usersmodel.h @@ -0,0 +1,49 @@ +/* + * Copyright (C) 2010-2011 David Edmundson. + * Author: David Edmundson + * + * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 3 of the License, or (at your option) any + * later version. See http://www.gnu.org/copyleft/lgpl.html the full text of the + * license. + */ + +#ifndef QLIGHTDM_USER_H +#define QLIGHTDM_USER_H + +#include +#include +#include + +class UsersModelPrivate; +class UserItem; + +namespace QLightDM +{ +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; + +private slots: + /** Updates the model with new changes in the password file*/ + void loadUsers(); + +private: + UsersModelPrivate *d; + QList getUsers() const; +}; + +}; + +#endif // QLIGHTDM_USER_H diff --git a/liblightdm-qt/user.cpp b/liblightdm-qt/user.cpp deleted file mode 100644 index 0bb407df..00000000 --- a/liblightdm-qt/user.cpp +++ /dev/null @@ -1,233 +0,0 @@ -/* - * Copyright (C) 2010-2011 David Edmundson. - * Author: David Edmundson - * - * This library is free software; you can redistribute it and/or modify it under - * the terms of the GNU Lesser General Public License as published by the Free - * Software Foundation; either version 3 of the License, or (at your option) any - * later version. See http://www.gnu.org/copyleft/lgpl.html the full text of the - * license. - */ - -#include "QLightDM/user.h" - -#include -#include - -#include -#include -#include -#include -#include -#include -#include -#include - -using namespace QLightDM; - -class UserItem -{ -public: - QString name; - QString realName; - QString homeDirectory; - QString image; - bool isLoggedIn; - QString displayName() const; -}; - -QString UserItem::displayName() const { - if (realName.isEmpty()){ - return name; - } - else { - return realName; - } -} - - -class UsersModelPrivate { -public: - QList users; -}; - -UsersModel::UsersModel(QObject *parent) : - QAbstractListModel(parent), - d (new UsersModelPrivate()) -{ - //load users on startup and if the password file changes. - QFileSystemWatcher *watcher = new QFileSystemWatcher(this); - watcher->addPath("/etc/passwd"); //FIXME harcoded path - connect(watcher, SIGNAL(fileChanged(QString)), SLOT(loadUsers())); - - loadUsers(); -} - -UsersModel::~UsersModel() -{ - delete d; -} - - -int UsersModel::rowCount(const QModelIndex &parent) const -{ - return d->users.count(); -} - -QVariant UsersModel::data(const QModelIndex &index, int role) const -{ - if (!index.isValid()) { - return QVariant(); - } - - int row = index.row(); - switch (role) { - case Qt::DisplayRole: - return d->users[row].displayName(); - case Qt::DecorationRole: - return QPixmap(d->users[row].image); - case UsersModel::NameRole: - return d->users[row].name; - case UsersModel::RealNameRole: - return d->users[row].realName; - case UsersModel::LoggedInRole: - return d->users[row].isLoggedIn; - } - - return QVariant(); -} - - -QList UsersModel::getUsers() const -{ - QString file = "/etc/lightdm/users.conf"; //FIXME hardcoded path!! - - qDebug() << "Loading user configuration from " << file; - QSettings settings(file, QSettings::IniFormat); - - int minimumUid = settings.value("UserAccounts/minimum-uid", QVariant(500)).toInt(); - QStringList hiddenShells; - if (settings.contains("UserAccounts/hidden-shells")) { - hiddenShells = settings.value("UserAccounts/hidden-shells").toString().split(" "); - } - else { - hiddenShells = QStringList() << "/bin/false" << "/usr/sbin/nologin"; - } - QStringList hiddenUsers; - if (settings.contains("UserAccounts/hidden-users")) { - hiddenUsers = settings.value("UserAccounts/hidden-users").toString().split(" "); - } - else { - hiddenUsers = QStringList() << "nobody" << "nobody4" << "noaccess"; - } - QList users; - - setpwent(); - Q_FOREVER { - errno = 0; - struct passwd *entry = getpwent(); - if(!entry) { - break; - } - - /* Ignore system users */ - if(entry->pw_uid < minimumUid) { - continue; - } - - /* Ignore users disabled by shell */ - if(entry->pw_shell) { - if (hiddenShells.contains(entry->pw_shell)) { - continue; - } - } - - if (hiddenUsers.contains(entry->pw_name)) { - continue; - } - - QStringList tokens = QString(entry->pw_gecos).split(","); - QString realName; - 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)) { - image = ""; - } - } - - UserItem user; - user.name = entry->pw_name; - user.realName = realName; - user.homeDirectory = entry->pw_dir; - user.image = image; - user.isLoggedIn = false; - users.append(user); - } - - if(errno != 0) { - qDebug() << "Failed to read password database: " << strerror(errno); - } - endpwent(); - - - return users; -} - -void UsersModel::loadUsers() -{ - QList 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 newUserList = getUsers(); - - 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) { - alreadyInList = true; -// d->users[i].update(user.name(), user.homeDirectory(), user.image(), user.isLoggedIn()); - QModelIndex index = createIndex(i,0); - dataChanged(index, index); - } - } - - if (!alreadyInList) { - usersToAdd.append(user); - } - } - - //loop through all the existing users, if they're not in the newly gathered list, remove them. - - //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 UserItem &user, newUserList) { - if (d->users[i].name == user.name) { - found = true; - } - } - - if (!found) { - beginRemoveRows(QModelIndex(), i, i); - d->users.removeAt(i); - endRemoveRows(); - } - } - - //append new users - if (usersToAdd.size() > 0) { - beginInsertRows(QModelIndex(), d->users.size(), d->users.size() + usersToAdd.size() -1); - d->users.append(usersToAdd); - endInsertRows(); - } -} - -#include "user_moc.cpp" diff --git a/liblightdm-qt/usersmodel.cpp b/liblightdm-qt/usersmodel.cpp new file mode 100644 index 00000000..3fcc73db --- /dev/null +++ b/liblightdm-qt/usersmodel.cpp @@ -0,0 +1,233 @@ +/* + * Copyright (C) 2010-2011 David Edmundson. + * Author: David Edmundson + * + * This library is free software; you can redistribute it and/or modify it under + * the terms of the GNU Lesser General Public License as published by the Free + * Software Foundation; either version 3 of the License, or (at your option) any + * later version. See http://www.gnu.org/copyleft/lgpl.html the full text of the + * license. + */ + +#include "QLightDM/usersmodel.h" + +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace QLightDM; + +class UserItem +{ +public: + QString name; + QString realName; + QString homeDirectory; + QString image; + bool isLoggedIn; + QString displayName() const; +}; + +QString UserItem::displayName() const { + if (realName.isEmpty()){ + return name; + } + else { + return realName; + } +} + + +class UsersModelPrivate { +public: + QList users; +}; + +UsersModel::UsersModel(QObject *parent) : + QAbstractListModel(parent), + d (new UsersModelPrivate()) +{ + //load users on startup and if the password file changes. + QFileSystemWatcher *watcher = new QFileSystemWatcher(this); + watcher->addPath("/etc/passwd"); //FIXME harcoded path + connect(watcher, SIGNAL(fileChanged(QString)), SLOT(loadUsers())); + + loadUsers(); +} + +UsersModel::~UsersModel() +{ + delete d; +} + + +int UsersModel::rowCount(const QModelIndex &parent) const +{ + return d->users.count(); +} + +QVariant UsersModel::data(const QModelIndex &index, int role) const +{ + if (!index.isValid()) { + return QVariant(); + } + + int row = index.row(); + switch (role) { + case Qt::DisplayRole: + return d->users[row].displayName(); + case Qt::DecorationRole: + return QPixmap(d->users[row].image); + case UsersModel::NameRole: + return d->users[row].name; + case UsersModel::RealNameRole: + return d->users[row].realName; + case UsersModel::LoggedInRole: + return d->users[row].isLoggedIn; + } + + return QVariant(); +} + + +QList UsersModel::getUsers() const +{ + QString file = "/etc/lightdm/users.conf"; //FIXME hardcoded path!! + + qDebug() << "Loading user configuration from " << file; + QSettings settings(file, QSettings::IniFormat); + + int minimumUid = settings.value("UserAccounts/minimum-uid", QVariant(500)).toInt(); + QStringList hiddenShells; + if (settings.contains("UserAccounts/hidden-shells")) { + hiddenShells = settings.value("UserAccounts/hidden-shells").toString().split(" "); + } + else { + hiddenShells = QStringList() << "/bin/false" << "/usr/sbin/nologin"; + } + QStringList hiddenUsers; + if (settings.contains("UserAccounts/hidden-users")) { + hiddenUsers = settings.value("UserAccounts/hidden-users").toString().split(" "); + } + else { + hiddenUsers = QStringList() << "nobody" << "nobody4" << "noaccess"; + } + QList users; + + setpwent(); + Q_FOREVER { + errno = 0; + struct passwd *entry = getpwent(); + if(!entry) { + break; + } + + /* Ignore system users */ + if(entry->pw_uid < minimumUid) { + continue; + } + + /* Ignore users disabled by shell */ + if(entry->pw_shell) { + if (hiddenShells.contains(entry->pw_shell)) { + continue; + } + } + + if (hiddenUsers.contains(entry->pw_name)) { + continue; + } + + QStringList tokens = QString(entry->pw_gecos).split(","); + QString realName; + 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)) { + image = ""; + } + } + + UserItem user; + user.name = entry->pw_name; + user.realName = realName; + user.homeDirectory = entry->pw_dir; + user.image = image; + user.isLoggedIn = false; + users.append(user); + } + + if(errno != 0) { + qDebug() << "Failed to read password database: " << strerror(errno); + } + endpwent(); + + + return users; +} + +void UsersModel::loadUsers() +{ + QList 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 newUserList = getUsers(); + + 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) { + alreadyInList = true; +// d->users[i].update(user.name(), user.homeDirectory(), user.image(), user.isLoggedIn()); + QModelIndex index = createIndex(i,0); + dataChanged(index, index); + } + } + + if (!alreadyInList) { + usersToAdd.append(user); + } + } + + //loop through all the existing users, if they're not in the newly gathered list, remove them. + + //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 UserItem &user, newUserList) { + if (d->users[i].name == user.name) { + found = true; + } + } + + if (!found) { + beginRemoveRows(QModelIndex(), i, i); + d->users.removeAt(i); + endRemoveRows(); + } + } + + //append new users + if (usersToAdd.size() > 0) { + beginInsertRows(QModelIndex(), d->users.size(), d->users.size() + usersToAdd.size() -1); + d->users.append(usersToAdd); + endInsertRows(); + } +} + +#include "usersmodel_moc.cpp" -- cgit v1.2.1