[gnome-color-manager] Use GFile internally so we can support importing profiles from gvfs mount points. Fixes #610285



commit cdfe3f9f7539298185a72d0753eb96aa06115ac3
Author: Richard Hughes <richard hughsie com>
Date:   Thu Feb 18 09:07:35 2010 +0000

    Use GFile internally so we can support importing profiles from gvfs mount points. Fixes #610285

 src/gcm-calibrate-argyll.c |   42 +++++++++++++----
 src/gcm-import.c           |   15 ++++--
 src/gcm-prefs.c            |  109 ++++++++++++++++++++++----------------------
 src/gcm-profile-store.c    |    5 ++-
 src/gcm-utils.c            |   49 ++++++++++----------
 src/gcm-utils.h            |    8 ++--
 6 files changed, 130 insertions(+), 98 deletions(-)
---
diff --git a/src/gcm-calibrate-argyll.c b/src/gcm-calibrate-argyll.c
index 0dbb313..eb5ac68 100644
--- a/src/gcm-calibrate-argyll.c
+++ b/src/gcm-calibrate-argyll.c
@@ -826,13 +826,19 @@ gcm_calibrate_argyll_device_copy (GcmCalibrateArgyll *calibrate_argyll, GError *
 {
 	gboolean ret;
 	gchar *device = NULL;
-	gchar *dest_cht = NULL;
-	gchar *dest_ref = NULL;
+	gchar *destination_cht = NULL;
+	gchar *destination_ref = NULL;
 	gchar *filename = NULL;
-	gchar *filename_cht = NULL;
 	gchar *basename = NULL;
+	gchar *filename_cht = NULL;
 	gchar *filename_source = NULL;
 	gchar *filename_reference = NULL;
+	GFile *file_cht = NULL;
+	GFile *file_source = NULL;
+	GFile *file_reference = NULL;
+	GFile *dest_cht = NULL;
+	GFile *dest_source = NULL;
+	GFile *dest_reference = NULL;
 	const gchar *title;
 	const gchar *message;
 	const gchar *filename_tmp;
@@ -857,19 +863,29 @@ gcm_calibrate_argyll_device_copy (GcmCalibrateArgyll *calibrate_argyll, GError *
 	/* build filenames */
 	filename = g_strdup_printf ("%s.tif", basename);
 	device = g_build_filename (GCM_CALIBRATE_ARGYLL_TEMP_DIR, filename, NULL);
-	dest_cht = g_build_filename (GCM_CALIBRATE_ARGYLL_TEMP_DIR, "scanin.cht", NULL);
-	dest_ref = g_build_filename (GCM_CALIBRATE_ARGYLL_TEMP_DIR, "scanin-ref.txt", NULL);
+	destination_cht = g_build_filename (GCM_CALIBRATE_ARGYLL_TEMP_DIR, "scanin.cht", NULL);
+	destination_ref = g_build_filename (GCM_CALIBRATE_ARGYLL_TEMP_DIR, "scanin-ref.txt", NULL);
 
 	/* copy all files to /tmp as argyllcms doesn't cope well with paths */
 	filename_tmp = gcm_calibrate_argyll_reference_kind_to_filename (reference_kind);
 	filename_cht = g_build_filename ("/usr/share/color/argyll/ref", filename_tmp, NULL);
-	ret = gcm_utils_mkdir_and_copy (filename_cht, dest_cht, error);
+
+	/* convert to GFile */
+	file_cht = g_file_new_for_path (filename_cht);
+	file_source = g_file_new_for_path (filename_source);
+	file_reference = g_file_new_for_path (filename_reference);
+	dest_cht = g_file_new_for_path (destination_cht);
+	dest_source = g_file_new_for_path (device);
+	dest_reference = g_file_new_for_path (destination_ref);
+
+	/* do the copy */
+	ret = gcm_utils_mkdir_and_copy (file_cht, dest_cht, error);
 	if (!ret)
 		goto out;
-	ret = gcm_utils_mkdir_and_copy (filename_source, device, error);
+	ret = gcm_utils_mkdir_and_copy (file_source, dest_source, error);
 	if (!ret)
 		goto out;
-	ret = gcm_utils_mkdir_and_copy (filename_reference, dest_ref, error);
+	ret = gcm_utils_mkdir_and_copy (file_reference, dest_reference, error);
 	if (!ret)
 		goto out;
 out:
@@ -879,8 +895,14 @@ out:
 	g_free (filename_source);
 	g_free (filename_reference);
 	g_free (device);
-	g_free (dest_cht);
-	g_free (dest_ref);
+	g_free (destination_cht);
+	g_free (destination_ref);
+	g_object_unref (file_cht);
+	g_object_unref (file_source);
+	g_object_unref (file_reference);
+	g_object_unref (dest_cht);
+	g_object_unref (dest_source);
+	g_object_unref (dest_reference);
 	return ret;
 }
 
diff --git a/src/gcm-import.c b/src/gcm-import.c
index e0902af..ed4846b 100644
--- a/src/gcm-import.c
+++ b/src/gcm-import.c
@@ -60,7 +60,7 @@ main (int argc, char **argv)
 	gboolean ret;
 	gchar *copyright = NULL;
 	gchar *description = NULL;
-	gchar *destination = NULL;
+	GFile *destination = NULL;
 	gchar **files = NULL;
 	guint retval = 1;
 	GcmProfile *profile = NULL;
@@ -74,6 +74,7 @@ main (int argc, char **argv)
 	GcmXyz *red = NULL;
 	GcmXyz *green = NULL;
 	GcmXyz *blue = NULL;
+	GFile *file = NULL;
 
 	const GOptionEntry options[] = {
 		{ G_OPTION_REMAINING, '\0', 0, G_OPTION_ARG_FILENAME_ARRAY, &files,
@@ -145,8 +146,9 @@ main (int argc, char **argv)
 		      NULL);
 
 	/* check file does't already exist */
-	destination = gcm_utils_get_profile_destination (files[0]);
-	ret = g_file_test (destination, G_FILE_TEST_EXISTS);
+	file = g_file_new_for_path (files[0]);
+	destination = gcm_utils_get_profile_destination (file);
+	ret = g_file_query_exists (destination, NULL);
 	if (ret) {
 		/* TRANSLATORS: color profile already been installed */
 		dialog = gtk_message_dialog_new (NULL, GTK_DIALOG_MODAL, GTK_MESSAGE_INFO, GTK_BUTTONS_CLOSE, _("ICC profile already installed"));
@@ -195,7 +197,7 @@ main (int argc, char **argv)
 		goto out;
 
 	/* copy icc file to ~/.color/icc */
-	ret = gcm_utils_mkdir_and_copy (files[0], destination, &error);
+	ret = gcm_utils_mkdir_and_copy (file, destination, &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 copy file"));
@@ -215,6 +217,8 @@ main (int argc, char **argv)
 		goto out;
 	}
 out:
+	if (file != NULL)
+		g_object_unref (file);
 	if (white != NULL)
 		g_object_unref (white);
 	if (red != NULL)
@@ -227,7 +231,8 @@ out:
 		g_string_free (string, TRUE);
 	if (profile != NULL)
 		g_object_unref (profile);
-	g_free (destination);
+	if (destination != NULL)
+		g_object_unref (destination);
 	g_free (description);
 	g_free (copyright);
 	g_strfreev (files);
diff --git a/src/gcm-prefs.c b/src/gcm-prefs.c
index 2ffcc02..4ca0a03 100644
--- a/src/gcm-prefs.c
+++ b/src/gcm-prefs.c
@@ -449,12 +449,12 @@ out:
 /**
  * gcm_prefs_file_chooser_get_icc_profile:
  **/
-static gchar *
+static GFile *
 gcm_prefs_file_chooser_get_icc_profile (void)
 {
-	gchar *filename = NULL;
 	GtkWindow *window;
 	GtkWidget *dialog;
+	GFile *file = NULL;
 	GtkFileFilter *filter;
 
 	/* create new dialog */
@@ -468,6 +468,7 @@ gcm_prefs_file_chooser_get_icc_profile (void)
 	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);
+	gtk_file_chooser_set_local_only (GTK_FILE_CHOOSER(dialog), FALSE);
 
 	/* setup the filter */
 	filter = gtk_file_filter_new ();
@@ -486,30 +487,30 @@ gcm_prefs_file_chooser_get_icc_profile (void)
 
 	/* did user choose file */
 	if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
-		filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER(dialog));
+		file = gtk_file_chooser_get_file (GTK_FILE_CHOOSER(dialog));
 
 	/* we're done */
 	gtk_widget_destroy (dialog);
 
 	/* or NULL for missing */
-	return filename;
+	return file;
 }
 
 /**
- * gcm_prefs_profile_import:
+ * gcm_prefs_profile_import_file:
  **/
 static gboolean
-gcm_prefs_profile_import (const gchar *filename)
+gcm_prefs_profile_import_file (GFile *file)
 {
+	gboolean ret = FALSE;
 	GtkWidget *dialog;
 	GError *error = NULL;
-	gchar *destination = NULL;
+	GFile *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);
+	destination = gcm_utils_get_profile_destination (file);
+	ret = gcm_utils_mkdir_and_copy (file, destination, &error);
 	if (!ret) {
 		/* TRANSLATORS: could not read file */
 		window = GTK_WINDOW(gtk_builder_get_object (builder, "dialog_prefs"));
@@ -522,33 +523,7 @@ gcm_prefs_profile_import (const gchar *filename)
 		goto out;
 	}
 out:
-	g_free (destination);
-	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);
+	g_object_unref (destination);
 	return ret;
 }
 
