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

0001 /*
0002     SPDX-FileCopyrightText: 2018 Michail Vourlakos <mvourlakos@gmail.com>
0003     SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 
0006 #include "screenpool.h"
0007 
0008 // local
0009 #include "../../primaryoutputwatcher.h"
0010 #include "../../tools/commontools.h"
0011 
0012 // Qt
0013 #include <QDebug>
0014 #include <QDir>
0015 #include <QGuiApplication>
0016 #include <QScreen>
0017 
0018 // KDE
0019 #include <KConfigGroup>
0020 #include <KDirWatch>
0021 #include <KSharedConfig>
0022 
0023 #define PLASMARC "plasmashellrc"
0024 
0025 namespace Latte {
0026 namespace PlasmaExtended {
0027 
0028 ScreenPool::ScreenPool(QObject *parent)
0029     : QObject(parent),
0030       m_primaryWatcher(new PrimaryOutputWatcher(this))
0031 {
0032     m_plasmarcConfig = KSharedConfig::openConfig(PLASMARC);
0033     m_screensGroup = KConfigGroup(m_plasmarcConfig, "ScreenConnectors");
0034 
0035     load();
0036 
0037     QString plasmaSettingsFile = Latte::configPath() + "/" + PLASMARC;
0038 
0039     KDirWatch::self()->addFile(plasmaSettingsFile);
0040 
0041     connect(KDirWatch::self(), &KDirWatch::dirty, this, [ &, plasmaSettingsFile](const QString & path) {
0042         if (path == plasmaSettingsFile) {
0043             load();
0044         }
0045     });
0046 
0047     connect(KDirWatch::self(), &KDirWatch::created, this, [ &, plasmaSettingsFile](const QString & path) {
0048         if (path == plasmaSettingsFile) {
0049             load();
0050         }
0051     });
0052 }
0053 
0054 
0055 ScreenPool::~ScreenPool()
0056 {
0057 }
0058 
0059 void ScreenPool::load()
0060 {
0061     QMap<int, QString> connectorForId = m_connectorForId;
0062     QHash<QString, int> idForConnector = m_idForConnector;
0063 
0064     m_connectorForId.clear();
0065     m_idForConnector.clear();
0066 
0067     m_plasmarcConfig->reparseConfiguration();
0068 
0069     bool updated{false};
0070 
0071     for (const auto &screenId : m_screensGroup.keyList()) {
0072         QString screenName =  m_screensGroup.readEntry(screenId, QString());
0073         int scrId = screenId.toInt();
0074         if (scrId != 0) {
0075             insertScreenMapping(scrId, screenName);
0076 
0077             if (!connectorForId.contains(scrId) || connectorForId[scrId] != m_connectorForId[scrId]) {
0078                 updated = true;
0079             }
0080         }
0081     }
0082 
0083     //! If there are changes then print the new plasma screen ids and send a relevant signal
0084     if (connectorForId.count() != m_connectorForId.count()) {
0085         updated = true;
0086     }
0087 
0088     if (updated) {
0089         qDebug() << "---------------- Plasma Screen Ids ------------------";
0090         for (const auto &id : m_connectorForId.keys()) {
0091             qDebug() << id << "  __  " << m_connectorForId[id];
0092         }
0093         qDebug() << "----------------  ---------------  ------------------";
0094 
0095         emit idsChanged();
0096     }
0097 }
0098 
0099 void ScreenPool::insertScreenMapping(int id, const QString &connector)
0100 {
0101     if (id==0 || connector.startsWith(":")) {
0102         return;
0103     }
0104 
0105     m_connectorForId[id] = connector;
0106     m_idForConnector[connector] = id;
0107 }
0108 
0109 int ScreenPool::id(const QString &connector) const
0110 {
0111     if (!m_idForConnector.contains(connector)) {
0112         //! return 0 for primary screen, -1 for not found
0113         return m_primaryWatcher->primaryScreen()->name() == connector ? 0 : -1;
0114     }
0115 
0116     return m_idForConnector.value(connector);
0117 }
0118 
0119 QString ScreenPool::connector(int id) const
0120 {
0121     if (!m_connectorForId.contains(id)) {
0122         return id == 0 ? qGuiApp->primaryScreen()->name() : QString();
0123     }
0124 
0125     return m_connectorForId.value(id);
0126 }
0127 
0128 
0129 }
0130 }