[epiphany/wip/bookmarks: 21/76] bookmarks: Save to db asynchronously when bookmarks are added/removed



commit 7e07b2abbb7f11590cffe0e2aa90187cff0a119f
Author: Iulian Radu <iulian radu67 gmail com>
Date:   Mon Aug 1 23:26:02 2016 +0300

    bookmarks: Save to db asynchronously when bookmarks are added/removed

 src/ephy-bookmark-row.c                       |    2 +-
 src/ephy-bookmarks-manager.c                  |  129 +++++++++++++++++++++----
 src/ephy-bookmarks-manager.h                  |   10 ++-
 src/ephy-bookmarks-popover.c                  |   12 ++-
 src/resources/gtk/bookmark-properties-grid.ui |    5 +-
 src/resources/gtk/bookmarks-popover.ui        |   51 +++++++---
 6 files changed, 166 insertions(+), 43 deletions(-)
---
diff --git a/src/ephy-bookmark-row.c b/src/ephy-bookmark-row.c
index 0884c37..11de228 100644
--- a/src/ephy-bookmark-row.c
+++ b/src/ephy-bookmark-row.c
@@ -80,7 +80,7 @@ ephy_bookmark_row_favicon_loaded_cb (GObject      *source,
   EphyBookmarkRow *self = user_data;
   WebKitFaviconDatabase *database = (WebKitFaviconDatabase *)source;
   cairo_surface_t *icon_surface;
-  GdkPixbuf *favicon;
+  GdkPixbuf *favicon = NULL;
 
   g_assert (EPHY_IS_BOOKMARK_ROW (self));
 
diff --git a/src/ephy-bookmarks-manager.c b/src/ephy-bookmarks-manager.c
index ce7f929..4988290 100644
--- a/src/ephy-bookmarks-manager.c
+++ b/src/ephy-bookmarks-manager.c
@@ -19,6 +19,7 @@
 
 #include "ephy-bookmarks-manager.h"
 
+#include "ephy-debug.h"
 #include "ephy-file-helpers.h"
 #include "gvdb-builder.h"
 #include "gvdb-reader.h"
@@ -71,6 +72,54 @@ build_variant (EphyBookmark *bookmark)
 }
 
 static void
+data_saved_cb (GObject      *object,
+               GAsyncResult *result,
+               gpointer      user_data)
+{
+  EphyBookmarksManager *self = EPHY_BOOKMARKS_MANAGER (object);
+  gboolean ret;
+
+  ret = ephy_bookmarks_manager_save_to_file_finish (self, result, NULL);
+  if (ret)
+    LOG ("Bookmarks data saved");
+}
+
+static void
+add_tag_to_table (const char *tag, GHashTable *table)
+{
+  gvdb_hash_table_insert (table, tag);
+}
+
+
+static gboolean
+ephy_bookmarks_manager_save_to_file (EphyBookmarksManager *self)
+{
+  GHashTable *root_table;
+  GHashTable *table;
+  GList *l;
+  gboolean result;
+
+  root_table = gvdb_hash_table_new (NULL, NULL);
+
+  table = gvdb_hash_table_new (root_table, "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));
+  }
+  g_hash_table_unref (table);
+
+  table = gvdb_hash_table_new (root_table, "tags");
+  g_sequence_foreach (self->tags, (GFunc)add_tag_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);
+
+  return result;
+}
+
+static void
 ephy_bookmarks_manager_finalize (GObject *object)
 {
   EphyBookmarksManager *self = EPHY_BOOKMARKS_MANAGER (object);
@@ -101,6 +150,12 @@ ephy_bookmarks_manager_init (EphyBookmarksManager *self)
                                           NULL);
 
   self->tags = g_sequence_new (g_free);
+
+  /* Create DB file if it doesn't already exists */
+  if (!g_file_test (self->gvdb_filename, G_FILE_TEST_EXISTS))
+    ephy_bookmarks_manager_save_to_file (self);
+
+  ephy_bookmarks_manager_load_from_file (self);
 }
 
 static GType
