[epiphany/wip/sync] bookmarks: Fix problem causing bookmarks to disappear after an import



commit 2755f8e71a2343364b591fb7fa7a3249ed19274f
Author: Iulian Radu <iulian radu67 gmail com>
Date:   Fri Mar 31 17:43:35 2017 +0300

    bookmarks: Fix problem causing bookmarks to disappear after an import
    
    This fixes some bugs mainly related to importing bookmarks from Firefox.
    In ephy_bookmarks_manager_add_bookmarks(), g_sequence_lookup()
    was called on an unsorted sequence, which caused the function
    to fail and not add bookmarks to the manager. Besides that, the compare
    function used for various GSequence operations did not treat the
    case where the time_added was equal for both bookmarks.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=780564

 src/bookmarks/ephy-bookmark.c          |   22 +++++++++++--
 src/bookmarks/ephy-bookmark.h          |    4 +-
 src/bookmarks/ephy-bookmarks-manager.c |   52 ++++++++++++++++++--------------
 3 files changed, 50 insertions(+), 28 deletions(-)
---
diff --git a/src/bookmarks/ephy-bookmark.c b/src/bookmarks/ephy-bookmark.c
index e0f5f25..c3b9a2e 100644
--- a/src/bookmarks/ephy-bookmark.c
+++ b/src/bookmarks/ephy-bookmark.c
@@ -649,19 +649,35 @@ ephy_bookmark_get_tags (EphyBookmark *self)
 }
 
 int
-ephy_bookmark_bookmarks_sort_func (EphyBookmark *bookmark1,
-                                   EphyBookmark *bookmark2)
+ephy_bookmark_bookmarks_compare_func (EphyBookmark *bookmark1,
+                                      EphyBookmark *bookmark2)
 {
   gint64 time1;
   gint64 time2;
+  const char *title1;
+  const char *title2;
+  int title_result;
+  const char *id1;
+  const char *id2;
 
   g_assert (EPHY_IS_BOOKMARK (bookmark1));
   g_assert (EPHY_IS_BOOKMARK (bookmark2));
 
   time1 = ephy_bookmark_get_time_added (bookmark1);
   time2 = ephy_bookmark_get_time_added (bookmark2);
+  if (time2 - time1 != 0)
+    return time2 - time1;
 
-  return time2 - time1;
+  title1 = ephy_bookmark_get_title (bookmark1);
+  title2 = ephy_bookmark_get_title (bookmark2);
+  title_result = g_strcmp0 (title1, title2);
+  if (title_result != 0)
+    return title_result;
+
+  id1 = ephy_bookmark_get_id (bookmark1);
+  id2 = ephy_bookmark_get_id (bookmark2);
+
+  return g_strcmp0 (id1, id2);
 }
 
 int
diff --git a/src/bookmarks/ephy-bookmark.h b/src/bookmarks/ephy-bookmark.h
index fafdda1..8763365 100644
--- a/src/bookmarks/ephy-bookmark.h
+++ b/src/bookmarks/ephy-bookmark.h
@@ -66,8 +66,8 @@ gboolean             ephy_bookmark_has_tag               (EphyBookmark *self,
                                                           const char   *tag);
 GSequence           *ephy_bookmark_get_tags              (EphyBookmark *self);
 
-int                  ephy_bookmark_bookmarks_sort_func   (EphyBookmark *bookmark1,
-                                                          EphyBookmark *bookmark2);
+int                  ephy_bookmark_bookmarks_compare_func   (EphyBookmark *bookmark1,
+                                                             EphyBookmark *bookmark2);
 int                  ephy_bookmark_tags_compare          (const char *tag1,
                                                           const char *tag2);
 
diff --git a/src/bookmarks/ephy-bookmarks-manager.c b/src/bookmarks/ephy-bookmarks-manager.c
index 8aa995d..f4c1098 100644
--- a/src/bookmarks/ephy-bookmarks-manager.c
+++ b/src/bookmarks/ephy-bookmarks-manager.c
@@ -494,30 +494,41 @@ ephy_bookmarks_manager_unwatch_bookmark (EphyBookmarksManager *self,
   g_signal_handlers_disconnect_by_func (bookmark, bookmark_tag_removed_cb, self);
 }
 
