[gnome-control-center] color: Adapt to new async colord API



commit e51b037b765da34844de03d9ea6290c4e12e0c98
Author: Richard Hughes <richard hughsie com>
Date:   Mon May 30 22:06:36 2011 +0100

    color: Adapt to new async colord API
    
    The libcolord async API used to use a thread to get the properties for the
    device and profiles in the background.
    
    This was racy as hell, and was not good API design. Connect to each device and
    profiles explicitly before we access the properties.

 panels/color/cc-color-panel.c |  150 ++++++++++++++++++++++++++++++-----------
 1 files changed, 111 insertions(+), 39 deletions(-)
---
diff --git a/panels/color/cc-color-panel.c b/panels/color/cc-color-panel.c
index 24cef75..e7bcd43 100644
--- a/panels/color/cc-color-panel.c
+++ b/panels/color/cc-color-panel.c
@@ -246,7 +246,7 @@ gcm_prefs_device_add_cb (GtkWidget *widget, CcColorPanel *prefs)
 
 static gboolean
 gcm_prefs_is_profile_suitable_for_device (CdProfile *profile,
-            CdDevice *device)
+                                          CdDevice *device)
 {
   CdProfileKind profile_kind_tmp;
   CdProfileKind profile_kind;
@@ -351,6 +351,17 @@ gcm_prefs_add_profiles_suitable_for_devices (CcColorPanel *prefs,
       if (profile != NULL && cd_profile_equal (profile, profile_tmp))
         continue;
 
+      /* get properties */
+      ret = cd_profile_connect_sync (profile_tmp,
+                                     priv->cancellable,
+                                     &error);
+      if (!ret)
+        {
+          g_warning ("failed to get profile: %s", error->message);
+          g_error_free (error);
+          goto out;
+        }
+
       /* only add correct types */
       ret = gcm_prefs_is_profile_suitable_for_device (profile_tmp,
                                                       priv->current_device);
@@ -599,7 +610,7 @@ gcm_prefs_delete_cb (GtkWidget *widget, CcColorPanel *prefs)
 
   /* try to delete device */
   ret = cd_client_delete_device_sync (priv->client,
-                                      cd_device_get_id (priv->current_device),
+                                      priv->current_device,
                                       priv->cancellable,
                                       &error);
   if (!ret)
@@ -627,7 +638,7 @@ gcm_prefs_treeview_renderer_toggled (GtkCellRendererToggle *cell,
 
 static void
 gcm_prefs_add_devices_columns (CcColorPanel *prefs,
-             GtkTreeView *treeview)
+                               GtkTreeView *treeview)
 {
   GtkCellRenderer *renderer;
   GtkTreeViewColumn *column;
@@ -821,9 +832,9 @@ gcm_prefs_profile_clicked (CcColorPanel *prefs, CdProfile *profile, CdDevice *de
 
 
   /* find the profile relationship */
-  relation = cd_device_get_profile_relation (device,
-               profile,
-               NULL, NULL);
+  relation = cd_device_get_profile_relation_sync (device,
+                                                  profile,
+                                                  NULL, NULL);
 
   /* we can only remove hard relationships */
   widget = GTK_WIDGET (gtk_builder_get_object (priv->builder,
@@ -1046,8 +1057,6 @@ out:
     g_object_unref (profile);
 }
 
-
-
 static void
 gcm_prefs_sensor_coldplug (CcColorPanel *prefs)
 {
@@ -1082,8 +1091,8 @@ out:
 
 static void
 gcm_prefs_client_sensor_changed_cb (CdClient *client,
-            CdSensor *sensor,
-            CcColorPanel *prefs)
+                                    CdSensor *sensor,
+                                    CcColorPanel *prefs)
 {
   gcm_prefs_sensor_coldplug (prefs);
   gcm_prefs_set_calibrate_button_sensitivity (prefs);
@@ -1200,14 +1209,28 @@ out:
 }
 
 static gchar *
-gcm_prefs_get_profile_title (CdProfile *profile)
+gcm_prefs_get_profile_title (CcColorPanel *prefs, CdProfile *profile)
 {
   CdColorspace colorspace;
   const gchar *title;
   gchar *string;
+  gboolean ret;
+  GError *error = NULL;
+  CcColorPanelPrivate *priv = prefs->priv;
 
   g_return_val_if_fail (profile != NULL, NULL);
 
+  /* get properties */
+  ret = cd_profile_connect_sync (profile,
+                                 priv->cancellable,
+                                 &error);
+  if (!ret)
+    {
+      g_warning ("failed to get profile: %s", error->message);
+      g_error_free (error);
+      goto out;
+    }
+
   /* add profile description */
   title = cd_profile_get_title (profile);
   if (title != NULL)
@@ -1323,12 +1346,12 @@ out:
 static void
 gcm_prefs_device_set_model_by_iter (CcColorPanel *prefs, CdDevice *device, GtkTreeIter *iter)
 {
-  GString *status;
+  GString *status = NULL;
   const gchar *status_image = NULL;
   const gchar *tooltip = NULL;
   CdProfile *profile = NULL;
   gint age;
-  GPtrArray *profiles;
+  GPtrArray *profiles = NULL;
   CdProfile *profile_tmp;
   guint i;
   gchar *title_tmp;
@@ -1337,6 +1360,8 @@ gcm_prefs_device_set_model_by_iter (CcColorPanel *prefs, CdDevice *device, GtkTr
   GtkTreeIter iter_tmp;
   GtkTreeIter *iter_tmp_p;
   guint threshold = 0;
+  gboolean ret;
+  GError *error = NULL;
   CcColorPanelPrivate *priv = prefs->priv;
 
   /* set status */
@@ -1350,6 +1375,17 @@ gcm_prefs_device_set_model_by_iter (CcColorPanel *prefs, CdDevice *device, GtkTr
       goto skip;
     }
 
+  /* get properties */
+  ret = cd_profile_connect_sync (profile,
+                                 priv->cancellable,
+                                 &error);
+  if (!ret)
+    {
+      g_warning ("failed to get profile: %s", error->message);
+      g_error_free (error);
+      goto out;
+    }
+
   /* autogenerated printer defaults */
   if (cd_device_get_kind (device) == CD_DEVICE_KIND_PRINTER &&
       cd_profile_get_filename (profile) == NULL)
@@ -1415,7 +1451,7 @@ skip:
   for (i = 0; i < profiles->len; i++)
     {
       profile_tmp = g_ptr_array_index (profiles, i);
-      title_tmp = gcm_prefs_get_profile_title (profile_tmp);
+      title_tmp = gcm_prefs_get_profile_title (prefs, profile_tmp);
 
       /* don't show details for EDID profiles */
       if (gcm_prefs_profile_is_based_from_edid (profile_tmp))
@@ -1432,7 +1468,7 @@ skip:
 
       /* get an existing profile, or create a new one */
       iter_tmp_p = get_iter_for_profile (GTK_TREE_MODEL (priv->list_store_devices),
-          profile_tmp, iter);
+                                         profile_tmp, iter);
       if (iter_tmp_p == NULL)
         {
           gtk_tree_store_append (priv->list_store_devices, &iter_tmp, iter);
@@ -1457,7 +1493,8 @@ skip:
   /* remove old profiles that no longer exist */
   gcm_prefs_device_remove_profiles_phase2 (prefs, iter);
 out:
-  g_string_free (status, TRUE);
+  if (status != NULL)
+    g_string_free (status, TRUE);
   if (profiles != NULL)
     g_ptr_array_unref (profiles);
   if (profile != NULL)
@@ -1496,6 +1533,8 @@ gcm_prefs_device_changed_cb (CdDevice *device, CcColorPanel *prefs)
 static void
 gcm_prefs_add_device (CcColorPanel *prefs, CdDevice *device)
 {
+  gboolean ret;
+  GError *error = NULL;
   CdDeviceKind kind;
   const gchar *icon_name;
   const gchar *id;
@@ -1504,6 +1543,15 @@ gcm_prefs_add_device (CcColorPanel *prefs, CdDevice *device)
   GtkTreeIter parent;
   CcColorPanelPrivate *priv = prefs->priv;
 
+  /* get device properties */
+  ret = cd_device_connect_sync (device, priv->cancellable, &error);
+  if (!ret)
+    {
+      g_warning ("failed to connect to the device: %s", error->message);
+      g_error_free (error);
+      goto out;
+    }
+
   /* get icon */
   kind = cd_device_get_kind (device);
   icon_name = gcm_prefs_device_kind_to_icon_name (kind);
@@ -1532,6 +1580,7 @@ gcm_prefs_add_device (CcColorPanel *prefs, CdDevice *device)
                       GCM_PREFS_COLUMN_ICON, icon_name,
                       -1);
   gcm_prefs_device_set_model_by_iter (prefs, device, &parent);
+out:
   g_free (sort);
   g_free (title);
 }
@@ -1541,11 +1590,21 @@ gcm_prefs_remove_device (CcColorPanel *prefs, CdDevice *cd_device)
 {
   GtkTreeIter iter;
   GtkTreeModel *model;
+  GError *error = NULL;
   const gchar *id;
   gchar *id_tmp;
   gboolean ret;
   CcColorPanelPrivate *priv = prefs->priv;
 
+  /* get device properties */
+  ret = cd_device_connect_sync (cd_device, priv->cancellable, &error);
+  if (!ret)
+    {
+      g_warning ("failed to connect to the device: %s", error->message);
+      g_error_free (error);
+      return;
+    }
+
   /* remove */
   id = cd_device_get_id (cd_device);
 
@@ -1559,8 +1618,8 @@ gcm_prefs_remove_device (CcColorPanel *prefs, CdDevice *cd_device)
   do
     {
       gtk_tree_model_get (model, &iter,
-              GCM_PREFS_COLUMN_DEVICE_ID, &id_tmp,
-              -1);
+                          GCM_PREFS_COLUMN_DEVICE_ID, &id_tmp,
+                          -1);
       if (g_strcmp0 (id_tmp, id) == 0)
         {
           gtk_list_store_remove (GTK_LIST_STORE(model), &iter);
@@ -1858,6 +1917,36 @@ gcm_prefs_setup_drag_and_drop (GtkWidget *widget)
 }
 
 static void
+gcm_prefs_connect_cb (GObject *object,
+                      GAsyncResult *res,
+                      gpointer user_data)
+{
+  gboolean ret;
+  GError *error = NULL;
+  CcColorPanel *prefs = CC_COLOR_PANEL (user_data);
+  CcColorPanelPrivate *priv = prefs->priv;
+
+  ret = cd_client_connect_finish (priv->client,
+                                  res,
+                                  &error);
+  if (!ret)
+    {
+      g_warning ("failed to connect to colord: %s", error->message);
+      g_error_free (error);
+      return;
+    }
+
+  /* set calibrate button sensitivity */
+  gcm_prefs_sensor_coldplug (prefs);
+
+  /* get devices */
+  cd_client_get_devices (priv->client,
+                         priv->cancellable,
+                         gcm_prefs_get_devices_cb,
+                         prefs);
+}
+
+static void
 cc_color_panel_get_property (GObject    *object,
                               guint       property_id,
                               GValue     *value,
@@ -1951,8 +2040,6 @@ static void
 cc_color_panel_init (CcColorPanel *prefs)
 {
   CcColorPanelPrivate *priv;
-  gboolean ret;
-  gchar *text = NULL;
   GError *error = NULL;
   GtkStyleContext *context;
   GtkTreeSelection *selection;
@@ -2118,21 +2205,10 @@ cc_color_panel_init (CcColorPanel *prefs)
                     G_CALLBACK (gcm_prefs_changed_cb), prefs);
 
   /* connect to colord */
-  ret = cd_client_connect_sync (priv->client,
-                                priv->cancellable,
-                                &error);
-  if (!ret)
-    {
-      g_warning ("failed to connect to colord: %s", error->message);
-      g_error_free (error);
-      goto out;
-    }
-
-  /* get devices async */
-  cd_client_get_devices (priv->client,
-                         priv->cancellable,
-                         gcm_prefs_get_devices_cb,
-                         prefs);
+  cd_client_connect (priv->client,
+                     priv->cancellable,
+                     gcm_prefs_connect_cb,
+                     prefs);
 
   /* use the color sensor */
   g_signal_connect (priv->client, "sensor-added",
@@ -2142,11 +2218,7 @@ cc_color_panel_init (CcColorPanel *prefs)
                     G_CALLBACK (gcm_prefs_client_sensor_changed_cb),
                     prefs);
 
-out:
-  g_free (text);
-
   /* set calibrate button sensitivity */
-  gcm_prefs_sensor_coldplug (prefs);
   gcm_prefs_set_calibrate_button_sensitivity (prefs);
 
   widget = WID (priv->builder, "dialog-vbox1");



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