Import bookmarks from bookmarks.xbel on the first start.

The bookmark handling in main is not needed any more.
This commit is contained in:
Alexander Butenko 2010-06-13 15:40:28 -04:00 committed by Christian Dywan
parent 9bff58199a
commit 6f415c4e6e
3 changed files with 150 additions and 305 deletions

View file

@ -452,6 +452,24 @@ midori_bookmarks_initialize (KatzeArray* array,
return NULL; return NULL;
return db; return db;
} }
static void
midori_bookmarks_import (const gchar* filename,
sqlite3* db)
{
KatzeArray* bookmarks;
GError* error = NULL;
bookmarks = katze_array_new (KATZE_TYPE_ARRAY);
if (!midori_array_from_file (bookmarks, filename, "xbel", &error))
{
g_warning (_("The bookmarks couldn't be saved. %s"), error->message);
g_error_free (error);
return;
}
midori_bookmarks_import_array_db (bookmarks, db);
}
#endif #endif
static void static void
@ -523,105 +541,6 @@ midori_search_engines_modify_cb (KatzeArray* array,
g_free (config_file); g_free (config_file);
} }
static void
midori_bookmarks_notify_item_cb (KatzeArray* folder,
GParamSpec* pspec,
KatzeArray* bookmarks)
{
gchar* config_file;
GError* error;
config_file = build_config_filename (BOOKMARK_FILE);
error = NULL;
if (!midori_array_to_file (bookmarks, config_file, "xbel", &error))
{
g_warning (_("The bookmarks couldn't be saved. %s"), error->message);
g_error_free (error);
}
g_free (config_file);
}
static void
midori_bookmarks_add_item_cb (KatzeArray* folder,
KatzeItem* item,
KatzeArray* bookmarks);
static void
midori_bookmarks_remove_item_cb (KatzeArray* folder,
GObject* item,
KatzeArray* bookmarks);
static void
midori_bookmarks_connect_item (KatzeArray* folder,
KatzeItem* item,
KatzeArray* bookmarks)
{
if (KATZE_IS_ARRAY (item))
{
KatzeItem* child;
guint i = 0;
while ((child = katze_array_get_nth_item ((KatzeArray*)item, i++)))
midori_bookmarks_connect_item ((KatzeArray*)item, child, bookmarks);
g_signal_connect_after (item, "add-item",
G_CALLBACK (midori_bookmarks_add_item_cb), bookmarks);
g_signal_connect_after (item, "remove-item",
G_CALLBACK (midori_bookmarks_remove_item_cb), bookmarks);
}
g_signal_connect_after (item, "notify",
G_CALLBACK (midori_bookmarks_notify_item_cb), bookmarks);
}
static void
midori_bookmarks_add_item_cb (KatzeArray* folder,
KatzeItem* item,
KatzeArray* bookmarks)
{
gchar* config_file;
GError* error;
config_file = build_config_filename (BOOKMARK_FILE);
error = NULL;
if (!midori_array_to_file (bookmarks, config_file, "xbel", &error))
{
g_warning (_("The bookmarks couldn't be saved. %s"), error->message);
g_error_free (error);
}
g_free (config_file);
midori_bookmarks_connect_item (folder, item, bookmarks);
}
static void
midori_bookmarks_remove_item_cb (KatzeArray* folder,
GObject* item,
KatzeArray* bookmarks)
{
gchar* config_file;
GError* error;
config_file = build_config_filename (BOOKMARK_FILE);
error = NULL;
if (!midori_array_to_file (bookmarks, config_file, "xbel", &error))
{
g_warning (_("The bookmarks couldn't be saved. %s"), error->message);
g_error_free (error);
}
g_free (config_file);
if (KATZE_IS_ARRAY (item))
{
g_signal_handlers_disconnect_by_func (item,
midori_bookmarks_add_item_cb, bookmarks);
g_signal_handlers_disconnect_by_func (item,
midori_bookmarks_remove_item_cb, bookmarks);
}
g_signal_handlers_disconnect_by_func (item,
midori_bookmarks_notify_item_cb, bookmarks);
}
static void static void
midori_trash_add_item_cb (KatzeArray* trash, midori_trash_add_item_cb (KatzeArray* trash,
GObject* item) GObject* item)
@ -1958,7 +1877,16 @@ main (int argc,
g_free (errmsg); g_free (errmsg);
} }
else else
{
gchar* old_bookmarks = build_config_filename (BOOKMARK_FILE);
if (g_access (old_bookmarks, F_OK) == 0)
{
midori_bookmarks_import (old_bookmarks, db);
g_unlink(old_bookmarks);
}
g_free (old_bookmarks);
g_object_set_data (G_OBJECT (bookmarks), "db", db); g_object_set_data (G_OBJECT (bookmarks), "db", db);
}
#endif #endif
midori_startup_timer ("Bookmarks read: \t%f"); midori_startup_timer ("Bookmarks read: \t%f");
@ -2089,16 +2017,6 @@ main (int argc,
G_CALLBACK (midori_search_engines_modify_cb), search_engines); G_CALLBACK (midori_search_engines_modify_cb), search_engines);
} }
} }
katze_assign (config_file, build_config_filename (BOOKMARK_FILE));
/* Don't save bookmarks if they are not our own */
if (!g_path_is_absolute (BOOKMARK_FILE))
{
midori_bookmarks_connect_item (NULL, (KatzeItem*)bookmarks, bookmarks);
g_signal_connect_after (bookmarks, "add-item",
G_CALLBACK (midori_bookmarks_add_item_cb), bookmarks);
g_signal_connect_after (bookmarks, "remove-item",
G_CALLBACK (midori_bookmarks_remove_item_cb), bookmarks);
}
katze_assign (config_file, build_config_filename ("tabtrash.xbel")); katze_assign (config_file, build_config_filename ("tabtrash.xbel"));
g_signal_connect_after (trash, "add-item", g_signal_connect_after (trash, "add-item",
G_CALLBACK (midori_trash_add_item_cb), NULL); G_CALLBACK (midori_trash_add_item_cb), NULL);

