Lomiri
Loading...
Searching...
No Matches
SessionsModel.cpp
1/*
2 * Copyright (C) 2015-2016 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 */
17
18#include "SessionsModel.h"
19#include <QtCore/QFile>
20#include <QtCore/QSortFilterProxyModel>
21
22QHash<int, QByteArray> SessionsModel::roleNames() const
23{
24 return m_roleNames;
25}
26
27int SessionsModel::rowCount(const QModelIndex& parent) const
28{
29 return m_model->rowCount(parent);
30}
31
32QList<QUrl> SessionsModel::iconSearchDirectories() const
33{
34 return m_iconSearchDirectories;
35}
36
37void SessionsModel::setIconSearchDirectories(const QList<QUrl> searchDirectories)
38{
39 // QML gives us a url with file:// prepended which breaks QFile::exists()
40 // so convert the url to a local file
41 QList<QUrl> localList = {};
42 Q_FOREACH(const QUrl& searchDirectory, searchDirectories)
43 {
44 localList.append(searchDirectory.toLocalFile());
45 }
46 m_iconSearchDirectories = localList;
47 Q_EMIT iconSearchDirectoriesChanged();
48}
49
50QUrl SessionsModel::iconUrl(const QString sessionKey) const
51{
52 Q_FOREACH(const QUrl& searchDirectory, m_iconSearchDirectories)
53 {
54 // This is an established icon naming convention
55 QString customIconUrl = searchDirectory.toString(QUrl::StripTrailingSlash) +
56 "/custom_" + sessionKey + "_badge.png";
57 QString iconUrl = searchDirectory.toString(QUrl::StripTrailingSlash) +
58 "/" + sessionKey + "_badge.png";
59
60 QFile customIconFile(customIconUrl);
61 QFile iconFile(iconUrl);
62 if (customIconFile.exists()) {
63 return QUrl(customIconUrl);
64 } else if (iconFile.exists()) {
65 return QUrl(iconUrl);
66 } else {
67 // Search the legacy way
68 QString path = searchDirectory.toString(QUrl::StripTrailingSlash) + "/";
69 bool iconFound = false;
70 if (sessionKey == "ubuntu" || sessionKey == "ubuntu-2d") {
71 path += "ubuntu_badge.png";
72 iconFound = true;
73 } else if(
74 sessionKey == "gnome-classic" ||
75 sessionKey == "gnome-flashback-compiz" ||
76 sessionKey == "gnome-flashback-metacity" ||
77 sessionKey == "gnome-shell" ||
78 sessionKey == "gnome-wayland" ||
79 sessionKey == "gnome"
80 ){
81 path += "gnome_badge.png";
82 iconFound = true;
83 } else if (sessionKey == "plasma") {
84 path += "kde_badge.png";
85 iconFound = true;
86 } else if (sessionKey == "xterm") {
87 path += "recovery_console_badge.png";
88 iconFound = true;
89 } else if (sessionKey == "remote-login") {
90 path += "remote_login_help.png";
91 iconFound = true;
92 }
93
94 if (QFile(path).exists() && iconFound) {
95 return path;
96 }
97 }
98 }
99
100 // FIXME make this smarter
101 return QUrl("./graphics/session_icons/unknown_badge.png");
102}
103
104QVariant SessionsModel::data(const QModelIndex& index, int role) const
105{
106 switch (role) {
107 case SessionsModel::IconRole:
108 return iconUrl(m_model->data(index, QLightDM::SessionsModel::KeyRole).toString());
109 default:
110 return m_model->data(index, role);
111 }
112}
113
114SessionsModel::SessionsModel(QObject* parent)
115 : LomiriSortFilterProxyModelQML(parent)
116{
117 // Add a custom IconRole that isn't in either of the lightdm implementations
118 m_model = new QLightDM::SessionsModel(this);
119 m_roleNames = m_model->roleNames();
120 m_roleNames[IconRole] = "icon_url";
121
122 // Update search locations to use $SNAP prefix if specified
123 auto snapRoot = QFile::decodeName(qgetenv("SNAP"));
124 if (!snapRoot.isEmpty()) {
125 for (int i = 0; i < m_iconSearchDirectories.size(); i++) {
126 m_iconSearchDirectories[i] = snapRoot + m_iconSearchDirectories[i].path();
127 }
128 }
129
130 setModel(m_model);
131 setSortCaseSensitivity(Qt::CaseInsensitive);
132 setSortLocaleAware(true);
133 setSortRole(Qt::DisplayRole);
134 sort(0);
135}