[bijiben] Import Gnote or Tomboy : safer process



commit a58e22f82d9804ed619249a41753d4dacd07f9c3
Author: Pierre-Yves Luyten <py luyten fr>
Date:   Tue Dec 11 01:29:39 2012 +0100

    Import Gnote or Tomboy : safer process
    
    First load notes then serialize. Use children_enumerate_async, check for template, fix color

 src/bjb-bijiben.c            |  177 ++++++++++++++++++++++++-----------------
 src/libbiji/biji-note-book.c |   34 ++++----
 src/libbiji/biji-note-book.h |    4 +-
 src/libbiji/biji-note-obj.h  |    2 +
 4 files changed, 127 insertions(+), 90 deletions(-)
---
diff --git a/src/bjb-bijiben.c b/src/bjb-bijiben.c
index cd9e328..6bee1f6 100644
--- a/src/bjb-bijiben.c
+++ b/src/bjb-bijiben.c
@@ -51,7 +51,7 @@ bijiben_new_window_internal (GApplication *app,
   BijiNoteObj* note = NULL;
 
   if (file != NULL)
-    note = biji_note_get_new_from_file(g_file_get_path(file));
+    note = biji_note_get_new_from_file (g_file_get_path(file));
   else if (note_obj != NULL)
     note = note_obj;
 
@@ -101,127 +101,158 @@ bijiben_init (Bijiben *object)
   object->priv->settings = initialize_settings();
 }
 
-/* Currently the only purpose for this is to offer testing
- * but these func might still be improved and integrated later on
- * Not async to ensure files copied before book loads */
+/* Import. TODO : move to libbiji */
 
 #define ATTRIBUTES_FOR_NOTEBOOK "standard::content-type,standard::name"
 
-static void
-copy_note_file (GFileInfo *info,
-                GFile *dir,
-                GFile *dest)
+static BijiNoteObj *
+abort_note (BijiNoteObj *rejected)
+{
+  g_object_unref (rejected);
+  return NULL;
+}
+
+static BijiNoteObj *
+copy_note (GFileInfo *info, GFile *container)
 {
-  Bijiben *self;
-  BijiNoteObj *note_obj;
-  GFile *note, *result;
+  BijiNoteObj *retval = NULL;
   const gchar *name;
-  gchar *path, *default_color, *unique_title;
-  GError *error = NULL;
-  BjbSettings *settings;
-  GdkRGBA color;
+  gchar *path;
 
+  /* First make sure it's a note */
   name = g_file_info_get_name (info);
   if (!g_str_has_suffix (name, ".note"))
-    return;
+    return NULL;
 
-  path = g_build_filename (g_file_get_path(dir), name, NULL);
-  note = g_file_new_for_path (path);
+  /* Deserialize it */
+  path = g_build_filename (g_file_get_path (container), name, NULL);
+  retval = biji_note_get_new_from_file (path);
   g_free (path);
 
-  path = g_build_filename (g_file_get_path(dest), name, NULL);
-  result = g_file_new_for_path (path);
-
-  g_file_copy (note, result, G_FILE_COPY_NOFOLLOW_SYMLINKS,
-               NULL,NULL, NULL, &error);
+  g_return_val_if_fail (BIJI_IS_NOTE_OBJ (retval), NULL);
 
-  if (error)
-  {
-     g_warning ("error:%s", error->message);
-     g_error_free (error);
-  }
-
-  self = BIJIBEN_APPLICATION (g_application_get_default ());
-  note_obj = biji_note_get_new_from_file (path);
-
-  /* Sanitize color
-   * This is done here in Bijiben because
-   * default color is app choice */
-  settings = bjb_app_get_settings (self);
-  g_object_get (G_OBJECT(settings),"color", &default_color, NULL);
-  gdk_rgba_parse (&color, default_color);
-  g_free (default_color);
-  biji_note_obj_set_rgba (note_obj, &color);
-
-  /* Append the note refreshes main view */
-  unique_title = biji_note_book_get_unique_title (self->priv->book, biji_note_obj_get_title (note_obj));
-  biji_note_obj_set_title (note_obj, unique_title);
-  g_free (unique_title);
-  note_book_append_new_note (self->priv->book, note_obj);
+  /* Not a Template */
+  if (biji_note_obj_is_template (retval))
+    return abort_note (retval);
 
+  /* Assign the new path */
+  path = g_build_filename (g_get_user_data_dir (), "bijiben", name, NULL);
+  g_object_set (retval, "path", path, NULL);
   g_free (path);
-  g_object_unref (note);
-  g_object_unref (result);
+
+  return retval;
 }
 
+/* Some notes might have been added previously */
 static void
