]> rtime.felk.cvut.cz Git - sojka/lightdm.git/blob - liblightdm-qt/sessionsmodel.cpp
Start work on Mir sessions
[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 name;
25     QString comment;
26 };
27
28
29 class SessionsModelPrivate
30 {
31 public:
32     SessionsModelPrivate(SessionsModel *parent);
33     QList<SessionItem> items;
34
35     void loadSessions(SessionsModel::SessionType sessionType);
36
37 protected:
38     SessionsModel* q_ptr;
39
40 private:
41     Q_DECLARE_PUBLIC(SessionsModel)
42
43 };
44
45 SessionsModelPrivate::SessionsModelPrivate(SessionsModel *parent) :
46     q_ptr(parent)
47 {
48 #if !defined(GLIB_VERSION_2_36)
49     g_type_init();
50 #endif
51 }
52
53 void SessionsModelPrivate::loadSessions(SessionsModel::SessionType sessionType)
54 {
55     GList *ldmSessions;
56
57     switch (sessionType) {
58     case SessionsModel::RemoteSessions:
59         ldmSessions = lightdm_get_remote_sessions();
60         break;
61     case SessionsModel::MirSessions:
62         ldmSessions = lightdm_get_mir_sessions();
63         break;
64     case SessionsModel::LocalSessions:
65         /* Fall through*/
66     default:
67         ldmSessions = lightdm_get_sessions();
68         break;
69     }
70
71     for (GList* item = ldmSessions; item; item = item->next) {
72        LightDMSession *ldmSession = static_cast<LightDMSession*>(item->data);
73        Q_ASSERT(ldmSession);
74
75        SessionItem session;
76        session.key = QString::fromUtf8(lightdm_session_get_key(ldmSession));
77        session.name = QString::fromUtf8(lightdm_session_get_name(ldmSession));
78        session.comment = QString::fromUtf8(lightdm_session_get_comment(ldmSession));
79
80        items.append(session);
81    }
82
83    //this happens in the constructor so we don't need beginInsertRows() etc.
84 }
85
86
87 //deprecated constructor for ABI compatability.
88 SessionsModel::SessionsModel(QObject *parent) :
89     QAbstractListModel(parent),
90     d_ptr(new SessionsModelPrivate(this))
91 {
92     Q_D(SessionsModel);
93
94     QHash<int, QByteArray> roles = roleNames();
95     roles[KeyRole] = "key";
96     setRoleNames(roles);
97
98     d->loadSessions(SessionsModel::LocalSessions);
99 }
100
101 SessionsModel::SessionsModel(SessionsModel::SessionType sessionType, QObject *parent) :
102     QAbstractListModel(parent),
103     d_ptr(new SessionsModelPrivate(this))
104 {
105     Q_D(SessionsModel);
106
107     QHash<int, QByteArray> roles = roleNames();
108     roles[KeyRole] = "key";
109     setRoleNames(roles);
110
111     d->loadSessions(sessionType);
112 }
113
114 SessionsModel::~SessionsModel()
115 {
116     delete d_ptr;
117 }
118
119 int SessionsModel::rowCount(const QModelIndex &parent) const
120 {
121     Q_D(const SessionsModel);
122
123     if (parent == QModelIndex()) { //if top level
124         return d->items.size();
125     } else {
126         return 0; // no child elements.
127     }
128 }
129
130 QVariant SessionsModel::data(const QModelIndex &index, int role) const
131 {
132     Q_D(const SessionsModel);
133
134     if (! index.isValid()) {
135         return QVariant();
136     }
137
138     int row = index.row();
139
140     switch (role) {
141     case SessionsModel::KeyRole:
142         return d->items[row].key;
143     case Qt::DisplayRole:
144         return d->items[row].name;
145     case Qt::ToolTipRole:
146         return d->items[row].comment;
147
148     }
149     return QVariant();
150 }
151
152 #if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
153 #include "sessionsmodel_moc5.cpp"
154 #else
155 #include "sessionsmodel_moc4.cpp"
156 #endif