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