Beispiel #1
0
static void
cd_profile_connect_cb (GObject *source_object,
		       GAsyncResult *res,
		       gpointer user_data)
{
	CdProfile *profile;
	CdProfilePrivate *priv;
	g_autoptr(GError) error = NULL;
	g_autoptr(GTask) task = G_TASK (user_data);
	g_autoptr(GVariant) colorspace = NULL;
	g_autoptr(GVariant) created = NULL;
	g_autoptr(GVariant) filename = NULL;
	g_autoptr(GVariant) format = NULL;
	g_autoptr(GVariant) has_vcgt = NULL;
	g_autoptr(GVariant) id = NULL;
	g_autoptr(GVariant) is_system_wide = NULL;
	g_autoptr(GVariant) kind = NULL;
	g_autoptr(GVariant) metadata = NULL;
	g_autoptr(GVariant) owner = NULL;
	g_autoptr(GVariant) qualifier = NULL;
	g_autoptr(GVariant) scope = NULL;
	g_autoptr(GVariant) title = NULL;
	g_autoptr(GVariant) warnings = NULL;

	profile = CD_PROFILE (g_task_get_source_object (task));
	priv = GET_PRIVATE (profile);
	priv->proxy = g_dbus_proxy_new_for_bus_finish (res, &error);
	if (priv->proxy == NULL) {
		g_task_return_new_error (task,
						 CD_PROFILE_ERROR,
						 CD_PROFILE_ERROR_INTERNAL,
						 "Failed to connect to profile %s: %s",
						 cd_profile_get_object_path (profile),
						 error->message);
		return;
	}

	/* get profile id */
	id = g_dbus_proxy_get_cached_property (priv->proxy,
					       CD_PROFILE_PROPERTY_ID);
	if (id != NULL)
		priv->id = g_variant_dup_string (id, NULL);

	/* if the profile is missing, then fail */
	if (id == NULL) {
		g_task_return_new_error (task,
						 CD_PROFILE_ERROR,
						 CD_PROFILE_ERROR_INTERNAL,
						 "Failed to connect to missing profile %s",
						 cd_profile_get_object_path (profile));
		return;
	}

	/* get filename */
	filename = g_dbus_proxy_get_cached_property (priv->proxy,
						     CD_PROFILE_PROPERTY_FILENAME);
	if (filename != NULL)
		priv->filename = cd_profile_get_nullable_str (filename);

	/* get qualifier */
	qualifier = g_dbus_proxy_get_cached_property (priv->proxy,
						      CD_PROFILE_PROPERTY_QUALIFIER);
	if (qualifier != NULL)
		priv->qualifier = cd_profile_get_nullable_str (qualifier);

	/* get format */
	format = g_dbus_proxy_get_cached_property (priv->proxy,
						   CD_PROFILE_PROPERTY_FORMAT);
	if (format != NULL)
		priv->format = cd_profile_get_nullable_str (format);

	/* get title */
	title = g_dbus_proxy_get_cached_property (priv->proxy,
						  CD_PROFILE_PROPERTY_TITLE);
	if (title != NULL)
		priv->title = cd_profile_get_nullable_str (title);

	/* get kind */
	kind = g_dbus_proxy_get_cached_property (priv->proxy,
						 CD_PROFILE_PROPERTY_KIND);
	if (kind != NULL)
		priv->kind = cd_profile_kind_from_string (g_variant_get_string (kind, NULL));

	/* get colorspace */
	colorspace = g_dbus_proxy_get_cached_property (priv->proxy,
						       CD_PROFILE_PROPERTY_COLORSPACE);
	if (colorspace != NULL)
		priv->colorspace = cd_colorspace_from_string (g_variant_get_string (colorspace, NULL));

	/* get scope */
	scope = g_dbus_proxy_get_cached_property (priv->proxy,
						  CD_PROFILE_PROPERTY_SCOPE);
	if (scope != NULL)
		priv->scope = cd_object_scope_from_string (g_variant_get_string (scope, NULL));

	/* get owner */
	owner = g_dbus_proxy_get_cached_property (priv->proxy,
						  CD_PROFILE_PROPERTY_OWNER);
	if (owner != NULL)
		priv->owner = g_variant_get_uint32 (owner);

	/* get warnings */
	warnings = g_dbus_proxy_get_cached_property (priv->proxy,
						  CD_PROFILE_PROPERTY_WARNINGS);
	if (warnings != NULL)
		priv->warnings = g_variant_dup_strv (warnings, NULL);

	/* get created */
	created = g_dbus_proxy_get_cached_property (priv->proxy,
						    CD_PROFILE_PROPERTY_CREATED);
	if (created != NULL)
		priv->created = g_variant_get_int64 (created);

	/* get VCGT */
	has_vcgt = g_dbus_proxy_get_cached_property (priv->proxy,
						     CD_PROFILE_PROPERTY_HAS_VCGT);
	if (has_vcgt != NULL)
		priv->has_vcgt = g_variant_get_boolean (has_vcgt);

	/* get if system wide */
	is_system_wide = g_dbus_proxy_get_cached_property (priv->proxy,
							   CD_PROFILE_PROPERTY_IS_SYSTEM_WIDE);
	if (is_system_wide != NULL)
		priv->is_system_wide = g_variant_get_boolean (is_system_wide);

	/* get if system wide */
	metadata = g_dbus_proxy_get_cached_property (priv->proxy,
						     CD_PROFILE_PROPERTY_METADATA);
	if (metadata != NULL)
		cd_profile_set_metadata_from_variant (profile, metadata);

	/* get signals from DBus */
	g_signal_connect_object (priv->proxy,
				 "g-signal",
				 G_CALLBACK (cd_profile_dbus_signal_cb),
				 profile, 0);

	/* watch if any remote properties change */
	g_signal_connect_object (priv->proxy,
				 "g-properties-changed",
				 G_CALLBACK (cd_profile_dbus_properties_changed_cb),
				 profile, 0);

	/* success */
	g_task_return_boolean (task, TRUE);
}
/**
 * main:
 **/
