[gnome-notes] BijiTracker: GObjectify and adapt to changes



commit 5002bb1d9021f5a8631b8b431ec5f5129f3e7703
Author: Mohammed Sadiq <sadiq sadiqpk org>
Date:   Wed Jun 16 19:03:16 2021 +0530

    BijiTracker: GObjectify and adapt to changes

 src/bjb-controller.c                       |   27 +-
 src/bjb-organize-dialog.c                  |   36 +-
 src/bjb-window.c                           |   19 +-
 src/libbiji/biji-manager.c                 |  115 ++-
 src/libbiji/biji-manager.h                 |    3 +
 src/libbiji/biji-note-obj.c                |   30 +-
 src/libbiji/biji-notebook.c                |   80 +-
 src/libbiji/biji-tracker.c                 | 1130 ++++++++++++++--------------
 src/libbiji/biji-tracker.h                 |  162 ++--
 src/libbiji/provider/biji-local-note.c     |    8 +-
 src/libbiji/provider/biji-local-provider.c |   17 +-
 src/libbiji/provider/biji-memo-provider.c  |   11 +-
 src/libbiji/provider/biji-nextcloud-note.c |   11 +-
 13 files changed, 874 insertions(+), 775 deletions(-)
---
diff --git a/src/bjb-controller.c b/src/bjb-controller.c
index 7de35d7b..dd06d22e 100644
--- a/src/bjb-controller.c
+++ b/src/bjb-controller.c
@@ -24,6 +24,7 @@
  */
 
 #include "bjb-application.h"
+#include "biji-tracker.h"
 #include "bjb-controller.h"
 #include "bjb-window.h"
 
@@ -517,6 +518,18 @@ update_controller_callback (GList *result,
   }
 }
 
+static void
+on_controller_get_notes_cb (GObject      *object,
+                            GAsyncResult *result,
+                            gpointer      user_data)
+{
+  g_autoptr(BjbController) self = user_data;
+  g_autoptr(GList) notes = NULL;
+
+  notes = biji_tracker_get_notes_finish (BIJI_TRACKER (object), result, NULL);
+  update_controller_callback (notes, user_data);
+}
+
 void
 bjb_controller_apply_needle (BjbController *self)
 {
@@ -548,7 +561,10 @@ bjb_controller_apply_needle (BjbController *self)
   }
 
   /* There is a research, apply lookup */
-  biji_get_items_matching_async (self->manager, self->group, needle, update_controller_callback, self);
+  biji_tracker_get_notes_async (biji_manager_get_tracker (self->manager),
+                                self->group, needle,
+                                on_controller_get_notes_cb,
+                                g_object_ref (self));
 }
 
 static void
@@ -856,10 +872,11 @@ bjb_controller_set_notebook (BjbController *self,
 
   self->needle = g_strdup ("");
   self->notebook = coll;
-  biji_get_items_with_notebook_async (self->manager,
-                                      biji_item_get_title (BIJI_ITEM (coll)),
-                                      update_controller_callback,
-                                      self);
+
+  biji_tracker_get_notes_with_notebook_async (biji_manager_get_tracker (self->manager),
+                                              biji_item_get_title (BIJI_ITEM (coll)),
+                                              on_controller_get_notes_cb,
+                                              g_object_ref (self));
 }
 
 
diff --git a/src/bjb-organize-dialog.c b/src/bjb-organize-dialog.c
index cc9d2a39..eeba0530 100644
--- a/src/bjb-organize-dialog.c
+++ b/src/bjb-organize-dialog.c
@@ -18,6 +18,7 @@
 
 #include <glib/gi18n.h>
 
+#include "biji-tracker.h"
 #include "bjb-organize-dialog.h"
 #include "bjb-window.h"
 
@@ -152,15 +153,21 @@ bjb_get_path_for_str (GtkTreeModel  *model,
 }
 
 static void
-bjb_organize_dialog_handle_tags (GHashTable *result, gpointer user_data)
+on_get_notebooks_cb (GObject      *object,
+                     GAsyncResult *result,
+                     gpointer      user_data)
 {
-  BjbOrganizeDialog *self = BJB_ORGANIZE_DIALOG (user_data);
+  g_autoptr(BjbOrganizeDialog) self = user_data;
+  GHashTable *notebooks;
   GList *tracker_info;
 
+  g_assert (BJB_IS_ORGANIZE_DIALOG (self));
+
   if (self->notebooks)
     g_hash_table_destroy (self->notebooks);
 
-  self->notebooks = result;
+  notebooks = biji_tracker_get_notebooks_finish (BIJI_TRACKER (object), result, NULL);
+  self->notebooks = notebooks;
 
   tracker_info = g_hash_table_get_values (self->notebooks);
   tracker_info = g_list_sort (tracker_info, bjb_compare_notebook);
@@ -190,8 +197,8 @@ update_notebooks_model_async (BjbOrganizeDialog *self)
 
   manager = bjb_window_get_manager (GTK_WIDGET (self->window));
   gtk_list_store_clear (self->notebook_store);
-  biji_get_all_notebooks_async (manager,
-                                bjb_organize_dialog_handle_tags, NULL, self);
+  biji_tracker_get_notebooks_async (biji_manager_get_tracker (manager),
+                                    on_get_notebooks_cb, g_object_ref (self));
 }
 
 /* Libbiji handles tracker & saving */
