gcm-prefs.c 88.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
 *
 * Copyright (C) 2009 Richard Hughes <richard@hughsie.com>
 *
 * Licensed under the GNU General Public License Version 2
 *
 * 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
15
 * GNU General Public License for more details.
16 17 18
 *
 * You should have received a copy of the GNU General Public License
 * 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 23
#include "config.h"

24 25 26
#include <glib/gi18n.h>
#include <gtk/gtk.h>
#include <math.h>
27
#include <unique/unique.h>
28
#include <glib/gstdio.h>
29
#include <gudev/gudev.h>
30
#include <libgnomeui/gnome-rr.h>
31
#include <gconf/gconf-client.h>
32
#include <locale.h>
33 34 35

#include "egg-debug.h"

36
#include "gcm-calibrate-argyll.h"
37
#include "gcm-cie-widget.h"
38 39 40
#include "gcm-client.h"
#include "gcm-color-device.h"
#include "gcm-profile.h"
41
#include "gcm-profile-store.h"
42
#include "gcm-trc-widget.h"
43 44
#include "gcm-utils.h"
#include "gcm-xyz.h"
45

46 47 48 49
/* DISTROS: you will have to patch if you have changed the name of these packages */
#define GCM_PREFS_PACKAGE_NAME_SHARED_COLOR_TARGETS	"shared-color-targets"
#define GCM_PREFS_PACKAGE_NAME_ARGYLLCMS		"argyllcms"

50
static GtkBuilder *builder = NULL;
51
static GtkListStore *list_store_devices = NULL;
52
static GtkListStore *list_store_profiles = NULL;
53
static GcmDevice *current_device = NULL;
54
static GcmProfileStore *profile_store = NULL;
55
static GcmClient *gcm_client = NULL;
56
static GcmColorDevice *color_device = NULL;
57 58
static gboolean setting_up_device = FALSE;
static GtkWidget *info_bar = NULL;
59
static GtkWidget *cie_widget = NULL;
60
static GtkWidget *trc_widget = NULL;
61
static guint loading_refcount = 0;
62
static GConfClient *gconf_client = NULL;
63 64

enum {
65 66 67 68 69
	GCM_DEVICES_COLUMN_ID,
	GCM_DEVICES_COLUMN_SORT,
	GCM_DEVICES_COLUMN_ICON,
	GCM_DEVICES_COLUMN_TITLE,
	GCM_DEVICES_COLUMN_LAST
70
};
71

72
enum {
73 74 75 76 77 78 79 80 81 82 83
	GCM_PROFILES_COLUMN_ID,
	GCM_PROFILES_COLUMN_SORT,
	GCM_PROFILES_COLUMN_ICON,
	GCM_PROFILES_COLUMN_TITLE,
	GCM_PROFILES_COLUMN_PROFILE,
	GCM_PROFILES_COLUMN_LAST
};

enum {
	GCM_PREFS_COMBO_COLUMN_TEXT,
	GCM_PREFS_COMBO_COLUMN_PROFILE,
84
	GCM_PREFS_COMBO_COLUMN_TYPE,
85
	GCM_PREFS_COMBO_COLUMN_LAST
86 87
};

88 89 90 91 92 93 94
typedef enum {
	GCM_PREFS_ENTRY_TYPE_PROFILE,
	GCM_PREFS_ENTRY_TYPE_NONE,
	GCM_PREFS_ENTRY_TYPE_IMPORT,
	GCM_PREFS_ENTRY_TYPE_LAST
} GcmPrefsEntryType;

95 96
static void gcm_prefs_devices_treeview_clicked_cb (GtkTreeSelection *selection, gpointer userdata);

97 98 99 100 101 102 103 104 105 106
/**
 * gcm_prefs_close_cb:
 **/
static void
gcm_prefs_close_cb (GtkWidget *widget, gpointer data)
{
	GMainLoop *loop = (GMainLoop *) data;
	g_main_loop_quit (loop);
}

107 108 109 110 111 112 113
/**
 * gcm_prefs_set_default:
 **/
static gboolean
gcm_prefs_set_default (GcmDevice *device)
{
	GError *error = NULL;
114
	gboolean ret = FALSE;
115 116 117
	gchar *cmdline = NULL;
	gchar *filename = NULL;
	gchar *id = NULL;
118
	gchar *install_cmd = NULL;
119 120 121 122 123 124 125 126

	/* get device properties */
	g_object_get (device,
		      "profile-filename", &filename,
		      "id", &id,
		      NULL);

	/* nothing set */
127 128
	if (filename == NULL) {
		egg_debug ("no filename for %s", id);
129
		goto out;
130
	}
131 132

	/* run using PolicyKit */
133
	install_cmd = g_build_filename (SBINDIR, "gcm-install-system-wide", NULL);
134 135 136 137 138 139 140 141 142 143
	cmdline = g_strdup_printf ("pkexec %s --id %s \"%s\"", install_cmd, id, filename);
	egg_debug ("running: %s", cmdline);
	ret = g_spawn_command_line_sync (cmdline, NULL, NULL, NULL, &error);
	if (!ret) {
		egg_warning ("failed to set default: %s", error->message);
		g_error_free (error);
		goto out;
	}
out:
	g_free (id);
144
	g_free (install_cmd);
145 146 147 148 149
	g_free (cmdline);
	g_free (filename);
	return ret;
}

