]> rtime.felk.cvut.cz Git - sojka/lightdm.git/blob - liblightdm-qt/sessionsmodel.cpp
Changes to QLightDM
[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 3 of the License, or (at your option) any
8  * later version. See http://www.gnu.org/copyleft/lgpl.html the full text of the
9  * license.
10  */
11
12 #include "QLightDM/sessionsmodel.h"
13
14 #include <QtCore/QVariant>
15 #include <QtCore/QDebug>
16
17 #include <lightdm.h>
18
19 using namespace QLightDM;
20
21 class SessionItem
22 {
23 public:
24     QString key;
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     g_type_init();
50 }
51
52 void SessionsModelPrivate::loadSessions(SessionsModel::SessionType sessionType)
53 {
54     GList *ldmSessions;
55
56     switch (sessionType) {
57     case SessionsModel::RemoteSessions:
58         ldmSessions = lightdm_get_remote_sessions();
59         break;
60     case SessionsModel::LocalSessions:
61         /* Fall through*/
62     default:
63         ldmSessions = lightdm_get_sessions();
64         break;
65     }
66
67     for (GList* item = ldmSessions; item; item = item->next) {
68        LightDMSession *ldmSession = static_cast<LightDMSession*>(item->data);
69        Q_ASSERT(ldmSession);
70
71        SessionItem session;
72        session.key = QString::fromUtf8(lightdm_session_get_key(ldmSession));
73        session.name = QString::fromUtf8(lightdm_session_get_name(ldmSession));
74        session.comment = QString::fromUtf8(lightdm_session_get_comment(ldmSession));
75
76        items.append(session);
77    }
78
79    //this happens in the constructor so we don't need beginInsertRows() etc.
80 }
81
82
83 //deprecated constructor for ABI compatability.
84 SessionsModel::SessionsModel(QObject *parent) :
85     QAbstractListModel(parent),
86     d_ptr(new SessionsModelPrivate(this))
87 {
88     Q_D(SessionsModel);
89
90     QHash<int, QByteArray> roles = roleNames();
91     roles[KeyRole] = "key";
92     setRoleNames(roles);
93
94     d->loadSessions(SessionsModel::LocalSessions);
95 }
96
97 SessionsModel::SessionsModel(SessionsModel::SessionType sessionType, QObject *parent) :
98     QAbstractListModel(parent),
99     d_ptr(new SessionsModelPrivate(this))
100 {
101     Q_D(SessionsModel);
102
103     QHash<int, QByteArray> roles = roleNames();
104     roles[KeyRole] = "key";
105     setRoleNames(roles);
106
107     d->loadSessions(sessionType);
108 }
109
110 SessionsModel::~SessionsModel()
111 {
112     delete d_ptr;
113 }
114
115 int SessionsModel::rowCount(const QModelIndex &parent) const
116 {
117     Q_D(const SessionsModel);
118
119     if (parent == QModelIndex()) { //if top level
120         return d->items.size();
121     } else {
122         return 0; // no child elements.
123     }
124 }
125
126 QVariant SessionsModel::data(const QModelIndex &index, int role) const
127 {
128     Q_D(const SessionsModel);
129
130     if (! index.isValid()) {
131         return QVariant();
132     }
133
134     int row = index.row();
135
136     switch (role) {
137     case SessionsModel::KeyRole:
138         return d->items[row].key;
139     case Qt::DisplayRole:
140         return d->items[row].name;
141     case Qt::ToolTipRole:
142         return d->items[row].comment;
143
144     }
145     return QVariant();
146 }
147
148 #include "sessionsmodel_moc.cpp"