File indexing completed on 2024-04-28 05:34:23

0001 /*
0002  * SPDX-FileCopyrightText: 2019 Arjen Hiemstra <ahiemstra@heimr.nl>
0003  *
0004  * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005  */
0006 
0007 #include "ToolsModel.h"
0008 
0009 #include <QDBusConnection>
0010 #include <QDBusConnectionInterface>
0011 #include <QDBusPendingCall>
0012 
0013 #include <QDebug>
0014 
0015 // #include <KRun>
0016 #include <KGlobalAccel>
0017 #include <KIO/ApplicationLauncherJob>
0018 #include <KIO/CommandLauncherJob>
0019 #include <KLocalizedString>
0020 
0021 ToolsModel::ToolsModel(QObject *parent)
0022     : QAbstractListModel(parent)
0023 {
0024     addFromService(QStringLiteral("org.kde.konsole"));
0025     //     addFromService(QStringLiteral("org.kde.ksysguard"));
0026     addFromService(QStringLiteral("org.kde.ksystemlog"));
0027     addFromService(QStringLiteral("org.kde.kinfocenter"));
0028     addFromService(QStringLiteral("org.kde.filelight"));
0029     addFromService(QStringLiteral("org.kde.sweeper"));
0030     addFromService(QStringLiteral("org.kde.kmag"));
0031     addFromService(QStringLiteral("htop"));
0032 
0033     if (QDBusConnection::sessionBus().interface()->isServiceRegistered(QStringLiteral("org.kde.KWin"))) {
0034         auto entry = Entry{};
0035         entry.id = QStringLiteral("killWindow");
0036         entry.icon = QStringLiteral("document-close");
0037         entry.name = i18nc("@action:inmenu", "Kill a Window…");
0038         const auto killWindowShortcutList = KGlobalAccel::self()->globalShortcut(QStringLiteral("kwin"), QStringLiteral("Kill Window"));
0039         if (!killWindowShortcutList.isEmpty()) {
0040             entry.shortcut = killWindowShortcutList.first().toString();
0041         }
0042         m_entries << entry;
0043     }
0044 }
0045 
0046 QHash<int, QByteArray> ToolsModel::roleNames() const
0047 {
0048     static QHash<int, QByteArray> names = {//
0049                                            {IdRole, "id"},
0050                                            {NameRole, "name"},
0051                                            {IconRole, "icon"},
0052                                            {ShortcutRole, "shortcut"}};
0053     return names;
0054 }
0055 
0056 int ToolsModel::rowCount(const QModelIndex &parent) const
0057 {
0058     if (parent.isValid()) {
0059         return 0;
0060     }
0061 
0062     return m_entries.count();
0063 }
0064 
0065 QVariant ToolsModel::data(const QModelIndex &index, int role) const
0066 {
0067     if (!checkIndex(index, CheckIndexOption::IndexIsValid | CheckIndexOption::DoNotUseParent)) {
0068         return QVariant{};
0069     }
0070 
0071     auto entry = m_entries.at(index.row());
0072     switch (role) {
0073     case IdRole:
0074         return entry.id;
0075     case NameRole:
0076         return entry.name;
0077     case IconRole:
0078         return entry.icon;
0079     case ShortcutRole:
0080         return entry.shortcut;
0081     }
0082 
0083     return QVariant{};
0084 }
0085 
0086 void ToolsModel::trigger(const QString &id)
0087 {
0088     auto itr = std::find_if(m_entries.cbegin(), m_entries.cend(), [id](const Entry &entry) {
0089         return entry.id == id;
0090     });
0091     if (itr == m_entries.cend()) {
0092         return;
0093     }
0094 
0095     if (itr->service) {
0096         auto job = new KIO::ApplicationLauncherJob(itr->service);
0097         job->start();
0098     }
0099 
0100     if (itr->id == QStringLiteral("killWindow")) {
0101         auto message = QDBusMessage::createMethodCall(QStringLiteral("org.kde.KWin"),
0102                                                       QStringLiteral("/KWin"),
0103                                                       QStringLiteral("org.kde.KWin"),
0104                                                       QStringLiteral("killWindow"));
0105         QDBusConnection::sessionBus().asyncCall(message);
0106     }
0107 }
0108 
0109 void ToolsModel::addFromService(const QString &serviceName)
0110 {
0111     auto service = KService::serviceByDesktopName(serviceName);
0112     if (service) {
0113         Entry entry;
0114         entry.id = serviceName;
0115         entry.name = i18nc("@action:inmenu %1 is application name", "Launch %1", service->name());
0116         entry.icon = service->icon();
0117         entry.service = service;
0118         m_entries << entry;
0119     }
0120 }
0121 
0122 #include "moc_ToolsModel.cpp"