File indexing completed on 2024-04-14 15:37:15
0001 /* 0002 * Copyright 2018 Michail Vourlakos <mvourlakos@gmail.com> 0003 * 0004 * This file is part of Latte-Dock 0005 * 0006 * Latte-Dock is free software; you can redistribute it and/or 0007 * modify it under the terms of the GNU General Public License as 0008 * published by the Free Software Foundation; either version 2 of 0009 * the License, or (at your option) any later version. 0010 * 0011 * Latte-Dock is distributed in the hope that it will be useful, 0012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 0013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0014 * GNU General Public License for more details. 0015 * 0016 * You should have received a copy of the GNU General Public License 0017 * along with this program. If not, see <http://www.gnu.org/licenses/>. 0018 */ 0019 0020 #include "screenpool.h" 0021 0022 // Qt 0023 #include <QDebug> 0024 #include <QDir> 0025 #include <QGuiApplication> 0026 #include <QScreen> 0027 0028 // KDE 0029 #include <KConfigGroup> 0030 #include <KDirWatch> 0031 #include <KSharedConfig> 0032 0033 #define PLASMARC "plasmashellrc" 0034 0035 namespace Latte { 0036 namespace PlasmaExtended { 0037 0038 ScreenPool::ScreenPool(QObject *parent) 0039 : QObject(parent) 0040 { 0041 KSharedConfigPtr plasmaPtr = KSharedConfig::openConfig(PLASMARC); 0042 m_screensGroup = KConfigGroup(plasmaPtr, "ScreenConnectors"); 0043 0044 load(); 0045 0046 QString plasmaSettingsFile = QDir::homePath() + "/.config/" + PLASMARC; 0047 0048 KDirWatch::self()->addFile(plasmaSettingsFile); 0049 0050 connect(KDirWatch::self(), &KDirWatch::dirty, this, [ &, plasmaSettingsFile](const QString & path) { 0051 if (path == plasmaSettingsFile) { 0052 load(); 0053 } 0054 }); 0055 0056 connect(KDirWatch::self(), &KDirWatch::created, this, [ &, plasmaSettingsFile](const QString & path) { 0057 if (path == plasmaSettingsFile) { 0058 load(); 0059 } 0060 }); 0061 } 0062 0063 0064 ScreenPool::~ScreenPool() 0065 { 0066 } 0067 0068 void ScreenPool::load() 0069 { 0070 QMap<int, QString> connectorForId = m_connectorForId; 0071 QHash<QString, int> idForConnector = m_idForConnector; 0072 0073 m_connectorForId.clear(); 0074 m_idForConnector.clear(); 0075 0076 bool updated{false}; 0077 0078 for (const auto &screenId : m_screensGroup.keyList()) { 0079 QString screenName = m_screensGroup.readEntry(screenId, QString()); 0080 if (screenId != 0) { 0081 int scrId = screenId.toInt(); 0082 insertScreenMapping(scrId, screenName); 0083 0084 if (!connectorForId.contains(scrId) || connectorForId[scrId] != m_connectorForId[scrId]) { 0085 updated = true; 0086 } 0087 } 0088 } 0089 0090 //! If there are changes then print the new plasma screen ids and send a relevant signal 0091 if (connectorForId.count() != m_connectorForId.count()) { 0092 updated = true; 0093 } 0094 0095 if (updated) { 0096 qDebug() << "---------------- Plasma Screen Ids ------------------"; 0097 for (const auto &id : m_connectorForId.keys()) { 0098 qDebug() << id << " __ " << m_connectorForId[id]; 0099 } 0100 qDebug() << "---------------- --------------- ------------------"; 0101 0102 emit idsChanged(); 0103 } 0104 } 0105 0106 void ScreenPool::insertScreenMapping(int id, const QString &connector) 0107 { 0108 if (id==0 || connector.startsWith(":")) { 0109 return; 0110 } 0111 0112 m_connectorForId[id] = connector; 0113 m_idForConnector[connector] = id; 0114 } 0115 0116 int ScreenPool::id(const QString &connector) const 0117 { 0118 if (!m_idForConnector.contains(connector)) { 0119 //! return 0 for primary screen, -1 for not found 0120 return qGuiApp->primaryScreen()->name() == connector ? 0 : -1; 0121 } 0122 0123 return m_idForConnector.value(connector); 0124 } 0125 0126 QString ScreenPool::connector(int id) const 0127 { 0128 if (!m_connectorForId.contains(id)) { 0129 return id == 0 ? qGuiApp->primaryScreen()->name() : QString(); 0130 } 0131 0132 return m_connectorForId.value(id); 0133 } 0134 0135 0136 } 0137 }