[bijiben] Use GHashTable for tags and name them labels
- From: Pierre-Yves Luyten <pyluyten src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [bijiben] Use GHashTable for tags and name them labels
- Date: Mon, 10 Dec 2012 21:48:32 +0000 (UTC)
commit f8b2d276109db9681bdb5d1d980bdfc846fb1fb7
Author: Pierre-Yves Luyten <py luyten fr>
Date: Mon Dec 10 22:47:02 2012 +0100
Use GHashTable for tags and name them labels
Evolution of this part is unclear but this changes simplify things
src/bjb-note-tag-dialog.c | 8 +-
src/libbiji/biji-note-obj.c | 149 +++++++--------------
src/libbiji/biji-note-obj.h | 35 ++----
src/libbiji/deserializer/biji-lazy-deserializer.c | 3 +-
src/libbiji/serializer/biji-lazy-serializer.c | 2 +-
5 files changed, 66 insertions(+), 131 deletions(-)
---
diff --git a/src/bjb-note-tag-dialog.c b/src/bjb-note-tag-dialog.c
index 73b1bb5..add9b87 100644
--- a/src/bjb-note-tag-dialog.c
+++ b/src/bjb-note-tag-dialog.c
@@ -72,12 +72,12 @@ append_tag (gchar *tag, BjbNoteTagDialog *self)
GList *l;
gtk_list_store_append (priv->store, &iter);
- note_has_tag = biji_note_obj_has_tag (priv->notes->data, tag);
+ note_has_tag = biji_note_obj_has_label (priv->notes->data, tag);
/* Check if other notes have the same */
for (l = priv->notes; l != NULL; l = l->next)
{
- if (biji_note_obj_has_tag (l->data, tag) != note_has_tag)
+ if (biji_note_obj_has_label (l->data, tag) != note_has_tag)
{
note_has_tag = SELECTION_INCONSISTENT;
break;
@@ -117,13 +117,13 @@ update_tags_model (BjbNoteTagDialog *self)
static void
note_dialog_add_tag (BijiNoteObj *note, gchar *tag)
{
- biji_note_obj_add_tag (note, tag);
+ biji_note_obj_add_label (note, tag, TRUE);
}
static void
note_dialog_remove_tag (BijiNoteObj *note, gchar *tag)
{
- biji_note_obj_remove_tag (note, tag);
+ biji_note_obj_remove_label (note, tag);
}
static void
diff --git a/src/libbiji/biji-note-obj.c b/src/libbiji/biji-note-obj.c
index 8b5c0ab..f13d665 100644
--- a/src/libbiji/biji-note-obj.c
+++ b/src/libbiji/biji-note-obj.c
@@ -54,9 +54,9 @@ struct _BijiNoteObjPrivate
GdkPixbuf *icon;
gboolean icon_needs_update;
- /* TAGS may be notebooks.
- * Templates are just "system:notebook:" tags.*/
- GList *tags ;
+ /* Tags
+ * In Tomboy, templates are 'system:notebook:%s' tags.*/
+ GHashTable *labels;
gboolean is_template ;
gboolean does_title_survive;
@@ -129,7 +129,7 @@ biji_note_obj_init (BijiNoteObj *self)
/* Keep value unitialied, so bijiben knows to assign default color */
priv->color = NULL;
- priv->tags = NULL;
+ priv->labels = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
}
static void
@@ -151,7 +151,7 @@ biji_note_obj_finalize (GObject *object)
if (priv->raw_text);
g_free (priv->raw_text);
- g_list_free (priv->tags);
+ g_hash_table_destroy (priv->labels);
g_clear_object (&priv->icon);
@@ -571,106 +571,91 @@ void biji_note_obj_set_raw_text (BijiNoteObj *note, gchar *plain_text)
}
GList *
-_biji_note_obj_get_tags(BijiNoteObj *n)
+biji_note_obj_get_labels (BijiNoteObj *n)
{
g_return_val_if_fail (BIJI_IS_NOTE_OBJ (n), NULL);
- return g_list_copy(n->priv->tags);
+ return g_hash_table_get_values (n->priv->labels);
}
gboolean
-_biji_note_obj_has_tag(BijiNoteObj *note,gchar *tag)
+biji_note_obj_has_label (BijiNoteObj *note, gchar *label)
{
- gint i ;
+ if (g_hash_table_lookup (note->priv->labels, label))
+ return TRUE;
- if ( note->priv-> tags == NULL )
- return FALSE ;
+ return FALSE;
+}
+
+gboolean
+biji_note_obj_add_label (BijiNoteObj *note, gchar *label, gboolean on_user_action_cb)
+{
+ g_return_val_if_fail (BIJI_IS_NOTE_OBJ (note), FALSE);
+ g_return_val_if_fail (label != NULL, FALSE);
+ g_return_val_if_fail (!biji_note_obj_has_label (note, label), FALSE);
- for ( i=0 ; i<g_list_length (note->priv->tags) ; i++ )
+ g_hash_table_add (note->priv->labels, label);
+
+ if (on_user_action_cb)
{
- if ( g_strcmp0 ((gchar*)g_list_nth_data(note->priv->tags,i),tag)==0)
- {
- return TRUE ;
- }
+ push_existing_or_new_tag_to_note (label, note); // Tracker
+ biji_note_id_set_last_metadata_change_date_now (note->priv->id);
+ biji_note_obj_save_note (note);
}
- return FALSE ;
+
+ return TRUE;
}
gboolean
-biji_note_obj_add_tag (BijiNoteObj *note, gchar *tag)
+biji_note_obj_remove_label (BijiNoteObj *note, gchar *label)
{
g_return_val_if_fail (BIJI_IS_NOTE_OBJ (note), FALSE);
- g_return_val_if_fail (tag != NULL, FALSE);
- g_return_val_if_fail (! _biji_note_obj_has_tag (note, tag), FALSE);
- note->priv->tags = g_list_prepend (note->priv->tags, g_strdup (tag));
- push_existing_or_new_tag_to_note (tag, note);
- biji_note_id_set_last_metadata_change_date_now (note->priv->id);
- biji_note_obj_save_note (note);
+ if (g_hash_table_remove (note->priv->labels, label))
+ {
+ remove_tag_from_note (label, note); // tracker.
+ biji_note_id_set_last_metadata_change_date_now (note->priv->id);
+ biji_note_obj_save_note (note);
+ return TRUE;
+ }
- return TRUE;
+ return FALSE;
}
gboolean
-_biji_note_obj_has_tag_prefix(BijiNoteObj *note,gchar *tag)
+biji_note_obj_has_tag_prefix (BijiNoteObj *note, gchar *label)
{
- gint i ;
+ gboolean retval = FALSE;
+ GList *tags, *l;
- if ( note->priv-> tags == NULL )
- return FALSE ;
+ tags = g_hash_table_get_keys (note->priv->labels);
- for ( i=0 ; i<g_list_length (note->priv->tags) ; i++ )
+ for (l = tags; l != NULL; l=l->next)
{
- if ( g_str_has_prefix ((gchar*)g_list_nth_data(note->priv->tags,i),tag))
+ if (g_str_has_prefix (l->data, label))
{
- return TRUE ;
+ retval = TRUE;
+ break;
}
}
- return FALSE ;
-}
-
-void
-_biji_note_obj_set_tags(BijiNoteObj *n, GList *tags)
-{
- if ( n->priv->tags != NULL )
- {
- g_list_free(n->priv->tags);
- }
-
- n->priv->tags = g_list_copy (tags);
+ g_list_free (tags);
+ return retval;
}
gboolean
-note_obj_is_template(BijiNoteObj *n)
+note_obj_is_template (BijiNoteObj *n)
{
- g_return_val_if_fail(BIJI_IS_NOTE_OBJ(n),FALSE);
+ g_return_val_if_fail(BIJI_IS_NOTE_OBJ(n),FALSE);
return n->priv->is_template;
}
void
-note_obj_set_is_template(BijiNoteObj *n,gboolean is_template)
+note_obj_set_is_template (BijiNoteObj *n,gboolean is_template)
{
n->priv->is_template = is_template ;
}
-gchar *
-_biji_note_template_get_tag(BijiNoteObj *template)
-{
- if ( template->priv->is_template == FALSE )
- {
- g_message("BIJI NOTE TEMPLATE GET TAG ONLY WORKS WITH TEMPLATES");
- return NULL ;
- }
-
- if ( template->priv->tags == NULL )
- {
- g_message("template has no tag, which should never happen");
- }
-
- return g_list_nth_data (template->priv->tags,0);
-}
-
/* TODO : see if note beeing deleted. set metadata date
* and last_change date according to a WHAT param */
void
@@ -797,42 +782,6 @@ biji_note_obj_is_template(BijiNoteObj *note)
return note_obj_is_template(note);
}
-gboolean
-biji_note_obj_has_tag(BijiNoteObj *note,gchar *tag)
-{
- return _biji_note_obj_has_tag(note,tag);
-}
-
-GList *biji_note_obj_get_tags(BijiNoteObj *note)
-{
- return _biji_note_obj_get_tags(note);
-}
-
-gboolean
-biji_note_obj_remove_tag (BijiNoteObj *note,gchar *tag)
-{
- g_return_val_if_fail (BIJI_IS_NOTE_OBJ (note), FALSE);
- g_return_val_if_fail (_biji_note_obj_has_tag (note, tag), FALSE);
-
- remove_tag_from_note (tag, note); // tracker.
-
- GList *current = biji_note_obj_get_tags (note);
- GList *l;
-
- for (l=current; l != NULL; l=l->next)
- {
- if (g_strcmp0 (l->data,tag) == 0)
- {
- _biji_note_obj_set_tags (note, g_list_remove (current, l->data));
- biji_note_id_set_last_metadata_change_date_now (note->priv->id);
- biji_note_obj_save_note (note);
- return TRUE;
- }
- }
-
- return FALSE;
-}
-
gchar *
biji_note_obj_get_last_change_date(BijiNoteObj *note)
{
diff --git a/src/libbiji/biji-note-obj.h b/src/libbiji/biji-note-obj.h
index e0d7d71..996d26e 100644
--- a/src/libbiji/biji-note-obj.h
+++ b/src/libbiji/biji-note-obj.h
@@ -62,15 +62,6 @@ struct _BijiNoteObj
GType biji_note_obj_get_type (void) G_GNUC_CONST;
-// This structure, for convenience, associates a tag with its notes
-typedef struct
-{
- gchar *name ;
- BijiNoteObj *template_note ;
- GList *notes ;
- gint length ;
-} TagBook ;
-
BijiNoteObj * biji_note_obj_new_from_path (const gchar *path);
/////////////////////////////////////////////////// Relationships other notes
@@ -118,17 +109,21 @@ gboolean biji_note_obj_get_rgba(BijiNoteObj *n, GdkRGBA *rgba) ;
void biji_note_obj_set_rgba(BijiNoteObj *n, GdkRGBA *rgba) ;
-////////////////////////////////////////////////////// Tags (as in tag clouds)
-GList * _biji_note_obj_get_tags(BijiNoteObj *n) ;
-gboolean _biji_note_obj_has_tag(BijiNoteObj *note,gchar *tag);
-void _biji_note_obj_add_tag(BijiNoteObj *note, gchar *tag) ;
-gboolean _biji_note_obj_has_tag_prefix(BijiNoteObj *note,gchar *tag);
-void _biji_note_obj_set_tags(BijiNoteObj *n, GList *tags) ;
+/* Tracker Tags. Free the GList.
+ * Remove label always due to user action. Add label has to precise */
+
+GList * biji_note_obj_get_labels (BijiNoteObj *n);
+
+gboolean biji_note_obj_has_label (BijiNoteObj *note, gchar *label);
+
+gboolean biji_note_obj_add_label (BijiNoteObj *note, gchar *label, gboolean on_user_action_cb);
+
+gboolean biji_note_obj_remove_label (BijiNoteObj *note, gchar *label);
/////////////////////////////////////////////////////////////////// templates
gboolean note_obj_is_template(BijiNoteObj *n) ;
+
void note_obj_set_is_template(BijiNoteObj *n,gboolean is_template);
-gchar *_biji_note_template_get_tag(BijiNoteObj *n);
/////////////////////////////////////////////////////////////////// Save
void biji_note_obj_save_note (BijiNoteObj *self);
@@ -147,14 +142,6 @@ gboolean biji_note_obj_set_title (BijiNoteObj* note_obj_ptr,gchar* title);
gboolean biji_note_obj_is_template(BijiNoteObj *note);
-gboolean biji_note_obj_has_tag(BijiNoteObj *note,gchar *tag);
-
-GList *biji_note_obj_get_tags(BijiNoteObj *note);
-
-gboolean biji_note_obj_add_tag(BijiNoteObj *note, gchar *tag);
-
-gboolean biji_note_obj_remove_tag(BijiNoteObj *note,gchar *tag);
-
gchar *biji_note_obj_get_last_change_date(BijiNoteObj *note);
gchar *biji_note_obj_get_create_date(BijiNoteObj *note);
diff --git a/src/libbiji/deserializer/biji-lazy-deserializer.c b/src/libbiji/deserializer/biji-lazy-deserializer.c
index 6e687a9..20d691e 100644
--- a/src/libbiji/deserializer/biji-lazy-deserializer.c
+++ b/src/libbiji/deserializer/biji-lazy-deserializer.c
@@ -598,8 +598,7 @@ processNode (BijiLazyDeserializer *self)
{
norm = g_string_new (tag);
g_string_erase (norm,0,16);
- _biji_note_obj_set_tags (n, g_list_prepend((GList*)_biji_note_obj_get_tags(n),
- g_string_free (norm,FALSE)));
+ biji_note_obj_add_label (n, g_string_free (norm, FALSE), FALSE);
}
free (tag);
diff --git a/src/libbiji/serializer/biji-lazy-serializer.c b/src/libbiji/serializer/biji-lazy-serializer.c
index 80b0d71..975f124 100644
--- a/src/libbiji/serializer/biji-lazy-serializer.c
+++ b/src/libbiji/serializer/biji-lazy-serializer.c
@@ -391,7 +391,7 @@ biji_lazy_serialize_internal (BijiLazySerializer *self)
//<tags>
xmlTextWriterWriteRaw(priv->writer, BAD_CAST "\n ");
xmlTextWriterStartElement (priv->writer, BAD_CAST "tags");
- tags = _biji_note_obj_get_tags (priv->note);
+ tags = biji_note_obj_get_labels (priv->note);
g_list_foreach (tags, (GFunc) serialize_tags, priv->writer);
xmlTextWriterEndElement (priv->writer);
g_list_free (tags);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]