summaryrefslogtreecommitdiff
path: root/liblightdm-qt/usersmodel.cpp
blob: 9440286793424d3b1cb5da2e73984a475323f472 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*
 * Copyright (C) 2010-2011 David Edmundson.
 * Author: David Edmundson <kde@davidedmundson.co.uk>
 *
 * 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 <QtCore/QString>
#include <QtCore/QDebug>
#include <QtGui/QPixmap>

#include <lightdm.h>

using namespace QLightDM;

class UserItem
{
public:
    QString name;
    QString realName;
    QString homeDirectory;
    QString image;
    QString background;
    bool isLoggedIn;
    QString displayName() const;
};

QString UserItem::displayName() const {
    if (realName.isEmpty()){
        return name;
    }
    else {
        return realName;
    }
}

namespace QLightDM {
class UsersModelPrivate {
public:
    UsersModelPrivate(UsersModel *parent);
    virtual ~UsersModelPrivate();
    QList<UserItem> users;

    protected:
        UsersModel * const q_ptr;

        void loadUsers();

        static void cb_userAdded(LightDMUserList *user_list, LightDMUser *user, gpointer data);
        static void cb_userChanged(LightDMUserList *user_list, LightDMUser *user, gpointer data);
        static void cb_userRemoved(LightDMUserList *user_list, LightDMUser *user, gpointer data);
    private:
        Q_DECLARE_PUBLIC(UsersModel)
};
}

UsersModelPrivate::UsersModelPrivate(UsersModel* parent) :
    q_ptr(parent)
{
    g_type_init();
}

UsersModelPrivate::~UsersModelPrivate()
{
    g_signal_handlers_disconnect_by_func(lightdm_user_list_get_instance(), NULL, this);
}

void UsersModelPrivate::loadUsers()
{
    Q_Q(UsersModel);

    int rowCount = lightdm_user_list_get_length(lightdm_user_list_get_instance());

    if (rowCount == 0) {
        return;
    } else {
        q->beginInsertRows(QModelIndex(), 0, rowCount-1);

        const GList *items, *item;
        items = lightdm_user_list_get_users(lightdm_user_list_get_instance());
        for (item = items; item; item = item->next) {
            LightDMUser *ldmUser = static_cast<LightDMUser*>(item->data);

            UserItem user;
            user.name = QString::fromLocal8Bit(lightdm_user_get_name(ldmUser));
            user.homeDirectory = QString::fromLocal8Bit(lightdm_user_get_home_directory(ldmUser));
            user.realName = QString::fromLocal8Bit(lightdm_user_get_real_name(ldmUser));
            user.image = QString::fromLocal8Bit(lightdm_user_get_image(ldmUser));
            user.background = QString::fromLocal8Bit(lightdm_user_get_background(ldmUser));
            user.isLoggedIn = lightdm_user_get_logged_in(ldmUser);
            users.append(user);
        }

        q->endInsertRows();
    }
    g_signal_connect(lightdm_user_list_get_instance(), "user-added", G_CALLBACK (cb_userAdded), this);
    g_signal_connect(lightdm_user_list_get_instance(), "user-changed", G_CALLBACK (cb_userChanged), this);
    g_signal_connect(lightdm_user_list_get_instance(), "user-removed", G_CALLBACK (cb_userRemoved), this);
}



void UsersModelPrivate::cb_userAdded(LightDMUserList *user_list, LightDMUser *ldmUser, gpointer data)
{
    Q_UNUSED(user_list)
    UsersModelPrivate *that = static_cast<UsersModelPrivate*>(data);

    that->q_func()->beginInsertRows(QModelIndex(), that->users.size(), that->users.size());

    UserItem user;
    user.name = QString::fromLocal8Bit(lightdm_user_get_name(ldmUser));
    user.homeDirectory = QString::fromLocal8Bit(lightdm_user_get_home_directory(ldmUser));
    user.realName = QString::fromLocal8Bit(lightdm_user_get_real_name(ldmUser));
    user.image = QString::fromLocal8Bit(lightdm_user_get_image(ldmUser));
    user.background = QString::fromLocal8Bit(lightdm_user_get_background(ldmUser));
    user.isLoggedIn = lightdm_user_get_logged_in(ldmUser);
    that->users.append(user);

    that->q_func()->endInsertRows();

}

void UsersModelPrivate::cb_userChanged(LightDMUserList *user_list, LightDMUser *ldmUser, gpointer data)
{
    Q_UNUSED(user_list)
    UsersModelPrivate *that = static_cast<UsersModelPrivate*>(data);

    QString userToChange = QString::fromLocal8Bit(lightdm_user_get_name(ldmUser));

    for (int i=0;i<that->users.size();i++) {
        if (that->users[i].name == userToChange) {

            that->users[i].homeDirectory = QString::fromLocal8Bit(lightdm_user_get_home_directory(ldmUser));
            that->users[i].realName = QString::fromLocal8Bit(lightdm_user_get_real_name(ldmUser));
            that->users[i].image = QString::fromLocal8Bit(lightdm_user_get_image(ldmUser));
            that->users[i].background = QString::fromLocal8Bit(lightdm_user_get_background(ldmUser));
            that->users[i].isLoggedIn = lightdm_user_get_logged_in(ldmUser);

            QModelIndex index = that->q_ptr->createIndex(i, 0);
            that->q_ptr->dataChanged(index, index);
            break;
        }
    }
}


void UsersModelPrivate::cb_userRemoved(LightDMUserList *user_list, LightDMUser *ldmUser, gpointer data)
{
    Q_UNUSED(user_list)

    UsersModelPrivate *that = static_cast<UsersModelPrivate*>(data);
    QString userToRemove = QString::fromLocal8Bit(lightdm_user_get_name(ldmUser));

    QList<UserItem>::iterator i;
    for (int i=0;i<that->users.size();i++) {
        if (that->users[i].name == userToRemove) {
            that->q_ptr->beginRemoveRows(QModelIndex(), i, i);
            that->users.removeAt(i);
            that->q_ptr->endMoveRows();
            break;
        }
    }
}

UsersModel::UsersModel(QObject *parent) :
    QAbstractListModel(parent),
    d_ptr(new UsersModelPrivate(this))
{
    Q_D(UsersModel);
    // Extend roleNames (we want to keep the "display" role)
    QHash<int, QByteArray> roles = roleNames();
    roles[NameRole] = "name";
    roles[LoggedInRole] = "loggedIn";
    setRoleNames(roles);
    d->loadUsers();

}

UsersModel::~UsersModel()
{
    delete d_ptr;
}


int UsersModel::rowCount(const QModelIndex &parent) const
{
    Q_D(const UsersModel);
    if (parent == QModelIndex()) {
        return d->users.size();
    }

    return 0;
}

QVariant UsersModel::data(const QModelIndex &index, int role) const
{
    Q_D(const UsersModel);

    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;
    case UsersModel::BackgroundRole:
        return QPixmap(d->users[row].background);
    }

    return QVariant();
}


#include "usersmodel_moc.cpp"