@@ -558,18 +533,20 @@ out:
 static void
 gcm_prefs_profile_import_cb (GtkWidget *widget, gpointer data)
 {
-	gchar *filename;
+	GFile *file;
 
 	/* get new file */
-	filename = gcm_prefs_file_chooser_get_icc_profile ();
-	if (filename == NULL)
+	file = gcm_prefs_file_chooser_get_icc_profile ();
+	if (file == NULL) {
+		egg_warning ("failed to get filename");
 		goto out;
+	}
 
 	/* import this */
-	gcm_prefs_profile_import (filename);
-
+	gcm_prefs_profile_import_file (file);
 out:
-	g_free (filename);
+	if (file != NULL)
+		g_object_unref (file);
 }
 
 /**
@@ -580,6 +557,7 @@ gcm_prefs_drag_data_received_cb (GtkWidget *widget, GdkDragContext *context, gin
 {
 	const guchar *filename;
 	gchar **filenames = NULL;
+	GFile *file = NULL;
 	guint i;
 	gboolean ret;
 
@@ -598,7 +576,8 @@ gcm_prefs_drag_data_received_cb (GtkWidget *widget, GdkDragContext *context, gin
 	for (i=0; filenames[i]!=NULL; i++) {
 
 		/* check this is an ICC profile */
