File indexing completed on 2024-12-29 05:06:02

0001 /*
0002  *  SPDX-FileCopyrightText: 2022 Devin Lin <devin@kde.org>
0003  *
0004  *  SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #include "mobileshellsettings.h"
0008 
0009 #include <KIO/CommandLauncherJob>
0010 #include <KNotificationJobUiDelegate>
0011 #include <KPluginFactory>
0012 
0013 #include <QDBusConnection>
0014 #include <QDBusMessage>
0015 #include <QDBusPendingCall>
0016 #include <QDebug>
0017 
0018 const QString CONFIG_FILE = QStringLiteral("plasmamobilerc");
0019 const QString GENERAL_CONFIG_GROUP = QStringLiteral("General");
0020 
0021 MobileShellSettings::MobileShellSettings(QObject *parent)
0022     : QObject{parent}
0023     , m_config{KSharedConfig::openConfig(CONFIG_FILE, KConfig::SimpleConfig)}
0024 {
0025     m_configWatcher = KConfigWatcher::create(m_config);
0026 
0027     connect(m_configWatcher.data(), &KConfigWatcher::configChanged, this, [this](const KConfigGroup &group, const QByteArrayList &names) -> void {
0028         if (group.name() == GENERAL_CONFIG_GROUP) {
0029             Q_EMIT vibrationsEnabledChanged();
0030             Q_EMIT vibrationDurationChanged();
0031             Q_EMIT animationsEnabledChanged();
0032             Q_EMIT navigationPanelEnabledChanged();
0033             Q_EMIT alwaysShowKeyboardToggleOnNavigationPanelChanged();
0034             Q_EMIT keyboardButtonEnabledChanged();
0035             Q_EMIT taskSwitcherPreviewsEnabledChanged();
0036             Q_EMIT actionDrawerTopLeftModeChanged();
0037             Q_EMIT actionDrawerTopRightModeChanged();
0038             Q_EMIT convergenceModeEnabledChanged();
0039         }
0040     });
0041 }
0042 
0043 bool MobileShellSettings::vibrationsEnabled() const
0044 {
0045     auto group = KConfigGroup{m_config, GENERAL_CONFIG_GROUP};
0046     return group.readEntry("vibrationsEnabled", true);
0047 }
0048 
0049 void MobileShellSettings::setVibrationsEnabled(bool vibrationsEnabled)
0050 {
0051     auto group = KConfigGroup{m_config, GENERAL_CONFIG_GROUP};
0052     group.writeEntry("vibrationsEnabled", vibrationsEnabled, KConfigGroup::Notify);
0053     m_config->sync();
0054 }
0055 
0056 int MobileShellSettings::vibrationDuration() const
0057 {
0058     auto group = KConfigGroup{m_config, GENERAL_CONFIG_GROUP};
0059     return group.readEntry("vibrationDuration", 10);
0060 }
0061 
0062 void MobileShellSettings::setVibrationDuration(int vibrationDuration)
0063 {
0064     auto group = KConfigGroup{m_config, GENERAL_CONFIG_GROUP};
0065     group.writeEntry("vibrationDuration", vibrationDuration, KConfigGroup::Notify);
0066     m_config->sync();
0067 }
0068 
0069 bool MobileShellSettings::animationsEnabled() const
0070 {
0071     auto group = KConfigGroup{m_config, GENERAL_CONFIG_GROUP};
0072     return group.readEntry("animationsEnabled", true);
0073 }
0074 
0075 void MobileShellSettings::setAnimationsEnabled(bool animationsEnabled)
0076 {
0077     auto group = KConfigGroup{m_config, GENERAL_CONFIG_GROUP};
0078     group.writeEntry("animationsEnabled", animationsEnabled, KConfigGroup::Notify);
0079     m_config->sync();
0080 }
0081 
0082 bool MobileShellSettings::navigationPanelEnabled() const
0083 {
0084     auto group = KConfigGroup{m_config, GENERAL_CONFIG_GROUP};
0085     return group.readEntry("navigationPanelEnabled", true);
0086 }
0087 
0088 void MobileShellSettings::setNavigationPanelEnabled(bool navigationPanelEnabled)
0089 {
0090     auto group = KConfigGroup{m_config, GENERAL_CONFIG_GROUP};
0091     group.writeEntry("navigationPanelEnabled", navigationPanelEnabled, KConfigGroup::Notify);
0092     m_config->sync();
0093 
0094     // TODO: Gesture only mode setting is disabled until we get the kwin effect gesture working: https://invent.kde.org/plasma/plasma-mobile/-/issues/300
0095     // updateNavigationBarsInPlasma(navigationPanelEnabled);
0096 }
0097 
0098 bool MobileShellSettings::alwaysShowKeyboardToggleOnNavigationPanel() const
0099 {
0100     auto group = KConfigGroup{m_config, GENERAL_CONFIG_GROUP};
0101     return group.readEntry("alwaysShowKeyboardToggleOnNavigationPanel", false);
0102 }
0103 
0104 void MobileShellSettings::setAlwaysShowKeyboardToggleOnNavigationPanel(bool alwaysShowKeyboardToggleOnNavigationPanel)
0105 {
0106     auto group = KConfigGroup{m_config, GENERAL_CONFIG_GROUP};
0107     group.writeEntry("alwaysShowKeyboardToggleOnNavigationPanel", alwaysShowKeyboardToggleOnNavigationPanel, KConfigGroup::Notify);
0108     m_config->sync();
0109 }
0110 
0111 MobileShellSettings::ActionDrawerMode MobileShellSettings::actionDrawerTopLeftMode() const
0112 {
0113     auto group = KConfigGroup{m_config, GENERAL_CONFIG_GROUP};
0114     return (ActionDrawerMode)group.readEntry("actionDrawerTopLeftMode", (int)ActionDrawerMode::Pinned);
0115 }
0116 
0117 void MobileShellSettings::setActionDrawerTopLeftMode(ActionDrawerMode actionDrawerMode)
0118 {
0119     auto group = KConfigGroup{m_config, GENERAL_CONFIG_GROUP};
0120     group.writeEntry("actionDrawerTopLeftMode", (int)actionDrawerMode, KConfigGroup::Notify);
0121     m_config->sync();
0122 }
0123 
0124 MobileShellSettings::ActionDrawerMode MobileShellSettings::actionDrawerTopRightMode() const
0125 {
0126     auto group = KConfigGroup{m_config, GENERAL_CONFIG_GROUP};
0127     return (ActionDrawerMode)group.readEntry("actionDrawerTopRightMode", (int)ActionDrawerMode::Expanded);
0128 }
0129 
0130 void MobileShellSettings::setActionDrawerTopRightMode(ActionDrawerMode actionDrawerMode)
0131 {
0132     auto group = KConfigGroup{m_config, GENERAL_CONFIG_GROUP};
0133     group.writeEntry("actionDrawerTopRightMode", (int)actionDrawerMode, KConfigGroup::Notify);
0134     m_config->sync();
0135 }
0136 
0137 bool MobileShellSettings::convergenceModeEnabled() const
0138 {
0139     auto group = KConfigGroup{m_config, GENERAL_CONFIG_GROUP};
0140     return group.readEntry("convergenceModeEnabled", false);
0141 }
0142 
0143 void MobileShellSettings::setConvergenceModeEnabled(bool enabled)
0144 {
0145     auto group = KConfigGroup{m_config, GENERAL_CONFIG_GROUP};
0146     group.writeEntry("convergenceModeEnabled", enabled, KConfigGroup::Notify);
0147     m_config->sync();
0148 
0149     // update environment settings
0150     auto *job = new KIO::CommandLauncherJob(QStringLiteral("plasma-mobile-envmanager --apply-settings"), {});
0151     job->setUiDelegate(new KNotificationJobUiDelegate(KJobUiDelegate::AutoErrorHandlingEnabled));
0152     job->setDesktopName(QStringLiteral("org.kde.plasma-mobile-envmanager"));
0153     job->start();
0154 }
0155 
0156 void MobileShellSettings::updateNavigationBarsInPlasma(bool navigationPanelEnabled)
0157 {
0158     auto message = QDBusMessage::createMethodCall(QLatin1String("org.kde.plasmashell"),
0159                                                   QLatin1String("/PlasmaShell"),
0160                                                   QLatin1String("org.kde.PlasmaShell"),
0161                                                   QLatin1String("evaluateScript"));
0162 
0163     if (navigationPanelEnabled) {
0164         QString createNavigationPanelScript = R"(
0165             var bottomPanel = new Panel("org.kde.plasma.mobile.taskpanel")
0166             bottomPanel.location = "bottom";
0167             bottomPanel.height = 2 * gridUnit;
0168         )";
0169 
0170         message << createNavigationPanelScript;
0171 
0172     } else {
0173         QString deleteNavigationPanelScript = R"(
0174             let allPanels = panels();
0175             for (var i = 0; i < allPanels.length; i++) {
0176                 if (allPanels[i].type === "org.kde.plasma.mobile.taskpanel") {
0177                     allPanels[i].remove();
0178                 }
0179             }
0180         )";
0181 
0182         message << deleteNavigationPanelScript;
0183     }
0184 
0185     // TODO check for error response
0186     QDBusConnection::sessionBus().asyncCall(message);
0187 }