1 /*
2     SPDX-FileCopyrightText: 2018 Michail Vourlakos <mvourlakos@gmail.com>
3     SPDX-License-Identifier: GPL-2.0-or-later
4 */
5 
6 #include "screenpool.h"
7 
8 // local
9 #include "../../tools/commontools.h"
10 
11 // Qt
12 #include <QDebug>
13 #include <QDir>
14 #include <QGuiApplication>
15 #include <QScreen>
16 
17 // KDE
18 #include <KConfigGroup>
19 #include <KDirWatch>
20 #include <KSharedConfig>
21 
22 #define PLASMARC "plasmashellrc"
23 
24 namespace Latte {
25 namespace PlasmaExtended {
26 
ScreenPool(QObject * parent)27 ScreenPool::ScreenPool(QObject *parent)
28     : QObject(parent)
29 {
30     KSharedConfigPtr plasmaPtr = KSharedConfig::openConfig(PLASMARC);
31     m_screensGroup = KConfigGroup(plasmaPtr, "ScreenConnectors");
32 
33     load();
34 
35     QString plasmaSettingsFile = Latte::configPath() + "/" + PLASMARC;
36 
37     KDirWatch::self()->addFile(plasmaSettingsFile);
38 
39     connect(KDirWatch::self(), &KDirWatch::dirty, this, [ &, plasmaSettingsFile](const QString & path) {
40         if (path == plasmaSettingsFile) {
41             load();
42         }
43     });
44 
45     connect(KDirWatch::self(), &KDirWatch::created, this, [ &, plasmaSettingsFile](const QString & path) {
46         if (path == plasmaSettingsFile) {
47             load();
48         }
49     });
50 }
51 
52 
~ScreenPool()53 ScreenPool::~ScreenPool()
54 {
55 }
56 
load()57 void ScreenPool::load()
58 {
59     QMap<int, QString> connectorForId = m_connectorForId;
60     QHash<QString, int> idForConnector = m_idForConnector;
61 
62     m_connectorForId.clear();
63     m_idForConnector.clear();
64 
65     bool updated{false};
66 
67     for (const auto &screenId : m_screensGroup.keyList()) {
68         QString screenName =  m_screensGroup.readEntry(screenId, QString());
69         if (screenId != 0) {
70             int scrId = screenId.toInt();
71             insertScreenMapping(scrId, screenName);
72 
73             if (!connectorForId.contains(scrId) || connectorForId[scrId] != m_connectorForId[scrId]) {
74                 updated = true;
75             }
76         }
77     }
78 
79     //! If there are changes then print the new plasma screen ids and send a relevant signal
80     if (connectorForId.count() != m_connectorForId.count()) {
81         updated = true;
82     }
83 
84     if (updated) {
85         qDebug() << "---------------- Plasma Screen Ids ------------------";
86         for (const auto &id : m_connectorForId.keys()) {
87             qDebug() << id << "  __  " << m_connectorForId[id];
88         }
89         qDebug() << "----------------  ---------------  ------------------";
90 
91         emit idsChanged();
92     }
93 }
94 
insertScreenMapping(int id,const QString & connector)95 void ScreenPool::insertScreenMapping(int id, const QString &connector)
96 {
97     if (id==0 || connector.startsWith(":")) {
98         return;
99     }
100 
101     m_connectorForId[id] = connector;
102     m_idForConnector[connector] = id;
103 }
104 
id(const QString & connector) const105 int ScreenPool::id(const QString &connector) const
106 {
107     if (!m_idForConnector.contains(connector)) {
108         //! return 0 for primary screen, -1 for not found
109         return qGuiApp->primaryScreen()->name() == connector ? 0 : -1;
110     }
111 
112     return m_idForConnector.value(connector);
113 }
114 
connector(int id) const115 QString ScreenPool::connector(int id) const
116 {
117     if (!m_connectorForId.contains(id)) {
118         return id == 0 ? qGuiApp->primaryScreen()->name() : QString();
119     }
120 
121     return m_connectorForId.value(id);
122 }
123 
124 
125 }
126 }
127