[epiphany/wip/bookmarks-import-export: 9/10] bookmarks: Add option to import bookmarks from .gvdb file



commit bab8f892698f0290af3d726e9a8ba858aff0a385
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

 src/Makefile.am                        |    2 +
 src/bookmarks/ephy-bookmarks-import.c  |  118 ++++++++++++++++++++++++++++++++
 src/bookmarks/ephy-bookmarks-import.h  |   30 ++++++++
 src/bookmarks/ephy-bookmarks-manager.c |   74 +-------------------
 src/window-commands.c                  |   94 +++++++++++++++++++++++--
 5 files changed, 239 insertions(+), 79 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..e6c5c7e
--- /dev/null
+++ b/src/bookmarks/ephy-bookmarks-import.c
@@ -0,0 +1,118 @@
+/* -*- 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 "gvdb-builder.h"
+#include "gvdb-reader.h"
+
+gboolean
+ephy_bookmarks_import (EphyBookmarksManager *manager,
+                       const char           *filename)
+{
+  GvdbTable *root_table = NULL;
+  GvdbTable *table = NULL;
+  GSequence *bookmarks = NULL;
+  char **list = NULL;
+  int length;
+  int i;
+  gboolean res = TRUE;
+
+  /* Create a new table to hold data stored in file. */
+  root_table = gvdb_table_new (filename, TRUE, NULL);
+  if (!root_table) {
+    res = FALSE;
+    goto exit;
+  }
+
+  /* Add tags to the bookmark manager's sequence. */
+  table = gvdb_table_get_table (root_table, "tags");
+  if (!table) {
+    res = FALSE;
+    goto exit;
+  }
+
+  /* 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) {
+    res = FALSE;
+    goto exit;
+  }
+
+  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 (manager, bookmarks);
+
+  exit:
+    g_strfreev (list);
+    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..753ce58
--- /dev/null
+++ b/src/bookmarks/ephy-bookmarks-import.h
@@ -0,0 +1,30 @@
+/* -*- 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);
+
+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..8e5a4d3 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);
 }
 
 void
diff --git a/src/window-commands.c b/src/window-commands.c
index 28aca78..4c33a95 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-manager.h"
+#include "ephy-bookmarks-import.h"
 #include "ephy-debug.h"
 #include "ephy-embed-container.h"
 #include "ephy-embed-prefs.h"
@@ -92,13 +94,13 @@ window_cmd_new_incognito_window (GSimpleAction *action,
   ephy_open_incognito_window (NULL);
 }
 
+const gchar *option_names[1] = {
+    N_("Import from .gvdb file")
+  };
+
 static GtkTreeModel *
 create_tree_model (void)
 {
-  const gchar *option_names[1] = {
-    N_("Import from file")
-  };
-
   enum {
     TEXT_COL
   };
@@ -118,6 +120,74 @@ 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;
+  GtkWidget *suggested;
+  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", GTK_WINDOW (dialog),
+                                          "title", _("Choose File"),
+                                          NULL);
+      gtk_dialog_add_buttons (GTK_DIALOG (file_chooser_dialog),
+                              _("Cancel"), GTK_RESPONSE_CANCEL,
+                              _("Import"), GTK_RESPONSE_OK,
+                              NULL);
+      gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_OK);
+
+      suggested = gtk_dialog_get_widget_for_response (GTK_DIALOG (file_chooser_dialog), GTK_RESPONSE_OK);
+      gtk_style_context_add_class (gtk_widget_get_style_context (suggested),
+                                   GTK_STYLE_CLASS_SUGGESTED_ACTION);
+
+      chooser_response = gtk_dialog_run (GTK_DIALOG (file_chooser_dialog));
+      if (chooser_response == GTK_RESPONSE_OK) {
+        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);
+        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,
+                                                     imported ? "Bookmarks successfully imported!" :
+                                                                "There was an error importing bookmarks!");
+        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,
@@ -127,21 +197,25 @@ window_cmd_import_bookmarks (GSimpleAction *action,
   GtkWidget *dialog;
   GtkWidget *content_area;
   GtkWidget *combo_box;
+  GtkWidget *suggested;
   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,
                                         _("_Cancel"),
                                         GTK_RESPONSE_CANCEL,
-                                        _("_Import"),
+                                        _("_Choose File"),
                                         GTK_RESPONSE_OK,
                                         NULL);
 
+  suggested = gtk_dialog_get_widget_for_response (GTK_DIALOG (dialog),
+                                                  GTK_RESPONSE_OK);
+  gtk_style_context_add_class (gtk_widget_get_style_context (suggested),
+                               GTK_STYLE_CLASS_SUGGESTED_ACTION);
+
   content_area = gtk_dialog_get_content_area (GTK_DIALOG (dialog));
-  gtk_container_set_border_width (GTK_CONTAINER (content_area), 5);
 
   tree_model = create_tree_model ();
   combo_box = gtk_combo_box_new_with_model (GTK_TREE_MODEL (tree_model));
@@ -154,7 +228,13 @@ window_cmd_import_bookmarks (GSimpleAction *action,
                                   "text", 0, NULL);
   gtk_container_add (GTK_CONTAINER (content_area), combo_box);
 
+  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);
+  gtk_container_set_border_width (GTK_CONTAINER (content_area), 25);
 }
 
 void


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]