From f1408dfd270bd5af05219d95d99a0990d235df68 Mon Sep 17 00:00:00 2001 From: Thomas Thurman Date: Sun, 30 Aug 2009 15:12:14 -0400 Subject: [PATCH] Recent towers list --- belltower.c | 100 ++++++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 92 insertions(+), 8 deletions(-) diff --git a/belltower.c b/belltower.c index f09b7bf..0406988 100644 --- a/belltower.c +++ b/belltower.c @@ -17,8 +17,10 @@ #include #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" @@ -462,14 +464,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 +488,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 MAX_RECENT); +} + static FilterResult single_tower_cb (tower *details, gpointer data) @@ -595,6 +666,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; @@ -930,9 +1008,9 @@ 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 +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); } /** -- 1.7.9.5