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 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
12 #include "QLightDM/sessionsmodel.h"
14 #include <QtCore/QVariant>
15 #include <QtCore/QDebug>
19 using namespace QLightDM;
30 class SessionsModelPrivate
33 SessionsModelPrivate(SessionsModel *parent);
34 QList<SessionItem> items;
42 Q_DECLARE_PUBLIC(SessionsModel)
46 SessionsModelPrivate::SessionsModelPrivate(SessionsModel *parent) :
53 void SessionsModelPrivate::loadSessions()
55 qDebug() << "loading sessions";
57 GList *ldmSessions = lightdm_get_sessions();
58 for (GList* item = ldmSessions; item; item = item->next) {
59 LightDMSession *ldmSession = static_cast<LightDMSession*>(item->data);
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));
67 qDebug() << "adding session" << session.key;
69 items.append(session);
72 //this happens in the constructor so we don't need beginInsertRows() etc.
76 SessionsModel::SessionsModel(QObject *parent) :
77 QAbstractListModel(parent),
78 d_ptr(new SessionsModelPrivate(this))
80 QHash<int, QByteArray> roles = roleNames();
81 roles[KeyRole] = "key";
85 SessionsModel::~SessionsModel()
90 int SessionsModel::rowCount(const QModelIndex &parent) const
92 Q_D(const SessionsModel);
94 if (parent == QModelIndex()) { //if top level
95 return d->items.size();
97 return 0; // no child elements.
101 QVariant SessionsModel::data(const QModelIndex &index, int role) const
103 Q_D(const SessionsModel);
105 if (! index.isValid()) {
109 int row = index.row();
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;
123 #include "sessionsmodel_moc.cpp"