on a072961 help sign in
unity8/plugins/LightDM/SessionsModel.cpp
85 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>
21#include <QDebug>
22QHash<int, QByteArray> SessionsModel::roleNames() const
24 return m_roleNames;
27int SessionsModel::rowCount(const QModelIndex& parent) const
29 return m_model->rowCount(parent);
32QList<QUrl> SessionsModel::iconSearchDirectories() const
34 return m_iconSearchDirectories;
37void SessionsModel::setIconSearchDirectories(QList<QUrl> searchDirectories)
39 m_iconSearchDirectories = searchDirectories;
40 Q_EMIT iconSearchDirectoriesChanged();
43QUrl SessionsModel::iconUrl(QString sessionName) const
45 Q_FOREACH(const QUrl& searchDirectory, m_iconSearchDirectories)
46 {
47 qDebug() << "LOOKING IN: " << searchDirectory.toString();
48 // This is an established icon naming convention
49 QString iconUrl = searchDirectory.toString(QUrl::StripTrailingSlash) +
50 "/" + sessionName.toLower() + "_badge.png";
51 QFile iconFile(iconUrl);
52 if (iconFile.exists()) {
53 return QUrl(iconUrl);
54 }
55 }
57 // FIXME make this smarter
58 return QUrl("./graphics/session_icons/unknown_badge.png");
61QVariant SessionsModel::data(const QModelIndex& index, int role) const
63 switch (role) {
64 case SessionsModel::IconRole:
65 return QVariant(iconUrl(m_model->data(index, Qt::DisplayRole).toString()));
66 default:
67 return m_model->data(index, role);
68 }
71SessionsModel::SessionsModel(QObject* parent)
72 : UnitySortFilterProxyModelQML(parent)
74 // Add a custom IconRole that isn't in either of the lightdm implementations
75 m_model = new QLightDM::SessionsModel(this);
76 m_roleNames = m_model->roleNames();
77 m_roleNames[IconRole] = "icon_url";
79 setModel(m_model);
80 setSourceModel(m_model);
81 setSortCaseSensitivity(Qt::CaseInsensitive);
82 setSortLocaleAware(true);
83 setSortRole(Qt::DisplayRole);
84 sort(0);