150 151 152 153
/**
 * gcm_prefs_combobox_add_profile:
 **/
static void
154
gcm_prefs_combobox_add_profile (GtkWidget *widget, GcmProfile *profile, GcmPrefsEntryType entry_type)
155 156 157 158 159 160
{
	GtkTreeModel *model;
	GtkTreeIter iter;
	gchar *description;

	/* use description */
161 162
	if (entry_type == GCM_PREFS_ENTRY_TYPE_NONE) {
		/* TRANSLATORS: this is where no profile is selected */
163
		description = g_strdup (_("None"));
164 165 166
	} else if (entry_type == GCM_PREFS_ENTRY_TYPE_IMPORT) {
		/* TRANSLATORS: this is where the user can click and import a profile */
		description = g_strdup (_("Other profile..."));
167 168 169 170 171 172 173 174 175 176 177 178
	} else {
		g_object_get (profile,
			      "description", &description,
			      NULL);
	}

	/* also add profile */
	model = gtk_combo_box_get_model (GTK_COMBO_BOX(widget));
	gtk_list_store_append (GTK_LIST_STORE(model), &iter);
	gtk_list_store_set (GTK_LIST_STORE(model), &iter,
			    GCM_PREFS_COMBO_COLUMN_TEXT, description,
			    GCM_PREFS_COMBO_COLUMN_PROFILE, profile,
179
			    GCM_PREFS_COMBO_COLUMN_TYPE, entry_type,
180 181 182 183
			    -1);
	g_free (description);
}

184 185 186 187 188 189 190 191
/**
 * gcm_prefs_default_cb:
 **/
static void
gcm_prefs_default_cb (GtkWidget *widget, gpointer data)
{
	GPtrArray *array = NULL;
	GcmDevice *device;
192
	GcmDeviceTypeEnum type;
193 194 195 196 197 198 199 200 201 202 203 204
	gboolean ret;
	guint i;

	/* set for each output */
	array = gcm_client_get_devices (gcm_client);
	for (i=0; i<array->len; i++) {
		device = g_ptr_array_index (array, i);
		g_object_get (device,
			      "type", &type,
			      NULL);

		/* not a xrandr panel */
205
		if (type != GCM_DEVICE_TYPE_ENUM_DISPLAY)
206 207 208 209 210 211 212 213 214 215
			continue;

		/* set for this device */
		ret = gcm_prefs_set_default (device);
		if (!ret)
			break;
	}
	g_ptr_array_unref (array);
}

216
/**
217
 * gcm_prefs_help_cb:
218
 **/
219 220
static void
gcm_prefs_help_cb (GtkWidget *widget, gpointer data)
221
{
222
	gcm_gnome_help ("preferences");
223 224 225
}

/**
226
 * gcm_prefs_delete_event_cb:
227
 **/
228 229
static gboolean
gcm_prefs_delete_event_cb (GtkWidget *widget, GdkEvent *event, gpointer data)
230
{
231 232
	gcm_prefs_close_cb (widget, data);
	return FALSE;
233 234
}

235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
/**
 * gcm_prefs_get_time:
 **/
static gchar *
gcm_prefs_get_time (void)
{
	gchar *text;
	time_t c_time;

	/* get the time now */
	time (&c_time);
	text = g_new0 (gchar, 255);

	/* format text */
	strftime (text, 254, "%H-%M-%S", localtime (&c_time));
	return text;
}

253 254 255 256 257 258 259 260 261
/**
 * gcm_prefs_calibrate_get_basename:
 **/
static gchar *
gcm_prefs_calibrate_get_basename (GcmDevice *device)
{
	gchar *serial = NULL;
	gchar *manufacturer = NULL;
	gchar *model = NULL;
262
	gchar *timespec = NULL;
263 264 265 266 267 268 269 270 271 272 273 274 275
	GDate *date = NULL;
	GString *basename;

	/* get device properties */
	g_object_get (device,
		      "serial", &serial,
		      "manufacturer", &manufacturer,
		      "model", &model,
		      NULL);

	/* create date and set it to now */
	date = g_date_new ();
	g_date_set_time_t (date, time (NULL));
276
	timespec = gcm_prefs_get_time ();
277 278

	/* form basename */
279
	basename = g_string_new ("GCM");
280
	if (manufacturer != NULL)
281
		g_string_append_printf (basename, " - %s", manufacturer);
282
	if (model != NULL)
283
		g_string_append_printf (basename, " - %s", model);
284
	if (serial != NULL)
285 286 287 288 289 290
		g_string_append_printf (basename, " - %s", serial);
	g_string_append_printf (basename, " (%04i-%02i-%02i)", date->year, date->month, date->day);

	/* maybe configure in GConf? */
	if (0)
		g_string_append_printf (basename, " [%s]", timespec);
291 292 293 294 295

	g_date_free (date);
	g_free (serial);
	g_free (manufacturer);
	g_free (model);
296
	g_free (timespec);
297 298 299
	return g_string_free (basename, FALSE);
}

