Add bookmarks through array and update the panel

This commit is contained in:
Christian Dywan 2010-11-21 16:49:20 +01:00
parent 93c483c904
commit 8ad224c9a8
3 changed files with 70 additions and 66 deletions

View file

@ -489,6 +489,51 @@ midori_history_terminate (KatzeArray* array,
sqlite3_close (db); 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 static void
midori_bookmarks_remove_item_cb (KatzeArray* array, midori_bookmarks_remove_item_cb (KatzeArray* array,
KatzeItem* item, KatzeItem* item,
@ -542,6 +587,8 @@ midori_bookmarks_initialize (KatzeArray* array,
"desc text, app integer, toolbar integer);", "desc text, app integer, toolbar integer);",
NULL, NULL, errmsg) != SQLITE_OK) NULL, NULL, errmsg) != SQLITE_OK)
return NULL; return NULL;
g_signal_connect (array, "add-item",
G_CALLBACK (midori_bookmarks_add_item_cb), db);
g_signal_connect (array, "remove-item", g_signal_connect (array, "remove-item",
G_CALLBACK (midori_bookmarks_remove_item_cb), db); G_CALLBACK (midori_bookmarks_remove_item_cb), db);
return db; return db;

View file

@ -178,11 +178,6 @@ midori_bookmarks_export_array_db (sqlite3* db,
KatzeArray* array, KatzeArray* array,
const gchar* folder); const gchar* folder);
void
midori_bookmarks_insert_item_db (sqlite3* db,
KatzeItem* item,
gchar* folder);
void void
midori_browser_open_bookmark (MidoriBrowser* browser, midori_browser_open_bookmark (MidoriBrowser* browser,
KatzeItem* item); KatzeItem* item);
@ -909,8 +904,6 @@ midori_browser_edit_bookmark_dialog_new (MidoriBrowser* browser,
if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT) if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
{ {
gchar* selected; gchar* selected;
GtkTreeView* treeview;
GtkTreeModel* model;
if (!new_bookmark) if (!new_bookmark)
katze_array_remove_item (browser->bookmarks, 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)); selected = gtk_combo_box_get_active_text (GTK_COMBO_BOX (combo_folder));
if (!strcmp (selected, _("Toplevel folder"))) if (!strcmp (selected, _("Toplevel folder")))
selected = g_strdup (""); katze_assign (selected, g_strdup (""));
katze_item_set_meta_string (bookmark, "folder", selected);
midori_bookmarks_insert_item_db (db, bookmark, selected); katze_array_add_item (browser->bookmarks, bookmark);
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);
}
if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (check_toolbar))) if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (check_toolbar)))
if (!gtk_widget_get_visible (browser->bookmarkbar) if (!gtk_widget_get_visible (browser->bookmarkbar))
&& browser->bookmarks != NULL)
_action_set_active (browser, "Bookmarkbar", TRUE); _action_set_active (browser, "Bookmarkbar", TRUE);
g_free (selected); g_free (selected);
return_status = TRUE; return_status = TRUE;

View file

@ -161,7 +161,8 @@ midori_bookmarks_import_array_db (sqlite3* db,
{ {
if (KATZE_IS_ARRAY (item)) if (KATZE_IS_ARRAY (item))
midori_bookmarks_import_array_db (db, KATZE_ARRAY (item), folder); 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); g_object_unref (item);
} }
void static void
midori_bookmarks_insert_item_db (sqlite3* db, midori_bookmarks_add_item_cb (KatzeArray* array,
KatzeItem* item, KatzeItem* item,
const gchar* folder) MidoriBookmarks* bookmarks)
{ {
gchar* sqlcmd; GtkTreeModel* model;
char* errmsg = NULL; model = gtk_tree_view_get_model (GTK_TREE_VIEW (bookmarks->treeview));
KatzeItem* old_parent; if (!strcmp (katze_item_get_meta_string (item, "folder"), ""))
gchar* parent; gtk_tree_store_insert_with_values (GTK_TREE_STORE (model),
gchar* uri; NULL, NULL, G_MAXINT, 0, item, -1);
if (KATZE_ITEM_IS_BOOKMARK (item))
uri = g_strdup (katze_item_get_uri (item));
else 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); gtk_tree_store_clear (GTK_TREE_STORE (model));
sqlite3_free (errmsg); 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 static void
@ -318,7 +291,8 @@ midori_bookmarks_row_changed_cb (GtkTreeModel* model,
parent_name = g_strdup (""); parent_name = g_strdup ("");
katze_array_remove_item (bookmarks->array, item); 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 static void
@ -468,11 +442,11 @@ midori_bookmarks_set_app (MidoriBookmarks* bookmarks,
g_object_ref (app); g_object_ref (app);
bookmarks->array = katze_object_get_object (app, "bookmarks"); 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_signal_connect (bookmarks->array, "remove-item",
G_CALLBACK (midori_bookmarks_remove_item_cb), bookmarks); 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_signal_connect_after (model, "row-changed",
G_CALLBACK (midori_bookmarks_row_changed_cb), G_CALLBACK (midori_bookmarks_row_changed_cb),
bookmarks); bookmarks);