-list_notes_to_copy (GFileEnumerator *enumerator,
-                    GFile *dest)
+list_notes_to_copy (GFile *src_obj_file, GAsyncResult *res, Bijiben *self)
 {
-  GFile *dir = g_file_enumerator_get_container (enumerator);
-  GFileInfo *info = g_file_enumerator_next_file (enumerator, NULL,NULL);
+  /* Go through */
+  GFileEnumerator *enumerator;
+  GError *error = NULL;
+  GList *notes_proposal = NULL;
+  GList *notes = NULL;
+
+  /* Handle note */
+  GFileInfo *info;
+  BijiNoteObj *iter;
+  GFile *container;
+
+  /* Sanitize title & color */
+  gchar *unique_title, *default_color;
+  BjbSettings *settings;
+  GdkRGBA color;
+
+  enumerator = g_file_enumerate_children_finish (src_obj_file, res, &error);
 
+  if (error)
+  {
+    g_warning ("Enumerator failed : %s", error->message);
+    g_error_free (error);
+    return;
+  }
+
+  container = g_file_enumerator_get_container (enumerator);
+  info = g_file_enumerator_next_file (enumerator, NULL,NULL);
+
+  /* Get the GList of notes and load them */
   while (info)
   {
-    copy_note_file (info, dir, dest);
+    iter = copy_note (info, container);
+    if (iter)
+      notes_proposal = g_list_prepend (notes_proposal, iter);
+
     g_object_unref (info);
     info = g_file_enumerator_next_file (enumerator, NULL,NULL);
   }
 
-  g_object_unref (dir);
+  for (notes = notes_proposal; notes != NULL; notes = notes->next)
+  {
+    BijiNoteObj *note = notes->data;
+
+    /* Don't add an already imported note */
+    if (note_book_get_note_at_path (self->priv->book, biji_note_obj_get_path (note)))
+    {
+      abort_note (note);
+    }
+
+    /* Sanitize, append & save */
+    else
+    {
+      /* Title */
+      unique_title = biji_note_book_get_unique_title (self->priv->book,
+                                                      biji_note_obj_get_title (note));
+      biji_note_obj_set_title (note, unique_title);
+      g_free (unique_title);
+
+      /* Color */
+      settings = bjb_app_get_settings (self);
+      g_object_get (G_OBJECT (settings), "color", &default_color, NULL);
+      if (gdk_rgba_parse (&color, default_color))
+        biji_note_obj_set_rgba (note, &color);
+
+      g_free (default_color);
+
+      biji_note_book_append_new_note (self->priv->book, note, FALSE);
+      biji_note_obj_save_note (note);
+    }
+  }
+
+  /* NoteBook will notify for all opened windows */
+  g_list_free (notes_proposal);
+  biji_note_book_notify_changed (self->priv->book);
 }
 
 static void
-import_notes_from_x (GFile *bijiben_dir, gchar *path)
+import_notes_from_x (gchar *x, Bijiben *self)
 {
-  GFile *to_import;
-  GFileEnumerator *enumerator;
+  GFile *to_import = g_file_new_for_path (x);
 
-  to_import = g_file_new_for_path (path);
-  enumerator = g_file_enumerate_children (to_import,
-                                          ATTRIBUTES_FOR_NOTEBOOK,
-                                          G_PRIORITY_DEFAULT, NULL,NULL);
+  g_file_enumerate_children_async (to_import, ATTRIBUTES_FOR_NOTEBOOK,
+                                   G_FILE_QUERY_INFO_NONE, G_PRIORITY_DEFAULT,
+                                   NULL, (GAsyncReadyCallback) list_notes_to_copy, self);
 
-  list_notes_to_copy (enumerator, bijiben_dir);
-  g_object_unref (enumerator);
   g_object_unref (to_import);
 }
 
 void
 import_notes (Bijiben *self, gchar *location)
 {
-  gchar *storage_path, *path_to_import;
-  GFile *bjb_dir;
-
-  storage_path = g_build_filename (g_get_user_data_dir (), "bijiben", NULL);
-  bjb_dir = g_file_new_for_path (storage_path);
-  g_free (storage_path);
+  gchar *path_to_import;
 
   if (g_strcmp0 (location, "tomboy") ==0)
   {
     path_to_import = g_build_filename (g_get_user_data_dir (), "tomboy", NULL);
-    import_notes_from_x (bjb_dir, path_to_import);
+    import_notes_from_x (path_to_import, self);
     g_free (path_to_import);
   }
 
   else if (g_strcmp0 (location, "gnote") ==0)
   {
     path_to_import = g_build_filename (g_get_user_data_dir (), "gnote", NULL);
-    import_notes_from_x (bjb_dir, path_to_import);
+    import_notes_from_x (path_to_import, self);
     g_free (path_to_import);
   }
-
-  g_object_unref (bjb_dir);
 }
 
 static void
diff --git a/src/libbiji/biji-note-book.c b/src/libbiji/biji-note-book.c
index 0470e11..7469734 100644
--- a/src/libbiji/biji-note-book.c
+++ b/src/libbiji/biji-note-book.c
@@ -170,11 +170,11 @@ biji_note_book_get_unique_title (BijiNoteBook *book, gchar *title)
   return new_title;
 }
 
