2 * Copyright (C) 2010-2011 David Edmundson.
3 * Author: David Edmundson <kde@davidedmundson.co.uk>
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.
11 #include "QLightDM/sessionsmodel.h"
13 #include <QtCore/QVariant>
14 #include <QtCore/QDebug>
18 using namespace QLightDM;
29 class SessionsModelPrivate
32 SessionsModelPrivate(SessionsModel *parent);
33 QList<SessionItem> items;
35 void loadSessions(SessionsModel::SessionType sessionType);
41 Q_DECLARE_PUBLIC(SessionsModel)
45 SessionsModelPrivate::SessionsModelPrivate(SessionsModel *parent) :
48 #if !defined(GLIB_VERSION_2_36)
53 void SessionsModelPrivate::loadSessions(SessionsModel::SessionType sessionType)
57 switch (sessionType) {
58 case SessionsModel::RemoteSessions:
59 ldmSessions = lightdm_get_remote_sessions();
61 case SessionsModel::LocalSessions:
64 ldmSessions = lightdm_get_sessions();
68 for (GList* item = ldmSessions; item; item = item->next) {
69 LightDMSession *ldmSession = static_cast<LightDMSession*>(item->data);
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));
77 items.append(session);
80 //this happens in the constructor so we don't need beginInsertRows() etc.
84 //deprecated constructor for ABI compatability.
85 SessionsModel::SessionsModel(QObject *parent) :
86 QAbstractListModel(parent),
87 d_ptr(new SessionsModelPrivate(this))
91 QHash<int, QByteArray> roles = roleNames();
92 roles[KeyRole] = "key";
95 d->loadSessions(SessionsModel::LocalSessions);
98 SessionsModel::SessionsModel(SessionsModel::SessionType sessionType, QObject *parent) :
99 QAbstractListModel(parent),
100 d_ptr(new SessionsModelPrivate(this))
104 QHash<int, QByteArray> roles = roleNames();
105 roles[KeyRole] = "key";
108 d->loadSessions(sessionType);
111 SessionsModel::~SessionsModel()
116 int SessionsModel::rowCount(const QModelIndex &parent) const
118 Q_D(const SessionsModel);
120 if (parent == QModelIndex()) { //if top level
121 return d->items.size();
123 return 0; // no child elements.
127 QVariant SessionsModel::data(const QModelIndex &index, int role) const
129 Q_D(const SessionsModel);
131 if (! index.isValid()) {
135 int row = index.row();
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;
149 #if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
150 #include "sessionsmodel_moc5.cpp"
152 #include "sessionsmodel_moc4.cpp"