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/QDebug>
20#include <QtCore/QFile>
21#include <QtCore/QLoggingCategory>
22#include <QtCore/QSortFilterProxyModel>
23
24Q_LOGGING_CATEGORY(SessionsModelLogger, "lomiri.plugins.LightDM.SessionsModel")
25
26QHash<int, QByteArray> SessionsModel::roleNames() const
27{
28 return m_roleNames;
29}
30
31int SessionsModel::rowCount(const QModelIndex& parent) const
32{
33 return m_model->rowCount(parent);
34}
35
36QList<QUrl> SessionsModel::iconSearchDirectories() const
37{
38 return m_iconSearchDirectories;
39}
40
41void SessionsModel::setIconSearchDirectories(const QList<QUrl> searchDirectories)
42{
43 // QML gives us a url with file:// prepended which breaks QFile::exists()
44 // so convert the url to a local file
45 QList<QUrl> localList = {};
46 Q_FOREACH(const QUrl& searchDirectory, searchDirectories)
47 {
48 localList.append(searchDirectory.toLocalFile());
49 }
50 m_iconSearchDirectories = localList;
51 Q_EMIT iconSearchDirectoriesChanged();
52}
53
54QUrl SessionsModel::iconUrl(const QString sessionKey) const
55{
56 const QStringList imgExtensions { "svg", "png" };
57 for (const QUrl& searchDirectory : m_iconSearchDirectories)
58 {
59 for (const QString& imgExt : imgExtensions)
60 {
61 // This is an established icon naming convention
62 QString customIconUrl = searchDirectory.toString(QUrl::StripTrailingSlash) +
63 "/custom_" + sessionKey + "_badge." + imgExt;
64 QString iconUrl = searchDirectory.toString(QUrl::StripTrailingSlash) +
65 "/" + sessionKey + "_badge." + imgExt;
66
67 QFile customIconFile(customIconUrl);
68 QFile iconFile(iconUrl);
69 if (customIconFile.exists()) {
70 qCWarning(SessionsModelLogger) << "Found custom session badge icon: " << customIconUrl;
71 return QUrl(customIconUrl);
72 } else if (iconFile.exists()) {
73 qCWarning(SessionsModelLogger) << "Found session badge icon: " << iconUrl;
74 return QUrl(iconUrl);
75 } else {
76 qCWarning(SessionsModelLogger) << "Failed to find icon file: " << iconUrl;
77 }
78 // Skip the legacy part for .svg image extension... (SVGs have never been shipped in
79 // lomiri sources, they only exist in ayatana-greeter-badges).
80 if (imgExt == "png") {
81
82 qCWarning(SessionsModelLogger) << "Trying legacy mechanism for finding an appropriate icon file.";
83
84 // Search the legacy way, only needed if ayatana-greeter-badges is not used
85 // as session icon badge source.
86 QString path = searchDirectory.toString(QUrl::StripTrailingSlash) + "/";
87 bool iconFound = false;
88 if (sessionKey == "ubuntu" || sessionKey == "ubuntu-2d") {
89 path += "ubuntu_badge.png";
90 iconFound = true;
91 } else if(
92 sessionKey == "gnome-classic" ||
93 sessionKey == "gnome-flashback-compiz" ||
94 sessionKey == "gnome-flashback-metacity" ||
95 sessionKey == "gnome-shell" ||
96 sessionKey == "gnome-wayland" ||
97 sessionKey == "gnome"
98 ) {
99 path += "gnome_badge.png";
100 iconFound = true;
101 } else if (sessionKey == "plasma") {
102 path += "kde_badge.png";
103 iconFound = true;
104 } else if (sessionKey == "xterm") {
105 path += "recovery_console_badge.png";
106 iconFound = true;
107 } else if (sessionKey == "remote-login") {
108 path += "remote_login_help.png";
109 iconFound = true;
110 }
111
112 if (QFile(path).exists() && iconFound) {
113 qCWarning(SessionsModelLogger) << "Using session badge icon: " << path << " for session type: " << sessionKey;
114 return path;
115 }
116 }
117 }
118 }
119
120 // FIXME make this smarter
121 qCWarning(SessionsModelLogger) << "No suitable icon found! Falling back to unknown_badge.png icon.";
122 return QUrl("./graphics/session_icons/unknown_badge.png");
123}
124
125QVariant SessionsModel::data(const QModelIndex& index, int role) const
126{
127 switch (role) {
128 case SessionsModel::IconRole:
129 return iconUrl(m_model->data(index, QLightDM::SessionsModel::KeyRole).toString());
130 default:
131 return m_model->data(index, role);
132 }
133}
134
135SessionsModel::SessionsModel(QObject* parent)
136 : LomiriSortFilterProxyModelQML(parent)
137{
138 // Add a custom IconRole that isn't in either of the lightdm implementations
139 m_model = new QLightDM::SessionsModel(this);
140 m_roleNames = m_model->roleNames();
141 m_roleNames[IconRole] = "icon_url";
142
143 // Update search locations to use $SNAP prefix if specified
144 auto snapRoot = QFile::decodeName(qgetenv("SNAP"));
145 if (!snapRoot.isEmpty()) {
146 for (int i = 0; i < m_iconSearchDirectories.size(); i++) {
147 m_iconSearchDirectories[i] = snapRoot + m_iconSearchDirectories[i].path();
148 }
149 }
150
151 setModel(m_model);
152 setSortCaseSensitivity(Qt::CaseInsensitive);
153 setSortLocaleAware(true);
154 setSortRole(Qt::DisplayRole);
155 sort(0);
156}