300
/**
301
 * gcm_prefs_calibrate_display:
302
 **/
303
static gboolean
304
gcm_prefs_calibrate_display (GcmCalibrate *calibrate)
305
{
306
	gboolean ret = FALSE;
307
	gboolean ret_tmp;
308
	GError *error = NULL;
309
	gchar *output_name = NULL;
310 311 312 313
	gchar *basename = NULL;
	gchar *manufacturer = NULL;
	gchar *model = NULL;
	gchar *description = NULL;
314
	gchar *device = NULL;
315
	GtkWindow *window;
316

317 318 319 320
	/* no device */
	if (current_device == NULL)
		goto out;

321
	/* get the device */
322
	g_object_get (current_device,
323
		      "native-device-xrandr", &output_name,
324 325 326 327
		      "serial", &basename,
		      "manufacturer", &manufacturer,
		      "model", &model,
		      "title", &description,
328
		      NULL);
329
	if (output_name == NULL) {
330 331 332 333
		egg_warning ("failed to get output");
		goto out;
	}

334
	/* get a filename based on the serial number */
335
	basename = gcm_prefs_calibrate_get_basename (current_device);
336 337

	/* get model */
338 339 340 341
	if (model == NULL) {
		/* TRANSLATORS: this is saved in the profile */
		model = g_strdup (_("Unknown model"));
	}
342 343

	/* get description */
344 345 346 347
	if (description == NULL) {
		/* TRANSLATORS: this is saved in the profile */
		description = g_strdup (_("Unknown display"));
	}
348 349

	/* get manufacturer */
350 351 352 353 354
	if (manufacturer == NULL) {
		/* TRANSLATORS: this is saved in the profile */
		manufacturer = g_strdup (_("Unknown manufacturer"));
	}

355 356 357 358
	/* get calibration device model */
	g_object_get (color_device,
		      "model", &device,
		      NULL);
359

360
	/* get device, harder */
361 362 363 364
	if (device == NULL) {
		/* TRANSLATORS: this is the formattted custom profile description. "Custom" refers to the fact that it's user generated */
		device = g_strdup (_("Custom"));
	}
365 366

	/* set the proper output name */
367
	g_object_set (calibrate,
368
		      "output-name", output_name,
369 370 371 372
		      "basename", basename,
		      "model", model,
		      "description", description,
		      "manufacturer", manufacturer,
373
		      "device", device,
374 375
		      NULL);

376 377
	/* run each task in order */
	window = GTK_WINDOW(gtk_builder_get_object (builder, "dialog_prefs"));
378
	ret = gcm_calibrate_display (calibrate, window, &error);
379 380 381
	if (!ret) {
		egg_warning ("failed to calibrate: %s", error->message);
		g_error_free (error);
382
		goto out;
383
	}
384
out:
385 386
	/* need to set the gamma back to the default after calibration */
	error = NULL;
387 388
	ret_tmp = gcm_utils_set_gamma_for_device (current_device, &error);
	if (!ret_tmp) {
389 390 391 392
		egg_warning ("failed to set output gamma: %s", error->message);
		g_error_free (error);
	}

393
	g_free (device);
394
	g_free (output_name);
395 396 397 398
	g_free (basename);
	g_free (manufacturer);
	g_free (model);
	g_free (description);
399 400 401 402
	return ret;
}

/**
403
 * gcm_prefs_calibrate_device_get_scanned_profile:
404 405
 **/
static gchar *
406
gcm_prefs_calibrate_device_get_scanned_profile (const gchar *directory)
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
{
	gchar *filename = NULL;
	GtkWindow *window;
	GtkWidget *dialog;
	GtkFileFilter *filter;

	/* create new dialog */
	window = GTK_WINDOW(gtk_builder_get_object (builder, "dialog_prefs"));
	/* TRANSLATORS: dialog for file->open dialog */
	dialog = gtk_file_chooser_dialog_new (_("Select scanned reference file"), window,
					       GTK_FILE_CHOOSER_ACTION_OPEN,
					       GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
					       GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
					      NULL);
	gtk_window_set_icon_name (GTK_WINDOW (dialog), GCM_STOCK_ICON);
422
	gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER(dialog), directory);
423 424 425 426 427 428 429 430
	gtk_file_chooser_set_create_folders (GTK_FILE_CHOOSER(dialog), FALSE);

	/* setup the filter */
	filter = gtk_file_filter_new ();
	gtk_file_filter_add_mime_type (filter, "image/tiff");
	/* TRANSLATORS: filter name on the file->open dialog */
	gtk_file_filter_set_name (filter, _("Supported images files"));
	gtk_file_chooser_add_filter (GTK_FILE_CHOOSER(dialog), filter);
431
//	g_object_unref (filter);
432 433 434 435 436 437 438 439 440 441 442 443 444

	/* did user choose file */
	if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
		filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER(dialog));

	/* we're done */
	gtk_widget_destroy (dialog);

	/* or NULL for missing */
	return filename;
}

