[bijiben] noteTagDialog: creating a collection pushes to note book



commit 981075ed334bf31c8ad64ba2a6a1f2431c0ad913
Author: Pierre-Yves Luyten <py luyten fr>
Date:   Sat May 4 00:08:16 2013 +0200

    noteTagDialog: creating a collection pushes to note book
    
    The tracker update retrieves the URN
    for the sake of note book.

 src/bjb-bijiben.c            |    2 +-
 src/bjb-note-tag-dialog.c    |   10 ++-
 src/libbiji/biji-note-book.c |   38 +++++++++---
 src/libbiji/biji-note-book.h |    2 +-
 src/libbiji/biji-tracker.c   |  133 +++++++++++++++++++++++++++++++++++++-----
 src/libbiji/biji-tracker.h   |    2 +-
 6 files changed, 157 insertions(+), 30 deletions(-)
---
diff --git a/src/bjb-bijiben.c b/src/bjb-bijiben.c
index 0a9b37f..4f967c7 100644
--- a/src/bjb-bijiben.c
+++ b/src/bjb-bijiben.c
@@ -204,7 +204,7 @@ go_through_notes_cb (GFileEnumerator *enumerator, GAsyncResult *res, Bijiben *se
 
       g_free (default_color);
 
-      biji_note_book_append_new_note (self->priv->book, note, FALSE);
+      biji_note_book_add_item (self->priv->book, BIJI_ITEM (note), FALSE);
       biji_note_obj_save_note (note);
     }
 
diff --git a/src/bjb-note-tag-dialog.c b/src/bjb-note-tag-dialog.c
index b73c9e3..2c59f28 100644
--- a/src/bjb-note-tag-dialog.c
+++ b/src/bjb-note-tag-dialog.c
@@ -265,13 +265,17 @@ on_new_collection_created_cb (gpointer user_data)
   gtk_entry_set_text (GTK_ENTRY (priv->entry), "");
 }
 
+/* Gives the title and book :
+ * the collection is created & book updated.
+ * afterward, our callback comes */
 static void
 add_new_tag (BjbNoteTagDialog *self)
 {
-  const gchar *collection = gtk_entry_get_text (GTK_ENTRY (self->priv->entry));
+  BijiNoteBook *book = bjb_window_base_get_book (GTK_WIDGET (self->priv->window));
+  const gchar *title = gtk_entry_get_text (GTK_ENTRY (self->priv->entry));
 
-  if (collection && g_utf8_strlen (collection, -1) > 0)
-    biji_create_new_collection (collection, on_new_collection_created_cb, self);
+  if (title && g_utf8_strlen (title, -1) > 0)
+    biji_create_new_collection_async (book, title, on_new_collection_created_cb, self);
 }
 
 static void
diff --git a/src/libbiji/biji-note-book.c b/src/libbiji/biji-note-book.c
index 03726b2..ccda58c 100644
--- a/src/libbiji/biji-note-book.c
+++ b/src/libbiji/biji-note-book.c
@@ -449,17 +449,37 @@ biji_note_book_remove_item (BijiNoteBook *book, BijiItem *item)
   return retval;
 }
 
-/* Notes collection */
-void
-biji_note_book_append_new_note (BijiNoteBook *book, BijiNoteObj *note, gboolean notify)
+gboolean
+biji_note_book_add_item (BijiNoteBook *book, BijiItem *item, gboolean notify)
 {
-  g_return_if_fail (BIJI_IS_NOTE_BOOK (book));
-  g_return_if_fail (BIJI_IS_NOTE_OBJ (note));
+  g_return_val_if_fail (BIJI_IS_NOTE_BOOK (book), FALSE);
+  g_return_val_if_fail (BIJI_IS_ITEM (item), FALSE);
 
-  _biji_note_book_add_one_note (book,note);
+  gchar *uid;
+  gboolean retval = TRUE;
 
-  if (notify)
-    biji_note_book_notify_changed (book, BIJI_BOOK_ITEM_ADDED, BIJI_ITEM (note));
+  uid = biji_item_get_uuid (item);
+
+  if (g_hash_table_lookup (book->priv->items, uid))
+    retval = FALSE;
+
+  else if (BIJI_IS_NOTE_OBJ (item))
+    _biji_note_book_add_one_note (book, BIJI_NOTE_OBJ (item));
+
+  else if (BIJI_IS_COLLECTION (item))
+  {
+    g_hash_table_insert (book->priv->items,
+                         biji_item_get_uuid (item),
+                         item);
+
+    g_signal_connect (item, "deleted",
+                      G_CALLBACK (on_item_deleted_cb), book);
+  }
+
+  if (retval && notify)
+    biji_note_book_notify_changed (book, BIJI_BOOK_ITEM_ADDED, item);
+
+  return retval;
 }
 
 GList *
