]> rtime.felk.cvut.cz Git - sojka/lightdm.git/blob - liblightdm-qt/sessionsmodel.cpp
Merge with trunk
[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::LocalSessions:
62         /* Fall through*/
63     default:
64         ldmSessions = lightdm_get_sessions();
65         break;
66     }
67
68     for (GList* item = ldmSessions; item; item = item->next) {
69        LightDMSession *ldmSession = static_cast<LightDMSession*>(item->data);
70        Q_ASSERT(ldmSession);
71
72        SessionItem session;
73        session.key = QString::fromUtf8(lightdm_session_get_key(ldmSession));
74        session.name = QString::fromUtf8(lightdm_session_get_name(ldmSession));
75        session.comment = QString::fromUtf8(lightdm_session_get_comment(ldmSession));
76
77        items.append(session);
78    }
79
80    //this happens in the constructor so we don't need beginInsertRows() etc.
81 }
82
83
84 //deprecated constructor for ABI compatability.
85 SessionsModel::SessionsModel(QObject *parent) :
86     QAbstractListModel(parent),
87     d_ptr(new SessionsModelPrivate(this))
88 {
89     Q_D(SessionsModel);
90
91     QHash<int, QByteArray> roles = roleNames();
92     roles[KeyRole] = "key";
93     setRoleNames(roles);
94
95     d->loadSessions(SessionsModel::LocalSessions);
96 }
97
98 SessionsModel::SessionsModel(SessionsModel::SessionType sessionType, QObject *parent) :
99     QAbstractListModel(parent),
100     d_ptr(new SessionsModelPrivate(this))
101 {
102     Q_D(SessionsModel);
103
104     QHash<int, QByteArray> roles = roleNames();
105     roles[KeyRole] = "key";
106     setRoleNames(roles);
107
108     d->loadSessions(sessionType);
109 }
110
111 SessionsModel::~SessionsModel()
112 {
113     delete d_ptr;
114 }
115
116 int SessionsModel::rowCount(const QModelIndex &parent) const
117 {
118     Q_D(const SessionsModel);
119
120     if (parent == QModelIndex()) { //if top level
121         return d->items.size();
122     } else {
123         return 0; // no child elements.
124     }
125 }
126
127 QVariant SessionsModel::data(const QModelIndex &index, int role) const
128 {
129     Q_D(const SessionsModel);
130
131     if (! index.isValid()) {
132         return QVariant();
133     }
134
135     int row = index.row();
136
137     switch (role) {
138     case SessionsModel::KeyRole:
139         return d->items[row].key;
140     case Qt::DisplayRole:
141         return d->items[row].name;
142     case Qt::ToolTipRole:
143         return d->items[row].comment;
144
145     }
146     return QVariant();
147 }
148
149 #if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
150 #include "sessionsmodel_moc5.cpp"
151 #else
152 #include "sessionsmodel_moc4.cpp"
153 #endif