Esempio n. 1
0
/**
 * cd_profile_load_icc:
 * @profile: a #CdProfile instance.
 * @flags: options for loading the profile
 * @cancellable: A #GCancellable, or %NULL
 * @error: A #GError or %NULL
 *
 * Loads a local ICC object from the abstract profile.
 *
 * Return value: (transfer full): A new #CdIcc object, or %NULL for error
 *
 * Since: 0.1.32
 **/
CdIcc *
cd_profile_load_icc (CdProfile *profile,
		     CdIccLoadFlags flags,
		     GCancellable *cancellable,
		     GError **error)
{
	CdProfilePrivate *priv = GET_PRIVATE (profile);
	g_autoptr(CdIcc) icc = NULL;
	g_autoptr(GFile) file = NULL;

	g_return_val_if_fail (CD_IS_PROFILE (profile), NULL);

	/* not a local profile */
	if (priv->filename == NULL) {
		g_set_error (error,
			     CD_PROFILE_ERROR,
			     CD_PROFILE_ERROR_INTERNAL,
			     "%s has no local instance",
			     priv->id);
		return NULL;
	}

	/* load local instance */
	icc = cd_icc_new ();
	file = g_file_new_for_path (priv->filename);
	if (!cd_icc_load_file (icc, file, flags, cancellable, error))
		return NULL;

	/* success */
	return g_object_ref (icc);
}
Esempio n. 2
0
/**
 * cd_util_get_coverage:
 * @profile: A valid #CdUtil
 * @filename_in: A filename to proof against
 * @error: A #GError, or %NULL
 *
 * Gets the gamut coverage of two profiles
 *
 * Return value: A positive value for success, or -1.0 for error.
 **/
static gdouble
cd_util_get_coverage (cmsHPROFILE profile_proof,
		      const gchar *filename_in,
		      GError **error)
{
	gdouble coverage = -1.0;
	gboolean ret;
	GFile *file = NULL;
	CdIcc *icc = NULL;
	CdIcc *icc_ref;

	/* load proofing profile */
	icc_ref = cd_icc_new ();
	ret = cd_icc_load_handle (icc_ref, profile_proof,
				  CD_ICC_LOAD_FLAGS_NONE, error);
	if (!ret)
		goto out;

	/* load target profile */
	icc = cd_icc_new ();
	file = g_file_new_for_path (filename_in);
	ret = cd_icc_load_file (icc, file, CD_ICC_LOAD_FLAGS_NONE, NULL, error);
	if (!ret)
		goto out;

	/* get coverage */
	ret = cd_icc_utils_get_coverage (icc, icc_ref, &coverage, error);
	if (!ret)
		return FALSE;
out:
	if (file != NULL)
		g_object_unref (file);
	if (icc != NULL)
		g_object_unref (icc);
	g_object_unref (icc_ref);
	return ret;
}
/**
 * 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;
}
Esempio n. 4
0
/**
 * main:
 **/
