From b48e44f7c9e2c46c45f396e755a90967fc41e197 Mon Sep 17 00:00:00 2001 From: "Dirk-Jan C. Binnema" Date: Wed, 9 Dec 2009 19:53:30 +0200 Subject: [PATCH] * mu-config,mu-util.[ch]: try MAILDIR, they ~/Maildir as a guess for the maildir --- src/mu-config.c | 7 +++++-- src/mu-util.c | 36 ++++++++++++++++++++++++++++++++++++ src/mu-util.h | 9 +++++++++ 3 files changed, 50 insertions(+), 2 deletions(-) diff --git a/src/mu-config.c b/src/mu-config.c index b8030ebb..ed0d063c 100644 --- a/src/mu-config.c +++ b/src/mu-config.c @@ -105,6 +105,8 @@ mu_config_options_group_query (MuConfigOptions *opts) + + void mu_config_init (MuConfigOptions *opts) { @@ -120,8 +122,9 @@ mu_config_init (MuConfigOptions *opts) opts->log_append = TRUE; opts->log_stderr = FALSE; - /* indexing */ - opts->maildir = mu_util_dir_expand ("~/Maildir"); + /* indexing */ + opts->maildir = mu_util_guess_maildir (); + opts->cleanup = FALSE; opts->reindex = FALSE; diff --git a/src/mu-util.c b/src/mu-util.c index 5467aa5b..7e54eb41 100644 --- a/src/mu-util.c +++ b/src/mu-util.c @@ -23,6 +23,11 @@ #include #include +#include +#include +#include + + #include "mu-util.h" char* @@ -76,3 +81,34 @@ mu_util_strlist_free (GSList *lst) g_slist_foreach (lst, (GFunc)g_free, NULL); g_slist_free (lst); } + + +static gboolean +_is_readable_dir (const gchar* path) +{ + struct stat statbuf; + + return path && + access (path, F_OK) && + stat (path, &statbuf) == 0 && + S_ISDIR(statbuf.st_mode); +} + +gchar* +mu_util_guess_maildir (void) +{ + char *dir; + + /* first, try MAILDIR */ + dir = getenv ("MAILDIR"); + if (_is_readable_dir (dir)) + return g_strdup (dir); + + /* then, try ~/Maildir */ + dir = mu_util_dir_expand ("~/Maildir"); + if (_is_readable_dir (dir)) + return dir; + + /* nope; nothing found */ + return NULL; +} diff --git a/src/mu-util.h b/src/mu-util.h index 9c74406c..a6916d2f 100644 --- a/src/mu-util.h +++ b/src/mu-util.h @@ -35,6 +35,15 @@ G_BEGIN_DECLS char* mu_util_dir_expand (const char* path); + +/** + * guess the maildir; first try MAILDIR, then try ~/Maildir + * if both fail, return NULL + * + * @return full path of the guessed Maildir, or NULL; must be freed (gfree) + */ +char* mu_util_guess_maildir (void); + /** * take a char*[] and turn it into a GSList *