[frogr] Store sorting preferences in a persistent way



commit 30b8faa08b3b33341c606b67e8ee2421b7b14551
Author: Mario Sanchez Prada <msanchez igalia com>
Date:   Mon Apr 25 08:24:08 2011 -0700

    Store sorting preferences in a persistent way

 src/frogr-config.c    |  165 ++++++++++++++++++++++++++++++++++++++++++++-----
 src/frogr-config.h    |   22 +++++++
 src/frogr-main-view.c |   43 +++++++------
 3 files changed, 193 insertions(+), 37 deletions(-)
---
diff --git a/src/frogr-config.c b/src/frogr-config.c
index 255496b..1efe603 100644
--- a/src/frogr-config.c
+++ b/src/frogr-config.c
@@ -60,6 +60,8 @@ struct _FrogrConfigPrivate
 
   gboolean tags_autocompletion;
   gboolean remove_file_extensions;
+  SortingCriteria mainview_sorting_criteria;
+  SortingDirection mainview_sorting_direction;
   gboolean mainview_enable_tooltips;
 
   gboolean use_proxy;
@@ -81,6 +83,10 @@ static void _load_visibility_xml (FrogrConfig *self,
                                   xmlDocPtr     xml,
                                   xmlNodePtr    rootnode);
 
+static void _load_mainview_sorting_pictures_xml (FrogrConfig *self,
+                                                 xmlDocPtr     xml,
+                                                 xmlNodePtr    rootnode);
+
 static void _load_proxy_data_xml (FrogrConfig *self,
                                   xmlDocPtr     xml,
                                   xmlNodePtr    rootnode);
@@ -98,6 +104,14 @@ static gboolean _save_accounts (FrogrConfig *self);
 
 static void _save_account_xml (FrogrAccount *faccount, xmlNodePtr parent);
 
+static xmlNodePtr _xml_add_int_child (xmlNodePtr   parent,
+                                      const gchar *xml_name,
+                                      gint value);
+
+static xmlNodePtr _xml_add_bool_child (xmlNodePtr   parent,
+                                       const gchar *xml_name,
+                                       gboolean value);
+
 static xmlNodePtr _xml_add_string_child (xmlNodePtr   parent,
                                          const gchar *xml_name,
                                          const gchar *content);
@@ -237,6 +251,9 @@ _load_settings (FrogrConfig *self, const gchar *config_dir)
               xmlFree (content);
             }
 
+          if (!xmlStrcmp (node->name, (const xmlChar*) "mainview-sorting-pictures"))
+            _load_mainview_sorting_pictures_xml (self, xml, node);
+
           if (!xmlStrcmp (node->name, (const xmlChar*) "default-visibility"))
             _load_visibility_xml (self, xml, node);
 
@@ -314,6 +331,55 @@ _load_visibility_xml (FrogrConfig *self,
 }
 
 static void
+_load_mainview_sorting_pictures_xml (FrogrConfig *self,
+                                     xmlDocPtr     xml,
+                                     xmlNodePtr    rootnode)
+{
+  FrogrConfigPrivate *priv = NULL;
+  xmlNodePtr node;
+  xmlChar *content = NULL;
+
+  g_return_if_fail (FROGR_IS_CONFIG (self));
+  g_return_if_fail (xml      != NULL);
+  g_return_if_fail (rootnode != NULL);
+
+  priv = FROGR_CONFIG_GET_PRIVATE (self);
+
+  /* Traverse child nodes and extract relevant information. */
+  for (node = rootnode->children; node != NULL; node = node->next)
+    {
+      /* We must initialize this always at the beginning of the loop */
+      content = NULL;
+
+      if (node->type != XML_ELEMENT_NODE)
+        continue;
+
+      if (!xmlStrcmp (node->name, (const xmlChar*) "criteria"))
+        {
+          content = xmlNodeGetContent (node);
+          if (!xmlStrcmp (content, (const xmlChar*) "1"))
+            priv->mainview_sorting_criteria = SORT_BY_TITLE;
+          else if (!xmlStrcmp (content, (const xmlChar*) "2"))
+            priv->mainview_sorting_criteria = SORT_BY_DATE;
+          else
+            priv->mainview_sorting_criteria = SORT_AS_LOADED;
+        }
+
+      if (!xmlStrcmp (node->name, (const xmlChar*) "direction"))
+        {
+          content = xmlNodeGetContent (node);
+          if (!xmlStrcmp (content, (const xmlChar*) "1"))
+            priv->mainview_sorting_direction = SORT_DESCENDING;
+          else
+            priv->mainview_sorting_direction = SORT_ASCENDING;
+        }
+
+      if (content)
+        xmlFree (content);
+    }
+}
+
+static void
 _load_proxy_data_xml (FrogrConfig *self,
                       xmlDocPtr     xml,
                       xmlNodePtr    rootnode)