int
main (int argc, char **argv)
{
	CdClient *client = NULL;
	CdProfile *profile_tmp = NULL;
	const gchar *copyright;
	const gchar *description;
	const gchar *title;
	const gchar *lang;
	gboolean ret;
	gchar **files = NULL;
	CdIcc *icc = NULL;
	GError *error = NULL;
	GFile *destination = NULL;
	GFile *file = NULL;
	GOptionContext *context;
	GString *string = NULL;
	GtkResponseType response;
	GtkWidget *image = NULL;
	GtkWidget *dialog;
	guint retval = 1;

	const GOptionEntry options[] = {
		{ G_OPTION_REMAINING, '\0', 0, G_OPTION_ARG_FILENAME_ARRAY, &files,
		  /* TRANSLATORS: command line option: a list of catalogs to install */
		  _("ICC profile to install"), NULL },
		{ NULL}
	};

	setlocale (LC_ALL, "");

	bindtextdomain (GETTEXT_PACKAGE, LOCALEDIR);
	bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
	textdomain (GETTEXT_PACKAGE);

	gtk_init (&argc, &argv);

	context = g_option_context_new ("gnome-color-manager import program");
	g_option_context_add_main_entries (context, options, NULL);
	g_option_context_add_group (context, gcm_debug_get_option_group ());
	g_option_context_add_group (context, gtk_get_option_group (TRUE));
	g_option_context_parse (context, &argc, &argv, NULL);
	g_option_context_free (context);

	/* nothing sent */
	if (files == NULL) {
		/* TRANSLATORS: nothing was specified on the command line */
		dialog = gtk_message_dialog_new (NULL,
						 0,
						 GTK_MESSAGE_ERROR,
						 GTK_BUTTONS_CLOSE,
						 _("No filename specified"));
		gtk_window_set_icon_name (GTK_WINDOW (dialog),
					  GCM_STOCK_ICON);
		gtk_dialog_run (GTK_DIALOG (dialog));
		gtk_widget_destroy (dialog);
		goto out;
	}

	/* load profile */
	icc = cd_icc_new ();
	file = g_file_new_for_path (files[0]);
	ret = cd_icc_load_file (icc, file,
				CD_ICC_LOAD_FLAGS_FALLBACK_MD5,
				NULL, &error);
	if (!ret) {
		/* TRANSLATORS: could not read file */
		dialog = gtk_message_dialog_new (NULL,
						 0,
						 GTK_MESSAGE_ERROR,
						 GTK_BUTTONS_CLOSE,
						 _("Failed to open ICC profile"));
		gtk_window_set_icon_name (GTK_WINDOW (dialog),
					  GCM_STOCK_ICON);
		/* TRANSLATORS: parsing error */
		gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
							  _("Failed to parse file: %s"),
							  error->message);
		gtk_dialog_run (GTK_DIALOG (dialog));
		g_error_free (error);
		gtk_widget_destroy (dialog);
		goto out;
	}

	/* get data */
	lang = g_getenv ("LANG");
	description = cd_icc_get_description (icc, lang, NULL);
	copyright = cd_icc_get_copyright (icc, lang, NULL);

	/* use the same icon as the color control panel */
	image = gtk_image_new_from_icon_name ("preferences-color",
					      GTK_ICON_SIZE_DIALOG);
	gtk_misc_set_alignment (GTK_MISC (image), 0.5, 0.0);
	gtk_widget_show (image);

	/* create message */
	string = g_string_new ("");
	/* TRANSLATORS: message text */
	g_string_append_printf (string, _("Profile description: %s"),
				description != NULL ? description : files[0]);

	/* add copyright */
	if (copyright != NULL) {
		if (g_str_has_prefix (copyright, "Copyright "))
			copyright += 10;
		if (g_str_has_prefix (copyright, "Copyright, "))
			copyright += 11;
		/* TRANSLATORS: message text */
		g_string_append_printf (string, "\n%s %s", _("Profile copyright:"), copyright);
	}

	/* check file does't already exist as system-wide */
	client = cd_client_new ();
	ret = cd_client_connect_sync (client,
				      NULL,
				      &error);
	if (!ret) {
		g_warning ("failed to connect to colord: %s",
			   error->message);
		g_error_free (error);
		goto out;
	}

	profile_tmp = cd_client_find_profile_by_property_sync (client,
							      CD_PROFILE_METADATA_FILE_CHECKSUM,
							      cd_icc_get_checksum (icc),
							      NULL,
							      NULL);
	if (profile_tmp != NULL) {
		ret = cd_profile_connect_sync (profile_tmp,
					       NULL,
					       &error);
		if (!ret) {
			g_warning ("failed to connect to profile %s: %s",
				   cd_profile_get_object_path (profile_tmp),
				   error->message);
			g_error_free (error);
			goto out;
		}
		/* TRANSLATORS: color profile already been installed */
		dialog = gtk_message_dialog_new (NULL,
						 0,
						 GTK_MESSAGE_INFO,
						 GTK_BUTTONS_CLOSE,
						 _("Color profile is already imported"));
		gtk_window_set_icon_name (GTK_WINDOW (dialog), GCM_STOCK_ICON);
		gtk_message_dialog_set_image (GTK_MESSAGE_DIALOG (dialog), image);
		gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog), "%s", string->str);
		gtk_dialog_add_button (GTK_DIALOG (dialog), _("Show Details"), GTK_RESPONSE_HELP);
		response = gtk_dialog_run (GTK_DIALOG (dialog));
		if (response == GTK_RESPONSE_HELP) {
			gcm_import_show_details (GTK_WINDOW (dialog),
						 TRUE,
						 cd_profile_get_id (profile_tmp));
			goto try_harder;
		}
		gtk_widget_destroy (dialog);
		goto out;
	}

	/* get correct title */
	switch (cd_icc_get_kind (icc)) {
	case CD_PROFILE_KIND_DISPLAY_DEVICE:
		/* TRANSLATORS: the profile type */
		title = _("Import display color profile?");
		break;
	case CD_PROFILE_KIND_OUTPUT_DEVICE:
		/* TRANSLATORS: the profile type */
		title = _("Import device color profile?");
		break;
	case CD_PROFILE_KIND_NAMED_COLOR:
		/* TRANSLATORS: the profile type */
		title = _("Import named color profile?");
		break;
	default:
		/* TRANSLATORS: the profile type */
		title = _("Import color profile?");
		break;
	}

	/* ask confirmation */
	dialog = gtk_message_dialog_new (NULL,
					 0,
					 GTK_MESSAGE_INFO,
					 GTK_BUTTONS_CANCEL,
					 "%s",
					 title);
	gtk_message_dialog_set_image (GTK_MESSAGE_DIALOG (dialog), image);
	gtk_window_set_icon_name (GTK_WINDOW (dialog), GCM_STOCK_ICON);
	gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog), "%s", string->str);
	/* TRANSLATORS: button text */
	gtk_dialog_add_button (GTK_DIALOG (dialog), _("Import"), GTK_RESPONSE_OK);
	gtk_dialog_add_button (GTK_DIALOG (dialog), _("Show Details"), GTK_RESPONSE_HELP);
try_harder:
	response = gtk_dialog_run (GTK_DIALOG (dialog));
	if (response == GTK_RESPONSE_HELP) {
		gcm_import_show_details (GTK_WINDOW (dialog), FALSE, files[0]);
		goto try_harder;
	}
	gtk_widget_destroy (dialog);

	/* not the correct response */
	if (response != GTK_RESPONSE_OK)
		goto out;

	/* copy icc file to users profile path */
	profile_tmp = cd_client_import_profile_sync (client,
						     file,
						     NULL,
						     &error);
	if (profile_tmp == NULL) {
		/* TRANSLATORS: could not read file */
		dialog = gtk_message_dialog_new (NULL,
						 0,
						 GTK_MESSAGE_ERROR,
						 GTK_BUTTONS_CLOSE,
						 _("Failed to import file"));
		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:
	if (file != NULL)
		g_object_unref (file);
	if (string != NULL)
		g_string_free (string, TRUE);
	if (icc != NULL)
		g_object_unref (icc);
	if (client != NULL)
		g_object_unref (client);
	if (profile_tmp != NULL)
		g_object_unref (profile_tmp);
	if (destination != NULL)
		g_object_unref (destination);
	g_strfreev (files);
	return retval;
}