int
main (int argc, char *argv[])
{
	CdUtilPrivate *priv;
	gboolean ret = TRUE;
	gboolean verbose = FALSE;
	guint retval = 1;
	g_autoptr(GError) error = NULL;
	g_autofree gchar *cmd_descriptions = NULL;
	g_autofree gchar *locale = NULL;
	g_autoptr(GFile) file = NULL;
	const GOptionEntry options[] = {
		{ "verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose,
			/* TRANSLATORS: command line option */
			_("Show extra debugging information"), NULL },
		{ "locale", '\0', 0, G_OPTION_ARG_STRING, &locale,
			/* TRANSLATORS: command line option */
			_("The locale to use when setting localized text"), NULL },
		{ NULL}
	};

	setlocale (LC_ALL, "");

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

	/* create helper object */
	priv = g_new0 (CdUtilPrivate, 1);
	priv->rewrite_file = TRUE;
	priv->client = cd_client_new ();

	/* add commands */
	priv->cmd_array = g_ptr_array_new_with_free_func ((GDestroyNotify) cd_util_item_free);
	cd_util_add (priv->cmd_array,
		     "extract-vcgt",
		     /* TRANSLATORS: command description */
		     _("Generate the VCGT calibration of a given size"),
		     cd_util_extract_vcgt);
	cd_util_add (priv->cmd_array,
		     "md-clear",
		     /* TRANSLATORS: command description */
		     _("Clear any metadata in the profile"),
		     cd_util_clear_metadata);
	cd_util_add (priv->cmd_array,
		     "md-init",
		     /* TRANSLATORS: command description */
		     _("Initialize any metadata for the profile"),
		     cd_util_init_metadata);
	cd_util_add (priv->cmd_array,
		     "md-add",
		     /* TRANSLATORS: command description */
		     _("Add a metadata item to the profile"),
		     cd_util_add_metadata);
	cd_util_add (priv->cmd_array,
		     "md-remove",
		     /* TRANSLATORS: command description */
		     _("Remove a metadata item from the profile"),
		     cd_util_remove_metadata);
	cd_util_add (priv->cmd_array,
		     "set-copyright",
		     /* TRANSLATORS: command description */
		     _("Sets the copyright string"),
		     cd_util_set_copyright);
	cd_util_add (priv->cmd_array,
		     "set-description",
		     /* TRANSLATORS: command description */
		     _("Sets the description string"),
		     cd_util_set_description);
	cd_util_add (priv->cmd_array,
		     "set-manufacturer",
		     /* TRANSLATORS: command description */
		     _("Sets the manufacturer string"),
		     cd_util_set_manufacturer);
	cd_util_add (priv->cmd_array,
		     "set-model",
		     /* TRANSLATORS: command description */
		     _("Sets the model string"),
		     cd_util_set_model);
	cd_util_add (priv->cmd_array,
		     "md-fix",
		     /* TRANSLATORS: command description */
		     _("Automatically fix metadata in the profile"),
		     cd_util_set_fix_metadata);
	cd_util_add (priv->cmd_array,
		     "set-version",
		     /* TRANSLATORS: command description */
		     _("Set the ICC profile version"),
		     cd_util_set_version);
	cd_util_add (priv->cmd_array,
		     "export-tag-data",
		     /* TRANSLATORS: command description */
		     _("Export the tag data"),
		     cd_util_export_tag_data);

	/* sort by command name */
	g_ptr_array_sort (priv->cmd_array,
			  (GCompareFunc) cd_sort_command_name_cb);

	/* get a list of the commands */
	priv->context = g_option_context_new (NULL);
	cmd_descriptions = cd_util_get_descriptions (priv->cmd_array);
	g_option_context_set_summary (priv->context, cmd_descriptions);

	/* TRANSLATORS: program name */
	g_set_application_name (_("Color Management"));
	g_option_context_add_main_entries (priv->context, options, NULL);
	ret = g_option_context_parse (priv->context, &argc, &argv, &error);
	if (!ret) {
		/* TRANSLATORS: the user didn't read the man page */
		g_print ("%s: %s\n", _("Failed to parse arguments"),
			 error->message);
		goto out;
	}

	/* use explicit locale */
	priv->locale = g_strdup (locale);

	/* set verbose? */
	if (verbose) {
		g_setenv ("COLORD_VERBOSE", "1", FALSE);
	} else {
		g_log_set_handler (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,
				   cd_util_ignore_cb, NULL);
	}

	/* the first option is always the filename */
	if (argc < 2) {
		g_print ("%s\n", "Filename must be the first argument");
		goto out;
	}

	/* open file */
	file = g_file_new_for_path (argv[1]);
	priv->icc = cd_icc_new ();
	ret = cd_icc_load_file (priv->icc,
				file,
				CD_ICC_LOAD_FLAGS_ALL,
				NULL,
				&error);
	if (!ret) {
		g_print ("%s\n", error->message);
		goto out;
	}

	/* run the specified command */
	ret = cd_util_run (priv, argv[2], (gchar**) &argv[2], &error);
	if (!ret) {
		g_print ("%s\n", error->message);
		goto out;
	}

	/* save file */
	if (priv->rewrite_file) {
		ret = cd_icc_save_file (priv->icc,
					file,
					CD_ICC_SAVE_FLAGS_NONE,
					NULL,
					&error);
		if (!ret) {
			g_print ("%s\n", error->message);
			goto out;
		}
	}

	/* success */
	retval = 0;
out:
	if (priv != NULL) {
		if (priv->cmd_array != NULL)
			g_ptr_array_unref (priv->cmd_array);
		g_option_context_free (priv->context);
		if (priv->icc != NULL)
			g_object_unref (priv->icc);
		g_object_unref (priv->client);
		g_free (priv->locale);
		g_free (priv);
	}
	return retval;
}