[epiphany] bookmarks: Add option to import bookmarks from .gvdb file
- From: Iulian Radu <iulianradu src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [epiphany] bookmarks: Add option to import bookmarks from .gvdb file
- Date: Wed, 30 Nov 2016 13:30:33 +0000 (UTC)
commit d46f782ded6689b7a3756445e0a08edf38fd0d5d
Author: Iulian Radu <iulian radu67 gmail com>
Date: Mon Nov 28 14:07:32 2016 +0200
bookmarks: Add option to import bookmarks from .gvdb file
https://bugzilla.gnome.org/show_bug.cgi?id=772423
src/Makefile.am | 2 +
src/bookmarks/ephy-bookmarks-import.c | 154 ++++++++++++++++++++++++++++++++
src/bookmarks/ephy-bookmarks-import.h | 31 +++++++
src/bookmarks/ephy-bookmarks-manager.c | 74 +---------------
src/window-commands.c | 75 +++++++++++++++-
5 files changed, 263 insertions(+), 73 deletions(-)
---
diff --git a/src/Makefile.am b/src/Makefile.am
index 0ef00c0..407eb1e 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -30,6 +30,8 @@ libephymain_la_SOURCES = \
bookmarks/ephy-bookmark-properties-grid.h \
bookmarks/ephy-bookmark-row.c \
bookmarks/ephy-bookmark-row.h \
+ bookmarks/ephy-bookmarks-import.c \
+ bookmarks/ephy-bookmarks-import.h \
bookmarks/ephy-bookmarks-list-model.c \
bookmarks/ephy-bookmarks-list-model.h \
bookmarks/ephy-bookmarks-manager.c \
diff --git a/src/bookmarks/ephy-bookmarks-import.c b/src/bookmarks/ephy-bookmarks-import.c
new file mode 100644
index 0000000..aa00872
--- /dev/null
+++ b/src/bookmarks/ephy-bookmarks-import.c
@@ -0,0 +1,154 @@
+/* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/*
+ * Copyright © 2016 Iulian-Gabriel Radu <iulian radu67 gnome org>
+ *
+ * This file is part of Epiphany.
+ *
+ * Epiphany is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Epiphany is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Epiphany. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "ephy-bookmarks-import.h"
+
+#include "config.h"
+
+#include "gvdb-builder.h"
+#include "gvdb-reader.h"
+
+#include <glib/gi18n.h>
+
+GQuark bookmarks_import_error_quark (void);
+G_DEFINE_QUARK (bookmarks-import-error-quark, bookmarks_import_error)
+#define BOOKMARKS_IMPORT_ERROR bookmarks_import_error_quark ()
+
+typedef enum {
+ BOOKMARKS_IMPORT_ERROR_TAGS = 1001,
+ BOOKMARKS_IMPORT_ERROR_BOOKMARKS = 1002
+} BookmarksImportErrorCode;
+
+static GSequence *
+get_bookmarks_from_table (GvdbTable *table)
+{
+ GSequence *bookmarks = NULL;
+ char **list = NULL;
+ int length;
+ int i;
+
+ bookmarks = g_sequence_new (g_object_unref);
+
+ /* Iterate over all keys (url's) in the table. */
+ list = gvdb_table_get_names (table, &length);
+ for (i = 0; i < length; i++) {
+ EphyBookmark *bookmark;
+ GVariant *value;
+ GVariantIter *iter;
+ GSequence *tags;
+ char *tag;
+ const char *title;
+ gint64 time_added;
+ char *id;
+ double modified;
+ gboolean uploaded;
+
+ /* Obtain the correspoding GVariant. */
+ value = gvdb_table_get_value (table, list[i]);
+
+ g_variant_get (value, "(x&s&sdbas)", &time_added, &title, &id, &modified, &uploaded, &iter);
+
+ /* Add all stored tags in a GSequence. */
+ tags = g_sequence_new (g_free);
+ while (g_variant_iter_next (iter, "s", &tag)) {
+ g_sequence_insert_sorted (tags, tag,
+ (GCompareDataFunc)ephy_bookmark_tags_compare,
+ NULL);
+ }
+ g_variant_iter_free (iter);
+
+ /* Create the new bookmark. */
+ bookmark = ephy_bookmark_new (list[i], title, tags);
+ ephy_bookmark_set_time_added (bookmark, time_added);
+ ephy_bookmark_set_id (bookmark, id);
+ ephy_bookmark_set_modification_time (bookmark, modified);
+ ephy_bookmark_set_is_uploaded (bookmark, uploaded);
+ g_sequence_prepend (bookmarks, bookmark);
+
+ g_variant_unref (value);
+ }
+
+ g_strfreev (list);
+
+ return bookmarks;
+}
+
+gboolean
+ephy_bookmarks_import (EphyBookmarksManager *manager,
+ const char *filename,
+ GError **error)
+{
+ GvdbTable *root_table = NULL;
+ GvdbTable *table = NULL;
+ GSequence *bookmarks = NULL;
+ char **list = NULL;
+ gboolean res = TRUE;
+ int length;
+ int i;
+
+ /* Create a new table to hold data stored in file. */
+ root_table = gvdb_table_new (filename, TRUE, error);
+ if (!root_table) {
+ res = FALSE;
+ goto out;
+ }
+
+ /* Add tags to the bookmark manager's sequence. */
+ table = gvdb_table_get_table (root_table, "tags");
+ if (!table) {
+ g_set_error (error,
+ BOOKMARKS_IMPORT_ERROR,
+ BOOKMARKS_IMPORT_ERROR_TAGS,
+ _("File is not a valid Epiphany bookmarks file: missing tags table"));
+ res = FALSE;
+ goto out;
+ }
+
+ /* Iterate over all keys (url's) in the table. */
+ list = gvdb_table_get_names (table, &length);
+ for (i = 0; i < length; i++)
+ ephy_bookmarks_manager_create_tag (manager, list[i]);
+ g_strfreev (list);
+ gvdb_table_free (table);
+
+ /* Get bookmarks table */
+ table = gvdb_table_get_table (root_table, "bookmarks");
+ if (!table) {
+ g_set_error (error,
+ BOOKMARKS_IMPORT_ERROR,
+ BOOKMARKS_IMPORT_ERROR_BOOKMARKS,
+ _("File is not a valid Epiphany bookmarks file: missing bookmarks table"));
+ res = FALSE;
+ goto out;
+ }
+
+ bookmarks = get_bookmarks_from_table (table);
+ ephy_bookmarks_manager_add_bookmarks (manager, bookmarks);
+
+ out:
+ if (table)
+ gvdb_table_free (table);
+ if (bookmarks)
+ g_sequence_free (bookmarks);
+ if (root_table)
+ gvdb_table_free (root_table);
+
+ return res;
+}
diff --git a/src/bookmarks/ephy-bookmarks-import.h b/src/bookmarks/ephy-bookmarks-import.h
new file mode 100644
index 0000000..3f23ffd
--- /dev/null
+++ b/src/bookmarks/ephy-bookmarks-import.h
@@ -0,0 +1,31 @@
+/* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/*
+ * Copyright © 2016 Iulian-Gabriel Radu <iulian radu67 gnome org>
+ *
+ * This file is part of Epiphany.
+ *
+ * Epiphany is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Epiphany is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Epiphany. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#pragma once
+
+#include "ephy-bookmarks-manager.h"
+
+G_BEGIN_DECLS
+
+gboolean ephy_bookmarks_import (EphyBookmarksManager *manager,
+ const char *filename,
+ GError **error);
+
+G_END_DECLS
\ No newline at end of file
diff --git a/src/bookmarks/ephy-bookmarks-manager.c b/src/bookmarks/ephy-bookmarks-manager.c
index bda3f4a..0cea07a 100644
--- a/src/bookmarks/ephy-bookmarks-manager.c
+++ b/src/bookmarks/ephy-bookmarks-manager.c
@@ -22,6 +22,7 @@
#include "ephy-bookmarks-manager.h"
+#include "ephy-bookmarks-import.h"
#include "ephy-debug.h"
#include "ephy-file-helpers.h"
#include "gvdb-builder.h"
@@ -560,78 +561,7 @@ ephy_bookmarks_manager_save_to_file_finish (EphyBookmarksManager *self,
void
ephy_bookmarks_manager_load_from_file (EphyBookmarksManager *self)
{
- GvdbTable *root_table;
- GvdbTable *table;
- GSequence *bookmarks;
- char **list;
- int length;
- int i;
-
- /* Create a new table to hold data stored in file. */
- root_table = gvdb_table_new (self->gvdb_filename, TRUE, NULL);
- g_assert (root_table);
-
- /* Add tags to the bookmark manager's sequence. */
- table = gvdb_table_get_table (root_table, "tags");
- g_assert (table);
-
- /* Iterate over all keys (url's) in the table. */
- list = gvdb_table_get_names (table, &length);
- for (i = 0; i < length; i++)
- ephy_bookmarks_manager_create_tag (self, list[i]);
- g_strfreev (list);
- gvdb_table_free (table);
-
- /* Get bookmarks table */
- table = gvdb_table_get_table (root_table, "bookmarks");
- g_assert (table);
-
- bookmarks = g_sequence_new (g_object_unref);
-
- /* Iterate over all keys (url's) in the table. */
- list = gvdb_table_get_names (table, &length);
- for (i = 0; i < length; i++) {
- EphyBookmark *bookmark;
- GVariant *value;
- GVariantIter *iter;
- GSequence *tags;
- char *tag;
- const char *title;
- gint64 time_added;
- char *id;
- double modified;
- gboolean uploaded;
-
- /* Obtain the correspoding GVariant. */
- value = gvdb_table_get_value (table, list[i]);
-
- g_variant_get (value, "(x&s&sdbas)", &time_added, &title, &id, &modified, &uploaded, &iter);
-
- /* Add all stored tags in a GSequence. */
- tags = g_sequence_new (g_free);
- while (g_variant_iter_next (iter, "s", &tag)) {
- g_sequence_insert_sorted (tags, tag,
- (GCompareDataFunc)ephy_bookmark_tags_compare,
- NULL);
- }
- g_variant_iter_free (iter);
-
- /* Create the new bookmark. */
- bookmark = ephy_bookmark_new (list[i], title, tags);
- ephy_bookmark_set_time_added (bookmark, time_added);
- ephy_bookmark_set_id (bookmark, id);
- ephy_bookmark_set_modification_time (bookmark, modified);
- ephy_bookmark_set_is_uploaded (bookmark, uploaded);
- g_sequence_prepend (bookmarks, bookmark);
-
- g_variant_unref (value);
- }
- ephy_bookmarks_manager_add_bookmarks (self, bookmarks);
-
- g_strfreev (list);
- gvdb_table_free (table);
- g_sequence_free (bookmarks);
- gvdb_table_free (root_table);
+ ephy_bookmarks_import (self, self->gvdb_filename, NULL);
}
void
diff --git a/src/window-commands.c b/src/window-commands.c
index 8744269..60880a9 100644
--- a/src/window-commands.c
+++ b/src/window-commands.c
@@ -26,6 +26,8 @@
#include "window-commands.h"
#include "ephy-add-bookmark-popover.h"
+#include "ephy-bookmarks-import.h"
+#include "ephy-bookmarks-manager.h"
#include "ephy-debug.h"
#include "ephy-embed-container.h"
#include "ephy-embed-prefs.h"
@@ -117,6 +119,73 @@ create_tree_model (void)
return GTK_TREE_MODEL (list_store);
}
+static void
+dialog_bookmarks_import_cb (GtkDialog *dialog,
+ int response,
+ GtkComboBox *combo_box)
+{
+ EphyBookmarksManager *manager = ephy_shell_get_bookmarks_manager (ephy_shell_get_default ());
+ GtkWidget *file_chooser_dialog;
+ GtkWidget *import_info_dialog;
+ int active;
+ int chooser_response;
+ gboolean imported;
+
+ if (response == GTK_RESPONSE_OK) {
+ active = gtk_combo_box_get_active (combo_box);
+ if (active == 0) {
+ GtkFileFilter *filter;
+
+ filter = gtk_file_filter_new ();
+ gtk_file_filter_add_pattern (filter, "*.gvdb");
+
+ file_chooser_dialog = g_object_new (GTK_TYPE_FILE_CHOOSER_DIALOG,
+ "action", GTK_FILE_CHOOSER_ACTION_OPEN,
+ "filter", filter,
+ "modal", TRUE,
+ "show-hidden", TRUE,
+ "transient-for", dialog,
+ "title", _("Choose File"),
+ NULL);
+
+ gtk_dialog_add_buttons (GTK_DIALOG (file_chooser_dialog),
+ _("_Cancel"), GTK_RESPONSE_CANCEL,
+ _("I_mport"), GTK_RESPONSE_OK,
+ NULL);
+ gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_OK);
+
+ chooser_response = gtk_dialog_run (GTK_DIALOG (file_chooser_dialog));
+ if (chooser_response == GTK_RESPONSE_OK) {
+ GError *error = NULL;
+ char *filename;
+
+ gtk_widget_hide (file_chooser_dialog);
+
+ filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (file_chooser_dialog));
+ imported = ephy_bookmarks_import (manager, filename, &error);
+ g_free (filename);
+
+ import_info_dialog = gtk_message_dialog_new (GTK_WINDOW (dialog),
+ GTK_DIALOG_MODAL,
+ imported ? GTK_MESSAGE_INFO : GTK_MESSAGE_WARNING,
+ GTK_BUTTONS_OK,
+ "%s",
+ imported ? _("Bookmarks successfully imported!") :
+ error->message);
+ gtk_dialog_run (GTK_DIALOG (import_info_dialog));
+
+ gtk_widget_destroy (import_info_dialog);
+ }
+ gtk_widget_destroy (file_chooser_dialog);
+
+ if (imported)
+ gtk_widget_destroy (GTK_WIDGET (dialog));
+ }
+ } else if (response == GTK_RESPONSE_CANCEL) {
+ gtk_widget_destroy (GTK_WIDGET (dialog));
+ }
+}
+
void
window_cmd_import_bookmarks (GSimpleAction *action,
GVariant *parameter,
@@ -131,7 +200,6 @@ window_cmd_import_bookmarks (GSimpleAction *action,
GtkTreeModel *tree_model;
GtkCellRenderer *cell_renderer;
- /* Show dialog with icon, title. */
dialog = gtk_dialog_new_with_buttons (_("Import bookmarks"),
GTK_WINDOW (window),
GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT |
GTK_DIALOG_USE_HEADER_BAR,
@@ -166,6 +234,11 @@ window_cmd_import_bookmarks (GSimpleAction *action,
gtk_container_add (GTK_CONTAINER (content_area), hbox);
+ gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_OK);
+ g_signal_connect (dialog, "response",
+ G_CALLBACK (dialog_bookmarks_import_cb),
+ GTK_COMBO_BOX (combo_box));
+
gtk_widget_show_all (dialog);
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]