fix memory leak
[belltower] / belltower.c
index bceef99..f571278 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"
 
 GtkWidget *window;
-
 LocationGPSDevice *device;
+GKeyFile *static_content;
+GKeyFile *config;
 
 typedef enum {
   /** stop scanning the database */
@@ -85,10 +93,6 @@ typedef struct {
   int n_fields;
 } tower;
 
-gboolean gps_working = FALSE;
-double current_lat = 40.2452778;
-double current_long = -75.65;;
-
 static void
 show_message (char *message)
 {
@@ -100,6 +104,55 @@ show_message (char *message)
   gtk_widget_destroy (GTK_WIDGET (note));
 }
 
+/**
+ * Loads the content of the static and dynamic data files.
+ * Possibly puts up a warning if we can't load the static file.
+ */
+static void
+load_config (void)
+{
+  static_content = g_key_file_new ();
+
+  if (!g_key_file_load_from_file (static_content,
+                                 "/usr/share/belltower/static.ini",
+                                 G_KEY_FILE_NONE,
+                                 NULL))
+    {
+      show_message ("Could not load static content.  Attempting to continue.");
+    }
+
+  config = g_key_file_new ();
+  /* it doesn't matter if this fails */
+  g_key_file_load_from_file (config,
+                            CONFIG_FILENAME,
+                            G_KEY_FILE_KEEP_COMMENTS,
+                            NULL);
+}
+
+/**
+ * Saves the dynamic data file to disk.
+ * Puts up a message if there was any error.
+ */
+static void
+save_config (void)
+{
+  gchar *data;
+
+  g_mkdir_with_parents (CONFIG_DIRECTORY, 0700);
+
+  data = g_key_file_to_data (config, NULL, NULL);
+
+  if (!g_file_set_contents (CONFIG_FILENAME,
+                           data,
+                           -1,
+                           NULL))
+    {
+      show_message ("Could not write config file.");
+    }
+
+  g_free (data);
+}
+
 static gint
 distance_to_tower (tower *details)
 {
@@ -114,8 +167,8 @@ distance_to_tower (tower *details)
   tower_long = strtod(details->fields[FieldLong], &endptr);
   if (*endptr) return -1;
 
-  km_distance = location_distance_between (current_lat,
-                                          current_long,
+  km_distance = location_distance_between (device->fix->latitude,
+                                          device->fix->longitude,
                                           tower_lat,
                                           tower_long);
 
@@ -236,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)
@@ -374,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)
@@ -450,7 +626,10 @@ single_tower_cb (tower *details,
   add_table_field ("Tenor", str);
   g_free (str);
 
-  add_button ("Tower website", show_tower_website);
+  if (strcmp(details->fields[FieldWebPage], "")!=0)
+    {
+      add_button ("Tower website", show_tower_website);
+    }
   add_button ("Peals", show_peals_list);
   add_button ("Map", show_tower_map);
   add_button ("Directions", NULL);
@@ -458,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);
 
@@ -479,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;
@@ -501,7 +695,7 @@ found_tower_new (tower *basis)
   result->sortkey = g_strdup (basis->fields[FieldPrimaryKey]);
   result->primarykey = g_strdup (basis->fields[FieldPrimaryKey]);
 
-  if (gps_working)
+  if (device->fix->fields & LOCATION_GPS_DEVICE_LATLONG_SET)
     {
       gchar *distance = distance_to_tower_str (basis);
       result->displayname = g_strdup_printf ("%s, %s (%s, %s) (%s)",
@@ -613,6 +807,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;
@@ -639,7 +847,7 @@ show_towers_from_list (GSList *list)
                                     "One tower found.");
       show_tower (found->primarykey);
 
-      /* FIXME: and free the list */
+      free_tower_list (list);
       return;
     }
 
@@ -676,7 +884,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,
@@ -701,7 +909,7 @@ nearby_towers (void)
 {
   GSList *matches = NULL;
 
-  if (!gps_working)
+  if (!(device->fix->fields & LOCATION_GPS_DEVICE_LATLONG_SET))
     {
       show_message ("I don't know where you are!");
       return;
@@ -812,10 +1020,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
@@ -852,19 +1063,96 @@ 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);
 }
 
+/**
+ * Displays a web page.
+ * (Perhaps this should be merged with show_browser().)
+ *
+ * \param url  The URL.
+ */
 static void
-grab_gps (void)
+show_web_page (GtkButton *dummy,
+              gpointer url)
 {
-  gps_working = (device->fix->fields & LOCATION_GPS_DEVICE_LATLONG_SET)!=0;
+  show_browser (url);
+}
 
-  if (gps_working)
+/**
+ * Shows the credits.
+ *
+ * \param source If non-null, we were called from a button press,
+ *               so always show the credits.  If null, we were called
+ *               automatically on startup, so show the credits if
+ *               they haven't already been seen.
+ */
+static void
+show_credits (GtkButton *source,
+             gpointer dummy)
+{
+  gboolean from_button = (source!=NULL);
+  GtkWidget *dialog, *label, *button;
+
+  if (!from_button &&
+      g_key_file_get_boolean (config,
+                             CONFIG_GENERAL_GROUP,
+                             CONFIG_SEEN_CREDITS_KEY,
+                             NULL))
     {
-      current_lat = device->fix->latitude;
-      current_long = device->fix->longitude;
+      return;
     }
+                             
+
+  dialog = gtk_dialog_new_with_buttons ("Credits",
+                                       GTK_WINDOW (window),
+                                       GTK_DIALOG_MODAL|GTK_DIALOG_DESTROY_WITH_PARENT,
+                                       NULL
+                                       );
+
+  button = hildon_button_new_with_text (HILDON_SIZE_AUTO_WIDTH | HILDON_SIZE_FINGER_HEIGHT,
+                                       HILDON_BUTTON_ARRANGEMENT_VERTICAL,
+                                       "View the GNU General Public Licence",
+                                       "This program is provided under the GPL, with no warranty.");
+  g_signal_connect (button, "clicked", G_CALLBACK (show_web_page),
+                   "www.gnu.org/copyleft/gpl.html");
+  gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox),
+                   button,
+                   TRUE, TRUE, 0);
+
+  button = hildon_button_new_with_text (HILDON_SIZE_AUTO_WIDTH | HILDON_SIZE_FINGER_HEIGHT,
+                                       HILDON_BUTTON_ARRANGEMENT_VERTICAL,
+                                       "View Dove's Guide for Church Bell Ringers",
+                                       "The source of this program's data.");
+  g_signal_connect (button, "clicked", G_CALLBACK (show_web_page),
+                   "http://dove.cccbr.org.uk");
+  gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox),
+                   button,
+                   TRUE, TRUE, 0);
+
+  button = hildon_button_new_with_text (HILDON_SIZE_AUTO_WIDTH | HILDON_SIZE_FINGER_HEIGHT,
+                                       HILDON_BUTTON_ARRANGEMENT_VERTICAL,
+                                       "View belfry photograph",
+                                       "Image \xc2\xa9 Amanda Slater, cc-by-sa.");
+  g_signal_connect (button, "clicked", G_CALLBACK (show_web_page),
+                   "http://www.flickr.com/photos/pikerslanefarm/3398769335/");
+  gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox),
+                   button,
+                   TRUE, TRUE, 0);
+  
+  gtk_widget_show_all (GTK_WIDGET (dialog));
+
+  g_key_file_set_boolean (config,
+                         CONFIG_GENERAL_GROUP,
+                         CONFIG_SEEN_CREDITS_KEY,
+                         TRUE);
+  save_config ();
 }
 
 int
@@ -877,8 +1165,6 @@ main(int argc, char **argv)
   g_set_application_name ("Belltower");
 
   device = g_object_new (LOCATION_TYPE_GPS_DEVICE, NULL);
-  grab_gps ();
-  /* FIXME and call that when the location changes, too */
 
   window = hildon_stackable_window_new ();
   gtk_window_set_title (GTK_WINDOW (window), "Belltower");
@@ -897,7 +1183,7 @@ main(int argc, char **argv)
 
   /* extra buttons for the app menu */
   button = gtk_button_new_with_label ("Credits");
-  hildon_app_menu_append (menu, GTK_BUTTON (button));
+  g_signal_connect (button, "clicked", G_CALLBACK (show_credits), NULL);
   hildon_app_menu_append (menu, GTK_BUTTON (button));
 
   gtk_widget_show_all (GTK_WIDGET (menu));
@@ -912,6 +1198,9 @@ main(int argc, char **argv)
   gtk_container_add (GTK_CONTAINER (window), hbox);
   gtk_widget_show_all (GTK_WIDGET (window));
 
+  load_config ();
+  show_credits (NULL, NULL);
+
   gtk_main ();
 
   return EXIT_SUCCESS;