From: Dale Whittaker Date: Wed, 15 Oct 2008 23:31:47 +0000 (+0200) Subject: Implement number of days history is saved X-Git-Url: https://spindle.queued.net/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=49a4b00b71e0a5d62375de0c84ae4a83fd8ca83f;p=midori Implement number of days history is saved Note that this will 'break' any existing history and all dates will suddenly jump the beginning of the unix epoch. --- diff --git a/katze/katze-item.c b/katze/katze-item.c index f2a40d24..3cc437f1 100644 --- a/katze/katze-item.c +++ b/katze/katze-item.c @@ -114,11 +114,13 @@ katze_item_class_init (KatzeItemClass* class) g_object_class_install_property (gobject_class, PROP_ADDED, - g_param_spec_string ( + g_param_spec_int64 ( "added", "Added", "When the item was added", - NULL, + G_MININT64, + G_MAXINT64, + 0, flags)); g_object_class_install_property (gobject_class, @@ -152,7 +154,6 @@ katze_item_finalize (GObject* object) g_free (item->uri); g_free (item->icon); g_free (item->token); - g_free (item->added); G_OBJECT_CLASS (katze_item_parent_class)->finalize (object); } @@ -183,7 +184,7 @@ katze_item_set_property (GObject* object, katze_assign (item->token, g_value_dup_string (value)); break; case PROP_ADDED: - katze_assign (item->added, g_value_dup_string (value)); + item->added = g_value_get_int64 (value); break; case PROP_VISITS: item->visits = g_value_get_int (value); @@ -220,7 +221,7 @@ katze_item_get_property (GObject* object, g_value_set_string (value, item->token); break; case PROP_ADDED: - g_value_set_string (value, item->added); + g_value_set_int64 (value, item->added); break; case PROP_VISITS: g_value_set_int (value, item->visits); @@ -419,10 +420,10 @@ katze_item_set_token (KatzeItem* item, * * Return value: a timestamp **/ -const gchar* +gint64 katze_item_get_added (KatzeItem* item) { - g_return_val_if_fail (KATZE_IS_ITEM (item), NULL); + g_return_val_if_fail (KATZE_IS_ITEM (item), 0); return item->added; } @@ -435,12 +436,12 @@ katze_item_get_added (KatzeItem* item) * Sets when @item was added. **/ void -katze_item_set_added (KatzeItem* item, - const gchar* added) +katze_item_set_added (KatzeItem* item, + gint64 added) { g_return_if_fail (KATZE_IS_ITEM (item)); - katze_assign (item->added, g_strdup (added)); + item->added = added; g_object_notify (G_OBJECT (item), "added"); } diff --git a/katze/katze-item.h b/katze/katze-item.h index 8014b57a..6aceb449 100644 --- a/katze/katze-item.h +++ b/katze/katze-item.h @@ -41,8 +41,8 @@ struct _KatzeItem gchar* uri; gchar* icon; gchar* token; - gchar* added; - gint visits; + gint64 added; + gint visits; KatzeItem* parent; }; @@ -93,12 +93,12 @@ void katze_item_set_token (KatzeItem* item, const gchar* token); -const gchar* +gint64 katze_item_get_added (KatzeItem* item); void katze_item_set_added (KatzeItem* item, - const gchar* added); + gint64 added); gint katze_item_get_visits (KatzeItem* item); diff --git a/midori/main.c b/midori/main.c index 90de8150..1c6e5410 100644 --- a/midori/main.c +++ b/midori/main.c @@ -656,7 +656,7 @@ gettimestr (void* data, g_return_val_if_fail (argc == 1, 1); - katze_item_set_added (item, argv[0]); + katze_item_set_added (item, g_ascii_strtoull (argv[0], NULL, 10)); return 0; } @@ -672,7 +672,7 @@ midori_history_remove_item_cb (KatzeArray* history, g_return_if_fail (KATZE_IS_ITEM (item)); sqlcmd = g_strdup_printf ("DELETE FROM history WHERE uri = '%s' AND" - " title = '%s' AND date = '%s' AND visits = %d", + " title = '%s' AND date = %ld AND visits = %d", katze_item_get_uri (item), katze_item_get_name (item), katze_item_get_added (item), @@ -734,7 +734,7 @@ midori_history_add_item_cb (KatzeArray* array, /* New item, set added to the current date/ time */ if (!katze_item_get_added (item)) { - if (!db_exec_callback (db, "SELECT datetime('now')", + if (!db_exec_callback (db, "SELECT date('now')", gettimestr, item, &error)) { g_printerr (_("Failed to add history item. %s\n"), error->message); @@ -743,7 +743,7 @@ midori_history_add_item_cb (KatzeArray* array, } } sqlcmd = g_strdup_printf ("INSERT INTO history VALUES" - "('%s', '%s', '%s', %d)", + "('%s', '%s', %" G_GUINT64_FORMAT ", %d)", katze_item_get_uri (item), katze_item_get_name (item), katze_item_get_added (item), @@ -767,10 +767,10 @@ midori_history_add_items (void* data, KatzeItem* item; KatzeArray* parent = NULL; KatzeArray* array = KATZE_ARRAY (data); - gchar* newdate; + gint64 date; + time_t newdate; gint i, j, n; gint ncols = 4; - gsize len; g_return_val_if_fail (KATZE_IS_ARRAY (array), 1); @@ -789,28 +789,24 @@ midori_history_add_items (void* data, item = katze_item_new (); katze_item_set_uri (item, argv[i]); katze_item_set_name (item, argv[i + 1]); - katze_item_set_added (item, argv[i + 2]); + date = g_ascii_strtoull (argv[i + 2], NULL, 10); + katze_item_set_added (item, date); katze_item_set_visits (item, atoi (argv[i + 3])); - len = (g_strrstr (argv[i + 2], " ") - argv[i + 2]); - newdate = g_strndup (argv[i + 2], len); - n = katze_array_get_length (array); for (j = 0; j < n; j++) { parent = katze_array_get_nth_item (array, j); - if (newdate && g_ascii_strcasecmp - (katze_item_get_added (KATZE_ITEM (parent)), newdate) == 0) + newdate = katze_item_get_added (KATZE_ITEM (parent)); + if (sokoke_same_day (&date, &newdate)) break; } if (j == n) { parent = katze_array_new (KATZE_TYPE_ARRAY); - katze_item_set_added (KATZE_ITEM (parent), newdate); + katze_item_set_added (KATZE_ITEM (parent), date); katze_array_add_item (array, parent); } - g_free (newdate); - katze_array_add_item (parent, item); } } @@ -832,13 +828,13 @@ midori_history_initialize (KatzeArray* array, if (!db_exec (db, "CREATE TABLE IF NOT EXISTS " - "history(uri text, title text, date text, visits integer)", + "history(uri text, title text, date integer, visits integer)", error)) return NULL; if (!db_exec_callback (db, "SELECT uri, title, date, visits FROM history " - "ORDER BY strftime('%s', date) ASC", + "ORDER BY date ASC", midori_history_add_items, array, error)) @@ -857,6 +853,29 @@ midori_history_initialize (KatzeArray* array, } return db; } + +static void +midori_history_terminate (sqlite3* db, + gint max_history_age) +{ + gchar* sqlcmd; + gboolean success = TRUE; + GError* error = NULL; + + sqlcmd = g_strdup_printf ( + "DELETE FROM history WHERE " + "(julianday(date('now')) - julianday(date(date,'unixepoch')))" + " >= %d", max_history_age); + db_exec (db, sqlcmd, &error); + if (!success) + { + g_printerr (_("Failed to remove old history items. %s\n"), error->message); + g_error_free (error); + return ; + } + g_free (sqlcmd); + db_close (db); +} #endif static gchar* @@ -1060,6 +1079,7 @@ main (int argc, gchar* uri_ready; #ifdef HAVE_SQLITE sqlite3* db; + gint max_history_age; #endif #if ENABLE_NLS @@ -1364,7 +1384,8 @@ main (int argc, g_mkdir_with_parents (config_path, 0755); g_object_unref (history); #ifdef HAVE_SQLITE - db_close (db); + g_object_get (settings, "maximum-history-age", &max_history_age, NULL); + midori_history_terminate (db, max_history_age); #endif config_file = g_build_filename (config_path, "search", NULL); error = NULL; diff --git a/midori/midori-browser.c b/midori/midori-browser.c index 9c96b890..52453d2c 100644 --- a/midori/midori-browser.c +++ b/midori/midori-browser.c @@ -2418,6 +2418,8 @@ midori_browser_history_render_text_cb (GtkTreeViewColumn* column, GtkWidget* treeview) { KatzeItem* item; + time_t date; + char sdate[50]; gtk_tree_model_get (model, iter, 0, &item, -1); @@ -2431,7 +2433,11 @@ midori_browser_history_render_text_cb (GtkTreeViewColumn* column, } if (KATZE_IS_ARRAY (item)) - g_object_set (renderer, "text", katze_item_get_added (item), NULL); + { + date = (time_t)katze_item_get_added (item); + strftime (sdate, sizeof (sdate), "%Y-%m-%d", localtime (&date)); + g_object_set (renderer, "text", sdate, NULL); + } else g_object_set (renderer, "text", katze_item_get_name (item), NULL); @@ -4062,9 +4068,7 @@ midori_browser_new_history_item (MidoriBrowser* browser, gint i; gboolean found; time_t now; - gchar newdate [70]; - gchar *today; - gsize len; + gint64 date; if (!sokoke_object_get_boolean (browser->settings, "remember-last-visited-pages")) return; @@ -4073,19 +4077,15 @@ midori_browser_new_history_item (MidoriBrowser* browser, treemodel = gtk_tree_view_get_model (treeview); now = time (NULL); - strftime (newdate, sizeof (newdate), "%Y-%m-%d %H:%M:%S", localtime (&now)); - katze_item_set_added (item, newdate); - - len = (g_strrstr (newdate, " ") - newdate); - today = g_strndup (newdate, len); + katze_item_set_added (item, now); found = FALSE; i = 0; while (gtk_tree_model_iter_nth_child (treemodel, &iter, NULL, i++)) { gtk_tree_model_get (treemodel, &iter, 0, &parent, -1); - if (g_ascii_strcasecmp (today, - katze_item_get_added (KATZE_ITEM (parent))) == 0) + date = katze_item_get_added (KATZE_ITEM (parent)); + if (sokoke_same_day (&now, &date)) { found = TRUE; break; @@ -4095,7 +4095,7 @@ midori_browser_new_history_item (MidoriBrowser* browser, if (!found) { parent = katze_array_new (KATZE_TYPE_ARRAY); - katze_item_set_added (KATZE_ITEM (parent), today); + katze_item_set_added (KATZE_ITEM (parent), now); katze_array_add_item (browser->history, parent); katze_array_add_item (parent, item); _tree_store_insert_history_item (GTK_TREE_STORE (treemodel), NULL, @@ -4108,7 +4108,6 @@ midori_browser_new_history_item (MidoriBrowser* browser, katze_array_add_item (parent, item); g_object_unref (parent); } - g_free (today); } diff --git a/midori/midori-websettings.c b/midori/midori-websettings.c index 0c91990c..1ff3cb98 100644 --- a/midori/midori-websettings.c +++ b/midori/midori-websettings.c @@ -575,7 +575,7 @@ midori_web_settings_class_init (MidoriWebSettingsClass* class) _("Maximum history age"), _("The maximum number of days to save the history for"), 0, G_MAXINT, 30, - G_PARAM_READABLE)); + flags)); g_object_class_install_property (gobject_class, PROP_REMEMBER_LAST_FORM_INPUTS, diff --git a/midori/sokoke.c b/midori/sokoke.c index 925c7489..6ab9dcae 100644 --- a/midori/sokoke.c +++ b/midori/sokoke.c @@ -648,3 +648,33 @@ sokoke_tree_view_get_selected_iter (GtkTreeView* tree_view, return TRUE; return FALSE; } + +/** + * sokoke_same_day: + * @day1: a time_t timestamp value + * @day2: a time_t timestamp value + * + * Compares two timestamps to see if their values are on the + * same day. + * + * Return value: %TRUE if the day of the timestamps match. + **/ +gboolean +sokoke_same_day (const time_t* day1, + const time_t* day2) +{ + struct tm* tm1; + struct tm* tm2; + gboolean same; + + tm2 = localtime (day1); + tm1 = (struct tm*) g_memdup (tm2, sizeof (struct tm)); + tm2 = localtime (day2); + + same = (tm1->tm_year == tm2->tm_year && + tm1->tm_mon == tm2->tm_mon && + tm1->tm_mday == tm2->tm_mday) ? TRUE : FALSE; + g_free (tm1); + return same; +} + diff --git a/midori/sokoke.h b/midori/sokoke.h index c40ba2bd..39113af5 100644 --- a/midori/sokoke.h +++ b/midori/sokoke.h @@ -139,4 +139,8 @@ sokoke_tree_view_get_selected_iter (GtkTreeView* tree_view, GtkTreeModel** model, GtkTreeIter* iter); +gboolean +sokoke_same_day (const time_t* day1, + const time_t* day2); + #endif /* !__SOKOKE_H__ */