[gnome-color-manager] Allow the user to just drag an image file onto the main UI to create a virtual profile



commit 2968c4f3c54a963d6ad8c5a40d81dcc60db2de5d
Author: Richard Hughes <richard hughsie com>
Date:   Fri May 21 14:54:15 2010 +0100

    Allow the user to just drag an image file onto the main UI to create a virtual profile

 src/gcm-exif.c  |    6 ++--
 src/gcm-prefs.c |  107 +++++++++++++++++++++++++++++++++++++++++++-----------
 2 files changed, 88 insertions(+), 25 deletions(-)
---
diff --git a/src/gcm-exif.c b/src/gcm-exif.c
index 5f1adb1..a985383 100644
--- a/src/gcm-exif.c
+++ b/src/gcm-exif.c
@@ -91,7 +91,7 @@ gcm_exif_parse_tiff (GcmExif *exif, const gchar *filename, GError **error)
 		g_set_error (error,
 			     GCM_EXIF_ERROR,
 			     GCM_EXIF_ERROR_NO_DATA,
-			     "failed to get EXIF data from TIFF");
+			     "Failed to get EXIF data from TIFF");
 		ret = FALSE;
 		goto out;
 	}
@@ -178,7 +178,7 @@ gcm_exif_parse_jpeg (GcmExif *exif, const gchar *filename, GError **error)
 		g_set_error (error,
 			     GCM_EXIF_ERROR,
 			     GCM_EXIF_ERROR_NO_DATA,
-			     "failed to get EXIF data from TIFF");
+			     "Failed to get EXIF data from JPEG");
 		ret = FALSE;
 		goto out;
 	}
@@ -236,7 +236,7 @@ gcm_exif_parse (GcmExif *exif, GFile *file, GError **error)
 	g_set_error (error,
 		     GCM_EXIF_ERROR,
 		     GCM_EXIF_ERROR_NO_SUPPORT,
-		     "no support for %s content type", content_type);
+		     "No support for %s content type", content_type);
 out:
 	g_free (filename);
 	if (info != NULL)
diff --git a/src/gcm-prefs.c b/src/gcm-prefs.c
index 1654d7e..bdbc5f8 100644
--- a/src/gcm-prefs.c
+++ b/src/gcm-prefs.c
@@ -563,10 +563,17 @@ gcm_prefs_file_chooser_get_icc_profile (void)
 static gboolean
 gcm_prefs_profile_import_file (GFile *file)
 {
-	gboolean ret = FALSE;
+	gboolean ret;
 	GError *error = NULL;
 	GFile *destination = NULL;
 
+	/* check if correct type */
+	ret = gcm_utils_is_icc_profile (file);
+	if (!ret) {
+		egg_debug ("not a ICC profile");
+		goto out;
+	}
+
 	/* copy icc file to ~/.color/icc */
 	destination = gcm_utils_get_profile_destination (file);
 	ret = gcm_utils_mkdir_and_copy (file, destination, &error);
@@ -577,7 +584,71 @@ gcm_prefs_profile_import_file (GFile *file)
 		goto out;
 	}
 out:
-	g_object_unref (destination);
+	if (destination != NULL)
+		g_object_unref (destination);
+	return ret;
+}
+
+/**
+ * gcm_prefs_profile_add_virtual_file:
+ **/
+static gboolean
+gcm_prefs_profile_add_virtual_file (GFile *file)
+{
+	gboolean ret;
+	GcmExif *exif;
+	GError *error = NULL;
+	GcmDevice *device = NULL;
+
+	/* parse file */
+	exif = gcm_exif_new ();
+	ret = gcm_exif_parse (exif, file, &error);
+	if (!ret) {
+		/* TRANSLATORS: could not add virtual device */
+		if (error->domain != GCM_EXIF_ERROR ||
+		    error->code != GCM_EXIF_ERROR_NO_SUPPORT)
+			gcm_prefs_error_dialog (_("Failed to get metadata from image"), error->message);
+		else
+			egg_debug ("not a supported image format: %s", error->message);
+		g_error_free (error);
+		goto out;
+	}
+
+	/* create device */
+	device = gcm_device_virtual_new	();
+	ret = gcm_device_virtual_create_from_params (GCM_DEVICE_VIRTUAL (device),
+						     gcm_exif_get_device_kind (exif),
+						     gcm_exif_get_model (exif),
+						     gcm_exif_get_manufacturer (exif),
+						     /* FIXME: serial?! */
+						     GCM_COLORSPACE_RGB);
+	if (!ret) {
+		/* TRANSLATORS: could not add virtual device */
+		gcm_prefs_error_dialog (_("Failed to create virtual device"), NULL);
+		goto out;
+	}
+
+	/* save what we've got */
+	ret = gcm_device_save (device, &error);
+	if (!ret) {
+		/* TRANSLATORS: could not add virtual device */
+		gcm_prefs_error_dialog (_("Failed to save virtual device"), error->message);
+		g_error_free (error);
+		goto out;
+	}
+
+	/* add to the device list */
+	ret = gcm_client_add_virtual_device (gcm_client, device, &error);
+	if (!ret) {
+		/* TRANSLATORS: could not add virtual device */
+		gcm_prefs_error_dialog (_("Failed to add virtual device"), error->message);
+		g_error_free (error);
+		goto out;
+	}
+out:
+	g_object_unref (exif);
+	if (device != NULL)
+		g_object_unref (device);
 	return ret;
 }
 
@@ -614,13 +685,12 @@ gcm_prefs_drag_data_received_cb (GtkWidget *widget, GdkDragContext *context, gin
 	GFile *file = NULL;
 	guint i;
 	gboolean ret;
+	gboolean success = FALSE;
 
 	/* get filenames */
 	filename = gtk_selection_data_get_data (data);
-	if (filename == NULL) {
-		gtk_drag_finish (context, FALSE, FALSE, _time);
+	if (filename == NULL)
 		goto out;
-	}
 
 	/* import this */
 	egg_debug ("dropped: %p (%s)", data, filename);
@@ -633,31 +703,24 @@ gcm_prefs_drag_data_received_cb (GtkWidget *widget, GdkDragContext *context, gin
 		if (filenames[i][0] == '\0')
 			continue;
 
-		/* check this is an ICC profile */
-		egg_debug ("trying to import %s", filenames[i]);
+		/* convert the URI */
 		file = g_file_new_for_uri (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);
-			goto out;
-		}
 
 		/* try to import it */
 		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;
-		}
+		if (ret)
+			success = TRUE;
+
+		/* try to add a virtual profile with it */
+		ret = gcm_prefs_profile_add_virtual_file (file);
+		if (ret)
+			success = TRUE;
+
 		g_object_unref (file);
-		file = NULL;
 	}
 
-	gtk_drag_finish (context, TRUE, FALSE, _time);
 out:
-	if (file != NULL)
-		g_object_unref (file);
+	gtk_drag_finish (context, success, FALSE, _time);
 	g_strfreev (filenames);
 }
 



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