@@ -557,7 +577,7 @@ biji_note_book_note_new           (BijiNoteBook *book, gchar *str)
   }
 
   biji_note_obj_save_note (ret);
-  biji_note_book_append_new_note (book, ret, TRUE);
+  biji_note_book_add_item (book, BIJI_ITEM (ret), TRUE);
 
   return ret;
 }
diff --git a/src/libbiji/biji-note-book.h b/src/libbiji/biji-note-book.h
index 498370b..8d29c7c 100644
--- a/src/libbiji/biji-note-book.h
+++ b/src/libbiji/biji-note-book.h
@@ -47,7 +47,7 @@ BijiNoteBook * biji_note_book_new (GFile *location);
 
 gchar * biji_note_book_get_unique_title (BijiNoteBook *book, gchar *title);
 
-void biji_note_book_append_new_note (BijiNoteBook *book, BijiNoteObj *note, gboolean notify);
+gboolean biji_note_book_add_item (BijiNoteBook *book, BijiItem *item, gboolean notify);
 
 gboolean biji_note_book_notify_changed (BijiNoteBook           *book,
                                         BijiNoteBookChangeFlag  flag,
diff --git a/src/libbiji/biji-tracker.c b/src/libbiji/biji-tracker.c
index cd946e7..2f5fad3 100644
--- a/src/libbiji/biji-tracker.c
+++ b/src/libbiji/biji-tracker.c
@@ -18,11 +18,14 @@
 #include "biji-item.h"
 #include "biji-tracker.h"
 
-/* To perform something after async tracker query */
+/* To perform something after async tracker query
+ * TODO : implemet this with GObject */
 typedef struct {
 
-  /* query, could add the cancellable */
-  gchar *query;
+  BijiNoteBook *book;
+
+  /* usually a query */
+  gchar *str;
 
   /* after the query */
   BijiFunc func;
@@ -31,11 +34,15 @@ typedef struct {
 } BijiTrackerFinisher;
 
 static BijiTrackerFinisher *
-biji_tracker_finisher_new (gchar *query, BijiFunc f, gpointer user_data)
+biji_tracker_finisher_new (BijiNoteBook *book,
+                           gchar        *str,
+                           BijiFunc      f,
+                           gpointer      user_data)
 {
   BijiTrackerFinisher *retval = g_new (BijiTrackerFinisher, 1);
 
-  retval->query = query;
+  retval->book = book;
+  retval->str = str;
   retval->func = f;
   retval->user_data = user_data;
 
@@ -45,8 +52,8 @@ biji_tracker_finisher_new (gchar *query, BijiFunc f, gpointer user_data)
 static void
 biji_tracker_finisher_free (BijiTrackerFinisher *f)
 {
-  if (f->query)
-    g_free (f->query);
+  if (f->str)
+    g_free (f->str);
 
   g_free (f);
 }
@@ -90,7 +97,7 @@ biji_finish_update (GObject *source_object,
   TrackerSparqlConnection *self = TRACKER_SPARQL_CONNECTION (source_object);
   GError *error = NULL;
   BijiTrackerFinisher *finisher = user_data;
-  gchar *query = finisher->query;
+  gchar *query = finisher->str;
 
   tracker_sparql_connection_update_finish (self, res, &error);
 
@@ -110,7 +117,7 @@ biji_finish_update (GObject *source_object,
 static void
 biji_perform_update_async_and_free (gchar *query, BijiFunc f, gpointer user_data)
 {
-  BijiTrackerFinisher *finisher = biji_tracker_finisher_new (query, f, user_data);
+  BijiTrackerFinisher *finisher = biji_tracker_finisher_new (NULL, query, f, user_data);
 
   tracker_sparql_connection_update_async (get_connection_singleton(),
                                           query,
@@ -336,13 +343,109 @@ biji_get_notes_with_string_or_collection_async (gchar *needle, GAsyncReadyCallba
   bjb_perform_query_async (query, f, user_data);
 }
 
-void 
-biji_create_new_collection (const gchar *tag, BijiFunc afterward, gpointer user_data)
-{ 
-  gchar *query = g_strdup_printf ("INSERT {_:result a nfo:DataContainer;a nie:DataObject;nie:title '%s' ; 
nie:generator 'Bijiben'}"
-                                  ,tag);
+static void
+on_new_collection_query_executed (GObject *source_object, GAsyncResult *res, gpointer user_data)
+{
+  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;
+
+  error = NULL;
+  variant = tracker_sparql_connection_update_blank_finish (connection, res, &error);
+  if (error != NULL)
+    {
+      g_warning ("Unable to create collection: %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);
+
+  child = g_variant_get_child_value (variant, 1);
+  val = g_variant_dup_string (child, NULL);
+  g_variant_unref (child);
+
+  g_variant_unref (variant);
 
-  biji_perform_update_async_and_free (query, afterward, user_data);
+  if (g_strcmp0 (key, "res") == 0)
+    urn = val;
+
+  /* Update the note book */
+  if (urn)
+  {
+    BijiCollection *collection;
+
+    collection = biji_collection_new (urn, finisher->str);
+    biji_note_book_add_item (finisher->book, BIJI_ITEM (collection), TRUE);
+  }
+
+  /* Run the callback from the caller */
+
+ out:
+  if (finisher->func != NULL)
+    (*finisher->func) (finisher->user_data);
+
+  g_free (val);
+  g_free (key);
+  biji_tracker_finisher_free (finisher);
+}
+
+/* This func creates the collection,
+ * gives the urn to the notebook,
+ * then run the 'afterward' callback */
+void
+biji_create_new_collection_async (BijiNoteBook *book,
+                                  const gchar  *name,
+                                  BijiFunc      afterward,
+                                  gpointer      user_data)
+{
+  gchar *query;
+  GTimeVal tv;
+  gchar *time;
+  gint64 timestamp;
+  BijiTrackerFinisher *finisher;
+
+  timestamp = g_get_real_time () / G_USEC_PER_SEC;
+  tv.tv_sec = timestamp;
+  tv.tv_usec = 0;
+  time = g_time_val_to_iso8601 (&tv);
+
+  query = g_strdup_printf ("INSERT { _:res a nfo:DataContainer ; a nie:DataObject ; "
+                            "nie:contentLastModified '%s' ; "
+                            "nie:title '%s' ; "
+                            "nie:generator 'Bijiben' }",
+                            time,
+                            name);
+  g_free (time);
+
+  /* The finisher has all the pointers we want.
+   * And the callback will free it */
+  finisher = biji_tracker_finisher_new (book, g_strdup (name), afterward, user_data);
+  tracker_sparql_connection_update_blank_async (get_connection_singleton (),
+                                                query,
+                                                G_PRIORITY_DEFAULT,
+                                                NULL,
+                                                on_new_collection_query_executed,
+                                                finisher);
 }
 
 /* removes the tag EVEN if files associated.
diff --git a/src/libbiji/biji-tracker.h b/src/libbiji/biji-tracker.h
index 6e75a47..f559be5 100644
--- a/src/libbiji/biji-tracker.h
+++ b/src/libbiji/biji-tracker.h
@@ -51,7 +51,7 @@ GHashTable * biji_get_all_collections_finish (GObject *source_object, GAsyncResu
 
 void biji_get_all_collections_async (GAsyncReadyCallback f, gpointer user_data);
 
-void biji_create_new_collection (const gchar *tag, BijiFunc afterward, gpointer user_data);
+void biji_create_new_collection_async (BijiNoteBook *book, const gchar *tag, BijiFunc afterward, gpointer 
user_data);
 
 void biji_remove_collection_from_tracker (gchar *urn);
 


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