[gnome-control-center/gsoc2011/location] Add some helper functions to deal with GVariant arrays.



commit d97962722aef22aaf8be1bc85a28df841869b053
Author: StÃphane Maniaci <stephane maniaci gmail com>
Date:   Thu Jul 14 23:03:05 2011 +0200

    Add some helper functions to deal with GVariant arrays.

 panels/location/Makefile.am         |    2 +
 panels/location/cc-location-panel.c |   87 ++++++++++++++++++++++++++++++----
 panels/location/location-utils.c    |   77 +++++++++++++++++++++++++++++++
 panels/location/location-utils.h    |   41 ++++++++++++++++
 4 files changed, 196 insertions(+), 11 deletions(-)
---
diff --git a/panels/location/Makefile.am b/panels/location/Makefile.am
index 652de50..9988266 100644
--- a/panels/location/Makefile.am
+++ b/panels/location/Makefile.am
@@ -13,6 +13,8 @@ ccpanels_LTLIBRARIES = liblocation.la
 
 liblocation_la_SOURCES =    \
 	location-module.c   \
+	location-utils.h    \
+	location-utils.c    \
 	cc-location-panel.c \
 	cc-location-panel.h 
 
diff --git a/panels/location/cc-location-panel.c b/panels/location/cc-location-panel.c
index 2958e3d..2921340 100644
--- a/panels/location/cc-location-panel.c
+++ b/panels/location/cc-location-panel.c
@@ -32,7 +32,7 @@
 */
 
 #include "cc-location-panel.h"
-
+#include "location-utils.h"
 #include <gdesktop-enums.h>
 
 #define CLOCK_SCHEMA "org.gnome.desktop.interface"
@@ -48,7 +48,9 @@ G_DEFINE_DYNAMIC_TYPE (CcLocationPanel, cc_location_panel, CC_TYPE_PANEL)
 struct _CcLocationPanelPrivate
 {
   GtkBuilder *builder;
-  GSettings *settings;
+  GSettings  *settings;
+  GSettings  *location_settings;
+  GVariant   *locations;
   GDesktopClockFormat clock_format;
 
   GtkListStore *location_store;
@@ -56,6 +58,58 @@ struct _CcLocationPanelPrivate
 };
 
 static void
+populate_locations (GtkListStore *store,
+                    GVariant     *locations)
+{
+  GtkTreeIter iter;
+  GVariantIter *viter = g_variant_iter_new (locations);
+  GVariant *entry;
+  GVariant *value;
+  while (g_variant_iter_loop (viter, "v", &entry)) {
+    value = g_variant_lookup_value (entry,
+                                    "city",
+                                    G_VARIANT_TYPE_STRING);
+    gtk_list_store_append (store, &iter);
+    gtk_list_store_set (store, &iter, 0, g_variant_get_string (value, NULL));
+
+    value = g_variant_lookup_value (entry,
+                                    "country",
+                                    G_VARIANT_TYPE_STRING);
+    gtk_list_store_append (store, &iter);
+    gtk_list_store_set (store, &iter, 1, g_variant_get_string (value, NULL));
+
+    value = g_variant_lookup_value (entry,
+                                    "timezone",
+                                    G_VARIANT_TYPE_INT16);
+    int timezone = g_variant_get_int16 (value);
+    char *tz;
+    if (timezone > 0)
+      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);
+    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));
+    */
+      }
+}
+
+static void
 _on_add_location (GtkToolButton   *bt,
                   CcLocationPanel *self)
 {
@@ -68,17 +122,24 @@ _on_add_location (GtkToolButton   *bt,
   GtkWidget *dialog = WID ("add-location-dialog");
   int res = gtk_dialog_run (GTK_DIALOG (dialog));
 
-  GtkTreeIter iter;
   switch (res) {
     case GTK_RESPONSE_OK:
+      printf ("s");
       // FIXME: column ids
-      gtk_list_store_append (priv->location_store, &iter);
-      gtk_list_store_set (priv->location_store, &iter,
-                          0, gtk_entry_get_text (GTK_ENTRY (WID ("city-entry"))),
-                          1, gtk_entry_get_text (GTK_ENTRY (WID ("country-entry"))),
-                          3, gtk_entry_get_text (GTK_ENTRY (WID ("tz-entry"))),
-                          2, "20:00",
-                          -1);
+      const char *city = gtk_entry_get_text (GTK_ENTRY (WID ("city-entry")));
+      const char *ctry = gtk_entry_get_text (GTK_ENTRY (WID ("country-entry")));
+      //const char *tz = gtk_entry_get_text (GTK_ENTRY (WID ("tz-entry")));
+      const double longitude = 0.23;
+      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);
+      gtk_list_store_clear (priv->location_store);
+      populate_locations (priv->location_store,
+                          priv->locations);
       break;
     default:
       break;
@@ -183,7 +244,8 @@ cc_location_panel_init (CcLocationPanel *self)
   self->priv = LOCATION_PANEL_PRIVATE (self);
 
   self->priv->settings = g_settings_new (CLOCK_SCHEMA);
-  self->priv->builder = gtk_builder_new ();
+  self->priv->builder  = gtk_builder_new ();
+  self->priv->settings = g_settings_new ("org.gnome.desktop.location");
 
   error = NULL;
   gtk_builder_add_from_file (self->priv->builder,
@@ -224,6 +286,9 @@ cc_location_panel_init (CcLocationPanel *self)
   widget = WID ("location-vbox");
   gtk_widget_reparent (widget, (GtkWidget *) self);
 
+  self->priv->locations = g_settings_get_value (self->priv->location_settings,
+                                                "locations");
+  populate_locations (self->priv->location_store, self->priv->locations);
 }
 
 void
diff --git a/panels/location/location-utils.c b/panels/location/location-utils.c
new file mode 100644
index 0000000..3794a21
--- /dev/null
+++ b/panels/location/location-utils.c
@@ -0,0 +1,77 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
+ *
+ * Copyright 2009-2010  Red Hat, Inc,
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ */
+
+#include "location-utils.h"
+#include "config.h"
+
+#include <glib.h>
+
+GVariant *
+g_variant_location_new (const char  *city,
+			const char  *country,
+			const int    timezone,
+			const double latitude,
+			const double longitude)
+{
+  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);
+
+  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 *
+g_variant_array_add_value (GVariant *container,
+                           GVariant *value)
+{
+  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_variant_builder_add_value (builder, val);
+  g_variant_builder_add_value (builder,value);
+
+  return g_variant_builder_end (builder);
+}
diff --git a/panels/location/location-utils.h b/panels/location/location-utils.h
new file mode 100644
index 0000000..63fd19a
--- /dev/null
+++ b/panels/location/location-utils.h
@@ -0,0 +1,41 @@
+/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
+ *
+ * Copyright 2009-2010  Red Hat, Inc,
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ */
+
+#ifndef __LOCATION_UTILS_H__
+#define __LOCATION_UTILS_H__
+
+#include <glib.h>
+
+G_BEGIN_DECLS
+
+GVariant *
+g_variant_location_new (const char  *city,
+			const char  *country,
+			const int    timezone,
+			const double latitude,
+			const double longitude);
+
+GVariant *
+g_variant_array_add_value (GVariant *container,
+                           GVariant *value);
+
+G_END_DECLS
+
+#endif /* __LOCATION_UTILS_H */



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