/**
445
 * gcm_prefs_calibrate_device_get_reference_data:
446 447
 **/
static gchar *
448
gcm_prefs_calibrate_device_get_reference_data (const gchar *directory)
449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
{
	gchar *filename = NULL;
	GtkWindow *window;
	GtkWidget *dialog;
	GtkFileFilter *filter;

	/* create new dialog */
	window = GTK_WINDOW(gtk_builder_get_object (builder, "dialog_prefs"));
	/* TRANSLATORS: dialog for file->open dialog */
	dialog = gtk_file_chooser_dialog_new (_("Select CIE reference values file"), window,
					       GTK_FILE_CHOOSER_ACTION_OPEN,
					       GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
					       GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
					      NULL);
	gtk_window_set_icon_name (GTK_WINDOW (dialog), GCM_STOCK_ICON);
464
	gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER(dialog), directory);
465 466 467 468
	gtk_file_chooser_set_create_folders (GTK_FILE_CHOOSER(dialog), FALSE);

	/* setup the filter */
	filter = gtk_file_filter_new ();
469 470 471
	gtk_file_filter_add_mime_type (filter, "application/x-it87");

	/* we can remove this when we depend on a new shared-mime-info */
472 473
	gtk_file_filter_add_pattern (filter, "*.txt");
	gtk_file_filter_add_pattern (filter, "*.TXT");
474 475 476
	gtk_file_filter_add_pattern (filter, "*.it8");
	gtk_file_filter_add_pattern (filter, "*.IT8");

477 478 479
	/* TRANSLATORS: filter name on the file->open dialog */
	gtk_file_filter_set_name (filter, _("CIE values"));
	gtk_file_chooser_add_filter (GTK_FILE_CHOOSER(dialog), filter);
480
//	g_object_unref (filter);
481 482 483 484 485 486 487 488 489 490

	/* did user choose file */
	if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
		filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER(dialog));

	/* we're done */
	gtk_widget_destroy (dialog);

	/* or NULL for missing */
	return filename;
491
}
492

493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530
/**
 * gcm_prefs_get_device_for_it8_file:
 **/
static gchar *
gcm_prefs_get_device_for_it8_file (const gchar *filename)
{
	gchar *contents = NULL;
	gchar **lines = NULL;
	gchar *device = NULL;
	gboolean ret;
	GError *error = NULL;
	guint i;

	/* get contents */
	ret = g_file_get_contents (filename, &contents, NULL, &error);
	if (!ret) {
		egg_warning ("failed to get contents: %s", error->message);
		g_error_free (error);
		goto out;
	}

	/* split */
	lines = g_strsplit (contents, "\n", 15);
	for (i=0; lines[i] != NULL; i++) {
		if (!g_str_has_prefix (lines[i], "ORIGINATOR"))
			continue;

		/* copy, without the header or double quotes */
		device = g_strdup (lines[i]+12);
		g_strdelimit (device, "\"", '\0');
		break;
	}
out:
	g_free (contents);
	g_strfreev (lines);
	return device;
}

531
/**
532
 * gcm_prefs_calibrate_device:
533
 **/
534
static gboolean
535
gcm_prefs_calibrate_device (GcmCalibrate *calibrate)
536
{
537
	gboolean ret = FALSE;
538
	gboolean has_shared_targets;
539 540 541
	GError *error = NULL;
	gchar *scanned_image = NULL;
	gchar *reference_data = NULL;
542 543 544 545
	gchar *basename = NULL;
	gchar *manufacturer = NULL;
	gchar *model = NULL;
	gchar *description = NULL;
546
	gchar *device = NULL;
547
	const gchar *directory;
548
	GtkWindow *window;
549 550 551 552
	GString *string;
	GtkResponseType response;
	GtkWidget *dialog;
	const gchar *title;
553 554

	window = GTK_WINDOW(gtk_builder_get_object (builder, "dialog_prefs"));
555
	string = g_string_new ("");
556 557 558 559

	/* install shared-color-targets package */
	has_shared_targets = g_file_test ("/usr/share/shared-color-targets", G_FILE_TEST_IS_DIR);
	if (!has_shared_targets) {
560
#ifdef GCM_USE_PACKAGEKIT
561 562 563 564
		/* ask the user to confirm */
		dialog = gtk_message_dialog_new (window, GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_QUESTION, GTK_BUTTONS_NONE,
						 /* TRANSLATORS: title, usually we can tell based on the EDID data or output name */
						 _("Install missing files?"));
565

566
		/* TRANSLATORS: dialog message saying the color targets are not installed */
567
		g_string_append_printf (string, "%s ", _("Common IT8 color target files are not installed on this computer."));
568 569 570 571 572
		/* TRANSLATORS: dialog message saying the color targets are not installed */
		g_string_append_printf (string, "%s\n\n", _("Color target files are needed to convert the image to a color profile."));
		/* TRANSLATORS: dialog message, asking if it's okay to install them */
		g_string_append_printf (string, "%s\n\n", _("Do you want them to be automatically installed?"));
		/* TRANSLATORS: dialog message, if the user has the target file on a CDROM then there's no need for this package */
573
		g_string_append_printf (string, "%s", _("If you have already have the correct IT8 file then you can skip this step."));
574 575 576 577 578 579 580 581 582 583 584 585

		gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog), "%s", string->str);
		gtk_window_set_icon_name (GTK_WINDOW (dialog), GCM_STOCK_ICON);
		/* TRANSLATORS: button, install a package */
		gtk_dialog_add_button (GTK_DIALOG (dialog), _("Install"), GTK_RESPONSE_YES);
		/* TRANSLATORS: button, skip installing a package */
		gtk_dialog_add_button (GTK_DIALOG (dialog), _("Do not install"), GTK_RESPONSE_CANCEL);
		response = gtk_dialog_run (GTK_DIALOG (dialog));
		gtk_widget_destroy (dialog);

		/* only install if the user wanted to */
		if (response == GTK_RESPONSE_YES)
