File: appsmodel.cpp

package info (click to toggle)
flatpak-kcm 6.5.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,636 kB
  • sloc: cpp: 3,783; xml: 62; makefile: 5; sh: 1
file content (84 lines) | stat: -rw-r--r-- 1,994 bytes parent folder | download
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
84
/*
    SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
    SPDX-FileCopyrightText: 2025 David Redondo <kde@david-redondo.de>
*/

#include "appsmodel.h"

#include <KApplicationTrader>

using namespace Qt::StringLiterals;

AppsModel::AppsModel(QObject *parent)
{
    loadApps();
}

void AppsModel::loadApps()
{
    beginResetModel();
    m_apps = KApplicationTrader::query([](const KService::Ptr &app) {
        return !app->noDisplay();
    });
    endResetModel();
}

int AppsModel::rowCount(const QModelIndex &parent) const
{
    return m_apps.size();
}

QHash<int, QByteArray> AppsModel::roleNames() const
{
    return {
        {Qt::DisplayRole, "display"_ba},
        {Qt::DecorationRole, "decoration"_ba},
        {AppIdRole, "appId"_ba},
        {IsHostRole, "isHost"_ba},
    };
}

QString sandBoxId(const KService::Ptr &app)
{
    if (const auto flatpak = app->property<QString>("X-Flatpak"_L1); !flatpak.isEmpty()) {
        return flatpak;
    }
    if (const auto snap = app->property<QString>("X-SnapInstanceName"_L1); !snap.isEmpty()) {
        return snap;
    }
    return QString();
}

QVariant AppsModel::data(const QModelIndex &index, int role) const
{
    if (!checkIndex(index, CheckIndexOption::IndexIsValid | CheckIndexOption::ParentIsInvalid)) {
        return {};
    }

    const auto &app = m_apps.at(index.row());
    switch (role) {
    case Qt::DisplayRole:
        return app->name();
    case Qt::DecorationRole:
        return app->icon();
    case AppIdRole: {
        const auto id = sandBoxId(app);
        return id.isEmpty() ? app->desktopEntryName() : id;
    }
    case IsHostRole:
        return sandBoxId(app).isEmpty();
    }
    return {};
}

QString AppsModel::findIconNameById(const QString &id) const
{
    for (const auto &app : m_apps) {
        if (sandBoxId(app) == id || app->desktopEntryName() == id) {
            return app->icon();
        }
    }
    return QString();
}

#include "moc_appsmodel.cpp"