first pass at many/few distinction
[belltower] / belltower.c
index f09b7bf..6787255 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"
 
+/**
+ * Somewhat arbitrary minimum number of belltowers in
+ * one country for the country to be considered to have
+ * "many" belltowers.
+ */
+#define MANY_BELLTOWERS 10
+
 GtkWidget *window;
 LocationGPSDevice *device;
 GKeyFile *static_content;
@@ -359,17 +368,24 @@ get_countries_cb (tower *details,
                  gpointer data)
 {
   GHashTable *hash = (GHashTable *)data;
+  gpointer value;
 
   if (details->serial==0)
     return TRUE; /* header row */
 
   if (!g_hash_table_lookup_extended (hash,
-                                   details->fields[FieldCountry],
-                                    NULL, NULL))
+                                    details->fields[FieldCountry],
+                                    NULL, &value))
     {
       g_hash_table_insert (hash,
                           g_strdup(details->fields[FieldCountry]),
-                          g_strdup (details->fields[FieldCountry]));
+                          GINT_TO_POINTER (0));
+    }
+  else
+    {
+      g_hash_table_replace (hash,
+                           g_strdup(details->fields[FieldCountry]),
+                           GINT_TO_POINTER (GPOINTER_TO_INT (value)+1));
     }
 
   return FILTER_IGNORE;
@@ -462,14 +478,21 @@ 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_bookmarked_towers_cb (tower *details,
+get_group_of_towers_cb (tower *details,
                          gpointer data)
 {
-  if (g_key_file_get_boolean (config,
-                             CONFIG_BOOKMARK_GROUP,
-                             details->fields[FieldPrimaryKey],
-                             NULL))
+  if (g_key_file_has_key (config,
+                         (char*) data,
+                         details->fields[FieldPrimaryKey],
+                         NULL))
     {
       return FILTER_ACCEPT;
     }
@@ -479,6 +502,68 @@ get_bookmarked_towers_cb (tower *details,
     }
 }
 
+/**
+ * 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)
@@ -595,6 +680,13 @@ single_tower_cb (tower *details,
   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;
@@ -729,6 +821,20 @@ show_tower (char *primary_key)
 }
 
 static void
+free_tower_list (GSList *list)
+{
+  GSList *cursor = list;
+
+  while (cursor)
+    {
+      found_tower_free ((FoundTower*) cursor->data);
+      cursor = cursor->next;
+    }
+
+  g_slist_free (list);
+}
+
+static void
 show_towers_from_list (GSList *list)
 {
   GtkWidget *dialog;
@@ -755,7 +861,7 @@ show_towers_from_list (GSList *list)
                                     "One tower found.");
       show_tower (found->primarykey);
 
-      /* FIXME: and free the list */
+      free_tower_list (list);
       return;
     }
 
@@ -792,7 +898,7 @@ show_towers_from_list (GSList *list)
       show_tower (found->primarykey);
     }
 
-  /* FIXME: and free the list */
+  free_tower_list (list);
 }
 
 static gint strcmp_f (gconstpointer a,
@@ -880,23 +986,73 @@ towers_by_subarea (gchar *area)
   gtk_widget_destroy (GTK_WIDGET (dialog));
 }
 
+/**
+ * Maps a hash table from country names to counts of belltowers to a
+ * newly-created hash table mapping country names to display
+ * names, containing only those countries which have many
+ * (or few) belltowers.
+ *
+ * \param source    the source table
+ * \param want_many true if you want countries with many belltowers;
+ *                  false if you want countries with few.
+ */
+static GHashTable*
+get_countries_with_many (GHashTable *source,
+                        gboolean want_many)
+{
+  GHashTable *result = g_hash_table_new_full (g_str_hash,
+                                             g_str_equal,
+                                             g_free,
+                                             NULL);
+  GList *countries = g_hash_table_get_keys (source);
+  GList *cursor = countries;
+
+  while (cursor)
+    {
+      gboolean has_many =
+       GPOINTER_TO_INT (g_hash_table_lookup (source,
+                                             cursor->data)) >= MANY_BELLTOWERS;
+
+      if (has_many == want_many)
+       {
+         g_hash_table_insert (result,
+                              g_strdup (cursor->data),
+                              g_strdup (cursor->data));
+       }
+
+      cursor = cursor->next;
+    }
+
+  g_list_free (countries);
+  return result;
+}
+
+#define COUNTRIES_WITH_MANY "Countries with many belltowers"
+#define COUNTRIES_WITH_FEW "Countries with few belltowers"
+
 static void
-towers_by_area (void)
+towers_by_area_with_many (gboolean countries_with_many)
 {
   GtkWidget *dialog = hildon_picker_dialog_new (GTK_WINDOW (window));
   GtkWidget *selector = hildon_touch_selector_new_text ();
-  GHashTable *hash = g_hash_table_new_full (g_str_hash,
-                                           g_str_equal,
-                                           g_free,
-                                           g_free);
+  GHashTable *countries_to_counts = g_hash_table_new_full (g_str_hash,
+                                                          g_str_equal,
+                                                          g_free,
+                                                          NULL);
+  GHashTable *country_names;
   GSList *list = NULL, *cursor;
   gchar *result = NULL;
 
-  gtk_window_set_title (GTK_WINDOW (dialog), "Areas of the world");
+  gtk_window_set_title (GTK_WINDOW (dialog),
+                       countries_with_many?
+                       COUNTRIES_WITH_MANY : COUNTRIES_WITH_FEW);
 
-  parse_dove (get_countries_cb, NULL, hash);
+  parse_dove (get_countries_cb, NULL, countries_to_counts);
 
-  g_hash_table_foreach (hash,
+  country_names = get_countries_with_many (countries_to_counts,
+                                          countries_with_many);
+
+  g_hash_table_foreach (country_names,
                        put_areas_into_list,
                        &list);
 
@@ -906,6 +1062,12 @@ towers_by_area (void)
                                         cursor->data);
     }
 
+  if (countries_with_many)
+    {
+      hildon_touch_selector_append_text (HILDON_TOUCH_SELECTOR (selector),
+                                        COUNTRIES_WITH_FEW);
+    }
+
   hildon_picker_dialog_set_selector (HILDON_PICKER_DIALOG (dialog),
                                     HILDON_TOUCH_SELECTOR (selector));
 
@@ -915,24 +1077,39 @@ towers_by_area (void)
     {
       result = g_strdup (hildon_touch_selector_get_current_text (HILDON_TOUCH_SELECTOR (selector)));
     }
-  g_hash_table_unref (hash);
+
+  g_hash_table_unref (countries_to_counts);
+  g_hash_table_unref (country_names);
   gtk_widget_destroy (GTK_WIDGET (dialog));
 
   if (result)
     {
-      towers_by_subarea (result);
+      if (strcmp (result, COUNTRIES_WITH_FEW)==0)
+       towers_by_area_with_many (FALSE);
+      else
+       towers_by_subarea (result);
+
       g_free (result);
     }
 }
 
+/**
+ * Shows all the towers in areas with many towers.
+ */
+static void
+towers_by_area (void)
+{
+  towers_by_area_with_many (TRUE);
+}
+
 static void
 show_bookmarks (void)
 {
   GSList *matches = NULL;
 
-  parse_dove (get_bookmarked_towers_cb,
+  parse_dove (get_group_of_towers_cb,
              &matches,
-             NULL);
+             CONFIG_BOOKMARK_GROUP);
 
   show_towers_from_list (matches);
 }
@@ -971,7 +1148,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);
 }
 
 /**