Recent towers list
[belltower] / belltower.c
index 78ed46f..0406988 100644 (file)
 #include <dbus/dbus-glib.h>
 
 #define MAX_FIELDS 50
+#define MAX_RECENT 5
 #define CONFIG_GENERAL_GROUP "General"
+#define CONFIG_BOOKMARK_GROUP "Bookmarks"
+#define CONFIG_RECENT_GROUP "Recent"
 #define CONFIG_SEEN_CREDITS_KEY "seen_credits"
 #define CONFIG_DIRECTORY "/home/user/.config/belltower"
 #define CONFIG_FILENAME CONFIG_DIRECTORY "/belltower.ini"
@@ -286,17 +289,54 @@ add_button (char *label,
 }
 
 
+char *tower_displayed = NULL;
+char *tower_website = NULL;
+char *tower_map = NULL;
+char *tower_directions = NULL;
+char *peals_list = NULL;
+
+#define BUTTON_BOOKMARKED_YES "Remove from bookmarks"
+#define BUTTON_BOOKMARKED_NO "Bookmark"
+
 static void
 bookmark_toggled (GtkButton *button,
                  gpointer dummy)
 {
-  show_message ("Bookmarks are not yet implemented.");
-}
+  if (g_key_file_get_boolean (config,
+                             CONFIG_BOOKMARK_GROUP,
+                             tower_displayed,
+                             NULL))
+    {
 
-char *tower_website = NULL;
-char *tower_map = NULL;
-char *tower_directions = NULL;
-char *peals_list = NULL;
+      /* it's bookmarked; remove the bookmark */
+
+      if (!g_key_file_remove_key (config,
+                                 CONFIG_BOOKMARK_GROUP,
+                                 tower_displayed,
+                                 NULL))
+       {
+         show_message ("Could not remove bookmark.");
+         return;
+       }
+
+      save_config ();
+      gtk_button_set_label (button,
+                           BUTTON_BOOKMARKED_NO);
+    }
+  else
+    {
+      /* it's not bookmarked; add a bookmark */
+
+      g_key_file_set_boolean (config,
+                             CONFIG_BOOKMARK_GROUP,
+                             tower_displayed,
+                             TRUE);
+
+      save_config ();
+      gtk_button_set_label (button,
+                           BUTTON_BOOKMARKED_YES);
+    }
+}
 
 static void
 show_tower_website (void)
@@ -424,6 +464,92 @@ get_towers_by_search_cb (tower *details,
     }
 }
 
+/**
+ * A filter which accepts towers based on whether they
+ * appear in a particular group in the config file.
+ *
+ * \param details  the candidate tower
+ * \param data     pointer to a char* which names the group
+ */
+static FilterResult
+get_group_of_towers_cb (tower *details,
+                         gpointer data)
+{
+  if (g_key_file_has_key (config,
+                         (char*) data,
+                         details->fields[FieldPrimaryKey],
+                         NULL))
+    {
+      return FILTER_ACCEPT;
+    }
+  else
+    {
+      return FILTER_IGNORE;
+    }
+}
+
+/**
+ * Removes the oldest entry from the [Recent] group in the config
+ * file until there are only five entries left.  Does not save
+ * the file; you have to do that.
+ */
+static void
+remove_old_recent_entries (void)
+{
+  gint count;
+
+  do
+    {
+      gchar **towers;
+      gint oldest_date = 0;
+      gchar *oldest_tower = NULL;
+      gint i;
+
+      /* It is a bit inefficient to do this every
+       * time we go around the loop.  However, it
+       * makes the code far simpler, and we almost
+       * never go around more than once.
+       */
+      towers = g_key_file_get_keys (config,
+                                   CONFIG_RECENT_GROUP,
+                                   &count,
+                                   NULL);
+
+      if (count <= MAX_RECENT)
+       /* everything's fine */
+       return;
+
+      for (i=0; i<count; i++)
+       {
+         gint date = g_key_file_get_integer (config,
+                                             CONFIG_RECENT_GROUP,
+                                             towers[i],
+                                             NULL);
+
+         if (date==0)
+           continue;
+
+         if (oldest_date==0 ||
+             date < oldest_date)
+           {
+             oldest_tower = towers[i];
+             oldest_date = date;
+           }
+       }
+
+      if (oldest_tower)
+       {
+         g_key_file_remove_key (config,
+                                CONFIG_RECENT_GROUP,
+                                oldest_tower,
+                                NULL);
+         count --;
+       }
+      g_strfreev (towers);
+    }
+  while (count > MAX_RECENT);
+}
+
 static FilterResult
 single_tower_cb (tower *details,
                 gpointer data)
@@ -511,7 +637,12 @@ single_tower_cb (tower *details,
   /* don't use a toggle button: it looks stupid */
   button = hildon_button_new_with_text (HILDON_SIZE_AUTO_WIDTH | HILDON_SIZE_FINGER_HEIGHT,
                                        HILDON_BUTTON_ARRANGEMENT_VERTICAL,
-                                       "Bookmark", NULL);
+                                       g_key_file_get_boolean (config,
+                                                               CONFIG_BOOKMARK_GROUP,
+                                                               details->fields[FieldPrimaryKey],
+                                                               NULL)?
+                                       BUTTON_BOOKMARKED_YES: BUTTON_BOOKMARKED_NO,
+                                       NULL);
   g_signal_connect (button, "clicked", G_CALLBACK (bookmark_toggled), NULL);
   gtk_box_pack_start (GTK_BOX (buttons), button, FALSE, FALSE, 0);
 
@@ -532,6 +663,16 @@ single_tower_cb (tower *details,
   tower_map = g_strdup_printf ("http://maps.google.com/maps?q=%s,%s",
         details->fields[FieldLat],
         details->fields[FieldLong]);
+  g_free (tower_displayed);
+  tower_displayed = g_strdup (details->fields[FieldPrimaryKey]);
+
+  g_key_file_set_integer (config,
+                         CONFIG_RECENT_GROUP,
+                         tower_displayed,
+                         time (NULL));
+  remove_old_recent_entries ();
+  save_config ();
+
   gtk_widget_show_all (GTK_WIDGET (tower_window));
 
   return FILTER_STOP;
@@ -865,10 +1006,13 @@ towers_by_area (void)
 static void
 show_bookmarks (void)
 {
-  /* well, there are currently no bookmarks,
-     because you can't set them. */
+  GSList *matches = NULL;
+
+  parse_dove (get_group_of_towers_cb,
+             &matches,
+             CONFIG_BOOKMARK_GROUP);
 
-  show_towers_from_list (NULL);
+  show_towers_from_list (matches);
 }
 
 static void
@@ -905,7 +1049,13 @@ tower_search (void)
 static void
 recent_towers (void)
 {
-  show_message ("This is not yet implemented.");
+  GSList *matches = NULL;
+
+  parse_dove (get_group_of_towers_cb,
+             &matches,
+             CONFIG_RECENT_GROUP);
+
+  show_towers_from_list (matches);
 }
 
 /**