586
			has_shared_targets = gcm_utils_install_package (GCM_PREFS_PACKAGE_NAME_SHARED_COLOR_TARGETS, window);
587 588 589
#else
		egg_warning ("cannot install: this package was not compiled with --enable-packagekit");
#endif
590
	}
591

592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622
	/* TRANSLATORS: title, we're setting up the device ready for calibration */
	title = _("Setting up device");
	g_string_set_size (string, 0);

	/* TRANSLATORS: dialog message, preface */
	g_string_append_printf (string, "%s\n", _("Before calibrating the device, you have to manually acquire a reference image and save it as a TIFF image file."));

	/* TRANSLATORS: dialog message, preface */
	g_string_append_printf (string, "%s\n", _("Ensure that the contrast and brightness is not changed and color correction profiles are not applied."));

	/* TRANSLATORS: dialog message, suffix */
	g_string_append_printf (string, "%s\n", _("The device sensor should have been cleaned prior to scanning and the output file resolution should be at least 200dpi."));

	/* TRANSLATORS: dialog message, suffix */
	g_string_append_printf (string, "\n%s\n", _("For best results, the reference image should also be less than two years old."));

	/* TRANSLATORS: dialog question */
	g_string_append_printf (string, "\n%s", _("Do you have a scanned TIFF file of a IT8.7/2 reference image?"));

	/* ask the user to confirm */
	window = GTK_WINDOW(gtk_builder_get_object (builder, "dialog_prefs"));
	dialog = gtk_message_dialog_new (window, GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_QUESTION, GTK_BUTTONS_CANCEL, "%s", title);
	gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog), "%s", string->str);
	gtk_window_set_icon_name (GTK_WINDOW (dialog), GCM_STOCK_ICON);
	/* TRANSLATORS: button, confirm the user has a file */
	gtk_dialog_add_button (GTK_DIALOG (dialog), _("I have already scanned in a file"), GTK_RESPONSE_YES);
	response = gtk_dialog_run (GTK_DIALOG (dialog));
	gtk_widget_destroy (dialog);
	if (response != GTK_RESPONSE_YES)
		goto out;

623 624
	/* get the device */
	g_object_get (current_device,
625 626 627
		      "model", &model,
		      "title", &description,
		      "manufacturer", &manufacturer,
628
		      NULL);
629 630

	/* get scanned image */
631 632
	directory = g_get_home_dir ();
	scanned_image = gcm_prefs_calibrate_device_get_scanned_profile (directory);
633 634 635 636
	if (scanned_image == NULL)
		goto out;

	/* get reference data */
637 638
	directory = has_shared_targets ? "/usr/share/color/targets" : "/media";
	reference_data = gcm_prefs_calibrate_device_get_reference_data (directory);
639 640 641
	if (reference_data == NULL)
		goto out;

642
	/* ensure we have data */
643
	basename = gcm_prefs_calibrate_get_basename (current_device);
644
	if (manufacturer == NULL)
645
		manufacturer = g_strdup ("Generic manufacturer");
646 647 648 649 650
	if (model == NULL)
		model = g_strdup ("Generic model");
	if (description == NULL)
		description = g_strdup ("Generic scanner");

651 652 653 654 655
	/* use the ORIGINATOR in the it8 file */
	device = gcm_prefs_get_device_for_it8_file (reference_data);
	if (device == NULL)
		device = g_strdup ("IT8.7");

656
	/* set the calibration parameters */
657
	g_object_set (calibrate,
658 659 660 661
		      "basename", basename,
		      "model", model,
		      "description", description,
		      "manufacturer", manufacturer,
662 663
		      "filename-source", scanned_image,
		      "filename-reference", reference_data,
664
		      "device", device,
665 666
		      NULL);

667
	/* do each step */
668
	ret = gcm_calibrate_device (calibrate, window, &error);
669 670 671 672 673 674
	if (!ret) {
		egg_warning ("failed to calibrate: %s", error->message);
		g_error_free (error);
		goto out;
	}
out:
675 676
	if (string != NULL)
		g_string_free (string, TRUE);
677
	g_free (device);