@@ -139,6 +194,20 @@ list_model_iface_init (GListModelInterface *iface)
   iface->get_item = ephy_bookmarks_manager_get_item;
 }
 
+static int
+bookmark_url_compare (gpointer a, gpointer b)
+{
+  EphyBookmark *bookmark1 = EPHY_BOOKMARK (a);
+  EphyBookmark *bookmark2 = EPHY_BOOKMARK (b);
+  const char *url1;
+  const char *url2;
+
+  url1 = ephy_bookmark_get_url (bookmark1);
+  url2 = ephy_bookmark_get_url (bookmark2);
+
+  return g_strcmp0 (url1, url2);
+}
+
 void
 ephy_bookmarks_manager_add_bookmark (EphyBookmarksManager *self,
                                      EphyBookmark         *bookmark)
@@ -146,7 +215,9 @@ ephy_bookmarks_manager_add_bookmark (EphyBookmarksManager *self,
   g_return_if_fail (EPHY_IS_BOOKMARKS_MANAGER (self));
   g_return_if_fail (EPHY_IS_BOOKMARK (bookmark));
 
-  if (g_list_find (self->bookmarks, bookmark))
+  if (g_list_find_custom (self->bookmarks,
+                          bookmark,
+                          (GCompareFunc)bookmark_url_compare))
     return;
 
   g_signal_connect_object (bookmark,
@@ -156,6 +227,10 @@ ephy_bookmarks_manager_add_bookmark (EphyBookmarksManager *self,
                            G_CONNECT_SWAPPED);
 
   self->bookmarks = g_list_prepend (self->bookmarks, bookmark);
+
+  ephy_bookmarks_manager_save_to_file_async (self, NULL,
+                                             (GAsyncReadyCallback) data_saved_cb,
+                                             NULL);
 }
 
 void
@@ -172,6 +247,10 @@ ephy_bookmarks_manager_remove_bookmark (EphyBookmarksManager *self,
 
   self->bookmarks = g_list_remove (self->bookmarks, bookmark);
   g_list_model_items_changed (G_LIST_MODEL (self), position, 1, 0);
+
+  ephy_bookmarks_manager_save_to_file_async (self, NULL,
+                                             (GAsyncReadyCallback) data_saved_cb,
+                                             NULL);
 }
 
 void
@@ -277,34 +356,44 @@ ephy_bookmarks_manager_get_tags (EphyBookmarksManager *self)
 }
 
 static void
-add_tag_to_table (const char *tag, GHashTable *table)
+ephy_bookmarks_manager_save_to_file_thread (GTask        *task,
+                                            gpointer      source_object,
+                                            gpointer      task_data,
+                                            GCancellable *cancellable)
 {
-  gvdb_hash_table_insert (table, tag);
+  EphyBookmarksManager *self = source_object;
+  gboolean result;
+
+  g_assert (G_IS_TASK (task));
+  g_assert (EPHY_IS_BOOKMARKS_MANAGER (self));
+
+  result = ephy_bookmarks_manager_save_to_file (self);
+
+  g_task_return_boolean (task, result);
 }
 
 void
-ephy_bookmarks_manager_save_to_file (EphyBookmarksManager *self)
+ephy_bookmarks_manager_save_to_file_async (EphyBookmarksManager *self,
+                                           GCancellable         *cancellable,
+                                           GAsyncReadyCallback   callback,
+                                           gpointer              user_data)
 {
-  GHashTable *root_table;
-  GHashTable *table;
-  GList *l;
+  GTask *task;
 
-  root_table = gvdb_hash_table_new (NULL, NULL);
+  task = g_task_new (self, cancellable, callback, user_data);
 
-  table = gvdb_hash_table_new (root_table, "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));
-  }
-  g_hash_table_unref (table);
+  g_task_run_in_thread (task, ephy_bookmarks_manager_save_to_file_thread);
+  g_object_unref (task);
+}
 
