Skip to content

Commit

Permalink
let notmuch search config file if undefined by astroid
Browse files Browse the repository at this point in the history
  • Loading branch information
jorsn committed Jun 29, 2024
1 parent cf84e3f commit 717ffcc
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 24 deletions.
7 changes: 0 additions & 7 deletions src/config.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
37 changes: 21 additions & 16 deletions src/db.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,37 +37,42 @@ namespace Astroid {
std::vector<ustring> 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) {
LOG (error) << "db: error: HOME variable not set.";
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);
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/db.hh
Original file line number Diff line number Diff line change
Expand Up @@ -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:
/*
Expand Down

0 comments on commit 717ffcc

Please sign in to comment.