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.
This commit is contained in:
Dale Whittaker 2008-10-16 01:31:47 +02:00 committed by Christian Dywan
parent ae14651145
commit 49a4b00b71
7 changed files with 101 additions and 46 deletions

View file

@ -114,11 +114,13 @@ katze_item_class_init (KatzeItemClass* class)
g_object_class_install_property (gobject_class, g_object_class_install_property (gobject_class,
PROP_ADDED, PROP_ADDED,
g_param_spec_string ( g_param_spec_int64 (
"added", "added",
"Added", "Added",
"When the item was added", "When the item was added",
NULL, G_MININT64,
G_MAXINT64,
0,
flags)); flags));
g_object_class_install_property (gobject_class, g_object_class_install_property (gobject_class,
@ -152,7 +154,6 @@ katze_item_finalize (GObject* object)
g_free (item->uri); g_free (item->uri);
g_free (item->icon); g_free (item->icon);
g_free (item->token); g_free (item->token);
g_free (item->added);
G_OBJECT_CLASS (katze_item_parent_class)->finalize (object); 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)); katze_assign (item->token, g_value_dup_string (value));
break; break;
case PROP_ADDED: case PROP_ADDED:
katze_assign (item->added, g_value_dup_string (value)); item->added = g_value_get_int64 (value);
break; break;
case PROP_VISITS: case PROP_VISITS:
item->visits = g_value_get_int (value); item->visits = g_value_get_int (value);
@ -220,7 +221,7 @@ katze_item_get_property (GObject* object,
g_value_set_string (value, item->token); g_value_set_string (value, item->token);
break; break;
case PROP_ADDED: case PROP_ADDED:
g_value_set_string (value, item->added); g_value_set_int64 (value, item->added);
break; break;
case PROP_VISITS: case PROP_VISITS:
g_value_set_int (value, item->visits); g_value_set_int (value, item->visits);
@ -419,10 +420,10 @@ katze_item_set_token (KatzeItem* item,
* *
* Return value: a timestamp * Return value: a timestamp
**/ **/
const gchar* gint64
katze_item_get_added (KatzeItem* item) 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; return item->added;
} }
@ -435,12 +436,12 @@ katze_item_get_added (KatzeItem* item)
* Sets when @item was added. * Sets when @item was added.
**/ **/
void void
katze_item_set_added (KatzeItem* item, katze_item_set_added (KatzeItem* item,
const gchar* added) gint64 added)
{ {
g_return_if_fail (KATZE_IS_ITEM (item)); 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"); g_object_notify (G_OBJECT (item), "added");
} }

View file

@ -41,8 +41,8 @@ struct _KatzeItem
gchar* uri; gchar* uri;
gchar* icon; gchar* icon;
gchar* token; gchar* token;
gchar* added; gint64 added;
gint visits; gint visits;
KatzeItem* parent; KatzeItem* parent;
}; };
@ -93,12 +93,12 @@ void
katze_item_set_token (KatzeItem* item, katze_item_set_token (KatzeItem* item,
const gchar* token); const gchar* token);
const gchar* gint64
katze_item_get_added (KatzeItem* item); katze_item_get_added (KatzeItem* item);
void void
katze_item_set_added (KatzeItem* item, katze_item_set_added (KatzeItem* item,
const gchar* added); gint64 added);
gint gint
katze_item_get_visits (KatzeItem* item); katze_item_get_visits (KatzeItem* item);

View file

