Store history dates in 'token' to avoid locatime() and revamp deleting

Loading history items used to involve frequent calls to
localtime() and g_memdup(). We avoid this by storing the
date strings in parent items now, which can even be used to
display items in the treeview.
Deleting of treeview items was also reworked so it should
work from menu items and the panel.
This commit is contained in:
Dale Whittaker 2009-01-11 19:30:43 +01:00 committed by Christian Dywan
parent d55b6ddd00
commit 6753a9ade3
5 changed files with 82 additions and 100 deletions

View file

@ -597,7 +597,7 @@ db_exec_callback (sqlite3* db,
{ {
*error = g_error_new (MIDORI_HISTORY_ERROR, *error = g_error_new (MIDORI_HISTORY_ERROR,
MIDORI_HISTORY_ERROR_EXEC_SQL, MIDORI_HISTORY_ERROR_EXEC_SQL,
_("Failed to open database: %s\n"), _("Failed to execute SQL statement: %s\n"),
errmsg); errmsg);
} }
sqlite3_free (errmsg); sqlite3_free (errmsg);
@ -642,9 +642,9 @@ 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 ( sqlcmd = sqlite3_mprintf (
"DELETE FROM history WHERE uri = '%s' AND" "DELETE FROM history WHERE uri = '%q' AND"
" title = '%s' AND date = %" G_GINT64_FORMAT, " title = '%q' AND date = %" G_GINT64_FORMAT,
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));
@ -655,7 +655,7 @@ midori_history_remove_item_cb (KatzeArray* history,
g_error_free (error); g_error_free (error);
return ; return ;
} }
g_free (sqlcmd); sqlite3_free (sqlcmd);
} }
static void static void
@ -738,9 +738,9 @@ midori_history_add_items (void* data,
KatzeArray* parent = NULL; KatzeArray* parent = NULL;
KatzeArray* array = KATZE_ARRAY (data); KatzeArray* array = KATZE_ARRAY (data);
gint64 date; gint64 date;
time_t newdate; gint i;
gint i, j, n;
gint ncols = 3; gint ncols = 3;
gchar token[50];
g_return_val_if_fail (KATZE_IS_ARRAY (array), 1); g_return_val_if_fail (KATZE_IS_ARRAY (array), 1);
@ -761,18 +761,15 @@ midori_history_add_items (void* data,
date = g_ascii_strtoull (argv[i + 2], NULL, 10); date = g_ascii_strtoull (argv[i + 2], NULL, 10);
katze_item_set_added (item, date); katze_item_set_added (item, date);
n = katze_array_get_length (array); strftime (token, sizeof (token), "%Y-%m-%d",
for (j = 0; j < n; j++) localtime ((time_t *)&date));
{ parent = katze_array_find_token (array, token);
parent = katze_array_get_nth_item (array, j);
newdate = katze_item_get_added (KATZE_ITEM (parent)); if (!parent)
if (sokoke_same_day ((time_t *)&date, (time_t *)&newdate))
break;
}
if (j == n)
{ {
parent = katze_array_new (KATZE_TYPE_ARRAY); parent = katze_array_new (KATZE_TYPE_ARRAY);
katze_item_set_added (KATZE_ITEM (parent), date); katze_item_set_added (KATZE_ITEM (parent), date);
katze_item_set_token (KATZE_ITEM (parent), token);
katze_array_add_item (array, parent); katze_array_add_item (array, parent);
} }
katze_array_add_item (parent, item); katze_array_add_item (parent, item);

View file

@ -2332,6 +2332,62 @@ midori_browser_bookmark_edit_activate_cb (GtkWidget* menuitem,
midori_browser_edit_bookmark_dialog_new (browser, item, FALSE); midori_browser_edit_bookmark_dialog_new (browser, item, FALSE);
} }
static void
midori_browser_model_remove_item (GtkTreeModel* model,
KatzeItem* item,
GtkTreeIter* iter)
{
GtkTreeIter child_iter;
KatzeItem* child;
KatzeArray* parent;
gint i, n;
if (KATZE_IS_ARRAY (item))
{
n = katze_array_get_length (KATZE_ARRAY (item));
for (i = 0; i < n; i++)
{
child = katze_array_get_nth_item (KATZE_ARRAY (item), 0);
katze_array_remove_item (KATZE_ARRAY (item), child);
}
while (gtk_tree_model_iter_nth_child (model, &child_iter, iter, 0))
gtk_tree_store_remove (GTK_TREE_STORE (model), &child_iter);
}
gtk_tree_store_remove (GTK_TREE_STORE (model), iter);
g_object_unref (item);
parent = katze_item_get_parent (item);
katze_array_remove_item (parent, item);
}
static void
midori_browser_history_delete (MidoriBrowser* browser)
{
GtkTreeView* treeview;
GtkTreeModel* model;
GtkTreeIter iter;
KatzeItem* item;
GtkAction* location_action;
treeview = GTK_TREE_VIEW (browser->panel_history);
if (sokoke_tree_view_get_selected_iter (treeview, &model, &iter))
{
location_action = _action_by_name (browser, "Location");
gtk_tree_model_get (model, &iter, 0, &item, -1);
midori_browser_model_remove_item (model, item, &iter);
midori_location_action_delete_item_from_uri (
MIDORI_LOCATION_ACTION (location_action), katze_item_get_uri (item));
}
}
static void
midori_browser_history_delete_activate_cb (GtkWidget* menuitem,
MidoriBrowser* browser)
{
midori_browser_history_delete (browser);
}
static void static void
midori_browser_bookmark_delete_activate_cb (GtkWidget* menuitem, midori_browser_bookmark_delete_activate_cb (GtkWidget* menuitem,
MidoriBrowser* browser) MidoriBrowser* browser)
@ -2377,10 +2433,15 @@ midori_browser_bookmark_popup (GtkWidget* widget,
gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem); gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
gtk_widget_show (menuitem); gtk_widget_show (menuitem);
if (!history_item) if (!history_item)
{
midori_browser_bookmark_popup_item (menu, GTK_STOCK_EDIT, NULL, midori_browser_bookmark_popup_item (menu, GTK_STOCK_EDIT, NULL,
item, midori_browser_bookmark_edit_activate_cb, browser); item, midori_browser_bookmark_edit_activate_cb, browser);
midori_browser_bookmark_popup_item (menu, GTK_STOCK_DELETE, NULL, midori_browser_bookmark_popup_item (menu, GTK_STOCK_DELETE, NULL,
item, midori_browser_bookmark_delete_activate_cb, browser); item, midori_browser_bookmark_delete_activate_cb, browser);
}
else
midori_browser_bookmark_popup_item (menu, GTK_STOCK_DELETE, NULL,
item, midori_browser_history_delete_activate_cb, browser);
sokoke_widget_popup (widget, GTK_MENU (menu), sokoke_widget_popup (widget, GTK_MENU (menu),
event, SOKOKE_MENU_POSITION_CURSOR); event, SOKOKE_MENU_POSITION_CURSOR);
@ -2525,35 +2586,6 @@ midori_panel_history_button_release_event_cb (GtkWidget* widget,
return FALSE; return FALSE;
} }
static void
midori_browser_model_remove_item (GtkTreeModel* model,
KatzeItem* item,
GtkTreeIter* iter)
{
GtkTreeIter child_iter;
KatzeItem* child;
KatzeArray* parent;
gint i, n;
if (KATZE_IS_ARRAY (item))
{
n = katze_array_get_length (KATZE_ARRAY (item));
for (i = 0; i < n; i++)
{
child = katze_array_get_nth_item (KATZE_ARRAY (item), 0);
katze_array_remove_item (KATZE_ARRAY (item), child);
}
while (gtk_tree_model_iter_nth_child (model, &child_iter, iter, 0))
gtk_tree_store_remove (GTK_TREE_STORE (model), &child_iter);
}
gtk_tree_store_remove (GTK_TREE_STORE (model), iter);
g_object_unref (item);
parent = katze_item_get_parent (item);
katze_array_remove_item (parent, item);
}
static gboolean static gboolean
midori_panel_history_key_release_event_cb (GtkWidget* widget, midori_panel_history_key_release_event_cb (GtkWidget* widget,
GdkEventKey* event, GdkEventKey* event,
@ -2733,8 +2765,6 @@ midori_browser_history_render_text_cb (GtkTreeViewColumn* column,
GtkWidget* treeview) GtkWidget* treeview)
{ {
KatzeItem* item; KatzeItem* item;
time_t date;
gchar datebuf[50];
char* sdate; char* sdate;
gint age; gint age;
@ -2748,9 +2778,7 @@ midori_browser_history_render_text_cb (GtkTreeViewColumn* column,
if (age > 7) if (age > 7)
{ {
date = (time_t)katze_item_get_added (item); g_object_set (renderer, "text", katze_item_get_token (item), NULL);
strftime (datebuf, sizeof (datebuf), "%Y-%m-%d", localtime (&date));
g_object_set (renderer, "text", datebuf, NULL);
} }
else if (age > 6) else if (age > 6)
{ {
@ -2965,21 +2993,7 @@ static void
_action_history_delete_activate (GtkAction* action, _action_history_delete_activate (GtkAction* action,
MidoriBrowser* browser) MidoriBrowser* browser)
{ {
GtkTreeView* treeview; midori_browser_history_delete (browser);
GtkTreeModel* model;
GtkTreeIter iter;
KatzeItem* item;
GtkAction* location_action;
treeview = GTK_TREE_VIEW (browser->panel_history);
if (sokoke_tree_view_get_selected_iter (treeview, &model, &iter))
{
location_action = _action_by_name (browser, "Location");
gtk_tree_model_get (model, &iter, 0, &item, -1);
midori_location_action_delete_item_from_uri (
MIDORI_LOCATION_ACTION (location_action), katze_item_get_uri (item));
midori_browser_model_remove_item (model, item, &iter);
}
} }
static void static void
@ -3518,6 +3532,7 @@ midori_browser_new_history_item (MidoriBrowser* browser,
time_t date_; time_t date_;
gint age; gint age;
gint newage; gint newage;
gchar token[50];
if (!katze_object_get_boolean (browser->settings, "remember-last-visited-pages")) if (!katze_object_get_boolean (browser->settings, "remember-last-visited-pages"))
return; return;
@ -3550,8 +3565,10 @@ midori_browser_new_history_item (MidoriBrowser* browser,
} }
if (!found) if (!found)
{ {
strftime (token, sizeof (token), "%Y-%m-%d", localtime (&now));
parent = katze_array_new (KATZE_TYPE_ARRAY); parent = katze_array_new (KATZE_TYPE_ARRAY);
katze_item_set_added (KATZE_ITEM (parent), now); katze_item_set_added (KATZE_ITEM (parent), now);
katze_item_set_token (KATZE_ITEM (parent), token);
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,

View file

@ -15,6 +15,7 @@
#include "marshal.h" #include "marshal.h"
#include "sokoke.h" #include "sokoke.h"
#include <string.h>
#include <glib/gi18n.h> #include <glib/gi18n.h>
#include <gdk/gdkkeysyms.h> #include <gdk/gdkkeysyms.h>

View file

@ -663,35 +663,6 @@ sokoke_tree_view_get_selected_iter (GtkTreeView* tree_view,
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;
}
/** /**
* sokoke_days_between: * sokoke_days_between:
* @day1: a time_t timestamp value * @day1: a time_t timestamp value

View file

@ -126,10 +126,6 @@ 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);
gint gint
sokoke_days_between (const time_t* day1, sokoke_days_between (const time_t* day1,
const time_t* day2); const time_t* day2);