+static GSequenceIter *
+ephy_bookmarks_search_and_insert_bookmark (GSequence     *bookmarks,
+                                           EphyBookmark  *bookmark)
+{
+  GSequenceIter *iter;
+  GSequenceIter *prev_iter;
+
+  iter = g_sequence_search (bookmarks, bookmark,
+                            (GCompareDataFunc)ephy_bookmark_bookmarks_compare_func,
+                            NULL);
+
+  prev_iter = g_sequence_iter_prev (iter);
+  if (g_sequence_iter_is_end (prev_iter)
+      || ephy_bookmark_bookmarks_compare_func (g_sequence_get (prev_iter), bookmark) != 0) {
+    return g_sequence_insert_before (iter, bookmark);
+  }
+
+  return NULL;
+}
+
 void
 ephy_bookmarks_manager_add_bookmark (EphyBookmarksManager *self,
                                      EphyBookmark         *bookmark)
 {
   GSequenceIter *iter;
-  GSequenceIter *prev_iter;
   gint position;
 
   g_return_if_fail (EPHY_IS_BOOKMARKS_MANAGER (self));
   g_return_if_fail (EPHY_IS_BOOKMARK (bookmark));
 
-  iter = g_sequence_search (self->bookmarks,
-                            bookmark,
-                            (GCompareDataFunc)ephy_bookmark_bookmarks_sort_func,
-                            NULL);
-
-  prev_iter = g_sequence_iter_prev (iter);
-  if (g_sequence_iter_is_end (prev_iter)
-      || ephy_bookmark_get_time_added (g_sequence_get (prev_iter)) != ephy_bookmark_get_time_added 
(bookmark)) {
-    g_sequence_insert_before (iter, bookmark);
-
+  iter = ephy_bookmarks_search_and_insert_bookmark (self->bookmarks, bookmark);
+  if (iter) {
     /* Update list */
     position = g_sequence_iter_get_position (iter);
-    g_list_model_items_changed (G_LIST_MODEL (self), position - 1, 0, 1);
+    g_list_model_items_changed (G_LIST_MODEL (self), position, 0, 1);
 
     g_signal_emit (self, signals[BOOKMARK_ADDED], 0, bookmark);
 
@@ -533,6 +544,7 @@ ephy_bookmarks_manager_add_bookmarks (EphyBookmarksManager *self,
                                       GSequence            *bookmarks)
 {
   GSequenceIter *iter;
+  GSequenceIter *new_iter;
 
   g_return_if_fail (EPHY_IS_BOOKMARKS_MANAGER (self));
   g_return_if_fail (bookmarks != NULL);
@@ -542,20 +554,14 @@ ephy_bookmarks_manager_add_bookmarks (EphyBookmarksManager *self,
        iter = g_sequence_iter_next (iter)) {
     EphyBookmark *bookmark = g_sequence_get (iter);
 
-    if (!g_sequence_lookup (self->bookmarks,
-                            bookmark,
-                            (GCompareDataFunc)ephy_bookmark_bookmarks_sort_func,
-                            NULL)) {
-      g_sequence_prepend (self->bookmarks, g_object_ref (bookmark));
+    new_iter = ephy_bookmarks_search_and_insert_bookmark (self->bookmarks,
+                                                          g_object_ref (bookmark));
+    if (new_iter) {
       g_signal_emit (self, signals[BOOKMARK_ADDED], 0, bookmark);
       ephy_bookmarks_manager_watch_bookmark (self, bookmark);
     }
   }
 
-  g_sequence_sort (self->bookmarks,
-                   (GCompareDataFunc)ephy_bookmark_bookmarks_sort_func,
-                   NULL);
-
   ephy_bookmarks_manager_save_to_file_async (self, NULL,
                                              
(GAsyncReadyCallback)ephy_bookmarks_manager_save_to_file_warn_on_error_cb,
                                              NULL);
@@ -734,7 +740,7 @@ ephy_bookmarks_manager_get_bookmarks_with_tag (EphyBookmarksManager *self,
       if (g_sequence_is_empty (ephy_bookmark_get_tags (bookmark))) {
         g_sequence_insert_sorted (bookmarks,
                                   g_object_ref (bookmark),
-                                  (GCompareDataFunc)ephy_bookmark_bookmarks_sort_func,
+                                  (GCompareDataFunc)ephy_bookmark_bookmarks_compare_func,
                                   NULL);
       }
     }
@@ -747,7 +753,7 @@ ephy_bookmarks_manager_get_bookmarks_with_tag (EphyBookmarksManager *self,
       if (ephy_bookmark_has_tag (bookmark, tag))
         g_sequence_insert_sorted (bookmarks,
                                   g_object_ref (bookmark),
-                                  (GCompareDataFunc)ephy_bookmark_bookmarks_sort_func,
+                                  (GCompareDataFunc)ephy_bookmark_bookmarks_compare_func,
                                   NULL);
     }
   }


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