]> rtime.felk.cvut.cz Git - sojka/lightdm.git/blob - liblightdm-qt/sessionsmodel.cpp
Load all users only when really needed
[sojka/lightdm.git] / liblightdm-qt / sessionsmodel.cpp
1 /*
2  * Copyright (C) 2010-2011 David Edmundson.
3  * Author: David Edmundson <kde@davidedmundson.co.uk>
4  *
5  * This library is free software; you can redistribute it and/or modify it under
6  * the terms of the GNU Lesser General Public License as published by the Free
7  * Software Foundation; either version 2 or version 3 of the License.
8  * See http://www.gnu.org/copyleft/lgpl.html the full text of the license.
9  */
10
11 #include "QLightDM/sessionsmodel.h"
12
13 #include <QtCore/QVariant>
14 #include <QtCore/QDebug>
15
16 #include <lightdm.h>
17
18 using namespace QLightDM;
19
20 class SessionItem
21 {
22 public:
23     QString key;
24     QString type;
25     QString name;
26     QString comment;
27 };
28
29
30 class SessionsModelPrivate
31 {
32 public:
33     SessionsModelPrivate(SessionsModel *parent);
34     QList<SessionItem> items;
35
36     void loadSessions(SessionsModel::SessionType sessionType);
37
38 protected:
39     SessionsModel* q_ptr;
40
41 private:
42     Q_DECLARE_PUBLIC(SessionsModel)
43
44 };
45
46 SessionsModelPrivate::SessionsModelPrivate(SessionsModel *parent) :
47     q_ptr(parent)
48 {
49 #if !defined(GLIB_VERSION_2_36)
50     g_type_init();
51 #endif
52 }
53
54 void SessionsModelPrivate::loadSessions(SessionsModel::SessionType sessionType)
55 {
56     GList *ldmSessions;
57
58     switch (sessionType) {
59     case SessionsModel::RemoteSessions:
60         ldmSessions = lightdm_get_remote_sessions();
61         break;
62     case SessionsModel::LocalSessions:
63         /* Fall through*/
64     default:
65         ldmSessions = lightdm_get_sessions();
66         break;
67     }
68
69     for (GList* item = ldmSessions; item; item = item->next) {
70        LightDMSession *ldmSession = static_cast<LightDMSession*>(item->data);
71        Q_ASSERT(ldmSession);
72
73        SessionItem session;
74        session.key = QString::fromUtf8(lightdm_session_get_key(ldmSession));
75        session.type = QString::fromUtf8(lightdm_session_get_session_type(ldmSession));
76        session.name = QString::fromUtf8(lightdm_session_get_name(ldmSession));
77        session.comment = QString::fromUtf8(lightdm_session_get_comment(ldmSession));
78
79        items.append(session);
80    }
81
82    //this happens in the constructor so we don't need beginInsertRows() etc.
83 }
84
85
86 //deprecated constructor for ABI compatability.
87 SessionsModel::SessionsModel(QObject *parent) :
88     QAbstractListModel(parent),
89     d_ptr(new SessionsModelPrivate(this))
90 {
91     Q_D(SessionsModel);
92
93     QHash<int, QByteArray> roles = roleNames();
94     roles[KeyRole] = "key";
95     setRoleNames(roles);
96
97     d->loadSessions(SessionsModel::LocalSessions);
98 }
99
100 SessionsModel::SessionsModel(SessionsModel::SessionType sessionType, QObject *parent) :
101     QAbstractListModel(parent),
102     d_ptr(new SessionsModelPrivate(this))
103 {
104     Q_D(SessionsModel);
105
106     QHash<int, QByteArray> roles = roleNames();
107     roles[KeyRole] = "key";
108     setRoleNames(roles);
109
110     d->loadSessions(sessionType);
111 }
112
113 SessionsModel::~SessionsModel()
114 {
115     delete d_ptr;
116 }
117
118 int SessionsModel::rowCount(const QModelIndex &parent) const
119 {
120     Q_D(const SessionsModel);
121
122     if (parent == QModelIndex()) { //if top level
123         return d->items.size();
124     } else {
125         return 0; // no child elements.
126     }
127 }
128
129 QVariant SessionsModel::data(const QModelIndex &index, int role) const
130 {
131     Q_D(const SessionsModel);
132
133     if (! index.isValid()) {
134         return QVariant();
135     }
136
137     int row = index.row();
138
139     switch (role) {
140     case SessionsModel::KeyRole:
141         return d->items[row].key;
142     case SessionsModel::TypeRole:
143         return d->items[row].type;
144     case Qt::DisplayRole:
145         return d->items[row].name;
146     case Qt::ToolTipRole:
147         return d->items[row].comment;
148
149     }
150     return QVariant();
151 }
152
153 #if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
154 #include "sessionsmodel_moc5.cpp"
155 #else
156 #include "sessionsmodel_moc4.cpp"
157 #endif