-		ret = gcm_utils_is_icc_profile (filenames[i]);
+		file = g_file_new_for_path (filenames[i]);
+		ret = gcm_utils_is_icc_profile (file);
 		if (!ret) {
 			egg_debug ("%s is not a ICC profile", filenames[i]);
 			gtk_drag_finish (context, FALSE, FALSE, _time);
@@ -606,16 +585,20 @@ gcm_prefs_drag_data_received_cb (GtkWidget *widget, GdkDragContext *context, gin
 		}
 
 		/* try to import it */
-		ret = gcm_prefs_profile_import_uri (filenames[i]);
+		ret = gcm_prefs_profile_import_file (file);
 		if (!ret) {
 			egg_debug ("%s did not import correctly", filenames[i]);
 			gtk_drag_finish (context, FALSE, FALSE, _time);
 			goto out;
 		}
+		g_object_unref (file);
+		file = NULL;
 	}
 
 	gtk_drag_finish (context, TRUE, FALSE, _time);
 out:
+	if (file != NULL)
+		g_object_unref (file);
 	g_strfreev (filenames);
 }
 
@@ -689,9 +672,11 @@ gcm_prefs_calibrate_cb (GtkWidget *widget, gpointer data)
 	gchar *filename = NULL;
 	guint i;
 	gchar *name;
-	gchar *destination = NULL;
 	GcmProfile *profile;
 	GPtrArray *profile_array = NULL;
+	GFile *file = NULL;
+	GFile *dest = NULL;
+	gchar *destination = NULL;
 
 	/* ensure argyllcms is installed */
 	ret = gcm_prefs_ensure_argyllcms_installed ();
@@ -736,8 +721,9 @@ gcm_prefs_calibrate_cb (GtkWidget *widget, gpointer data)
 	}
 
 	/* copy the ICC file to the proper location */
