File indexing completed on 2024-04-14 05:24:38

0001 /*
0002     SPDX-FileCopyrightText: 2019 Michail Vourlakos <mvourlakos@gmail.com>
0003     SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 
0006 // local
0007 #include "shortcutstracker.h"
0008 #include "../tools/commontools.h"
0009 
0010 // Qt
0011 #include <QAction>
0012 #include <QDir>
0013 #include <QDebug>
0014 #include <QLatin1String>
0015 
0016 // KDE
0017 #include <KConfigGroup>
0018 #include <KDirWatch>
0019 #include <KGlobalAccel>
0020 
0021 
0022 #define GLOBALSHORTCUTSCONFIG "kglobalshortcutsrc"
0023 #define APPLETSHORTCUTKEY "activate widget "
0024 
0025 namespace Latte {
0026 namespace ShortcutsPart {
0027 
0028 ShortcutsTracker::ShortcutsTracker(QObject *parent)
0029     : QObject(parent)
0030 {
0031     //! load global shortcuts badges at startup
0032     initGlobalShortcutsWatcher();
0033     parseGlobalShortcuts();
0034     clearAllAppletShortcuts();
0035 }
0036 
0037 ShortcutsTracker::~ShortcutsTracker()
0038 {
0039 }
0040 
0041 void ShortcutsTracker::initGlobalShortcutsWatcher()
0042 {
0043     for (int i=1; i<=19; ++i) {
0044         m_badgesForActivate << QString();
0045     }
0046 
0047     const QString globalShortcutsFilePath = Latte::configPath() + "/" + GLOBALSHORTCUTSCONFIG;
0048     m_shortcutsConfigPtr = KSharedConfig::openConfig(globalShortcutsFilePath);
0049 
0050     KDirWatch::self()->addFile(globalShortcutsFilePath);
0051 
0052     connect(KDirWatch::self(), &KDirWatch::dirty, this, &ShortcutsTracker::shortcutsFileChanged, Qt::QueuedConnection);
0053     connect(KDirWatch::self(), &KDirWatch::created, this, &ShortcutsTracker::shortcutsFileChanged, Qt::QueuedConnection);
0054 }
0055 
0056 bool ShortcutsTracker::basedOnPositionEnabled() const
0057 {
0058     return m_basedOnPositionEnabled;
0059 }
0060 
0061 QStringList ShortcutsTracker::badgesForActivate() const
0062 {
0063     return m_badgesForActivate;
0064 }
0065 
0066 void ShortcutsTracker::shortcutsFileChanged(const QString &file)
0067 {
0068     if (!file.endsWith(GLOBALSHORTCUTSCONFIG)) {
0069         return;
0070     }
0071 
0072     m_shortcutsConfigPtr->reparseConfiguration();
0073     parseGlobalShortcuts();
0074 }
0075 
0076 QList<uint> ShortcutsTracker::appletsWithPlasmaShortcuts()
0077 {
0078     return m_appletShortcuts.keys();
0079 }
0080 
0081 QString ShortcutsTracker::appletShortcutBadge(int appletId)
0082 {
0083     if (m_appletShortcuts.contains(appletId)) {
0084         return m_appletShortcuts[appletId];
0085     }
0086 
0087     return QString();
0088 }
0089 
0090 QString ShortcutsTracker::shortcutToBadge(QStringList shortcutRecords)
0091 {
0092     QString badge;
0093 
0094     if (shortcutRecords.count()>0 && shortcutRecords[0] != "none") {
0095         QStringList modifiers = shortcutRecords[0].split("+");
0096 
0097         if (modifiers.count() >= 1) {
0098             badge = modifiers[modifiers.count() - 1];
0099 
0100             //! when shortcut follows Meta+"Character" scheme
0101             if (modifiers.count() == 2 && modifiers[0] == QLatin1String("Meta")) {
0102                 badge = badge.toLower();
0103             } else {
0104                 badge = badge.toUpper();
0105             }
0106         }
0107     }
0108 
0109     return badge;
0110 }
0111 
0112 void ShortcutsTracker::parseGlobalShortcuts()
0113 {
0114     KConfigGroup latteGroup = KConfigGroup(m_shortcutsConfigPtr, "lattedock");
0115 
0116     if (latteGroup.exists()) {
0117         m_badgesForActivate.clear();
0118         m_appletShortcuts.clear();
0119 
0120         for (int i = 1; i <= 19; ++i) {
0121             QString entry = "activate entry " + QString::number(i);
0122 
0123             if (latteGroup.hasKey(entry)) {
0124                 QStringList records = latteGroup.readEntry(entry, QStringList());
0125                 if (records.count() > 0) {
0126                     records[0] = records[0].split("\t")[0];
0127                 }
0128                 m_badgesForActivate << shortcutToBadge(records);
0129             } else {
0130                 m_badgesForActivate << "";
0131             }
0132         }
0133 
0134         m_basedOnPositionEnabled = (!m_badgesForActivate[0].isEmpty() && !m_badgesForActivate[1].isEmpty());
0135 
0136         for(auto &key : latteGroup.keyList()) {
0137             if (key.startsWith(APPLETSHORTCUTKEY)) {
0138                 QStringList records = latteGroup.readEntry(key, QStringList());
0139                 int appletId = key.remove(APPLETSHORTCUTKEY).toInt();
0140 
0141                 m_appletShortcuts[appletId] = shortcutToBadge(records);
0142             }
0143         }
0144 
0145         qDebug() << "badges based on position updated to :: " << m_badgesForActivate;
0146         qDebug() << "badges for applet shortcuts updated to :: " << m_appletShortcuts;
0147 
0148         emit badgesForActivateChanged();
0149     }
0150 }
0151 
0152 void ShortcutsTracker::clearAllAppletShortcuts()
0153 {
0154     KConfigGroup latteGroup = KConfigGroup(m_shortcutsConfigPtr, "lattedock");
0155 
0156     for(const auto &key : latteGroup.keyList()) {
0157         if (key.startsWith(APPLETSHORTCUTKEY)) {
0158             QAction *appletAction = new QAction(this);
0159 
0160             appletAction->setText(QString("Activate ") + key);
0161             appletAction->setObjectName(key);
0162             appletAction->setShortcut(QKeySequence());
0163             KGlobalAccel::setGlobalShortcut(appletAction, QKeySequence());
0164             KGlobalAccel::self()->removeAllShortcuts(appletAction);
0165 
0166             appletAction->deleteLater();
0167         }
0168     }
0169 }
0170 
0171 
0172 }
0173 }