[epiphany] Add HTML bookmark export



commit b3fff208bc0a516603633e21b6cf6535da39325a
Author: Jan-Michael Brummer <jan brummer tabos org>
Date:   Sat Jan 9 16:25:44 2021 +0100

    Add HTML bookmark export
    
    Export bookmarks as Netscape Bookmark HTML file, which is used among different browser
    for bookmark export. Furthermore set it as default export format instead of gvdb.
    
    Fixes: https://gitlab.gnome.org/GNOME/epiphany/-/issues/483

 src/bookmarks/ephy-bookmarks-export.c | 119 ++++++++++++++++++++++++++++------
 src/window-commands.c                 |   5 +-
 2 files changed, 103 insertions(+), 21 deletions(-)
---
diff --git a/src/bookmarks/ephy-bookmarks-export.c b/src/bookmarks/ephy-bookmarks-export.c
index f1bc54d1c..42e7a863b 100644
--- a/src/bookmarks/ephy-bookmarks-export.c
+++ b/src/bookmarks/ephy-bookmarks-export.c
@@ -97,6 +97,53 @@ write_contents_cb (GObject      *source_object,
   g_task_return_boolean (task, TRUE);
 }
 
+static void
+add_tags_to_string (const char *tag,
+                    GString    *tags)
+{
+  g_string_append_printf (tags, "%s%s", tags->len ? ", " : "", tag);
+}
+
+static void
+add_bookmark_to_html (EphyBookmark *bookmark,
+                      GString      *html)
+{
+  GSequence *tag_sequence;
+  g_autoptr (GString) tags = NULL;
+
+  tag_sequence = ephy_bookmark_get_tags (bookmark);
+  if (tag_sequence) {
+    tags = g_string_new ("");
+    g_sequence_foreach (tag_sequence, (GFunc)add_tags_to_string, tags);
+  }
+
+  g_string_append_printf (html,
+                          "<DT><A HREF=\"%s\" ADD_DATE=\"%ld\" TAGS=\"%s\">%s</A>\n",
+                          ephy_bookmark_get_url (bookmark),
+                          ephy_bookmark_get_time_added (bookmark),
+                          tags ? tags->str : "",
+                          ephy_bookmark_get_title (bookmark));
+}
+
+static void
+write_html_contents_cb (GObject      *source_object,
+                        GAsyncResult *result,
+                        gpointer      user_data)
+{
+  g_autoptr (GTask) task = user_data;
+  GFile *file;
+  GError *error = NULL;
+
+  file = g_task_get_task_data (task);
+
+  if (!g_file_replace_contents_finish (file, result, NULL, &error)) {
+    g_task_return_error (task, error);
+    return;
+  }
+
+  g_task_return_boolean (task, TRUE);
+}
+
 void
 ephy_bookmarks_export (EphyBookmarksManager *manager,
                        const char           *filename,
@@ -104,25 +151,59 @@ ephy_bookmarks_export (EphyBookmarksManager *manager,
                        GAsyncReadyCallback   callback,
                        gpointer              user_data)
 {
-  GHashTable *root_table;
-  GHashTable *table;
-  GTask *task;
-
-  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);
-
-  task = g_task_new (manager, cancellable, callback, user_data);
-  g_task_set_task_data (task, root_table, (GDestroyNotify)g_hash_table_unref);
-
-  gvdb_table_write_contents_async (root_table, filename, FALSE,
-                                   cancellable, write_contents_cb, task);
+  if (g_str_has_suffix (filename, ".gvdb")) {
+    GHashTable *root_table;
+    GHashTable *table;
+    GTask *task;
+
+    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);
+
+    task = g_task_new (manager, cancellable, callback, user_data);
+    g_task_set_task_data (task, root_table, (GDestroyNotify)g_hash_table_unref);
+
+    gvdb_table_write_contents_async (root_table, filename, FALSE,
+                                     cancellable, write_contents_cb, task);
+  } else {
+    g_autoptr (GString) html = NULL;
+    g_autoptr (GBytes) bytes = NULL;
+    g_autoptr (GFile) file = NULL;
+    GTask *task;
+
+    html = g_string_new ("<!DOCTYPE NETSCAPE-Bookmark-file-1>\n");
+    g_string_append (html, "<META HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html; charset=UTF-8\">\n");
+    g_string_append (html, "<TITLE>Bookmarks</TITLE>\n");
+    g_string_append (html, "<H1>Epiphany Bookmarks</H1>\n");
+    g_string_append (html, "<DL><p>\n");
+    g_string_append (html, "<DT><H3>Epiphany</H3>\n");
+    g_string_append (html, "<DL><p>\n");
+
+    g_sequence_foreach (ephy_bookmarks_manager_get_bookmarks (manager), (GFunc)add_bookmark_to_html, html);
+
+    g_string_append (html, "</DL>\n");
+
+    file = g_file_new_for_path (filename);
+
+    task = g_task_new (manager, cancellable, callback, user_data);
+    g_task_set_task_data (task, file, (GDestroyNotify)g_object_unref);
+
+    bytes = g_bytes_new (html->str, html->len);
+    g_file_replace_contents_bytes_async (g_steal_pointer (&file),
+                                         bytes,
+                                         NULL,
+                                         FALSE,
+                                         G_FILE_CREATE_REPLACE_DESTINATION,
+                                         cancellable,
+                                         write_html_contents_cb,
+                                         g_steal_pointer (&task));
+  }
 }
 
 gboolean
diff --git a/src/window-commands.c b/src/window-commands.c
index c4ed583d5..7ea0aba02 100644
--- a/src/window-commands.c
+++ b/src/window-commands.c
@@ -646,10 +646,11 @@ window_cmd_export_bookmarks (GSimpleAction *action,
                                                           _("_Cancel")));
   gtk_file_chooser_set_show_hidden (dialog, TRUE);
 
-  /* Translators: Only translate the part before ".gvdb" (e.g. "bookmarks") */
-  gtk_file_chooser_set_current_name (dialog, _("bookmarks.gvdb"));
+  /* Translators: Only translate the part before ".html" (e.g. "bookmarks") */
+  gtk_file_chooser_set_current_name (dialog, _("bookmarks.html"));
 
   filter = gtk_file_filter_new ();
+  gtk_file_filter_add_pattern (filter, "*.html");
   gtk_file_filter_add_pattern (filter, "*.gvdb");
   gtk_file_chooser_set_filter (dialog, filter);
 


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