Show number of new mails in Maildir mailboxes.
Reuse the st_size value for this purpose.
This fixes bug 2421.

v2:
- fix stale values in local new variable

Signed-off-by: Olaf Hering <o...@aepfle.de>
---
 browser.c | 35 ++++++++++++++++++++++++++++++++++-
 1 file changed, 34 insertions(+), 1 deletion(-)

diff --git a/browser.c b/browser.c
index ef9783f..3f0fedd 100644
--- a/browser.c
+++ b/browser.c
@@ -372,6 +372,25 @@ static void init_state (struct browser_state *state, 
MUTTMENU *menu)
     menu->data = state->entry;
 }
 
+static unsigned int walk_maildir_new(const char *path)
+{
+  struct dirent *de;
+  DIR *d = opendir(path);
+  unsigned int count = 0;
+
+  if (!d)
+    return count;
+
+  while ((de = readdir(d))) {
+    if (de->d_name[0] == '.' && (!de->d_name[1] || (de->d_name[1] == '.' && 
!de->d_name[2])))
+     continue;
+    count++;
+  }
+  closedir(d);
+
+  return count;
+}
+
 static int examine_directory (MUTTMENU *menu, struct browser_state *state,
                              char *d, const char *prefix)
 {
@@ -380,6 +399,7 @@ static int examine_directory (MUTTMENU *menu, struct 
browser_state *state,
   struct dirent *de;
   char buffer[_POSIX_PATH_MAX + SHORT_STRING];
   BUFFY *tmp;
+  unsigned int new;
 
   while (stat (d, &s) == -1)
   {
@@ -425,6 +445,17 @@ static int examine_directory (MUTTMENU *menu, struct 
browser_state *state,
       continue;
 
     mutt_concat_path (buffer, d, de->d_name, sizeof (buffer));
+    if (mx_is_maildir (buffer))
+    {
+      size_t remaining = sizeof(buffer) - strlen(buffer);
+      if (snprintf (buffer + strlen(buffer), remaining, "/new") >= remaining)
+        continue;
+      if (lstat (buffer, &s) < 0)
+        continue;
+      s.st_size = new = walk_maildir_new(buffer);
+    }
+    else
+    {
     if (lstat (buffer, &s) == -1)
       continue;
     
@@ -437,7 +468,9 @@ static int examine_directory (MUTTMENU *menu, struct 
browser_state *state,
     tmp = Incoming;
     while (tmp && mutt_strcmp (buffer, tmp->path))
       tmp = tmp->next;
-    add_folder (menu, state, de->d_name, &s, (tmp) ? tmp->new : 0);
+    new = tmp ? tmp->new : 0;
+    }
+    add_folder (menu, state, de->d_name, &s, new);
   }
   closedir (dp);  
   browser_sort (state);

Reply via email to