[epiphany/wip/bookmarks: 256/315] bookmarks-manager: Populate bookmarks from database file



commit 71a5fe662fb4dc5c383fd72cf976736b53702c4a
Author: Iulian Radu <iulian radu67 gmail com>
Date:   Wed Jul 27 17:33:34 2016 +0300

    bookmarks-manager: Populate bookmarks from database file

 src/ephy-bookmarks-manager.c |  108 ++++++++++++++++++++++++++++++++++++++++--
 src/ephy-bookmarks-manager.h |    3 +
 src/ephy-bookmarks-popover.c |   18 +-------
 3 files changed, 108 insertions(+), 21 deletions(-)
---
diff --git a/src/ephy-bookmarks-manager.c b/src/ephy-bookmarks-manager.c
index 4257aaf..6b86c02 100644
--- a/src/ephy-bookmarks-manager.c
+++ b/src/ephy-bookmarks-manager.c
@@ -20,6 +20,8 @@
 #include "ephy-bookmarks-manager.h"
 
 #include "ephy-file-helpers.h"
+#include "gvdb-builder.h"
+#include "gvdb-reader.h"
 
 #define EPHY_BOOKMARKS_FILE "bookmarks.gvdb"
 
@@ -28,7 +30,7 @@ struct _EphyBookmarksManager {
 
   GList      *bookmarks;
 
-  gchar      *gvdb_file;
+  gchar      *gvdb_filename;
 };
 
 static void list_model_iface_init     (GListModelInterface *iface);
@@ -37,6 +39,37 @@ G_DEFINE_TYPE_EXTENDED (EphyBookmarksManager, ephy_bookmarks_manager, G_TYPE_OBJ
                         G_IMPLEMENT_INTERFACE (G_TYPE_LIST_MODEL, list_model_iface_init))
 
 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 ("as"));
+  g_variant_builder_add (&builder, "s", ephy_bookmark_get_title (bookmark));
+
+  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));
+  }
+
+  return g_variant_builder_end (&builder);
+}
+
+static void
 ephy_bookmarks_manager_class_init (EphyBookmarksManagerClass *klass)
 {
 }
@@ -44,9 +77,11 @@ ephy_bookmarks_manager_class_init (EphyBookmarksManagerClass *klass)
 static void
 ephy_bookmarks_manager_init (EphyBookmarksManager *self)
 {
-  self->gvdb_file = g_build_filename (ephy_dot_dir (),
-                                         EPHY_BOOKMARKS_FILE,
-                                         NULL);
+  self->gvdb_filename = g_build_filename (ephy_dot_dir (),
+                                          EPHY_BOOKMARKS_FILE,
+                                          NULL);
+
+  ephy_bookmarks_manager_load_from_file (self);
 }
 
 static GType
@@ -158,3 +193,68 @@ ephy_bookmarks_manager_get_tags (EphyBookmarksManager *self)
 
   return tags;
 }
+
+void
+ephy_bookmarks_manager_save_to_file (EphyBookmarksManager *self)
+{
+  GHashTable *table;
+  GList *l;
+
+  table = gvdb_hash_table_new (NULL, "bookmarks");
+
+  for (l = self->bookmarks; l != NULL; l = l->next) {
+    gvdb_hash_table_insert_variant (table,
+                                    ephy_bookmark_get_url (l->data),
+                                    build_variant (l->data));
+  }
+
+  gvdb_table_write_contents (table, self->gvdb_filename, FALSE, NULL);
+
+  g_hash_table_unref (table);
+}
+
+void
+ephy_bookmarks_manager_load_from_file (EphyBookmarksManager *self)
+{
+  GvdbTable *table;
+  char **list;
+  int length;
+  int i;
+
+  /* Create a new table to hold data stored in file. */
+  table = gvdb_table_new (self->gvdb_filename, TRUE, NULL);
+
+  /* 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;
+    char *title;
+
+    /* Obtain the correspoding GVariant. */
+    value = gvdb_table_get_value (table, list[i]);
+
+    g_variant_iter_init (&iter, value);
+
+    /* The first string in the array is the bookmark's title. */
+    g_variant_iter_next (&iter, "s", &title);
+
+    /* 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)g_strcmp0,
+                                NULL);
+    }
+
+    /* Create the new bookmark. */
+    bookmark = ephy_bookmark_new (g_strdup (list[i]), title);
+    ephy_bookmark_set_tags (bookmark, tags);
+    ephy_bookmarks_manager_add_bookmark (self, bookmark);
+  }
+
+  gvdb_table_free (table);
+}
diff --git a/src/ephy-bookmarks-manager.h b/src/ephy-bookmarks-manager.h
index a284a00..814a2e6 100644
--- a/src/ephy-bookmarks-manager.h
+++ b/src/ephy-bookmarks-manager.h
@@ -34,6 +34,9 @@ void         ephy_bookmarks_manager_remove_bookmark      (EphyBookmarksManager *
 GList       *ephy_bookmarks_manager_get_bookmarks        (EphyBookmarksManager *self);
 GSequence   *ephy_bookmarks_manager_get_tags             (EphyBookmarksManager *self);
 
+void         ephy_bookmarks_manager_save_to_file         (EphyBookmarksManager *self);
+void         ephy_bookmarks_manager_load_from_file       (EphyBookmarksManager *self);
+
 G_END_DECLS
 
 #endif /* EPHY_BOOKMARKS_MANAGER_H */
diff --git a/src/ephy-bookmarks-popover.c b/src/ephy-bookmarks-popover.c
index 3aa2be0..2066308 100644
--- a/src/ephy-bookmarks-popover.c
+++ b/src/ephy-bookmarks-popover.c
@@ -156,27 +156,11 @@ static void
 ephy_bookmarks_popover_init (EphyBookmarksPopover *self)
 {
   EphyBookmarksManager *manager = ephy_shell_get_bookmarks_manager (ephy_shell_get_default ());
-  GSequence *tags, *tags1, *tags2;
+  GSequence *tags;
   GSequenceIter *iter;
-  EphyBookmark *dummy_bookmark;
-  gint i;
 
   gtk_widget_init_template (GTK_WIDGET (self));
 
-  dummy_bookmark = ephy_bookmark_new (g_strdup ("https://duckduckgo.com/asdasdas/asdas";), g_strdup ("Test 
title"));
-  tags1 = g_sequence_new (g_free);
-  for (i = 0; i < 20; i++)
-    g_sequence_insert_sorted (tags1, g_strdup_printf ("Fun %d", i), (GCompareDataFunc)g_strcmp0, NULL);
-
-  ephy_bookmark_set_tags (dummy_bookmark, tags1);
-  ephy_bookmarks_manager_add_bookmark (manager, dummy_bookmark);
-
-  dummy_bookmark = ephy_bookmark_new (g_strdup ("https://wikipedia.com";), g_strdup ("wikipedia"));
-  ephy_bookmarks_manager_add_bookmark (manager, dummy_bookmark);
-  tags2 = g_sequence_new (g_free);
-  g_sequence_insert_sorted (tags2, g_strdup_printf ("Not Fun %d", 1), (GCompareDataFunc)g_strcmp0, NULL);
-  ephy_bookmark_set_tags (dummy_bookmark, tags2);
-
   gtk_list_box_bind_model (GTK_LIST_BOX (self->bookmarks_list_box),
                            G_LIST_MODEL (manager),
                            create_bookmark_row,


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