678 679 680 681
	g_free (basename);
	g_free (manufacturer);
	g_free (model);
	g_free (description);
682 683 684
	g_free (scanned_image);
	g_free (reference_data);
	return ret;
685 686
}

687 688 689 690
/**
 * gcm_prefs_profile_type_to_icon_name:
 **/
static const gchar *
691
gcm_prefs_profile_type_to_icon_name (GcmProfileTypeEnum type)
692
{
693
	if (type == GCM_PROFILE_TYPE_ENUM_DISPLAY_DEVICE)
694
		return "video-display";
695
	if (type == GCM_PROFILE_TYPE_ENUM_INPUT_DEVICE)
696
		return "scanner";
697
	if (type == GCM_PROFILE_TYPE_ENUM_OUTPUT_DEVICE)
698
		return "printer";
699
	if (type == GCM_PROFILE_TYPE_ENUM_COLORSPACE_CONVERSION)
700
		return "view-refresh";
701 702 703
	return "image-missing";
}

704 705 706 707
/**
 * gcm_prefs_profile_get_sort_string:
 **/
static const gchar *
708
gcm_prefs_profile_get_sort_string (GcmProfileTypeEnum type)
709
{
710
	if (type == GCM_PROFILE_TYPE_ENUM_DISPLAY_DEVICE)
711
		return "1";
712
	if (type == GCM_PROFILE_TYPE_ENUM_INPUT_DEVICE)
713
		return "2";
714
	if (type == GCM_PROFILE_TYPE_ENUM_OUTPUT_DEVICE)
715 716 717 718
		return "3";
	return "4";
}

719 720 721 722 723 724 725 726 727
/**
 * gcm_prefs_update_profile_list:
 **/
static void
gcm_prefs_update_profile_list (void)
{
	GtkTreeIter iter;
	gchar *description;
	const gchar *icon_name;
728
	GcmProfileTypeEnum profile_type = GCM_PROFILE_TYPE_ENUM_UNKNOWN;
729 730 731
	GcmProfile *profile;
	guint i;
	gchar *filename = NULL;
732
	gchar *sort = NULL;
733
	GPtrArray *profile_array = NULL;
734 735 736

	egg_debug ("updating profile list");

737 738 739
	/* get new list */
	profile_array = gcm_profile_store_get_array (profile_store);

740 741 742 743
	/* clear existing list */
	gtk_list_store_clear (list_store_profiles);

	/* update each list */
744 745
	for (i=0; i<profile_array->len; i++) {
		profile = g_ptr_array_index (profile_array, i);
746 747 748
		g_object_get (profile,
			      "description", &description,
			      "type", &profile_type,
749
			      "filename", &filename,
750 751 752 753 754
			      NULL);

		egg_debug ("add %s to profiles list", filename);
		icon_name = gcm_prefs_profile_type_to_icon_name (profile_type);
		gtk_list_store_append (list_store_profiles, &iter);
Richard Hughes's avatar
Richard Hughes committed
755 756 757
		sort = g_strdup_printf ("%s%s",
					gcm_prefs_profile_get_sort_string (profile_type),
					description);
758
		gtk_list_store_set (list_store_profiles, &iter,
759 760 761 762 763
				    GCM_PROFILES_COLUMN_ID, filename,
				    GCM_PROFILES_COLUMN_SORT, sort,
				    GCM_PROFILES_COLUMN_TITLE, description,
				    GCM_PROFILES_COLUMN_ICON, icon_name,
				    GCM_PROFILES_COLUMN_PROFILE, profile,
764 765
				    -1);

766
		g_free (sort);
767
		g_free (filename);
768 769
		g_free (description);
	}
770 771
	if (profile_array != NULL)
		g_ptr_array_unref (profile_array);
772 773
}

774 775 776 777 778 779 780 781 782 783 784 785 786
/**
 * gcm_prefs_profile_delete_cb:
 **/
static void
gcm_prefs_profile_delete_cb (GtkWidget *widget, gpointer data)
{
	GtkWidget *dialog;
	GtkResponseType response;
	GtkWindow *window;
	gint retval;
	gchar *filename = NULL;
	GcmProfile *profile;
	GtkTreeSelection *selection;
787 788
	GtkTreeModel *model;
	GtkTreeIter iter;
789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805

	/* ask the user to confirm */
	window = GTK_WINDOW(gtk_builder_get_object (builder, "dialog_prefs"));
	dialog = gtk_message_dialog_new (window, GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_QUESTION, GTK_BUTTONS_CANCEL,
					 /* TRANSLATORS: title, usually we can tell based on the EDID data or output name */
					 _("Permanently delete profile?"));
	gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
						  /* TRANSLATORS: dialog message */
						  _("Are you sure you want to remove this profile from your system permanently?"));
	gtk_window_set_icon_name (GTK_WINDOW (dialog), GCM_STOCK_ICON);
	/* TRANSLATORS: button, delete a profile */
	gtk_dialog_add_button (GTK_DIALOG (dialog), _("Delete"), GTK_RESPONSE_YES);
	response = gtk_dialog_run (GTK_DIALOG (dialog));
	gtk_widget_destroy (dialog);
	if (response != GTK_RESPONSE_YES)
		goto out;

