-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDesktopListModel.cpp
80 lines (75 loc) · 2.15 KB
/
DesktopListModel.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include "DesktopListModel.h"
#include <QDir>
#include <QSettings>
#include <QStringList>
DesktopModel::DesktopModel(QObject *parent)
: QAbstractListModel(parent)
{
QStringList entries;
auto searchSessions = [&entries, this](const QString &dir) {
if (auto entryDir = QDir(dir); entryDir.exists()) {
QStringList desktops = entryDir.entryList({"*.desktop"}, QDir::Files);
for (QString &desktop : desktops) {
if (entries.contains(desktop)) {
continue;
}
entries.push_back(desktop);
auto path = QString("%1/%2").arg(dir).arg(desktop);
QSettings settings(path, QSettings::IniFormat);
settings.beginGroup("Desktop Entry");
m_infos.append(DesktopInfo{.name = settings.value("Name").toString().trimmed(),
.exec = settings.value("Exec").toString().trimmed(),
.fileName = desktop});
}
}
};
searchSessions("/usr/local/share/wayland-sessions/");
searchSessions("/usr/share/wayland-sessions/");
}
int
DesktopModel::rowCount(const QModelIndex &) const
{
return m_infos.length();
}
QVariant
DesktopModel::data(const QModelIndex &index, int role) const
{
switch (role) {
case Name:
return m_infos[index.row()].name;
case Exec:
return m_infos[index.row()].exec;
case FileName:
return m_infos[index.row()].fileName;
default:
return QVariant();
}
}
QHash<int, QByteArray>
DesktopModel::roleNames() const
{
static const QHash<int, QByteArray> roles{
{Name, "name"}, {Exec, "exec"}, {FileName, "fileName"}};
return roles;
}
int
DesktopModel::get_currentIndex(const QString &name)
{
if (m_infos.length() == 0) {
return -1;
}
int index = 0;
for (const DesktopInfo &info : m_infos) {
if (info.name == name) {
return index;
}
index++;
}
return 0;
}
QDebug
operator<<(QDebug d, const DesktopModel::DesktopInfo &info)
{
d << info.name << info.exec << info.fileName;
return d;
}