From 8ad224c9a8f7240cd2d0e003d70c43b783369761 Mon Sep 17 00:00:00 2001 From: Christian Dywan Date: Sun, 21 Nov 2010 16:49:20 +0100 Subject: [PATCH] Add bookmarks through array and update the panel --- midori/main.c | 47 ++++++++++++++++++++++++++++ midori/midori-browser.c | 25 +++------------ panels/midori-bookmarks.c | 64 ++++++++++++--------------------------- 3 files changed, 70 insertions(+), 66 deletions(-) diff --git a/midori/main.c b/midori/main.c index 430e52e1..63be1051 100644 --- a/midori/main.c +++ b/midori/main.c @@ -489,6 +489,51 @@ midori_history_terminate (KatzeArray* array, sqlite3_close (db); } +static void +midori_bookmarks_add_item_cb (KatzeArray* array, + KatzeItem* item, + sqlite3* db) +{ + gchar* sqlcmd; + char* errmsg = NULL; + KatzeItem* old_parent; + const gchar* uri; + const gchar* folder = katze_item_get_meta_string (item, "folder"); + const gchar* parent; + + if (KATZE_ITEM_IS_BOOKMARK (item)) + uri = katze_item_get_uri (item); + else + uri = ""; + + /* Use folder, otherwise fallback to parent folder */ + old_parent = katze_item_get_parent (item); + if (folder && *folder) + parent = folder; + else if (old_parent && katze_item_get_name (old_parent)) + parent = katze_item_get_name (old_parent); + else + parent = ""; + + sqlcmd = sqlite3_mprintf ( + "INSERT into bookmarks (uri, title, desc, folder, toolbar, app) values" + " ('%q', '%q', '%q', '%q', %d, %d)", + uri, + katze_item_get_name (item), + katze_item_get_text (item), + parent, + katze_item_get_meta_boolean (item, "toolbar"), + katze_item_get_meta_boolean (item, "app")); + + if (sqlite3_exec (db, sqlcmd, NULL, NULL, &errmsg) != SQLITE_OK) + { + g_printerr (_("Failed to add bookmark item: %s\n"), errmsg); + sqlite3_free (errmsg); + } + + sqlite3_free (sqlcmd); +} + static void midori_bookmarks_remove_item_cb (KatzeArray* array, KatzeItem* item, @@ -542,6 +587,8 @@ midori_bookmarks_initialize (KatzeArray* array, "desc text, app integer, toolbar integer);", NULL, NULL, errmsg) != SQLITE_OK) return NULL; + g_signal_connect (array, "add-item", + G_CALLBACK (midori_bookmarks_add_item_cb), db); g_signal_connect (array, "remove-item", G_CALLBACK (midori_bookmarks_remove_item_cb), db); return db; diff --git a/midori/midori-browser.c b/midori/midori-browser.c index 85c841f7..d4560328 100644 --- a/midori/midori-browser.c +++ b/midori/midori-browser.c @@ -178,11 +178,6 @@ midori_bookmarks_export_array_db (sqlite3* db, KatzeArray* array, const gchar* folder); -void -midori_bookmarks_insert_item_db (sqlite3* db, - KatzeItem* item, - gchar* folder); - void midori_browser_open_bookmark (MidoriBrowser* browser, KatzeItem* item); @@ -909,8 +904,6 @@ midori_browser_edit_bookmark_dialog_new (MidoriBrowser* browser, if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT) { gchar* selected; - GtkTreeView* treeview; - GtkTreeModel* model; if (!new_bookmark) katze_array_remove_item (browser->bookmarks, bookmark); @@ -930,23 +923,13 @@ midori_browser_edit_bookmark_dialog_new (MidoriBrowser* browser, } selected = gtk_combo_box_get_active_text (GTK_COMBO_BOX (combo_folder)); - if (!strcmp (selected, _("Toplevel folder"))) - selected = g_strdup (""); - - midori_bookmarks_insert_item_db (db, bookmark, selected); - - if (new_bookmark) - { - treeview = g_object_get_data (G_OBJECT (browser->bookmarks), "treeview"); - model = gtk_tree_view_get_model (GTK_TREE_VIEW (treeview)); - gtk_tree_store_insert_with_values (GTK_TREE_STORE (model), - NULL, NULL, G_MAXINT, 0, bookmark, -1); - } + katze_assign (selected, g_strdup ("")); + katze_item_set_meta_string (bookmark, "folder", selected); + katze_array_add_item (browser->bookmarks, bookmark); if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (check_toolbar))) - if (!gtk_widget_get_visible (browser->bookmarkbar) - && browser->bookmarks != NULL) + if (!gtk_widget_get_visible (browser->bookmarkbar)) _action_set_active (browser, "Bookmarkbar", TRUE); g_free (selected); return_status = TRUE; diff --git a/panels/midori-bookmarks.c b/panels/midori-bookmarks.c index 4933f465..07a312de 100644 --- a/panels/midori-bookmarks.c +++ b/panels/midori-bookmarks.c @@ -161,7 +161,8 @@ midori_bookmarks_import_array_db (sqlite3* db, { if (KATZE_IS_ARRAY (item)) midori_bookmarks_import_array_db (db, KATZE_ARRAY (item), folder); - midori_bookmarks_insert_item_db (db, item, folder); + katze_item_set_meta_string (item, "folder", folder); + katze_array_add_item (array, item); } } @@ -229,50 +230,22 @@ midori_bookmarks_read_from_db_to_model (MidoriBookmarks* bookmarks, g_object_unref (item); } -void -midori_bookmarks_insert_item_db (sqlite3* db, - KatzeItem* item, - const gchar* folder) +static void +midori_bookmarks_add_item_cb (KatzeArray* array, + KatzeItem* item, + MidoriBookmarks* bookmarks) { - gchar* sqlcmd; - char* errmsg = NULL; - KatzeItem* old_parent; - gchar* parent; - gchar* uri; - - if (KATZE_ITEM_IS_BOOKMARK (item)) - uri = g_strdup (katze_item_get_uri (item)); + GtkTreeModel* model; + model = gtk_tree_view_get_model (GTK_TREE_VIEW (bookmarks->treeview)); + if (!strcmp (katze_item_get_meta_string (item, "folder"), "")) + gtk_tree_store_insert_with_values (GTK_TREE_STORE (model), + NULL, NULL, G_MAXINT, 0, item, -1); else - uri = g_strdup (""); - - /* Use folder, otherwise fallback to parent folder */ - old_parent = katze_item_get_parent (item); - if (folder && *folder) - parent = g_strdup (folder); - else if (old_parent && katze_item_get_name (old_parent)) - parent = g_strdup (katze_item_get_name (old_parent)); - else - parent = g_strdup (""); - - sqlcmd = sqlite3_mprintf ( - "INSERT into bookmarks (uri, title, desc, folder, toolbar, app) values" - " ('%q', '%q', '%q', '%q', %d, %d)", - uri, - katze_item_get_name (item), - katze_item_get_text (item), - parent, - katze_item_get_meta_boolean (item, "toolbar"), - katze_item_get_meta_boolean (item, "app")); - - if (sqlite3_exec (db, sqlcmd, NULL, NULL, &errmsg) != SQLITE_OK) { - g_printerr (_("Failed to add bookmark item: %s\n"), errmsg); - sqlite3_free (errmsg); + gtk_tree_store_clear (GTK_TREE_STORE (model)); + midori_bookmarks_read_from_db_to_model (bookmarks, + GTK_TREE_STORE (model), NULL, NULL, bookmarks->filter); } - - g_free (uri); - g_free (parent); - sqlite3_free (sqlcmd); } static void @@ -318,7 +291,8 @@ midori_bookmarks_row_changed_cb (GtkTreeModel* model, parent_name = g_strdup (""); katze_array_remove_item (bookmarks->array, item); - midori_bookmarks_insert_item_db (db, item, parent_name); + katze_item_set_meta_string (item, "folder", parent_name); + katze_array_add_item (bookmarks->array, item); } static void @@ -468,11 +442,11 @@ midori_bookmarks_set_app (MidoriBookmarks* bookmarks, g_object_ref (app); bookmarks->array = katze_object_get_object (app, "bookmarks"); - g_object_set_data (G_OBJECT (bookmarks->array), "treeview", bookmarks->treeview); + midori_bookmarks_read_from_db_to_model (bookmarks, GTK_TREE_STORE (model), NULL, "", NULL); + g_signal_connect (bookmarks->array, "add-item", + G_CALLBACK (midori_bookmarks_add_item_cb), bookmarks); g_signal_connect (bookmarks->array, "remove-item", G_CALLBACK (midori_bookmarks_remove_item_cb), bookmarks); - - midori_bookmarks_read_from_db_to_model (bookmarks, GTK_TREE_STORE (model), NULL, "", NULL); g_signal_connect_after (model, "row-changed", G_CALLBACK (midori_bookmarks_row_changed_cb), bookmarks);