806 807 808 809 810 811 812 813 814 815
	/* get the selected row */
	widget = GTK_WIDGET (gtk_builder_get_object (builder, "treeview_profiles"));
	selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (widget));
	if (!gtk_tree_selection_get_selected (selection, &model, &iter)) {
		egg_debug ("no row selected");
		goto out;
	}

	/* get profile */
	gtk_tree_model_get (model, &iter,
816
			    GCM_PROFILES_COLUMN_PROFILE, &profile,
817 818
			    -1);

819
	/* get device data */
820 821
	g_object_get (profile,
		      "filename", &filename,
822 823 824 825 826 827 828 829 830 831 832
		      NULL);

	/* try to remove file */
	retval = g_unlink (filename);
	if (retval != 0)
		goto out;
out:
	g_free (filename);
}

/**
833
 * gcm_prefs_file_chooser_get_icc_profile:
834 835
 **/
static gchar *
836
gcm_prefs_file_chooser_get_icc_profile (void)
837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857
{
	gchar *filename = NULL;
	GtkWindow *window;
	GtkWidget *dialog;
	GtkFileFilter *filter;

	/* create new dialog */
	window = GTK_WINDOW(gtk_builder_get_object (builder, "dialog_prefs"));
	/* TRANSLATORS: dialog for file->open dialog */
	dialog = gtk_file_chooser_dialog_new (_("Select ICC profile file"), window,
					       GTK_FILE_CHOOSER_ACTION_OPEN,
					       GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
					       GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
					      NULL);
	gtk_window_set_icon_name (GTK_WINDOW (dialog), GCM_STOCK_ICON);
	gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER(dialog), g_get_home_dir ());
	gtk_file_chooser_set_create_folders (GTK_FILE_CHOOSER(dialog), FALSE);

	/* setup the filter */
	filter = gtk_file_filter_new ();
	gtk_file_filter_add_mime_type (filter, "application/vnd.iccprofile");
858 859 860 861 862 863 864

	/* we can remove this when we depend on a new shared-mime-info */
	gtk_file_filter_add_pattern (filter, "*.icc");
	gtk_file_filter_add_pattern (filter, "*.icm");
	gtk_file_filter_add_pattern (filter, "*.ICC");
	gtk_file_filter_add_pattern (filter, "*.ICM");

865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881
	/* TRANSLATORS: filter name on the file->open dialog */
	gtk_file_filter_set_name (filter, _("Supported ICC profiles"));
	gtk_file_chooser_add_filter (GTK_FILE_CHOOSER(dialog), filter);
//	g_object_unref (filter);

	/* did user choose file */
	if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
		filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER(dialog));

	/* we're done */
	gtk_widget_destroy (dialog);

	/* or NULL for missing */
	return filename;
}

/**
882
 * gcm_prefs_profile_import:
883
 **/
884 885
static gboolean
gcm_prefs_profile_import (const gchar *filename)
886 887 888 889 890 891 892 893 894 895 896 897 898
{
	GtkWidget *dialog;
	GError *error = NULL;
	gchar *destination = NULL;
	GtkWindow *window;
	gboolean ret;

	/* copy icc file to ~/.color/icc */
	destination = gcm_utils_get_profile_destination (filename);
	ret = gcm_utils_mkdir_and_copy (filename, destination, &error);
	if (!ret) {
		/* TRANSLATORS: could not read file */
		window = GTK_WINDOW(gtk_builder_get_object (builder, "dialog_prefs"));
899
		dialog = gtk_message_dialog_new (window, GTK_DIALOG_MODAL, GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE, _("Failed to copy file"));
900 901 902 903 904 905 906 907 908
		gtk_window_set_icon_name (GTK_WINDOW (dialog), GCM_STOCK_ICON);
		gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog), "%s", error->message);
		gtk_dialog_run (GTK_DIALOG (dialog));
		g_error_free (error);
		gtk_widget_destroy (dialog);
		goto out;
	}
out:
	g_free (destination);
909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946
	return ret;
}

/**
 * gcm_prefs_profile_import_uri:
 **/
static gboolean
gcm_prefs_profile_import_uri (const gchar *uri)
{
	GFile *file;
	gchar *path;
	gboolean ret = FALSE;

	/* resolve */
	file = g_file_new_for_uri (uri);
	path = g_file_get_path (file);
	if (path == NULL) {
		egg_warning ("failed to get path: %s", uri);
		goto out;
	}

	/* import */
	ret = gcm_prefs_profile_import (path);
out:
	g_free (path);
	g_object_unref (file);
	return ret;
}

/**
 * gcm_prefs_profile_import_cb:
 **/
static void
gcm_prefs_profile_import_cb (GtkWidget *widget, gpointer data)
{
	gchar *filename;

	/* get new file */
947
	filename = gcm_prefs_file_chooser_get_icc_profile ();
948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002
	if (filename == NULL)
		goto out;

	/* import this */
	gcm_prefs_profile_import (filename);

out:
	g_free (filename);
}

