~ubuntu-branches/ubuntu/quantal/colord/quantal-proposed

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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
 *
 * Copyright (C) 2011 Richard Hughes <richard@hughsie.com>
 *
 * Licensed under the GNU Lesser General Public License Version 2.1
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
 */

#include <dbus/dbus.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define QUAL_COLORSPACE		0
#define QUAL_MEDIA		1
#define QUAL_RESOLUTION		2
#define QUAL_SIZE		3

static char **
split_qualifier (const char *qualifier)
{
	char *qualifier_copy;
	char **split = NULL;
	char *tmp;

	/* allocate enough for a null termination */
	split = calloc(QUAL_SIZE + 1, sizeof(char*));

	/* work on a copy as we muller the string */
	qualifier_copy = strdup(qualifier);

	/* get colorspace */
	tmp = strstr(qualifier_copy, ".");
	if (tmp == NULL)
		goto out;
	tmp[0] = '\0';
	split[QUAL_COLORSPACE] = strdup(qualifier_copy);

	/* get paper type */
	split[QUAL_MEDIA] = strdup(tmp + 1);
	tmp = strstr(split[QUAL_MEDIA], ".");
	if (tmp == NULL)
		goto out;
	tmp[0] = '\0';

	/* get resulution */
	split[QUAL_RESOLUTION] = strdup(tmp + 1);
out:
	free(qualifier_copy);
	return split;
}

static char *
get_filename_for_profile_path (DBusConnection *con,
			       const char *object_path)
{
	char *filename = NULL;
	const char *interface = "org.freedesktop.ColorManager.Profile";
	const char *property = "Filename";
	const char *tmp;
	DBusError error;
	DBusMessageIter args;
	DBusMessage *message = NULL;
	DBusMessage *reply = NULL;
	DBusMessageIter sub;

	message = dbus_message_new_method_call("org.freedesktop.ColorManager",
					       object_path,
					       "org.freedesktop.DBus.Properties",
					       "Get");

	dbus_message_iter_init_append(message, &args);
	dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &interface);
	dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &property);

	/* send syncronous */
	dbus_error_init(&error);
	printf("Calling %s.Get(%s)\n", interface, property);
	reply = dbus_connection_send_with_reply_and_block(con,
							  message,
							  -1,
							  &error);
	if (reply == NULL) {
		printf("failed to send: %s:%s\n",
		       error.name, error.message);
		dbus_error_free(&error);
		goto out;
	}

	/* get reply data */
        dbus_message_iter_init(reply, &args);
	if (dbus_message_iter_get_arg_type(&args) != DBUS_TYPE_VARIANT) {
		printf("incorrect reply type\n");
		goto out;
	}

	dbus_message_iter_recurse(&args, &sub);
	dbus_message_iter_get_basic(&sub, &tmp);
	filename = strdup(tmp);
out:
	if (message != NULL)
		dbus_message_unref(message);
	if (reply != NULL)
		dbus_message_unref(reply);
	return filename;
}

