[gnome-color-manager] trivial: add the conveience function gcm_calibrate_set_from_device() to abstract out some code from



commit 8552bd19855195525368e95f5e183d039605a662
Author: Richard Hughes <richard hughsie com>
Date:   Fri Feb 12 11:55:01 2010 +0000

    trivial: add the conveience function gcm_calibrate_set_from_device() to abstract out some code from gcm-prefs

 src/gcm-calibrate.c |  160 ++++++++++++++++++++++++++++++++++++++++++++++++
 src/gcm-calibrate.h |    5 ++
 src/gcm-prefs.c     |  169 ++++-----------------------------------------------
 3 files changed, 177 insertions(+), 157 deletions(-)
---
diff --git a/src/gcm-calibrate.c b/src/gcm-calibrate.c
index 8b82884..a0a066a 100644
--- a/src/gcm-calibrate.c
+++ b/src/gcm-calibrate.c
@@ -34,6 +34,7 @@
 #include "gcm-calibrate.h"
 #include "gcm-utils.h"
 #include "gcm-brightness.h"
+#include "gcm-color-device.h"
 
 #include "egg-debug.h"
 
@@ -48,6 +49,7 @@ static void     gcm_calibrate_finalize	(GObject     *object);
  **/
 struct _GcmCalibratePrivate
 {
+	GcmColorDevice			*color_device;
 	gboolean			 is_lcd;
 	gboolean			 is_crt;
 	gchar				*output_name;
@@ -79,6 +81,162 @@ enum {
 
 G_DEFINE_TYPE (GcmCalibrate, gcm_calibrate, G_TYPE_OBJECT)
 
+
+/**
+ * gcm_calibrate_get_time:
+ **/
+static gchar *
+gcm_calibrate_get_time (void)
+{
+	gchar *text;
+	time_t c_time;
+
+	/* get the time now */
+	time (&c_time);
+	text = g_new0 (gchar, 255);
+
+	/* format text */
+	strftime (text, 254, "%H-%M-%S", localtime (&c_time));
+	return text;
+}
+
+/**
+ * gcm_calibrate_get_basename_for_device:
+ **/
+static gchar *
+gcm_calibrate_get_basename_for_device (GcmDevice *device)
+{
+	gchar *serial = NULL;
+	gchar *manufacturer = NULL;
+	gchar *model = NULL;
+	gchar *timespec = NULL;
+	GDate *date = NULL;
+	GString *basename;
+
+	/* get device properties */
+	g_object_get (device,
+		      "serial", &serial,
+		      "manufacturer", &manufacturer,
+		      "model", &model,
+		      NULL);
+
+	/* create date and set it to now */
+	date = g_date_new ();
+	g_date_set_time_t (date, time (NULL));
+	timespec = gcm_calibrate_get_time ();
+
+	/* form basename */
+	basename = g_string_new ("GCM");
+	if (manufacturer != NULL)
+		g_string_append_printf (basename, " - %s", manufacturer);
+	if (model != NULL)
+		g_string_append_printf (basename, " - %s", model);
+	if (serial != NULL)
+		g_string_append_printf (basename, " - %s", serial);
+	g_string_append_printf (basename, " (%04i-%02i-%02i)", date->year, date->month, date->day);
+
+	/* maybe configure in GConf? */
+	if (0)
+		g_string_append_printf (basename, " [%s]", timespec);
+
+	g_date_free (date);
+	g_free (serial);
+	g_free (manufacturer);
+	g_free (model);
+	g_free (timespec);
+	return g_string_free (basename, FALSE);
+}
+
+/**
+ * gcm_calibrate_set_from_device:
+ **/
+gboolean
+gcm_calibrate_set_from_device (GcmCalibrate *calibrate, GcmDevice *device, GError **error)
+{
+	gboolean ret = TRUE;
+	gchar *native_device = NULL;
+	gchar *basename = NULL;
+	gchar *manufacturer = NULL;
+	gchar *model = NULL;
+	gchar *description = NULL;
+	gchar *hardware_device = NULL;
+	GcmDeviceTypeEnum type;
+	GcmCalibratePrivate *priv = calibrate->priv;
+
+	/* get the device */
+	g_object_get (device,
+		      "native-device", &native_device,
+		      "type", &type,
+//		      "serial", &basename,
+		      "model", &model,
+		      "title", &description,
+		      "manufacturer", &manufacturer,
+		      NULL);
+	if (native_device == NULL) {
+		g_set_error (error, 1, 0, "failed to get output");
+		ret = FALSE;
+		goto out;
+	}
+
+	/* get a filename based on the serial number */
+	basename = gcm_calibrate_get_basename_for_device (device);
+
+	/* get model */
+	if (model == NULL) {
+		/* TRANSLATORS: this is saved in the profile */
+		model = g_strdup (_("Unknown model"));
+	}
+
+	/* get description */
+	if (description == NULL) {
+		/* TRANSLATORS: this is saved in the profile */
+		description = g_strdup (_("Unknown device"));
+	}
+
+	/* get manufacturer */
+	if (manufacturer == NULL) {
+		/* TRANSLATORS: this is saved in the profile */
+		manufacturer = g_strdup (_("Unknown manufacturer"));
+	}
+
+	/* set the proper output name */
+	g_object_set (calibrate,
+		      "basename", basename,
+		      "model", model,
+		      "description", description,
+		      "manufacturer", manufacturer,
+		      NULL);
+
+	/* display specific properties */
+	if (type == GCM_DEVICE_TYPE_ENUM_DISPLAY) {
+
+		/* get calibration device model */
+		g_object_get (priv->color_device,
+			      "model", &hardware_device,
+			      NULL);
+
+		/* get device, harder */
+		if (hardware_device == NULL) {
+			/* TRANSLATORS: this is the formattted custom profile description. "Custom" refers to the fact that it's user generated */
+			hardware_device = g_strdup (_("Custom"));
+		}
+
+		g_object_set (calibrate,
+			      "output-name", native_device,
+			      "device", hardware_device,
+			      NULL);
+	}
+
+out:
+	g_free (device);
+	g_free (native_device);
+	g_free (basename);
+	g_free (manufacturer);
+	g_free (model);
+	g_free (description);
+	return ret;
+}
+
 /**
  * gcm_calibrate_display:
  **/
@@ -500,6 +658,7 @@ gcm_calibrate_init (GcmCalibrate *calibrate)
 	calibrate->priv->model = NULL;
 	calibrate->priv->description = NULL;
 	calibrate->priv->device = NULL;
+	calibrate->priv->color_device = gcm_color_device_new ();
 }
 
 /**
@@ -520,6 +679,7 @@ gcm_calibrate_finalize (GObject *object)
 	g_free (priv->model);
 	g_free (priv->description);
 	g_free (priv->device);
+	g_object_unref (priv->color_device);
 
 	G_OBJECT_CLASS (gcm_calibrate_parent_class)->finalize (object);
 }
diff --git a/src/gcm-calibrate.h b/src/gcm-calibrate.h
index d5cd9b8..4e69229 100644
--- a/src/gcm-calibrate.h
+++ b/src/gcm-calibrate.h
@@ -24,6 +24,8 @@
 
 #include <glib-object.h>
 
+#include "gcm-device.h"
+
 G_BEGIN_DECLS
 
 #define GCM_TYPE_CALIBRATE		(gcm_calibrate_get_type ())
@@ -69,6 +71,9 @@ gboolean	 gcm_calibrate_display			(GcmCalibrate	*calibrate,
 gboolean	 gcm_calibrate_device			(GcmCalibrate	*calibrate,
 							 GtkWindow	*window,
 							 GError		**error);
+gboolean	 gcm_calibrate_set_from_device		(GcmCalibrate	*calibrate,
+							 GcmDevice	*device,
+							 GError		**error);
 
 G_END_DECLS
 
diff --git a/src/gcm-prefs.c b/src/gcm-prefs.c
index 8dd8e6c..ba5f1e3 100644
--- a/src/gcm-prefs.c
+++ b/src/gcm-prefs.c
@@ -233,71 +233,6 @@ gcm_prefs_delete_event_cb (GtkWidget *widget, GdkEvent *event, gpointer data)
 }
 
 /**
- * gcm_prefs_get_time:
- **/
-static gchar *
-gcm_prefs_get_time (void)
-{
-	gchar *text;
-	time_t c_time;
-
-	/* get the time now */
-	time (&c_time);
-	text = g_new0 (gchar, 255);
-
-	/* format text */
-	strftime (text, 254, "%H-%M-%S", localtime (&c_time));
-	return text;
-}
-
-/**
- * gcm_prefs_calibrate_get_basename:
- **/
-static gchar *
-gcm_prefs_calibrate_get_basename (GcmDevice *device)
-{
-	gchar *serial = NULL;
-	gchar *manufacturer = NULL;
-	gchar *model = NULL;
-	gchar *timespec = NULL;
-	GDate *date = NULL;
-	GString *basename;
-
-	/* get device properties */
-	g_object_get (device,
-		      "serial", &serial,
-		      "manufacturer", &manufacturer,
-		      "model", &model,
-		      NULL);
-
-	/* create date and set it to now */
-	date = g_date_new ();
-	g_date_set_time_t (date, time (NULL));
-	timespec = gcm_prefs_get_time ();
-
-	/* form basename */
-	basename = g_string_new ("GCM");
-	if (manufacturer != NULL)
-		g_string_append_printf (basename, " - %s", manufacturer);
-	if (model != NULL)
-		g_string_append_printf (basename, " - %s", model);
-	if (serial != NULL)
-		g_string_append_printf (basename, " - %s", serial);
-	g_string_append_printf (basename, " (%04i-%02i-%02i)", date->year, date->month, date->day);
-
-	/* maybe configure in GConf? */
-	if (0)
-		g_string_append_printf (basename, " [%s]", timespec);
-
-	g_date_free (date);
-	g_free (serial);
-	g_free (manufacturer);
-	g_free (model);
-	g_free (timespec);
-	return g_string_free (basename, FALSE);
-}
-
-/**
  * gcm_prefs_calibrate_display:
  **/
 static gboolean
@@ -306,73 +241,20 @@ gcm_prefs_calibrate_display (GcmCalibrate *calibrate)
 	gboolean ret = FALSE;
 	gboolean ret_tmp;
 	GError *error = NULL;
-	gchar *output_name = NULL;
-	gchar *basename = NULL;
-	gchar *manufacturer = NULL;
-	gchar *model = NULL;
-	gchar *description = NULL;
-	gchar *device = NULL;
 	GtkWindow *window;
 
 	/* no device */
 	if (current_device == NULL)
 		goto out;
 
-	/* get the device */
-	g_object_get (current_device,
-		      "native-device", &output_name,
-		      "serial", &basename,
-		      "manufacturer", &manufacturer,
-		      "model", &model,
-		      "title", &description,
-		      NULL);
-	if (output_name == NULL) {
-		egg_warning ("failed to get output");
+	/* set properties from the device */
+	ret = gcm_calibrate_set_from_device (calibrate, current_device, &error);
+	if (!ret) {
+		egg_warning ("failed to calibrate: %s", error->message);
+		g_error_free (error);
 		goto out;
 	}
 
-	/* get a filename based on the serial number */
-	basename = gcm_prefs_calibrate_get_basename (current_device);
-
-	/* get model */
-	if (model == NULL) {
-		/* TRANSLATORS: this is saved in the profile */
-		model = g_strdup (_("Unknown model"));
-	}
-
-	/* get description */
-	if (description == NULL) {
-		/* TRANSLATORS: this is saved in the profile */
-		description = g_strdup (_("Unknown display"));
-	}
-
-	/* get manufacturer */
-	if (manufacturer == NULL) {
-		/* TRANSLATORS: this is saved in the profile */
-		manufacturer = g_strdup (_("Unknown manufacturer"));
-	}
-
-	/* get calibration device model */
-	g_object_get (color_device,
-		      "model", &device,
-		      NULL);
-
-	/* get device, harder */
-	if (device == NULL) {
-		/* TRANSLATORS: this is the formattted custom profile description. "Custom" refers to the fact that it's user generated */
-		device = g_strdup (_("Custom"));
-	}
-
-	/* set the proper output name */
-	g_object_set (calibrate,
-		      "output-name", output_name,
-		      "basename", basename,
-		      "model", model,
-		      "description", description,
-		      "manufacturer", manufacturer,
-		      "device", device,
-		      NULL);
-
 	/* run each task in order */
 	window = GTK_WINDOW(gtk_builder_get_object (builder, "dialog_prefs"));
 	ret = gcm_calibrate_display (calibrate, window, &error);
@@ -389,13 +271,6 @@ out:
 		egg_warning ("failed to apply profile: %s", error->message);
 		g_error_free (error);
 	}
-
-	g_free (device);
-	g_free (output_name);
-	g_free (basename);
-	g_free (manufacturer);
-	g_free (model);
-	g_free (description);
 	return ret;
 }
 