@@ -528,7 +594,6 @@ _save_settings (FrogrConfig *self)
   xmlNodePtr root = NULL;
   xmlNodePtr node = NULL;
   gchar *xml_path = NULL;
-  gchar *int_string = NULL;
   gboolean retval = TRUE;
 
   g_return_val_if_fail (FROGR_IS_CONFIG (self), FALSE);
@@ -541,29 +606,30 @@ _save_settings (FrogrConfig *self)
 
   /* Default visibility */
   node = xmlNewNode (NULL, (const xmlChar*) "default-visibility");
-  _xml_add_string_child (node, "public", priv->public ? "1" : "0");
-  _xml_add_string_child (node, "family", priv->family ? "1" : "0");
-  _xml_add_string_child (node, "friend", priv->friend ? "1" : "0");
-  _xml_add_string_child (node, "show-in-search", priv->show_in_search ? "1" : "0");
+  _xml_add_bool_child (node, "public", priv->public);
+  _xml_add_bool_child (node, "family", priv->family);
+  _xml_add_bool_child (node, "friend", priv->friend);
+  _xml_add_bool_child (node, "show-in-search", priv->show_in_search);
   xmlAddChild (root, node);
 
   /* Default content type and safety level */
-  int_string = g_strdup_printf ("%d", priv->content_type);
-  _xml_add_string_child (root, "default-content-type", int_string);
-  g_free (int_string);
-
-  int_string = g_strdup_printf ("%d", priv->safety_level);
-  _xml_add_string_child (root, "default-safety-level", int_string);
-  g_free (int_string);
+  _xml_add_int_child (root, "default-content-type", priv->content_type);
+  _xml_add_int_child (root, "default-safety-level", priv->safety_level);
 
   /* Other stuff */
-  _xml_add_string_child (root, "tags-autocompletion", priv->tags_autocompletion ? "1" : "0");
-  _xml_add_string_child (root, "remove-file-extensions", priv->remove_file_extensions ? "1" : "0");
-  _xml_add_string_child (root, "mainview-enable-tooltips", priv->mainview_enable_tooltips ? "1" : "0");
+  _xml_add_bool_child (root, "tags-autocompletion", priv->tags_autocompletion);
+  _xml_add_bool_child (root, "remove-file-extensions", priv->remove_file_extensions);
+  _xml_add_bool_child (root, "mainview-enable-tooltips", priv->mainview_enable_tooltips);
+
+  node = xmlNewNode (NULL, (const xmlChar*) "mainview-sorting-pictures");
+  _xml_add_int_child (node, "criteria", priv->mainview_sorting_criteria);
+  _xml_add_int_child (node, "direction", priv->mainview_sorting_direction);
+  xmlAddChild (root, node);
+
 
   /* Use proxy */
   node = xmlNewNode (NULL, (const xmlChar*) "http-proxy");
-  _xml_add_string_child (node, "use-proxy", priv->use_proxy ? "1" : "0");
+  _xml_add_bool_child (node, "use-proxy", priv->use_proxy);
   _xml_add_string_child (node, "proxy-host", priv->proxy_host);
   _xml_add_string_child (node, "proxy-port", priv->proxy_port);
   _xml_add_string_child (node, "proxy-username", priv->proxy_username);
@@ -643,6 +709,33 @@ _save_account_xml (FrogrAccount *faccount, xmlNodePtr parent)
   xmlAddChild (parent, node);
 }
 
