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
81
82
83
|
#include "DesktopBackend.h"
#include <QLocale>
#include <QVariantMap>
DesktopBackend::DesktopBackend(QObject* parent)
: QObject(parent)
, m_locale(QLocale::system().name())
{}
void DesktopBackend::setDesktops(const QVariantList& desktops)
{
m_rawDesktops = desktops;
m_selectedIndex = -1;
rebuildDesktops();
emit desktopsChanged();
emit selectedIndexChanged();
}
void DesktopBackend::setLocale(const QString& locale)
{
if (m_locale == locale)
return;
m_locale = locale;
rebuildDesktops();
emit localeChanged();
emit desktopsChanged();
}
void DesktopBackend::rebuildDesktops()
{
const bool hungarian = m_locale.startsWith(QLatin1String("hu"));
const bool german = m_locale.startsWith(QLatin1String("de"));
m_desktops.clear();
for (const QVariant& v : m_rawDesktops)
{
const QVariantMap raw = v.toMap();
QVariantMap item;
item.insert(QStringLiteral("name"), raw.value(QStringLiteral("name")));
item.insert(
QStringLiteral("description"),
hungarian ? raw.value(QStringLiteral("description_hu"), raw.value(QStringLiteral("description")))
: german ? raw.value(QStringLiteral("description_de"), raw.value(QStringLiteral("description")))
: raw.value(QStringLiteral("description_en"), raw.value(QStringLiteral("description"))));
item.insert(
QStringLiteral("badge"),
hungarian ? raw.value(QStringLiteral("badge_hu"), raw.value(QStringLiteral("badge")))
: german ? raw.value(QStringLiteral("badge_de"), raw.value(QStringLiteral("badge")))
: raw.value(QStringLiteral("badge_en"), raw.value(QStringLiteral("badge"))));
item.insert(QStringLiteral("badge_color"), raw.value(QStringLiteral("badge_color")));
item.insert(QStringLiteral("available"), raw.value(QStringLiteral("available"), true));
item.insert(QStringLiteral("packages"), raw.value(QStringLiteral("packages")));
m_desktops.append(item);
}
}
void DesktopBackend::selectDesktop(int index)
{
if (index < 0 || index >= m_desktops.size())
return;
if (!m_desktops[index].toMap().value(QStringLiteral("available"), true).toBool())
return;
if (index == m_selectedIndex)
return;
m_selectedIndex = index;
emit selectedIndexChanged();
emit selectionMade();
}
QStringList DesktopBackend::selectedPackages() const
{
if (m_selectedIndex < 0 || m_selectedIndex >= m_desktops.size())
return {};
const QVariantMap desktop = m_desktops[m_selectedIndex].toMap();
QStringList pkgs;
for (const auto& v : desktop.value(QStringLiteral("packages")).toList())
pkgs << v.toString();
return pkgs;
}
|