1
#include "usersmodel.h"
8
#include <QtCore/QString>
9
#include <QtCore/QFileSystemWatcher>
10
#include <QtGui/QPixmap>
12
using namespace QLightDM;
14
class UsersModelPrivate {
17
QLightDM::Config *config;
20
UsersModel::UsersModel(QLightDM::Config *config, QObject *parent) :
21
QAbstractListModel(parent),
22
d (new UsersModelPrivate())
26
if (d->config->loadUsers()) {
27
//load users on startup and if the password file changes.
28
QFileSystemWatcher *watcher = new QFileSystemWatcher(this);
29
watcher->addPath("/etc/passwd"); //FIXME harcoded path
30
connect(watcher, SIGNAL(fileChanged(QString)), SLOT(loadUsers()));
36
UsersModel::~UsersModel()
42
int UsersModel::rowCount(const QModelIndex &parent) const
44
return d->users.count();
47
QVariant UsersModel::data(const QModelIndex &index, int role) const
49
if (!index.isValid()) {
53
int row = index.row();
56
return d->users[row]->displayName();
57
case Qt::DecorationRole:
58
return QPixmap(d->users[row]->image());
65
void UsersModel::loadUsers()
67
QStringList hiddenUsers, hiddenShells;
69
QList<User*> newUsers;
71
minimumUid = d->config->minimumUid();
72
hiddenUsers = d->config->hiddenUsers();
73
hiddenShells = d->config->hiddenShells();
74
//FIXME accidently not got the "if contact removed" code. Need to fix.
83
QString realName, image;
92
/* Ignore system users */
93
if(entry->pw_uid < minimumUid)
96
/* Ignore users disabled by shell */
99
for(i = 0; i < hiddenShells.size(); i++)
100
if(entry->pw_shell == hiddenShells.at(i))
102
if(i < hiddenShells.size())
106
/* Ignore certain users */
107
for(i = 0; i < hiddenUsers.size(); i++)
108
if(entry->pw_name == hiddenUsers.at(i))
110
if(i < hiddenUsers.size())
113
tokens = QString(entry->pw_gecos).split(",");
114
if(tokens.size() > 0 && tokens.at(i) != "")
115
realName = tokens.at(i);
118
//replace this with QFile::exists();
119
QDir homeDir(entry->pw_dir);
120
imageFile = new QFile(homeDir.filePath(".face"));
121
if(!imageFile->exists())
124
imageFile = new QFile(homeDir.filePath(".face.icon"));
126
if(imageFile->exists()) {
127
image = "file://" + imageFile->fileName();
131
//FIXME don't create objects on the heap in the middle of a loop with breaks in it! Destined for fail.
132
//FIXME pointers all over the place in this code.
133
user = new User(entry->pw_name, realName, entry->pw_dir, image, false, this);
135
/* Update existing users if have them */
136
bool matchedUser = false;
138
for (int i=0; i < d->users.size(); i++)
140
User* info = d->users[i];
141
if(info->name() == user->name()) {
143
info->update(user->realName(), user->homeDirectory(), user->image(), user->isLoggedIn());
144
dataChanged(createIndex(i, 0), createIndex(i,0));
149
newUsers.append(user);
154
qDebug() << "Failed to read password database: " << strerror(errno);
159
//FIXME accidently not got the "if contact removed" code. Need to restore that.
160
//should call beginRemoveRows, and then remove the row from the model.
161
//might get rid of "User" object, keep as private object (like sessionsmodel) - or make it copyable.
165
if (newUsers.size() > 0) {
166
beginInsertRows(QModelIndex(), 0, newUsers.size());
167
d->users.append(newUsers);