-  table = gvdb_hash_table_new (root_table, "tags");
-  g_sequence_foreach (self->tags, (GFunc)add_tag_to_table, table);
-  g_hash_table_unref (table);
+gboolean
+ephy_bookmarks_manager_save_to_file_finish (EphyBookmarksManager *self,
+                                            GAsyncResult         *result,
+                                            GError              **error)
+{
+  g_return_val_if_fail (g_task_is_valid (result, self), FALSE);
 
-  gvdb_table_write_contents (root_table, self->gvdb_filename, FALSE, NULL);
-  g_hash_table_unref (root_table);
+  return g_task_propagate_boolean (G_TASK (result), error);
 }
 
 void
diff --git a/src/ephy-bookmarks-manager.h b/src/ephy-bookmarks-manager.h
index 77caa14..2bb6b89 100644
--- a/src/ephy-bookmarks-manager.h
+++ b/src/ephy-bookmarks-manager.h
@@ -20,6 +20,8 @@
 
 #include "ephy-bookmark.h"
 
+#include <gio/gio.h>
+
 G_BEGIN_DECLS
 
 #define EPHY_TYPE_BOOKMARKS_MANAGER (ephy_bookmarks_manager_get_type ())
@@ -43,7 +45,13 @@ GList       *ephy_bookmarks_manager_get_bookmarks_with_tag (EphyBookmarksManager
                                                             const char           *tag);
 GSequence   *ephy_bookmarks_manager_get_tags               (EphyBookmarksManager *self);
 
-void         ephy_bookmarks_manager_save_to_file           (EphyBookmarksManager *self);
+void        ephy_bookmarks_manager_save_to_file_async      (EphyBookmarksManager *self,
+                                                            GCancellable         *cancellable,
+                                                            GAsyncReadyCallback   callback,
+                                                            gpointer              user_data);
+gboolean     ephy_bookmarks_manager_save_to_file_finish    (EphyBookmarksManager *self,
+                                                            GAsyncResult         *result,
+                                                            GError              **error);
 void         ephy_bookmarks_manager_load_from_file         (EphyBookmarksManager *self);
 
 G_END_DECLS
diff --git a/src/ephy-bookmarks-popover.c b/src/ephy-bookmarks-popover.c
index bfc07dd..7c26d46 100644
--- a/src/ephy-bookmarks-popover.c
+++ b/src/ephy-bookmarks-popover.c
@@ -22,6 +22,7 @@
 #include "ephy-bookmark.h"
 #include "ephy-bookmark-row.h"
 #include "ephy-bookmarks-manager.h"
+#include "ephy-debug.h"
 #include "ephy-shell.h"
 
 #include <glib/gi18n.h>
@@ -268,6 +269,7 @@ ephy_bookmarks_popover_init (EphyBookmarksPopover *self)
   GList *l;
   EphyBookmark *dummy_bookmark;
   GSimpleActionGroup *group;
+  int i;
 
   gtk_widget_init_template (GTK_WIDGET (self));
 
@@ -278,10 +280,14 @@ ephy_bookmarks_popover_init (EphyBookmarksPopover *self)
                                   G_ACTION_GROUP (group));
   g_object_unref (group);
 
