Krita Source Code Documentation
Loading...
Searching...
No Matches
KisColord.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2012 Daniel Nicoletti <dantti12@gmail.com>
3 * SPDX-FileCopyrightText: 2015 Boudewijn Rempt <boud@valdyas.org>
4 *
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 */
7
8#include "KisColord.h"
9
10#include <klocalizedstring.h>
11#include <kis_debug.h>
12
13
14#include "CdInterface.h"
15#include "CdProfileInterface.h"
16#include "CdDeviceInterface.h"
17
18
19struct Profile {
20 QString kind;
21 QString filename;
22 QString title;
23 qulonglong created;
24 QString colorspace;
25};
26
27struct Device {
28
30 qDeleteAll(profiles);
31 profiles.clear();
32 }
33
34 QString id;
35 QString kind;
36 QString model;
37 QString vendor;
38 QString colorspace;
39
41
42};
43
44KisColord::KisColord(QObject *parent)
45 : QObject(parent)
46{
47 //dbgKrita << "Creating KisColorD";
48
49 m_cdInterface = new CdInterface(QLatin1String("org.freedesktop.ColorManager"),
50 QLatin1String("/org/freedesktop/ColorManager"),
51 QDBusConnection::systemBus(),
52 this);
53
54 // listen to colord for device events
55 connect(m_cdInterface, SIGNAL(DeviceAdded(QDBusObjectPath)),
56 this, SLOT(deviceAdded(QDBusObjectPath)));
57 connect(m_cdInterface, SIGNAL(DeviceRemoved(QDBusObjectPath)),
58 this, SLOT(deviceRemoved(QDBusObjectPath)));
59 connect(m_cdInterface, SIGNAL(DeviceChanged(QDBusObjectPath)),
60 this, SLOT(deviceChanged(QDBusObjectPath)));
61
62 // Ask for devices
63 QDBusPendingReply<QList<QDBusObjectPath> > async = m_cdInterface->GetDevices();
64 QDBusPendingCallWatcher *displayWatcher = new QDBusPendingCallWatcher(async, this);
65 connect(displayWatcher, SIGNAL(finished(QDBusPendingCallWatcher*)),
66 this, SLOT(gotDevices(QDBusPendingCallWatcher*)));
67
68
69 // Make sure we know is colord is running
70 QDBusServiceWatcher *watcher = new QDBusServiceWatcher("org.freedesktop.ColorManager",
71 QDBusConnection::systemBus(),
72 QDBusServiceWatcher::WatchForOwnerChange,
73 this);
74
75 connect(watcher, SIGNAL(serviceOwnerChanged(QString,QString,QString)),
76 this, SLOT(serviceOwnerChanged(QString,QString,QString)));
77}
78
80{
81 qDeleteAll(m_devices);
82 m_devices.clear();
83}
84
85QStringList KisColord::devices(const QString &type) const
86{
87 QStringList res;
88 Q_FOREACH (Device *dev, m_devices.values()) {
89 if (type == dev->kind) {
90 res << dev->id;
91 }
92 }
93 return res;
94}
95
96const QString KisColord::deviceName(const QString &id) const
97{
98 QString res;
99 Q_FOREACH (Device *dev, m_devices.values()) {
100 if (dev->id == id) {
101 res = dev->model + ", " + dev->vendor;
102 }
103 }
104 return res;
105}
106
107QByteArray KisColord::deviceProfile(const QString &id, int p)
108{
109 QByteArray ba;
110 Device *dev = 0;
111 Profile *profile = 0;
112 Q_FOREACH (Device *d, m_devices.values()) {
113 if (d->id == id) {
114 dev = d;
115 break;
116 }
117 }
118
119 if (dev) {
120 if (dev->profiles.size() > 0) {
121 if (dev->profiles.size() < p) {
122 profile = dev->profiles[p];
123 }
124 else {
125 profile = dev->profiles[0];
126 }
127 }
128
129 if (profile) {
130 //dbgKrita << "profile filename" << profile->filename;
131 QFile f(profile->filename);
132 if (f.open(QFile::ReadOnly)) {
133 ba = f.readAll();
134 }
135 else {
136 dbgKrita << "Could not load profile" << profile->title << profile->filename;
137 }
138 }
139 }
140
141 return ba;
142}
143
144
145
146void KisColord::serviceOwnerChanged(const QString &serviceName, const QString &oldOwner, const QString &newOwner)
147{
148 Q_UNUSED(serviceName);
149 if (newOwner.isEmpty() || oldOwner != newOwner) {
150 // colord has quit or restarted
151 qDeleteAll(m_devices);
152 m_devices.clear();
153 }
154 Q_EMIT changed();
155}
156
157void KisColord::gotDevices(QDBusPendingCallWatcher *call)
158{
159 //dbgKrita << "Got devices!!!";
160
161 QDBusPendingReply<QList<QDBusObjectPath> > reply = *call;
162 if (reply.isError()) {
163 dbgKrita << "Unexpected message" << reply.error().message();
164 } else {
165 QList<QDBusObjectPath> devices = reply.argumentAt<0>();
166 Q_FOREACH (const QDBusObjectPath &device, devices) {
167 deviceAdded(device, false);
168 }
169 Q_EMIT changed();
170 }
171 //dbgKrita << "gotDevices" << m_devices.count();
172 call->deleteLater();
173}
174
175void KisColord::deviceChanged(const QDBusObjectPath &objectPath)
176{
177 CdDeviceInterface device(QLatin1String("org.freedesktop.ColorManager"),
178 objectPath.path(),
179 QDBusConnection::systemBus());
180 if (!device.isValid()) {
181 return;
182 }
183
184 if (!m_devices.contains(objectPath)) {
185 //dbgKrita << "deviceChanged for an unknown device" << objectPath.path();
186 deviceAdded(objectPath, false);
187 return;
188 }
189
190 QList<QDBusObjectPath> profiles = device.profiles();
191
192 Device *dev = m_devices[objectPath];
193 qDeleteAll(dev->profiles);
194 dev->profiles.clear();
195
196 addProfilesToDevice(dev, profiles);
197
198 //dbgKrita << "deviceChanged" << dev->id << "with" << profiles.size() << "profiles";
199
200 Q_EMIT changed(dev->id);
201}
202
203void KisColord::deviceAdded(const QDBusObjectPath &objectPath, bool emitChanged)
204{
205 if (m_devices.contains(objectPath)) {
206 //dbgKrita << "Device is already on the list" << objectPath.path();
207 return;
208 }
209
210 CdDeviceInterface device(QLatin1String("org.freedesktop.ColorManager"),
211 objectPath.path(),
212 QDBusConnection::systemBus());
213 if (!device.isValid()) {
214 dbgKrita << "Got an invalid device" << objectPath.path();
215 return;
216 }
217
218 Device *dev = new Device;
219
220 dev->id = device.deviceId();
221 dev->kind = device.kind();
222 dev->model = device.model();
223 dev->vendor = device.vendor();
224 dev->colorspace = device.colorspace();
225
226 m_devices[objectPath] = dev;
227
228 QList<QDBusObjectPath> profiles = device.profiles();
229 addProfilesToDevice(dev, profiles);
230
231// dbgKrita << "deviceAdded" << dev->id
232// << dev->kind
233// << dev->model
234// << dev->vendor
235// << "with" << profiles.size() << "profiles";
236
237 if (emitChanged) {
238 Q_EMIT changed();
239 }
240}
241
242void KisColord::deviceRemoved(const QDBusObjectPath &objectPath)
243{
244 if (m_devices.contains(objectPath)) {
245 delete m_devices.take(objectPath);
246 }
247 Q_EMIT changed();
248}
249
251{
252
253 Q_FOREACH (const QDBusObjectPath &profileObjectPath, profiles) {
254
255 CdProfileInterface profile(QLatin1String("org.freedesktop.ColorManager"),
256 profileObjectPath.path(),
257 QDBusConnection::systemBus());
258 if (!profile.isValid()) {
259 return;
260 }
261
262
263 Profile *p = new Profile;
264
265 p->kind = profile.kind();
266 p->filename = profile.filename();
267 p->title = profile.title();
268 p->created = profile.created();
269 p->colorspace = profile.colorspace();
270
271 dev->profiles << p;
272 }
273}
274
const Params2D p
connect(this, SIGNAL(optionsChanged()), this, SLOT(saveOptions()))
QStringList devices(const QString &type) const
Definition KisColord.cpp:85
void gotDevices(QDBusPendingCallWatcher *call)
QByteArray deviceProfile(const QString &id, int profile)
CdInterface * m_cdInterface
Definition KisColord.h:65
void serviceOwnerChanged(const QString &serviceName, const QString &oldOwner, const QString &newOwner)
void deviceChanged(const QDBusObjectPath &objectPath)
void changed()
const QString deviceName(const QString &id) const
Definition KisColord.cpp:96
~KisColord() override
Definition KisColord.cpp:79
void deviceRemoved(const QDBusObjectPath &objectPath)
void addProfilesToDevice(Device *dev, QList< QDBusObjectPath > profiles) const
QMap< QDBusObjectPath, Device * > m_devices
Definition KisColord.h:63
KisColord(QObject *parent=0)
Definition KisColord.cpp:44
void deviceAdded(const QDBusObjectPath &objectPath, bool emitChanged=true)
#define dbgKrita
Definition kis_debug.h:45
QString vendor
Definition KisColord.cpp:37
QList< Profile * > profiles
Definition KisColord.cpp:40
QString model
Definition KisColord.cpp:36
QString id
Definition KisColord.cpp:34
QString kind
Definition KisColord.cpp:35
QString colorspace
Definition KisColord.cpp:38
QString colorspace
Definition KisColord.cpp:24
QString filename
Definition KisColord.cpp:21
QString kind
Definition KisColord.cpp:20
qulonglong created
Definition KisColord.cpp:23
QString title
Definition KisColord.cpp:22