on 4132cd5 help sign in
unity8/plugins/LightDM/SessionsModel.cpp
83 lines 2.6 kB history raw
1/*
2 * Copyright (C) 2015 Canonical, Ltd.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 3.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 */
18#include "SessionsModel.h"
19#include <QtCore/QFile>
20#include <QtCore/QSortFilterProxyModel>
21QHash<int, QByteArray> SessionsModel::roleNames() const
23 return m_roleNames;
26int SessionsModel::rowCount(const QModelIndex& parent) const
28 return m_model->rowCount(parent);
31QModelIndex SessionsModel::sessionIndex(QString sessionName) const
33 for (int i = 0; i < rowCount(QModelIndex()); i ++) {
34 QModelIndex index = m_model->index(i, Qt::DisplayRole);
35 QString aSession(this->data(index, Qt::DisplayRole).toString());
36 if (!QString::compare(aSession, sessionName, Qt::CaseInsensitive)) {
37 return index;
38 }
39 }
40 return QModelIndex();
43QUrl SessionsModel::iconUrl(QString sessionName) const
45 Q_FOREACH(const QUrl& searchDirectory, m_iconSearchDirectories)
46 {
47 // This is an established icon naming convention
48 QString iconUrl = searchDirectory.toString(QUrl::StripTrailingSlash) +
49 "/" + sessionName.toLower() + "_badge.png";
50 QFile iconFile(iconUrl);
51 if (iconFile.exists()) {
52 return QUrl(iconUrl);
53 }
54 }
56 // FIXME make this smarter
57 return QUrl("./graphics/session_icons/unknown_badge.png");
60QVariant SessionsModel::data(const QModelIndex& index, int role) const
62 switch (role) {
63 case SessionsModel::IconRole:
64 return QVariant(iconUrl(m_model->data(index, Qt::DisplayRole).toString()));
65 default:
66 return m_model->data(index, role);
67 }
70SessionsModel::SessionsModel(QObject* parent)
71 : UnitySortFilterProxyModelQML(parent)
73 // Add a custom IconRole that isn't in either of the lightdm implementations
74 m_model = new QLightDM::SessionsModel(this);
75 m_roleNames = m_model->roleNames();
76 m_roleNames[IconRole] = "icon_url";
78 setModel(m_model);
79 setSortCaseSensitivity(Qt::CaseInsensitive);
80 setSortLocaleAware(true);
81 setSortRole(Qt::DisplayRole);
82 sort(0);