-  dummy_bookmark = ephy_bookmark_new (g_strdup ("https://www.facebook.com/";),
-                                      g_strdup ("Facebook"),
+  for (i = 0; i < 1; i++) {
+    dummy_bookmark = ephy_bookmark_new (g_strdup_printf ("https://www.duckduckgo.com/%d";, i),
+                                      g_strdup_printf ("DuckDuckGo %d", i),
                                       g_sequence_new (g_free));
-  ephy_bookmarks_manager_add_bookmark (manager, dummy_bookmark);
+    ephy_bookmarks_manager_add_tag (manager, g_strdup_printf ("Tag %d", i));
+    ephy_bookmarks_manager_add_tag (manager, g_strdup_printf ("Favorite %d", i));
+    ephy_bookmarks_manager_add_bookmark (manager, dummy_bookmark);
+  }
 
   gtk_list_box_bind_model (GTK_LIST_BOX (self->bookmarks_list_box),
                            G_LIST_MODEL (manager),
diff --git a/src/resources/gtk/bookmark-properties-grid.ui b/src/resources/gtk/bookmark-properties-grid.ui
index 45a3e83..4a9cdf5 100644
--- a/src/resources/gtk/bookmark-properties-grid.ui
+++ b/src/resources/gtk/bookmark-properties-grid.ui
@@ -99,10 +99,9 @@
     <child>
       <object class="GtkScrolledWindow">
         <property name="valign">start</property>
-        <property name="height-request">100</property>
-        <property name="min-content-height">100</property>
+        <property name="min-content-height">40</property>
+        <property name="max-content-height">132</property>
         <property name="hscrollbar-policy">never</property>
-        <property name="vscrollbar-policy">automatic</property>
         <property name="visible">true</property>
         <child>
           <object class="GtkFlowBox" id="tags_box">
diff --git a/src/resources/gtk/bookmarks-popover.ui b/src/resources/gtk/bookmarks-popover.ui
index 26a2594..b36a5f5 100644
--- a/src/resources/gtk/bookmarks-popover.ui
+++ b/src/resources/gtk/bookmarks-popover.ui
@@ -32,12 +32,19 @@
                     <property name="transition-type">GTK_STACK_TRANSITION_TYPE_SLIDE_LEFT_RIGHT</property>
                     <property name="visible">true</property>
                     <child>
-                      <object class="GtkListBox" id="bookmarks_list_box">
-                        <property name="selection-mode">none</property>
+                      <object class="GtkScrolledWindow">
+                        <property name="max-content-height">400</property>
+                        <property name="hscrollbar-policy">never</property>
                         <property name="visible">true</property>
-                        <style>
-                          <class name="background"/>
-                        </style>
+                        <child>
+                          <object class="GtkListBox" id="bookmarks_list_box">
+                            <property name="selection-mode">none</property>
+                            <property name="visible">true</property>
+                            <style>
+                              <class name="background"/>
+                            </style>
+                          </object>
+                        </child>
                       </object>
                       <packing>
                         <property name="name">all</property>
@@ -45,12 +52,19 @@
                       </packing>
                     </child>
                     <child>
-                      <object class="GtkListBox" id="tags_list_box">
-                        <property name="selection-mode">none</property>
+                      <object class="GtkScrolledWindow">
+                        <property name="max-content-height">400</property>
+                        <property name="hscrollbar-policy">never</property>
                         <property name="visible">true</property>
-                        <style>
-                          <class name="background"/>
-                        </style>
+                        <child>
+                          <object class="GtkListBox" id="tags_list_box">
+                            <property name="selection-mode">none</property>
+                            <property name="visible">true</property>
+                            <style>
+                              <class name="background"/>
+                            </style>
+                          </object>
+                        </child>
                       </object>
                       <packing>
                         <property name="name">tags</property>
@@ -107,12 +121,19 @@
                   </object>
                 </child>
                 <child>
-                  <object class="GtkListBox" id="tag_detail_list_box">
-                    <property name="selection-mode">none</property>
+                  <object class="GtkScrolledWindow">
+                    <property name="max-content-height">400</property>
+                    <property name="hscrollbar-policy">never</property>
                     <property name="visible">true</property>
-                    <style>
-                      <class name="background"/>
-                    </style>
+                    <child>
+                      <object class="GtkListBox" id="tag_detail_list_box">
+                        <property name="selection-mode">none</property>
+                        <property name="visible">true</property>
+                        <style>
+                          <class name="background"/>
+                        </style>
+                      </object>
+                    </child>
                   </object>
                 </child>
               </object>


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