Implement search in the bookmark panel

This commit is contained in:
Alexander Butenko 2010-07-03 01:47:41 -04:00 committed by Christian Dywan
parent d6d1cd21a9
commit 8c784e7486

View file

@ -19,12 +19,15 @@
#include "midori-viewable.h" #include "midori-viewable.h"
#include "sokoke.h" #include "sokoke.h"
#include "gtkiconentry.h"
#include <glib/gi18n.h> #include <glib/gi18n.h>
#include <string.h> #include <string.h>
#include <gdk/gdkkeysyms.h> #include <gdk/gdkkeysyms.h>
#define COMPLETION_DELAY 200
gboolean gboolean
midori_browser_edit_bookmark_dialog_new (MidoriBrowser* browser, midori_browser_edit_bookmark_dialog_new (MidoriBrowser* browser,
KatzeItem* bookmark, KatzeItem* bookmark,
@ -45,6 +48,9 @@ struct _MidoriBookmarks
GtkWidget* treeview; GtkWidget* treeview;
MidoriApp* app; MidoriApp* app;
KatzeArray* array; KatzeArray* array;
gint filter_timeout;
gchar* filter;
}; };
struct _MidoriBookmarksClass struct _MidoriBookmarksClass
@ -139,7 +145,8 @@ midori_bookmarks_import_array_db (sqlite3* db,
static KatzeArray* static KatzeArray*
midori_bookmarks_read_from_db (MidoriBookmarks* bookmarks, midori_bookmarks_read_from_db (MidoriBookmarks* bookmarks,
const gchar* folder) const gchar* folder,
const gchar* keyword)
{ {
sqlite3* db; sqlite3* db;
sqlite3_stmt* statement; sqlite3_stmt* statement;
@ -148,10 +155,25 @@ midori_bookmarks_read_from_db (MidoriBookmarks* bookmarks,
db = g_object_get_data (G_OBJECT (bookmarks->array), "db"); db = g_object_get_data (G_OBJECT (bookmarks->array), "db");
sqlcmd = "SELECT uri, title, app, toolbar, folder from bookmarks where folder = ?" if (keyword && *keyword)
" ORDER BY uri DESC"; {
gchar* filterstr;
sqlcmd = "SELECT uri, title, app, toolbar, folder from bookmarks where "
" title like ? ORDER BY uri DESC";
result = sqlite3_prepare_v2 (db, sqlcmd, -1, &statement, NULL); result = sqlite3_prepare_v2 (db, sqlcmd, -1, &statement, NULL);
sqlite3_bind_text (statement, 1, g_strdup(folder), -1, g_free); filterstr = g_strdup_printf ("%%%s%%", keyword);
sqlite3_bind_text (statement, 1, g_strdup (filterstr), -1, g_free);
g_free (filterstr);
}
else
{
if (!folder)
folder = "";
sqlcmd = "SELECT uri, title, app, toolbar, folder from bookmarks where "
" folder = ? ORDER BY uri DESC";
result = sqlite3_prepare_v2 (db, sqlcmd, -1, &statement, NULL);
sqlite3_bind_text (statement, 1, g_strdup (folder), -1, g_free);
}
if (result != SQLITE_OK) if (result != SQLITE_OK)
return NULL; return NULL;
@ -163,18 +185,25 @@ static void
midori_bookmarks_read_from_db_to_model (MidoriBookmarks* bookmarks, midori_bookmarks_read_from_db_to_model (MidoriBookmarks* bookmarks,
GtkTreeStore* model, GtkTreeStore* model,
GtkTreeIter* parent, GtkTreeIter* parent,
const gchar* folder) const gchar* folder,
const gchar* keyword)
{ {
KatzeArray* array; KatzeArray* array;
gint last; gint last;
KatzeItem* item;
GtkTreeIter child; GtkTreeIter child;
array = midori_bookmarks_read_from_db (bookmarks, folder); array = midori_bookmarks_read_from_db (bookmarks, folder, keyword);
katze_bookmark_populate_tree_view (array, model, parent); katze_bookmark_populate_tree_view (array, model, parent);
/* Remove invisible dummy row */ /* Remove invisible dummy row */
last = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (model), parent); last = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (model), parent);
if (!last)
return;
gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (model), &child, parent, last - 1); gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (model), &child, parent, last - 1);
gtk_tree_model_get (GTK_TREE_MODEL (model), &child, 0, &item, -1);
if (KATZE_ITEM_IS_SEPARATOR (item))
gtk_tree_store_remove (model, &child); gtk_tree_store_remove (model, &child);
g_object_unref (item);
} }
void void
@ -474,7 +503,7 @@ midori_bookmarks_set_app (MidoriBookmarks* bookmarks,
g_object_set_data (G_OBJECT (bookmarks->array), "treeview", bookmarks->treeview); g_object_set_data (G_OBJECT (bookmarks->array), "treeview", bookmarks->treeview);
#if HAVE_SQLITE #if HAVE_SQLITE
midori_bookmarks_read_from_db_to_model (bookmarks, GTK_TREE_STORE (model), NULL, ""); midori_bookmarks_read_from_db_to_model (bookmarks, GTK_TREE_STORE (model), NULL, "", NULL);
g_signal_connect_after (model, "row-changed", g_signal_connect_after (model, "row-changed",
G_CALLBACK (midori_bookmarks_row_changed_cb), G_CALLBACK (midori_bookmarks_row_changed_cb),
bookmarks); bookmarks);
@ -659,7 +688,7 @@ midori_bookmarks_open_in_tab_activate_cb (GtkWidget* menuitem,
KatzeArray* array; KatzeArray* array;
guint i = 0; guint i = 0;
array = midori_bookmarks_read_from_db (bookmarks, katze_item_get_name (item)); array = midori_bookmarks_read_from_db (bookmarks, katze_item_get_name (item), NULL);
g_return_if_fail (KATZE_IS_ARRAY (array)); g_return_if_fail (KATZE_IS_ARRAY (array));
while ((child = katze_array_get_nth_item (KATZE_ARRAY (array), i))) while ((child = katze_array_get_nth_item (KATZE_ARRAY (array), i)))
{ {
@ -840,7 +869,7 @@ midori_bookmarks_row_expanded_cb (GtkTreeView* treeview,
gtk_tree_model_get (model, iter, 0, &item, -1); gtk_tree_model_get (model, iter, 0, &item, -1);
#if HAVE_SQLITE #if HAVE_SQLITE
midori_bookmarks_read_from_db_to_model (bookmarks, GTK_TREE_STORE (model), midori_bookmarks_read_from_db_to_model (bookmarks, GTK_TREE_STORE (model),
iter, katze_item_get_name (item)); iter, katze_item_get_name (item), NULL);
#endif #endif
g_object_unref (item); g_object_unref (item);
} }
@ -864,15 +893,76 @@ midori_bookmarks_row_collapsed_cb (GtkTreeView *treeview,
0, 0, NULL, -1); 0, 0, NULL, -1);
} }
static gboolean
midori_bookmarks_filter_timeout_cb (gpointer data)
{
MidoriBookmarks* bookmarks = data;
GtkTreeModel* model;
GtkTreeStore* treestore;
model = gtk_tree_view_get_model (GTK_TREE_VIEW (bookmarks->treeview));
treestore = GTK_TREE_STORE (model);
gtk_tree_store_clear (treestore);
midori_bookmarks_read_from_db_to_model (bookmarks,
treestore, NULL, NULL, bookmarks->filter);
return FALSE;
}
static void
midori_bookmarks_filter_entry_changed_cb (GtkEntry* entry,
MidoriBookmarks* bookmarks)
{
if (bookmarks->filter_timeout)
g_source_remove (bookmarks->filter_timeout);
katze_assign (bookmarks->filter, g_strdup (gtk_entry_get_text (entry)));
bookmarks->filter_timeout = g_timeout_add (COMPLETION_DELAY,
midori_bookmarks_filter_timeout_cb, bookmarks);
}
static void
midori_bookmarks_filter_entry_clear_cb (GtkEntry* entry,
gint icon_pos,
gint button,
MidoriBookmarks* bookmarks)
{
if (icon_pos == GTK_ICON_ENTRY_SECONDARY)
gtk_entry_set_text (entry, "");
}
static void static void
midori_bookmarks_init (MidoriBookmarks* bookmarks) midori_bookmarks_init (MidoriBookmarks* bookmarks)
{ {
GtkWidget* entry;
GtkWidget* box;
GtkTreeStore* model; GtkTreeStore* model;
GtkWidget* treeview; GtkWidget* treeview;
GtkTreeViewColumn* column; GtkTreeViewColumn* column;
GtkCellRenderer* renderer_pixbuf; GtkCellRenderer* renderer_pixbuf;
GtkCellRenderer* renderer_text; GtkCellRenderer* renderer_text;
/* Create the filter entry */
entry = gtk_icon_entry_new ();
gtk_icon_entry_set_icon_from_stock (GTK_ICON_ENTRY (entry),
GTK_ICON_ENTRY_PRIMARY,
GTK_STOCK_FIND);
gtk_icon_entry_set_icon_from_stock (GTK_ICON_ENTRY (entry),
GTK_ICON_ENTRY_SECONDARY,
GTK_STOCK_CLEAR);
gtk_icon_entry_set_icon_highlight (GTK_ICON_ENTRY (entry),
GTK_ICON_ENTRY_SECONDARY, TRUE);
g_signal_connect (entry, "icon-release",
G_CALLBACK (midori_bookmarks_filter_entry_clear_cb), bookmarks);
#if HAVE_SQLITE
g_signal_connect (entry, "changed",
G_CALLBACK (midori_bookmarks_filter_entry_changed_cb), bookmarks);
#endif
box = gtk_hbox_new (FALSE, 0);
gtk_box_pack_start (GTK_BOX (box), entry, TRUE, TRUE, 3);
gtk_widget_show_all (box);
gtk_box_pack_start (GTK_BOX (bookmarks), box, FALSE, FALSE, 5);
/* Create the treeview */ /* Create the treeview */
model = gtk_tree_store_new (1, KATZE_TYPE_ITEM); model = gtk_tree_store_new (1, KATZE_TYPE_ITEM);
treeview = gtk_tree_view_new_with_model (GTK_TREE_MODEL (model)); treeview = gtk_tree_view_new_with_model (GTK_TREE_MODEL (model));