File indexing completed on 2024-12-29 05:06:05
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 connect(KSycoca::self(), &KSycoca::databaseChanged, this, &ApplicationListModel::sycocaDbChanged); 0025 } 0026 0027 ApplicationListModel::~ApplicationListModel() = default; 0028 0029 ApplicationListModel *ApplicationListModel::self() 0030 { 0031 static ApplicationListModel *inst = new ApplicationListModel(nullptr); 0032 return inst; 0033 } 0034 0035 QHash<int, QByteArray> ApplicationListModel::roleNames() const 0036 { 0037 return {{ApplicationRole, QByteArrayLiteral("application")}}; 0038 } 0039 0040 void ApplicationListModel::sycocaDbChanged() 0041 { 0042 loadApplications(); 0043 } 0044 0045 void ApplicationListModel::loadApplications() 0046 { 0047 auto cfg = KSharedConfig::openConfig(QStringLiteral("applications-blacklistrc")); 0048 auto blgroup = KConfigGroup(cfg, QStringLiteral("Applications")); 0049 0050 const QStringList blacklist = blgroup.readEntry("blacklist", QStringList()); 0051 0052 beginResetModel(); 0053 0054 m_applicationList.clear(); 0055 0056 QList<Application *> unorderedList; 0057 0058 auto filter = [blacklist](const KService::Ptr &service) -> bool { 0059 if (service->noDisplay()) { 0060 return false; 0061 } 0062 0063 if (!service->showOnCurrentPlatform()) { 0064 return false; 0065 } 0066 0067 if (blacklist.contains(service->desktopEntryName())) { 0068 return false; 0069 } 0070 0071 return true; 0072 }; 0073 0074 const KService::List apps = KApplicationTrader::query(filter); 0075 0076 for (const KService::Ptr &service : apps) { 0077 Application *application = new Application{this, service}; 0078 unorderedList.append(application); 0079 } 0080 0081 std::sort(unorderedList.begin(), unorderedList.end(), [](const Application *a1, const Application *a2) { 0082 return a1->name().compare(a2->name(), Qt::CaseInsensitive) < 0; 0083 }); 0084 0085 m_applicationList << unorderedList; 0086 0087 endResetModel(); 0088 } 0089 0090 QVariant ApplicationListModel::data(const QModelIndex &index, int role) const 0091 { 0092 if (!index.isValid()) { 0093 return QVariant(); 0094 } 0095 0096 return QVariant::fromValue(m_applicationList.at(index.row())); 0097 } 0098 0099 int ApplicationListModel::rowCount(const QModelIndex &parent) const 0100 { 0101 if (parent.isValid()) { 0102 return 0; 0103 } 0104 0105 return m_applicationList.count(); 0106 }