Debian Codegraph @

examples/cd-dbus-use-qualifier.c

raw
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
2 *
3 * Copyright (C) 2011 Richard Hughes <[email protected]>
4 *
5 * Licensed under the GNU Lesser General Public License Version 2.1
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library 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 GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include <dbus/dbus.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26
27 #define QUAL_COLORSPACE 0
28 #define QUAL_MEDIA 1
29 #define QUAL_RESOLUTION 2
30 #define QUAL_SIZE 3
31
32 static char **
33 split_qualifier (const char *qualifier)
34 {
35 char *qualifier_copy;
36 char **split = NULL;
37 char *tmp;
38
39 /* allocate enough for a null termination */
40 split = calloc(QUAL_SIZE + 1, sizeof(char*));
41
42 /* work on a copy as we muller the string */
43 qualifier_copy = strdup(qualifier);
44
45 /* get colorspace */
46 tmp = strstr(qualifier_copy, ".");
47 if (tmp == NULL)
48 goto out;
49 tmp[0] = '\0';
50 split[QUAL_COLORSPACE] = strdup(qualifier_copy);
51
52 /* get paper type */
53 split[QUAL_MEDIA] = strdup(tmp + 1);
54 tmp = strstr(split[QUAL_MEDIA], ".");
55 if (tmp == NULL)
56 goto out;
57 tmp[0] = '\0';
58
59 /* get resolution */
60 split[QUAL_RESOLUTION] = strdup(tmp + 1);
61 out:
62 free(qualifier_copy);
63 return split;
64 }
65
66 static char *
67 get_filename_for_profile_path (DBusConnection *con,
68 const char *object_path)
69 {
70 char *filename = NULL;
71 const char *interface = "org.freedesktop.ColorManager.Profile";
72 const char *property = "Filename";
73 const char *tmp;
74 DBusError error;
75 DBusMessageIter args;
76 DBusMessage *message = NULL;
77 DBusMessage *reply = NULL;
78 DBusMessageIter sub;
79
80 message = dbus_message_new_method_call("org.freedesktop.ColorManager",
81 object_path,
82 "org.freedesktop.DBus.Properties",
83 "Get");
84
85 dbus_message_iter_init_append(message, &args);
86 dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &interface);
87 dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &property);
88
89 /* send synchronous */
90 dbus_error_init(&error);
91 printf("Calling %s.Get(%s)\n", interface, property);
92 reply = dbus_connection_send_with_reply_and_block(con,
93 message,
94 -1,
95 &error);
96 if (reply == NULL) {
97 printf("failed to send: %s:%s\n",
98 error.name, error.message);
99 dbus_error_free(&error);
100 goto out;
101 }
102
103 /* get reply data */
104 dbus_message_iter_init(reply, &args);
105 if (dbus_message_iter_get_arg_type(&args) != DBUS_TYPE_VARIANT) {
106 printf("incorrect reply type\n");
107 goto out;
108 }
109
110 dbus_message_iter_recurse(&args, &sub);
111 dbus_message_iter_get_basic(&sub, &tmp);
112 filename = strdup(tmp);
113 out:
114 if (message != NULL)
115 dbus_message_unref(message);
116 if (reply != NULL)
117 dbus_message_unref(reply);
118 return filename;
119 }
120
121 static char *
122 get_profile_for_device_path (DBusConnection *con,
123 const char *object_path,
124 const char *qualifier)
125 {
126 char **key = NULL;
127 char *profile = NULL;
128 char **split = NULL;
129 char str[256];
130 const char *tmp;
131 DBusError error;
132 DBusMessageIter args;
133 DBusMessageIter entry;
134 DBusMessage *message = NULL;
135 DBusMessage *reply = NULL;
136 int i = 0;
137
138 message = dbus_message_new_method_call("org.freedesktop.ColorManager",
139 object_path,
140 "org.freedesktop.ColorManager.Device",
141 "GetProfileForQualifiers");
142 dbus_message_iter_init_append(message, &args);
143
144 /* split qualifier */
145 split = split_qualifier(qualifier);
146 if (split == NULL)
147 goto out;
148
149 /* create the fallbacks */
150 key = calloc(6, sizeof(char*));
151 if (key == NULL)
152 goto out;
153
154 /* exact match */
155 snprintf(str, 256, "%s.%s.%s",
156 split[QUAL_COLORSPACE],
157 split[QUAL_MEDIA],
158 split[QUAL_RESOLUTION]);
159 key[i++] = strdup(str);
160 snprintf(str, 256, "%s.%s.*",
161 split[QUAL_COLORSPACE],
162 split[QUAL_MEDIA]);
163 key[i++] = strdup(str);
164 snprintf(str, 256, "%s.*.%s",
165 split[QUAL_COLORSPACE],
166 split[QUAL_RESOLUTION]);
167 key[i++] = strdup(str);
168 snprintf(str, 256, "%s.*.*",
169 split[QUAL_COLORSPACE]);
170 key[i++] = strdup(str);
171 key[i++] = strdup("*");
172 printf("specified %i qualifiers\n", i);
173 dbus_message_iter_open_container(&args,
174 DBUS_TYPE_ARRAY,
175 "s",
176 &entry);
177 for (i = 0; key[i] != NULL; i++) {
178 dbus_message_iter_append_basic(&entry,
179 DBUS_TYPE_STRING,
180 &key[i]);
181 }
182 dbus_message_iter_close_container(&args, &entry);
183
184 /* send synchronous */
185 dbus_error_init(&error);
186 printf("Calling GetProfileForQualifiers(%s...)\n", key[0]);
187 reply = dbus_connection_send_with_reply_and_block(con,
188 message,
189 -1,
190 &error);
191 if (reply == NULL) {
192 printf("failed to send: %s:%s\n",
193 error.name, error.message);
194 dbus_error_free(&error);
195 goto out;
196 }
197
198 /* get reply data */
199 dbus_message_iter_init(reply, &args);
200 if (dbus_message_iter_get_arg_type(&args) != DBUS_TYPE_OBJECT_PATH) {
201 printf("incorrect reply type\n");
202 goto out;
203 }
204 dbus_message_iter_get_basic(&args, &tmp);
205 printf("found profile %s\n", tmp);
206
207 /* get filename */
208 profile = get_filename_for_profile_path(con, tmp);
209
210 out:
211 if (message != NULL)
212 dbus_message_unref(message);
213 if (reply != NULL)
214 dbus_message_unref(reply);
215 if (key != NULL) {
216 for (i = 0; key[i] != NULL; i++)
217 free(key[i]);
218 free(key);
219 }
220 if (split != NULL) {
221 for (i = 0; split[i] != NULL; i++)
222 free(split[i]);
223 free(split);
224 }
225 return profile;
226 }
227
228 static char *
229 get_profile_for_device_id (DBusConnection *con,
230 const char *device_id,
231 const char *qualifier)
232 {
233 char *profile = NULL;
234 const char *device_path_tmp;
235 DBusError error;
236 DBusMessageIter args;
237 DBusMessage *message = NULL;
238 DBusMessage *reply = NULL;
239
240 message = dbus_message_new_method_call("org.freedesktop.ColorManager",
241 "/org/freedesktop/ColorManager",
242 "org.freedesktop.ColorManager",
243 "FindDeviceById");
244 dbus_message_iter_init_append(message, &args);
245 dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &device_id);
246
247 /* send synchronous */
248 dbus_error_init(&error);
249 printf("Calling FindDeviceById(%s)\n", device_id);
250 reply = dbus_connection_send_with_reply_and_block(con,
251 message,
252 -1,
253 &error);
254 if (reply == NULL) {
255 printf("failed to send: %s:%s\n",
256 error.name, error.message);
257 dbus_error_free(&error);
258 goto out;
259 }
260
261 /* get reply data */
262 dbus_message_iter_init(reply, &args);
263 if (dbus_message_iter_get_arg_type(&args) != DBUS_TYPE_OBJECT_PATH) {
264 printf("incorrect reply type\n");
265 goto out;
266 }
267 dbus_message_iter_get_basic(&args, &device_path_tmp);
268 printf("found device %s\n", device_path_tmp);
269 profile = get_profile_for_device_path(con, device_path_tmp, qualifier);
270 out:
271 if (message != NULL)
272 dbus_message_unref(message);
273 if (reply != NULL)
274 dbus_message_unref(reply);
275 return profile;
276 }
277
278 int
279 main (int argc, char **argv)
280 {
281 DBusConnection *con = NULL;
282 char *filename = NULL;
283
284 /* check number of arguments */
285 if (argc != 3) {
286 printf("expected [device-id] [qualifier]\n");
287 printf(" e.g. \"cups-Photosmart-B109a-m\" \"RGB.Glossy.300dpi\"\n");
288 goto out;
289 }
290
291 /* connect to system bus */
292 con = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
293 if (con == NULL) {
294 printf("failed to connect to system bus\n");
295 goto out;
296 }
297
298 /* get the best profile for the device */
299 filename = get_profile_for_device_id (con, argv[1], argv[2]);
300 if (filename == NULL) {
301 printf("failed to get profile filename!\n");
302 goto out;
303 }
304 printf("Use profile filename: %s\n", filename);
305 out:
306 free (filename);
307 if (con != NULL)
308 dbus_connection_unref(con);
309 return 0;
310 }