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;
30 class SessionsModelPrivate
33 SessionsModelPrivate(SessionsModel *parent);
34 QList<SessionItem> items;
36 void loadSessions(SessionsModel::SessionType sessionType);
42 Q_DECLARE_PUBLIC(SessionsModel)
46 SessionsModelPrivate::SessionsModelPrivate(SessionsModel *parent) :
49 #if !defined(GLIB_VERSION_2_36)
54 void SessionsModelPrivate::loadSessions(SessionsModel::SessionType sessionType)
58 switch (sessionType) {
59 case SessionsModel::RemoteSessions:
60 ldmSessions = lightdm_get_remote_sessions();
62 case SessionsModel::LocalSessions:
65 ldmSessions = lightdm_get_sessions();
69 for (GList* item = ldmSessions; item; item = item->next) {
70 LightDMSession *ldmSession = static_cast<LightDMSession*>(item->data);
74 session.key = QString::fromUtf8(lightdm_session_get_key(ldmSession));
75 session.type = QString::fromUtf8(lightdm_session_get_session_type(ldmSession));
76 session.name = QString::fromUtf8(lightdm_session_get_name(ldmSession));
77 session.comment = QString::fromUtf8(lightdm_session_get_comment(ldmSession));
79 items.append(session);
82 //this happens in the constructor so we don't need beginInsertRows() etc.
86 //deprecated constructor for ABI compatability.
87 SessionsModel::SessionsModel(QObject *parent) :
88 QAbstractListModel(parent),
89 d_ptr(new SessionsModelPrivate(this))
93 QHash<int, QByteArray> roles = roleNames();
94 roles[KeyRole] = "key";
97 d->loadSessions(SessionsModel::LocalSessions);
100 SessionsModel::SessionsModel(SessionsModel::SessionType sessionType, QObject *parent) :
101 QAbstractListModel(parent),
102 d_ptr(new SessionsModelPrivate(this))
106 QHash<int, QByteArray> roles = roleNames();
107 roles[KeyRole] = "key";
110 d->loadSessions(sessionType);
113 SessionsModel::~SessionsModel()
118 int SessionsModel::rowCount(const QModelIndex &parent) const
120 Q_D(const SessionsModel);
122 if (parent == QModelIndex()) { //if top level
123 return d->items.size();
125 return 0; // no child elements.
129 QVariant SessionsModel::data(const QModelIndex &index, int role) const
131 Q_D(const SessionsModel);
133 if (! index.isValid()) {
137 int row = index.row();
140 case SessionsModel::KeyRole:
141 return d->items[row].key;
142 case SessionsModel::TypeRole:
143 return d->items[row].type;
144 case Qt::DisplayRole:
145 return d->items[row].name;
146 case Qt::ToolTipRole:
147 return d->items[row].comment;
153 #if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
154 #include "sessionsmodel_moc5.cpp"
156 #include "sessionsmodel_moc4.cpp"