File indexing completed on 2024-11-10 04:56:53

0001 /*
0002     SPDX-FileCopyrightText: 2020 Aleix Pol Gonzalez <aleixpol@kde.org>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "kcmvirtualkeyboard.h"
0008 
0009 #include <KApplicationTrader>
0010 #include <KConfigGroup>
0011 #include <KDesktopFile>
0012 #include <KLocalizedString>
0013 #include <KPluginFactory>
0014 
0015 #include <virtualkeyboarddata.h>
0016 #include <virtualkeyboardsettings.h>
0017 
0018 K_PLUGIN_FACTORY_WITH_JSON(KcmVirtualKeyboardFactory, "kcm_virtualkeyboard.json", registerPlugin<KcmVirtualKeyboard>(); registerPlugin<VirtualKeyboardData>();)
0019 
0020 KcmVirtualKeyboard::KcmVirtualKeyboard(QObject *parent, const KPluginMetaData &metaData)
0021     : KQuickManagedConfigModule(parent, metaData)
0022     , m_data(new VirtualKeyboardData(this))
0023     , m_model(new VirtualKeyboardsModel(this))
0024 {
0025     qmlRegisterAnonymousType<VirtualKeyboardSettings>("org.kde.kwin.virtualkeyboardsettings", 1);
0026 }
0027 
0028 KcmVirtualKeyboard::~KcmVirtualKeyboard() = default;
0029 
0030 VirtualKeyboardSettings *KcmVirtualKeyboard::settings() const
0031 {
0032     return m_data->settings();
0033 }
0034 
0035 VirtualKeyboardsModel::VirtualKeyboardsModel(QObject *parent)
0036     : QAbstractListModel(parent)
0037 {
0038     m_services = KApplicationTrader::query([](const KService::Ptr &service) {
0039         return service->property<bool>("X-KDE-Wayland-VirtualKeyboard");
0040     });
0041 
0042     m_services.prepend({});
0043 }
0044 
0045 QHash<int, QByteArray> VirtualKeyboardsModel::roleNames() const
0046 {
0047     QHash<int, QByteArray> ret = QAbstractListModel::roleNames();
0048     ret.insert(DesktopFileNameRole, "desktopFileName");
0049     return ret;
0050 }
0051 
0052 QVariant VirtualKeyboardsModel::data(const QModelIndex &index, int role) const
0053 {
0054     if (!index.isValid() || index.parent().isValid() || index.row() > m_services.count()) {
0055         return {};
0056     }
0057 
0058     const KService::Ptr service = m_services[index.row()];
0059     switch (role) {
0060     case Qt::DisplayRole:
0061         return service ? service->name() : i18n("None");
0062     case Qt::DecorationRole:
0063         return service ? service->icon() : QStringLiteral("edit-none");
0064     case Qt::ToolTipRole:
0065         return service ? service->comment() : i18n("Do not use any virtual keyboard");
0066     case DesktopFileNameRole:
0067         return service ? QStandardPaths::locate(QStandardPaths::ApplicationsLocation, service->desktopEntryName() + QLatin1String(".desktop")) : QString();
0068     }
0069     return {};
0070 }
0071 
0072 int VirtualKeyboardsModel::inputMethodIndex(const QString &desktopFile) const
0073 {
0074     if (desktopFile.isEmpty()) {
0075         return 0;
0076     }
0077 
0078     int i = 0;
0079     for (const auto &service : m_services) {
0080         if (service && desktopFile.endsWith(service->desktopEntryName() + QLatin1String(".desktop"))) {
0081             return i;
0082         }
0083         ++i;
0084     }
0085     return -1;
0086 }
0087 
0088 int VirtualKeyboardsModel::rowCount(const QModelIndex &parent) const
0089 {
0090     return parent.isValid() ? 0 : m_services.count();
0091 }
0092 
0093 #include "kcmvirtualkeyboard.moc"
0094 #include "moc_kcmvirtualkeyboard.cpp"