-	destination = gcm_utils_get_profile_destination (filename);
-	ret = gcm_utils_mkdir_and_copy (filename, destination, &error);
+	file = g_file_new_for_path (filename);
+	dest = gcm_utils_get_profile_destination (file);
+	ret = gcm_utils_mkdir_and_copy (file, dest, &error);
 	if (!ret) {
 		egg_warning ("failed to calibrate: %s", error->message);
 		g_error_free (error);
@@ -748,6 +734,7 @@ gcm_prefs_calibrate_cb (GtkWidget *widget, gpointer data)
 	profile_array = gcm_profile_store_get_array (profile_store);
 
 	/* find an existing profile of this name */
+	destination = g_file_get_path (dest);
 	for (i=0; i<profile_array->len; i++) {
 		profile = g_ptr_array_index (profile_array, i);
 		g_object_get (profile,
@@ -786,6 +773,10 @@ out:
 		g_ptr_array_unref (profile_array);
 	if (calibrate != NULL)
 		g_object_unref (calibrate);
+	if (file != NULL)
+		g_object_unref (file);
+	if (dest != NULL)
+		g_object_unref (dest);
 }
 
 /**
@@ -1625,7 +1616,7 @@ static void
 gcm_prefs_profile_combo_changed_cb (GtkWidget *widget, gpointer data)
 {
 	gchar *profile_old = NULL;
-	gchar *filename = NULL;
+	GFile *file = NULL;
 	gboolean ret;
 	GError *error = NULL;
 	GcmProfile *profile = NULL;
@@ -1636,6 +1627,7 @@ gcm_prefs_profile_combo_changed_cb (GtkWidget *widget, gpointer data)
 	GtkTreeModel *model;
 	GcmPrefsEntryType entry_type;
 	gboolean has_vcgt;
+	gchar *filename = NULL;
 
 	/* no devices */
 	if (current_device == NULL)
@@ -1655,13 +1647,20 @@ gcm_prefs_profile_combo_changed_cb (GtkWidget *widget, gpointer data)
 
 	/* import */
 	if (entry_type == GCM_PREFS_ENTRY_TYPE_IMPORT) {
-		filename = gcm_prefs_file_chooser_get_icc_profile ();
-		if (filename == NULL) {
+		file = gcm_prefs_file_chooser_get_icc_profile ();
+		if (file == NULL) {
+			egg_warning ("failed to get ICC file");
 			gtk_combo_box_set_active (GTK_COMBO_BOX (widget), 0);
 			goto out;
 		}
-		gcm_prefs_profile_import (filename);
-		goto out;
+		ret = gcm_prefs_profile_import_file (file);
+		if (!ret) {
+			gchar *uri;
+			uri = g_file_get_uri (file);
+			egg_debug ("%s did not import correctly", uri);
+			g_free (uri);
+			goto out;
+		}
 	}
 
 	/* get profile filename */
@@ -1711,8 +1710,10 @@ gcm_prefs_profile_combo_changed_cb (GtkWidget *widget, gpointer data)
 		}
 	}
 out:
-	g_free (filename);
+	if (file != NULL)
+		g_object_unref (file);
 	g_free (profile_old);
+	g_free (filename);
 }
 
 /**
diff --git a/src/gcm-profile-store.c b/src/gcm-profile-store.c
index ffa803b..be06a12 100644
--- a/src/gcm-profile-store.c
+++ b/src/gcm-profile-store.c
@@ -247,7 +247,10 @@ gcm_profile_store_add_profiles_for_path (GcmProfileStore *profile_store, const g
 	if (g_file_test (path, G_FILE_TEST_IS_REGULAR)) {
 
 		/* check the file actually is a profile */