@ -656,7 +656,7 @@ gettimestr (void* data,
g_return_val_if_fail (argc == 1, 1); 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; return 0;
} }
@ -672,7 +672,7 @@ midori_history_remove_item_cb (KatzeArray* history,
g_return_if_fail (KATZE_IS_ITEM (item)); g_return_if_fail (KATZE_IS_ITEM (item));
sqlcmd = g_strdup_printf ("DELETE FROM history WHERE uri = '%s' AND" 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_uri (item),
katze_item_get_name (item), katze_item_get_name (item),
katze_item_get_added (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 */ /* New item, set added to the current date/ time */
if (!katze_item_get_added (item)) 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)) gettimestr, item, &error))
{ {
g_printerr (_("Failed to add history item. %s\n"), error->message); 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" 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_uri (item),
katze_item_get_name (item), katze_item_get_name (item),
katze_item_get_added (item), katze_item_get_added (item),
@ -767,10 +767,10 @@ midori_history_add_items (void* data,
KatzeItem* item; KatzeItem* item;
KatzeArray* parent = NULL; KatzeArray* parent = NULL;
KatzeArray* array = KATZE_ARRAY (data); KatzeArray* array = KATZE_ARRAY (data);
gchar* newdate; gint64 date;
time_t newdate;
gint i, j, n; gint i, j, n;
gint ncols = 4; gint ncols = 4;
gsize len;
g_return_val_if_fail (KATZE_IS_ARRAY (array), 1); g_return_val_if_fail (KATZE_IS_ARRAY (array), 1);
@ -789,28 +789,24 @@ midori_history_add_items (void* data,
item = katze_item_new (); item = katze_item_new ();
katze_item_set_uri (item, argv[i]); katze_item_set_uri (item, argv[i]);
katze_item_set_name (item, argv[i + 1]); 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])); 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); n = katze_array_get_length (array);
for (j = 0; j < n; j++) for (j = 0; j < n; j++)
{ {
parent = katze_array_get_nth_item (array, j); parent = katze_array_get_nth_item (array, j);
if (newdate && g_ascii_strcasecmp newdate = katze_item_get_added (KATZE_ITEM (parent));
(katze_item_get_added (KATZE_ITEM (parent)), newdate) == 0) if (sokoke_same_day (&date, &newdate))
break; break;
} }
if (j == n) if (j == n)
{ {
parent = katze_array_new (KATZE_TYPE_ARRAY); 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); katze_array_add_item (array, parent);
} }
g_free (newdate);
katze_array_add_item (parent, item); katze_array_add_item (parent, item);
} }
} }
@ -832,13 +828,13 @@ midori_history_initialize (KatzeArray* array,
if (!db_exec (db, if (!db_exec (db,
"CREATE TABLE IF NOT EXISTS " "CREATE TABLE IF NOT EXISTS "
"history(uri text, title text, date text, visits integer)", "history(uri text, title text, date integer, visits integer)",
error)) error))
return NULL; return NULL;
if (!db_exec_callback (db, if (!db_exec_callback (db,
"SELECT uri, title, date, visits FROM history " "SELECT uri, title, date, visits FROM history "
"ORDER BY strftime('%s', date) ASC", "ORDER BY date ASC",
midori_history_add_items, midori_history_add_items,
array, array,
error)) error))
@ -857,6 +853,29 @@ midori_history_initialize (KatzeArray* array,
} }
return db; 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 #endif
static gchar* static gchar*
@ -1060,6 +1079,7 @@ main (int argc,
gchar* uri_ready; gchar* uri_ready;
#ifdef HAVE_SQLITE #ifdef HAVE_SQLITE
sqlite3* db; sqlite3* db;
gint max_history_age;
#endif #endif
#if ENABLE_NLS #if ENABLE_NLS
@ -1364,7 +1384,8 @@ main (int argc,
g_mkdir_with_parents (config_path, 0755); g_mkdir_with_parents (config_path, 0755);
g_object_unref (history); g_object_unref (history);
#ifdef HAVE_SQLITE #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 #endif
config_file = g_build_filename (config_path, "search", NULL); config_file = g_build_filename (config_path, "search", NULL);
error = NULL; error = NULL;

View file

@ -2418,6 +2418,8 @@ midori_browser_history_render_text_cb (GtkTreeViewColumn* column,
GtkWidget* treeview) GtkWidget* treeview)
{ {
KatzeItem* item; KatzeItem* item;
time_t date;
char sdate[50];
gtk_tree_model_get (model, iter, 0, &item, -1); 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)) 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 else
g_object_set (renderer, "text", katze_item_get_name (item), NULL); g_object_set (renderer, "text", katze_item_get_name (item), NULL);
@ -4062,9 +4068,7 @@ midori_browser_new_history_item (MidoriBrowser* browser,
gint i; gint i;
gboolean found; gboolean found;
time_t now; time_t now;
gchar newdate [70]; gint64 date;
gchar *today;
gsize len;
if (!sokoke_object_get_boolean (browser->settings, "remember-last-visited-pages")) if (!sokoke_object_get_boolean (browser->settings, "remember-last-visited-pages"))
return; return;
@ -4073,19 +4077,15 @@ midori_browser_new_history_item (MidoriBrowser* browser,
treemodel = gtk_tree_view_get_model (treeview); treemodel = gtk_tree_view_get_model (treeview);
now = time (NULL); now = time (NULL);
strftime (newdate, sizeof (newdate), "%Y-%m-%d %H:%M:%S", localtime (&now)); katze_item_set_added (item, now);
katze_item_set_added (item, newdate);
len = (g_strrstr (newdate, " ") - newdate);
today = g_strndup (newdate, len);
found = FALSE; found = FALSE;
i = 0; i = 0;
while (gtk_tree_model_iter_nth_child (treemodel, &iter, NULL, i++)) while (gtk_tree_model_iter_nth_child (treemodel, &iter, NULL, i++))
{ {
gtk_tree_model_get (treemodel, &iter, 0, &parent, -1); gtk_tree_model_get (treemodel, &iter, 0, &parent, -1);
if (g_ascii_strcasecmp (today, date = katze_item_get_added (KATZE_ITEM (parent));
katze_item_get_added (KATZE_ITEM (parent))) == 0) if (sokoke_same_day (&now, &date))
{ {
found = TRUE; found = TRUE;
break; break;
@ -4095,7 +4095,7 @@ midori_browser_new_history_item (MidoriBrowser* browser,
if (!found) if (!found)
{ {
parent = katze_array_new (KATZE_TYPE_ARRAY); 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 (browser->history, parent);
katze_array_add_item (parent, item); katze_array_add_item (parent, item);
_tree_store_insert_history_item (GTK_TREE_STORE (treemodel), NULL, _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); katze_array_add_item (parent, item);
g_object_unref (parent); g_object_unref (parent);
} }
g_free (today);
} }

View file

@ -575,7 +575,7 @@ midori_web_settings_class_init (MidoriWebSettingsClass* class)
_("Maximum history age"), _("Maximum history age"),
_("The maximum number of days to save the history for"), _("The maximum number of days to save the history for"),
0, G_MAXINT, 30, 0, G_MAXINT, 30,
G_PARAM_READABLE)); flags));
g_object_class_install_property (gobject_class, g_object_class_install_property (gobject_class,
PROP_REMEMBER_LAST_FORM_INPUTS, PROP_REMEMBER_LAST_FORM_INPUTS,

View file

@ -648,3 +648,33 @@ sokoke_tree_view_get_selected_iter (GtkTreeView* tree_view,
return TRUE; return TRUE;
return FALSE; 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;
}

View file

@ -139,4 +139,8 @@ sokoke_tree_view_get_selected_iter (GtkTreeView* tree_view,
GtkTreeModel** model, GtkTreeModel** model,
GtkTreeIter* iter); GtkTreeIter* iter);
gboolean
sokoke_same_day (const time_t* day1,
const time_t* day2);
#endif /* !__SOKOKE_H__ */ #endif /* !__SOKOKE_H__ */