-static gboolean
-notify_changed(BijiNoteObj *note, BijiNoteBook *book)
+gboolean
+biji_note_book_notify_changed (BijiNoteBook *book)
 {
   g_signal_emit ( G_OBJECT (book), biji_book_signals[BOOK_AMENDED],0);
-  return FALSE ;
+  return FALSE;
 }
 
 static void
@@ -188,9 +188,9 @@ _biji_note_book_add_one_note(BijiNoteBook *book,BijiNoteObj *note)
   g_hash_table_insert (book->priv->notes,
                        biji_note_obj_get_path (note), note);
 
-  book->priv->note_renamed = g_signal_connect(note,"renamed",
-                                              G_CALLBACK(notify_changed),book);
-  g_signal_connect (note,"changed", G_CALLBACK(notify_changed),book);
+  book->priv->note_renamed = g_signal_connect_swapped (note,"renamed",
+                                            G_CALLBACK(biji_note_book_notify_changed),book);
+  g_signal_connect_swapped (note,"changed", G_CALLBACK(biji_note_book_notify_changed),book);
 }
 
 #define ATTRIBUTES_FOR_NOTEBOOK "standard::content-type,standard::name"
@@ -253,7 +253,7 @@ enumerate_next_files_ready_cb (GObject *source,
   g_free (base_path);
   g_list_free_full (files, g_object_unref);
 
-  g_signal_emit (G_OBJECT (self), biji_book_signals[BOOK_AMENDED],0);
+  biji_note_book_notify_changed (self);
 }
 
 static void
@@ -367,13 +367,15 @@ _note_book_remove_one_note(BijiNoteBook *book,BijiNoteObj *note)
 }
 
 /* Notes collection */
-void note_book_append_new_note(BijiNoteBook *book,BijiNoteObj *note)
-{    
-  if (BIJI_IS_NOTE_BOOK(book) && BIJI_IS_NOTE_OBJ(note))
-  {
-    _biji_note_book_add_one_note (book,note);
-    g_signal_emit (G_OBJECT (book), biji_book_signals[BOOK_AMENDED],0);
-  }
+void
+biji_note_book_append_new_note (BijiNoteBook *book, BijiNoteObj *note, gboolean notify)
+{
+  g_return_if_fail (BIJI_IS_NOTE_BOOK (book));
+  g_return_if_fail (BIJI_IS_NOTE_OBJ (note));
+
+  _biji_note_book_add_one_note (book,note);
+  if (notify)
+    biji_note_book_notify_changed (book);
 }
 
 gboolean 
@@ -469,7 +471,7 @@ biji_note_book_get_new_note_from_string (BijiNoteBook *book,
     biji_note_obj_set_title (ret, title);
 
   biji_note_obj_save_note (ret);
-  note_book_append_new_note (book,ret);
+  biji_note_book_append_new_note (book, ret, TRUE);
 
   return ret;
 }
@@ -489,7 +491,7 @@ biji_note_book_new_note_with_text (BijiNoteBook *book,
   biji_note_obj_set_html_content (ret, plain_text);
 
   biji_note_obj_save_note (ret);
-  note_book_append_new_note (book,ret);
+  biji_note_book_append_new_note (book, ret, TRUE);
 
   return ret;
 }
diff --git a/src/libbiji/biji-note-book.h b/src/libbiji/biji-note-book.h
index 4ce104a..94729bf 100644
--- a/src/libbiji/biji-note-book.h
+++ b/src/libbiji/biji-note-book.h
@@ -42,7 +42,9 @@ gboolean _note_book_remove_one_note(BijiNoteBook *book,BijiNoteObj *note);
 
 void _biji_note_book_add_note_to_tag_book(BijiNoteBook *book,BijiNoteObj *note,gchar *tag);
 
-void note_book_append_new_note(BijiNoteBook *book,BijiNoteObj *note);
+void biji_note_book_append_new_note (BijiNoteBook *book, BijiNoteObj *note, gboolean notify);
+
+gboolean biji_note_book_notify_changed (BijiNoteBook *book);
 
 gboolean biji_note_book_remove_note(BijiNoteBook *book,BijiNoteObj *note);
 
diff --git a/src/libbiji/biji-note-obj.h b/src/libbiji/biji-note-obj.h
index 996d26e..b3078fa 100644
--- a/src/libbiji/biji-note-obj.h
+++ b/src/libbiji/biji-note-obj.h
@@ -64,6 +64,8 @@ GType biji_note_obj_get_type (void) G_GNUC_CONST;
 
 BijiNoteObj * biji_note_obj_new_from_path (const gchar *path);
 
+void biji_note_obj_fix_path (BijiNoteObj *note, gchar *path);
+
 /////////////////////////////////////////////////// Relationships other notes
 gpointer biji_note_obj_get_note_book(BijiNoteObj *note);
 



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