[gnome-color-manager] Allow parsing of ICC data by data, as well as by filename



commit 429bc5259c6ef12cdf40e1379e6e8548355c7210
Author: Richard Hughes <richard hughsie com>
Date:   Tue Nov 3 15:54:18 2009 +0000

    Allow parsing of ICC data by data, as well as by filename

 src/gcm-clut.c    |    2 +-
 src/gcm-import.c  |    2 +-
 src/gcm-prefs.c   |    2 +-
 src/gcm-profile.c |   62 ++++++++++++++++++++++++++++++++++++++--------------
 src/gcm-profile.h |    6 ++++-
 src/gcm-utils.c   |   24 ++++++++++----------
 src/gcm-utils.h   |    4 +-
 7 files changed, 67 insertions(+), 35 deletions(-)
---
diff --git a/src/gcm-clut.c b/src/gcm-clut.c
index aec402d..dfae3dd 100644
--- a/src/gcm-clut.c
+++ b/src/gcm-clut.c
@@ -128,7 +128,7 @@ gcm_clut_load_from_profile (GcmClut *clut, GError **error)
 	profile = gcm_profile_new ();
 
 	/* load the profile */
-	ret = gcm_profile_load (profile, clut->priv->profile, &error_local);
+	ret = gcm_profile_parse (profile, clut->priv->profile, &error_local);
 	if (!ret) {
 		if (error != NULL)
 			*error = g_error_new (1, 0, "failed to set from profile: %s", error_local->message);
diff --git a/src/gcm-import.c b/src/gcm-import.c
index 5be77d6..19217dd 100644
--- a/src/gcm-import.c
+++ b/src/gcm-import.c
@@ -78,7 +78,7 @@ main (int argc, char **argv)
 
 	/* load profile */
 	profile = gcm_profile_new ();
-	ret = gcm_profile_load (profile, files[0], &error);
+	ret = gcm_profile_parse (profile, files[0], &error);
 	if (!ret) {
 		/* TRANSLATORS: could not read file */
 		dialog = gtk_message_dialog_new (NULL, GTK_DIALOG_MODAL, GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE, _("Failed to open ICC profile"));
diff --git a/src/gcm-prefs.c b/src/gcm-prefs.c
index 061b7d8..f03c134 100644
--- a/src/gcm-prefs.c
+++ b/src/gcm-prefs.c
@@ -465,7 +465,7 @@ gcm_prefs_add_profiles (GtkWidget *widget)
 	for (i=0; i<profiles_array->len; i++) {
 		filename = g_ptr_array_index (profiles_array, i);
 		profile = gcm_profile_new ();
-		ret = gcm_profile_load (profile, filename, &error);
+		ret = gcm_profile_parse (profile, filename, &error);
 		if (!ret) {
 			egg_warning ("failed to add profile: %s", error->message);
 			g_error_free (error);
diff --git a/src/gcm-profile.c b/src/gcm-profile.c
index 54030b5..5562df2 100644
--- a/src/gcm-profile.c
+++ b/src/gcm-profile.c
@@ -473,14 +473,12 @@ gcm_parser_load_icc_trc (GcmProfile *profile, const gchar *data, gsize offset, g
 }
 
 /**
- * gcm_profile_load:
+ * gcm_profile_parse_data:
  **/
 gboolean
-gcm_profile_load (GcmProfile *profile, const gchar *filename, GError **error)
+gcm_profile_parse_data (GcmProfile *profile, const gchar *data, gsize length, GError **error)
 {
-	gchar *data = NULL;
 	gboolean ret;
-	gsize length;
 	GError *error_local = NULL;
 	guint num_tags;
 	guint i;
@@ -488,23 +486,14 @@ gcm_profile_load (GcmProfile *profile, const gchar *filename, GError **error)
 	guint offset;
 	guint tag_size;
 	guint tag_offset;
+	gchar *signature;
 
 	g_return_val_if_fail (GCM_IS_PROFILE (profile), FALSE);
-	g_return_val_if_fail (filename != NULL, FALSE);
+	g_return_val_if_fail (data != NULL, FALSE);
 	g_return_val_if_fail (profile->priv->loaded == FALSE, FALSE);
 
-	egg_debug ("loading '%s'", filename);
 	profile->priv->loaded = TRUE;
 
-	/* load files */
-	ret = g_file_get_contents (filename, &data, &length, &error_local);
-	if (!ret) {
-		if (error != NULL)
-			*error = g_error_new (1, 0, "failed to load profile: %s", error_local->message);
-		g_error_free (error_local);
-		goto out;
-	}
-
 	/* ensure we have the header */
 	if (length < 0x84) {
 		if (error != NULL)
@@ -518,9 +507,14 @@ gcm_profile_load (GcmProfile *profile, const gchar *filename, GError **error)
 	    data[GCM_SIGNATURE+1] != 'c' ||
 	    data[GCM_SIGNATURE+2] != 's' ||
 	    data[GCM_SIGNATURE+3] != 'p') {
-		data[GCM_SIGNATURE+4] = '\0';
+
+		/* copy the 4 bytes of the invalid signature, with a '\0' byte */
+		signature = g_new0 (gchar, 5);
+		for (i=0; i<5; i++)
+			signature[i] = data[GCM_SIGNATURE+i];
 		if (error != NULL)
-			*error = g_error_new (1, 0, "not an ICC profile, signature is '%s', expecting 'acsp'", &data[GCM_SIGNATURE]);
+			*error = g_error_new (1, 0, "not an ICC profile, signature is '%s', expecting 'acsp'", signature);
+		g_free (signature);
 		ret = FALSE;
 		goto out;
 	}
@@ -593,6 +587,40 @@ gcm_profile_load (GcmProfile *profile, const gchar *filename, GError **error)
 	egg_debug ("Has curve table:  %s", profile->priv->has_curve_table ? "YES" : "NO");
 	egg_debug ("Has fixed gamma:  %s", profile->priv->has_curve_fixed ? "YES" : "NO");
 out:
+	return ret;
+}
+
+/**
+ * gcm_profile_parse:
+ **/
+gboolean
+gcm_profile_parse (GcmProfile *profile, const gchar *filename, GError **error)
+{
+	gchar *data = NULL;
+	gboolean ret;
+	gsize length;
+	GError *error_local = NULL;
+
+	g_return_val_if_fail (GCM_IS_PROFILE (profile), FALSE);
+	g_return_val_if_fail (filename != NULL, FALSE);
+	g_return_val_if_fail (profile->priv->loaded == FALSE, FALSE);
+
+	egg_debug ("loading '%s'", filename);
+
+	/* load files */
+	ret = g_file_get_contents (filename, &data, &length, &error_local);
+	if (!ret) {
+		if (error != NULL)
+			*error = g_error_new (1, 0, "failed to load profile: %s", error_local->message);
+		g_error_free (error_local);
+		goto out;
+	}
+
+	/* parse the data */
+	ret = gcm_profile_parse_data (profile, data, length, error);
+	if (!ret)
+		goto out;
+out:
 	g_free (data);
 	return ret;
 }
diff --git a/src/gcm-profile.h b/src/gcm-profile.h
index c13fb4b..d742110 100644
--- a/src/gcm-profile.h
+++ b/src/gcm-profile.h
@@ -57,9 +57,13 @@ struct _GcmProfileClass
 
 GType		 gcm_profile_get_type		  	(void);
 GcmProfile	*gcm_profile_new			(void);
-gboolean	 gcm_profile_load			(GcmProfile	*profile,
+gboolean	 gcm_profile_parse			(GcmProfile	*profile,
 							 const gchar	*filename,
 							 GError		**error);
+gboolean	 gcm_profile_parse_data			(GcmProfile	*profile,
+							 const gchar	*data,
+							 gsize		 length,
+							 GError		**error);
 GcmClutData	*gcm_profile_generate			(GcmProfile	*profile,
 							 guint		 size);
 
diff --git a/src/gcm-utils.c b/src/gcm-utils.c
index c4076ed..da242d3 100644
--- a/src/gcm-utils.c
+++ b/src/gcm-utils.c
@@ -460,7 +460,7 @@ gcm_gnome_help (const gchar *link_id)
  *
  * @id: the ID that is used according to ICC Profiles In X Specification
  * @data: the data that is returned from the XServer. Free with g_free()
- * @size: the size of the returned data, or %NULL if you don't care
+ * @length: the size of the returned data, or %NULL if you don't care
  * @error: a %GError that is set in the result of an error, or %NULL
  * Return value: %TRUE for success.
  *
@@ -470,7 +470,7 @@ gcm_gnome_help (const gchar *link_id)
  * Gets the ICC profile data from the XServer.
  **/
 gboolean
-gcm_utils_get_x11_icc_profile_data (guint id, guint8 **data, gsize *size, GError **error)
+gcm_utils_get_x11_icc_profile_data (guint id, guint8 **data, gsize *length, GError **error)
 {
 	gboolean ret = FALSE;
 	gchar *atom_name;
@@ -525,9 +525,9 @@ gcm_utils_get_x11_icc_profile_data (guint id, guint8 **data, gsize *size, GError
 	*data = g_new0 (guint8, nitems);
 	memcpy (*data, data_tmp, nitems);
 
-	/* copy the size */
-	if (size != NULL)
-		*size = nitems;
+	/* copy the length */
+	if (length != NULL)
+		*length = nitems;
 
 	/* success */
 	ret = TRUE;
@@ -555,17 +555,17 @@ gcm_utils_set_x11_icc_profile (guint id, const gchar *filename, GError **error)
 {
 	gboolean ret;
 	gchar *data = NULL;
-	gsize size;
+	gsize length;
 
 	g_return_val_if_fail (filename != NULL, FALSE);
 
 	/* get contents of file */
-	ret = g_file_get_contents (filename, &data, &size, error);
+	ret = g_file_get_contents (filename, &data, &length, error);
 	if (!ret)
 		goto out;
 
 	/* send to the XServer */
-	ret = gcm_utils_set_x11_icc_profile_data (id, (const guint8 *) data, size, error);
+	ret = gcm_utils_set_x11_icc_profile_data (id, (const guint8 *) data, length, error);
 	if (!ret)
 		goto out;
 out:
@@ -577,7 +577,7 @@ out:
  * gcm_utils_set_x11_icc_profile_data:
  * @id: the ID that is used according to ICC Profiles In X Specification
  * @data: the data that is to be set to the XServer
- * @size: the size of the data
+ * @length: the size of the data
  * @error: a %GError that is set in the result of an error, or %NULL
  * Return value: %TRUE for success.
  *
@@ -587,7 +587,7 @@ out:
  * map to the RROutput name or ID. Seek clarification.
  **/
 gboolean
-gcm_utils_set_x11_icc_profile_data (guint id, const guint8 *data, gsize size, GError **error)
+gcm_utils_set_x11_icc_profile_data (guint id, const guint8 *data, gsize length, GError **error)
 {
 	gboolean ret = FALSE;
 	gchar *atom_name;
@@ -599,7 +599,7 @@ gcm_utils_set_x11_icc_profile_data (guint id, const guint8 *data, gsize size, GE
 	Window window;
 
 	g_return_val_if_fail (data != NULL, FALSE);
-	g_return_val_if_fail (size != 0, FALSE);
+	g_return_val_if_fail (length != 0, FALSE);
 
 	/* get defaults for single screen */
 	display_gdk = gdk_display_get_default ();
@@ -616,7 +616,7 @@ gcm_utils_set_x11_icc_profile_data (guint id, const guint8 *data, gsize size, GE
 	/* get the value */
 	gdk_error_trap_push ();
 	atom = gdk_x11_get_xatom_by_name_for_display (display_gdk, atom_name);
-	rc = XChangeProperty (display, window, atom, XA_CARDINAL, 8, PropModeReplace, (unsigned char*) data, size);
+	rc = XChangeProperty (display, window, atom, XA_CARDINAL, 8, PropModeReplace, (unsigned char*) data, length);
 	gdk_error_trap_pop ();
 
 	/* for some reason this fails with BadRequest, but actually sets the value */
diff --git a/src/gcm-utils.h b/src/gcm-utils.h
index 24bb56b..10549ce 100644
--- a/src/gcm-utils.h
+++ b/src/gcm-utils.h
@@ -53,11 +53,11 @@ gchar		**gcm_utils_ptr_array_to_strv		(GPtrArray		*array);
 gboolean	 gcm_gnome_help				(const gchar		*link_id);
 gboolean	 gcm_utils_get_x11_icc_profile_data	(guint			 id,
 							 guint8			**data,
-							 gsize			*size,
+							 gsize			*length,
 							 GError			**error);
 gboolean	 gcm_utils_set_x11_icc_profile_data	(guint			 id,
 							 const guint8		*data,
-							 gsize			 size,
+							 gsize			 length,
 							 GError			**error);
 gboolean	 gcm_utils_set_x11_icc_profile		(guint			 id,
 							 const gchar		*filename,



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]