From 717ffcc1e00231580d56397c57d270f4fd205a6e Mon Sep 17 00:00:00 2001 From: Johannes Rosenberger Date: Sat, 29 Jun 2024 18:06:05 +0200 Subject: [PATCH] let notmuch search config file if undefined by astroid --- src/config.cc | 7 ------- src/db.cc | 37 +++++++++++++++++++++---------------- src/db.hh | 2 +- 3 files changed, 22 insertions(+), 24 deletions(-) diff --git a/src/config.cc b/src/config.cc index 14f27bbc..6715f8bf 100644 --- a/src/config.cc +++ b/src/config.cc @@ -161,13 +161,6 @@ namespace Astroid { ptree default_config; default_config.put ("astroid.config.version", CONFIG_VERSION); - std::string nm_cfg = path(std_paths.home / path (".notmuch-config")).string(); - char* nm_env = getenv("NOTMUCH_CONFIG"); - if (nm_env != NULL) { - nm_cfg.assign(nm_env, strlen(nm_env)); - } - default_config.put ("astroid.notmuch_config" , nm_cfg); - default_config.put ("astroid.debug.dryrun_sending", false); /* only show hints with a level higher than this */ diff --git a/src/db.cc b/src/db.cc index 5e2e128a..3c97d452 100644 --- a/src/db.cc +++ b/src/db.cc @@ -37,10 +37,11 @@ namespace Astroid { std::vector Db::tags; bfs::path Db::path_db; - bfs::path Db::path_config; + const char * Db::path_config; void Db::init () { - path_config = astroid->notmuch_config (); + // NULL: notmuch searches according to notmuch-config(1) / notmuch.h:notmuch_database_open_with_config() + path_config = astroid->has_notmuch_config () ? astroid->notmuch_config().c_str() : NULL; const char * home = getenv ("HOME"); if (home == NULL) { @@ -48,26 +49,30 @@ namespace Astroid { throw invalid_argument ("db: error: HOME environment variable not set."); } - if (!astroid->has_notmuch_config ()) { - throw database_error ("db: error: no notmuch config file found."); - } - notmuch_database_t *dbh = NULL; char *error_message = NULL; - notmuch_status_t s = notmuch_database_load_config (NULL, - path_config.c_str (), - NULL, &dbh, &error_message); + // NULL: notmuch searches according to notmuch-config(1) / notmuch.h:notmuch_database_open_with_config() + notmuch_status_t s = notmuch_database_load_config (NULL, path_config, NULL, &dbh, &error_message); if (error_message != NULL) { LOG (error) << "db: " << error_message; free (error_message); + } - if (s == NOTMUCH_STATUS_NO_DATABASE) { - notmuch_database_destroy (dbh); - throw database_error ("db: error: no database path specified"); + if (s != NOTMUCH_STATUS_SUCCESS) { + notmuch_database_destroy (dbh); + const char *db_err_str; + switch (s) { + case NOTMUCH_STATUS_NO_DATABASE: + db_err_str = "db: error: no database path specified"; + break; + case NOTMUCH_STATUS_NO_CONFIG: + db_err_str = "db: error: no notmuch config file found."; + break; + default: + db_err_str = "db: error: could not open notmuch config file"; } - - throw database_error ("db: error: could not open notmuch config file"); + throw database_error (db_err_str); } const char *db_path = notmuch_config_get (dbh, NOTMUCH_CONFIG_DATABASE_PATH); @@ -133,7 +138,7 @@ namespace Astroid { s = notmuch_database_open_with_config ( path_db.c_str(), notmuch_database_mode_t::NOTMUCH_DATABASE_MODE_READ_WRITE, - path_config.c_str(), + path_config, NULL, &nm_db, &error_message); if (error_message != NULL) { @@ -178,7 +183,7 @@ namespace Astroid { s = notmuch_database_open_with_config ( path_db.c_str(), notmuch_database_mode_t::NOTMUCH_DATABASE_MODE_READ_ONLY, - path_config.c_str(), + path_config, NULL, &nm_db, &error_message); if (error_message != NULL) { diff --git a/src/db.hh b/src/db.hh index 8ee62767..425062d0 100644 --- a/src/db.hh +++ b/src/db.hh @@ -150,7 +150,7 @@ namespace Astroid { static bool maildir_synchronize_flags; static void init (); static bfs::path path_db; - static bfs::path path_config; + static const char * path_config; private: /*