Back to index

lightdm  1.3.2
sessionsmodel.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2010-2011 David Edmundson.
00003  * Author: David Edmundson <kde@davidedmundson.co.uk>
00004  *
00005  * This library is free software; you can redistribute it and/or modify it under
00006  * the terms of the GNU Lesser General Public License as published by the Free
00007  * Software Foundation; either version 3 of the License, or (at your option) any
00008  * later version. See http://www.gnu.org/copyleft/lgpl.html the full text of the
00009  * license.
00010  */
00011 
00012 #include "QLightDM/sessionsmodel.h"
00013 
00014 #include <QtCore/QVariant>
00015 #include <QtCore/QDebug>
00016 
00017 #include <lightdm.h>
00018 
00019 using namespace QLightDM;
00020 
00021 class SessionItem
00022 {
00023 public:
00024     QString key;
00025     QString name;
00026     QString comment;
00027 };
00028 
00029 
00030 class SessionsModelPrivate
00031 {
00032 public:
00033     SessionsModelPrivate(SessionsModel *parent);
00034     QList<SessionItem> items;
00035     
00036     void loadSessions();
00037     
00038 protected:
00039     SessionsModel* q_ptr;
00040 
00041 private:
00042     Q_DECLARE_PUBLIC(SessionsModel)
00043     
00044 };
00045 
00046 SessionsModelPrivate::SessionsModelPrivate(SessionsModel *parent) :
00047     q_ptr(parent)
00048 {
00049     g_type_init();
00050     loadSessions();
00051 }
00052 
00053 void SessionsModelPrivate::loadSessions()
00054 {
00055     qDebug() << "loading sessions";
00056 
00057    GList *ldmSessions = lightdm_get_sessions();
00058    for (GList* item = ldmSessions; item; item = item->next) {
00059        LightDMSession *ldmSession = static_cast<LightDMSession*>(item->data);
00060        Q_ASSERT(ldmSession);
00061 
00062        SessionItem session;
00063        session.key = QString::fromUtf8(lightdm_session_get_key(ldmSession));
00064        session.name = QString::fromUtf8(lightdm_session_get_name(ldmSession));
00065        session.comment = QString::fromUtf8(lightdm_session_get_comment(ldmSession));
00066 
00067        qDebug() << "adding session" << session.key;
00068 
00069        items.append(session);
00070    }
00071 
00072    //this happens in the constructor so we don't need beginInsertRows() etc.
00073 }
00074 
00075 
00076 SessionsModel::SessionsModel(QObject *parent) :
00077     QAbstractListModel(parent),
00078     d_ptr(new SessionsModelPrivate(this))
00079 {
00080     QHash<int, QByteArray> roles = roleNames();
00081     roles[KeyRole] = "key";
00082     setRoleNames(roles);
00083 }
00084 
00085 SessionsModel::~SessionsModel()
00086 {
00087     delete d_ptr;
00088 }
00089 
00090 int SessionsModel::rowCount(const QModelIndex &parent) const
00091 {
00092     Q_D(const SessionsModel);
00093     
00094     if (parent == QModelIndex()) { //if top level
00095         return d->items.size();
00096     } else {
00097         return 0; // no child elements.
00098     }
00099 }
00100 
00101 QVariant SessionsModel::data(const QModelIndex &index, int role) const
00102 {
00103     Q_D(const SessionsModel);
00104 
00105     if (! index.isValid()) {
00106         return QVariant();
00107     }
00108 
00109     int row = index.row();
00110 
00111     switch (role) {
00112     case SessionsModel::KeyRole:
00113         return d->items[row].key;
00114     case Qt::DisplayRole:
00115         return d->items[row].name;
00116     case Qt::ToolTipRole:
00117         return d->items[row].comment;
00118 
00119     }
00120     return QVariant();
00121 }
00122 
00123 #include "sessionsmodel_moc.cpp"