]> rtime.felk.cvut.cz Git - sojka/lightdm.git/blob - liblightdm-qt/sessionsmodel.cpp
merge from 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 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();
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     loadSessions();
51 }
52
53 void SessionsModelPrivate::loadSessions()
54 {
55     qDebug() << "loading sessions";
56
57    GList *ldmSessions = lightdm_get_sessions();
58    for (GList* item = ldmSessions; item; item = item->next) {
59        LightDMSession *ldmSession = static_cast<LightDMSession*>(item->data);
60        Q_ASSERT(ldmSession);
61
62        SessionItem session;
63        session.key = QString::fromUtf8(lightdm_session_get_key(ldmSession));
64        session.name = QString::fromUtf8(lightdm_session_get_name(ldmSession));
65        session.comment = QString::fromUtf8(lightdm_session_get_comment(ldmSession));
66
67        qDebug() << "adding session" << session.key;
68
69        items.append(session);
70    }
71
72    //this happens in the constructor so we don't need beginInsertRows() etc.
73 }
74
75
76 SessionsModel::SessionsModel(QObject *parent) :
77     QAbstractListModel(parent),
78     d_ptr(new SessionsModelPrivate(this))
79 {
80     QHash<int, QByteArray> roles = roleNames();
81     roles[KeyRole] = "key";
82     setRoleNames(roles);
83 }
84
85 SessionsModel::~SessionsModel()
86 {
87     delete d_ptr;
88 }
89
90 int SessionsModel::rowCount(const QModelIndex &parent) const
91 {
92     Q_D(const SessionsModel);
93     
94     if (parent == QModelIndex()) { //if top level
95         return d->items.size();
96     } else {
97         return 0; // no child elements.
98     }
99 }
100
101 QVariant SessionsModel::data(const QModelIndex &index, int role) const
102 {
103     Q_D(const SessionsModel);
104
105     if (! index.isValid()) {
106         return QVariant();
107     }
108
109     int row = index.row();
110
111     switch (role) {
112     case SessionsModel::KeyRole:
113         return d->items[row].key;
114     case Qt::DisplayRole:
115         return d->items[row].name;
116     case Qt::ToolTipRole:
117         return d->items[row].comment;
118
119     }
120     return QVariant();
121 }
122
123 #include "sessionsmodel_moc.cpp"