summaryrefslogtreecommitdiffstats
path: root/plugins/color/gsd-color-profiles.c
blob: c73abd71d3dc436638e574c390c11debe0b2bee7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
 *
 * Copyright (C) 2007 William Jon McCann <mccann@jhu.edu>
 * Copyright (C) 2011-2013 Richard Hughes <richard@hughsie.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
 *
 */

#include "config.h"

#include <glib/gi18n.h>
#include <colord.h>

#include "gsd-color-profiles.h"

struct _GsdColorProfiles
{
        GObject          parent;

        GCancellable    *cancellable;
        CdClient        *client;
        CdIccStore      *icc_store;
};

static void     gsd_color_profiles_class_init  (GsdColorProfilesClass *klass);
static void     gsd_color_profiles_init        (GsdColorProfiles      *color_profiles);
static void     gsd_color_profiles_finalize    (GObject             *object);

G_DEFINE_TYPE (GsdColorProfiles, gsd_color_profiles, G_TYPE_OBJECT)

static void
gsd_color_profiles_class_init (GsdColorProfilesClass *klass)
{
        GObjectClass   *object_class = G_OBJECT_CLASS (klass);

        object_class->finalize = gsd_color_profiles_finalize;
}

static void
gcm_session_client_connect_cb (GObject *source_object,
                               GAsyncResult *res,
                               gpointer user_data)
{
        gboolean ret;
        GError *error = NULL;
        CdClient *client = CD_CLIENT (source_object);
        GsdColorProfiles *profiles;

        /* connected */
        ret = cd_client_connect_finish (client, res, &error);
        if (!ret) {
                if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
                        g_warning ("failed to connect to colord: %s", error->message);
                g_error_free (error);
                return;
        }

        /* is there an available colord instance? */
        profiles = GSD_COLOR_PROFILES (user_data);
        ret = cd_client_get_has_server (profiles->client);
        if (!ret) {
                g_warning ("There is no colord server available");
                return;
        }

        /* add profiles */
        ret = cd_icc_store_search_kind (profiles->icc_store,
                                        CD_ICC_STORE_SEARCH_KIND_USER,
                                        CD_ICC_STORE_SEARCH_FLAGS_CREATE_LOCATION,
                                        profiles->cancellable,
                                        &error);
        if (!ret) {
                g_warning ("failed to add user icc: %s", error->message);
                g_error_free (error);
        }
}

gboolean
gsd_color_profiles_start (GsdColorProfiles *profiles,
                          GError          **error)
{
        /* use a fresh cancellable for each start->stop operation */
        g_cancellable_cancel (profiles->cancellable);
        g_clear_object (&profiles->cancellable);
        profiles->cancellable = g_cancellable_new ();

        cd_client_connect (profiles->client,
                           profiles->cancellable,
                           gcm_session_client_connect_cb,
                           profiles);

        return TRUE;
}

void
gsd_color_profiles_stop (GsdColorProfiles *profiles)
{
        g_cancellable_cancel (profiles->cancellable);
}

static void
gcm_session_create_profile_cb (GObject *object,
                               GAsyncResult *res,
                               gpointer user_data)
{
        CdProfile *profile;
        GError *error = NULL;
        CdClient *client = CD_CLIENT (object);

        profile = cd_client_create_profile_finish (client, res, &error);
        if (profile == NULL) {
                if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED) &&
                    !g_error_matches (error, CD_CLIENT_ERROR, CD_CLIENT_ERROR_ALREADY_EXISTS))
                        g_warning ("%s", error->message);
                g_error_free (error);
                return;
        }
        g_object_unref (profile);
}

static void
gcm_session_icc_store_added_cb (CdIccStore *icc_store,
                                CdIcc *icc,
                                GsdColorProfiles *profiles)
{
        cd_client_create_profile_for_icc (profiles->client,
                                          icc,
                                          CD_OBJECT_SCOPE_TEMP,
                                          profiles->cancellable,
                                          gcm_session_create_profile_cb,
                                          profiles);
}

static void
gcm_session_delete_profile_cb (GObject *object,
                               GAsyncResult *res,
                               gpointer user_data)
{
        gboolean ret;
        GError *error = NULL;
        CdClient *client = CD_CLIENT (object);

        ret = cd_client_delete_profile_finish (client, res, &error);
        if (!ret) {
                if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
                        g_warning ("%s", error->message);
                g_error_free (error);
        }
}

static void
gcm_session_find_profile_by_filename_cb (GObject *object,
                                         GAsyncResult *res,
                                         gpointer user_data)
{
        GError *error = NULL;
        CdProfile *profile;
        CdClient *client = CD_CLIENT (object);
        GsdColorProfiles *profiles = GSD_COLOR_PROFILES (user_data);

        profile = cd_client_find_profile_by_filename_finish (client, res, &error);
        if (profile == NULL) {
                if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
                        g_warning ("%s", error->message);
                g_error_free (error);
                goto out;
        }

        /* remove it from colord */
        cd_client_delete_profile (profiles->client,
                                  profile,
                                  profiles->cancellable,
                                  gcm_session_delete_profile_cb,
                                  profiles);
out:
        if (profile != NULL)
                g_object_unref (profile);
}

static void
gcm_session_icc_store_removed_cb (CdIccStore *icc_store,
                                  CdIcc *icc,
                                  GsdColorProfiles *profiles)
{
        /* find the ID for the filename */
        g_debug ("filename %s removed", cd_icc_get_filename (icc));
        cd_client_find_profile_by_filename (profiles->client,
                                            cd_icc_get_filename (icc),
                                            profiles->cancellable,
                                            gcm_session_find_profile_by_filename_cb,
                                            profiles);
}

static void
gsd_color_profiles_init (GsdColorProfiles *profiles)
{
        /* have access to all user profiles */
        profiles->client = cd_client_new ();
        profiles->icc_store = cd_icc_store_new ();
        cd_icc_store_set_load_flags (profiles->icc_store,
                                     CD_ICC_LOAD_FLAGS_FALLBACK_MD5);
        g_signal_connect (profiles->icc_store, "added",
                          G_CALLBACK (gcm_session_icc_store_added_cb),
                          profiles);
        g_signal_connect (profiles->icc_store, "removed",
                          G_CALLBACK (gcm_session_icc_store_removed_cb),
                          profiles);
}

static void
gsd_color_profiles_finalize (GObject *object)
{
        GsdColorProfiles *profiles;

        g_return_if_fail (object != NULL);
        g_return_if_fail (GSD_IS_COLOR_PROFILES (object));

        profiles = GSD_COLOR_PROFILES (object);

        g_cancellable_cancel (profiles->cancellable);
        g_clear_object (&profiles->cancellable);
        g_clear_object (&profiles->icc_store);
        g_clear_object (&profiles->client);

        G_OBJECT_CLASS (gsd_color_profiles_parent_class)->finalize (object);
}

GsdColorProfiles *
gsd_color_profiles_new (void)
{
        GsdColorProfiles *profiles;
        profiles = g_object_new (GSD_TYPE_COLOR_PROFILES, NULL);
        return GSD_COLOR_PROFILES (profiles);
}