Debian Codegraph @

src/cd-device.c

raw
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
2 *
3 * Copyright (C) 2010-2012 Richard Hughes <[email protected]>
4 *
5 * Licensed under the GNU General Public License Version 2
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 */
21
22 #include "config.h"
23
24 #include <glib-object.h>
25 #include <gio/gio.h>
26 #include <sys/time.h>
27 #include <string.h>
28 #ifdef HAVE_PWD_H
29 #include <pwd.h>
30 #endif
31
32 #include "cd-common.h"
33 #include "cd-device.h"
34 #include "cd-mapping-db.h"
35 #include "cd-device-db.h"
36 #include "cd-profile-array.h"
37 #include "cd-profile.h"
38 #include "cd-inhibit.h"
39
40 static void cd_device_finalize (GObject *object);
41 static void cd_device_dbus_emit_property_changed (CdDevice *device,
42 const gchar *property_name,
43 GVariant *property_value);
44 static void cd_device_dbus_emit_device_changed (CdDevice *device);
45
46 #define GET_PRIVATE(o) (cd_device_get_instance_private (o))
47
48 typedef struct
49 {
50 CdObjectScope object_scope;
51 CdProfileArray *profile_array;
52 CdMappingDb *mapping_db;
53 CdDeviceDb *device_db;
54 CdInhibit *inhibit;
55 gchar *id;
56 gchar *model;
57 gchar *serial;
58 gchar *vendor;
59 gchar *colorspace;
60 gchar *format;
61 gchar *mode;
62 CdDeviceKind kind;
63 gchar *object_path;
64 GDBusConnection *connection;
65 GPtrArray *profiles; /* of CdDeviceProfileItem */
66 guint registration_id;
67 guint watcher_id;
68 guint64 created;
69 guint64 modified;
70 gboolean require_modified_signal;
71 gboolean is_virtual;
72 gboolean enabled;
73 gboolean embedded;
74 GHashTable *metadata;
75 guint owner;
76 gchar *seat;
77 } CdDevicePrivate;
78
79 enum {
80 SIGNAL_INVALIDATE,
81 SIGNAL_LAST
82 };
83
84 enum {
85 PROP_0,
86 PROP_OBJECT_PATH,
87 PROP_ID,
88 PROP_LAST
89 };
90
91 typedef struct {
92 CdProfile *profile;
93 CdDeviceRelation relation;
94 guint64 timestamp;
95 } CdDeviceProfileItem;
96
97 static guint signals[SIGNAL_LAST] = { 0 };
98 G_DEFINE_TYPE_WITH_PRIVATE (CdDevice, cd_device, G_TYPE_OBJECT)
99
100 GQuark
101 cd_device_error_quark (void)
102 {
103 guint i;
104 static GQuark quark = 0;
105 if (!quark) {
106 quark = g_quark_from_static_string ("CdDevice");
107 for (i = 0; i < CD_DEVICE_ERROR_LAST; i++) {
108 g_dbus_error_register_error (quark,
109 i,
110 cd_device_error_to_string (i));
111 }
112 }
113 return quark;
114 }
115
116 CdObjectScope
117 cd_device_get_scope (CdDevice *device)
118 {
119 CdDevicePrivate *priv = GET_PRIVATE (device);
120 g_return_val_if_fail (CD_IS_DEVICE (device), 0);
121 return priv->object_scope;
122 }
123
124 void
125 cd_device_set_scope (CdDevice *device, CdObjectScope object_scope)
126 {
127 CdDevicePrivate *priv = GET_PRIVATE (device);
128 g_return_if_fail (CD_IS_DEVICE (device));
129 priv->object_scope = object_scope;
130 }
131
132 guint
133 cd_device_get_owner (CdDevice *device)
134 {
135 CdDevicePrivate *priv = GET_PRIVATE (device);
136 g_return_val_if_fail (CD_IS_DEVICE (device), G_MAXUINT);
137 return priv->owner;
138 }
139
140 void
141 cd_device_set_owner (CdDevice *device, guint owner)
142 {
143 CdDevicePrivate *priv = GET_PRIVATE (device);
144 g_return_if_fail (CD_IS_DEVICE (device));
145 priv->owner = owner;
146 }
147
148 const gchar *
149 cd_device_get_seat (CdDevice *device)
150 {
151 CdDevicePrivate *priv = GET_PRIVATE (device);
152 g_return_val_if_fail (CD_IS_DEVICE (device), NULL);
153 return priv->seat;
154 }
155
156 void
157 cd_device_set_seat (CdDevice *device, const gchar *seat)
158 {
159 CdDevicePrivate *priv = GET_PRIVATE (device);
160 g_return_if_fail (CD_IS_DEVICE (device));
161 priv->seat = g_strdup (seat);
162 }
163
164 static const gchar *
165 _cd_device_mode_to_string (CdDeviceMode device_mode)
166 {
167 if (device_mode == CD_DEVICE_MODE_PHYSICAL)
168 return "physical";
169 if (device_mode == CD_DEVICE_MODE_VIRTUAL)
170 return "virtual";
171 return "unknown";
172 }
173
174 static CdDeviceMode
175 _cd_device_mode_from_string (const gchar *device_mode)
176 {
177 if (g_strcmp0 (device_mode, "physical") == 0)
178 return CD_DEVICE_MODE_PHYSICAL;
179 if (g_strcmp0 (device_mode, "virtual") == 0)
180 return CD_DEVICE_MODE_VIRTUAL;
181 return CD_DEVICE_MODE_UNKNOWN;
182 }
183
184 void
185 cd_device_set_mode (CdDevice *device, CdDeviceMode mode)
186 {
187 CdDevicePrivate *priv = GET_PRIVATE (device);
188 g_return_if_fail (CD_IS_DEVICE (device));
189 g_free (priv->mode);
190 priv->mode = g_strdup (_cd_device_mode_to_string (mode));
191 }
192
193 CdDeviceMode
194 cd_device_get_mode (CdDevice *device)
195 {
196 CdDevicePrivate *priv = GET_PRIVATE (device);
197 g_return_val_if_fail (CD_IS_DEVICE (device), CD_DEVICE_MODE_UNKNOWN);
198 return _cd_device_mode_from_string (priv->mode);
199 }
200
201 const gchar *
202 cd_device_get_object_path (CdDevice *device)
203 {
204 CdDevicePrivate *priv = GET_PRIVATE (device);
205 g_return_val_if_fail (CD_IS_DEVICE (device), NULL);
206 return priv->object_path;
207 }
208
209 const gchar *
210 cd_device_get_id (CdDevice *device)
211 {
212 CdDevicePrivate *priv = GET_PRIVATE (device);
213 g_return_val_if_fail (CD_IS_DEVICE (device), NULL);
214 return priv->id;
215 }
216
217 const gchar *
218 cd_device_get_model (CdDevice *device)
219 {
220 CdDevicePrivate *priv = GET_PRIVATE (device);
221 g_return_val_if_fail (CD_IS_DEVICE (device), NULL);
222 return priv->model;
223 }
224
225 CdDeviceKind
226 cd_device_get_kind (CdDevice *device)
227 {
228 CdDevicePrivate *priv = GET_PRIVATE (device);
229 g_return_val_if_fail (CD_IS_DEVICE (device), CD_DEVICE_KIND_UNKNOWN);
230 return priv->kind;
231 }
232
233 void
234 cd_device_set_kind (CdDevice *device, CdDeviceKind kind)
235 {
236 CdDevicePrivate *priv = GET_PRIVATE (device);
237 g_return_if_fail (CD_IS_DEVICE (device));
238 g_return_if_fail (kind != CD_DEVICE_KIND_UNKNOWN);
239 priv->kind = kind;
240 }
241
242 static void
243 cd_device_set_object_path (CdDevice *device)
244 {
245 CdDevicePrivate *priv = GET_PRIVATE (device);
246 #ifdef HAVE_PWD_H
247 struct passwd *pw;
248 #endif
249 g_autofree gchar *path_owner = NULL;
250 g_autofree gchar *path_tmp = NULL;
251
252 /* append the uid to the object path */
253 #ifdef HAVE_PWD_H
254 pw = getpwuid (priv->owner);
255 if (priv->owner == 0 ||
256 g_strcmp0 (pw->pw_name, DAEMON_USER) == 0) {
257 path_tmp = g_strdup (priv->id);
258 } else {
259 path_tmp = g_strdup_printf ("%s_%s_%u",
260 priv->id,
261 pw->pw_name,
262 priv->owner);
263 }
264 #else
265 if (priv->owner == 0) {
266 path_tmp = g_strdup (priv->id);
267 } else {
268 path_tmp = g_strdup_printf ("%s_%d",
269 priv->id,
270 priv->owner);
271 }
272 #endif
273
274 /* make sure object path is sane */
275 path_owner = cd_main_ensure_dbus_path (path_tmp);
276 priv->object_path = g_build_filename (COLORD_DBUS_PATH,
277 "devices",
278 path_owner,
279 NULL);
280 }
281
282 void
283 cd_device_set_id (CdDevice *device, const gchar *id)
284 {
285 CdDevicePrivate *priv = GET_PRIVATE (device);
286 g_autofree gchar *enabled_str = NULL;
287
288 g_return_if_fail (CD_IS_DEVICE (device));
289
290 g_free (priv->id);
291 priv->id = g_strdup (id);
292
293 /* now calculate this again */
294 cd_device_set_object_path (device);
295
296 /* find initial enabled state */
297 enabled_str = cd_device_db_get_property (priv->device_db,
298 priv->id,
299 "Enabled",
300 NULL);
301 if (g_strcmp0 (enabled_str, "False") == 0) {
302 g_debug ("%s disabled by db at load", id);
303 priv->enabled = FALSE;
304 } else {
305 priv->enabled = TRUE;
306 }
307 }
308
309 static void
310 cd_device_reset_modified (CdDevice *device)
311 {
312 CdDevicePrivate *priv = GET_PRIVATE (device);
313 g_debug ("CdDevice: set device Modified");
314 priv->modified = g_get_real_time ();
315 priv->require_modified_signal = TRUE;
316 }
317
318 static void
319 cd_device_dbus_emit_property_changed (CdDevice *device,
320 const gchar *property_name,
321 GVariant *property_value)
322 {
323 CdDevicePrivate *priv = GET_PRIVATE (device);
324 GVariantBuilder builder;
325 GVariantBuilder invalidated_builder;
326
327 /* not yet connected */
328 if (priv->connection == NULL)
329 return;
330
331 /* build the dict */
332 g_variant_builder_init (&invalidated_builder, G_VARIANT_TYPE ("as"));
333 g_variant_builder_init (&builder, G_VARIANT_TYPE_ARRAY);
334 g_variant_builder_add (&builder,
335 "{sv}",
336 property_name,
337 property_value);
338 if (priv->require_modified_signal) {
339 g_variant_builder_add (&builder,
340 "{sv}",
341 CD_DEVICE_PROPERTY_MODIFIED,
342 g_variant_new_uint64 (priv->modified));
343 priv->require_modified_signal = FALSE;
344 }
345 g_dbus_connection_emit_signal (priv->connection,
346 NULL,
347 priv->object_path,
348 "org.freedesktop.DBus.Properties",
349 "PropertiesChanged",
350 g_variant_new ("(sa{sv}as)",
351 COLORD_DBUS_INTERFACE_DEVICE,
352 &builder,
353 &invalidated_builder),
354 NULL);
355 g_variant_builder_clear (&builder);
356 g_variant_builder_clear (&invalidated_builder);
357 }
358
359 static void
360 cd_device_dbus_emit_device_changed (CdDevice *device)
361 {
362 CdDevicePrivate *priv = GET_PRIVATE (device);
363
364 /* not yet connected */
365 if (priv->connection == NULL)
366 return;
367
368 /* emit signal */
369 g_debug ("CdDevice: emit Changed on %s",
370 cd_device_get_object_path (device));
371 g_dbus_connection_emit_signal (priv->connection,
372 NULL,
373 cd_device_get_object_path (device),
374 COLORD_DBUS_INTERFACE_DEVICE,
375 "Changed",
376 NULL,
377 NULL);
378
379 /* emit signal */
380 g_debug ("CdDevice: emit Changed");
381 g_dbus_connection_emit_signal (priv->connection,
382 NULL,
383 COLORD_DBUS_PATH,
384 COLORD_DBUS_INTERFACE,
385 "DeviceChanged",
386 g_variant_new ("(o)",
387 cd_device_get_object_path (device)),
388 NULL);
389 }
390
391 static gboolean
392 cd_device_match_qualifier (const gchar *qual1, const gchar *qual2)
393 {
394 guint i;
395 g_auto(GStrv) split1 = NULL;
396 g_auto(GStrv) split2 = NULL;
397
398 /* split into substring */
399 split1 = g_strsplit (qual1, ".", 3);
400 split2 = g_strsplit (qual2, ".", 3);
401
402 /* ensure all substrings match */
403 for (i = 0; i < 3; i++) {
404
405 /* wildcard in query */
406 if (g_strcmp0 (split1[i], "*") == 0)
407 continue;
408
409 /* wildcard in qualifier */
410 if (g_strcmp0 (split2[i], "*") == 0)
411 continue;
412
413 /* exact match */
414 if (g_strcmp0 (split1[i], split2[i]) == 0)
415 continue;
416
417 /* failed to match substring */
418 return FALSE;
419 }
420
421 /* success */
422 return TRUE;
423 }
424
425 static CdProfile *
426 cd_device_find_by_qualifier (const gchar *regex,
427 GPtrArray *array,
428 CdDeviceRelation relation)
429 {
430 CdDeviceProfileItem *item;
431 const gchar *qualifier;
432 gboolean ret;
433 guint i;
434
435 /* find using a wildcard */
436 for (i = 0; i < array->len; i++) {
437 item = g_ptr_array_index (array, i);
438 if (item->relation != relation)
439 continue;
440
441 /* '*' matches anything, including a blank qualifier */
442 if (g_strcmp0 (regex, "*") == 0) {
443 g_debug ("anything matches, returning %s",
444 cd_profile_get_id (item->profile));
445 return item->profile;
446 }
447
448 /* match with a regex */
449 qualifier = cd_profile_get_qualifier (item->profile);
450 if (qualifier == NULL) {
451 g_debug ("no qualifier for %s, skipping",
452 cd_profile_get_id (item->profile));
453 continue;
454 }
455 ret = cd_device_match_qualifier (regex,
456 qualifier);
457 g_debug ("%s regex '%s' for '%s'",
458 ret ? "matched" : "unmatched",
459 regex,
460 qualifier);
461 if (ret)
462 return item->profile;
463 }
464 return NULL;
465 }
466
467 static CdProfile *
468 cd_device_find_profile_by_object_path (GPtrArray *array, const gchar *object_path)
469 {
470 CdDeviceProfileItem *item;
471 gboolean ret;
472 guint i;
473
474 /* find using an object path */
475 for (i = 0; i < array->len; i++) {
476 item = g_ptr_array_index (array, i);
477 ret = (g_strcmp0 (object_path,
478 cd_profile_get_object_path (item->profile)) == 0);
479 if (ret)
480 return item->profile;
481 }
482 return NULL;
483 }
484
485 static GVariant *
486 cd_device_get_profiles_as_variant (CdDevice *device)
487 {
488 CdDevicePrivate *priv = GET_PRIVATE (device);
489 CdDeviceProfileItem *item;
490 const gchar *tmp;
491 guint i;
492 guint idx = 0;
493 g_autofree GVariant **profiles = NULL;
494
495 /* Object paths are assembled in this order:
496 *
497 * 1. Hard mapped profiles from the database
498 * 2. Soft mapped profiles of DATA_source != EDID
499 * 2. Soft mapped profiles of DATA_source == EDID
500 */
501 profiles = g_new0 (GVariant *, priv->profiles->len + 1);
502 for (i = 0; i < priv->profiles->len; i++) {
503 item = g_ptr_array_index (priv->profiles, i);
504 if (item->relation != CD_DEVICE_RELATION_HARD)
505 continue;
506 tmp = cd_profile_get_object_path (item->profile);
507 profiles[idx++] = g_variant_new_object_path (tmp);
508 }
509 for (i = 0; i < priv->profiles->len; i++) {
510 item = g_ptr_array_index (priv->profiles, i);
511 if (item->relation != CD_DEVICE_RELATION_SOFT)
512 continue;
513 tmp = cd_profile_get_metadata_item (item->profile,
514 CD_PROFILE_METADATA_DATA_SOURCE);
515 if (g_strcmp0 (tmp, CD_PROFILE_METADATA_DATA_SOURCE_EDID) == 0)
516 continue;
517 tmp = cd_profile_get_object_path (item->profile);
518 profiles[idx++] = g_variant_new_object_path (tmp);
519 }
520 for (i = 0; i < priv->profiles->len; i++) {
521 item = g_ptr_array_index (priv->profiles, i);
522 if (item->relation != CD_DEVICE_RELATION_SOFT)
523 continue;
524 tmp = cd_profile_get_metadata_item (item->profile,
525 CD_PROFILE_METADATA_DATA_SOURCE);
526 if (g_strcmp0 (tmp, CD_PROFILE_METADATA_DATA_SOURCE_EDID) != 0)
527 continue;
528 tmp = cd_profile_get_object_path (item->profile);
529 profiles[idx++] = g_variant_new_object_path (tmp);
530 }
531
532 /* format the value */
533 return g_variant_new_array (G_VARIANT_TYPE_OBJECT_PATH,
534 profiles,
535 priv->profiles->len);
536 }
537
538 gboolean
539 cd_device_remove_profile (CdDevice *device,
540 const gchar *profile_object_path,
541 GError **error)
542 {
543 CdDevicePrivate *priv = GET_PRIVATE (device);
544 CdDeviceProfileItem *item;
545 gboolean ret = FALSE;
546 guint i;
547
548 /* check the profile exists on this device */
549 for (i = 0; i < priv->profiles->len; i++) {
550 item = g_ptr_array_index (priv->profiles, i);
551 if (g_strcmp0 (profile_object_path,
552 cd_profile_get_object_path (item->profile)) == 0) {
553 ret = TRUE;
554 break;
555 }
556 }
557 if (!ret) {
558 g_set_error (error,
559 CD_DEVICE_ERROR,
560 CD_DEVICE_ERROR_PROFILE_DOES_NOT_EXIST,
561 "profile object path '%s' does not exist on '%s'",
562 profile_object_path,
563 priv->object_path);
564 return FALSE;
565 }
566
567 /* remove from the arrays */
568 ret = g_ptr_array_remove (priv->profiles, item);
569 g_assert (ret);
570
571 /* reset modification time */
572 cd_device_reset_modified (device);
573
574 /* emit */
575 cd_device_dbus_emit_property_changed (device,
576 "Profiles",
577 cd_device_get_profiles_as_variant (device));
578
579 /* emit global signal */
580 cd_device_dbus_emit_device_changed (device);
581 return TRUE;
582 }
583
584 static CdDeviceRelation
585 cd_device_find_profile_relation (CdDevice *device,
586 const gchar *profile_object_path)
587 {
588 CdDevicePrivate *priv = GET_PRIVATE (device);
589 CdDeviceProfileItem *item;
590 guint i;
591
592 /* search profiles */
593 for (i = 0; i < priv->profiles->len; i++) {
594 item = g_ptr_array_index (priv->profiles, i);
595 if (g_strcmp0 (profile_object_path,
596 cd_profile_get_object_path (item->profile)) == 0) {
597 return item->relation;
598 }
599 }
600 return CD_DEVICE_RELATION_UNKNOWN;
601 }
602
603 static const gchar *
604 _cd_device_relation_to_string (CdDeviceRelation device_relation)
605 {
606 if (device_relation == CD_DEVICE_RELATION_HARD)
607 return "hard";
608 if (device_relation == CD_DEVICE_RELATION_SOFT)
609 return "soft";
610 return "unknown";
611 }
612
613 static gint
614 cd_device_profile_item_sort_cb (gconstpointer a, gconstpointer b)
615 {
616 gint64 tmp;
617 CdDeviceProfileItem **item_a = (CdDeviceProfileItem **) a;
618 CdDeviceProfileItem **item_b = (CdDeviceProfileItem **) b;
619
620 tmp = (gint64) (*item_b)->timestamp - (gint64) (*item_a)->timestamp;
621 if (tmp < 0)
622 return -1;
623 if (tmp > 0)
624 return 1;
625 return 0;
626 }
627
628 gboolean
629 cd_device_add_profile (CdDevice *device,
630 CdDeviceRelation relation,
631 const gchar *profile_object_path,
632 guint64 timestamp,
633 GError **error)
634 {
635 CdDevicePrivate *priv = GET_PRIVATE (device);
636 CdDeviceProfileItem *item;
637 gboolean create_item = TRUE;
638 guint i;
639 g_autoptr(CdProfile) profile = NULL;
640
641 /* is it available */
642 profile = cd_profile_array_get_by_object_path (priv->profile_array,
643 profile_object_path);
644 if (profile == NULL) {
645 g_set_error (error,
646 CD_DEVICE_ERROR,
647 CD_DEVICE_ERROR_PROFILE_DOES_NOT_EXIST,
648 "profile object path '%s' does not exist",
649 profile_object_path);
650 return FALSE;
651 }
652
653 /* check it does not already exist */
654 for (i = 0; i < priv->profiles->len; i++) {
655 item = g_ptr_array_index (priv->profiles, i);
656 if (g_strcmp0 (cd_profile_get_object_path (profile),
657 cd_profile_get_object_path (item->profile)) == 0) {
658
659 /* if we soft added this profile, and now the
660 * user hard adds it as well then we need to
661 * change the kind and not re-add it */
662 if (relation == CD_DEVICE_RELATION_HARD &&
663 item->relation == CD_DEVICE_RELATION_SOFT) {
664 g_debug ("CdDevice: converting %s hard->soft",
665 cd_profile_get_id (profile));
666 item->relation = CD_DEVICE_RELATION_HARD;
667 create_item = FALSE;
668 break;
669 }
670 g_set_error (error,
671 CD_DEVICE_ERROR,
672 CD_DEVICE_ERROR_PROFILE_ALREADY_ADDED,
673 "profile object path '%s' has already been added",
674 profile_object_path);
675 return FALSE;
676 }
677 }
678
679 /* add to the array */
680 if (create_item) {
681 g_debug ("Adding %s [%s] to %s",
682 cd_profile_get_id (profile),
683 _cd_device_relation_to_string (relation),
684 priv->id);
685 item = g_new0 (CdDeviceProfileItem, 1);
686 item->profile = g_object_ref (profile);
687 item->relation = relation;
688 item->timestamp = timestamp;
689 g_ptr_array_add (priv->profiles, item);
690 g_ptr_array_sort (priv->profiles,
691 cd_device_profile_item_sort_cb);
692 }
693
694 /* reset modification time */
695 cd_device_reset_modified (device);
696
697 /* emit */
698 cd_device_dbus_emit_property_changed (device,
699 "Profiles",
700 cd_device_get_profiles_as_variant (device));
701
702 /* emit global signal */
703 cd_device_dbus_emit_device_changed (device);
704 return TRUE;
705 }
706
707 static void
708 cd_device_set_property_to_db (CdDevice *device,
709 const gchar *property,
710 const gchar *value)
711 {
712 CdDevicePrivate *priv = GET_PRIVATE (device);
713 gboolean ret;
714 g_autoptr(GError) error = NULL;
715
716 if (priv->object_scope != CD_OBJECT_SCOPE_DISK)
717 return;
718
719 ret = cd_device_db_set_property (priv->device_db,
720 priv->id,
721 property,
722 value,
723 &error);
724 if (!ret) {
725 g_warning ("CdDevice: failed to save property to database: %s",
726 error->message);
727 }
728 }
729
730 static GVariant *
731 cd_device_get_metadata_as_variant (CdDevice *device)
732 {
733 CdDevicePrivate *priv = GET_PRIVATE (device);
734 GList *l;
735 GVariantBuilder builder;
736 g_autoptr(GList) list = NULL;
737
738 /* do not try to build an empty array */
739 if (g_hash_table_size (priv->metadata) == 0)
740 return g_variant_new_array (G_VARIANT_TYPE ("{ss}"), NULL, 0);
741
742 /* add all the keys in the dictionary to the variant builder */
743 list = g_hash_table_get_keys (priv->metadata);
744 g_variant_builder_init (&builder, G_VARIANT_TYPE_ARRAY);
745 for (l = list; l != NULL; l = l->next) {
746 g_variant_builder_add (&builder,
747 "{ss}",
748 l->data,
749 g_hash_table_lookup (priv->metadata,
750 l->data));
751 }
752 return g_variant_builder_end (&builder);
753 }
754
755 static void
756 cd_device_string_remove_suffix (gchar *vendor, const gchar *suffix)
757 {
758 g_strchomp (vendor);
759 if (g_str_has_suffix (vendor, suffix)) {
760 gint len, suffix_len;
761 len = strlen (vendor);
762 suffix_len = strlen (suffix);
763 vendor[len - suffix_len] = '\0';
764 }
765 g_strchomp (vendor);
766 }
767
768 static void
769 cd_device_set_vendor (CdDevice *device, const gchar *vendor)
770 {
771 CdDevicePrivate *priv = GET_PRIVATE (device);
772 g_free (priv->vendor);
773 priv->vendor = cd_quirk_vendor_name (vendor);
774 }
775
776 static void
777 cd_device_set_model (CdDevice *device, const gchar *model)
778 {
779 CdDevicePrivate *priv = GET_PRIVATE (device);
780 GString *tmp;
781
782 /* remove insanities */
783 tmp = g_string_new (model);
784
785 /* remove the kind suffix */
786 if (priv->kind == CD_DEVICE_KIND_PRINTER)
787 cd_device_string_remove_suffix (tmp->str, "Printer");
788 if (priv->kind == CD_DEVICE_KIND_DISPLAY) {
789 cd_device_string_remove_suffix (tmp->str, "Monitor");
790 cd_device_string_remove_suffix (tmp->str, "Screen");
791 }
792
793 /* okay, we're done now */
794 g_free (priv->model);
795 priv->model = g_string_free (tmp, FALSE);
796 }
797
798 static GVariant *
799 cd_device_get_nullable_for_string (const gchar *value)
800 {
801 if (value == NULL)
802 return g_variant_new_string ("");
803 return g_variant_new_string (value);
804 }
805
806 static void
807 cd_device_set_serial (CdDevice *device, const gchar *value)
808 {
809 CdDevicePrivate *priv = GET_PRIVATE (device);
810 gchar *tmp;
811
812 /* CUPS likes to hand us a serial with a URI prepended */
813 g_free (priv->serial);
814 tmp = g_strstr_len (value, -1, "?serial=");
815 if (tmp != NULL) {
816 priv->serial = g_strdup (tmp + 8);
817 return;
818 }
819 priv->serial = g_strdup (value);
820 }
821
822 gboolean
823 cd_device_set_property_internal (CdDevice *device,
824 const gchar *property,
825 const gchar *value,
826 gboolean save_in_db,
827 GError **error)
828 {
829 gboolean is_metadata = FALSE;
830 CdDevicePrivate *priv = GET_PRIVATE (device);
831
832 /* sanity check the length of the key and value */
833 if (strlen (property) > CD_DBUS_METADATA_KEY_LEN_MAX) {
834 g_set_error_literal (error,
835 CD_CLIENT_ERROR,
836 CD_CLIENT_ERROR_INPUT_INVALID,
837 "metadata key length invalid");
838 return FALSE;
839 }
840 if (value != NULL && strlen (value) > CD_DBUS_METADATA_VALUE_LEN_MAX) {
841 g_set_error_literal (error,
842 CD_CLIENT_ERROR,
843 CD_CLIENT_ERROR_INPUT_INVALID,
844 "metadata value length invalid");
845 return FALSE;
846 }
847
848 g_debug ("CdDevice: Attempting to set %s to %s on %s",
849 property, value, priv->id);
850 if (g_strcmp0 (property, CD_DEVICE_PROPERTY_MODEL) == 0) {
851 cd_device_set_model (device, value);
852 } else if (g_strcmp0 (property, CD_DEVICE_PROPERTY_KIND) == 0) {
853 priv->kind = cd_device_kind_from_string (value);
854 } else if (g_strcmp0 (property, CD_DEVICE_PROPERTY_VENDOR) == 0) {
855 cd_device_set_vendor (device, value);
856 } else if (g_strcmp0 (property, CD_DEVICE_PROPERTY_SERIAL) == 0) {
857 cd_device_set_serial (device, value);
858 } else if (g_strcmp0 (property, CD_DEVICE_PROPERTY_COLORSPACE) == 0) {
859 g_free (priv->colorspace);
860 priv->colorspace = g_strdup (value);
861 } else if (g_strcmp0 (property, CD_DEVICE_PROPERTY_FORMAT) == 0) {
862 g_free (priv->format);
863 priv->format = g_strdup (value);
864 } else if (g_strcmp0 (property, CD_DEVICE_PROPERTY_MODE) == 0) {
865 g_free (priv->mode);
866 priv->mode = g_strdup (value);
867 } else if (g_strcmp0 (property, CD_DEVICE_PROPERTY_SEAT) == 0) {
868 g_free (priv->seat);
869 priv->seat = g_strdup (value);
870 } else if (g_strcmp0 (property, CD_DEVICE_PROPERTY_EMBEDDED) == 0) {
871 priv->embedded = TRUE;
872 } else {
873 /* add to metadata */
874 is_metadata = TRUE;
875 g_hash_table_insert (priv->metadata,
876 g_strdup (property),
877 g_strdup (value));
878 cd_device_dbus_emit_property_changed (device,
879 CD_DEVICE_PROPERTY_METADATA,
880 cd_device_get_metadata_as_variant (device));
881 }
882
883 /* set this externally so we can add disk devices at startup
884 * without re-adding */
885 if (save_in_db) {
886 cd_device_set_property_to_db (device,
887 property,
888 value);
889 }
890
891 /* if a known property, emit the correct property changed signal */
892 if (!is_metadata) {
893 cd_device_dbus_emit_property_changed (device,
894 property,
895 cd_device_get_nullable_for_string (value));
896 }
897 return TRUE;
898 }
899
900 const gchar *
901 cd_device_get_metadata (CdDevice *device, const gchar *key)
902 {
903 CdDevicePrivate *priv = GET_PRIVATE (device);
904 if (g_strcmp0 (key, CD_DEVICE_PROPERTY_MODEL) == 0)
905 return priv->model;
906 if (g_strcmp0 (key, CD_DEVICE_PROPERTY_VENDOR) == 0)
907 return priv->vendor;
908 if (g_strcmp0 (key, CD_DEVICE_PROPERTY_SERIAL) == 0)
909 return priv->serial;
910 return g_hash_table_lookup (priv->metadata, key);
911 }
912
913 gboolean
914 cd_device_make_default (CdDevice *device,
915 const gchar *profile_object_path,
916 GError **error)
917 {
918 CdDevicePrivate *priv = GET_PRIVATE (device);
919 CdDeviceProfileItem *item;
920 CdProfile *profile;
921 guint i;
922
923 /* find profile */
924 profile = cd_device_find_profile_by_object_path (priv->profiles,
925 profile_object_path);
926 if (profile == NULL) {
927 g_set_error (error,
928 CD_DEVICE_ERROR,
929 CD_DEVICE_ERROR_PROFILE_DOES_NOT_EXIST,
930 "profile object path '%s' does not exist for this device",
931 profile_object_path);
932 return FALSE;
933 }
934
935 /* make the profile first in the array */
936 for (i=1; i<priv->profiles->len; i++) {
937 item = g_ptr_array_index (priv->profiles, i);
938 if (item->profile == profile) {
939 item->timestamp = g_get_real_time ();
940 item->relation = CD_DEVICE_RELATION_HARD;
941 g_ptr_array_sort (priv->profiles,
942 cd_device_profile_item_sort_cb);
943 break;
944 }
945 }
946
947 /* reset modification time */
948 cd_device_reset_modified (device);
949
950 /* emit */
951 cd_device_dbus_emit_property_changed (device,
952 "Profiles",
953 cd_device_get_profiles_as_variant (device));
954
955 /* emit global signal */
956 cd_device_dbus_emit_device_changed (device);
957 return TRUE;
958 }
959
960 static gboolean
961 cd_device_set_enabled (CdDevice *device,
962 gboolean enabled,
963 GError **error)
964 {
965 CdDevicePrivate *priv = GET_PRIVATE (device);
966 gboolean ret;
967 g_autoptr(GError) error_local = NULL;
968
969 /* device is already the correct state */
970 if (priv->enabled == enabled)
971 return TRUE;
972
973 /* update database */
974 ret = cd_device_db_set_property (priv->device_db,
975 priv->id,
976 "Enabled",
977 enabled ? "True" : "False",
978 &error_local);
979 if (!ret) {
980 g_set_error (error,
981 CD_DEVICE_ERROR,
982 CD_DEVICE_ERROR_INTERNAL,
983 "%s", error_local->message);
984 return FALSE;
985 }
986
987 /* change property */
988 priv->enabled = enabled;
989
990 /* reset modification time */
991 cd_device_reset_modified (device);
992
993 /* emit */
994 cd_device_dbus_emit_property_changed (device,
995 "Enabled",
996 g_variant_new_boolean (enabled));
997
998 /* emit global signal */
999 cd_device_dbus_emit_device_changed (device);
1000 return TRUE;
1001 }
1002
1003 static void
1004 cd_device_dbus_method_call (GDBusConnection *connection, const gchar *sender,
1005 const gchar *object_path, const gchar *interface_name,
1006 const gchar *method_name, GVariant *parameters,
1007 GDBusMethodInvocation *invocation, gpointer user_data)
1008 {
1009 CdDevice *device = CD_DEVICE (user_data);
1010 CdDevicePrivate *priv = GET_PRIVATE (device);
1011 CdDeviceRelation relation = CD_DEVICE_RELATION_UNKNOWN;
1012 CdProfile *profile = NULL;
1013 GVariant *tuple = NULL;
1014 GVariant *value = NULL;
1015 const gchar *id;
1016 const gchar *profile_object_path = NULL;
1017 const gchar *property_name = NULL;
1018 const gchar *property_value = NULL;
1019 gboolean ret;
1020 guint i = 0;
1021 g_autoptr(GError) error = NULL;
1022
1023 /* return '' */
1024 if (g_strcmp0 (method_name, "AddProfile") == 0) {
1025
1026 /* require auth */
1027 ret = cd_main_sender_authenticated (connection,
1028 sender,
1029 "org.freedesktop.color-manager.modify-device",
1030 &error);
1031 if (!ret) {
1032 g_dbus_method_invocation_return_error (invocation,
1033 CD_DEVICE_ERROR,
1034 CD_DEVICE_ERROR_FAILED_TO_AUTHENTICATE,
1035 "%s", error->message);
1036 return;
1037 }
1038
1039 /* check the profile_object_path exists */
1040 g_variant_get (parameters, "(&s&o)",
1041 &property_value,
1042 &profile_object_path);
1043 g_debug ("CdDevice %s:AddProfile(%s)",
1044 sender, profile_object_path);
1045
1046 /* convert the device->profile relationship into an enum */
1047 if (g_strcmp0 (property_value, "soft") == 0)
1048 relation = CD_DEVICE_RELATION_SOFT;
1049 else if (g_strcmp0 (property_value, "hard") == 0)
1050 relation = CD_DEVICE_RELATION_HARD;
1051
1052 /* nothing valid */
1053 if (relation == CD_DEVICE_RELATION_UNKNOWN) {
1054 g_dbus_method_invocation_return_error (invocation,
1055 CD_DEVICE_ERROR,
1056 CD_DEVICE_ERROR_INTERNAL,
1057 "relation '%s' unknown, expected 'hard' or 'soft'",
1058 property_value);
1059 return;
1060 }
1061
1062 /* add it */
1063 ret = cd_device_add_profile (device,
1064 relation,
1065 profile_object_path,
1066 g_get_real_time (),
1067 &error);
1068 if (!ret) {
1069 g_dbus_method_invocation_return_gerror (invocation, error);
1070 return;
1071 }
1072
1073 /* get profile id from object path */
1074 profile = cd_profile_array_get_by_object_path (priv->profile_array,
1075 profile_object_path);
1076 id = cd_profile_get_id (profile);
1077 g_object_unref (profile);
1078
1079 /* save this to the permanent database */
1080 if (relation == CD_DEVICE_RELATION_HARD) {
1081 ret = cd_mapping_db_add (priv->mapping_db,
1082 priv->id,
1083 id,
1084 &error);
1085 if (!ret) {
1086 g_warning ("CdDevice: failed to save mapping to database: %s",
1087 error->message);
1088 }
1089 }
1090
1091 g_dbus_method_invocation_return_value (invocation, NULL);
1092 return;
1093 }
1094
1095 if (g_strcmp0 (method_name, "RemoveProfile") == 0) {
1096
1097 /* require auth */
1098 ret = cd_main_sender_authenticated (connection,
1099 sender,
1100 "org.freedesktop.color-manager.modify-device",
1101 &error);
1102 if (!ret) {
1103 g_dbus_method_invocation_return_error (invocation,
1104 CD_DEVICE_ERROR,
1105 CD_DEVICE_ERROR_FAILED_TO_AUTHENTICATE,
1106 "%s", error->message);
1107 return;
1108 }
1109
1110 /* try to remove */
1111 g_variant_get (parameters, "(&o)",
1112 &profile_object_path);
1113 g_debug ("CdDevice %s:RemoveProfile(%s)",
1114 sender, profile_object_path);
1115 ret = cd_device_remove_profile (device,
1116 profile_object_path,
1117 &error);
1118 if (!ret) {
1119 g_dbus_method_invocation_return_gerror (invocation, error);
1120 return;
1121 }
1122
1123 /* get profile id from object path */
1124 profile = cd_profile_array_get_by_object_path (priv->profile_array,
1125 profile_object_path);
1126 id = cd_profile_get_id (profile);
1127 g_object_unref (profile);
1128
1129 /* leave the entry in the database to it never gets
1130 * soft added, even if there if metadata */
1131 ret = cd_mapping_db_clear_timestamp (priv->mapping_db,
1132 priv->id,
1133 id,
1134 &error);
1135 if (!ret) {
1136 g_warning ("CdDevice: failed to save mapping to database: %s",
1137 error->message);
1138 }
1139
1140 g_dbus_method_invocation_return_value (invocation, NULL);
1141 return;
1142 }
1143
1144 /* return 's' */
1145 if (g_strcmp0 (method_name, "GetProfileRelation") == 0) {
1146
1147 /* find the profile relation */
1148 g_variant_get (parameters, "(o)", &property_value);
1149 g_debug ("CdDevice %s:GetProfileRelation(%s)",
1150 sender, property_value);
1151
1152 relation = cd_device_find_profile_relation (device,
1153 property_value);
1154 if (relation == CD_DEVICE_RELATION_UNKNOWN) {
1155 g_dbus_method_invocation_return_error (invocation,
1156 CD_DEVICE_ERROR,
1157 CD_DEVICE_ERROR_PROFILE_DOES_NOT_EXIST,
1158 "no profile '%s' found",
1159 property_value);
1160 return;
1161 }
1162
1163 tuple = g_variant_new ("(s)",
1164 cd_device_relation_to_string (relation));
1165 g_dbus_method_invocation_return_value (invocation, tuple);
1166 return;
1167 }
1168
1169 /* return 'o' */
1170 if (g_strcmp0 (method_name, "GetProfileForQualifiers") == 0) {
1171 gchar **regexes = NULL;
1172 g_autofree gchar *strv_debug = NULL;
1173
1174 /* find the profile by the qualifier search string */
1175 g_variant_get (parameters, "(^a&s)", &regexes);
1176
1177 /* show all the qualifiers */
1178 strv_debug = g_strjoinv (",", regexes);
1179 g_debug ("CdDevice %s:GetProfileForQualifiers(%s)",
1180 sender, strv_debug);
1181
1182 /* are we profiling? */
1183 ret = cd_inhibit_valid (priv->inhibit);
1184 if (!ret) {
1185 g_debug ("CdDevice: returning no results for profiling");
1186 g_dbus_method_invocation_return_error (invocation,
1187 CD_DEVICE_ERROR,
1188 CD_DEVICE_ERROR_PROFILING,
1189 "profiling, so ignoring '%s'",
1190 strv_debug);
1191 return;
1192 }
1193
1194 /* search each regex against the profiles for this device */
1195 for (i = 0; profile == NULL && regexes[i] != NULL; i++) {
1196 if (i == 0)
1197 g_debug ("searching [hard]");
1198 profile = cd_device_find_by_qualifier (regexes[i],
1199 priv->profiles,
1200 CD_DEVICE_RELATION_HARD);
1201 }
1202 for (i = 0; profile == NULL && regexes[i] != NULL; i++) {
1203 if (i == 0)
1204 g_debug ("searching [soft]");
1205 profile = cd_device_find_by_qualifier (regexes[i],
1206 priv->profiles,
1207 CD_DEVICE_RELATION_SOFT);
1208 }
1209 if (profile == NULL) {
1210 g_dbus_method_invocation_return_error (invocation,
1211 CD_DEVICE_ERROR,
1212 CD_DEVICE_ERROR_NOTHING_MATCHED,
1213 "nothing matched expression '%s'",
1214 strv_debug);
1215 return;
1216 }
1217
1218 value = g_variant_new_object_path (cd_profile_get_object_path (profile));
1219 tuple = g_variant_new_tuple (&value, 1);
1220 g_dbus_method_invocation_return_value (invocation, tuple);
1221 return;
1222 }
1223
1224 /* return '' */
1225 if (g_strcmp0 (method_name, "MakeProfileDefault") == 0) {
1226
1227 /* require auth */
1228 ret = cd_main_sender_authenticated (connection,
1229 sender,
1230 "org.freedesktop.color-manager.modify-device",
1231 &error);
1232 if (!ret) {
1233 g_dbus_method_invocation_return_error (invocation,
1234 CD_DEVICE_ERROR,
1235 CD_DEVICE_ERROR_FAILED_TO_AUTHENTICATE,
1236 "%s", error->message);
1237 return;
1238 }
1239
1240 /* check the profile_object_path exists */
1241 g_variant_get (parameters, "(&o)",
1242 &profile_object_path);
1243 g_debug ("CdDevice %s:MakeProfileDefault(%s)",
1244 sender, profile_object_path);
1245
1246 /* make profile default */
1247 ret = cd_device_make_default (device,
1248 profile_object_path,
1249 &error);
1250 if (!ret) {
1251 g_dbus_method_invocation_return_gerror (invocation, error);
1252 return;
1253 }
1254
1255 /* reset modification time */
1256 cd_device_reset_modified (device);
1257
1258 /* get profile id from object path */
1259 profile = cd_profile_array_get_by_object_path (priv->profile_array,
1260 profile_object_path);
1261 id = cd_profile_get_id (profile);
1262 g_object_unref (profile);
1263
1264 /* save new timestamp in database */
1265 ret = cd_mapping_db_add (priv->mapping_db,
1266 priv->id,
1267 id,
1268 &error);
1269 if (!ret) {
1270 g_dbus_method_invocation_return_gerror (invocation, error);
1271 return;
1272 }
1273
1274 g_dbus_method_invocation_return_value (invocation, NULL);
1275 return;
1276 }
1277
1278 /* return '' */
1279 if (g_strcmp0 (method_name, "SetEnabled") == 0) {
1280
1281 gboolean enabled;
1282
1283 /* require auth */
1284 ret = cd_main_sender_authenticated (connection,
1285 sender,
1286 "org.freedesktop.color-manager.modify-device",
1287 &error);
1288 if (!ret) {
1289 g_dbus_method_invocation_return_error (invocation,
1290 CD_DEVICE_ERROR,
1291 CD_DEVICE_ERROR_FAILED_TO_AUTHENTICATE,
1292 "%s", error->message);
1293 return;
1294 }
1295
1296 /* set, and parse */
1297 g_variant_get (parameters, "(b)",
1298 &enabled);
1299 g_debug ("CdDevice %s:SetEnabled(%s)",
1300 sender, enabled ? "True" : "False");
1301 ret = cd_device_set_enabled (device, enabled, &error);
1302 if (!ret) {
1303 g_dbus_method_invocation_return_gerror (invocation, error);
1304 return;
1305 }
1306 g_dbus_method_invocation_return_value (invocation, NULL);
1307 return;
1308 }
1309
1310 /* return '' */
1311 if (g_strcmp0 (method_name, "SetProperty") == 0) {
1312
1313 /* require auth */
1314 ret = cd_main_sender_authenticated (connection,
1315 sender,
1316 "org.freedesktop.color-manager.modify-device",
1317 &error);
1318 if (!ret) {
1319 g_dbus_method_invocation_return_error (invocation,
1320 CD_DEVICE_ERROR,
1321 CD_DEVICE_ERROR_FAILED_TO_AUTHENTICATE,
1322 "%s", error->message);
1323 return;
1324 }
1325
1326 /* set, and parse */
1327 g_variant_get (parameters, "(&s&s)",
1328 &property_name,
1329 &property_value);
1330 g_debug ("CdDevice %s:SetProperty(%s,%s)",
1331 sender, property_name, property_value);
1332 ret = cd_device_set_property_internal (device,
1333 property_name,
1334 property_value,
1335 (priv->object_scope == CD_OBJECT_SCOPE_DISK),
1336 &error);
1337 if (!ret) {
1338 g_dbus_method_invocation_return_gerror (invocation, error);
1339 return;
1340 }
1341 g_dbus_method_invocation_return_value (invocation, NULL);
1342 return;
1343 }
1344
1345 /* return '' */
1346 if (g_strcmp0 (method_name, "ProfilingInhibit") == 0) {
1347
1348 /* require auth */
1349 ret = cd_main_sender_authenticated (connection,
1350 sender,
1351 "org.freedesktop.color-manager.device-inhibit",
1352 &error);
1353 if (!ret) {
1354 g_dbus_method_invocation_return_error (invocation,
1355 CD_DEVICE_ERROR,
1356 CD_DEVICE_ERROR_FAILED_TO_AUTHENTICATE,
1357 "%s", error->message);
1358 return;
1359 }
1360
1361 /* inhbit all profiles */
1362 g_debug ("CdDevice %s:ProfilingInhibit()",
1363 sender);
1364 ret = cd_inhibit_add (priv->inhibit,
1365 sender,
1366 &error);
1367 if (!ret) {
1368 g_dbus_method_invocation_return_error (invocation,
1369 CD_DEVICE_ERROR,
1370 CD_DEVICE_ERROR_FAILED_TO_INHIBIT,
1371 "%s", error->message);
1372 return;
1373 }
1374 g_dbus_method_invocation_return_value (invocation, NULL);
1375 return;
1376 }
1377
1378 /* return '' */
1379 if (g_strcmp0 (method_name, "ProfilingUninhibit") == 0) {
1380
1381 /* perhaps uninhibit all profiles */
1382 g_debug ("CdDevice %s:ProfilingUninhibit()",
1383 sender);
1384 ret = cd_inhibit_remove (priv->inhibit,
1385 sender,
1386 &error);
1387 if (!ret) {
1388 g_dbus_method_invocation_return_error (invocation,
1389 CD_DEVICE_ERROR,
1390 CD_DEVICE_ERROR_FAILED_TO_UNINHIBIT,
1391 "%s", error->message);
1392 return;
1393 }
1394 g_dbus_method_invocation_return_value (invocation, NULL);
1395 return;
1396 }
1397
1398 /* we suck */
1399 g_critical ("failed to process device method %s", method_name);
1400 }
1401
1402 static void
1403 cd_device_inhibit_changed_cb (CdInhibit *inhibit,
1404 gpointer user_data)
1405 {
1406 CdDevice *device = CD_DEVICE (user_data);
1407
1408 /* emit */
1409 g_debug ("Emitting Device.Profiles as inhibit changed");
1410 cd_device_dbus_emit_property_changed (device,
1411 "Profiles",
1412 cd_device_get_profiles_as_variant (device));
1413
1414 /* emit global signal */
1415 cd_device_dbus_emit_device_changed (device);
1416 }
1417
1418 static GVariant *
1419 cd_device_dbus_get_property (GDBusConnection *connection_, const gchar *sender,
1420 const gchar *object_path, const gchar *interface_name,
1421 const gchar *property_name, GError **error,
1422 gpointer user_data)
1423 {
1424 CdDevice *device = CD_DEVICE (user_data);
1425 CdDevicePrivate *priv = GET_PRIVATE (device);
1426 g_auto(GStrv) bus_names = NULL;
1427
1428 if (g_strcmp0 (property_name, CD_DEVICE_PROPERTY_CREATED) == 0)
1429 return g_variant_new_uint64 (priv->created);
1430 if (g_strcmp0 (property_name, CD_DEVICE_PROPERTY_MODIFIED) == 0)
1431 return g_variant_new_uint64 (priv->modified);
1432 if (g_strcmp0 (property_name, CD_DEVICE_PROPERTY_MODEL) == 0)
1433 return cd_device_get_nullable_for_string (priv->model);
1434 if (g_strcmp0 (property_name, CD_DEVICE_PROPERTY_VENDOR) == 0)
1435 return cd_device_get_nullable_for_string (priv->vendor);
1436 if (g_strcmp0 (property_name, CD_DEVICE_PROPERTY_SERIAL) == 0)
1437 return cd_device_get_nullable_for_string (priv->serial);
1438 if (g_strcmp0 (property_name, CD_DEVICE_PROPERTY_ENABLED) == 0)
1439 return g_variant_new_boolean (priv->enabled);
1440 if (g_strcmp0 (property_name, CD_DEVICE_PROPERTY_COLORSPACE) == 0)
1441 return cd_device_get_nullable_for_string (priv->colorspace);
1442 if (g_strcmp0 (property_name, CD_DEVICE_PROPERTY_FORMAT) == 0)
1443 return cd_device_get_nullable_for_string (priv->format);
1444 if (g_strcmp0 (property_name, CD_DEVICE_PROPERTY_MODE) == 0)
1445 return cd_device_get_nullable_for_string (priv->mode);
1446 if (g_strcmp0 (property_name, CD_DEVICE_PROPERTY_KIND) == 0)
1447 return cd_device_get_nullable_for_string (cd_device_kind_to_string (priv->kind));
1448 if (g_strcmp0 (property_name, CD_DEVICE_PROPERTY_ID) == 0)
1449 return g_variant_new_string (priv->id);
1450 if (g_strcmp0 (property_name, CD_DEVICE_PROPERTY_PROFILES) == 0)
1451 return cd_device_get_profiles_as_variant (device);
1452 if (g_strcmp0 (property_name, CD_DEVICE_PROPERTY_METADATA) == 0)
1453 return cd_device_get_metadata_as_variant (device);
1454 if (g_strcmp0 (property_name, CD_DEVICE_PROPERTY_SCOPE) == 0)
1455 return g_variant_new_string (cd_object_scope_to_string (priv->object_scope));
1456 if (g_strcmp0 (property_name, CD_DEVICE_PROPERTY_OWNER) == 0)
1457 return g_variant_new_uint32 (priv->owner);
1458 if (g_strcmp0 (property_name, CD_DEVICE_PROPERTY_SEAT) == 0)
1459 return cd_device_get_nullable_for_string (priv->seat);
1460 if (g_strcmp0 (property_name, CD_DEVICE_PROPERTY_EMBEDDED) == 0)
1461 return g_variant_new_boolean (priv->embedded);
1462 if (g_strcmp0 (property_name, CD_DEVICE_PROPERTY_PROFILING_INHIBITORS) == 0) {
1463 bus_names = cd_inhibit_get_bus_names (priv->inhibit);
1464 return g_variant_new_strv ((const gchar * const *) bus_names, -1);
1465 }
1466
1467 /* return an error */
1468 g_set_error (error,
1469 CD_DEVICE_ERROR,
1470 CD_DEVICE_ERROR_INTERNAL,
1471 "failed to get device property %s",
1472 property_name);
1473 return NULL;
1474 }
1475
1476 gboolean
1477 cd_device_register_object (CdDevice *device,
1478 GDBusConnection *connection,
1479 GDBusInterfaceInfo *info,
1480 GError **error)
1481 {
1482 CdDevicePrivate *priv = GET_PRIVATE (device);
1483 g_autoptr(GError) error_local = NULL;
1484
1485 static const GDBusInterfaceVTable interface_vtable = {
1486 cd_device_dbus_method_call,
1487 cd_device_dbus_get_property,
1488 NULL
1489 };
1490
1491 priv->connection = connection;
1492 priv->registration_id = g_dbus_connection_register_object (
1493 connection,
1494 priv->object_path,
1495 info,
1496 &interface_vtable,
1497 device, /* user_data */
1498 NULL, /* user_data_free_func */
1499 &error_local); /* GError** */
1500 if (priv->registration_id == 0) {
1501 g_set_error (error,
1502 CD_DEVICE_ERROR,
1503 CD_DEVICE_ERROR_INTERNAL,
1504 "failed to register object: %s",
1505 error_local->message);
1506 return FALSE;
1507 }
1508 g_debug ("CdDevice: Register interface %u on %s",
1509 priv->registration_id,
1510 priv->object_path);
1511 return TRUE;
1512 }
1513
1514 static void
1515 cd_device_name_vanished_cb (GDBusConnection *connection,
1516 const gchar *name,
1517 gpointer user_data)
1518 {
1519 CdDevice *device = CD_DEVICE (user_data);
1520 g_debug ("CdDevice: emit 'invalidate' as %s vanished", name);
1521 g_signal_emit (device, signals[SIGNAL_INVALIDATE], 0);
1522 }
1523
1524 void
1525 cd_device_watch_sender (CdDevice *device, const gchar *sender)
1526 {
1527 CdDevicePrivate *priv = GET_PRIVATE (device);
1528 g_return_if_fail (CD_IS_DEVICE (device));
1529 g_return_if_fail (sender != NULL);
1530 priv->watcher_id = g_bus_watch_name (G_BUS_TYPE_SYSTEM,
1531 sender,
1532 G_BUS_NAME_WATCHER_FLAGS_NONE,
1533 NULL,
1534 cd_device_name_vanished_cb,
1535 device,
1536 NULL);
1537 }
1538
1539 static void
1540 cd_device_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
1541 {
1542 CdDevice *device = CD_DEVICE (object);
1543 CdDevicePrivate *priv = GET_PRIVATE (device);
1544
1545 switch (prop_id) {
1546 case PROP_OBJECT_PATH:
1547 g_value_set_string (value, priv->object_path);
1548 break;
1549 case PROP_ID:
1550 g_value_set_string (value, priv->id);
1551 break;
1552 default:
1553 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1554 break;
1555 }
1556 }
1557
1558 static void
1559 cd_device_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
1560 {
1561 CdDevice *device = CD_DEVICE (object);
1562 CdDevicePrivate *priv = GET_PRIVATE (device);
1563
1564 switch (prop_id) {
1565 case PROP_OBJECT_PATH:
1566 g_free (priv->object_path);
1567 priv->object_path = g_strdup (g_value_get_string (value));
1568 break;
1569 case PROP_ID:
1570 g_free (priv->id);
1571 priv->id = g_strdup (g_value_get_string (value));
1572 break;
1573 default:
1574 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1575 break;
1576 }
1577 }
1578
1579 static void
1580 cd_device_class_init (CdDeviceClass *klass)
1581 {
1582 GParamSpec *pspec;
1583 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1584 object_class->finalize = cd_device_finalize;
1585 object_class->get_property = cd_device_get_property;
1586 object_class->set_property = cd_device_set_property;
1587
1588 /**
1589 * CdDevice:object-path:
1590 */
1591 pspec = g_param_spec_string ("object-path", NULL, NULL,
1592 NULL,
1593 G_PARAM_READWRITE);
1594 g_object_class_install_property (object_class, PROP_OBJECT_PATH, pspec);
1595
1596 /**
1597 * CdDevice:id:
1598 */
1599 pspec = g_param_spec_string ("id", NULL, NULL,
1600 NULL,
1601 G_PARAM_READWRITE);
1602 g_object_class_install_property (object_class, PROP_ID, pspec);
1603
1604 /**
1605 * CdDevice::invalidate:
1606 **/
1607 signals[SIGNAL_INVALIDATE] =
1608 g_signal_new ("invalidate",
1609 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
1610 G_STRUCT_OFFSET (CdDeviceClass, invalidate),
1611 NULL, NULL, g_cclosure_marshal_VOID__VOID,
1612 G_TYPE_NONE, 0);
1613 }
1614
1615 static void
1616 cd_device_profiles_item_free (CdDeviceProfileItem *item)
1617 {
1618 g_object_unref (item->profile);
1619 g_free (item);
1620 }
1621
1622 static void
1623 cd_device_init (CdDevice *device)
1624 {
1625 CdDevicePrivate *priv = GET_PRIVATE (device);
1626 priv->profiles = g_ptr_array_new_with_free_func ((GDestroyNotify) cd_device_profiles_item_free);
1627 priv->profile_array = cd_profile_array_new ();
1628 priv->created = g_get_real_time ();
1629 priv->modified = g_get_real_time ();
1630 priv->mapping_db = cd_mapping_db_new ();
1631 priv->device_db = cd_device_db_new ();
1632 priv->inhibit = cd_inhibit_new ();
1633 g_signal_connect (priv->inhibit,
1634 "changed",
1635 G_CALLBACK (cd_device_inhibit_changed_cb),
1636 device);
1637 priv->metadata = g_hash_table_new_full (g_str_hash,
1638 g_str_equal,
1639 g_free,
1640 g_free);
1641 }
1642
1643 static void
1644 cd_device_finalize (GObject *object)
1645 {
1646 CdDevice *device = CD_DEVICE (object);
1647 CdDevicePrivate *priv = GET_PRIVATE (device);
1648
1649 if (priv->watcher_id > 0)
1650 g_bus_unwatch_name (priv->watcher_id);
1651 if (priv->registration_id > 0) {
1652 g_debug ("CdDevice: Unregister interface %u on %s",
1653 priv->registration_id,
1654 priv->object_path);
1655 g_dbus_connection_unregister_object (priv->connection,
1656 priv->registration_id);
1657 }
1658 g_free (priv->id);
1659 g_free (priv->model);
1660 g_free (priv->vendor);
1661 g_free (priv->colorspace);
1662 g_free (priv->format);
1663 g_free (priv->mode);
1664 g_free (priv->serial);
1665 g_free (priv->seat);
1666 g_free (priv->object_path);
1667 g_ptr_array_unref (priv->profiles);
1668 g_object_unref (priv->profile_array);
1669 g_object_unref (priv->mapping_db);
1670 g_object_unref (priv->device_db);
1671 g_object_unref (priv->inhibit);
1672 g_hash_table_unref (priv->metadata);
1673
1674 G_OBJECT_CLASS (cd_device_parent_class)->finalize (object);
1675 }
1676
1677 CdDevice *
1678 cd_device_new (void)
1679 {
1680 CdDevice *device;
1681 device = g_object_new (CD_TYPE_DEVICE, NULL);
1682 return CD_DEVICE (device);
1683 }