static char *
get_profile_for_device_path (DBusConnection *con,
			     const char *object_path,
			     const char *qualifier)
{
	char **key = NULL;
	char *profile = NULL;
	char **split = NULL;
	char str[256];
	const char *tmp;
	DBusError error;
	DBusMessageIter args;
	DBusMessageIter entry;
	DBusMessage *message = NULL;
	DBusMessage *reply = NULL;
	int i = 0;

	message = dbus_message_new_method_call("org.freedesktop.ColorManager",
					       object_path,
					       "org.freedesktop.ColorManager.Device",
					       "GetProfileForQualifiers");
	dbus_message_iter_init_append(message, &args);

	/* split qualifier */
	split = split_qualifier(qualifier);

	/* create the fallbacks */
	key = calloc(6, sizeof(char*));

	/* exact match */
	snprintf(str, 256, "%s.%s.%s",
		 split[QUAL_COLORSPACE],
		 split[QUAL_MEDIA],
		 split[QUAL_RESOLUTION]);
	key[i++] = strdup(str);
	snprintf(str, 256, "%s.%s.*",
		 split[QUAL_COLORSPACE],
		 split[QUAL_MEDIA]);
	key[i++] = strdup(str);
	snprintf(str, 256, "%s.*.%s",
		 split[QUAL_COLORSPACE],
		 split[QUAL_RESOLUTION]);
	key[i++] = strdup(str);
	snprintf(str, 256, "%s.*.*",
		 split[QUAL_COLORSPACE]);
	key[i++] = strdup(str);
	key[i++] = strdup("*");
	printf("specified %i qualifiers\n", i);
	dbus_message_iter_open_container(&args,
					 DBUS_TYPE_ARRAY,
					 "s",
					 &entry);
	for (i=0; key[i] != NULL; i++) {
		dbus_message_iter_append_basic(&entry,
					       DBUS_TYPE_STRING,
					       &key[i]);
	}
	dbus_message_iter_close_container(&args, &entry);

	/* send syncronous */
	dbus_error_init(&error);
	printf("Calling GetProfileForQualifiers(%s...)\n", key[0]);
	reply = dbus_connection_send_with_reply_and_block(con,
							  message,
							  -1,
							  &error);
	if (reply == NULL) {
		printf("failed to send: %s:%s\n",
		       error.name, error.message);
		dbus_error_free(&error);
		goto out;
	}

	/* get reply data */
        dbus_message_iter_init(reply, &args);
	if (dbus_message_iter_get_arg_type(&args) != DBUS_TYPE_OBJECT_PATH) {
		printf("incorrect reply type\n");
		goto out;
	}
        dbus_message_iter_get_basic(&args, &tmp);
	printf("found profile %s\n", tmp);

	/* get filename */
	profile = get_filename_for_profile_path(con, tmp);

out:
	if (message != NULL)
		dbus_message_unref(message);
	if (reply != NULL)
		dbus_message_unref(reply);
	if (key != NULL) {
		for (i=0; key[i] != NULL; i++)
			free(key[i]);
		free(key);
	}
	if (split != NULL) {
		for (i=0; split[i] != NULL; i++)
			free(split[i]);
		free(split);
	}
	return profile;
}

static char *
get_profile_for_device_id (DBusConnection *con,
			   const char *device_id,
			   const char *qualifier)
{
	char *profile = NULL;
	const char *device_path_tmp;
	DBusError error;
	DBusMessageIter args;
	DBusMessage *message = NULL;
	DBusMessage *reply = NULL;

	message = dbus_message_new_method_call("org.freedesktop.ColorManager",
					       "/org/freedesktop/ColorManager",
					       "org.freedesktop.ColorManager",
					       "FindDeviceById");
	dbus_message_iter_init_append(message, &args);
	dbus_message_iter_append_basic(&args, DBUS_TYPE_STRING, &device_id);

	/* send syncronous */
	dbus_error_init(&error);
	printf("Calling FindDeviceById(%s)\n", device_id);
	reply = dbus_connection_send_with_reply_and_block(con,
							  message,
							  -1,
							  &error);
	if (reply == NULL) {
		printf("failed to send: %s:%s\n",
			  error.name, error.message);
		dbus_error_free(&error);
		goto out;
	}

	/* get reply data */
        dbus_message_iter_init(reply, &args);
	if (dbus_message_iter_get_arg_type(&args) != DBUS_TYPE_OBJECT_PATH) {
		printf("incorrect reply type\n");
		goto out;
	}
        dbus_message_iter_get_basic(&args, &device_path_tmp);
	printf("found device %s\n", device_path_tmp);
	profile = get_profile_for_device_path(con, device_path_tmp, qualifier);
out:
	if (message != NULL)
		dbus_message_unref(message);
	if (reply != NULL)
		dbus_message_unref(reply);
	return profile;
}

int
main (int argc, char **argv)
{
	DBusConnection *con = NULL;
	char *filename = NULL;

	/* check number of arguments */
	if (argc != 3) {
		printf("expected [device-id] [qualifier]\n");
		printf(" e.g. \"cups-Photosmart-B109a-m\" \"RGB.Glossy.300dpi\"\n");
		goto out;
	}

	/* connect to system bus */
	con = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
	if (con == NULL) {
		printf("failed to connect to system bus\n");
		goto out;
	}

	/* get the best profile for the device */
	filename = get_profile_for_device_id (con, argv[1], argv[2]);
	if (filename == NULL) {
		printf("failed to get profile filename!\n");
		goto out;
	}
	printf("Use profile filename: %s\n", filename);
out:
	free (filename);
	if (con != NULL)
		dbus_connection_unref(con);
	return 0;
}