+
+static xmlNodePtr
+_xml_add_int_child (xmlNodePtr parent, const gchar *xml_name, gint value)
+{
+  xmlNodePtr result = NULL;
+  gchar *int_str = NULL;
+
+  int_str = g_strdup_printf ("%d", value);
+  result = _xml_add_string_child (parent, xml_name, int_str);
+  g_free (int_str);
+
+  return result;
+}
+
+static xmlNodePtr
+_xml_add_bool_child (xmlNodePtr parent, const gchar *xml_name, gboolean value)
+{
+  xmlNodePtr result = NULL;
+  gchar *bool_str = NULL;
+
+  bool_str = g_strdup_printf ("%d", value ? 1 : 0);
+  result = _xml_add_string_child (parent, xml_name, bool_str);
+  g_free (bool_str);
+
+  return result;
+}
+
 static xmlNodePtr
 _xml_add_string_child (xmlNodePtr   parent,
                        const gchar *xml_name,
@@ -785,6 +878,8 @@ frogr_config_init (FrogrConfig *self)
   priv->content_type = FSP_CONTENT_TYPE_PHOTO;
   priv->tags_autocompletion = TRUE;
   priv->remove_file_extensions = TRUE;
+  priv->mainview_sorting_criteria = SORT_AS_LOADED;
+  priv->mainview_sorting_direction = SORT_ASCENDING;
   priv->mainview_enable_tooltips = TRUE;
   priv->use_proxy = FALSE;
   priv->proxy_host = NULL;
@@ -1103,6 +1198,44 @@ frogr_config_get_mainview_enable_tooltips (FrogrConfig *self)
 }
 
 void
+frogr_config_set_mainview_sorting_criteria (FrogrConfig *self,
+                                            SortingCriteria criteria)
+{
+  g_return_if_fail (FROGR_IS_CONFIG (self));
+
+  FrogrConfigPrivate * priv = FROGR_CONFIG_GET_PRIVATE (self);
+  priv->mainview_sorting_criteria = criteria;
+}
+
+SortingCriteria
+frogr_config_get_mainview_sorting_criteria (FrogrConfig *self)
+{
+  g_return_val_if_fail (FROGR_IS_CONFIG (self), SORT_AS_LOADED);
+
+  FrogrConfigPrivate * priv = FROGR_CONFIG_GET_PRIVATE (self);
+  return priv->mainview_sorting_criteria;
+}
+
+void
+frogr_config_set_mainview_sorting_direction (FrogrConfig *self,
+                                            SortingDirection direction)
+{
+  g_return_if_fail (FROGR_IS_CONFIG (self));
+
+  FrogrConfigPrivate * priv = FROGR_CONFIG_GET_PRIVATE (self);
+  priv->mainview_sorting_direction = direction;
+}
+
+SortingDirection
+frogr_config_get_mainview_sorting_direction (FrogrConfig *self)
+{
+  g_return_val_if_fail (FROGR_IS_CONFIG (self), SORT_AS_LOADED);
+
+  FrogrConfigPrivate * priv = FROGR_CONFIG_GET_PRIVATE (self);
+  return priv->mainview_sorting_direction;
+}
+
+void
 frogr_config_set_use_proxy (FrogrConfig *self, gboolean value)
 {
   g_return_if_fail (FROGR_IS_CONFIG (self));
diff --git a/src/frogr-config.h b/src/frogr-config.h
index c3cc691..aa84665 100644
--- a/src/frogr-config.h
+++ b/src/frogr-config.h
@@ -57,6 +57,17 @@ struct _FrogrConfigClass
 
 GType frogr_config_get_type (void) G_GNUC_CONST;
 
+typedef enum {
+  SORT_AS_LOADED,
+  SORT_BY_TITLE,
+  SORT_BY_DATE
+} SortingCriteria;
+
+typedef enum {
+  SORT_ASCENDING,
+  SORT_DESCENDING,
+} SortingDirection;
+
 FrogrConfig* frogr_config_get_instance (void);
 
 gboolean frogr_config_save_all (FrogrConfig *self);
@@ -114,6 +125,17 @@ void frogr_config_set_mainview_enable_tooltips (FrogrConfig *self, gboolean valu
 
 gboolean frogr_config_get_mainview_enable_tooltips (FrogrConfig *self);
 
+void frogr_config_set_mainview_sorting_criteria (FrogrConfig *self,
+                                                 SortingCriteria criteria);
+
+SortingCriteria frogr_config_get_mainview_sorting_criteria (FrogrConfig *self);
+
+
+void frogr_config_set_mainview_sorting_direction (FrogrConfig *self,
+                                                  SortingDirection direction);
+
+SortingDirection frogr_config_get_mainview_sorting_direction (FrogrConfig *self);
+
 void frogr_config_set_use_proxy (FrogrConfig *self, gboolean value);
 
 gboolean frogr_config_get_use_proxy (FrogrConfig *self);
diff --git a/src/frogr-main-view.c b/src/frogr-main-view.c
index 7e99352..29ab767 100644
--- a/src/frogr-main-view.c
+++ b/src/frogr-main-view.c
@@ -65,17 +65,6 @@ G_DEFINE_TYPE (FrogrMainView, frogr_main_view, G_TYPE_OBJECT)
 
 /* Private Data */
 
-typedef enum {
-  SORT_AS_LOADED,
-  SORT_BY_TITLE,
-  SORT_BY_DATE
-} SortingCriteria;
-
-typedef enum {
-  SORT_ASCENDING,
-  SORT_DESCENDING,
-} SortingDirection;
-
 typedef struct _FrogrMainViewPrivate {
   FrogrMainViewModel *model;
   FrogrController *controller;
@@ -433,6 +422,13 @@ _populate_menu_bar (FrogrMainView *self)
                     self);
   priv->sort_by_date_menu_item = menu_item;
 
+  if (priv->sorting_criteria == SORT_BY_TITLE)
+    gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (priv->sort_by_title_menu_item), TRUE);
+  else if (priv->sorting_criteria == SORT_BY_DATE)
+    gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (priv->sort_by_date_menu_item), TRUE);
+  else
+    gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (priv->sort_as_loaded_menu_item), TRUE);
+
   gtk_menu_shell_append (GTK_MENU_SHELL (submenu), gtk_separator_menu_item_new ());
 
   menu_item = gtk_check_menu_item_new_with_mnemonic (_("Reversed order"));