View file

@ -22,14 +22,8 @@
#include <glib/gi18n.h> #include <glib/gi18n.h>
#include <string.h> #include <string.h>
#include <katze/katze.h>
#include <gdk/gdkkeysyms.h> #include <gdk/gdkkeysyms.h>
#include "config.h"
#if HAVE_SQLITE
#include <sqlite3.h>
#endif
void void
midori_browser_edit_bookmark_dialog_new (MidoriBrowser* browser, midori_browser_edit_bookmark_dialog_new (MidoriBrowser* browser,
KatzeItem* bookmark, KatzeItem* bookmark,
@ -122,6 +116,25 @@ midori_bookmarks_get_stock_id (MidoriViewable* viewable)
} }
#if HAVE_SQLITE #if HAVE_SQLITE
void
midori_bookmarks_import_array_db (KatzeArray* array,
sqlite3* db)
{
GList* list = NULL;
GList* bookmarks;
bookmarks = katze_array_get_items ((KatzeArray*)array);
for (list = bookmarks; list != NULL; list = g_list_next (list))
{
KatzeItem* item;
if (KATZE_IS_ARRAY (list->data))
midori_bookmarks_import_array_db (list->data, db);
item = (KatzeItem*) list->data;
midori_bookmarks_insert_item_db (db, item);
}
}
static gboolean static gboolean
midori_bookmarks_read_from_db (MidoriBookmarks* bookmarks, midori_bookmarks_read_from_db (MidoriBookmarks* bookmarks,
GtkTreeStore* model, GtkTreeStore* model,
@ -158,7 +171,6 @@ midori_bookmarks_read_from_db (MidoriBookmarks* bookmarks,
type = sqlite3_column_int64 (statement, 2); type = sqlite3_column_int64 (statement, 2);
item = katze_item_new (); item = katze_item_new ();
katze_item_set_uri (item, (gchar*)uri);
katze_item_set_name (item, (gchar*)title); katze_item_set_name (item, (gchar*)title);
/* type 0 -- folder, 1 -- entry */ /* type 0 -- folder, 1 -- entry */
@ -175,6 +187,7 @@ midori_bookmarks_read_from_db (MidoriBookmarks* bookmarks,
if (!uri) if (!uri)
continue; continue;
katze_item_set_uri (item, (gchar*)uri);
gtk_tree_store_insert_with_values (model, NULL, parent, gtk_tree_store_insert_with_values (model, NULL, parent,
0, 0, item, -1); 0, 0, item, -1);
} }
@ -187,6 +200,73 @@ midori_bookmarks_read_from_db (MidoriBookmarks* bookmarks,
sqlite3_finalize (statement); sqlite3_finalize (statement);
return FALSE; return FALSE;
} }
void
midori_bookmarks_insert_item_db (sqlite3* db,
KatzeItem* item)
{
gchar* sqlcmd;
char* errmsg = NULL;
gint64 type;
gchar* parent;
if (katze_item_get_uri (item))
type = 1;
else
type = 0;
if (katze_item_get_name (katze_item_get_parent (item)))
parent = g_strdup (katze_item_get_name (katze_item_get_parent (item)));
else
parent = g_strdup ("");
sqlcmd = sqlite3_mprintf (
"INSERT into bookmarks (uri, title, folder, type) values"
" ('%q', '%q', '%q', %u)",
katze_item_get_uri (item),
katze_item_get_name (item),
parent,
type);
if (sqlite3_exec (db, sqlcmd, NULL, NULL, &errmsg) != SQLITE_OK)
{
g_printerr (_("Failed to remove history item: %s\n"), errmsg);
sqlite3_free (errmsg);
}
g_free (parent);
sqlite3_free (sqlcmd);
}
static void
midori_bookmarks_remove_item_from_db (MidoriBookmarks* bookmarks,
KatzeItem* item)
{
gchar* sqlcmd;
sqlite3* db;
char* errmsg = NULL;
db = g_object_get_data (G_OBJECT (bookmarks->array), "db");
if (katze_item_get_uri (item))
sqlcmd = sqlite3_mprintf (
"DELETE FROM bookmarks WHERE uri = '%q' AND"
" title = '%q'",
katze_item_get_uri (item),
katze_item_get_name (item));
else
sqlcmd = sqlite3_mprintf (
"DELETE FROM bookmarks WHERE folder = '%q'", katze_item_get_name (item));
if (sqlite3_exec (db, sqlcmd, NULL, NULL, &errmsg) != SQLITE_OK)
{
g_printerr (_("Failed to remove history item: %s\n"), errmsg);
sqlite3_free (errmsg);
}
g_debug ("%s", sqlcmd);
sqlite3_free (sqlcmd);
}
#endif #endif
static void static void
@ -234,15 +314,13 @@ midori_bookmarks_delete_clicked_cb (GtkWidget* toolitem,
&model, &iter)) &model, &iter))
{ {
KatzeItem* item; KatzeItem* item;
KatzeArray* parent;
gtk_tree_model_get (model, &iter, 0, &item, -1); gtk_tree_model_get (model, &iter, 0, &item, -1);
/* FIXME: Even toplevel items should technically have a parent */ #if HAVE_SQLITE
g_return_if_fail (katze_item_get_parent (item)); midori_bookmarks_remove_item_from_db (bookmarks, item);
#endif
parent = katze_item_get_parent (item); gtk_tree_store_remove (GTK_TREE_STORE (model), &iter);
katze_array_remove_item (parent, item);
g_object_unref (item); g_object_unref (item);
} }
@ -273,8 +351,9 @@ midori_bookmarks_cursor_or_row_changed_cb (GtkTreeView* treeview,
gboolean is_separator; gboolean is_separator;
gtk_tree_model_get (model, &iter, 0, &item, -1); gtk_tree_model_get (model, &iter, 0, &item, -1);
if (!item)
is_separator = !KATZE_IS_ARRAY (item) && !katze_item_get_uri (item); return;
is_separator = !katze_item_get_uri (item);
gtk_widget_set_sensitive (bookmarks->edit, !is_separator); gtk_widget_set_sensitive (bookmarks->edit, !is_separator);
gtk_widget_set_sensitive (bookmarks->delete, TRUE); gtk_widget_set_sensitive (bookmarks->delete, TRUE);
@ -356,175 +435,6 @@ midori_bookmarks_viewable_iface_init (MidoriViewableIface* iface)
iface->get_toolbar = midori_bookmarks_get_toolbar; iface->get_toolbar = midori_bookmarks_get_toolbar;
} }
static void
midori_bookmarks_add_item_cb (KatzeArray* array,
KatzeItem* added_item,
MidoriBookmarks* bookmarks);
static void
midori_bookmarks_remove_item_cb (KatzeArray* array,
KatzeItem* removed_item,
MidoriBookmarks* bookmarks);
static void
midori_bookmarks_clear_cb (KatzeArray* array,
MidoriBookmarks* bookmarks);
static void
midori_bookmarks_add_item_cb (KatzeArray* array,
KatzeItem* added_item,
MidoriBookmarks* bookmarks)
{
GtkTreeModel* model;
GtkTreeIter iter;
guint i;
g_return_if_fail (KATZE_IS_ARRAY (array));
g_return_if_fail (KATZE_IS_ITEM (added_item));
if (KATZE_IS_ARRAY (added_item))
{
g_signal_connect (added_item, "add-item",
G_CALLBACK (midori_bookmarks_add_item_cb), bookmarks);
g_signal_connect (added_item, "remove-item",
G_CALLBACK (midori_bookmarks_remove_item_cb), bookmarks);
g_signal_connect (added_item, "clear",
G_CALLBACK (midori_bookmarks_clear_cb), bookmarks);
}
g_object_ref (added_item);
model = gtk_tree_view_get_model (GTK_TREE_VIEW (bookmarks->treeview));
if (array == bookmarks->array)
{
gtk_tree_store_insert_with_values (GTK_TREE_STORE (model),
&iter, NULL, G_MAXINT, 0, added_item, -1);
return;
}
i = 0;
/* FIXME: Recurse over children of folders, too */
while (gtk_tree_model_iter_nth_child (model, &iter, NULL, i))
{
KatzeItem* item;
gtk_tree_model_get (model, &iter, 0, &item, -1);
if (item == (KatzeItem*)array)
{
GtkTreeIter child_iter;
gtk_tree_store_insert_with_values (GTK_TREE_STORE (model),
&child_iter, &iter, G_MAXINT, 0, added_item, -1);
break;
}
g_object_unref (item);
i++;
}
}
static void
midori_bookmarks_remove_iter (GtkTreeModel* model,
GtkTreeIter* parent,
KatzeItem* removed_item)
{
guint i;
GtkTreeIter iter;
i = 0;
while (gtk_tree_model_iter_nth_child (model, &iter, parent, i))
{
KatzeItem* item;
gtk_tree_model_get (model, &iter, 0, &item, -1);
if (item == removed_item)
{
gtk_tree_store_remove (GTK_TREE_STORE (model), &iter);
g_object_unref (item);
break;
}
if (KATZE_IS_ARRAY (item))
midori_bookmarks_remove_iter (model, &iter, removed_item);
g_object_unref (item);
i++;
}
}
static void
midori_bookmarks_remove_item_cb (KatzeArray* array,
KatzeItem* removed_item,
MidoriBookmarks* bookmarks)
{
GtkTreeModel* model;
g_return_if_fail (KATZE_IS_ARRAY (array));
g_return_if_fail (KATZE_IS_ITEM (removed_item));
model = gtk_tree_view_get_model (GTK_TREE_VIEW (bookmarks->treeview));
midori_bookmarks_remove_iter (model, NULL, removed_item);
g_object_unref (removed_item);
}
static void
midori_bookmarks_clear_cb (KatzeArray* array,
MidoriBookmarks* bookmarks)
{
GtkTreeView* treeview;
GtkTreeStore* store;
g_return_if_fail (KATZE_IS_ARRAY (array));
if (array == bookmarks->array)
{
treeview = GTK_TREE_VIEW (bookmarks->treeview);
store = GTK_TREE_STORE (gtk_tree_view_get_model (treeview));
gtk_tree_store_clear (store);
}
else
{
KatzeItem* item;
guint i;
i = 0;
while ((item = katze_array_get_nth_item (array, i++)))
midori_bookmarks_remove_item_cb (array, item, bookmarks);
}
}
static void
midori_bookmarks_insert_folder (MidoriBookmarks* bookmarks,
GtkTreeStore* treestore,
GtkTreeIter* parent,
KatzeArray* array)
{
KatzeItem* item;
guint i;
GtkTreeIter iter;
g_return_if_fail (KATZE_IS_ARRAY (array));
g_signal_connect (array, "add-item",
G_CALLBACK (midori_bookmarks_add_item_cb), bookmarks);
g_signal_connect (array, "remove-item",
G_CALLBACK (midori_bookmarks_remove_item_cb), bookmarks);
g_signal_connect (array, "clear",
G_CALLBACK (midori_bookmarks_clear_cb), bookmarks);
i = 0;
while ((item = katze_array_get_nth_item (array, i++)))
{
g_object_ref (item);
gtk_tree_store_insert_with_values (treestore, &iter, parent, i,
0, item, -1);
if (KATZE_IS_ARRAY (item))
midori_bookmarks_insert_folder (bookmarks, treestore,
&iter, KATZE_ARRAY (item));
}
}
static void static void
midori_bookmarks_set_app (MidoriBookmarks* bookmarks, midori_bookmarks_set_app (MidoriBookmarks* bookmarks,
MidoriApp* app) MidoriApp* app)
@ -601,17 +511,18 @@ midori_bookmarks_treeview_render_icon_cb (GtkTreeViewColumn* column,
/* TODO: Would it be better to not do this on every redraw? */ /* TODO: Would it be better to not do this on every redraw? */
pixbuf = NULL; pixbuf = NULL;
if (KATZE_IS_ARRAY (item)) if (item && !katze_item_get_uri (item) && katze_item_get_name (item))
pixbuf = gtk_widget_render_icon (treeview, GTK_STOCK_DIRECTORY, pixbuf = gtk_widget_render_icon (treeview, GTK_STOCK_DIRECTORY,
GTK_ICON_SIZE_MENU, NULL); GTK_ICON_SIZE_MENU, NULL);
else if (katze_item_get_uri (item)) else if (item && katze_item_get_uri (item))
pixbuf = katze_load_cached_icon (katze_item_get_uri (item), treeview); pixbuf = katze_load_cached_icon (katze_item_get_uri (item), treeview);
g_object_set (renderer, "pixbuf", pixbuf, NULL); g_object_set (renderer, "pixbuf", pixbuf, NULL);
if (pixbuf) if (pixbuf)
g_object_unref (pixbuf); g_object_unref (pixbuf);
g_object_unref (item); if (item)
g_object_unref (item);
} }
static void static void
@ -625,13 +536,14 @@ midori_bookmarks_treeview_render_text_cb (GtkTreeViewColumn* column,
gtk_tree_model_get (model, iter, 0, &item, -1); gtk_tree_model_get (model, iter, 0, &item, -1);
if (KATZE_IS_ARRAY (item) || katze_item_get_uri (item)) if (item && katze_item_get_name (item))
g_object_set (renderer, "markup", NULL, g_object_set (renderer, "markup", NULL,
"text", katze_item_get_name (item), NULL); "text", katze_item_get_name (item), NULL);
else else
g_object_set (renderer, "markup", _("<i>Separator</i>"), NULL); g_object_set (renderer, "markup", _("<i>Separator</i>"), NULL);
g_object_unref (item); if (item)
g_object_unref (item);
} }
static void static void
@ -793,15 +705,12 @@ midori_bookmarks_delete_activate_cb (GtkWidget* menuitem,
MidoriBookmarks* bookmarks) MidoriBookmarks* bookmarks)
{ {
KatzeItem* item; KatzeItem* item;
KatzeArray* parent;
item = (KatzeItem*)g_object_get_data (G_OBJECT (menuitem), "KatzeItem"); item = (KatzeItem*)g_object_get_data (G_OBJECT (menuitem), "KatzeItem");
#if HAVE_SQLITE
/* FIXME: Even toplevel items should technically have a parent */ midori_bookmarks_remove_item_from_db (bookmarks, item);
g_return_if_fail (katze_item_get_parent (item)); #endif
/* FIXME: Refresh menu */
parent = katze_item_get_parent (item);
katze_array_remove_item (parent, item);
} }
static void static void
@ -931,8 +840,10 @@ midori_bookmarks_row_expanded_cb (GtkTreeView* treeview,
model = gtk_tree_view_get_model (GTK_TREE_VIEW (treeview)); model = gtk_tree_view_get_model (GTK_TREE_VIEW (treeview));
gtk_tree_model_get (model, iter, 0, &item, -1); gtk_tree_model_get (model, iter, 0, &item, -1);
#if HAVE_SQLITE
midori_bookmarks_read_from_db (bookmarks, GTK_TREE_STORE (model), midori_bookmarks_read_from_db (bookmarks, GTK_TREE_STORE (model),
iter, katze_item_get_name (item)); iter, katze_item_get_name (item));
#endif
g_object_unref (item); g_object_unref (item);
} }

View file

@ -12,7 +12,14 @@
#ifndef __MIDORI_BOOKMARKS_H__ #ifndef __MIDORI_BOOKMARKS_H__
#define __MIDORI_BOOKMARKS_H__ #define __MIDORI_BOOKMARKS_H__
#include "config.h"
#if HAVE_SQLITE
#include <sqlite3.h>
#endif
#include <gtk/gtk.h> #include <gtk/gtk.h>
#include <katze/katze.h>
G_BEGIN_DECLS G_BEGIN_DECLS
@ -38,6 +45,15 @@ midori_bookmarks_get_type (void);
GtkWidget* GtkWidget*
midori_bookmarks_new (void); midori_bookmarks_new (void);
#if HAVE_SQLITE
void
midori_bookmarks_insert_item_db (sqlite3* db,
KatzeItem* item);
void
midori_bookmarks_import_array_db (KatzeArray* array,
sqlite3* db);
#endif
G_END_DECLS G_END_DECLS
#endif /* __MIDORI_BOOKMARKS_H__ */ #endif /* __MIDORI_BOOKMARKS_H__ */