[bijiben] NoteIcon: Save and read files



commit 1449760dcb25b85c03fc9a95e8f8c2169ae3ac18
Author: Pierre-Yves Luyten <py luyten fr>
Date:   Thu Dec 6 00:49:18 2012 +0100

    NoteIcon: Save and read files
    
    XDG cache dir is used, data lost would just slow down.

 src/libbiji/biji-note-obj.c                       |   23 +++++++++++++
 src/libbiji/biji-note-obj.h                       |    2 +
 src/libbiji/deserializer/biji-lazy-deserializer.c |   29 +++++++++++++++++
 src/libbiji/editor/biji-webkit-editor.c           |    1 +
 src/libbiji/serializer/biji-lazy-serializer.c     |   36 +++++++++++++++++++++
 5 files changed, 91 insertions(+), 0 deletions(-)
---
diff --git a/src/libbiji/biji-note-obj.c b/src/libbiji/biji-note-obj.c
index 534c71a..62ba15e 100644
--- a/src/libbiji/biji-note-obj.c
+++ b/src/libbiji/biji-note-obj.c
@@ -86,9 +86,13 @@ on_save_timeout (BijiNoteObj *self)
   if (!priv->needs_save)
     return;
 
+  g_object_ref (self);
+
   biji_lazy_serialize (self);
   bijiben_push_note_to_tracker(self);
+
   priv->needs_save = FALSE;
+  g_object_unref (self);
 }
 
 static void
@@ -658,6 +662,25 @@ biji_note_obj_save_note (BijiNoteObj *self)
   biji_timeout_reset (self->priv->timeout, 3000);
 }
 
+gchar *
+biji_note_obj_get_uuid (BijiNoteObj *note)
+{
+  g_return_val_if_fail (BIJI_IS_NOTE_OBJ (note), NULL);
+  return biji_note_id_get_uuid (note->priv->id);
+}
+
+void
+biji_note_obj_set_icon (BijiNoteObj *note, GdkPixbuf *pix)
+{
+  g_return_if_fail (BIJI_IS_NOTE_OBJ (note));
+
+  if (!note->priv->icon)
+    note->priv->icon = pix;
+
+  else
+    g_warning ("Cannot use _set_icon_ with iconified note. This has no sense.");
+}
+
 GdkPixbuf *
 biji_note_obj_get_icon (BijiNoteObj *note)
 {
diff --git a/src/libbiji/biji-note-obj.h b/src/libbiji/biji-note-obj.h
index 0e8b962..e4932e2 100644
--- a/src/libbiji/biji-note-obj.h
+++ b/src/libbiji/biji-note-obj.h
@@ -135,7 +135,9 @@ void biji_note_obj_save_note (BijiNoteObj *self);
 
 GdkPixbuf * biji_note_obj_get_icon (BijiNoteObj *note);
 
+void biji_note_obj_set_icon (BijiNoteObj *note, GdkPixbuf *pix);
 
+gchar *biji_note_obj_get_uuid (BijiNoteObj *note);
 
 gchar *biji_note_get_raw_text(BijiNoteObj *note);
 
diff --git a/src/libbiji/deserializer/biji-lazy-deserializer.c b/src/libbiji/deserializer/biji-lazy-deserializer.c
index 69ee620..f20752b 100644
--- a/src/libbiji/deserializer/biji-lazy-deserializer.c
+++ b/src/libbiji/deserializer/biji-lazy-deserializer.c
@@ -699,6 +699,33 @@ biji_lazy_deserializer_new (BijiNoteObj *note)
                        NULL);
 }
 
+/* As soon as a deserializer is there, 
+ * the note HAS a path, a GFile, and most probably an icon.*/
+static void
+biji_note_obj_load_icon (BijiNoteObj *note)
+{
+  gchar *filename;
+  GdkPixbuf *retval;
+  GError *error = NULL;
+
+  filename = g_build_filename (g_get_user_cache_dir (),
+                               g_get_application_name (),
+                               biji_note_obj_get_uuid (note),
+                               NULL);
+
+  retval = gdk_pixbuf_new_from_file (filename, &error);
+  if (error)
+  {
+     g_warning ("%s", error->message);
+     g_error_free (error);
+  }
+
+  else
+  {
+     biji_note_obj_set_icon (note, retval);
+  }
+}
+
 gboolean
 biji_lazy_deserialize (BijiNoteObj *note)
 {
@@ -709,5 +736,7 @@ biji_lazy_deserialize (BijiNoteObj *note)
   result = biji_lazy_deserialize_internal (bld);
   g_clear_object (&bld);
 
+  biji_note_obj_load_icon (note);
+
   return result;
 }
diff --git a/src/libbiji/editor/biji-webkit-editor.c b/src/libbiji/editor/biji-webkit-editor.c
index 061e05c..88c0703 100644
--- a/src/libbiji/editor/biji-webkit-editor.c
+++ b/src/libbiji/editor/biji-webkit-editor.c
@@ -17,6 +17,7 @@
 
 #include <libxml/xmlwriter.h>
 
+#include "../biji-string.h"
 #include "biji-webkit-editor.h"
 #include "biji-editor-selection.h"
 
diff --git a/src/libbiji/serializer/biji-lazy-serializer.c b/src/libbiji/serializer/biji-lazy-serializer.c
index 6307541..70e9345 100644
--- a/src/libbiji/serializer/biji-lazy-serializer.c
+++ b/src/libbiji/serializer/biji-lazy-serializer.c
@@ -411,6 +411,40 @@ biji_lazy_serialize_internal (BijiLazySerializer *self)
                               NULL);
 }
 
+/* No matter if icon is saved or not.
+ * We just try */
+static void
+biji_note_obj_save_icon (BijiNoteObj *note)
+{
+  gchar *filename;
+  GFile *directory;
+  GError *error = NULL;
+
+  /* First ensure the dir exists */
+  filename = g_build_filename (g_get_user_cache_dir (),
+                               g_get_application_name (),
+                               NULL);
+  directory = g_file_new_for_path (filename);
+  g_file_make_directory (directory, NULL, NULL);
+
+  /* Png */
+  GdkPixbuf *icon = biji_note_obj_get_icon (note);
+  filename = g_build_filename (g_get_user_cache_dir (),
+                               g_get_application_name (),
+                               biji_note_obj_get_uuid (note),
+                               NULL);
+
+  gdk_pixbuf_save (icon, filename, "png", &error, NULL);
+
+  if (error)
+  {
+    g_warning ("%s", error->message);
+    g_error_free (error);
+  }
+
+  g_free (filename);
+}
+
 gboolean
 biji_lazy_serialize (BijiNoteObj *note)
 {
@@ -422,6 +456,8 @@ biji_lazy_serialize (BijiNoteObj *note)
   result = biji_lazy_serialize_internal (self);
   g_object_unref (self);
 
+  biji_note_obj_save_icon (note);
+
   return result;
 }
 



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