[bijiben] Tracker: insert or replace note



commit f696a1224e7b3dbad5283d9da6f2ab305b02550b
Author: Pierre-Yves Luyten <py luyten fr>
Date:   Tue Nov 27 01:28:11 2012 +0100

    Tracker: insert or replace note
    
    This preserves existing tags. Also fixes tag insertion.
    Tag removal is still to be fixed

 src/bjb-note-tag-dialog.c  |    2 +-
 src/libbiji/biji-tracker.c |   68 +++++++++++++++++---------------------------
 src/libbiji/biji-tracker.h |    2 +-
 3 files changed, 28 insertions(+), 44 deletions(-)
---
diff --git a/src/bjb-note-tag-dialog.c b/src/bjb-note-tag-dialog.c
index 3b63654..38e55b5 100644
--- a/src/bjb-note-tag-dialog.c
+++ b/src/bjb-note-tag-dialog.c
@@ -125,7 +125,7 @@ note_dialog_add_tag (BijiNoteObj *note, gchar *tag)
   if (biji_note_obj_add_tag (note, tag))
   {
     biji_note_obj_save_note (note);
-    push_existing_tag_to_note (tag, note);
+    push_existing_or_new_tag_to_note (tag, note);
   }
 }
 
diff --git a/src/libbiji/biji-tracker.c b/src/libbiji/biji-tracker.c
index 111ddfc..8a50581 100644
--- a/src/libbiji/biji-tracker.c
+++ b/src/libbiji/biji-tracker.c
@@ -90,6 +90,12 @@ to_8601_date( gchar * dot_iso_8601_date )
                            g_utf8_strncpy  (result ,dot_iso_8601_date, 19) );
 }
 
+static gchar *
+get_note_url (BijiNoteObj *note)
+{
+  return g_strdup_printf ("file://%s", biji_note_obj_get_path (note));
+}
+
 /////////////// Tags
 
 /* This func only provides tags.
@@ -165,22 +171,31 @@ remove_tag_from_tracker(gchar *tag)
 }
 
 void
-push_existing_tag_to_note(gchar *tag,BijiNoteObj *note)
+push_existing_or_new_tag_to_note (gchar *tag,BijiNoteObj *note)
 {
-  const gchar *query = g_strdup_printf( "INSERT { <%s> nao:hasTag ?id } \
-  WHERE {   ?id nao:prefLabel '%s' }", biji_note_obj_get_path(note),tag ) ;
-    
+  gchar *url = get_note_url (note);
+  const gchar *query = g_strdup_printf (
+            "INSERT {_:tag a nao:Tag ; nao:prefLabel '%s'. \
+            ?unknown nao:hasTag _:tag} WHERE {?unknown nie:url '%s'}",
+            tag, url);
+
   biji_perform_update_async (query);
+
+  g_free (url);
   g_free ((gchar*) query);
 }
 
+/* This one is to be fixed */
 void
 remove_tag_from_note (gchar *tag, BijiNoteObj *note)
 {
+  gchar *url = get_note_url (note);
   const gchar *query = g_strdup_printf( "DELETE { <%s> nao:hasTag ?id } \
-  WHERE {   ?id nao:prefLabel '%s' }", biji_note_obj_get_path(note),tag ) ;
+  WHERE {   ?id nao:prefLabel '%s' }", url, tag ) ;
     
   biji_perform_update_async (query);
+
+  g_free (url);
   g_free ((gchar*) query);
 }
 
@@ -194,10 +209,8 @@ biji_note_delete_from_tracker (BijiNoteObj *note)
   g_free ((gchar*) query);
 }
 
-/* There is no standard UPDATE into tracker
- * delete the note, when we're sure it's done then insert it */
-static void 
-biji_note_create_into_tracker(BijiNoteObj *note)
+void
+bijiben_push_note_to_tracker (BijiNoteObj *note)
 {
   gchar *title,*content,*file,*create_date,*last_change_date ;
     
@@ -207,9 +220,11 @@ biji_note_create_into_tracker(BijiNoteObj *note)
   last_change_date = to_8601_date (biji_note_obj_get_last_change_date (note));
   content = tracker_str (biji_note_get_raw_text (note));
 
-  /* TODO : nie:mimeType Note ; \ */
+  /* TODO : nie:mimeType Note ;
+   * All these properties are unique and thus can be "updated"
+   * which is not the case of tags */
   const gchar *query = g_strdup_printf (
-                            "INSERT { <%s> a nfo:Note , nie:DataObject ; \
+                           "INSERT OR REPLACE { <%s> a nfo:Note , nie:DataObject ; \
                             nie:url '%s' ; \
                             nie:contentLastModified '%s' ; \
                             nie:contentCreated '%s' ; \
@@ -233,34 +248,3 @@ biji_note_create_into_tracker(BijiNoteObj *note)
   g_free(last_change_date);
 }
 
-static void
-update_note_into_tracker (GObject *source_object,
-                          GAsyncResult *res,
-                          gpointer user_data)
-{
-  if (user_data && BIJI_IS_NOTE_OBJ (user_data))
-  {
-    BijiNoteObj *note = BIJI_NOTE_OBJ (user_data);
-
-    biji_finish_update (source_object, res, NULL);
-    biji_note_create_into_tracker (note);
-  }
-}
-
-
-void
-bijiben_push_note_to_tracker(BijiNoteObj *note)
-{
-  const gchar *query = g_strdup_printf ("DELETE { <%s> a rdfs:Resource }",
-                                        biji_note_obj_get_path(note));
-
-  tracker_sparql_connection_update_async (get_connection_singleton(),
-                                          query,
-                                          0,     // priority
-                                          NULL,
-                                          update_note_into_tracker,
-                                          note); //user_data
-
-  g_free ((gchar*) query);
-}
-
diff --git a/src/libbiji/biji-tracker.h b/src/libbiji/biji-tracker.h
index 6d80e76..1e2dd87 100644
--- a/src/libbiji/biji-tracker.h
+++ b/src/libbiji/biji-tracker.h
@@ -32,7 +32,7 @@ void push_tag_to_tracker(gchar *tag);
 
 void remove_tag_from_tracker(gchar *tag);
 
-void push_existing_tag_to_note(gchar *tag,BijiNoteObj *note);
+void push_existing_or_new_tag_to_note (gchar *tag,BijiNoteObj *note);
 
 void remove_tag_from_note (gchar *tag, BijiNoteObj *note) ;
 



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