[bijiben] Fix Unique Note Titles mechanism



commit e142011814f9165874cb9181b972b001e05832e6
Author: Pierre-Yves Luyten <py luyten fr>
Date:   Fri Dec 7 02:23:19 2012 +0100

    Fix Unique Note Titles mechanism
    
    It's now up to the note creator to make title unique, previous notes are not checked. But renaming note performs the check.

 src/bjb-bijiben.c                       |    5 ++-
 src/libbiji/biji-note-book.c            |   88 ++++++++++++++++---------------
 src/libbiji/biji-note-book.h            |    3 +-
 src/libbiji/biji-note-obj.c             |   37 ++++++++++---
 src/libbiji/editor/biji-webkit-editor.c |   19 ++++---
 5 files changed, 92 insertions(+), 60 deletions(-)
---
diff --git a/src/bjb-bijiben.c b/src/bjb-bijiben.c
index 8c21294..cd9e328 100644
--- a/src/bjb-bijiben.c
+++ b/src/bjb-bijiben.c
@@ -116,7 +116,7 @@ copy_note_file (GFileInfo *info,
   BijiNoteObj *note_obj;
   GFile *note, *result;
   const gchar *name;
-  gchar *path, *default_color;
+  gchar *path, *default_color, *unique_title;
   GError *error = NULL;
   BjbSettings *settings;
   GdkRGBA color;
@@ -154,6 +154,9 @@ copy_note_file (GFileInfo *info,
   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);
 
   g_free (path);
diff --git a/src/libbiji/biji-note-book.c b/src/libbiji/biji-note-book.c
index 57fd061..c0c9e3e 100644
--- a/src/libbiji/biji-note-book.c
+++ b/src/libbiji/biji-note-book.c
@@ -176,31 +176,53 @@ biji_book_get_or_create_tag_book(BijiNoteBook *book, gchar *tag)
   return result ;
 }
 
-/* If title not unique, add sufffix "n", starting with 2, until ok */
-static void
-_biji_note_book_sanitize_title (BijiNoteBook *book, BijiNoteObj *note)
+static gboolean
+title_is_unique (BijiNoteBook *book,gchar *title)
 {
-  gchar *title, *new_title;
+  gboolean is_unique = TRUE;
+  BijiNoteObj *iter;
+  GList *notes, *l;
 
-  title = biji_note_obj_get_title (note);
+  notes = g_hash_table_get_values (book->priv->notes);
 
-  if (title)
+  for ( l=notes ; l != NULL ; l = l->next)
   {
-    new_title = g_strdup (title);
-    gint suffix = 2;
+    iter = BIJI_NOTE_OBJ (l->data);
 
-    while (!_biji_note_book_is_title_unique (book, new_title))
+    if (g_strcmp0 (biji_note_obj_get_title (iter), title) == 0)
     {
-      g_free (new_title);
-      new_title = g_strdup_printf("%s (%i)", title, suffix);
-      suffix++;
+     is_unique = FALSE;
+     break;
     }
+  }
 
-    if ( g_strcmp0 (new_title, title) != 0)
-      biji_note_obj_set_title (note, new_title);
+  g_list_free (notes);
+  return is_unique;
+}
 
-    g_free(new_title);
+/* If title not unique, add sufffix "n", starting with 2, until ok */
+gchar *
+biji_note_book_get_unique_title (BijiNoteBook *book, gchar *title)
+{
+  if (!book)
+    return g_strdup (title);
+
+  gchar *new_title;
+
+  if (!title)
+    title = "";
+
+  new_title = g_strdup (title);
+  gint suffix = 2;
+
+  while (!title_is_unique (book, new_title))
+  {
+    g_free (new_title);
+    new_title = g_strdup_printf("%s (%i)", title, suffix);
+    suffix++;
   }
+
+  return new_title;
 }
 
 static gboolean