/**
 * gcm_prefs_drag_data_received_cb:
 **/
static void
gcm_prefs_drag_data_received_cb (GtkWidget *widget, GdkDragContext *context, gint x, gint y, GtkSelectionData *data, guint _time, gpointer user_data)
{
	const guchar *filename;
	gchar **filenames = NULL;
	guint i;
	gboolean ret;

	/* get filenames */
	filename = gtk_selection_data_get_data (data);
	if (filename == NULL) {
		gtk_drag_finish (context, FALSE, FALSE, _time);
		goto out;
	}

	/* import this */
	egg_debug ("dropped: %p (%s)", data, filename);

	/* split, as multiple drag targets are accepted */
	filenames = g_strsplit_set ((const gchar *)filename, "\r\n", -1);
	for (i=0; filenames[i]!=NULL; i++) {

		/* check this is an ICC profile */
		ret = gcm_utils_is_icc_profile (filenames[i]);
		if (!ret) {
			egg_debug ("%s is not a ICC profile", filenames[i]);
			gtk_drag_finish (context, FALSE, FALSE, _time);
			goto out;
		}

		/* try to import it */
		ret = gcm_prefs_profile_import_uri (filenames[i]);
		if (!ret) {
			egg_debug ("%s did not import correctly", filenames[i]);
			gtk_drag_finish (context, FALSE, FALSE, _time);
			goto out;
		}
	}

	gtk_drag_finish (context, TRUE, FALSE, _time);
out:
	g_strfreev (filenames);
1003 1004
}

1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021
/**
 * gcm_prefs_ensure_argyllcms_installed:
 **/
static gboolean
gcm_prefs_ensure_argyllcms_installed (void)
{
	gboolean ret;
	GtkWindow *window;
	GtkWidget *dialog;
	GtkResponseType response;
	GString *string = NULL;

	/* find whether argyllcms is installed using a tool which should exist */
	ret = g_file_test ("/usr/bin/dispcal", G_FILE_TEST_EXISTS);
	if (ret)
		goto out;

1022 1023 1024 1025 1026
#ifndef GCM_USE_PACKAGEKIT
	egg_warning ("cannot install: this package was not compiled with --enable-packagekit");
	goto out;
#endif

1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061
	/* ask the user to confirm */
	window = GTK_WINDOW(gtk_builder_get_object (builder, "dialog_prefs"));
	dialog = gtk_message_dialog_new (window, GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_QUESTION, GTK_BUTTONS_NONE,
					 /* TRANSLATORS: title, usually we can tell based on the EDID data or output name */
					 _("Install missing calibration software?"));

	string = g_string_new ("");
	/* TRANSLATORS: dialog message saying the argyllcms is not installed */
	g_string_append_printf (string, "%s\n", _("Calibration software is not installed on this computer."));
	/* TRANSLATORS: dialog message saying the color targets are not installed */
	g_string_append_printf (string, "%s\n\n", _("These tools are required to build color profiles for devices."));
	/* TRANSLATORS: dialog message, asking if it's okay to install it */
	g_string_append_printf (string, "%s", _("Do you want them to be automatically installed?"));

	gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog), "%s", string->str);
	gtk_window_set_icon_name (GTK_WINDOW (dialog), GCM_STOCK_ICON);
	/* TRANSLATORS: button, install a package */
	gtk_dialog_add_button (GTK_DIALOG (dialog), _("Install"), GTK_RESPONSE_YES);
	/* TRANSLATORS: button, skip installing a package */
	gtk_dialog_add_button (GTK_DIALOG (dialog), _("Do not install"), GTK_RESPONSE_CANCEL);
	response = gtk_dialog_run (GTK_DIALOG (dialog));
	gtk_widget_destroy (dialog);

	/* only install if the user wanted to */
	if (response != GTK_RESPONSE_YES)
		goto out;

	/* do the install */
	ret = gcm_utils_install_package (GCM_PREFS_PACKAGE_NAME_ARGYLLCMS, window);
out:
	if (string != NULL)
		g_string_free (string, TRUE);
	return ret;
}

1062 1063 1064 1065 1066 1067
/**
 * gcm_prefs_calibrate_cb:
 **/
static void
gcm_prefs_calibrate_cb (GtkWidget *widget, gpointer data)
{
1068
	GcmCalibrate *calibrate = NULL;
1069
	GcmDeviceTypeEnum type;
1070 1071 1072 1073
	gboolean ret;
	GError *error = NULL;
	gchar *filename = NULL;
	guint i;
1074
	gchar *name;
1075
	gchar *destination = NULL;
1076
	GcmProfile *profile;
1077
	GPtrArray *profile_array = NULL;
1078

1079 1080 1081 1082 1083
	/* ensure argyllcms is installed */
	ret = gcm_prefs_ensure_argyllcms_installed ();
	if (!ret)
		goto out;

1084 1085 1086 1087 1088 1089
	/* get the type */
	g_object_get (current_device,
		      "type", &type,
		      NULL);

	/* create new calibration object */
1090
	calibrate = GCM_CALIBRATE(gcm_calibrate_argyll_new ());