[notmuch] [PATCH 2/2] Preserve folder information when indexing

2010-01-29 Thread Michal Sojka
On Thursday 28 of January 2010 18:13:06 micah anderson wrote:
> On Thu, 28 Jan 2010 16:25:17 +0100, Michal Sojka  
wrote:
> > This patch was originally sent by Andreas Kl?ckner. This is a slightly
> > reworked version (only coding style changes) which was manually rebased
> > to the current HEAD.
> 
> Just a quick note about this patch: I received an offline email back
> From Andreas who said he is no longer going to improve the patch because
> he has stopped using notmuch. I hope that this doesn't mean that this is

I know. Andreas wrote it to the list as well. That was the reason why I tried 
to update the patch myself.

> dropped, it provides a pretty useful functionality for transitioning
> From a folder-based IMAP setup to notmuch! I wish I could volunteer to
> pick this up and finish it myself, but I cannot commit to doing this at
> the moment.

I have the same goal as you so I'll probably invest some time to this patch in 
order to be merged.

Michal


[notmuch] [PATCH 2/2] Preserve folder information when indexing

2010-01-28 Thread Michal Sojka
This patch was originally sent by Andreas Kl?ckner. This is a slightly
reworked version (only coding style changes) which was manually rebased
to the current HEAD.

There are still things to fix (see the previous Carl's email)
- Redundant trailing directory name traversal
- Supporting hierarchical folders
---
 lib/database.cc |   13 +
 lib/notmuch.h   |1 +
 notmuch-new.c   |   38 +-
 3 files changed, 47 insertions(+), 5 deletions(-)

diff --git a/lib/database.cc b/lib/database.cc
index cce7847..5f2f35d 100644
--- a/lib/database.cc
+++ b/lib/database.cc
@@ -84,9 +84,9 @@ typedef struct {
  * MESSAGE_ID: The unique ID of the mail mess (see "id" above)
  *
  * In addition, terms from the content of the message are added with
- * "from", "to", "attachment", and "subject" prefixes for use by the
- * user in searching. But the database doesn't really care itself
- * about any of these.
+ * "from", "to", "attachment", "subject" and "folder" prefixes for use
+ * by the user in searching. But the database doesn't really care
+ * itself about any of these.
  *
  * The data portion of a mail document is empty.
  *
@@ -154,7 +154,8 @@ prefix_t PROBABILISTIC_PREFIX[]= {
 { "from",  "XFROM" },
 { "to","XTO" },
 { "attachment","XATTACHMENT" },
-{ "subject",   "XSUBJECT"}
+{ "subject",   "XSUBJECT"},
+{ "folder","XFOLDER"}
 };

 int
@@ -1317,6 +1318,7 @@ _notmuch_database_link_message (notmuch_database_t 
*notmuch,
 notmuch_status_t
 notmuch_database_add_message (notmuch_database_t *notmuch,
  const char *filename,
+ const char *folder_name,
  notmuch_message_t **message_ret)
 {
 notmuch_message_file_t *message_file;
@@ -1432,6 +1434,9 @@ notmuch_database_add_message (notmuch_database_t *notmuch,
date = notmuch_message_file_get_header (message_file, "date");
_notmuch_message_set_date (message, date);

+   if (folder_name != NULL)
+   _notmuch_message_gen_terms (message, "folder", folder_name);
+
_notmuch_message_index_file (message, filename);
} else {
ret = NOTMUCH_STATUS_DUPLICATE_MESSAGE_ID;
diff --git a/lib/notmuch.h b/lib/notmuch.h
index 15c9db4..3a5ab78 100644
--- a/lib/notmuch.h
+++ b/lib/notmuch.h
@@ -263,6 +263,7 @@ notmuch_database_get_directory (notmuch_database_t 
*database,
 notmuch_status_t
 notmuch_database_add_message (notmuch_database_t *database,
  const char *filename,
+ const char *folder_name,
  notmuch_message_t **message);

 /* Remove a message from the given notmuch database.
diff --git a/notmuch-new.c b/notmuch-new.c
index f25c71f..b7c65db 100644
--- a/notmuch-new.c
+++ b/notmuch-new.c
@@ -21,6 +21,7 @@
 #include "notmuch-client.h"

 #include 
+#include 

 typedef struct _filename_node {
 char *filename;
@@ -169,6 +170,35 @@ _entries_resemble_maildir (struct dirent **entries, int 
count)
 return 0;
 }

+static char*
+_get_folder_base_name(const char *path)
+{
+gchar *full_folder_name = NULL;
+gchar *folder_base_name = NULL;
+
+/* Find name of "folder" containing the email. */
+full_folder_name = g_strdup(path);
+while (1) {
+   folder_base_name = g_path_get_basename(full_folder_name);
+
+   if (strcmp(folder_base_name, "cur") == 0
+   || strcmp(folder_base_name, "new") == 0) {
+   gchar *parent_name = g_path_get_dirname(full_folder_name);
+   g_free(full_folder_name);
+   full_folder_name = parent_name;
+   } else
+   break;
+}
+
+g_free(full_folder_name);
+
+if (strcmp(folder_base_name, ".") == 0) {
+   g_free(folder_base_name);
+   folder_base_name = NULL;
+}
+return folder_base_name;
+}
+
 /* Examine 'path' recursively as follows:
  *
  *   o Ask the filesystem for the mtime of 'path' (fs_mtime)
@@ -222,6 +252,7 @@ add_files_recursive (notmuch_database_t *notmuch,
 notmuch_filenames_t *db_subdirs = NULL;
 struct stat st;
 notmuch_bool_t is_maildir, new_directory;
+char *folder_base_name = NULL;

 if (stat (path, )) {
fprintf (stderr, "Error reading directory %s: %s\n",
@@ -407,7 +438,10 @@ add_files_recursive (notmuch_database_t *notmuch,
fflush (stdout);
}

-   status = notmuch_database_add_message (notmuch, next, );
+   folder_base_name = _get_folder_base_name(path);
+   status = notmuch_database_add_message (notmuch, next,
+  folder_base_name,
+  );
switch (status) {
/* success */
case NOTMUCH_STATUS_SUCCESS:
@@ -499,6 +533,8 @@ add_files_recursive (notmuch_database_t *notmuch,

[notmuch] [PATCH 2/2] Preserve folder information when indexing

2010-01-28 Thread micah anderson
On Thu, 28 Jan 2010 16:25:17 +0100, Michal Sojka  wrote:
> This patch was originally sent by Andreas Kl?ckner. This is a slightly
> reworked version (only coding style changes) which was manually rebased
> to the current HEAD.

Just a quick note about this patch: I received an offline email back