@@ -214,9 +236,7 @@ static void
 _biji_note_book_add_one_note(BijiNoteBook *book,BijiNoteObj *note)
 {
   g_return_if_fail(BIJI_IS_NOTE_OBJ(note));
-  gint i ; 
-
-  _biji_note_book_sanitize_title(book,note);
+  gint i;
 
   /* Welcome to the book ! */
   _biji_note_obj_set_book(note,(gpointer)book);
@@ -404,26 +424,6 @@ biji_note_book_class_init (BijiNoteBookClass *klass)
   g_type_class_add_private (klass, sizeof (BijiNoteBookPrivate));
 }
 
-gboolean
-_biji_note_book_is_title_unique(BijiNoteBook *book,gchar *title)
-{
-  gint i;
-  gboolean result = TRUE;
-  BijiNoteObj *iter;
-  GList *notes = g_hash_table_get_values (book->priv->notes);
-
-  for ( i=0 ; i < g_hash_table_size (book->priv->notes) ; i++)
-  {
-    iter = BIJI_NOTE_OBJ (g_list_nth_data (notes, i));
-
-    if (g_strcmp0 (biji_note_obj_get_title (iter), title) == 0)
-     result = FALSE;
-  }
-
-  g_list_free (notes);
-  return result;
-}
-
 void
 _biji_note_book_add_note_to_tag_book(BijiNoteBook *book,
                                      BijiNoteObj *note,
@@ -693,14 +693,15 @@ biji_note_book_get_new_note_from_string (BijiNoteBook *book,
 {
   BijiNoteObj *ret = get_note_skeleton (book);
 
-  /* Note will copy title */
-  if (title && g_strcmp0 (title, "") != 0)
+  /* Note will copy title
+   * We do NOT sanitize here because blank title is allowed ("initial") */
+  if (title && g_strcmp0 (title, "") !=0)
     biji_note_obj_set_title (ret, title);
 
   biji_note_obj_save_note (ret);
   note_book_append_new_note (book,ret);
 
-  return ret ;
+  return ret;
 }
 
 BijiNoteObj *
@@ -708,9 +709,12 @@ biji_note_book_new_note_with_text (BijiNoteBook *book,
                                    gchar *plain_text)
 {
   BijiNoteObj *ret = get_note_skeleton (book);
+  gchar *unique_title = biji_note_book_get_unique_title (book, DEFAULT_NOTE_TITLE);
 
   /* Note will copy title, raw_text and html strings */
-  biji_note_obj_set_title (ret, DEFAULT_NOTE_TITLE);
+  biji_note_obj_set_title (ret, unique_title);
+  g_free (unique_title);
+
   biji_note_obj_set_raw_text (ret, g_strdup (plain_text));
   biji_note_obj_set_html_content (ret, plain_text);
 
diff --git a/src/libbiji/biji-note-book.h b/src/libbiji/biji-note-book.h
index e626351..d0d07c0 100644
--- a/src/libbiji/biji-note-book.h
+++ b/src/libbiji/biji-note-book.h
@@ -36,7 +36,8 @@ GType biji_note_book_get_type (void) G_GNUC_CONST;
 
 BijiNoteBook * biji_note_book_new (GFile *location);
 
-gboolean _biji_note_book_is_title_unique(BijiNoteBook *book,gchar *title);
+gchar * biji_note_book_get_unique_title (BijiNoteBook *book, gchar *title);
+
 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);
 
diff --git a/src/libbiji/biji-note-obj.c b/src/libbiji/biji-note-obj.c
index 4f42fe1..564eaaf 100644
--- a/src/libbiji/biji-note-obj.c
+++ b/src/libbiji/biji-note-obj.c
@@ -412,24 +412,35 @@ biji_note_obj_get_title(BijiNoteObj *obj)
 
 /* If already a title, then note is renamed */
 gboolean
-biji_note_obj_set_title(BijiNoteObj *note,gchar *title)
+biji_note_obj_set_title (BijiNoteObj *note, gchar *proposed_title)
 {
   gboolean initial = FALSE;
   note->priv->does_title_survive = TRUE;
+  gchar *title;
 
-  if (!biji_note_id_get_title(note->priv->id))
+  if (!biji_note_id_get_title (note->priv->id))
     initial = TRUE;
 
-  if (g_strcmp0 (title, biji_note_id_get_title (note->priv->id))==0)
+  if (g_strcmp0 (proposed_title, biji_note_id_get_title (note->priv->id))==0)
     return FALSE;
 
-  /* Emit signal even if initial title, just to let know */
-  biji_note_id_set_title (note->priv->id,title);
-  g_signal_emit (G_OBJECT (note), biji_obj_signals[NOTE_RENAMED],0);
-
+  /* If the note is really renamed, check the new title */
   if (!initial)
+  {
+    title = biji_note_book_get_unique_title (note->priv->book, proposed_title);
     biji_note_id_set_last_metadata_change_date_now (note->priv->id);
+  }
 
+  /* Otherwise it's up to the caller to sanitize its title */
+  else
+  {
+    title = g_strdup (proposed_title);
+  }
+
+  /* Emit signal even if initial title, just to let know */
+  biji_note_id_set_title (note->priv->id, title);
+  g_free (title);
+  g_signal_emit (G_OBJECT (note), biji_obj_signals[NOTE_RENAMED],0);
   return TRUE;
 }
 
@@ -918,11 +929,19 @@ _biji_note_obj_close (BijiNoteObj *note)
    * - new note
    * - only one row
    * In such case we want to change title */
-  if ( ! biji_note_obj_title_survives (note)
+ /* if ( ! biji_note_obj_title_survives (note)
       && note->priv->raw_text
-      && g_strcmp0 (note->priv->raw_text, "") != 0)
+      && g_strcmp0 (note->priv->raw_text, "") != 0) 
   {
     biji_note_obj_set_title (note, note->priv->raw_text);
+  } */
+
+  if (!biji_note_obj_title_survives (note))
+  {
+    gchar *title = biji_note_book_get_unique_title (biji_note_obj_get_note_book (note),
+                                                    note->priv->raw_text);
+    biji_note_obj_set_title (note, title);
+    g_free (title);
   }
 }
 
diff --git a/src/libbiji/editor/biji-webkit-editor.c b/src/libbiji/editor/biji-webkit-editor.c
index 88c0703..789c231 100644
--- a/src/libbiji/editor/biji-webkit-editor.c
+++ b/src/libbiji/editor/biji-webkit-editor.c
@@ -18,6 +18,7 @@
 #include <libxml/xmlwriter.h>
 
 #include "../biji-string.h"
+#include "../biji-note-book.h"
 #include "biji-webkit-editor.h"
 #include "biji-editor-selection.h"
 
@@ -264,16 +265,20 @@ on_content_changed (WebKitWebView *view)
     g_warning ("title is %s", rows[0]);
 
     /* if we have a carriage return and thus, a proper title
-     * we still need to ensure it's clean */
+     * we still need to ensure it's clean and unique */
     if (g_strv_length (rows) > 1)
     {
-      gchar *sanitized_title;
+      gchar *sane_title, *unique_title;
 
-      sanitized_title = biji_str_mass_replace (rows[0],
-                                               "&nbsp;", "",
-                                               NULL);
-      biji_note_obj_set_title (note, sanitized_title);
-      g_free (sanitized_title);
+      sane_title = biji_str_mass_replace (rows[0],
+                                          "&nbsp;", "",
+                                          NULL);
+
+      unique_title = biji_note_book_get_unique_title (biji_note_obj_get_note_book (note),
+                                                      sane_title);
+      g_free (sane_title);
+      biji_note_obj_set_title (note, unique_title);
+      g_free (unique_title);
     }
 
     g_strfreev (rows);



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