@@ -442,7 +438,8 @@ _populate_menu_bar (FrogrMainView *self)
                     self);
   priv->sort_reversed_menu_item = menu_item;
 
-  gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (priv->sort_as_loaded_menu_item), TRUE);
+  if (priv->sorting_direction == SORT_DESCENDING)
+    gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (menu_item), TRUE);
 
   gtk_menu_shell_append (GTK_MENU_SHELL (menu), gtk_separator_menu_item_new ());
 
@@ -866,19 +863,23 @@ _on_check_menu_item_toggled (GtkCheckMenuItem *item, gpointer self)
     }
   else if (GTK_WIDGET (item) == priv->sort_reversed_menu_item)
     {
-      _reorder_pictures (mainview, priv->sorting_criteria,
-                         checked ? SORT_DESCENDING : SORT_ASCENDING);
+      SortingDirection direction = checked ? SORT_DESCENDING : SORT_ASCENDING;
+      _reorder_pictures (mainview, priv->sorting_criteria, direction);
+      frogr_config_set_mainview_sorting_direction (priv->config, direction);
     }
   else if (checked)
     {
       /* Radio buttons handling here (only care about 'em when checked) */
 
-      if (GTK_WIDGET (item) == priv->sort_as_loaded_menu_item)
-        _reorder_pictures (mainview, SORT_AS_LOADED, priv->sorting_direction);
-      else if (GTK_WIDGET (item) == priv->sort_by_title_menu_item)
-        _reorder_pictures (mainview, SORT_BY_TITLE, priv->sorting_direction);
+      SortingCriteria criteria = SORT_AS_LOADED;
+
+      if (GTK_WIDGET (item) == priv->sort_by_title_menu_item)
+        criteria = SORT_BY_TITLE;
       else if (GTK_WIDGET (item) == priv->sort_by_date_menu_item)
-        _reorder_pictures (mainview, SORT_BY_DATE, priv->sorting_direction);
+        criteria = SORT_BY_DATE;
+
+      _reorder_pictures (mainview, criteria, priv->sorting_direction);
+      frogr_config_set_mainview_sorting_criteria (priv->config, criteria);
     }
 
   /* State for check menu items should be immediately stored */
@@ -1791,8 +1792,8 @@ frogr_main_view_init (FrogrMainView *self)
   priv->upload_button = upload_button;
 
   /* Initialize sorting criteria and direction */
-  priv->sorting_criteria = SORT_AS_LOADED;
-  priv->sorting_direction = SORT_ASCENDING;
+  priv->sorting_criteria = frogr_config_get_mainview_sorting_criteria (priv->config);
+  priv->sorting_direction = frogr_config_get_mainview_sorting_direction (priv->config);
 
   /* Read value for 'tooltips enabled' */
   priv->tooltips_enabled = frogr_config_get_mainview_enable_tooltips (priv->config);



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