@@ -712,10 +587,6 @@ gcm_prefs_calibrate_device (GcmCalibrate *calibrate)
 	GError *error = NULL;
 	gchar *scanned_image = NULL;
 	gchar *reference_data = NULL;
-	gchar *basename = NULL;
-	gchar *manufacturer = NULL;
-	gchar *model = NULL;
-	gchar *description = NULL;
 	gchar *device = NULL;
 	const gchar *directory;
 	GtkWindow *window;
@@ -794,13 +665,6 @@ gcm_prefs_calibrate_device (GcmCalibrate *calibrate)
 	if (response != GTK_RESPONSE_YES)
 		goto out;
 
-	/* get the device */
-	g_object_get (current_device,
-		      "model", &model,
-		      "title", &description,
-		      "manufacturer", &manufacturer,
-		      NULL);
-
 	/* set the reference kind */
 	reference_kind = gcm_prefs_get_reference_kind ();
 	if (reference_kind == GCM_CALIBRATE_ARGYLL_REFERENCE_KIND_UNKNOWN) {
@@ -821,14 +685,13 @@ gcm_prefs_calibrate_device (GcmCalibrate *calibrate)
 	if (reference_data == NULL)
 		goto out;
 
-	/* ensure we have data */
-	basename = gcm_prefs_calibrate_get_basename (current_device);
-	if (manufacturer == NULL)
-		manufacturer = g_strdup ("Generic manufacturer");
-	if (model == NULL)
-		model = g_strdup ("Generic model");
-	if (description == NULL)
-		description = g_strdup ("Generic scanner");
+	/* set defaults from device */
+	ret = gcm_calibrate_set_from_device (calibrate, current_device, &error);
+	if (!ret) {
+		egg_warning ("failed to calibrate: %s", error->message);
+		g_error_free (error);
+		goto out;
+	}
 
 	/* use the ORIGINATOR in the it8 file */
 	device = gcm_prefs_get_device_for_it8_file (reference_data);
@@ -837,10 +700,6 @@ gcm_prefs_calibrate_device (GcmCalibrate *calibrate)
 
 	/* set the calibration parameters */
 	g_object_set (calibrate,
-		      "basename", basename,
-		      "model", model,
-		      "description", description,
-		      "manufacturer", manufacturer,
 		      "filename-source", scanned_image,
 		      "filename-reference", reference_data,
 		      "device", device,
@@ -857,10 +716,6 @@ out:
 	if (string != NULL)
 		g_string_free (string, TRUE);
 	g_free (device);
-	g_free (basename);
-	g_free (manufacturer);
-	g_free (model);
-	g_free (description);
 	g_free (scanned_image);
 	g_free (reference_data);
 	return ret;



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