]> spindle.queued.net Git - midori/commitdiff
Use KatzeArray* to remove and monitor deleted bookmarks
authorChristian Dywan <christian@twotoasts.de>
Tue, 10 Aug 2010 21:46:49 +0000 (23:46 +0200)
committerChristian Dywan <christian@twotoasts.de>
Tue, 10 Aug 2010 21:46:49 +0000 (23:46 +0200)
The panel and the bookmarkbar need to monitor the removal of
bookmarks, and there can be multiple windows, so the database
should only be used to remove in one central place.

midori/main.c
midori/midori-browser.c
panels/midori-bookmarks.c
panels/midori-bookmarks.h

index 5b480b2a82bfa9f3b9e6241f54776299a9aa4419..14b97272a41a3387420be88b876a048682f911c7 100644 (file)
@@ -428,6 +428,37 @@ midori_history_terminate (sqlite3* db,
     sqlite3_close (db);
 }
 
+static void
+midori_bookmarks_remove_item_cb (KatzeArray* array,
+                                 KatzeItem*  item,
+                                 sqlite3*    db)
+{
+    gchar* sqlcmd;
+    char* errmsg = NULL;
+
+    if (KATZE_ITEM_IS_BOOKMARK (item))
+        sqlcmd = sqlite3_mprintf (
+            "DELETE FROM bookmarks WHERE uri = '%q' "
+            " AND folder = '%q'",
+            katze_item_get_uri (item),
+            katze_item_get_meta_string (item, "folder"));
+
+    else
+       sqlcmd = sqlite3_mprintf (
+            "DELETE FROM bookmarks WHERE title = '%q'"
+            " AND folder = '%q'",
+            katze_item_get_name (item),
+            katze_item_get_meta_string (item, "folder"));
+
+    if (sqlite3_exec (db, sqlcmd, NULL, NULL, &errmsg) != SQLITE_OK)
+    {
+        g_printerr (_("Failed to remove history item: %s\n"), errmsg);
+        sqlite3_free (errmsg);
+    }
+
+    sqlite3_free (sqlcmd);
+}
+
 static sqlite3*
 midori_bookmarks_initialize (KatzeArray*  array,
                              const gchar* filename,
@@ -450,6 +481,8 @@ midori_bookmarks_initialize (KatzeArray*  array,
                       "desc text, app integer, toolbar integer);",
                       NULL, NULL, errmsg) != SQLITE_OK)
         return NULL;
+    g_signal_connect (array, "remove-item",
+                      G_CALLBACK (midori_bookmarks_remove_item_cb), db);
     return db;
 }
 
index 0e3e7633366d0eea0c11b3dcd77a051db953c87f..df8312232abd9da444d2a36db1d00dd7601bb7e7 100644 (file)
@@ -183,10 +183,6 @@ midori_bookmarks_insert_item_db (sqlite3*   db,
                                  KatzeItem* item,
                                  gchar*     folder);
 
-void
-midori_bookmarks_remove_item_from_db (sqlite3*   db,
-                                      KatzeItem* item);
-
 static void
 midori_bookmarkbar_populate (MidoriBrowser* browser);
 
