[epiphany/wip/bookmarks: 2/17] bookmarks: Add multiple bookmarks at once



commit 5b06a6886f63457ae62c1f192a341f6d525ebdf0
Author: Iulian Radu <iulian radu67 gmail com>
Date:   Tue Aug 9 00:01:28 2016 +0300

    bookmarks: Add multiple bookmarks at once

 src/ephy-bookmarks-manager.c |   68 +++++++++++++++++++++++++++---------------
 src/ephy-bookmarks-manager.h |    2 +
 2 files changed, 46 insertions(+), 24 deletions(-)
---
diff --git a/src/ephy-bookmarks-manager.c b/src/ephy-bookmarks-manager.c
index fb15106..4b3dbf6 100644
--- a/src/ephy-bookmarks-manager.c
+++ b/src/ephy-bookmarks-manager.c
@@ -102,8 +102,8 @@ 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)
+static void
+ephy_bookmarks_manager_save_to_file (EphyBookmarksManager *self, GTask *task)
 {
   GHashTable *root_table;
   GHashTable *table;
@@ -122,7 +122,8 @@ ephy_bookmarks_manager_save_to_file (EphyBookmarksManager *self)
   result = gvdb_table_write_contents (root_table, self->gvdb_filename, FALSE, NULL);
   g_hash_table_unref (root_table);
 
-  return result;
+  if (task)
+    g_task_return_boolean (task, result);
 }
 
 static void
@@ -161,7 +162,7 @@ ephy_bookmarks_manager_init (EphyBookmarksManager *self)
 
   /* 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_save_to_file (self, NULL);
 
   ephy_bookmarks_manager_load_from_file (self);
 }
@@ -232,7 +233,6 @@ ephy_bookmarks_manager_add_bookmark (EphyBookmarksManager *self,
   }
 }
 
-
 static int
 bookmarks_url_compare (EphyBookmark *bookmark1,
                        EphyBookmark *bookmark2)
@@ -250,6 +250,36 @@ bookmarks_url_compare (EphyBookmark *bookmark1,
 }
 
 void
+ephy_bookmarks_manager_add_bookmarks (EphyBookmarksManager *self,
+                                      GSequence            *bookmarks)
+{
+  GSequenceIter *iter;
+
+  g_return_if_fail (EPHY_IS_BOOKMARKS_MANAGER (self));
+  g_return_if_fail (bookmarks != NULL);
+
+  for (iter = g_sequence_get_begin_iter (bookmarks);
+       !g_sequence_iter_is_end (iter);
+       iter = g_sequence_iter_next (iter)) {
+    EphyBookmark *bookmark = g_sequence_get (iter);
+
+    if (!g_sequence_lookup (self->bookmarks,
+                            bookmark,
+                            (GCompareDataFunc)bookmarks_url_compare,
+                            NULL))
+      g_sequence_prepend (self->bookmarks, g_object_ref (bookmark));
+  }
+
+  g_sequence_sort (self->bookmarks,
+                   (GCompareDataFunc)ephy_bookmark_bookmarks_sort_func,
+                   NULL);
+
+  ephy_bookmarks_manager_save_to_file_async (self, NULL,
+                                             (GAsyncReadyCallback)data_saved_cb,
+                                             NULL);
+}
+
+void
 ephy_bookmarks_manager_remove_bookmark (EphyBookmarksManager *self,
                                         EphyBookmark         *bookmark)
 {
@@ -417,23 +447,6 @@ ephy_bookmarks_manager_get_tags (EphyBookmarksManager *self)
   return self->tags;
 }
 
-static void
-ephy_bookmarks_manager_save_to_file_thread (GTask        *task,
-                                            gpointer      source_object,
-                                            gpointer      task_data,
-                                            GCancellable *cancellable)
-{
-  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_async (EphyBookmarksManager *self,
                                            GCancellable         *cancellable,
@@ -444,7 +457,8 @@ ephy_bookmarks_manager_save_to_file_async (EphyBookmarksManager *self,
 
   task = g_task_new (self, cancellable, callback, user_data);
 
-  g_task_run_in_thread (task, ephy_bookmarks_manager_save_to_file_thread);
+  ephy_bookmarks_manager_save_to_file (self, task);
+
   g_object_unref (task);
 }
 
@@ -463,6 +477,7 @@ ephy_bookmarks_manager_load_from_file (EphyBookmarksManager *self)
 {
   GvdbTable *root_table;
   GvdbTable *table;
+  GSequence *bookmarks;
   char **list;
   int length;
   int i;
@@ -475,6 +490,8 @@ ephy_bookmarks_manager_load_from_file (EphyBookmarksManager *self)
   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++) {
@@ -503,9 +520,12 @@ ephy_bookmarks_manager_load_from_file (EphyBookmarksManager *self)
     /* Create the new bookmark. */
     bookmark = ephy_bookmark_new (g_strdup (list[i]), title, tags);
     ephy_bookmark_set_time_added (bookmark, time_added);
-    ephy_bookmarks_manager_add_bookmark (self, bookmark);
+    g_sequence_prepend (bookmarks, bookmark);
   }
+  ephy_bookmarks_manager_add_bookmarks (self, bookmarks);
+
   gvdb_table_free (table);
+  g_sequence_free (bookmarks);
 
   /* Also add tags to the bookmark manager's sequence. */
   table = gvdb_table_get_table (root_table, "tags");
diff --git a/src/ephy-bookmarks-manager.h b/src/ephy-bookmarks-manager.h
index 3fae9c5..2928d3a 100644
--- a/src/ephy-bookmarks-manager.h
+++ b/src/ephy-bookmarks-manager.h
@@ -30,6 +30,8 @@ G_DECLARE_FINAL_TYPE (EphyBookmarksManager, ephy_bookmarks_manager, EPHY, BOOKMA
 
 void         ephy_bookmarks_manager_add_bookmark           (EphyBookmarksManager *self,
                                                             EphyBookmark         *bookmark);
+void         ephy_bookmarks_manager_add_bookmarks          (EphyBookmarksManager *self,
+                                                            GSequence            *bookmarks);
 void         ephy_bookmarks_manager_remove_bookmark        (EphyBookmarksManager *self,
                                                             EphyBookmark         *bookmark);
 EphyBookmark *ephy_bookmarks_manager_get_bookmark_by_url   (EphyBookmarksManager *self,


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