[gnome-control-center/gsoc2011/location] simplify dictionnary creation



commit 30cc7383add53457186af51117df4a7aafb07f00
Author: StÃphane Maniaci <stephane maniaci gmail com>
Date:   Fri Jul 15 16:56:26 2011 +0200

    simplify dictionnary creation

 panels/location/cc-location-panel.c |   63 ++++++++++++++---------------
 panels/location/location-utils.c    |   75 ++++++++++++++++++++++------------
 panels/location/location-utils.h    |    2 +
 3 files changed, 80 insertions(+), 60 deletions(-)
---
diff --git a/panels/location/cc-location-panel.c b/panels/location/cc-location-panel.c
index 2921340..01f1102 100644
--- a/panels/location/cc-location-panel.c
+++ b/panels/location/cc-location-panel.c
@@ -61,24 +61,26 @@ static void
 populate_locations (GtkListStore *store,
                     GVariant     *locations)
 {
+  g_debug ("Updating liststore with new locations");
   GtkTreeIter iter;
-  GVariantIter *viter = g_variant_iter_new (locations);
-  GVariant *entry;
+  GVariantIter viter;
+  GVariant *dict;
   GVariant *value;
-  while (g_variant_iter_loop (viter, "v", &entry)) {
-    value = g_variant_lookup_value (entry,
+
+  g_variant_iter_init (&viter, locations);
+  while ((dict = g_variant_iter_next_value (&viter))) {
+    gtk_list_store_append (store, &iter);
+    value = g_variant_lookup_value (dict,
                                     "city",
                                     G_VARIANT_TYPE_STRING);
-    gtk_list_store_append (store, &iter);
-    gtk_list_store_set (store, &iter, 0, g_variant_get_string (value, NULL));
+    gtk_list_store_set (store, &iter, 0, g_variant_get_string (value, NULL), -1);
 
-    value = g_variant_lookup_value (entry,
+    value = g_variant_lookup_value (dict,
                                     "country",
                                     G_VARIANT_TYPE_STRING);
-    gtk_list_store_append (store, &iter);
-    gtk_list_store_set (store, &iter, 1, g_variant_get_string (value, NULL));
+    gtk_list_store_set (store, &iter, 1, g_variant_get_string (value, NULL), -1);
 
-    value = g_variant_lookup_value (entry,
+    value = g_variant_lookup_value (dict,
                                     "timezone",
                                     G_VARIANT_TYPE_INT16);
     int timezone = g_variant_get_int16 (value);
@@ -87,26 +89,21 @@ populate_locations (GtkListStore *store,
       tz = g_strdup_printf ("GMT +%i", timezone);
     else
       tz = g_strdup_printf ("GMT -%i", timezone);
-    gtk_list_store_append (store, &iter);
-    gtk_list_store_set (store, &iter, 2, tz);
+    gtk_list_store_set (store, &iter, 2, tz, -1);
     g_free (tz);
 
-    gtk_list_store_append (store, &iter);
-    gtk_list_store_set (store, &iter, 3, "20:00");
-
-    /*    value = g_variant_lookup_value (entry,
-          "longitude",
-          G_VARIANT_TYPE_DOUBLE);
-          gtk_list_store_append (store, &iter);
-          gtk_list_store_set (store, &iter, 1, g_value_get_double (value));
-
-          value = g_variant_lookup_value (entry,
-          "latitude",
-          G_VARIANT_TYPE_DOUBLE);
-          gtk_list_store_append (store, &iter);
-          gtk_list_store_set (store, &iter, 1, g_value_get_double (value));
-    */
-      }
+    gtk_list_store_set (store, &iter, 3, "20:00", -1);
+
+    value = g_variant_lookup_value (dict,
+                                    "longitude",
+                                    G_VARIANT_TYPE_DOUBLE);
+    //    gtk_list_store_set (store, &iter, 1, g_variant_get_double (value), -1);
+
+    value = g_variant_lookup_value (dict,
+                                    "latitude",
+                                    G_VARIANT_TYPE_DOUBLE);
+    // gtk_list_store_set (store, &iter, 1, g_variant_get_double (value), -1);
+  }
 }
 
 static void
@@ -133,10 +130,10 @@ _on_add_location (GtkToolButton   *bt,
       const double latitude = 0.32;
       GVariant *newloc = g_variant_location_new (city, ctry,
                                                  3, longitude, latitude);
-      g_variant_array_add_value (priv->locations, newloc);
-      g_settings_set_value (priv->location_settings,
-                            "locations",
-                            newloc);
+      priv->locations = g_variant_array_add_value (priv->locations, newloc);
+      g_assert (g_settings_set_value (priv->location_settings,
+                                      "locations",
+                                      priv->locations));
       gtk_list_store_clear (priv->location_store);
       populate_locations (priv->location_store,
                           priv->locations);
@@ -245,7 +242,7 @@ cc_location_panel_init (CcLocationPanel *self)
 
   self->priv->settings = g_settings_new (CLOCK_SCHEMA);
   self->priv->builder  = gtk_builder_new ();
-  self->priv->settings = g_settings_new ("org.gnome.desktop.location");
+  self->priv->location_settings = g_settings_new ("org.gnome.desktop.location");
 
   error = NULL;
   gtk_builder_add_from_file (self->priv->builder,
diff --git a/panels/location/location-utils.c b/panels/location/location-utils.c
index 3794a21..0a93c01 100644
--- a/panels/location/location-utils.c
+++ b/panels/location/location-utils.c
@@ -30,48 +30,69 @@ g_variant_location_new (const char  *city,
 			const double latitude,
 			const double longitude)
 {
+  g_debug ("Creating a new GVariant");
   GVariantBuilder *builder = g_variant_builder_new (G_VARIANT_TYPE_DICTIONARY);
 
-  GVariant *key = g_variant_new_string ("city");
-  GVariant *value = g_variant_new_string (city);
-  GVariant *entry = g_variant_new_dict_entry (key, value);
-  g_variant_builder_add_value (builder, entry);
+  g_variant_builder_add_parsed (builder, "{'city', <%s>}", city);
+  g_variant_builder_add_parsed (builder, "{'country', <%s>}", country);
+  g_variant_builder_add_parsed (builder, "{'timezone', <%n>}", timezone);
+  g_variant_builder_add_parsed (builder, "{'longitude', <%d>}", longitude);
+  g_variant_builder_add_parsed (builder, "{'latitude', <%d>}", latitude);
 
-  key = g_variant_new_string ("country");
-  value = g_variant_new_string (country);
-  entry = g_variant_new_dict_entry (key, value);
-  g_variant_builder_add_value (builder, entry);
-
-  key = g_variant_new_string ("timezone");
-  value = g_variant_new_int16 (timezone);
-  entry = g_variant_new_dict_entry (key, value);
-  g_variant_builder_add_value (builder, entry);
-
-  key = g_variant_new_string ("longitude");
-  value = g_variant_new_double (longitude);
-  entry = g_variant_new_dict_entry (key, value);
-  g_variant_builder_add_value (builder, entry);
-
-  key = g_variant_new_string ("latitude");
-  value = g_variant_new_double (latitude);
-  entry = g_variant_new_dict_entry (key, value);
-  g_variant_builder_add_value (builder, entry);
-
-  return g_variant_builder_end (builder);
+  GVariant *val = g_variant_builder_end (builder);
+  g_variant_location_print (val);
+  return val;
 }
 
 GVariant *
 g_variant_array_add_value (GVariant *container,
                            GVariant *value)
 {
+  g_debug ("Adding value to the locations array");
+
   GVariantBuilder *builder = g_variant_builder_new (G_VARIANT_TYPE_ARRAY);
   GVariantIter iter;
   GVariant *val;
 
   g_variant_iter_init (&iter, container);
-  while (g_variant_iter_loop (&iter, "av", &val))
+  g_debug ("About to loop in the previous locations");
+  while ((val = g_variant_iter_next_value (&iter))) {
+    g_debug ("In the loop\n");
+    g_variant_location_print (val);
     g_variant_builder_add_value (builder, val);
-  g_variant_builder_add_value (builder,value);
+  }
+  g_debug ("Added the previous locations");
+  g_variant_builder_add_value (builder, value);
 
   return g_variant_builder_end (builder);
 }
+
+void
+g_variant_location_print (GVariant *location)
+{
+  g_print("Location:\n");
+  g_print ("\tcity:%s\n"
+           "\tcountry: %s\n"
+           "\ttimezone: %i\n"
+           "\tlong: %f\n"
+           "\tlat: %f\n",
+           g_variant_get_string (g_variant_lookup_value (location,
+                                                         "city",
+                                                         G_VARIANT_TYPE_STRING),
+                                 NULL),
+           g_variant_get_string (g_variant_lookup_value (location,
+                                                         "country",
+                                                         G_VARIANT_TYPE_STRING),
+                                 NULL),
+           g_variant_get_int16 (g_variant_lookup_value (location,
+                                                        "timezone",
+                                                        G_VARIANT_TYPE_INT16)),
+           g_variant_get_double (g_variant_lookup_value (location,
+                                                         "latitude",
+                                                         G_VARIANT_TYPE_DOUBLE)),
+           g_variant_get_double (g_variant_lookup_value (location,
+                                                         "longitude",
+                                                         G_VARIANT_TYPE_DOUBLE))
+           );
+
+}
diff --git a/panels/location/location-utils.h b/panels/location/location-utils.h
index 63fd19a..35e8c32 100644
--- a/panels/location/location-utils.h
+++ b/panels/location/location-utils.h
@@ -36,6 +36,8 @@ GVariant *
 g_variant_array_add_value (GVariant *container,
                            GVariant *value);
 
+void
+g_variant_location_print (GVariant *location);
 G_END_DECLS
 
 #endif /* __LOCATION_UTILS_H */



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