Warning, file /plasma/plasma-mobile/containments/homescreens/halcyon/applicationlistmodel.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).
0001 // SPDX-FileCopyrightText: 2014 Antonis Tsiapaliokas <antonis.tsiapaliokas@kde.org> 0002 // SPDX-FileCopyrightText: 2022 Devin Lin <devin@kde.org> 0003 // SPDX-License-Identifier: GPL-2.0-or-later 0004 0005 #include "applicationlistmodel.h" 0006 0007 #include <QByteArray> 0008 #include <QDebug> 0009 #include <QModelIndex> 0010 #include <QProcess> 0011 #include <QQuickWindow> 0012 0013 #include <KApplicationTrader> 0014 #include <KConfigGroup> 0015 #include <KIO/ApplicationLauncherJob> 0016 #include <KNotificationJobUiDelegate> 0017 #include <KService> 0018 #include <KSharedConfig> 0019 #include <KSycoca> 0020 0021 ApplicationListModel::ApplicationListModel(QObject *parent) 0022 : QAbstractListModel(parent) 0023 { 0024 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) 0025 connect(KSycoca::self(), qOverload<const QStringList &>(&KSycoca::databaseChanged), this, &ApplicationListModel::sycocaDbChanged); 0026 #else 0027 connect(KSycoca::self(), &KSycoca::databaseChanged, this, &ApplicationListModel::sycocaDbChanged); 0028 #endif 0029 } 0030 0031 ApplicationListModel::~ApplicationListModel() = default; 0032 0033 QHash<int, QByteArray> ApplicationListModel::roleNames() const 0034 { 0035 return {{ApplicationRole, QByteArrayLiteral("application")}}; 0036 } 0037 0038 void ApplicationListModel::sycocaDbChanged() 0039 { 0040 loadApplications(); 0041 } 0042 0043 void ApplicationListModel::loadApplications() 0044 { 0045 auto cfg = KSharedConfig::openConfig(QStringLiteral("applications-blacklistrc")); 0046 auto blgroup = KConfigGroup(cfg, QStringLiteral("Applications")); 0047 0048 const QStringList blacklist = blgroup.readEntry("blacklist", QStringList()); 0049 0050 beginResetModel(); 0051 0052 m_applicationList.clear(); 0053 0054 QList<Application *> unorderedList; 0055 0056 auto filter = [blacklist](const KService::Ptr &service) -> bool { 0057 if (service->noDisplay()) { 0058 return false; 0059 } 0060 0061 if (!service->showOnCurrentPlatform()) { 0062 return false; 0063 } 0064 0065 if (blacklist.contains(service->desktopEntryName())) { 0066 return false; 0067 } 0068 0069 return true; 0070 }; 0071 0072 const KService::List apps = KApplicationTrader::query(filter); 0073 0074 for (const KService::Ptr &service : apps) { 0075 Application *application = new Application{this, service}; 0076 unorderedList.append(application); 0077 } 0078 0079 std::sort(unorderedList.begin(), unorderedList.end(), [](const Application *a1, const Application *a2) { 0080 return a1->name().compare(a2->name(), Qt::CaseInsensitive) < 0; 0081 }); 0082 0083 m_applicationList << unorderedList; 0084 0085 endResetModel(); 0086 } 0087 0088 QVariant ApplicationListModel::data(const QModelIndex &index, int role) const 0089 { 0090 if (!index.isValid()) { 0091 return QVariant(); 0092 } 0093 0094 return QVariant::fromValue(m_applicationList.at(index.row())); 0095 } 0096 0097 int ApplicationListModel::rowCount(const QModelIndex &parent) const 0098 { 0099 if (parent.isValid()) { 0100 return 0; 0101 } 0102 0103 return m_applicationList.count(); 0104 }