Debian Codegraph @

src/cd-profile.c

raw
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
2 *
3 * Copyright (C) 2010-2014 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 <gio/gio.h>
25 #include <glib-object.h>
26 #include <lcms2.h>
27 #include <string.h>
28 #include <stdlib.h>
29 #ifdef HAVE_PWD_H
30 #include <pwd.h>
31 #endif
32 #include <math.h>
33
34 #include "cd-common.h"
35 #include "cd-profile.h"
36 #include "cd-profile-db.h"
37
38 #include "colord-resources.h"
39
40 static void cd_profile_finalize (GObject *object);
41 static void cd_profile_set_filename (CdProfile *profile,
42 const gchar *filename);
43
44 #define GET_PRIVATE(o) (cd_profile_get_instance_private (o))
45
46 typedef struct
47 {
48 CdObjectScope object_scope;
49 gchar *filename;
50 gchar *id;
51 gchar *object_path;
52 gchar *qualifier;
53 gchar *format;
54 gchar *checksum;
55 gchar *title;
56 GDBusConnection *connection;
57 guint registration_id;
58 guint watcher_id;
59 CdProfileKind kind;
60 CdColorspace colorspace;
61 GHashTable *metadata;
62 gboolean has_vcgt;
63 gboolean is_system_wide;
64 gint64 created;
65 guint owner;
66 gchar **warnings;
67 GMappedFile *mapped_file;
68 guint score;
69 CdProfileDb *db;
70 } CdProfilePrivate;
71
72 enum {
73 SIGNAL_INVALIDATE,
74 SIGNAL_LAST
75 };
76
77 enum {
78 PROP_0,
79 PROP_OBJECT_PATH,
80 PROP_ID,
81 PROP_QUALIFIER,
82 PROP_TITLE,
83 PROP_FILENAME,
84 PROP_LAST
85 };
86
87 static guint signals[SIGNAL_LAST] = { 0 };
88 G_DEFINE_TYPE_WITH_PRIVATE (CdProfile, cd_profile, G_TYPE_OBJECT)
89
90 GQuark
91 cd_profile_error_quark (void)
92 {
93 guint i;
94 static GQuark quark = 0;
95 if (!quark) {
96 quark = g_quark_from_static_string ("CdProfile");
97 for (i = 0; i < CD_PROFILE_ERROR_LAST; i++) {
98 g_dbus_error_register_error (quark,
99 i,
100 cd_profile_error_to_string (i));
101 }
102 }
103 return quark;
104 }
105
106 CdObjectScope
107 cd_profile_get_scope (CdProfile *profile)
108 {
109 CdProfilePrivate *priv = GET_PRIVATE (profile);
110 g_return_val_if_fail (CD_IS_PROFILE (profile), 0);
111 return priv->object_scope;
112 }
113
114 void
115 cd_profile_set_scope (CdProfile *profile, CdObjectScope object_scope)
116 {
117 CdProfilePrivate *priv = GET_PRIVATE (profile);
118 g_return_if_fail (CD_IS_PROFILE (profile));
119 priv->object_scope = object_scope;
120 }
121
122 guint
123 cd_profile_get_owner (CdProfile *profile)
124 {
125 CdProfilePrivate *priv = GET_PRIVATE (profile);
126 g_return_val_if_fail (CD_IS_PROFILE (profile), G_MAXUINT);
127 return priv->owner;
128 }
129
130 void
131 cd_profile_set_owner (CdProfile *profile, guint owner)
132 {
133 CdProfilePrivate *priv = GET_PRIVATE (profile);
134 g_return_if_fail (CD_IS_PROFILE (profile));
135 priv->owner = owner;
136 }
137
138 void
139 cd_profile_set_is_system_wide (CdProfile *profile, gboolean is_system_wide)
140 {
141 CdProfilePrivate *priv = GET_PRIVATE (profile);
142 g_return_if_fail (CD_IS_PROFILE (profile));
143 priv->is_system_wide = is_system_wide;
144
145 /* by default, prefer systemwide profiles over user profiles */
146 priv->score += 1;
147 }
148
149 gboolean
150 cd_profile_get_is_system_wide (CdProfile *profile)
151 {
152 CdProfilePrivate *priv = GET_PRIVATE (profile);
153 g_return_val_if_fail (CD_IS_PROFILE (profile), FALSE);
154 return priv->is_system_wide;
155 }
156
157 const gchar *
158 cd_profile_get_object_path (CdProfile *profile)
159 {
160 CdProfilePrivate *priv = GET_PRIVATE (profile);
161 g_return_val_if_fail (CD_IS_PROFILE (profile), NULL);
162 return priv->object_path;
163 }
164
165 const gchar *
166 cd_profile_get_id (CdProfile *profile)
167 {
168 CdProfilePrivate *priv = GET_PRIVATE (profile);
169 g_return_val_if_fail (CD_IS_PROFILE (profile), NULL);
170 return priv->id;
171 }
172
173 static void
174 cd_profile_set_object_path (CdProfile *profile)
175 {
176 CdProfilePrivate *priv = GET_PRIVATE (profile);
177 #ifdef HAVE_PWD_H
178 struct passwd *pw;
179 #endif
180 g_autofree gchar *path_tmp = NULL;
181 g_autofree gchar *path_owner = NULL;
182
183 /* append the uid to the object path */
184 #ifdef HAVE_PWD_H
185 pw = getpwuid (priv->owner);
186 if (priv->owner == 0 ||
187 g_strcmp0 (pw->pw_name, DAEMON_USER) == 0) {
188 path_tmp = g_strdup (priv->id);
189 } else {
190 path_tmp = g_strdup_printf ("%s_%s_%u",
191 priv->id,
192 pw->pw_name,
193 priv->owner);
194 }
195 #else
196 if (priv->owner == 0) {
197 path_tmp = g_strdup (priv->id);
198 } else {
199 path_tmp = g_strdup_printf ("%s_%d",
200 priv->id,
201 priv->owner);
202 }
203 #endif
204 /* make sure object path is sane */
205 path_owner = cd_main_ensure_dbus_path (path_tmp);
206
207 priv->object_path = g_build_filename (COLORD_DBUS_PATH,
208 "profiles",
209 path_owner,
210 NULL);
211 }
212
213 static void
214 cd_profile_set_metadata (CdProfile *profile,
215 const gchar *property,
216 const gchar *value)
217 {
218 CdProfilePrivate *priv = GET_PRIVATE (profile);
219 /* i1Profiler sets this */
220 if (g_strcmp0 (property, "CreatorApp") == 0)
221 property = CD_PROFILE_METADATA_CMF_PRODUCT;
222 g_hash_table_insert (priv->metadata,
223 g_strdup (property),
224 g_strdup (value));
225 }
226
227 void
228 cd_profile_set_id (CdProfile *profile, const gchar *id)
229 {
230 CdProfilePrivate *priv = GET_PRIVATE (profile);
231
232 CdStandardSpace standard_space = CD_STANDARD_SPACE_UNKNOWN;
233
234 g_return_if_fail (CD_IS_PROFILE (profile));
235
236 g_free (priv->id);
237 priv->id = g_strdup (id);
238
239 /* all profiles have a score initially */
240 priv->score = 1;
241
242 /* http://www.color.org/srgbprofiles.xalter */
243 if (g_strcmp0 (id, "icc-34562abf994ccd066d2c5721d0d68c5d") == 0) {
244 /* sRGB_v4_ICC_preference */
245 standard_space = CD_STANDARD_SPACE_SRGB;
246 priv->score = 10;
247 }
248 if (g_strcmp0 (id, "icc-fc66337837e2886bfd72e9838228f1b8") == 0) {
249 /* sRGB_v4_ICC_preference_displayclass */
250 standard_space = CD_STANDARD_SPACE_SRGB;
251 priv->score = 8;
252 }
253 if (g_strcmp0 (id, "icc-29f83ddeaff255ae7842fae4ca83390d") == 0) {
254 /* sRGB_IEC61966-2-1_black_scaled */
255 standard_space = CD_STANDARD_SPACE_SRGB;
256 priv->score = 6;
257 }
258 if (g_strcmp0 (id, "icc-c95bd637e95d8a3b0df38f99c1320389") == 0) {
259 /* sRGB_IEC61966-2-1_no_black_scaling */
260 standard_space = CD_STANDARD_SPACE_SRGB;
261 priv->score = 4;
262 }
263
264 /* from http://download.adobe.com/pub/adobe/iccprofiles/ */
265 if (g_strcmp0 (id, "icc-dea88382d899d5f6e573b432473ae138") == 0) {
266 /* AdobeRGB1998 */
267 standard_space = CD_STANDARD_SPACE_ADOBE_RGB;
268 priv->score = 10;
269 }
270 if (g_strcmp0 (id, "icc-91cf26c58e07eda724fdbf3eadce4505") == 0) {
271 /* ColorMatchRGB */
272 standard_space = CD_STANDARD_SPACE_LAST;
273 }
274 if (g_strcmp0 (id, "icc-2e54d10b392cac47226469ba2ea95bd8") == 0) {
275 /* AppleRGB */
276 standard_space = CD_STANDARD_SPACE_LAST;
277 }
278
279 /* add additional metadata to fix the GUIs */
280 if (standard_space != CD_STANDARD_SPACE_UNKNOWN) {
281 cd_profile_set_metadata (profile,
282 CD_PROFILE_METADATA_STANDARD_SPACE,
283 cd_standard_space_to_string (standard_space));
284 cd_profile_set_metadata (profile,
285 CD_PROFILE_METADATA_DATA_SOURCE,
286 CD_PROFILE_METADATA_DATA_SOURCE_STANDARD);
287 }
288
289 /* now calculate this again */
290 cd_profile_set_object_path (profile);
291 }
292
293 const gchar *
294 cd_profile_get_filename (CdProfile *profile)
295 {
296 CdProfilePrivate *priv = GET_PRIVATE (profile);
297 g_return_val_if_fail (CD_IS_PROFILE (profile), NULL);
298 return priv->filename;
299 }
300
301 static void
302 cd_profile_dbus_emit_property_changed (CdProfile *profile,
303 const gchar *property_name,
304 GVariant *property_value)
305 {
306 CdProfilePrivate *priv = GET_PRIVATE (profile);
307 GVariantBuilder builder;
308 GVariantBuilder invalidated_builder;
309
310 /* not yet connected */
311 if (priv->connection == NULL)
312 return;
313
314 /* build the dict */
315 g_variant_builder_init (&invalidated_builder, G_VARIANT_TYPE ("as"));
316 g_variant_builder_init (&builder, G_VARIANT_TYPE_ARRAY);
317 g_variant_builder_add (&builder,
318 "{sv}",
319 property_name,
320 property_value);
321 g_dbus_connection_emit_signal (priv->connection,
322 NULL,
323 priv->object_path,
324 "org.freedesktop.DBus.Properties",
325 "PropertiesChanged",
326 g_variant_new ("(sa{sv}as)",
327 COLORD_DBUS_INTERFACE_PROFILE,
328 &builder,
329 &invalidated_builder),
330 NULL);
331 g_variant_builder_clear (&builder);
332 g_variant_builder_clear (&invalidated_builder);
333 }
334
335 static void
336 cd_profile_dbus_emit_profile_changed (CdProfile *profile)
337 {
338 CdProfilePrivate *priv = GET_PRIVATE (profile);
339
340 /* not yet connected */
341 if (priv->connection == NULL)
342 return;
343
344 /* emit signal */
345 g_debug ("CdProfile: emit Changed on %s",
346 cd_profile_get_object_path (profile));
347 g_dbus_connection_emit_signal (priv->connection,
348 NULL,
349 cd_profile_get_object_path (profile),
350 COLORD_DBUS_INTERFACE_PROFILE,
351 "Changed",
352 NULL,
353 NULL);
354
355 /* emit signal */
356 g_debug ("CdProfile: emit Changed");
357 g_dbus_connection_emit_signal (priv->connection,
358 NULL,
359 COLORD_DBUS_PATH,
360 COLORD_DBUS_INTERFACE,
361 "ProfileChanged",
362 g_variant_new ("(o)",
363 cd_profile_get_object_path (profile)),
364 NULL);
365 }
366
367 static gboolean
368 cd_profile_install_system_wide (CdProfile *profile, GError **error)
369 {
370 CdProfilePrivate *priv = GET_PRIVATE (profile);
371 gboolean ret = TRUE;
372 g_autoptr(GError) error_local = NULL;
373 g_autofree gchar *basename = NULL;
374 g_autofree gchar *filename = NULL;
375 g_autoptr(GFile) file_dest = NULL;
376 g_autoptr(GFile) file = NULL;
377
378 /* is icc filename set? */
379 if (priv->filename == NULL) {
380 g_set_error (error,
381 CD_PROFILE_ERROR,
382 CD_PROFILE_ERROR_INTERNAL,
383 "icc filename not set");
384 return FALSE;
385 }
386
387 /* is profile already installed in /var/lib/color */
388 if (g_str_has_prefix (priv->filename,
389 CD_SYSTEM_PROFILES_DIR)) {
390 g_set_error (error,
391 CD_PROFILE_ERROR,
392 CD_PROFILE_ERROR_ALREADY_INSTALLED,
393 "file %s already installed in /var",
394 priv->filename);
395 return FALSE;
396 }
397
398 /* is profile already installed in /usr/share/color */
399 if (g_str_has_prefix (priv->filename,
400 DATADIR "/color")) {
401 g_set_error (error,
402 CD_PROFILE_ERROR,
403 CD_PROFILE_ERROR_ALREADY_INSTALLED,
404 "file %s already installed in /usr",
405 priv->filename);
406 return FALSE;
407 }
408
409 /* copy */
410 basename = g_path_get_basename (priv->filename);
411 filename = g_build_filename (CD_SYSTEM_PROFILES_DIR,
412 basename, NULL);
413 file_dest = g_file_new_for_path (filename);
414
415 /* try to write a mapped file first, else copy the file */
416 if (priv->mapped_file != NULL) {
417 g_debug ("writing mapped file to %s", filename);
418 ret = g_file_replace_contents (file_dest,
419 g_mapped_file_get_contents (priv->mapped_file),
420 g_mapped_file_get_length (priv->mapped_file),
421 NULL,
422 FALSE,
423 G_FILE_CREATE_REPLACE_DESTINATION,
424 NULL,
425 NULL, /* cancellable */
426 &error_local);
427 if (!ret) {
428 g_set_error (error,
429 CD_PROFILE_ERROR,
430 CD_PROFILE_ERROR_FAILED_TO_WRITE,
431 "failed to write mapped file %s: %s",
432 priv->filename,
433 error_local->message);
434 return FALSE;
435 }
436 } else {
437 file = g_file_new_for_path (priv->filename);
438 ret = g_file_copy (file, file_dest, G_FILE_COPY_OVERWRITE,
439 NULL, NULL, NULL, &error_local);
440 if (!ret) {
441 g_set_error (error,
442 CD_PROFILE_ERROR,
443 CD_PROFILE_ERROR_FAILED_TO_WRITE,
444 "failed to copy %s: %s",
445 priv->filename,
446 error_local->message);
447 return FALSE;
448 }
449 }
450 return TRUE;
451 }
452
453 static GVariant *
454 cd_profile_get_metadata_as_variant (CdProfile *profile)
455 {
456 CdProfilePrivate *priv = GET_PRIVATE (profile);
457 GList *l;
458 GVariantBuilder builder;
459 g_autoptr(GList) list = NULL;
460
461 /* do not try to build an empty array */
462 if (g_hash_table_size (priv->metadata) == 0)
463 return g_variant_new_array (G_VARIANT_TYPE ("{ss}"), NULL, 0);
464
465 /* add all the keys in the dictionary to the variant builder */
466 list = g_hash_table_get_keys (priv->metadata);
467 g_variant_builder_init (&builder, G_VARIANT_TYPE_ARRAY);
468 for (l = list; l != NULL; l = l->next) {
469 g_variant_builder_add (&builder,
470 "{ss}",
471 l->data,
472 g_hash_table_lookup (priv->metadata,
473 l->data));
474 }
475 return g_variant_builder_end (&builder);
476 }
477
478 static GVariant *
479 cd_profile_get_nullable_for_string (const gchar *value)
480 {
481 if (value == NULL)
482 return g_variant_new_string ("");
483 return g_variant_new_string (value);
484 }
485
486 static gboolean
487 cd_profile_set_title (CdProfile *profile,
488 const gchar *value,
489 guint sender_uid,
490 GError **error)
491 {
492 CdProfilePrivate *priv = GET_PRIVATE (profile);
493
494 /* check title is suitable */
495 if (value == NULL || strlen (value) < 3 ||
496 !g_utf8_validate (value, -1, NULL)) {
497 g_set_error (error,
498 CD_CLIENT_ERROR,
499 CD_CLIENT_ERROR_INPUT_INVALID,
500 "'Title' value input invalid: %s", value);
501 return FALSE;
502 }
503
504 /* save in database */
505 return cd_profile_db_set_property (priv->db, priv->id,
506 CD_PROFILE_PROPERTY_TITLE, sender_uid,
507 value, error);
508 }
509
510 gboolean
511 cd_profile_set_property_internal (CdProfile *profile,
512 const gchar *property,
513 const gchar *value,
514 guint sender_uid,
515 GError **error)
516 {
517 CdProfilePrivate *priv = GET_PRIVATE (profile);
518
519 /* sanity check the length of the key and value */
520 if (strlen (property) > CD_DBUS_METADATA_KEY_LEN_MAX) {
521 g_set_error_literal (error,
522 CD_CLIENT_ERROR,
523 CD_CLIENT_ERROR_INPUT_INVALID,
524 "metadata key length invalid");
525 return FALSE;
526 }
527 if (value != NULL && strlen (value) > CD_DBUS_METADATA_VALUE_LEN_MAX) {
528 g_set_error_literal (error,
529 CD_CLIENT_ERROR,
530 CD_CLIENT_ERROR_INPUT_INVALID,
531 "metadata value length invalid");
532 return FALSE;
533 }
534
535 if (g_strcmp0 (property, CD_PROFILE_PROPERTY_FILENAME) == 0) {
536 cd_profile_set_filename (profile, value);
537 cd_profile_dbus_emit_property_changed (profile,
538 property,
539 g_variant_new_string (value));
540 } else if (g_strcmp0 (property, CD_PROFILE_PROPERTY_QUALIFIER) == 0) {
541 cd_profile_set_qualifier (profile, value);
542 cd_profile_dbus_emit_property_changed (profile,
543 property,
544 g_variant_new_string (value));
545 } else if (g_strcmp0 (property, CD_PROFILE_PROPERTY_FORMAT) == 0) {
546 cd_profile_set_format (profile, value);
547 cd_profile_dbus_emit_property_changed (profile,
548 property,
549 g_variant_new_string (value));
550 } else if (g_strcmp0 (property, CD_PROFILE_PROPERTY_COLORSPACE) == 0) {
551 priv->colorspace = cd_colorspace_from_string (value);
552 cd_profile_dbus_emit_property_changed (profile,
553 property,
554 g_variant_new_string (value));
555 } else if (g_strcmp0 (property, CD_PROFILE_PROPERTY_TITLE) == 0) {
556 if (!cd_profile_set_title (profile, value, sender_uid, error))
557 return FALSE;
558 cd_profile_dbus_emit_property_changed (profile, property,
559 g_variant_new_string (value));
560 } else {
561 /* add to metadata */
562 cd_profile_set_metadata (profile, property, value);
563 cd_profile_dbus_emit_property_changed (profile,
564 CD_PROFILE_PROPERTY_METADATA,
565 cd_profile_get_metadata_as_variant (profile));
566 return TRUE;
567 }
568
569 /* emit global signal */
570 cd_profile_dbus_emit_profile_changed (profile);
571 return TRUE;
572 }
573
574 static void
575 cd_profile_dbus_method_call (GDBusConnection *connection, const gchar *sender,
576 const gchar *object_path, const gchar *interface_name,
577 const gchar *method_name, GVariant *parameters,
578 GDBusMethodInvocation *invocation, gpointer user_data)
579 {
580 CdProfile *profile = CD_PROFILE (user_data);
581 CdProfilePrivate *priv = GET_PRIVATE (profile);
582 gboolean ret;
583 guint uid;
584 const gchar *property_name = NULL;
585 const gchar *property_value = NULL;
586 g_autoptr(GError) error = NULL;
587
588 /* return '' */
589 if (g_strcmp0 (method_name, "SetProperty") == 0) {
590
591 /* require auth */
592 ret = cd_main_sender_authenticated (connection,
593 sender,
594 "org.freedesktop.color-manager.modify-profile",
595 &error);
596 if (!ret) {
597 g_dbus_method_invocation_return_error (invocation,
598 CD_PROFILE_ERROR,
599 CD_PROFILE_ERROR_FAILED_TO_AUTHENTICATE,
600 "%s", error->message);
601 return;
602 }
603
604 /* get UID */
605 uid = cd_main_get_sender_uid (connection, sender, &error);
606 if (uid == G_MAXUINT) {
607 g_dbus_method_invocation_return_error (invocation,
608 CD_PROFILE_ERROR,
609 CD_PROFILE_ERROR_FAILED_TO_GET_UID,
610 "%s", error->message);
611 return;
612 }
613
614 /* set, and parse */
615 g_variant_get (parameters, "(&s&s)",
616 &property_name,
617 &property_value);
618 g_debug ("CdProfile %s:SetProperty(%s,%s)",
619 sender, property_name, property_value);
620 if (g_strcmp0 (property_name, CD_PROFILE_PROPERTY_FILENAME) == 0) {
621 g_dbus_method_invocation_return_error (invocation,
622 CD_PROFILE_ERROR,
623 CD_PROFILE_ERROR_PROPERTY_INVALID,
624 "Setting the %s property after "
625 "profile creation is no longer supported",
626 property_name);
627 return;
628 }
629 ret = cd_profile_set_property_internal (profile,
630 property_name,
631 property_value,
632 uid,
633 &error);
634 if (!ret) {
635 g_dbus_method_invocation_return_gerror (invocation, error);
636 return;
637 }
638 g_dbus_method_invocation_return_value (invocation, NULL);
639 return;
640 }
641
642 /* return '' */
643 if (g_strcmp0 (method_name, "InstallSystemWide") == 0) {
644
645 /* require auth */
646 g_debug ("CdProfile %s:InstallSystemWide() on %s",
647 sender, priv->object_path);
648 ret = cd_main_sender_authenticated (connection,
649 sender,
650 "org.freedesktop.color-manager.install-system-wide",
651 &error);
652 if (!ret) {
653 g_dbus_method_invocation_return_error (invocation,
654 CD_PROFILE_ERROR,
655 CD_PROFILE_ERROR_FAILED_TO_AUTHENTICATE,
656 "%s", error->message);
657 return;
658 }
659
660 /* copy systemwide */
661 ret = cd_profile_install_system_wide (profile, &error);
662 if (!ret) {
663 g_dbus_method_invocation_return_gerror (invocation, error);
664 return;
665 }
666
667 g_dbus_method_invocation_return_value (invocation, NULL);
668 return;
669 }
670
671
672 /* we suck */
673 g_critical ("failed to process method %s", method_name);
674 }
675
676 static GVariant *
677 cd_profile_dbus_get_property (GDBusConnection *connection, const gchar *sender,
678 const gchar *object_path, const gchar *interface_name,
679 const gchar *property_name, GError **error,
680 gpointer user_data)
681 {
682 CdProfile *profile = CD_PROFILE (user_data);
683 CdProfilePrivate *priv = GET_PRIVATE (profile);
684 gboolean ret;
685
686 if (g_strcmp0 (property_name, CD_PROFILE_PROPERTY_TITLE) == 0) {
687 guint uid;
688 g_autofree gchar *title_db = NULL;
689
690 uid = cd_main_get_sender_uid (connection, sender, error);
691 if (uid == G_MAXUINT)
692 return NULL;
693 ret = cd_profile_db_get_property (priv->db, priv->id,
694 property_name, uid,
695 &title_db, error);
696 if (!ret)
697 return NULL;
698 if (title_db != NULL)
699 return cd_profile_get_nullable_for_string (title_db);
700 return cd_profile_get_nullable_for_string (priv->title);
701 }
702 if (g_strcmp0 (property_name, CD_PROFILE_PROPERTY_ID) == 0)
703 return cd_profile_get_nullable_for_string (priv->id);
704 if (g_strcmp0 (property_name, CD_PROFILE_PROPERTY_QUALIFIER) == 0)
705 return cd_profile_get_nullable_for_string (priv->qualifier);
706 if (g_strcmp0 (property_name, CD_PROFILE_PROPERTY_FORMAT) == 0)
707 return cd_profile_get_nullable_for_string (priv->format);
708 if (g_strcmp0 (property_name, CD_PROFILE_PROPERTY_FILENAME) == 0)
709 return cd_profile_get_nullable_for_string (priv->filename);
710 if (g_strcmp0 (property_name, CD_PROFILE_PROPERTY_KIND) == 0)
711 return g_variant_new_string (cd_profile_kind_to_string (priv->kind));
712 if (g_strcmp0 (property_name, CD_PROFILE_PROPERTY_COLORSPACE) == 0)
713 return g_variant_new_string (cd_colorspace_to_string (priv->colorspace));
714 if (g_strcmp0 (property_name, CD_PROFILE_PROPERTY_HAS_VCGT) == 0)
715 return g_variant_new_boolean (priv->has_vcgt);
716 if (g_strcmp0 (property_name, CD_PROFILE_PROPERTY_IS_SYSTEM_WIDE) == 0)
717 return g_variant_new_boolean (priv->is_system_wide);
718 if (g_strcmp0 (property_name, CD_PROFILE_PROPERTY_METADATA) == 0)
719 return cd_profile_get_metadata_as_variant (profile);
720 if (g_strcmp0 (property_name, CD_PROFILE_PROPERTY_CREATED) == 0)
721 return g_variant_new_int64 (priv->created);
722 if (g_strcmp0 (property_name, CD_PROFILE_PROPERTY_SCOPE) == 0)
723 return g_variant_new_string (cd_object_scope_to_string (priv->object_scope));
724 if (g_strcmp0 (property_name, CD_PROFILE_PROPERTY_OWNER) == 0)
725 return g_variant_new_uint32 (priv->owner);
726 if (g_strcmp0 (property_name, CD_PROFILE_PROPERTY_WARNINGS) == 0) {
727 if (priv->warnings == NULL) {
728 const gchar *tmp[] = { NULL };
729 return g_variant_new_strv (tmp, -1);
730 }
731 return g_variant_new_strv ((const gchar * const *) priv->warnings, -1);
732 }
733
734 /* return an error */
735 g_set_error (error,
736 CD_PROFILE_ERROR,
737 CD_PROFILE_ERROR_INTERNAL,
738 "failed to get profile property %s",
739 property_name);
740 return NULL;
741 }
742
743 gboolean
744 cd_profile_register_object (CdProfile *profile,
745 GDBusConnection *connection,
746 GDBusInterfaceInfo *info,
747 GError **error)
748 {
749 CdProfilePrivate *priv = GET_PRIVATE (profile);
750 g_autoptr(GError) error_local = NULL;
751
752 static const GDBusInterfaceVTable interface_vtable = {
753 cd_profile_dbus_method_call,
754 cd_profile_dbus_get_property,
755 NULL
756 };
757
758 priv->connection = connection;
759 priv->registration_id = g_dbus_connection_register_object (
760 connection,
761 priv->object_path,
762 info,
763 &interface_vtable,
764 profile, /* user_data */
765 NULL, /* user_data_free_func */
766 &error_local); /* GError** */
767 if (priv->registration_id == 0) {
768 g_set_error (error,
769 CD_PROFILE_ERROR,
770 CD_PROFILE_ERROR_INTERNAL,
771 "failed to register object: %s",
772 error_local->message);
773 return FALSE;
774 }
775 return TRUE;
776 }
777
778 static gchar *
779 cd_profile_fixup_title (const gchar *text)
780 {
781 gchar *title;
782 gchar *tmp;
783 guint len;
784
785 /* nothing set */
786 if (text == NULL)
787 return NULL;
788
789 /* remove the hardcoded confusing title */
790 if (g_str_has_prefix (text, "Default, "))
791 text += 9;
792 title = g_strdup (text);
793
794 /* hack to make old profiles look nice */
795 tmp = g_strstr_len (title, -1, " (201");
796 if (tmp != NULL)
797 *tmp = '\0';
798
799 /* make underscores into spaces */
800 g_strdelimit (title, "_", ' ');
801
802 /* remove any shitty suffix */
803 if (g_str_has_suffix (title, ".icc") ||
804 g_str_has_suffix (title, ".ICC") ||
805 g_str_has_suffix (title, ".icm") ||
806 g_str_has_suffix (title, ".ICM")) {
807 len = strlen (title);
808 if (len > 4)
809 title[len - 4] = '\0';
810 }
811 return title;
812 }
813
814 static gboolean
815 cd_profile_set_from_profile (CdProfile *profile, CdIcc *icc, GError **error)
816 {
817 CdProfilePrivate *priv = GET_PRIVATE (profile);
818 CdProfileWarning warning;
819 GList *l;
820 cmsHPROFILE lcms_profile;
821 const gchar *key;
822 const gchar *value;
823 gboolean ret = FALSE;
824 guint i;
825 struct tm created;
826 g_autoptr(GArray) flags = NULL;
827 g_autoptr(GHashTable) metadata = NULL;
828 g_autoptr(GList) keys = NULL;
829
830 /* get the description as the title */
831 value = cd_icc_get_description (icc, NULL, error);
832 if (value == NULL)
833 return FALSE;
834 priv->title = cd_profile_fixup_title (value);
835
836 /* get the profile kind */
837 priv->kind = cd_icc_get_kind (icc);
838 priv->colorspace = cd_icc_get_colorspace (icc);
839
840 /* get metadata */
841 metadata = cd_icc_get_metadata (icc);
842 keys = g_hash_table_get_keys (metadata);
843 for (l = keys; l != NULL; l = l->next) {
844 key = l->data;
845 value = g_hash_table_lookup (metadata, key);
846 g_debug ("Adding metadata %s=%s", key, value);
847 g_hash_table_insert (priv->metadata,
848 g_strdup (key),
849 g_strdup (value));
850 }
851
852 /* set the format from the metadata */
853 value = g_hash_table_lookup (priv->metadata,
854 CD_PROFILE_METADATA_MAPPING_FORMAT);
855 if (value != NULL)
856 cd_profile_set_format (profile, value);
857
858 /* set the qualifier from the metadata */
859 value = g_hash_table_lookup (priv->metadata,
860 CD_PROFILE_METADATA_MAPPING_QUALIFIER);
861 if (value != NULL)
862 cd_profile_set_qualifier (profile, value);
863
864 /* set a generic qualifier if there was nothing set before */
865 if (priv->colorspace == CD_COLORSPACE_RGB &&
866 priv->qualifier == NULL) {
867 cd_profile_set_format (profile, "ColorSpace..");
868 cd_profile_set_qualifier (profile, "RGB..");
869 }
870
871 /* get the profile created time and date */
872 lcms_profile = cd_icc_get_handle (icc);
873 ret = cmsGetHeaderCreationDateTime (lcms_profile, &created);
874 if (ret) {
875 created.tm_isdst = -1;
876 priv->created = mktime (&created);
877 } else {
878 g_warning ("failed to get created time");
879 priv->created = 0;
880 }
881
882 /* do we have vcgt */
883 priv->has_vcgt = cmsIsTag (lcms_profile, cmsSigVcgtTag);
884
885 /* get the checksum for the profile if we can */
886 priv->checksum = g_strdup (cd_icc_get_checksum (icc));
887
888 /* get any warnings for the profile */
889 flags = cd_icc_get_warnings (icc);
890 priv->warnings = g_new0 (gchar *, flags->len + 1);
891 if (flags->len > 0) {
892 for (i = 0; i < flags->len; i++) {
893 warning = g_array_index (flags, CdProfileWarning, i);
894 priv->warnings[i] = g_strdup (cd_profile_warning_to_string (warning));
895 }
896 }
897 return TRUE;
898 }
899
900 const gchar **
901 cd_profile_get_warnings (CdProfile *profile)
902 {
903 CdProfilePrivate *priv = GET_PRIVATE (profile);
904 return (const gchar **) priv->warnings;
905 }
906
907 static void
908 cd_profile_emit_parsed_property_changed (CdProfile *profile)
909 {
910 CdProfilePrivate *priv = GET_PRIVATE (profile);
911
912 cd_profile_dbus_emit_property_changed (profile,
913 CD_PROFILE_PROPERTY_FILENAME,
914 cd_profile_get_nullable_for_string (priv->filename));
915 cd_profile_dbus_emit_property_changed (profile,
916 CD_PROFILE_PROPERTY_TITLE,
917 cd_profile_get_nullable_for_string (priv->title));
918 cd_profile_dbus_emit_property_changed (profile,
919 CD_PROFILE_PROPERTY_KIND,
920 g_variant_new_string (cd_profile_kind_to_string (priv->kind)));
921 cd_profile_dbus_emit_property_changed (profile,
922 CD_PROFILE_PROPERTY_COLORSPACE,
923 g_variant_new_string (cd_colorspace_to_string (priv->colorspace)));
924 cd_profile_dbus_emit_property_changed (profile,
925 CD_PROFILE_PROPERTY_HAS_VCGT,
926 g_variant_new_boolean (priv->has_vcgt));
927 cd_profile_dbus_emit_property_changed (profile,
928 CD_PROFILE_PROPERTY_METADATA,
929 cd_profile_get_metadata_as_variant (profile));
930 cd_profile_dbus_emit_property_changed (profile,
931 CD_PROFILE_PROPERTY_QUALIFIER,
932 cd_profile_get_nullable_for_string (priv->qualifier));
933 cd_profile_dbus_emit_property_changed (profile,
934 CD_PROFILE_PROPERTY_FORMAT,
935 cd_profile_get_nullable_for_string (priv->format));
936 cd_profile_dbus_emit_property_changed (profile,
937 CD_PROFILE_PROPERTY_CREATED,
938 g_variant_new_int64 (priv->created));
939 }
940
941 gboolean
942 cd_profile_load_from_icc (CdProfile *profile, CdIcc *icc, GError **error)
943 {
944 g_return_val_if_fail (CD_IS_PROFILE (profile), FALSE);
945
946 /* save filename */
947 cd_profile_set_filename (profile, cd_icc_get_filename (icc));
948
949 /* set the virtual profile from the lcms profile */
950 if (!cd_profile_set_from_profile (profile, icc, error))
951 return FALSE;
952
953 /* emit all the things that could have changed */
954 cd_profile_emit_parsed_property_changed (profile);
955 return TRUE;
956 }
957
958 gboolean
959 cd_profile_load_from_fd (CdProfile *profile,
960 gint fd,
961 GError **error)
962 {
963 CdProfilePrivate *priv = GET_PRIVATE (profile);
964 gboolean ret;
965 g_autoptr(GError) error_local = NULL;
966 g_autoptr(CdIcc) icc = NULL;
967
968 g_return_val_if_fail (CD_IS_PROFILE (profile), FALSE);
969
970 /* check we're not already set */
971 if (priv->kind != CD_PROFILE_KIND_UNKNOWN) {
972 g_set_error (error,
973 CD_PROFILE_ERROR,
974 CD_PROFILE_ERROR_INTERNAL,
975 "profile '%s' already set",
976 priv->object_path);
977 return FALSE;
978 }
979
980 /* open fd and parse the file */
981 icc = cd_icc_new ();
982 ret = cd_icc_load_fd (icc, fd,
983 CD_ICC_LOAD_FLAGS_METADATA,
984 &error_local);
985 if (!ret) {
986 g_set_error_literal (error,
987 CD_PROFILE_ERROR,
988 CD_PROFILE_ERROR_FAILED_TO_READ,
989 error_local->message);
990 return FALSE;
991 }
992
993 /* create a mapped file */
994 priv->mapped_file = g_mapped_file_new_from_fd (fd, FALSE, error);
995 if (priv->mapped_file == NULL) {
996 g_set_error (error,
997 CD_PROFILE_ERROR,
998 CD_PROFILE_ERROR_FAILED_TO_READ,
999 "failed to create mapped file from fd %i",
1000 fd);
1001 return FALSE;
1002 }
1003
1004 /* set the virtual profile from the lcms profile */
1005 if (!cd_profile_set_from_profile (profile, icc, error))
1006 return FALSE;
1007
1008 /* emit all the things that could have changed */
1009 cd_profile_emit_parsed_property_changed (profile);
1010 return TRUE;
1011 }
1012
1013 gboolean
1014 cd_profile_load_from_filename (CdProfile *profile, const gchar *filename, GError **error)
1015 {
1016 CdProfilePrivate *priv = GET_PRIVATE (profile);
1017 gboolean ret = FALSE;
1018 g_autoptr(GError) error_local = NULL;
1019 g_autoptr(CdIcc) icc = NULL;
1020 g_autoptr(GFile) file = NULL;
1021
1022 g_return_val_if_fail (CD_IS_PROFILE (profile), FALSE);
1023
1024 /* check we're not already set */
1025 if (priv->kind != CD_PROFILE_KIND_UNKNOWN) {
1026 g_set_error (error,
1027 CD_PROFILE_ERROR,
1028 CD_PROFILE_ERROR_INTERNAL,
1029 "profile '%s' already set",
1030 priv->object_path);
1031 return FALSE;
1032 }
1033
1034 /* open fd and parse the file */
1035 icc = cd_icc_new ();
1036 file = g_file_new_for_path (filename);
1037 ret = cd_icc_load_file (icc, file,
1038 CD_ICC_LOAD_FLAGS_METADATA,
1039 NULL,
1040 &error_local);
1041 if (!ret) {
1042 g_set_error_literal (error,
1043 CD_PROFILE_ERROR,
1044 CD_PROFILE_ERROR_FAILED_TO_READ,
1045 error_local->message);
1046 return FALSE;
1047 }
1048
1049 /* create a mapped file */
1050 priv->mapped_file = g_mapped_file_new (filename, FALSE, error);
1051 if (priv->mapped_file == NULL) {
1052 g_set_error (error,
1053 CD_PROFILE_ERROR,
1054 CD_PROFILE_ERROR_FAILED_TO_READ,
1055 "failed to create mapped file from filename %s",
1056 filename);
1057 return FALSE;
1058 }
1059
1060 /* set the virtual profile from the lcms profile */
1061 if (!cd_profile_set_from_profile (profile, icc, error))
1062 return FALSE;
1063
1064 /* emit all the things that could have changed */
1065 cd_profile_emit_parsed_property_changed (profile);
1066 return TRUE;
1067 }
1068
1069 const gchar *
1070 cd_profile_get_qualifier (CdProfile *profile)
1071 {
1072 CdProfilePrivate *priv = GET_PRIVATE (profile);
1073 g_return_val_if_fail (CD_IS_PROFILE (profile), NULL);
1074 return priv->qualifier;
1075 }
1076
1077 void
1078 cd_profile_set_qualifier (CdProfile *profile, const gchar *qualifier)
1079 {
1080 CdProfilePrivate *priv = GET_PRIVATE (profile);
1081 g_return_if_fail (CD_IS_PROFILE (profile));
1082 g_free (priv->qualifier);
1083 priv->qualifier = g_strdup (qualifier);
1084 }
1085
1086 void
1087 cd_profile_set_format (CdProfile *profile, const gchar *format)
1088 {
1089 CdProfilePrivate *priv = GET_PRIVATE (profile);
1090 g_return_if_fail (CD_IS_PROFILE (profile));
1091 g_free (priv->format);
1092 priv->format = g_strdup (format);
1093 }
1094
1095 static void
1096 cd_profile_set_filename (CdProfile *profile, const gchar *filename)
1097 {
1098 CdProfilePrivate *priv = GET_PRIVATE (profile);
1099 g_return_if_fail (CD_IS_PROFILE (profile));
1100 g_free (priv->filename);
1101 priv->filename = g_strdup (filename);
1102 }
1103
1104 const gchar *
1105 cd_profile_get_title (CdProfile *profile)
1106 {
1107 CdProfilePrivate *priv = GET_PRIVATE (profile);
1108 g_return_val_if_fail (CD_IS_PROFILE (profile), NULL);
1109 return priv->title;
1110 }
1111
1112 GHashTable *
1113 cd_profile_get_metadata (CdProfile *profile)
1114 {
1115 CdProfilePrivate *priv = GET_PRIVATE (profile);
1116 g_return_val_if_fail (CD_IS_PROFILE (profile), NULL);
1117 return priv->metadata;
1118 }
1119
1120 const gchar *
1121 cd_profile_get_metadata_item (CdProfile *profile, const gchar *key)
1122 {
1123 CdProfilePrivate *priv = GET_PRIVATE (profile);
1124 g_return_val_if_fail (CD_IS_PROFILE (profile), NULL);
1125 return g_hash_table_lookup (priv->metadata, key);
1126 }
1127
1128 /**
1129 * cd_profile_get_score:
1130 *
1131 * Profiles from official vendors such as http://www.color.org/ should be
1132 * more important than profiles generated from source data.
1133 *
1134 * Return value: A number which corresponds to the importance of the profile,
1135 * where larger numbers have more importance than lower numbers.
1136 **/
1137 guint
1138 cd_profile_get_score (CdProfile *profile)
1139 {
1140 CdProfilePrivate *priv = GET_PRIVATE (profile);
1141 g_return_val_if_fail (CD_IS_PROFILE (profile), 0);
1142 return priv->score;
1143 }
1144
1145 CdProfileKind
1146 cd_profile_get_kind (CdProfile *profile)
1147 {
1148 CdProfilePrivate *priv = GET_PRIVATE (profile);
1149 g_return_val_if_fail (CD_IS_PROFILE (profile), 0);
1150 return priv->kind;
1151 }
1152
1153 CdColorspace
1154 cd_profile_get_colorspace (CdProfile *profile)
1155 {
1156 CdProfilePrivate *priv = GET_PRIVATE (profile);
1157 g_return_val_if_fail (CD_IS_PROFILE (profile), 0);
1158 return priv->colorspace;
1159 }
1160
1161 gboolean
1162 cd_profile_get_has_vcgt (CdProfile *profile)
1163 {
1164 CdProfilePrivate *priv = GET_PRIVATE (profile);
1165 g_return_val_if_fail (CD_IS_PROFILE (profile), FALSE);
1166 return priv->has_vcgt;
1167 }
1168
1169 const gchar *
1170 cd_profile_get_checksum (CdProfile *profile)
1171 {
1172 CdProfilePrivate *priv = GET_PRIVATE (profile);
1173 g_return_val_if_fail (CD_IS_PROFILE (profile), NULL);
1174 return priv->checksum;
1175 }
1176
1177 static void
1178 cd_profile_name_vanished_cb (GDBusConnection *connection,
1179 const gchar *name,
1180 gpointer user_data)
1181 {
1182 CdProfile *profile = CD_PROFILE (user_data);
1183 g_debug ("CdProfile: emit 'invalidate' as %s vanished", name);
1184 g_signal_emit (profile, signals[SIGNAL_INVALIDATE], 0);
1185 }
1186
1187 void
1188 cd_profile_watch_sender (CdProfile *profile, const gchar *sender)
1189 {
1190 CdProfilePrivate *priv = GET_PRIVATE (profile);
1191 g_return_if_fail (CD_IS_PROFILE (profile));
1192 priv->watcher_id = g_bus_watch_name (G_BUS_TYPE_SYSTEM,
1193 sender,
1194 G_BUS_NAME_WATCHER_FLAGS_NONE,
1195 NULL,
1196 cd_profile_name_vanished_cb,
1197 profile,
1198 NULL);
1199 }
1200
1201 static void
1202 cd_profile_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
1203 {
1204 CdProfile *profile = CD_PROFILE (object);
1205 CdProfilePrivate *priv = GET_PRIVATE (profile);
1206
1207 switch (prop_id) {
1208 case PROP_OBJECT_PATH:
1209 g_value_set_string (value, priv->object_path);
1210 break;
1211 case PROP_ID:
1212 g_value_set_string (value, priv->id);
1213 break;
1214 default:
1215 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1216 break;
1217 }
1218 }
1219
1220 static void
1221 cd_profile_set_property (GObject *object, guint prop_id, const GValue *value, GParamSpec *pspec)
1222 {
1223 CdProfile *profile = CD_PROFILE (object);
1224 CdProfilePrivate *priv = GET_PRIVATE (profile);
1225
1226 switch (prop_id) {
1227 case PROP_OBJECT_PATH:
1228 g_free (priv->object_path);
1229 priv->object_path = g_strdup (g_value_get_string (value));
1230 break;
1231 case PROP_ID:
1232 g_free (priv->id);
1233 priv->id = g_strdup (g_value_get_string (value));
1234 break;
1235 default:
1236 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1237 break;
1238 }
1239 }
1240
1241 static void
1242 cd_profile_class_init (CdProfileClass *klass)
1243 {
1244 GParamSpec *pspec;
1245 GObjectClass *object_class = G_OBJECT_CLASS (klass);
1246 object_class->finalize = cd_profile_finalize;
1247 object_class->get_property = cd_profile_get_property;
1248 object_class->set_property = cd_profile_set_property;
1249
1250 /**
1251 * CdProfile:object-path:
1252 */
1253 pspec = g_param_spec_string ("object-path", NULL, NULL,
1254 NULL,
1255 G_PARAM_READWRITE);
1256 g_object_class_install_property (object_class, PROP_OBJECT_PATH, pspec);
1257
1258 /**
1259 * CdProfile:id:
1260 */
1261 pspec = g_param_spec_string ("id", NULL, NULL,
1262 NULL,
1263 G_PARAM_READWRITE);
1264 g_object_class_install_property (object_class, PROP_ID, pspec);
1265
1266 /**
1267 * CdProfile::invalidate:
1268 **/
1269 signals[SIGNAL_INVALIDATE] =
1270 g_signal_new ("invalidate",
1271 G_TYPE_FROM_CLASS (object_class), G_SIGNAL_RUN_LAST,
1272 G_STRUCT_OFFSET (CdProfileClass, invalidate),
1273 NULL, NULL, g_cclosure_marshal_VOID__VOID,
1274 G_TYPE_NONE, 0);
1275 }
1276
1277 static void
1278 cd_profile_init (CdProfile *profile)
1279 {
1280 CdProfilePrivate *priv = GET_PRIVATE (profile);
1281 priv->db = cd_profile_db_new ();
1282 priv->metadata = g_hash_table_new_full (g_str_hash,
1283 g_str_equal,
1284 g_free,
1285 g_free);
1286 }
1287
1288 static void
1289 cd_profile_finalize (GObject *object)
1290 {
1291 CdProfile *profile = CD_PROFILE (object);
1292 CdProfilePrivate *priv = GET_PRIVATE (profile);
1293
1294 if (priv->watcher_id > 0)
1295 g_bus_unwatch_name (priv->watcher_id);
1296 if (priv->registration_id > 0) {
1297 g_dbus_connection_unregister_object (priv->connection,
1298 priv->registration_id);
1299 }
1300 if (priv->mapped_file != NULL)
1301 g_mapped_file_unref (priv->mapped_file);
1302 g_free (priv->filename);
1303 g_free (priv->qualifier);
1304 g_free (priv->format);
1305 g_free (priv->title);
1306 g_free (priv->id);
1307 g_free (priv->checksum);
1308 g_free (priv->object_path);
1309 g_object_unref (priv->db);
1310 g_strfreev (priv->warnings);
1311 g_hash_table_unref (priv->metadata);
1312
1313 G_OBJECT_CLASS (cd_profile_parent_class)->finalize (object);
1314 }
1315
1316 CdProfile *
1317 cd_profile_new (void)
1318 {
1319 CdProfile *profile;
1320 profile = g_object_new (CD_TYPE_PROFILE, NULL);
1321 return CD_PROFILE (profile);
1322 }
1323