@@ -927,7 +923,7 @@ midori_browser_edit_bookmark_dialog_new (MidoriBrowser* browser,
         GtkTreeModel* model;
 
         if (!new_bookmark)
-            midori_bookmarks_remove_item_from_db (db, bookmark);
+            katze_array_remove_item (browser->bookmarks, bookmark);
 
         katze_item_set_name (bookmark,
             gtk_entry_get_text (GTK_ENTRY (entry_title)));
@@ -3862,14 +3858,10 @@ static void
 midori_browser_bookmark_delete_activate_cb (GtkWidget*     menuitem,
                                             MidoriBrowser* browser)
 {
-    sqlite3* db;
     KatzeItem* item;
 
-    db = g_object_get_data (G_OBJECT (browser->bookmarks), "db");
     item = (KatzeItem*)g_object_get_data (G_OBJECT (menuitem), "KatzeItem");
-
-    midori_bookmarks_remove_item_from_db (db, item);
-    g_object_unref (item);
+    katze_array_remove_item (browser->bookmarks, item);
 }
 
 static void
@@ -6367,6 +6359,15 @@ midori_bookmarkbar_insert_item (GtkWidget* toolbar,
     gtk_toolbar_insert (GTK_TOOLBAR (toolbar), toolitem, -1);
 }
 
+static void
+midori_bookmarkbar_remove_item_cb (KatzeArray*    bookmarks,
+                                   KatzeItem*     item,
+                                   MidoriBrowser* browser)
+{
+    if (gtk_widget_get_visible (browser->bookmarkbar))
+        midori_bookmarkbar_populate (browser);
+}
+
 static void
 midori_bookmarkbar_populate (MidoriBrowser* browser)
 {
@@ -6456,6 +6457,9 @@ midori_browser_set_bookmarks (MidoriBrowser* browser,
 {
     MidoriWebSettings* settings;
 
+    if (browser->bookmarks != NULL)
+        g_signal_handlers_disconnect_by_func (browser->bookmarks,
+            midori_bookmarkbar_remove_item_cb, browser);
     settings = midori_browser_get_settings (browser);
     g_signal_handlers_disconnect_by_func (settings,
         midori_browser_show_bookmarkbar_notify_value_cb, browser);
@@ -6468,6 +6472,8 @@ midori_browser_set_bookmarks (MidoriBrowser* browser,
     g_signal_connect (settings, "notify::show-bookmarkbar",
         G_CALLBACK (midori_browser_show_bookmarkbar_notify_value_cb), browser);
     g_object_notify (G_OBJECT (settings), "show-bookmarkbar");
+    g_signal_connect_after (bookmarks, "remove-item",
+        G_CALLBACK (midori_bookmarkbar_remove_item_cb), browser);
 }
 
 static void
index b572a98c4c590b68e95c1b6a11fb24658f2476ba..339c9d4edb72f5a36eae683673b43c672f305783 100644 (file)
@@ -281,34 +281,15 @@ midori_bookmarks_insert_item_db (sqlite3*     db,
     sqlite3_free (sqlcmd);
 }
 
-void
-midori_bookmarks_remove_item_from_db (sqlite3*   db,
-                                      KatzeItem* item)
+static void
+midori_bookmarks_remove_item_cb (KatzeArray*      array,
+                                 KatzeItem*       item,
+                                 MidoriBookmarks* bookmarks)
 {
-    gchar* sqlcmd;
-    char* errmsg = NULL;
-
-    if (KATZE_ITEM_IS_BOOKMARK (item))
-        sqlcmd = sqlite3_mprintf (
-            "DELETE FROM bookmarks WHERE uri = '%q' "
-            " AND folder = '%q'",
-            katze_item_get_uri (item),
-            katze_item_get_meta_string (item, "folder"));
-
-    else
-       sqlcmd = sqlite3_mprintf (
-            "DELETE FROM bookmarks WHERE title = '%q'"
-            " AND folder = '%q'",
-            katze_item_get_name (item),
-            katze_item_get_meta_string (item, "folder"));
-
-    if (sqlite3_exec (db, sqlcmd, NULL, NULL, &errmsg) != SQLITE_OK)
-    {
-        g_printerr (_("Failed to remove history item: %s\n"), errmsg);
-        sqlite3_free (errmsg);
-    }
-
-    sqlite3_free (sqlcmd);
+    GtkTreeModel* model = gtk_tree_view_get_model (GTK_TREE_VIEW (bookmarks->treeview));
+    gtk_tree_store_clear (GTK_TREE_STORE (model));
+    midori_bookmarks_read_from_db_to_model (bookmarks,
+        GTK_TREE_STORE (model), NULL, NULL, bookmarks->filter);
 }
 
 static void
@@ -342,7 +323,7 @@ midori_bookmarks_row_changed_cb (GtkTreeModel*    model,
     else
         parent_name = g_strdup ("");
 
-    midori_bookmarks_remove_item_from_db (db, item);
+    katze_array_remove_item (bookmarks->array, item);
     midori_bookmarks_insert_item_db (db, item, parent_name);
 }
 
@@ -387,9 +368,7 @@ midori_bookmarks_delete_clicked_cb (GtkWidget*       toolitem,
 {
     GtkTreeModel* model;
     GtkTreeIter iter;
-    sqlite3* db;
 
-    db = g_object_get_data (G_OBJECT (bookmarks->array), "db");
     if (katze_tree_view_get_selected_iter (GTK_TREE_VIEW (bookmarks->treeview),
                                            &model, &iter))
     {
@@ -397,9 +376,13 @@ midori_bookmarks_delete_clicked_cb (GtkWidget*       toolitem,
 
         gtk_tree_model_get (model, &iter, 0, &item, -1);
 
-        midori_bookmarks_remove_item_from_db (db, item);
+        /* Manually remove the iter and block clearing the treeview */
         gtk_tree_store_remove (GTK_TREE_STORE (model), &iter);
-
+        g_signal_handlers_block_by_func (bookmarks->array,
+            midori_bookmarks_remove_item_cb, bookmarks);
+        katze_array_remove_item (bookmarks->array, item);
+        g_signal_handlers_unblock_by_func (bookmarks->array,
+            midori_bookmarks_remove_item_cb, bookmarks);
         g_object_unref (item);
     }
 }
@@ -492,6 +475,8 @@ 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);
+    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",
index 2c53712f6e6d3d2d6a2ed73d2fcb0462cf7060eb..3dc260bed22fc3432bf260210b94d3aeb8dfd1d1 100644 (file)
@@ -47,10 +47,6 @@ midori_bookmarks_insert_item_db (sqlite3*     db,
                                  KatzeItem*   item,
                                  const gchar* folder);
 
-void
-midori_bookmarks_remove_item_from_db (sqlite3*   db,
-                                      KatzeItem* item);
-
 void
 midori_bookmarks_import_array_db (sqlite3*       db,
                                   KatzeArray*    array,