[epiphany/wip/bookmarks-import-export: 4/4] bookmarks: Add option to export bookmarks as .gvdb file



commit a667ed6f751e6811b9bfd198da10791cbbfdbad3
Author: Iulian Radu <iulian radu67 gmail com>
Date:   Mon Nov 28 17:12:25 2016 +0200

    bookmarks: Add option to export bookmarks as .gvdb file

 src/Makefile.am                        |    2 +
 src/bookmarks/ephy-bookmarks-export.c  |   99 ++++++++++++++++++++++++++++++++
 src/bookmarks/ephy-bookmarks-export.h  |   30 ++++++++++
 src/bookmarks/ephy-bookmarks-manager.c |   70 +----------------------
 src/window-commands.c                  |   51 ++++++++++++++++
 5 files changed, 184 insertions(+), 68 deletions(-)
---
diff --git a/src/Makefile.am b/src/Makefile.am
index 9a0acf4..3eaef28 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-export.c       \
+       bookmarks/ephy-bookmarks-export.h       \
        bookmarks/ephy-bookmarks-import.c       \
        bookmarks/ephy-bookmarks-import.h       \
        bookmarks/ephy-bookmarks-list-model.c   \
diff --git a/src/bookmarks/ephy-bookmarks-export.c b/src/bookmarks/ephy-bookmarks-export.c
new file mode 100644
index 0000000..41248da
--- /dev/null
+++ b/src/bookmarks/ephy-bookmarks-export.c
@@ -0,0 +1,99 @@
+/* -*- 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-export.h"
+
+#include "gvdb-builder.h"
+
+static void
+gvdb_hash_table_insert_variant (GHashTable *table,
+                                const char *key,
+                                GVariant   *value)
+{
+  GvdbItem *item;
+
+  item = gvdb_hash_table_insert (table, key);
+  gvdb_item_set_value (item, value);
+}
+
+static GVariant *
+build_variant (EphyBookmark *bookmark)
+{
+  GVariantBuilder builder;
+  GSequence *tags;
+  GSequenceIter *iter;
+
+  g_variant_builder_init (&builder, G_VARIANT_TYPE ("(xssdbas)"));
+
+  g_variant_builder_add (&builder, "x", ephy_bookmark_get_time_added (bookmark));
+  g_variant_builder_add (&builder, "s", ephy_bookmark_get_title (bookmark));
+  g_variant_builder_add (&builder, "s", ephy_bookmark_get_id (bookmark));
+  g_variant_builder_add (&builder, "d", ephy_bookmark_get_modification_time (bookmark));
+  g_variant_builder_add (&builder, "b", ephy_bookmark_is_uploaded (bookmark));
+
+  g_variant_builder_open (&builder, G_VARIANT_TYPE ("as"));
+  tags = ephy_bookmark_get_tags (bookmark);
+  for (iter = g_sequence_get_begin_iter (tags);
+       !g_sequence_iter_is_end (iter);
+       iter = g_sequence_iter_next (iter)) {
+    g_variant_builder_add (&builder, "s", g_sequence_get (iter));
+  }
+  g_variant_builder_close (&builder);
+
+  return g_variant_builder_end (&builder);
+}
+
+static void
+add_bookmark_to_table (EphyBookmark *bookmark, GHashTable *table)
+{
+  gvdb_hash_table_insert_variant (table,
+                                  ephy_bookmark_get_url (bookmark),
+                                  build_variant (bookmark));
+}
+
+static void
+add_tag_to_table (const char *tag, GHashTable *table)
+{
+  gvdb_hash_table_insert (table, tag);
+}
+
+gboolean
+ephy_bookmarks_export (EphyBookmarksManager *manager,
+                       const char           *filename)
+{
+  GHashTable *root_table;
+  GHashTable *table;
+  gboolean result;
+
+  root_table = gvdb_hash_table_new (NULL, NULL);
+
+  table = gvdb_hash_table_new (root_table, "tags");
+  g_sequence_foreach (ephy_bookmarks_manager_get_tags (manager), (GFunc)add_tag_to_table, table);
+  g_hash_table_unref (table);
+
+  table = gvdb_hash_table_new (root_table, "bookmarks");
+  g_sequence_foreach (ephy_bookmarks_manager_get_bookmarks (manager), (GFunc)add_bookmark_to_table, table);
+  g_hash_table_unref (table);
+
+  result = gvdb_table_write_contents (root_table, filename, FALSE, NULL);
+  g_hash_table_unref (root_table);
+
+  return result;
+}
diff --git a/src/bookmarks/ephy-bookmarks-export.h b/src/bookmarks/ephy-bookmarks-export.h
new file mode 100644
index 0000000..bc3c483
--- /dev/null
+++ b/src/bookmarks/ephy-bookmarks-export.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_export       (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 8e5a4d3..9ab1fdb 100644
--- a/src/bookmarks/ephy-bookmarks-manager.c
+++ b/src/bookmarks/ephy-bookmarks-manager.c
@@ -23,10 +23,9 @@
 #include "ephy-bookmarks-manager.h"
 
 #include "ephy-bookmarks-import.h"
+#include "ephy-bookmarks-export.h"
 #include "ephy-debug.h"
 #include "ephy-file-helpers.h"
-#include "gvdb-builder.h"
-#include "gvdb-reader.h"
 
 #define EPHY_BOOKMARKS_FILE "bookmarks.gvdb"
 
@@ -54,76 +53,11 @@ enum {
 static guint       signals[LAST_SIGNAL];
 
 static void
-gvdb_hash_table_insert_variant (GHashTable *table,
-                                const char *key,
-                                GVariant   *value)
-{
-  GvdbItem *item;
-
-  item = gvdb_hash_table_insert (table, key);
-  gvdb_item_set_value (item, value);
-}
-
-static GVariant *
-build_variant (EphyBookmark *bookmark)
-{
-  GVariantBuilder builder;
-  GSequence *tags;
-  GSequenceIter *iter;
-
-  g_variant_builder_init (&builder, G_VARIANT_TYPE ("(xssdbas)"));
-
-  g_variant_builder_add (&builder, "x", ephy_bookmark_get_time_added (bookmark));
-  g_variant_builder_add (&builder, "s", ephy_bookmark_get_title (bookmark));
-  g_variant_builder_add (&builder, "s", ephy_bookmark_get_id (bookmark));
-  g_variant_builder_add (&builder, "d", ephy_bookmark_get_modification_time (bookmark));
-  g_variant_builder_add (&builder, "b", ephy_bookmark_is_uploaded (bookmark));
-
-  g_variant_builder_open (&builder, G_VARIANT_TYPE ("as"));
-  tags = ephy_bookmark_get_tags (bookmark);
-  for (iter = g_sequence_get_begin_iter (tags);
-       !g_sequence_iter_is_end (iter);
-       iter = g_sequence_iter_next (iter)) {
-    g_variant_builder_add (&builder, "s", g_sequence_get (iter));
-  }
-  g_variant_builder_close (&builder);
-
-  return g_variant_builder_end (&builder);
-}
-
-static void
-add_bookmark_to_table (EphyBookmark *bookmark, GHashTable *table)
-{
-  gvdb_hash_table_insert_variant (table,
-                                  ephy_bookmark_get_url (bookmark),
-                                  build_variant (bookmark));
-}
-
-static void
-add_tag_to_table (const char *tag, GHashTable *table)
-{
-  gvdb_hash_table_insert (table, tag);
-}
-
-static void
 ephy_bookmarks_manager_save_to_file (EphyBookmarksManager *self, GTask *task)
 {
-  GHashTable *root_table;
-  GHashTable *table;
   gboolean result;
 
-  root_table = gvdb_hash_table_new (NULL, NULL);
-
-  table = gvdb_hash_table_new (root_table, "tags");
-  g_sequence_foreach (self->tags, (GFunc)add_tag_to_table, table);
-  g_hash_table_unref (table);
-
-  table = gvdb_hash_table_new (root_table, "bookmarks");
-  g_sequence_foreach (self->bookmarks, (GFunc)add_bookmark_to_table, table);
-  g_hash_table_unref (table);
-
-  result = gvdb_table_write_contents (root_table, self->gvdb_filename, FALSE, NULL);
-  g_hash_table_unref (root_table);
+  result = ephy_bookmarks_export (self, self->gvdb_filename);
 
   if (task)
     g_task_return_boolean (task, result);
diff --git a/src/window-commands.c b/src/window-commands.c
index 4c33a95..d773440 100644
--- a/src/window-commands.c
+++ b/src/window-commands.c
@@ -27,6 +27,7 @@
 
 #include "ephy-add-bookmark-popover.h"
 #include "ephy-bookmarks-manager.h"
+#include "ephy-bookmarks-export.h"
 #include "ephy-bookmarks-import.h"
 #include "ephy-debug.h"
 #include "ephy-embed-container.h"
@@ -242,7 +243,57 @@ window_cmd_export_bookmarks (GSimpleAction *action,
                              GVariant      *parameter,
                              gpointer       user_data)
 {
+  EphyBookmarksManager *manager = ephy_shell_get_bookmarks_manager (ephy_shell_get_default ());
+  GtkWidget *dialog;
+  GtkWidget *export_info_dialog;
+  GtkWidget *suggested;
+  int chooser_response;
+  gboolean exported;
+  GtkFileFilter *filter;
+
+  filter = gtk_file_filter_new ();
+  gtk_file_filter_add_pattern (filter, "*.gvdb");
+
+  dialog = g_object_new (GTK_TYPE_FILE_CHOOSER_DIALOG,
+                         "action", GTK_FILE_CHOOSER_ACTION_SAVE,
+                         "filter", filter,
+                         "modal", TRUE,
+                         "show-hidden", TRUE,
+                         "transient-for", GTK_WINDOW (user_data),
+                         "title", _("Choose File"),
+                         NULL);
+  gtk_dialog_add_buttons (GTK_DIALOG (dialog),
+                          _("Cancel"), GTK_RESPONSE_CANCEL,
+                          _("Save"), GTK_RESPONSE_OK,
+                          NULL);
+  gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_OK);
+  // Translators: Only translate the part before ".gvdb" (e.g. "bookmarks")
+  gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (dialog), _("bookmarks.gvdb"));
 
+  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);
+
+  chooser_response = gtk_dialog_run (GTK_DIALOG (dialog));
+  if (chooser_response == GTK_RESPONSE_OK) {
+    char *filename;
+
+    gtk_widget_hide (dialog);
+
+    filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (dialog));
+    exported = ephy_bookmarks_export (manager, filename);
+    g_free (filename);
+
+    export_info_dialog = gtk_message_dialog_new (GTK_WINDOW (dialog),
+                                                 GTK_DIALOG_MODAL,
+                                                 exported ? GTK_MESSAGE_INFO : GTK_MESSAGE_WARNING,
+                                                 GTK_BUTTONS_OK,
+                                                 exported ? "Bookmarks successfully exported!" :
+                                                            "There was an error exporting bookmarks!");
+    gtk_dialog_run (GTK_DIALOG (export_info_dialog));
+    gtk_widget_destroy (export_info_dialog);
+  }
+  gtk_widget_destroy (dialog);
 }
 
 void


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