-		ret = gcm_utils_is_icc_profile (path);
+		file = g_file_new_for_path (path);
+		ret = gcm_utils_is_icc_profile (file);
+		g_object_unref (file);
+		file = NULL;
 		if (ret) {
 			gcm_profile_store_add_profile (profile_store, path);
 			goto out;
diff --git a/src/gcm-utils.c b/src/gcm-utils.c
index 7fbad67..9c0992f 100644
--- a/src/gcm-utils.c
+++ b/src/gcm-utils.c
@@ -76,16 +76,16 @@ gcm_utils_linkify (const gchar *text)
  * gcm_utils_is_icc_profile:
  **/
 gboolean
-gcm_utils_is_icc_profile (const gchar *filename)
+gcm_utils_is_icc_profile (GFile *file)
 {
-	GFile *file;
 	GFileInfo *info;
 	const gchar *type;
 	GError *error = NULL;
 	gboolean ret = FALSE;
+	gchar *filename = NULL;
 
 	/* get content type for file */
-	file = g_file_new_for_path (filename);
+	filename = g_file_get_path (file);
 	info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE, G_FILE_QUERY_INFO_NONE, NULL, &error);
 	if (info != NULL) {
 		type = g_file_info_get_attribute_string (info, G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE);
@@ -118,7 +118,7 @@ gcm_utils_is_icc_profile (const gchar *filename)
 out:
 	if (info != NULL)
 		g_object_unref (info);
-	g_object_unref (file);
+	g_free (filename);
 	return ret;
 }
 
@@ -246,7 +246,7 @@ gcm_utils_mkdir_with_parents (const gchar *filename, GError **error)
 	/* ensure desination exists */
 	ret = g_file_test (filename, G_FILE_TEST_EXISTS);
 	if (!ret) {
-		file = g_file_new_for_path (filename);
+		file = g_file_new_for_uri (filename);
 		ret = g_file_make_directory_with_parents  (file, NULL, error);
 		if (!ret)
 			goto out;
@@ -267,7 +267,7 @@ gcm_utils_mkdir_for_filename (const gchar *filename, GError **error)
 	GFile *file;
 	GFile *parent_dir;
 
-	file = g_file_new_for_path (filename);
+	file = g_file_new_for_uri (filename);
 	parent_dir = g_file_get_parent (file);
 
 	/* ensure desination exists */
@@ -287,52 +287,53 @@ out:
  * gcm_utils_mkdir_and_copy:
  **/
 gboolean
-gcm_utils_mkdir_and_copy (const gchar *source, const gchar *destination, GError **error)
+gcm_utils_mkdir_and_copy (GFile *source, GFile *destination, GError **error)
 {
 	gboolean ret;
-	GFile *sourcefile;
-	GFile *destfile;
+	GFile *parent;
 
 	g_return_val_if_fail (source != NULL, FALSE);
 	g_return_val_if_fail (destination != NULL, FALSE);
 
-	/* setup paths */
-	sourcefile = g_file_new_for_path (source);
-	destfile = g_file_new_for_path (destination);
+	/* get parent */
+	parent = g_file_get_parent (destination);
 
 	/* create directory */
-	ret = gcm_utils_mkdir_for_filename (destination, error);
-	if (!ret)
-		goto out;
+	if (!g_file_query_exists (parent, NULL)) {
+		ret = g_file_make_directory_with_parents (parent, NULL, error);
+		if (!ret)
+			goto out;
+	}
 
 	/* do the copy */
-	egg_debug ("copying from %s to %s", source, destination);
-	ret = g_file_copy (sourcefile, destfile, G_FILE_COPY_OVERWRITE, NULL, NULL, NULL, error);
+	ret = g_file_copy (source, destination, G_FILE_COPY_OVERWRITE, NULL, NULL, NULL, error);
 	if (!ret)
 		goto out;
 out:
-	g_object_unref (sourcefile);
-	g_object_unref (destfile);
+	g_object_unref (parent);
 	return ret;
 }
 
 /**
  * gcm_utils_get_profile_destination:
  **/
-gchar *
-gcm_utils_get_profile_destination (const gchar *filename)
+GFile *
+gcm_utils_get_profile_destination (GFile *file)
 {
 	gchar *basename;
 	gchar *destination;
+	GFile *dest;
 
-	g_return_val_if_fail (filename != NULL, NULL);
+	g_return_val_if_fail (file != NULL, NULL);
 
 	/* get destination filename for this source file */
-	basename = g_path_get_basename (filename);
+	basename = g_file_get_basename (file);
 	destination = g_build_filename (g_get_home_dir (), GCM_PROFILE_PATH, basename, NULL);
+	dest = g_file_new_for_path (destination);
 
 	g_free (basename);
-	return destination;
+	g_free (destination);
+	return dest;
 }
 
 /**
diff --git a/src/gcm-utils.h b/src/gcm-utils.h
index cbf60c7..0cef661 100644
--- a/src/gcm-utils.h
+++ b/src/gcm-utils.h
@@ -52,10 +52,10 @@ gboolean	 gcm_utils_mkdir_for_filename		(const gchar		*filename,
 							 GError			**error);
 gboolean	 gcm_utils_mkdir_with_parents		(const gchar		*filename,
 							 GError			**error);
-gboolean	 gcm_utils_mkdir_and_copy		(const gchar		*source,
-							 const gchar		*destination,
+gboolean	 gcm_utils_mkdir_and_copy		(GFile			*source,
+							 GFile			*destination,
 							 GError			**error);
-gchar		*gcm_utils_get_profile_destination	(const gchar		*filename);
+GFile		*gcm_utils_get_profile_destination	(GFile			*file);
 gchar		**gcm_utils_ptr_array_to_strv		(GPtrArray		*array);
 gboolean	 gcm_gnome_help				(const gchar		*link_id);
 gboolean	 gcm_utils_output_is_lcd_internal	(const gchar		*output_name);
@@ -68,7 +68,7 @@ gchar		*gcm_utils_format_date_time		(const struct tm	*created);
 gboolean	 gcm_utils_install_package		(const gchar		*package_name,
 							 GtkWindow		*window);
 void		 gcm_utils_ensure_printable		(gchar			*text);
-gboolean	 gcm_utils_is_icc_profile		(const gchar		*filename);
+gboolean	 gcm_utils_is_icc_profile		(GFile			*file);
 gchar		*gcm_utils_linkify			(const gchar		*text);
 const gchar	*gcm_intent_enum_to_localized_text	(GcmIntentEnum	 intent);
 



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