From 5c6a00ee93e54cc044aee5146e9fcfbcb732a2ee Mon Sep 17 00:00:00 2001
From: Lukas Fleischer <calcurse@cryptocrack.de>
Date: Tue, 2 Aug 2011 13:49:05 +0200
Subject: Use hash-bashed file names in edit_note()

Note file names are now generated based on their content. Items using
the same note will share a single note file.

Please note that this implies a few changes:

* Both random-style and hash-style note files need to be handled to
  ensure we do not break backwards compatibility.

* Note files may not be moved or deleted if a note is changed or removed
  since the original note file might be used by another item as well.

* A garbage collector to remove unreferenced note files needs to be
  implemented.

Signed-off-by: Lukas Fleischer <calcurse@cryptocrack.de>
---
 src/note.c | 42 +++++++++++++++++++++++++++++++-----------
 1 file changed, 31 insertions(+), 11 deletions(-)

(limited to 'src')

diff --git a/src/note.c b/src/note.c
index c8aa1ec..36c2c0f 100644
--- a/src/note.c
+++ b/src/note.c
@@ -37,26 +37,46 @@
 #include <unistd.h>
 
 #include "calcurse.h"
+#include "sha1.h"
 
 /* Edit a note with an external editor. */
 void
 edit_note (char **note, char *editor)
 {
-  char fullname[BUFSIZ];
-  char *filename;
+  char tmppath[BUFSIZ];
+  char *tmpext;
+  char notepath[BUFSIZ];
+  char *sha1 = mem_malloc (SHA1_DIGESTLEN * 2 + 1);
+  FILE *fp;
 
-  if (*note == NULL)
+  strncpy (tmppath, get_tempdir (), BUFSIZ);
+  strncat (tmppath, "/calcurse-note.", BUFSIZ);
+  if ((tmpext = new_tempfile (tmppath, TMPEXTSIZ)) == NULL)
+    return;
+  strncat (tmppath, tmpext, BUFSIZ);
+  mem_free (tmpext);
+
+  if (*note != NULL)
     {
-      if ((filename = new_tempfile (path_notes, NOTESIZ)) != NULL)
-        *note = filename;
-      else
-        return;
+      snprintf (notepath, BUFSIZ, "%s%s", path_notes, *note);
+      io_file_cp (notepath, tmppath);
+    }
+
+  wins_launch_external (tmppath, editor);
+
+  if (io_file_is_empty (tmppath) > 0)
+    erase_note (note, ERASE_FORCE_KEEP_NOTE);
+  else if ((fp = fopen (tmppath, "r")))
+    {
+      sha1_stream (fp, sha1);
+      fclose (fp);
+      *note = sha1;
+
+      snprintf (notepath, BUFSIZ, "%s%s", path_notes, *note);
+      io_file_cp (tmppath, notepath);
     }
-  (void)snprintf (fullname, BUFSIZ, "%s%s", path_notes, *note);
-  wins_launch_external (fullname, editor);
 
-  if (io_file_is_empty (fullname) > 0)
-    erase_note (note, ERASE_FORCE);
+  unlink (tmppath);
 }
 
 /* View a note in an external pager. */
-- 
cgit v1.2.3-70-g09d2