@@ -258,12 +265,18 @@ on_tag_toggled (GtkCellRendererToggle *cell,
  * libbiji has to avoid creating a new one
  * and also check before tagging items */
 static void
-on_new_notebook_created_cb (BijiItem *coll, gpointer user_data)
+on_notebook_added_cb (GObject      *object,
+                      GAsyncResult *result,
+                      gpointer      user_data)
 {
-  BjbOrganizeDialog *self = user_data;
+  g_autoptr(BjbOrganizeDialog) self = user_data;
+  BijiItem *tag;
+
+  g_assert (BJB_IS_ORGANIZE_DIALOG (self));
 
+  tag = biji_tracker_add_notebook_finish (BIJI_TRACKER (object), result, NULL);
   self->tag_to_scroll_to = g_strdup (gtk_entry_get_text (GTK_ENTRY (self->entry)));
-  g_list_foreach (self->items, note_dialog_add_notebook, coll);
+  g_list_foreach (self->items, note_dialog_add_notebook, tag);
 
   update_notebooks_model_async (self);
   gtk_entry_set_text (GTK_ENTRY (self->entry), "");
@@ -278,8 +291,11 @@ add_new_tag (BjbOrganizeDialog *self)
   BijiManager *manager = bjb_window_get_manager (GTK_WIDGET (self->window));
   const gchar *title = gtk_entry_get_text (GTK_ENTRY (self->entry));
 
-  if (title && title[0])
-    biji_create_new_notebook_async (manager, title, on_new_notebook_created_cb, self);
+  if (title && *title)
+    biji_tracker_add_notebook_async (biji_manager_get_tracker (manager),
+                                     title,
+                                     on_notebook_added_cb,
+                                     g_object_ref (self));
 }
 
 static void
diff --git a/src/bjb-window.c b/src/bjb-window.c
index 75fc6579..7e715fb6 100644
--- a/src/bjb-window.c
+++ b/src/bjb-window.c
@@ -461,7 +461,7 @@ on_show_notebook_cb (GSimpleAction *action,
   else
   {
     manager = bjb_window_get_manager (GTK_WIDGET (self));
-    notebook = biji_manager_get_item_at_path (manager, note_uuid);
+    notebook = biji_manager_find_notebook (manager, note_uuid);
     bjb_controller_set_notebook (self->controller, BIJI_NOTEBOOK (notebook));
 
     /* Update headerbar title. */
@@ -588,21 +588,19 @@ append_notebook (BijiItem      *notebook,
 static void
 on_display_notebooks_changed (BjbWindow *self)
 {
-  g_autoptr(GList) children = NULL;
   BijiManager *manager;
   GListModel *notebooks;
-  GtkContainer *list;
   guint n_items;
 
   g_assert (BJB_IS_WINDOW (self));
 
   manager = bjb_window_get_manager (GTK_WIDGET (self));
   notebooks = biji_manager_get_notebooks (manager);
-  list = GTK_CONTAINER (self->notebooks_box);
-  children = gtk_container_get_children (list);
 
-  for (GList *child = children; child; child = child->next)
-    gtk_container_remove (list, child->data);
+  gtk_container_foreach (GTK_CONTAINER (self->notebooks_box),
+                         (GtkCallback) gtk_widget_destroy, NULL);
+
+  manager = bjb_window_get_manager (GTK_WIDGET (self));
 
   n_items = g_list_model_get_n_items (notebooks);
 
@@ -668,6 +666,7 @@ bjb_window_constructed (GObject *obj)
   gtk_stack_set_visible_child_name (self->main_stack, "spinner");
   gtk_widget_show (self->spinner);
   gtk_spinner_start (GTK_SPINNER (self->spinner));
+  gtk_widget_set_sensitive (self->notebooks_box, FALSE);
 
   self->no_note = bjb_empty_results_box_new ();
   gtk_stack_add_named (self->main_stack, self->no_note, "empty");
@@ -832,6 +831,8 @@ bjb_window_set_view (BjbWindow     *self,
 {
   g_return_if_fail (BJB_IS_WINDOW (self));
 
+  gtk_widget_set_sensitive (self->notebooks_box, FALSE);
+
   switch (view)
   {
 
@@ -846,11 +847,13 @@ bjb_window_set_view (BjbWindow     *self,
       hdy_leaflet_navigate (self->main_leaflet, HDY_NAVIGATION_DIRECTION_BACK);
       gtk_widget_show (GTK_WIDGET (self->search_bar));
       gtk_stack_set_visible_child_name (self->main_stack, "main-view");
+      gtk_widget_set_sensitive (self->notebooks_box, TRUE);
       break;
 
    case BJB_WINDOW_ARCHIVE_VIEW:
       gtk_widget_show (GTK_WIDGET (self->search_bar));
       gtk_stack_set_visible_child_name (self->main_stack, "main-view");
+      gtk_widget_set_sensitive (self->notebooks_box, TRUE);
       break;
 
     case BJB_WINDOW_SPINNER_VIEW:
@@ -865,6 +868,7 @@ bjb_window_set_view (BjbWindow     *self,
       gtk_widget_show (self->no_note);
       gtk_widget_hide (GTK_WIDGET (self->search_bar));
       gtk_stack_set_visible_child_name (self->main_stack, "empty");
+      gtk_widget_set_sensitive (self->notebooks_box, TRUE);
       break;
 
 
@@ -873,6 +877,7 @@ bjb_window_set_view (BjbWindow     *self,
                                       BJB_EMPTY_RESULTS_NO_RESULTS);
       gtk_widget_show (self->no_note);
       gtk_stack_set_visible_child_name (self->main_stack, "empty");
+      gtk_widget_set_sensitive (self->notebooks_box, TRUE);
       break;
 
 
diff --git a/src/libbiji/biji-manager.c b/src/libbiji/biji-manager.c
index 68e7f7e2..ca57bf97 100644
--- a/src/libbiji/biji-manager.c
+++ b/src/libbiji/biji-manager.c
@@ -54,7 +54,7 @@ struct _BijiManager
   gulong note_renamed ;
 
   GFile *location;
-  TrackerSparqlConnection *connection;
+  BijiTracker *tracker;
 
   GdkRGBA color;
 };
@@ -250,14 +250,22 @@ biji_manager_init (BijiManager *self)
    */
   self->providers = g_hash_table_new_full (g_str_hash, g_str_equal,
                                            NULL, g_object_unref);
+  self->tracker = biji_tracker_new (self);
 }
 
 TrackerSparqlConnection *
 biji_manager_get_tracker_connection (BijiManager *self)
 {
-  return self->connection;
+  return biji_tracker_get_connection (self->tracker);
 }
 
+gpointer
+biji_manager_get_tracker (BijiManager *self)
+{
+  g_return_val_if_fail (BIJI_IS_MANAGER (self), NULL);
+
+  return self->tracker;
+}
 
 void
 biji_manager_set_provider (BijiManager *self,
@@ -299,6 +307,7 @@ biji_manager_finalize (GObject *object)
 
   g_clear_object (&self->notebooks);
   g_clear_object (&self->location);
+  g_clear_object (&self->tracker);
   g_hash_table_destroy (self->items);
   g_hash_table_destroy (self->archives);
   g_hash_table_unref (self->providers);
@@ -714,6 +723,35 @@ biji_manager_get_notebooks (BijiManager *self)
   return G_LIST_MODEL (self->notebooks);
 }
 
+BijiItem *
+biji_manager_find_notebook (BijiManager *self,
+                            const char  *uuid)
+{
+  GListModel *notebooks;
+  guint n_items;
+
+  g_return_val_if_fail (BIJI_IS_MANAGER (self), NULL);
+  g_return_val_if_fail (uuid && *uuid, NULL);
+
+  notebooks = biji_manager_get_notebooks (self);
+  n_items = g_list_model_get_n_items (notebooks);
+
+  for (guint i = 0; i < n_items; i++)
+    {
+      g_autoptr(BijiItem) notebook = NULL;
+      const char *item_uuid;
+
+      notebook = g_list_model_get_item (notebooks, i);
+
+      item_uuid = biji_item_get_uuid (notebook);
+
+      if (g_strcmp0 (uuid, item_uuid) == 0)
+        return notebook;
+    }
+
+  return NULL;
+}
+
 BijiItem *
 biji_manager_get_item_at_path (BijiManager *self, const gchar *path)
 {
@@ -775,45 +813,11 @@ load_providers (GTask        *task,
 {
   BijiManager *self = source_object;
   GError *error = NULL;
-#ifdef TRACKER_PRIVATE_STORE
-  g_autofree char *filename = NULL;
-  g_autoptr(GFile) data_location = NULL;
-
-  filename = g_build_filename (g_get_user_cache_dir (),
-                               g_get_application_name (),
-#if HAVE_TRACKER3
-                               "tracker3",
-#else
-                               "tracker",
-#endif /* HAVE_TRACKER3 */
-                               NULL);
-  data_location = g_file_new_for_path (filename);
 
-  g_assert (BIJI_IS_MANAGER (self));
-  g_assert (G_IS_TASK (task));
-
-  /* If tracker fails for some reason,
-   * do not attempt anything */
-#if HAVE_TRACKER3
-  self->connection = tracker_sparql_connection_new (TRACKER_SPARQL_CONNECTION_FLAGS_NONE,
-                                                    data_location,
-                                                    tracker_sparql_get_ontology_nepomuk (),
-                                                    NULL,
-                                                    &error);
-#else
-  self->connection = tracker_sparql_connection_local_new (TRACKER_SPARQL_CONNECTION_FLAGS_NONE,
-                                                          data_location,
-                                                          NULL, NULL, NULL,
-                                                          &error);
-#endif /* HAVE_TRACKER3 */
-
-#else
-  self->connection = tracker_sparql_connection_get (NULL, &error);
-#endif /* TRACKER_PRIVATE_STORE */
-
-  if (error)
+  if (!biji_tracker_is_available (self->tracker))
     {
-      g_task_return_error (task, error);
+      g_task_return_new_error (task, G_IO_ERROR, G_IO_ERROR_FAILED,
+                               "Connecting to tracker failed");
       return;
     }
 
@@ -838,6 +842,36 @@ load_providers (GTask        *task,
   return;
 }
 
+static void
+on_tracker_get_notebooks_cb (GObject      *object,
+                             GAsyncResult *result,
+                             gpointer      user_data)
+{
+  g_autoptr(BijiManager) self = user_data;
+  g_autoptr(GHashTable) notebooks = NULL;
+  g_autoptr(GList) values = NULL;
+
+  g_assert (BIJI_IS_MANAGER (self));
+
+  notebooks = biji_tracker_get_notebooks_finish (BIJI_TRACKER (object), result, NULL);
+
+  if (!notebooks)
+    return;
+
+  values = g_hash_table_get_values (notebooks);
+
+  for (GList *value = values; value; value = value->next)
+    {
+      g_autoptr(BijiNotebook) notebook = NULL;
+      BijiInfoSet *set = value->data;
+
+      notebook = biji_notebook_new (G_OBJECT (self), set->tracker_urn,
+                                    set->title, set->mtime);
+      g_list_store_insert_sorted (self->notebooks, notebook,
+                                  compare_notebook, NULL);
+    }
+}
+
 void
 biji_manager_load_providers_async (BijiManager         *self,
                                    GAsyncReadyCallback  callback,
@@ -853,6 +887,9 @@ biji_manager_load_providers_async (BijiManager         *self,
   g_task_set_task_data (thread_task, task, g_object_unref);
 
   g_task_run_in_thread (thread_task, load_providers);
+  biji_tracker_get_notebooks_async (self->tracker,
+                                    on_tracker_get_notebooks_cb,
+                                    g_object_ref (self));
 }
 
 gboolean
diff --git a/src/libbiji/biji-manager.h b/src/libbiji/biji-manager.h
index 7ef784bb..0471d0fe 100644
--- a/src/libbiji/biji-manager.h
+++ b/src/libbiji/biji-manager.h
@@ -59,6 +59,7 @@ void             biji_manager_set_provider          (BijiManager *manager,
 GList           *biji_manager_get_providers         (BijiManager *manager); /* <ProviderInfo*> */
 TrackerSparqlConnection
                 *biji_manager_get_tracker_connection (BijiManager *manager);
+gpointer         biji_manager_get_tracker            (BijiManager *self);
 
 
 
@@ -92,6 +93,8 @@ BijiItem        *biji_manager_get_item_at_path      (BijiManager *manager,
 GList           *biji_manager_get_items             (BijiManager         *manager,
                                                      BijiItemsGroup       group);
 GListModel      *biji_manager_get_notebooks         (BijiManager         *self);
+BijiItem        *biji_manager_find_notebook         (BijiManager         *self,
+                                                     const char          *uuid);
 
 BijiNoteObj     *biji_manager_note_new              (BijiManager *manager,
                                                      const gchar *str,
diff --git a/src/libbiji/biji-note-obj.c b/src/libbiji/biji-note-obj.c
index 5686233c..ac227902 100644
--- a/src/libbiji/biji-note-obj.c
+++ b/src/libbiji/biji-note-obj.c
@@ -18,9 +18,9 @@
 #include "biji-date-time.h"
 #include "biji-manager.h"
 #include "../bjb-utils.h"
+#include "libbiji.h"
 #include "biji-note-obj.h"
 #include "biji-timeout.h"
-#include "biji-tracker.h"
 
 #include "editor/biji-webkit-editor.h"
 
@@ -452,13 +452,15 @@ has_notebook (BijiItem *item,
 }
 
 static void
-_biji_notebook_refresh (gboolean query_result,
-                        gpointer coll)
+on_note_obj_add_notebook_cb (GObject      *object,
+                             GAsyncResult *result,
+                             gpointer      user_data)
 {
-  g_return_if_fail (BIJI_IS_NOTEBOOK (coll));
+  g_autoptr(BijiNotebook) notebook = user_data;
 
-  if (query_result)
-    biji_notebook_refresh (BIJI_NOTEBOOK (coll));
+  g_assert (BIJI_IS_NOTEBOOK (notebook));
+
+  biji_notebook_refresh (notebook);
 }
 
 static gboolean
@@ -484,7 +486,13 @@ add_notebook (BijiItem *item,
 
   if (BIJI_IS_NOTEBOOK (notebook))
     {
-      biji_push_existing_notebook_to_note (self, label, _biji_notebook_refresh, notebook); // Tracker
+      BijiManager *manager;
+
+      manager = biji_item_get_manager (item);
+      biji_tracker_add_note_to_notebook_async (biji_manager_get_tracker (manager),
+                                               self, label, on_note_obj_add_notebook_cb,
+                                               g_object_ref (notebook));
+
       biji_note_obj_set_last_metadata_change_date (self, g_get_real_time () / G_USEC_PER_SEC);
       biji_note_obj_save_note (self);
     }
@@ -507,7 +515,13 @@ remove_notebook (BijiItem *item,
 
   if (g_hash_table_remove (priv->labels, biji_item_get_title (notebook)))
     {
-      biji_remove_notebook_from_note (self, notebook, _biji_notebook_refresh, notebook); // tracker.
+      BijiManager *manager;
+
+      manager = biji_item_get_manager (item);
+      biji_tracker_remove_note_notebook_async (biji_manager_get_tracker (manager),
+                                               self, notebook, on_note_obj_add_notebook_cb,
+                                               g_object_ref (notebook));
+
       biji_note_obj_set_last_metadata_change_date (self, g_get_real_time () / G_USEC_PER_SEC);
       biji_note_obj_save_note (self);
       return TRUE;
diff --git a/src/libbiji/biji-notebook.c b/src/libbiji/biji-notebook.c
index 84084c3a..9904e4db 100644
--- a/src/libbiji/biji-notebook.c
+++ b/src/libbiji/biji-notebook.c
@@ -32,11 +32,11 @@
 
 #include <math.h>
 
+#include "libbiji.h"
 #include "biji-notebook.h"
-#include "biji-tracker.h"
 
 
-static void biji_notebook_update_collected (GList *result, gpointer user_data);
+static void on_collected_item_change (BijiNotebook *self);
 
 
 struct _BijiNotebook
@@ -270,10 +270,14 @@ static gboolean
 biji_notebook_trash (BijiItem *item)
 {
   BijiNotebook *self;
+  BijiManager *manager;
+
   g_return_val_if_fail (BIJI_IS_NOTEBOOK (item), FALSE);
 
   self = BIJI_NOTEBOOK (item);
-  biji_remove_notebook_from_tracker (biji_item_get_manager (item), self->urn);
+  manager = biji_item_get_manager (item);
+
+  biji_tracker_remove_notebook (biji_manager_get_tracker (manager), self->urn);
 
   return TRUE;
 }
@@ -374,52 +378,52 @@ biji_notebook_get_property (GObject    *object,
 
 
 static void
-on_collected_item_change (BijiNotebook *self)
+on_notebook_get_notes_cb (GObject      *object,
+                          GAsyncResult *result,
+                          gpointer      user_data)
 {
-  BijiManager *manager;
-  GList *l;
+  g_autoptr(BijiNotebook) self = user_data;
+  GList *notes;
 
-  manager = biji_item_get_manager (BIJI_ITEM (self));
+  g_clear_pointer (&self->collected_items, g_list_free);
+  g_clear_pointer (&self->icon, cairo_surface_destroy);
+  g_clear_pointer (&self->emblem, cairo_surface_destroy);
 
-  /* Disconnected any handler */
-  for (l = self->collected_items; l != NULL; l = l->next)
-  {
-    g_signal_handlers_disconnect_by_func (l->data, on_collected_item_change, self);
-  }
+  notes = biji_tracker_get_notes_with_notebook_finish (BIJI_TRACKER (object), result, NULL);
+  self->collected_items = notes;
 
-  /* Then re-process the whole stuff */
-  biji_get_items_with_notebook_async (manager,
-                                      self->name,
-                                      biji_notebook_update_collected,
-                                      self);
+  /* Connect */
+  for (GList *l = self->collected_items; l != NULL; l = l->next)
+    {
+      g_signal_connect_swapped (l->data, "color-changed",
+                                G_CALLBACK (on_collected_item_change), self);
+
+      g_signal_connect_swapped (l->data, "trashed",
+                                G_CALLBACK (on_collected_item_change), self);
+    }
+
+  g_signal_emit (self, biji_notebooks_signals[NOTEBOOK_ICON_UPDATED], 0);
 }
 
-/* For convenience, items are retrieved async.
- * Thus use a signal once icon & emblem updated.*/
 static void
-biji_notebook_update_collected (GList *result,
-                                  gpointer user_data)
+on_collected_item_change (BijiNotebook *self)
 {
-  BijiNotebook *self = user_data;
+  BijiManager *manager;
   GList *l;
 
-  g_clear_pointer (&self->collected_items, g_list_free);
-  g_clear_pointer (&self->icon, cairo_surface_destroy);
-  g_clear_pointer (&self->emblem, cairo_surface_destroy);
-
-  self->collected_items = result;
+  manager = biji_item_get_manager (BIJI_ITEM (self));
 
-  /* Connect */
+  /* Disconnected any handler */
   for (l = self->collected_items; l != NULL; l = l->next)
   {
-    g_signal_connect_swapped (l->data, "color-changed",
-                              G_CALLBACK (on_collected_item_change), self);
-
-    g_signal_connect_swapped (l->data, "trashed",
-                              G_CALLBACK (on_collected_item_change), self);
+    g_signal_handlers_disconnect_by_func (l->data, on_collected_item_change, self);
   }
 
-  g_signal_emit (self, biji_notebooks_signals[NOTEBOOK_ICON_UPDATED], 0);
+  /* Then re-process the whole stuff */
+  biji_tracker_get_notes_with_notebook_async (biji_manager_get_tracker (manager),
+                                              self->name,
+                                              on_notebook_get_notes_cb,
+                                              g_object_ref (self));
 }
 
 void
@@ -437,10 +441,10 @@ biji_notebook_constructed (GObject *obj)
 
   manager = biji_item_get_manager (BIJI_ITEM (obj));
 
-  biji_get_items_with_notebook_async (manager,
-                                      self->name,
-                                      biji_notebook_update_collected,
-                                      self);
+  biji_tracker_get_notes_with_notebook_async (biji_manager_get_tracker (manager),
+                                              self->name,
+                                              on_notebook_get_notes_cb,
+                                              g_object_ref (self));
 }
 
 static gboolean
diff --git a/src/libbiji/biji-tracker.c b/src/libbiji/biji-tracker.c
index 9b038dc0..6a2d5b4d 100644
--- a/src/libbiji/biji-tracker.c
+++ b/src/libbiji/biji-tracker.c
@@ -1,5 +1,8 @@
+/* -*- mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*- */
 /* biji-tracker.c
+ *
  * Copyright (C) Pierre-Yves LUYTEN 2012, 2013 <py luyten fr>
+ * Copyright 2021 Mohammed Sadiq <sadiq sadiqpk org>
  *
  * bijiben is free software: you can redistribute it and/or modify it
  * under the terms of the GNU General Public License as published by the
@@ -17,12 +20,17 @@
 
 #define G_LOG_DOMAIN "bjb-tracker"
 
-#include "config.h"
 
-#include "biji-item.h"
-#include "biji-tracker.h"
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
 
+#include <stdbool.h>
+#include <tracker-sparql.h>
 
+#include "biji-date-time.h"
+#include "biji-notebook.h"
+#include "biji-tracker.h"
 
 typedef enum
 {
@@ -32,731 +40,723 @@ typedef enum
   BIJI_CONTENT_COL,
   BIJI_CREATED_COL,
   BIJI_NO_COL
-
 } BijiTrackerColumns;
 
+struct _BijiTracker
+{
+  GObject parent_instance;
 
+  BijiManager  *manager;
 
-/* To perform something after async tracker query
- * TODO : implement this with GObject */
-typedef struct {
+  GCancellable *cancellable;
+  TrackerSparqlConnection *connection;
+};
 
-  BijiManager *manager;
+G_DEFINE_TYPE (BijiTracker, biji_tracker, G_TYPE_OBJECT)
 
-  /* usually a query */
+static void
+on_add_notebook_cb (GObject      *object,
+                    GAsyncResult *result,
+                    gpointer      user_data)
+{
+  BijiTracker *self;
+  g_autoptr(GTask) task = user_data;
+  g_autofree char *key = NULL;
+  g_autofree char *val = NULL;
+  GVariant *variant, *child;
+  BijiNotebook *notebook = NULL;
+  char *notebook_str, *urn = NULL;
+  GError *error = NULL;
 
-  gchar *str;
-  BijiInfoSet *info;
+  g_assert (G_IS_TASK (task));
 
-  /* after the query, _one of_ the callbacks */
+  self = g_task_get_source_object (task);
+  g_assert (BIJI_IS_TRACKER (self));
 
-  BijiBoolCallback        bool_cb;
-  BijiInfoCallback        info_cb;
-  BijiItemCallback        item_cb;
-  BijiItemsListCallback   list_cb;
-  BijiInfoSetsHCallback   hash_cb;
+  /* variant is aaa{ss} */
+  /* https://gitlab.gnome.org/GNOME/tracker/-/blob/tracker-2.3/src/libtracker-data/tracker-sparql.c#L6693 */
+  variant = tracker_sparql_connection_update_blank_finish (self->connection, result, &error);
 
+  if (error)
+    {
+      g_warning ("Unable to create notebook: %s", error->message);
+      g_task_return_error (task, error);
+      return;
+    }
 
-  gpointer user_data;
+  child = g_variant_get_child_value (variant, 0); /* variant is now aa{ss} */
+  g_variant_unref (variant);
+  variant = child;
 
-} BijiTrackerFinisher;
+  child = g_variant_get_child_value (variant, 0); /* variant is now a{ss} */
+  g_variant_unref (variant);
+  variant = child;
 
+  child = g_variant_get_child_value (variant, 0); /* variant is now {ss} */
+  g_variant_unref (variant);
+  variant = child;
 
-/* finisher stores _one of the possible callbacks _
- * we could cast as well */
+  child = g_variant_get_child_value (variant, 0);
+  key = g_variant_dup_string (child, NULL); /* get the key of dictionary */
+  g_variant_unref (child);
 
+  child = g_variant_get_child_value (variant, 1); /* get the value of dict */
+  val = g_variant_dup_string (child, NULL);
+  g_variant_unref (child);
 
-static BijiTrackerFinisher *
-biji_tracker_finisher_new (BijiManager          *manager,
-                           gchar                 *str,
-                           BijiInfoSet           *info,
-                           BijiBoolCallback       bool_cb,
-                           BijiInfoCallback       info_cb,
-                           BijiItemCallback       item_cb,
-                           BijiItemsListCallback  list_cb,
-                           BijiInfoSetsHCallback  hash_cb,
-                           gpointer               user_data)
-{
-  BijiTrackerFinisher *retval = g_slice_new (BijiTrackerFinisher);
-
-  retval->manager = manager;
-  retval->str = str;
-  retval->info = info;
-  retval->bool_cb = bool_cb;
-  retval->info_cb = info_cb;
-  retval->item_cb = item_cb;
-  retval->list_cb = list_cb;
-  retval->hash_cb = hash_cb;
-  retval->user_data = user_data;
-
-  return retval;
-}
+  g_variant_unref (variant);
 
+  if (g_strcmp0 (key, "res") == 0)
+    urn = val;
 
-/* Only heap is str */
+  if (urn)
+    {
+      notebook_str = g_task_get_task_data (task);
+      notebook = biji_notebook_new (G_OBJECT (self->manager),
+                                    urn, notebook_str,
+                                    g_get_real_time () / G_USEC_PER_SEC);
+      biji_manager_add_item (self->manager, BIJI_ITEM (notebook), BIJI_LIVING_ITEMS, true);
+    }
 
-static void
-biji_tracker_finisher_free (BijiTrackerFinisher *f)
-{
-  g_clear_pointer (&f->str, g_free);
-  g_slice_free (BijiTrackerFinisher, f);
+  g_task_return_pointer (task, notebook, NULL);
 }
 
-
-static TrackerSparqlConnection*
-get_connection (BijiManager *manager)
+static void
+add_or_update_note (BijiTracker *self,
+                    BijiInfoSet *info,
+                    const char  *urn_uuid)
 {
-  return biji_manager_get_tracker_connection (manager);
+  g_autoptr(GDateTime) dt_created = NULL;
+  g_autoptr(GDateTime) dt_mtime = NULL;
+  g_autofree char *created = NULL;
+  g_autofree char *mtime = NULL;
+  g_autofree char *query = NULL;
+  g_autofree char *content = NULL;
+  const char *info_content;
+
+  g_assert (BIJI_IS_TRACKER (self));
+  g_assert (info);
+
+  info_content = info->content;
+  dt_created = g_date_time_new_from_unix_utc (info->created);
+  dt_mtime = g_date_time_new_from_unix_utc (info->mtime);
+  created = g_date_time_format_iso8601 (dt_created);
+  mtime = g_date_time_format_iso8601 (dt_mtime);
+
+  if (!info_content)
+    info_content = "";
+  content = g_strdelimit (tracker_sparql_escape_string (info_content), "\n'", ' ');
+
+  if (urn_uuid)
+    query = g_strdup_printf ("INSERT OR REPLACE { "
+                             "<%s> a nfo:Note , nie:DataObject ; "
+                             "nie:url '%s' ; "
+                             "nie:contentLastModified '%s' ; "
+                             "nie:contentCreated '%s' ; "
+                             "nie:title '%s' ; "
+                             "nie:plainTextContent '%s' ; "
+                             "nie:dataSource '%s' ;"
+                             "nie:generator 'Bijiben' . }",
+                             urn_uuid,
+                             info->url,
+                             mtime,
+                             created,
+                             info->title,
+                             content,
+                             info->datasource_urn);
+  else
+    query = g_strconcat ("INSERT { "
+                         "_:res a nfo:Note ; ",
+                         "a nie:DataObject ; "
+                         "nie:contentLastModified '", mtime, "' ; "
+                         "nie:contentCreated '", created, "' ; "
+                         "nie:title '", info->title, "' ; "
+                         "nie:url  '", info->url, "' ; "
+                         "nie:plainTextContent '", content, "' ; "
+                         "nie:dataSource '", info->datasource_urn, "' ; "
+                         "nie:generator 'Bijiben' }",
+                         NULL);
+
+  if (urn_uuid)
+    tracker_sparql_connection_update_async (self->connection, query,
+#if !HAVE_TRACKER3
+                                            0,     // priority
+#endif
+                                            NULL, NULL, NULL);
+  else
+    tracker_sparql_connection_update_blank_async (self->connection, query,
+#if !HAVE_TRACKER3
+                                                  G_PRIORITY_DEFAULT,
+#endif
+                                                  NULL, NULL, NULL);
 }
 
-
-/* TODO : populate the boolean */
-
 static void
-biji_finish_update (GObject *source_object,
-                    GAsyncResult *res,
-                    gpointer user_data)
+on_save_note_cb (GObject      *object,
+                 GAsyncResult *result,
+                 gpointer      user_data)
 {
-  TrackerSparqlConnection *self;
-  BijiTrackerFinisher *finisher;
-  GError *error;
-  gchar *query;
+  BijiTracker *self;
+  g_autoptr(GTask) task = user_data;
+  g_autoptr(TrackerSparqlCursor) cursor = NULL;
+  g_autoptr(GError) error = NULL;
+  g_autofree char *match = NULL;
+  BijiInfoSet *info;
 
+  g_assert (G_IS_TASK (task));
 
-  self = TRACKER_SPARQL_CONNECTION (source_object);
-  finisher = user_data;
-  error = NULL;
-  query = finisher->str;
+  self = g_task_get_source_object (task);
+  g_assert (BIJI_IS_TRACKER (self));
 
-  tracker_sparql_connection_update_finish (self, res, &error);
+  cursor = tracker_sparql_connection_query_finish (self->connection, result, &error);
+  info = g_task_get_task_data (task);
 
   if (error)
-  {
-    g_warning ("%s : query=|||%s|||", error->message, query);
-    g_error_free (error);
-  }
+    {
+      g_warning ("Tracker: %s", error->message);
+      return;
+    };
 
-  /* See if the query has something to perform afterward */
-  if (finisher->bool_cb)
-    finisher->bool_cb (TRUE, finisher->user_data);
+  if (tracker_sparql_cursor_next (cursor, NULL, NULL))
+    match = g_strdup (tracker_sparql_cursor_get_string (cursor, 0, NULL));
 
-  biji_tracker_finisher_free (finisher);
+  add_or_update_note (self, info, match);
 }
 
-
 static void
-biji_perform_update_async_and_free (TrackerSparqlConnection *connection,
-                                    gchar *query,
-                                    BijiBoolCallback f,
-                                    gpointer user_data)
+on_get_list_async_cb (GObject      *object,
+                      GAsyncResult *result,
+                      gpointer      user_data)
 {
-  BijiTrackerFinisher *finisher;
-
-  finisher = biji_tracker_finisher_new
-              (NULL, query, NULL, f, NULL, NULL, NULL, NULL, user_data);
-  tracker_sparql_connection_update_async (connection,
-                                          query,
-#if !HAVE_TRACKER3
-                                          0,     // priority
-#endif
-                                          NULL,
-                                          biji_finish_update,
-                                          finisher);
-}
+  BijiTracker *self;
+  g_autoptr(GTask) task = user_data;
+  g_autoptr(TrackerSparqlCursor) cursor = NULL;
+  GError *error = NULL;
+  GList *items = NULL;
 
+  g_assert (G_IS_TASK (task));
 
-/* Don't worry too much. We just want plain text here */
-static gchar *
-tracker_str (const gchar * string )
-{
-  g_return_val_if_fail (string != NULL, g_strdup (""));
+  self = g_task_get_source_object (task);
+  g_assert (BIJI_IS_TRACKER (self));
 
-  return biji_str_mass_replace (string, "\n", " ", "'", " ", NULL);
-}
+  cursor = tracker_sparql_connection_query_finish (self->connection, result, &error);
 
+  if (error)
+    {
+      g_task_return_error (task, error);
+      return;
+    }
 
+  if (cursor)
+    {
+      BijiItem *item = NULL;
+      const char *full_path;
+      char *path;
+
+      while (tracker_sparql_cursor_next (cursor, self->cancellable, NULL))
+        {
+          full_path = tracker_sparql_cursor_get_string (cursor, 0, NULL);
+
+          if (g_str_has_prefix (full_path, "file://"))
+            {
+              GString *string;
+
+              string = g_string_new (full_path);
+              g_string_erase (string, 0, strlen ("file://"));
+              path = g_string_free (string, false);
+            }
+          else
+            path = g_strdup (full_path);
+
+          item = biji_manager_get_item_at_path (self->manager, path);
+
+          /* Sorting is done in another place */
+          if (item)
+            items = g_list_prepend (items, item);
+        }
+    }
 
-static gchar *
-get_note_url (BijiNoteObj *note)
-{
-  return g_strdup_printf ("file://%s", biji_item_get_uuid (BIJI_ITEM (note)));
+  g_task_return_pointer (task, items, (GDestroyNotify)g_list_free);
 }
 
-
-
 static void
-biji_query_info_hash_finish (GObject      *source_object,
-                             GAsyncResult *res,
-                             gpointer      user_data)
+on_tracker_update_cb (GObject      *object,
+                      GAsyncResult *result,
+                      gpointer      user_data)
 {
-  TrackerSparqlConnection *self;
-  TrackerSparqlCursor *cursor;
-  GError *error;
-  GHashTable *result;
-  BijiTrackerFinisher *finisher;
-
-  self = TRACKER_SPARQL_CONNECTION (source_object);
-  finisher = (BijiTrackerFinisher*) user_data;
-  error = NULL;
-  result = g_hash_table_new_full (
-    g_str_hash, g_str_equal, NULL, (GDestroyNotify) biji_info_set_free);
-
-  cursor = tracker_sparql_connection_query_finish (self,
-                                                   res,
-                                                   &error);
+  BijiTracker *self;
+  g_autoptr(GTask) task = user_data;
+  GError *error = NULL;
 
-  if (error)
-  {
-    g_warning ("%s", error->message);
-    g_error_free (error);
-  }
+  g_assert (G_IS_TASK (task));
 
-  if (cursor)
-  {
+  self = g_task_get_source_object (task);
+  g_assert (BIJI_IS_TRACKER (self));
 
-    while (tracker_sparql_cursor_next (cursor, NULL, NULL))
-    {
-      BijiInfoSet *set = biji_info_set_new ();
+  tracker_sparql_connection_update_finish (self->connection, result, &error);
 
-      set->tracker_urn = g_strdup (tracker_sparql_cursor_get_string (cursor, BIJI_URN_COL, NULL));
-      set->title = g_strdup (tracker_sparql_cursor_get_string (cursor, BIJI_TITLE_COL, NULL));
-      set->mtime = iso8601_to_gint64 (tracker_sparql_cursor_get_string (cursor, BIJI_MTIME_COL, NULL));
-
-      g_hash_table_replace (result, set->tracker_urn, set);
+  if (error)
+    {
+      g_warning ("%s", error->message);
+      g_task_return_error (task, error);
     }
-
-    g_object_unref (cursor);
-  }
-
-  finisher->hash_cb (result, finisher->user_data);
-  biji_tracker_finisher_free (finisher);
-  return;
+  else
+    g_task_return_boolean (task, true);
 }
 
-
 static void
-biji_query_items_list_finish (GObject              *source_object,
-                              GAsyncResult         *res,
-                              gpointer              user_data)
+on_get_notebooks_async_cb (GObject      *object,
+                           GAsyncResult *result,
+                           gpointer      user_data)
 {
-  TrackerSparqlConnection *self;
-  TrackerSparqlCursor *cursor;
-  BijiTrackerFinisher  *finisher;
-  GError *error;
-  GList *result;
+  BijiTracker *self;
+  g_autoptr(GTask) task = user_data;
+  g_autoptr(TrackerSparqlCursor) cursor = NULL;
+  GHashTable *items = NULL;
+  GError *error = NULL;
 
-  self =  TRACKER_SPARQL_CONNECTION (source_object);
-  result = NULL;
-  error = NULL;
-  finisher = (BijiTrackerFinisher *) user_data;
+  g_assert (G_IS_TASK (task));
 
-  if (finisher->list_cb == NULL)
-    return;
+  self = g_task_get_source_object (task);
+  g_assert (BIJI_IS_TRACKER (self));
 
-  cursor = tracker_sparql_connection_query_finish (self, res, &error);
+  cursor = tracker_sparql_connection_query_finish (self->connection, result, &error);
 
   if (error)
-  {
-    g_warning ("%s", error->message);
-    g_error_free (error);
-  }
-
-  if (cursor)
-  {
-    const gchar *full_path;
-    gchar *path;
-    BijiItem *item = NULL;
-
-    while (tracker_sparql_cursor_next (cursor, NULL, NULL))
     {
-      full_path = tracker_sparql_cursor_get_string (cursor, 0, NULL);
-
-      if (g_str_has_prefix (full_path, "file://"))
-      {
-        GString *string;
+      g_task_return_error (task, error);
+      return;
+    }
 
-        string = g_string_new (full_path);
-        g_string_erase (string, 0, 7);
-        path = g_string_free (string, FALSE);
-      }
+  items = g_hash_table_new_full (g_str_hash, g_str_equal, NULL,
+                                 (GDestroyNotify) biji_info_set_free);
 
-      else
-      {
-        path = g_strdup (full_path);
-      }
+  while (tracker_sparql_cursor_next (cursor, self->cancellable, NULL))
+    {
+      BijiInfoSet *set = biji_info_set_new ();
 
-      item = biji_manager_get_item_at_path (finisher->manager, path);
+      set->tracker_urn = g_strdup (tracker_sparql_cursor_get_string (cursor, BIJI_URN_COL, NULL));
+      set->title = g_strdup (tracker_sparql_cursor_get_string (cursor, BIJI_TITLE_COL, NULL));
+      set->mtime = iso8601_to_gint64 (tracker_sparql_cursor_get_string (cursor, BIJI_MTIME_COL, NULL));
 
-      /* Sorting is done in another place */
-      if (item)
-        result = g_list_prepend (result, item);
+      g_hash_table_replace (items, set->tracker_urn, set);
     }
 
-    g_object_unref (cursor);
-  }
-
-  finisher->list_cb (result, finisher->user_data);
-  biji_tracker_finisher_free (finisher);
+  g_task_return_pointer (task, items, (GDestroyNotify)g_hash_table_unref);
 }
 
-
-
 static void
-bjb_query_async (BijiManager           *manager,
-                 gchar                  *query,
-                 BijiInfoSetsHCallback   hash_cb,
-                 BijiItemsListCallback  list_cb,
-                 gpointer                user_data)
+biji_tracker_finalize (GObject *object)
 {
-  BijiTrackerFinisher     *finisher;
-  GAsyncReadyCallback     callback = NULL;
-
-  finisher = biji_tracker_finisher_new (manager, NULL, NULL, NULL, NULL, NULL, list_cb, hash_cb, user_data);
+  BijiTracker *self = (BijiTracker *)object;
 
-  if (hash_cb != NULL)
-    callback = biji_query_info_hash_finish;
+  g_cancellable_cancel (self->cancellable);
+  g_clear_object (&self->cancellable);
+  g_clear_object (&self->connection);
 
-  else if (list_cb != NULL)
-    callback = biji_query_items_list_finish;
-
-  if (callback)
-   tracker_sparql_connection_query_async (
-      get_connection (manager), query, NULL, callback, finisher);
+  G_OBJECT_CLASS (biji_tracker_parent_class)->finalize (object);
 }
 
-
-void
-biji_get_all_notebooks_async (BijiManager          *manager,
-                              BijiInfoSetsHCallback hash_cb,
-                              BijiItemsListCallback list_cb,
-                              gpointer              user_data)
+static void
+biji_tracker_class_init (BijiTrackerClass *klass)
 {
-  gchar *query = g_strconcat (
-    "SELECT ?c ?title ?mtime ",
-    "WHERE { ?c a nfo:DataContainer ;",
-    "nie:title ?title ; ",
-    "nie:contentLastModified ?mtime ;"
-    "nie:generator 'Bijiben'}",
-    NULL);
-
-  bjb_query_async (manager, query, hash_cb, list_cb, user_data);
+  GObjectClass *object_class = G_OBJECT_CLASS (klass);
 
-  g_free (query);
+  object_class->finalize = biji_tracker_finalize;
 }
 
-
-
-
-/* FIXME: returns file://$PATH while we want $PATH
- *        workaround in biji_query_items_list_finish */
-void
-biji_get_items_with_notebook_async (BijiManager          *manager,
-                                      const gchar           *notebook,
-                                      BijiItemsListCallback  list_cb,
-                                      gpointer               user_data)
+static void
+biji_tracker_init (BijiTracker *self)
 {
-  gchar *query;
+  g_autofree char *filename = NULL;
+  g_autoptr(GFile) location = NULL;
+  g_autoptr(GError) error = NULL;
+
+  self->cancellable = g_cancellable_new ();
+
+#ifdef TRACKER_PRIVATE_STORE
+  filename = g_build_filename (g_get_user_cache_dir (),
+                               g_get_application_name (),
+#if HAVE_TRACKER3
+                               "tracker3",
+#else
+                               "tracker",
+#endif /* HAVE_TRACKER3 */
+                               NULL);
 
-  query = g_strdup_printf ("SELECT ?s WHERE {?c nie:isPartOf ?s; nie:title '%s'}",
-                           notebook);
+  location = g_file_new_for_path (filename);
+
+  /* If tracker fails for some reason,
+   * do not attempt anything */
+#if HAVE_TRACKER3
+  self->connection = tracker_sparql_connection_new (TRACKER_SPARQL_CONNECTION_FLAGS_NONE,
+                                                    location,
+                                                    tracker_sparql_get_ontology_nepomuk (),
+                                                    self->cancellable,
+                                                    &error);
+#else
+  self->connection = tracker_sparql_connection_local_new (TRACKER_SPARQL_CONNECTION_FLAGS_NONE,
+                                                          location,
+                                                          NULL, NULL,
+                                                          self->cancellable,
+                                                          &error);
+#endif /* HAVE_TRACKER3 */
+
+#else
+  self->connection = tracker_sparql_connection_get (self->cancellable, &error);
+#endif /* TRACKER_PRIVATE_STORE */
 
-  bjb_query_async (manager, query, NULL, list_cb, user_data);
+  if (error)
+    g_warning ("Error: %s", error->message);
 }
 
+BijiTracker *
+biji_tracker_new (BijiManager *manager)
+{
+  BijiTracker *self;
 
+  g_return_val_if_fail (BIJI_IS_MANAGER (manager), NULL);
 
+  self = g_object_new (BIJI_TYPE_TRACKER, NULL);
+  self->manager = manager;
+  g_object_add_weak_pointer (G_OBJECT (self->manager),
+                             (gpointer *)&self->manager);
 
-void
-biji_get_items_matching_async (BijiManager           *manager,
-                               BijiItemsGroup         group,
-                               gchar                 *needle,
-                               BijiItemsListCallback  list_cb,
-                               gpointer               user_data)
-{
-  gchar *lower;
-  gchar *query;
-
-
-  lower = g_utf8_strdown (needle, -1);
-
-  /* We want to retrieve the key that noteBook uses.
-   * for notes: that is url. A file path is unique.
-   * for notebooks: we have no url, directly use urn:uuid */
-
-  query = g_strconcat (
-    "SELECT tracker:coalesce (?url, ?urn) WHERE ",
-    "{",
-    "  {  ?urn a nfo:Note",
-    "    .?urn nie:title ?title",
-    "    .?urn nie:plainTextContent ?content",
-    "    .?urn nie:url ?url",
-    "    .?urn nie:generator 'Bijiben'",
-    "    .FILTER (",
-    "    fn:contains (fn:lower-case (?content), '", lower, "' ) || ",
-    "    fn:contains (fn:lower-case (?title)  , '", lower, "'))} ",
-    "UNION",
-    "  {  ?urn a nfo:DataContainer",
-    "    .?urn nie:title ?title",
-    "    .?urn nie:generator 'Bijiben'",
-    "    .FILTER (",
-    "    fn:contains (fn:lower-case (?title), '", lower, "'))}",
-    "}",
-    NULL);
-
-  g_free (lower);
-  bjb_query_async (manager, query, NULL, list_cb, user_data);
+  return self;
 }
 
-
-static void
-on_new_notebook_query_executed (GObject *source_object, GAsyncResult *res, gpointer user_data)
+gboolean
+biji_tracker_is_available (BijiTracker *self)
 {
-  BijiTrackerFinisher *finisher = user_data;
-  TrackerSparqlConnection *connection = TRACKER_SPARQL_CONNECTION (source_object);
-  GError *error;
-  GVariant *variant;
-  GVariant *child;
-  gchar *key = NULL;
-  gchar *val = NULL;
-  gchar *urn = NULL;
-  BijiNotebook *notebook = NULL;
-
-  error = NULL;
-  variant = tracker_sparql_connection_update_blank_finish (connection, res, &error);
-  if (error != NULL)
-    {
-      g_warning ("Unable to create notebook: %s", error->message);
-      g_error_free (error);
-      goto out;
-    }
-
-  child = g_variant_get_child_value (variant, 0); /* variant is now aa{ss} */
-  g_variant_unref (variant);
-  variant = child;
-
-  child = g_variant_get_child_value (variant, 0); /* variant is now s{ss} */
-  g_variant_unref (variant);
-  variant = child;
-
-  child = g_variant_get_child_value (variant, 0); /* variant is now {ss} */
-  g_variant_unref (variant);
-  variant = child;
-
-  child = g_variant_get_child_value (variant, 0);
-  key = g_variant_dup_string (child, NULL);
-  g_variant_unref (child);
+  g_return_val_if_fail (BIJI_IS_TRACKER (self), false);
 
-  child = g_variant_get_child_value (variant, 1);
-  val = g_variant_dup_string (child, NULL);
-  g_variant_unref (child);
-
-  g_variant_unref (variant);
+  return self->connection != NULL;
+}
 
-  if (g_strcmp0 (key, "res") == 0)
-    urn = val;
+TrackerSparqlConnection *
+biji_tracker_get_connection (BijiTracker *self)
+{
+  g_return_val_if_fail (BIJI_IS_TRACKER (self), NULL);
 
-  /* Update the note manager */
-  if (urn)
-  {
-    notebook = biji_notebook_new (
-                       G_OBJECT (finisher->manager),
-                       urn,
-                       finisher->str,
-                       g_get_real_time () / G_USEC_PER_SEC);
-    biji_manager_add_item (finisher->manager, BIJI_ITEM (notebook), BIJI_LIVING_ITEMS, TRUE);
-  }
-
-  /* Run the callback from the caller */
-
- out:
-  if (finisher->item_cb != NULL)
-    (*finisher->item_cb) (BIJI_ITEM (notebook), finisher->user_data);
-
-  g_free (val);
-  g_free (key);
-  biji_tracker_finisher_free (finisher);
+  return self->connection;
 }
 
-
-/* This func creates the notebook,
- * gives the urn to the notemanager,
- * then run the 'afterward' callback */
 void
-biji_create_new_notebook_async (BijiManager     *manager,
-                                const gchar      *name,
-                                BijiItemCallback  item_cb,
-                                gpointer          user_data)
+biji_tracker_add_notebook_async (BijiTracker         *self,
+                                 const char          *notebook,
+                                 GAsyncReadyCallback  callback,
+                                 gpointer             user_data)
 {
-  gchar *query;
-  g_autoptr(GDateTime) dt = g_date_time_new_now_utc ();
-  g_autofree char *time = g_date_time_format_iso8601 (dt);
-  BijiTrackerFinisher *finisher;
-
-  query = g_strdup_printf ("INSERT { _:res a nfo:DataContainer ; a nie:DataObject ; "
-                            "nie:contentLastModified '%s' ; "
-                            "nie:title '%s' ; "
-                            "nie:generator 'Bijiben' }",
-                            time,
-                            name);
-
-  /* The finisher has all the pointers we want.
-   * And the callback will free it */
-  finisher = biji_tracker_finisher_new (manager, g_strdup (name), NULL, NULL, NULL, item_cb, NULL, NULL, 
user_data);
-  tracker_sparql_connection_update_blank_async (get_connection (manager),
-                                                query,
+  g_autoptr(GDateTime) date_time = NULL;
+  g_autofree char *query = NULL;
+  g_autofree char *time = NULL;
+  GTask *task;
+
+  g_return_if_fail (BIJI_IS_TRACKER (self));
+  g_return_if_fail (callback);
+
+  date_time = g_date_time_new_now_utc ();
+  time = g_date_time_format_iso8601 (date_time);
+  query = g_strdup_printf ("INSERT { "
+                           "_:res a nfo:DataContainer ; a nie:DataObject ; "
+                           "nie:contentLastModified '%s' ; "
+                           "nie:title '%s' ; "
+                           "nie:generator 'Bijiben' }",
+                           time, notebook);
+
+  task = g_task_new (self, NULL, callback, user_data);
+  g_task_set_task_data (task, g_strdup (notebook), g_free);
+
+  tracker_sparql_connection_update_blank_async (self->connection, query,
 #if !HAVE_TRACKER3
                                                 G_PRIORITY_DEFAULT,
 #endif
                                                 NULL,
-                                                on_new_notebook_query_executed,
-                                                finisher);
+                                                on_add_notebook_cb, task);
 }
 
+BijiItem *
+biji_tracker_add_notebook_finish (BijiTracker  *self,
+                                  GAsyncResult *result,
+                                  GError       **error)
+{
+  g_return_val_if_fail (BIJI_IS_TRACKER (self), false);
+  g_return_val_if_fail (G_IS_ASYNC_RESULT (result), false);
 
-/* removes the tag EVEN if files associated. */
+  return g_task_propagate_pointer (G_TASK (result), error);
+}
 
 void
-biji_remove_notebook_from_tracker (BijiManager *manager, const gchar *urn)
+biji_tracker_remove_notebook (BijiTracker *self,
+                              const char  *notebook_urn)
 {
-  gchar *query;
+  g_autofree char *query = NULL;
 
-  query = g_strdup_printf ("DELETE {'%s' a nfo:DataContainer}", urn);
-  biji_perform_update_async_and_free (get_connection (manager), query, NULL, NULL);
-}
+  g_return_if_fail (BIJI_IS_TRACKER (self));
 
+  query = g_strdup_printf ("DELETE {'%s' a nfo:DataContainer}", notebook_urn);
+  tracker_sparql_connection_update_async (self->connection, query,
+#if !HAVE_TRACKER3
+                                          0,     // priority
+#endif
+                                          NULL, NULL, NULL);
+}
 
 void
-biji_push_existing_notebook_to_note (BijiNoteObj       *note,
-                                       gchar             *title,
-                                       BijiBoolCallback   afterward,
-                                       gpointer           user_data)
+biji_tracker_get_notes_async (BijiTracker         *self,
+                              BijiItemsGroup       group,
+                              const char          *needle,
+                              GAsyncReadyCallback  callback,
+                              gpointer             user_data)
 {
-  gchar *url, *query;
-
-  url = get_note_url (note);
-  query = g_strdup_printf ("INSERT {?urn nie:isPartOf '%s'} WHERE {?urn a nfo:DataContainer; nie:title '%s'; 
nie:generator 'Bijiben'}",
-                           url, title);
-
-  biji_perform_update_async_and_free (
-      get_connection (biji_item_get_manager (BIJI_ITEM (note))), query, afterward, user_data);
-  g_free (url);
+  g_autofree char *query = NULL;
+  g_autofree char *str = NULL;
+  GTask *task;
+
+  g_return_if_fail (BIJI_IS_TRACKER (self));
+  g_return_if_fail (needle && *needle);
+  g_return_if_fail (callback);
+
+  str = g_utf8_strdown (needle, -1);
+  query = g_strconcat ("SELECT tracker:coalesce (?url, ?urn) WHERE "
+                       "{"
+                       "  {  ?urn a nfo:Note"
+                       "    .?urn nie:title ?title"
+                       "    .?urn nie:plainTextContent ?content"
+                       "    .?urn nie:url ?url"
+                       "    .?urn nie:generator 'Bijiben'"
+                       "    .FILTER ("
+                       "    fn:contains (fn:lower-case (?content), '", str, "' ) || "
+                       "    fn:contains (fn:lower-case (?title)  , '", str, "'))} "
+                       "UNION"
+                       "  {  ?urn a nfo:DataContainer"
+                       "    .?urn nie:title ?title"
+                       "    .?urn nie:generator 'Bijiben'"
+                       "    .FILTER ("
+                       "    fn:contains (fn:lower-case (?title), '", str, "'))}"
+                       "}",
+                       NULL);
+
+  task = g_task_new (self, self->cancellable, callback, user_data);
+  tracker_sparql_connection_query_async (self->connection, query, self->cancellable,
+                                         on_get_list_async_cb, task);
 }
 
+GList *
+biji_tracker_get_notes_finish (BijiTracker   *self,
+                               GAsyncResult  *result,
+                               GError       **error)
+{
+  g_return_val_if_fail (BIJI_IS_TRACKER (self), NULL);
+  g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
 
+  return g_task_propagate_pointer (G_TASK (result), error);
+}
 
 void
-biji_remove_notebook_from_note (BijiNoteObj       *note,
-                                  BijiItem          *coll,
-                                  BijiBoolCallback   afterward,
-                                  gpointer           user_data)
+biji_tracker_get_notebooks_async (BijiTracker         *self,
+                                  GAsyncReadyCallback  callback,
+                                  gpointer             user_data)
 {
-  gchar *url, *query;
+  const char *query;
+  GTask *task;
 
-  url = get_note_url (note);
+  g_return_if_fail (BIJI_IS_TRACKER (self));
+  g_return_if_fail (callback);
 
-  query = g_strdup_printf (
-    "DELETE {'%s' nie:isPartOf '%s'}",
-    biji_item_get_uuid (coll), url);
+  query = "SELECT ?c ?title ?mtime "
+    "WHERE { ?c a nfo:DataContainer ;"
+    "nie:title ?title ; "
+    "nie:contentLastModified ?mtime ;"
+    "nie:generator 'Bijiben'}";
 
+  task = g_task_new (self, self->cancellable, callback, user_data);
 
-  biji_perform_update_async_and_free (get_connection (biji_item_get_manager (coll)), query, afterward, 
user_data);
-  g_free (url);
+  tracker_sparql_connection_query_async (self->connection, query,
+                                         self->cancellable,
+                                         on_get_notebooks_async_cb, task);
 }
 
+GHashTable *
+biji_tracker_get_notebooks_finish (BijiTracker   *self,
+                                   GAsyncResult  *result,
+                                   GError       **error)
+{
+  g_return_val_if_fail (BIJI_IS_TRACKER (self), NULL);
+  g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
+
+  return g_task_propagate_pointer (G_TASK (result), error);
+}
 
 void
-biji_note_delete_from_tracker (BijiNoteObj *note)
+biji_tracker_remove_note_notebook_async (BijiTracker         *self,
+                                         BijiNoteObj         *note,
+                                         BijiItem            *notebook,
+                                         GAsyncReadyCallback  callback,
+                                         gpointer             user_data)
 {
-  BijiItem *item;
-  gchar *query;
-  const gchar *uuid;
-  BijiManager *manager;
-
-  item = BIJI_ITEM (note);
-  manager = biji_item_get_manager (item);
-  uuid = biji_item_get_uuid (item);
-  query = g_strdup_printf ("DELETE { <%s> a rdfs:Resource }", uuid);
-  biji_perform_update_async_and_free (get_connection (manager), query, NULL, NULL);
-}
+  g_autofree char *url = NULL;
+  g_autofree char *query = NULL;
+  GTask *task;
 
+  g_return_if_fail (BIJI_IS_TRACKER (self));
+  g_return_if_fail (BIJI_IS_NOTE_OBJ (note));
+  g_return_if_fail (BIJI_IS_NOTEBOOK (notebook));
+  g_return_if_fail (callback);
 
+  url = g_strdup_printf ("file://%s", biji_item_get_uuid (BIJI_ITEM (note)));
+  query = g_strdup_printf ("DELETE {'%s' nie:isPartOf '%s'}",
+                           biji_item_get_uuid (notebook), url);
 
-void
-biji_tracker_trash_resource (BijiManager *manager,
-                              gchar *tracker_urn)
-{
-  gchar *query;
+  task = g_task_new (self, NULL, callback, user_data);
 
-  query = g_strdup_printf ("DELETE { <%s> a rdfs:Resource }", tracker_urn);
-  biji_perform_update_async_and_free (get_connection (manager), query, NULL, NULL);
+  tracker_sparql_connection_update_async (self->connection, query,
+#if !HAVE_TRACKER3
+                                          0,     // priority
+#endif
+                                          NULL,
+                                          on_tracker_update_cb, task);
 }
 
-
-static void
-update_resource (BijiTrackerFinisher *finisher, gchar *tracker_urn_uuid )
+gboolean
+biji_tracker_remove_note_notebook_finish (BijiTracker   *self,
+                                          GAsyncResult  *result,
+                                          GError       **error)
 {
-  BijiManager *manager = finisher->manager;
-  BijiInfoSet *info = finisher->info;
-  g_autoptr(GDateTime) dt_created = g_date_time_new_from_unix_utc (info->created);
-  g_autoptr(GDateTime) dt_mtime = g_date_time_new_from_unix_utc (info->mtime);
-  g_autofree char *created = g_date_time_format_iso8601 (dt_created);
-  g_autofree char *mtime = g_date_time_format_iso8601 (dt_mtime);
-  char *query, *content;
-
-  content = tracker_str (info->content);
-
-  g_message ("Updating resource <%s> %s", info->title, tracker_urn_uuid);
-
-  query = g_strdup_printf (
-      "INSERT OR REPLACE { <%s> a nfo:Note , nie:DataObject ; "
-      "nie:url '%s' ; "
-      "nie:contentLastModified '%s' ; "
-      "nie:contentCreated '%s' ; "
-      "nie:title '%s' ; "
-      "nie:plainTextContent '%s' ; "
-      "nie:dataSource '%s' ;"
-      "nie:generator 'Bijiben' . }",
-      tracker_urn_uuid,
-      info->url,
-      mtime,
-      created,
-      info->title,
-      content,
-      info->datasource_urn);
-
-  biji_perform_update_async_and_free (get_connection (manager), query, NULL, NULL);
-
-  g_free (tracker_urn_uuid);
-  g_free (content);
-  biji_tracker_finisher_free (finisher);
-}
+  g_return_val_if_fail (BIJI_IS_TRACKER (self), false);
+  g_return_val_if_fail (G_IS_ASYNC_RESULT (result), false);
 
+  return g_task_propagate_boolean (G_TASK (result), error);
+}
 
-static void
-push_new_note (BijiTrackerFinisher *finisher)
+void
+biji_tracker_add_note_to_notebook_async (BijiTracker         *self,
+                                         BijiNoteObj         *note,
+                                         const char          *notebook,
+                                         GAsyncReadyCallback  callback,
+                                         gpointer             user_data)
 {
-  BijiManager *manager = finisher->manager;
-  BijiInfoSet *info = finisher->info;
-  g_autoptr(GDateTime) dt_created = g_date_time_new_from_unix_utc (info->created);
-  g_autoptr(GDateTime) dt_mtime = g_date_time_new_from_unix_utc (info->mtime);
-  g_autofree char *created_time = g_date_time_format_iso8601 (dt_created);
-  g_autofree char *mtime = g_date_time_format_iso8601 (dt_mtime);
-  gchar *query, *content;
-
-  g_message ("Creating resource <%s> %s", info->title, info->url);
-
-  content = tracker_str (info->content);
-
-  query = g_strconcat (
-    "INSERT { _:res a nfo:Note ; ",
-    "     a nie:DataObject ; ",
-    "     nie:contentLastModified '", mtime,                "' ;",
-    "     nie:contentCreated      '", created_time,         "' ;",
-    "     nie:title               '", info->title,          "' ;",
-    "     nie:url                 '", info->url,            "' ;",
-    "     nie:plainTextContent    '", content,              "' ;",
-    "     nie:dataSource          '", info->datasource_urn, "' ;",
-    "     nie:generator                              'Bijiben' }",
-    NULL);
-
-
-  g_debug ("%s", query);
-
-  tracker_sparql_connection_update_blank_async (get_connection (manager),
-                                                query,
+  g_autofree char *query = NULL;
+  g_autofree char *url = NULL;
+  GTask *task;
+
+  g_return_if_fail (BIJI_IS_TRACKER (self));
+  g_return_if_fail (BIJI_IS_NOTE_OBJ (note));
+  g_return_if_fail (notebook && *notebook);
+  g_return_if_fail (callback);
+
+  url = g_strdup_printf ("file://%s", biji_item_get_uuid (BIJI_ITEM (note)));
+  query = g_strdup_printf ("INSERT {?urn nie:isPartOf '%s'} "
+                           "WHERE {?urn a nfo:DataContainer; nie:title '%s'; nie:generator 'Bijiben'}",
+                           url, notebook);
+
+  task = g_task_new (self, NULL, callback, user_data);
+
+  tracker_sparql_connection_update_async (self->connection, query,
 #if !HAVE_TRACKER3
-                                                G_PRIORITY_DEFAULT,
+                                          0,     // priority
 #endif
-                                                NULL,
-                                                NULL,  // callback,
-                                                NULL); // user_data);
+                                          NULL,
+                                          on_tracker_update_cb, task);
+}
 
+gboolean
+biji_tracker_add_note_to_notebook_finish (BijiTracker   *self,
+                                          GAsyncResult  *result,
+                                          GError       **error)
+{
+  g_return_val_if_fail (BIJI_IS_TRACKER (self), false);
+  g_return_val_if_fail (G_IS_ASYNC_RESULT (result), false);
 
-  g_free (query);
-  g_free (content);
-  biji_tracker_finisher_free (finisher);
+  return g_task_propagate_boolean (G_TASK (result), error);
 }
 
-
-static void
-ensure_resource_callback (GObject *source_object,
-                           GAsyncResult *res,
-                           gpointer user_data)
+void
+biji_tracker_get_notes_with_notebook_async (BijiTracker         *self,
+                                            const char          *notebook,
+                                            GAsyncReadyCallback  callback,
+                                            gpointer             user_data)
 {
-  TrackerSparqlConnection *connection;
-  TrackerSparqlCursor     *cursor;
-  BijiTrackerFinisher     *finisher;
-  GError                  *error;
-  gchar                   *urn_found;
+  g_autofree char *query = NULL;
+  GTask *task;
 
+  g_return_if_fail (BIJI_IS_TRACKER (self));
+  g_return_if_fail (callback);
 
-  connection = TRACKER_SPARQL_CONNECTION (source_object);
-  finisher = user_data;
-  error = NULL;
-  urn_found = NULL;
-  cursor = tracker_sparql_connection_query_finish (connection, res, &error);
+  query = g_strdup_printf ("SELECT ?s WHERE {?c nie:isPartOf ?s; nie:title '%s'}", notebook);
+  task = g_task_new (self, self->cancellable, callback, user_data);
 
-  if (error)
-  {
-    g_warning ("ENSURE RESOURCE : error %s", error->message);
-    g_error_free (error);
-    biji_tracker_finisher_free (finisher);
-    return;
-  }
-
-  /* Queried resource found into tracker */
+  tracker_sparql_connection_query_async (self->connection, query, self->cancellable,
+                                         on_get_list_async_cb, task);
+}
 
-  if (cursor)
-  {
+GList *
+biji_tracker_get_notes_with_notebook_finish (BijiTracker   *self,
+                                             GAsyncResult  *result,
+                                             GError       **error)
+{
+  g_return_val_if_fail (BIJI_IS_TRACKER (self), NULL);
+  g_return_val_if_fail (G_IS_ASYNC_RESULT (result), NULL);
 
-    if (tracker_sparql_cursor_next (cursor, NULL, NULL))
-      urn_found = g_strdup (tracker_sparql_cursor_get_string (cursor, 0, NULL));
+  return g_task_propagate_pointer (G_TASK (result), error);
+}
 
-    g_object_unref (cursor);
-  }
+void
+biji_tracker_delete_note (BijiTracker *self,
+                          BijiNoteObj *note)
+{
+  g_autofree char *query = NULL;
 
+  g_return_if_fail (BIJI_IS_TRACKER (self));
+  g_return_if_fail (BIJI_IS_NOTE_OBJ (note));
 
-  if (urn_found != NULL)
-    update_resource (finisher, urn_found);
+  query = g_strdup_printf ("DELETE { <%s> a rdfs:Resource }",
+                           biji_item_get_uuid (BIJI_ITEM (note)));
 
-  else
-    push_new_note (finisher);
+  tracker_sparql_connection_update_async (self->connection, query,
+#if !HAVE_TRACKER3
+                                          0,     // priority
+#endif
+                                          NULL, NULL, NULL);
 }
 
-
-
 void
-biji_tracker_ensure_resource_from_info (BijiManager *manager,
-                                         BijiInfoSet *info)
+biji_tracker_trash_resource (BijiTracker *self,
+                             const char  *tracker_urn)
 {
-  gchar *query;
-  BijiTrackerFinisher *finisher;
+  g_autofree char *query = NULL;
 
+  g_return_if_fail (BIJI_IS_TRACKER (self));
+  g_return_if_fail (tracker_urn);
 
-  query = g_strconcat (
-    "SELECT ?urn ?time WHERE { ?urn a nfo:Note ;",
-    "                          nie:title ?title ;",
-    "                          nie:contentLastModified ?time ;",
-    "                          nie:url '", info->url, "' }",
-                               NULL);
+  query = g_strdup_printf ("DELETE { <%s> a rdfs:Resource }", tracker_urn);
 
+  tracker_sparql_connection_update_async (self->connection, query,
+#if !HAVE_TRACKER3
+                                          0,     // priority
+#endif
+                                          NULL, NULL, NULL);
+}
+
+void
+biji_tracker_save_note (BijiTracker *self,
+                        BijiInfoSet *info)
+{
+  g_autofree char *query = NULL;
+  GTask *task;
 
-  /* No matter user callback or not,
-   * we'll need our own to push if needed */
+  g_return_if_fail (BIJI_IS_TRACKER (self));
 
-  finisher = biji_tracker_finisher_new (
-               manager,
-               NULL,
-               info,
-               NULL,
-               NULL,
-               NULL,
-               NULL,
-               NULL,
-               NULL);  // user_data);
+  query = g_strconcat ("SELECT ?urn ?time WHERE { "
+                       "?urn a nfo:Note ; "
+                       "nie:title ?title ; "
+                       "nie:contentLastModified ?time ; "
+                       "nie:url '", info->url, "' }",
+                       NULL);
 
-  tracker_sparql_connection_query_async (
-      get_connection (manager), query, NULL, ensure_resource_callback, finisher);
+  task = g_task_new (self, NULL, NULL, NULL);
+  g_task_set_task_data (task, info, NULL);
 
-  g_free (query);
+  tracker_sparql_connection_query_async (self->connection, query, NULL,
+                                         on_save_note_cb, task);
 }
diff --git a/src/libbiji/biji-tracker.h b/src/libbiji/biji-tracker.h
index d10f4417..ffe1579a 100644
--- a/src/libbiji/biji-tracker.h
+++ b/src/libbiji/biji-tracker.h
@@ -1,16 +1,19 @@
+/* -*- mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*- */
 /* biji-tracker.h
- * Copyright (C) Pierre-Yves LUYTEN 2012,2013 <py luyten fr>
- * 
+ *
+ * Copyright (C) Pierre-Yves LUYTEN 2012, 2013 <py luyten fr>
+ * Copyright 2021 Mohammed Sadiq <sadiq sadiqpk org>
+ *
  * bijiben is free software: you can redistribute it and/or modify it
  * under the terms of the GNU General Public License as published by the
  * Free Software Foundation, either version 3 of the License, or
  * (at your option) any later version.
- * 
+ *
  * bijiben is distributed in the hope that it will be useful, but
  * WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  * See the GNU General Public License for more details.
- * 
+ *
  * You should have received a copy of the GNU General Public License along
  * with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
@@ -18,89 +21,72 @@
 
 #pragma once
 
-#include <gtk/gtk.h>
+#include <glib-object.h>
 #include <tracker-sparql.h>
 
-#include "libbiji.h"
-#include "biji-info-set.h"
-
-
-/* All possible query return
- * Free the containers for list & hash<BijiInfoSets> */
-
-
-typedef void       (*BijiBoolCallback)          (gboolean result, gpointer user_data);
-
-
-typedef void       (*BijiItemCallback)          (BijiItem *item, gpointer user_data);
-
-
-typedef void       (*BijiItemsListCallback)     (GList *items, gpointer user_data);
-
-
-typedef void       (*BijiInfoSetsHCallback)     (GHashTable *info_sets, gpointer user_data);
-
-
-/* CALLER IS RESPONSIBLE FOR FREEING INFO SET */
-
-typedef void       (*BijiInfoCallback)          (BijiInfoSet *info, gpointer user_data);
-
-
-
-void        biji_get_items_with_notebook_async       (BijiManager *manager,
-                                                        const gchar *needle,
-                                                        BijiItemsListCallback cb,
-                                                        gpointer user_data);
-
-
-void        biji_get_items_matching_async              (BijiManager *manager,
-                                                        BijiItemsGroup group,
-                                                        gchar *needle,
-                                                        BijiItemsListCallback cb,
-                                                        gpointer user_data);
-
-
-
-void        biji_get_all_notebooks_async             (BijiManager          *manager,
-                                                      BijiInfoSetsHCallback cb,
-                                                      BijiItemsListCallback list_cb,
-                                                      gpointer              user_data);
-
-
-
-void        biji_create_new_notebook_async           (BijiManager *manager,
-                                                        const gchar *tag,
-                                                        BijiItemCallback afterward,
-                                                        gpointer user_data);
-
-
-
-void        biji_remove_notebook_from_tracker        (BijiManager *manager,
-                                                        const gchar *urn);
-
-
-
-
-void        biji_push_existing_notebook_to_note      (BijiNoteObj      *note,
-                                                        gchar            *title,
-                                                        BijiBoolCallback  bool_cb,
-                                                        gpointer          user_data);
-
-
-
-void        biji_remove_notebook_from_note           (BijiNoteObj      *note,
-                                                        BijiItem         *coll,
-                                                        BijiBoolCallback  bool_cb,
-                                                        gpointer          user_data);
-
-
-
-void        biji_note_delete_from_tracker              (BijiNoteObj *note);
-
-
-void        biji_tracker_trash_resource               (BijiManager *manager,
-                                                        gchar *tracker_urn);
-
-
-void        biji_tracker_ensure_resource_from_info    (BijiManager     *manager,
-                                                        BijiInfoSet *info);
+#include "biji-manager.h"
+
+G_BEGIN_DECLS
+
+#define BIJI_TYPE_TRACKER (biji_tracker_get_type ())
+
+G_DECLARE_FINAL_TYPE (BijiTracker, biji_tracker, BIJI, TRACKER, GObject)
+
+BijiTracker *biji_tracker_new                            (BijiManager         *manager);
+gboolean    biji_tracker_is_available                    (BijiTracker         *self);
+TrackerSparqlConnection *
+            biji_tracker_get_connection                  (BijiTracker         *self);
+void        biji_tracker_add_notebook_async              (BijiTracker         *self,
+                                                          const char          *notebook,
+                                                          GAsyncReadyCallback  callback,
+                                                          gpointer             user_data);
+BijiItem   *biji_tracker_add_notebook_finish             (BijiTracker         *self,
+                                                          GAsyncResult        *result,
+                                                          GError             **error);
+void        biji_tracker_remove_notebook                 (BijiTracker         *self,
+                                                          const char          *notebook_urn);
+void        biji_tracker_get_notes_async                 (BijiTracker         *self,
+                                                          BijiItemsGroup       group,
+                                                          const char          *needle,
+                                                          GAsyncReadyCallback  callback,
+                                                          gpointer             user_data);
+GList      *biji_tracker_get_notes_finish                (BijiTracker         *self,
+                                                          GAsyncResult        *result,
+                                                          GError             **error);
+void        biji_tracker_get_notebooks_async             (BijiTracker         *self,
+                                                          GAsyncReadyCallback  callback,
+                                                          gpointer             user_data);
+GHashTable *biji_tracker_get_notebooks_finish            (BijiTracker         *self,
+                                                          GAsyncResult        *result,
+                                                          GError             **error);
+void        biji_tracker_remove_note_notebook_async      (BijiTracker         *self,
+                                                          BijiNoteObj         *note,
+                                                          BijiItem            *notebook,
+                                                          GAsyncReadyCallback  callback,
+                                                          gpointer             user_data);
+gboolean    biji_tracker_remove_note_notebook_finish     (BijiTracker         *self,
+                                                          GAsyncResult        *result,
+                                                          GError             **error);
+void        biji_tracker_add_note_to_notebook_async      (BijiTracker         *self,
+                                                          BijiNoteObj         *note,
+                                                          const char          *notebook,
+                                                          GAsyncReadyCallback  callback,
+                                                          gpointer             user_data);
+gboolean    biji_tracker_add_note_to_notebook_finish     (BijiTracker         *self,
+                                                          GAsyncResult        *result,
+                                                          GError             **error);
+void        biji_tracker_get_notes_with_notebook_async   (BijiTracker         *self,
+                                                          const char          *notebooks,
+                                                          GAsyncReadyCallback  callback,
+                                                          gpointer             user_data);
+GList      *biji_tracker_get_notes_with_notebook_finish  (BijiTracker         *self,
+                                                          GAsyncResult        *result,
+                                                          GError             **error);
+void        biji_tracker_delete_note                     (BijiTracker         *self,
+                                                          BijiNoteObj         *note);
+void        biji_tracker_trash_resource                  (BijiTracker          *self,
+                                                          const char          *tracker_urn);
+void        biji_tracker_save_note                       (BijiTracker         *self,
+                                                          BijiInfoSet         *info);
+
+G_END_DECLS
diff --git a/src/libbiji/provider/biji-local-note.c b/src/libbiji/provider/biji-local-note.c
index 5fde1ff1..3c793606 100644
--- a/src/libbiji/provider/biji-local-note.c
+++ b/src/libbiji/provider/biji-local-note.c
@@ -69,6 +69,7 @@ local_note_save (BijiNoteObj *note)
   BijiLocalNote *self = BIJI_LOCAL_NOTE (note);
   const BijiProviderInfo *prov_info = biji_provider_get_info (self->provider);
   BijiInfoSet *info = biji_info_set_new ();
+  BijiManager *manager;
 
   /* File save */
   biji_lazy_serialize (note);
@@ -81,7 +82,8 @@ local_note_save (BijiNoteObj *note)
   info->created = biji_note_obj_get_create_date (note);
   info->datasource_urn = g_strdup (prov_info->datasource);
 
-  biji_tracker_ensure_resource_from_info (biji_item_get_manager (item), info);
+  manager = biji_item_get_manager (item);
+  biji_tracker_save_note (biji_manager_get_tracker (manager), info);
 }
 
 static void
@@ -223,10 +225,12 @@ local_note_delete (BijiItem *item)
 {
   BijiLocalNote *self = BIJI_LOCAL_NOTE (item);
   g_autofree char *file_path = g_file_get_path (self->location);
+  BijiTracker *tracker;
 
   g_debug ("local note delete : %s", file_path);
 
-  biji_note_delete_from_tracker (BIJI_NOTE_OBJ (self));
+  tracker = biji_manager_get_tracker (biji_item_get_manager (item));
+  biji_tracker_delete_note (tracker, BIJI_NOTE_OBJ (item));
   g_file_delete_async (self->location,
                        G_PRIORITY_LOW,
                        NULL,                  /* Cancellable */
diff --git a/src/libbiji/provider/biji-local-provider.c b/src/libbiji/provider/biji-local-provider.c
index 441c8307..478e02fb 100644
--- a/src/libbiji/provider/biji-local-provider.c
+++ b/src/libbiji/provider/biji-local-provider.c
@@ -134,15 +134,17 @@ create_notebook_if_needed (gpointer key,
 
 
 static void
-local_provider_finish (GHashTable *notebooks,
-                       gpointer user_data)
+on_get_local_notebooks_cb (GObject      *object,
+                           GAsyncResult *result,
+                           gpointer      user_data)
 {
   BijiLocalProvider *self;
-  BijiProviderHelper *helper;
+  BijiProviderHelper *helper = user_data;
+  GHashTable *notebooks;
   GList *list;
 
-  helper = user_data;
   self = BIJI_LOCAL_PROVIDER (helper->provider);
+  notebooks = biji_tracker_get_notebooks_finish (BIJI_TRACKER (object), result, NULL);
 
   if (helper->group == BIJI_LIVING_ITEMS)
     g_hash_table_foreach (notebooks, create_notebook_if_needed, user_data);
@@ -172,6 +174,7 @@ enumerate_next_files_ready_cb (GObject *source,
   GFileEnumerator *enumerator;
   BijiLocalProvider *self;
   BijiProviderHelper *helper;
+  BijiManager *manager;
   GList *files, *l;
   GError *error;
   gchar *base_path;
@@ -182,6 +185,7 @@ enumerate_next_files_ready_cb (GObject *source,
   self = BIJI_LOCAL_PROVIDER (helper->provider);
   error = NULL;
   files = g_file_enumerator_next_files_finish (enumerator, res, &error);
+  manager = biji_provider_get_manager (BIJI_PROVIDER (self));
   g_file_enumerator_close_async (enumerator, G_PRIORITY_DEFAULT, NULL,
                                  release_enum_cb, NULL);
 
@@ -236,9 +240,8 @@ enumerate_next_files_ready_cb (GObject *source,
 
   /* Now we have all notes,
    * load the notebooks and we're good to notify loading done */
-  biji_get_all_notebooks_async (biji_provider_get_manager (BIJI_PROVIDER (self)),
-                                local_provider_finish, NULL,
-                                helper);
+  biji_tracker_get_notebooks_async (biji_manager_get_tracker (manager),
+                                    on_get_local_notebooks_cb, helper);
 }
 
 static void
diff --git a/src/libbiji/provider/biji-memo-provider.c b/src/libbiji/provider/biji-memo-provider.c
index cb28b2dd..39d85c46 100644
--- a/src/libbiji/provider/biji-memo-provider.c
+++ b/src/libbiji/provider/biji-memo-provider.c
@@ -176,8 +176,10 @@ create_note_from_item (BijiMemoItem *item)
 static void
 trash (gpointer urn_uuid, gpointer self)
 {
-  biji_tracker_trash_resource (
-      biji_provider_get_manager (BIJI_PROVIDER (self)), (gchar*) urn_uuid);
+  BijiManager *manager;
+
+  manager = biji_provider_get_manager (BIJI_PROVIDER (self));
+  biji_tracker_trash_resource (biji_manager_get_tracker (manager), (gchar*) urn_uuid);
 }
 
 
@@ -185,6 +187,7 @@ static void
 handle_next_item (BijiMemoProvider *self)
 {
   BijiMemoItem *item;
+  BijiManager  *manager;
   GList        *list;
 
   item = g_queue_pop_head (self->queue);
@@ -200,8 +203,8 @@ handle_next_item (BijiMemoProvider *self)
     g_debug ("url=%s", item->set.url);
     g_debug ("content=%s\n================\n\n\n", item->set.content);
 
-    biji_tracker_ensure_resource_from_info (
-    biji_provider_get_manager (BIJI_PROVIDER (self)), &item->set);
+    manager = biji_provider_get_manager (BIJI_PROVIDER (self));
+    biji_tracker_save_note (biji_manager_get_tracker (manager), &item->set);
 
     //memo_item_free (item);
     handle_next_item (self);
diff --git a/src/libbiji/provider/biji-nextcloud-note.c b/src/libbiji/provider/biji-nextcloud-note.c
index d7b42ee5..8dae5aad 100644
--- a/src/libbiji/provider/biji-nextcloud-note.c
+++ b/src/libbiji/provider/biji-nextcloud-note.c
@@ -23,6 +23,7 @@
 #include <curl/curl.h>
 #include <json-glib/json-glib.h>
 
+#include "libbiji.h"
 #include "biji-nextcloud-note.h"
 #include "biji-nextcloud-provider.h"
 #include "biji-tracker.h"
@@ -93,6 +94,7 @@ save_note_finish (GObject      *source_object,
   BijiNextcloudNote *self = BIJI_NEXTCLOUD_NOTE (source_object);
   const BijiProviderInfo *prov_info = biji_provider_get_info (BIJI_PROVIDER (self->provider));
   BijiInfoSet *info = biji_info_set_new ();
+  BijiManager *manager;
 
   info->url = (char *) biji_note_obj_get_path (note);
   info->title = (char *) biji_note_obj_get_title (note);
@@ -101,7 +103,8 @@ save_note_finish (GObject      *source_object,
   info->created = biji_note_obj_get_create_date (note);
   info->datasource_urn = g_strdup (prov_info->datasource);
 
-  biji_tracker_ensure_resource_from_info (biji_item_get_manager (item), info);
+  manager = biji_item_get_manager (item);
+  biji_tracker_save_note (biji_manager_get_tracker (manager), info);
 }
 
 static size_t
@@ -228,6 +231,7 @@ static gboolean
 archive (BijiNoteObj *note)
 {
   BijiNextcloudNote *self = BIJI_NEXTCLOUD_NOTE (note);
+  BijiTracker *tracker;
   CURL *curl = curl_easy_init ();
   CURLcode res = 0;
 
@@ -246,7 +250,10 @@ archive (BijiNoteObj *note)
       if (res == CURLE_OK)
         {
           curl_easy_cleanup (curl);
-          biji_note_delete_from_tracker (note);
+
+          tracker = biji_manager_get_tracker (biji_item_get_manager (BIJI_ITEM (note)));
+          biji_tracker_delete_note (tracker, note);
+
           return TRUE;
         }
 


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