on 9ef0600 help sign in
unity8/plugins/LightDM/SessionsModel.cpp
79 lines 2.3 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>
22QHash<int, QByteArray> SessionsModel::roleNames() const
24 return m_roleNames;
27int SessionsModel::rowCount(const QModelIndex& parent) const
29 return m_model->rowCount(parent);
32// Used for testing
33QList<QUrl> SessionsModel::iconSearchDirectories() const
35 return m_iconSearchDirectories;
38QUrl SessionsModel::iconUrl(QString sessionName) const
40 Q_FOREACH(const QUrl& searchDirectory, m_iconSearchDirectories)
41 {
42 // This is an established icon naming convention
43 QString iconUrl = searchDirectory.toString(QUrl::StripTrailingSlash) +
44 "/" + sessionName.toLower() + "_badge.png";
45 QFile iconFile(iconUrl);
46 if (iconFile.exists()) {
47 return QUrl(iconUrl);
48 }
49 }
51 // FIXME make this smarter
52 return QUrl("./graphics/session_icons/unknown_badge.png");
55QVariant SessionsModel::data(const QModelIndex& index, int role) const
57 switch (role) {
58 case SessionsModel::IconRole:
59 return QVariant(iconUrl(m_model->data(index, Qt::DisplayRole).toString()));
60 default:
61 return m_model->data(index, role);
62 }
65SessionsModel::SessionsModel(QObject* parent)
66 : UnitySortFilterProxyModelQML(parent)
68 // Add a custom IconRole that isn't in either of the lightdm implementations
69 m_model = new QLightDM::SessionsModel(this);
70 m_roleNames = m_model->roleNames();
71 m_roleNames[IconRole] = "icon_url";
73 setModel(m_model);
74 setSourceModel(m_model);
75 setSortCaseSensitivity(Qt::CaseInsensitive);
76 setSortLocaleAware(true);
77 setSortRole(Qt::DisplayRole);
78 sort(0);