if (!ptr || !*ptr)
return;
- // struct NmAccountData *adata = *ptr;
+ struct NmAccountData *adata = *ptr;
+ if (adata->db)
+ {
+#ifdef NOTMUCH_API_3
+ notmuch_database_destroy(adata->db);
+#else
+ notmuch_database_close(adata->db);
+#endif
+ adata->db = NULL;
+ }
FREE(ptr);
}
return adata;
}
+/**
+ * nm_adata_get - Get the Notmuch Account data
+ * @param m Mailbox
+ * @retval ptr Success
+ * @retval NULL Failure, not a Notmuch mailbox
+ */
+static struct NmAccountData *nm_adata_get(struct Mailbox *m)
+{
+ if (!m || (m->magic != MUTT_NOTMUCH))
+ return NULL;
+
+ struct Account *a = m->account;
+ if (!a)
+ return NULL;
+
+ return a->adata;
+}
+
/**
* nm_mdata_free - Free data attached to the Mailbox
* @param ptr Notmuch data
struct NmMboxData *mdata = *ptr;
- if (mdata->db)
- {
-#ifdef NOTMUCH_API_3
- notmuch_database_destroy(mdata->db);
-#else
- notmuch_database_close(mdata->db);
-#endif
- mdata->db = NULL;
- }
-
url_free(&mdata->db_url);
FREE(&mdata->db_url_holder);
FREE(&mdata->db_query);
}
/**
- * nm_mdata_get - Get the Notmuch data
+ * nm_mdata_get - Get the Notmuch Mailbox data
* @param m Mailbox
* @retval ptr Success
* @retval NULL Failure, not a Notmuch mailbox
/**
* get_db_filename - Get the filename of the Notmuch database
- * @param mdata Notmuch Mailbox data
+ * @param m Notmuch Mailbox data
* @retval ptr Filename
*
* @note The return value is a pointer into the NmDefaultUri global variable.
* If that variable changes, the result will be invalid.
* It must not be freed.
*/
-static const char *get_db_filename(struct NmMboxData *mdata)
+static const char *get_db_filename(struct Mailbox *m)
{
+ struct NmMboxData *mdata = nm_mdata_get(m);
if (!mdata)
return NULL;
/**
* get_db - Get the Notmuch database
- * @param mdata Notmuch Mailbox data
+ * @param m Mailbox
* @param writable Read/write?
* @retval ptr Notmuch database
*/
-static notmuch_database_t *get_db(struct NmMboxData *mdata, bool writable)
+static notmuch_database_t *get_db(struct Mailbox *m, bool writable)
{
- if (!mdata)
+ if (!m || (m->magic != MUTT_NOTMUCH))
+ return NULL;
+ struct Account *a = m->account;
+ if (!a)
+ return NULL;
+ struct NmAccountData *adata = a->adata;
+ if (!adata)
return NULL;
- if (mdata->db)
- return mdata->db;
- const char *db_filename = get_db_filename(mdata);
+ const char *db_filename = get_db_filename(m);
if (db_filename)
- mdata->db = do_database_open(db_filename, writable, true);
+ adata->db = do_database_open(db_filename, writable, true);
- return mdata->db;
+ return adata->db;
}
/**
* release_db - Close the Notmuch database
- * @param mdata Notmuch Mailbox data
+ * @param m Mailbox
* @retval 0 Success
* @retval -1 Failure
*/
-static int release_db(struct NmMboxData *mdata)
+static int release_db(struct Mailbox *m)
{
- if (!mdata || !mdata->db)
+ struct NmAccountData *adata = nm_adata_get(m);
+ if (!adata || !adata->db)
return -1;
mutt_debug(1, "nm: db close\n");
#ifdef NOTMUCH_API_3
- notmuch_database_destroy(mdata->db);
+ notmuch_database_destroy(adata->db);
#else
- notmuch_database_close(mdata->db);
+ notmuch_database_close(adata->db);
#endif
- mdata->db = NULL;
- mdata->longrun = false;
+ adata->db = NULL;
+ adata->longrun = false;
return 0;
}
/**
* db_trans_begin - Start a Notmuch database transaction
- * @param mdata Notmuch Mailbox data
+ * @param m Mailbox
* @retval <0 error
* @retval 1 new transaction started
* @retval 0 already within transaction
*/
-static int db_trans_begin(struct NmMboxData *mdata)
+static int db_trans_begin(struct Mailbox *m)
{
- if (!mdata || !mdata->db)
+ struct NmAccountData *adata = nm_adata_get(m);
+ if (!adata || !adata->db)
return -1;
- if (mdata->trans)
+ if (adata->trans)
return 0;
mutt_debug(2, "nm: db trans start\n");
- if (notmuch_database_begin_atomic(mdata->db))
+ if (notmuch_database_begin_atomic(adata->db))
return -1;
- mdata->trans = true;
+ adata->trans = true;
return 1;
}
/**
* db_trans_end - End a database transaction
- * @param mdata Notmuch Mailbox data
+ * @param m Mailbox
* @retval 0 Success
* @retval -1 Failure
*/
-static int db_trans_end(struct NmMboxData *mdata)
+static int db_trans_end(struct Mailbox *m)
{
- if (!mdata || !mdata->db)
+ struct NmAccountData *adata = nm_adata_get(m);
+ if (!adata || !adata->db)
return -1;
- if (!mdata->trans)
+ if (!adata->trans)
return 0;
mutt_debug(2, "nm: db trans end\n");
- mdata->trans = false;
- if (notmuch_database_end_atomic(mdata->db))
+ adata->trans = false;
+ if (notmuch_database_end_atomic(adata->db))
return -1;
return 0;
/**
* is_longrun - Is Notmuch in the middle of a long-running transaction
- * @param mdata Notmuch Mailbox data
+ * @param m Notmuch Mailbox data
* @retval true if it is
*/
-static bool is_longrun(struct NmMboxData *mdata)
+static bool is_longrun(struct Mailbox *m)
{
- return mdata && mdata->longrun;
+ struct NmAccountData *adata = nm_adata_get(m);
+ if (!adata)
+ return false;
+
+ return adata->longrun;
}
/**
* get_database_mtime - Get the database modification time
- * @param[in] mdata Struct holding database info
+ * @param[in] m Mailbox
* @param[out] mtime Save the modification time
* @retval 0 Success (result in mtime)
* @retval -1 Error
* Get the "mtime" (modification time) of the database file.
* This is the time of the last update.
*/
-static int get_database_mtime(struct NmMboxData *mdata, time_t *mtime)
+static int get_database_mtime(struct Mailbox *m, time_t *mtime)
{
- if (!mdata)
+ if (!m || !mtime)
return -1;
char path[PATH_MAX];
- snprintf(path, sizeof(path), "%s/.notmuch/xapian", get_db_filename(mdata));
+ snprintf(path, sizeof(path), "%s/.notmuch/xapian", get_db_filename(m));
mutt_debug(2, "nm: checking '%s' mtime\n", path);
struct stat st;
if (stat(path, &st))
return -1;
- if (mtime)
- *mtime = st.st_mtime;
-
+ *mtime = st.st_mtime;
return 0;
}
/**
* get_query - Create a new query
- * @param mdata Notmuch Mailbox data
+ * @param m Mailbox
* @param writable Should the query be updateable?
* @retval ptr Notmuch query
* @retval NULL Error
*/
-static notmuch_query_t *get_query(struct NmMboxData *mdata, bool writable)
+static notmuch_query_t *get_query(struct Mailbox *m, bool writable)
{
+ struct NmMboxData *mdata = nm_mdata_get(m);
if (!mdata)
return NULL;
- notmuch_database_t *db = get_db(mdata, writable);
+ notmuch_database_t *db = get_db(m, writable);
const char *str = get_query_string(mdata, true);
if (!db || !str)
mutt_debug(2, "nm: query successfully initialized (%s)\n", str);
return q;
err:
- if (!is_longrun(mdata))
- release_db(mdata);
+ if (!is_longrun(m))
+ release_db(m);
return NULL;
}
/**
* remove_filename - Delete a file
- * @param mdata Notmuch Mailbox data
+ * @param m Mailbox
* @param path Path of file
* @retval 0 Success
* @retval -1 Failure
*/
-static int remove_filename(struct NmMboxData *mdata, const char *path)
+static int remove_filename(struct Mailbox *m, const char *path)
{
+ struct NmMboxData *mdata = nm_mdata_get(m);
+ if (!mdata)
+ return -1;
+
mutt_debug(2, "nm: remove filename '%s'\n", path);
- notmuch_database_t *db = get_db(mdata, true);
+ notmuch_database_t *db = get_db(m, true);
if (!db)
return -1;
if (st || !msg)
return -1;
- int trans = db_trans_begin(mdata);
+ int trans = db_trans_begin(m);
if (trans < 0)
return -1;
notmuch_message_destroy(msg);
if (trans)
- db_trans_end(mdata);
+ db_trans_end(m);
return 0;
}
/**
* rename_filename - Rename the file
- * @param mdata Notmuch Mailbox data
- * @param old Old filename
- * @param new New filename
- * @param e Email
+ * @param m Notmuch Mailbox data
+ * @param old Old filename
+ * @param new New filename
+ * @param e Email
* @retval 0 Success
* @retval -1 Failure
*/
-static int rename_filename(struct NmMboxData *mdata, const char *old,
- const char *new, struct Email *e)
+static int rename_filename(struct Mailbox *m, const char *old, const char *new,
+ struct Email *e)
{
- notmuch_database_t *db = get_db(mdata, true);
+ struct NmMboxData *mdata = nm_mdata_get(m);
+ if (!mdata)
+ return -1;
+
+ notmuch_database_t *db = get_db(m, true);
if (!db || !new || !old || (access(new, F_OK) != 0))
return -1;
notmuch_message_t *msg = NULL;
mutt_debug(1, "nm: rename filename, %s -> %s\n", old, new);
- int trans = db_trans_begin(mdata);
+ int trans = db_trans_begin(m);
if (trans < 0)
return -1;
if (msg)
notmuch_message_destroy(msg);
if (trans)
- db_trans_end(mdata);
+ db_trans_end(m);
return rc;
}
*/
void nm_longrun_init(struct Mailbox *m, bool writable)
{
- struct NmMboxData *mdata = nm_mdata_get(m);
+ struct NmAccountData *adata = nm_adata_get(m);
- if (mdata && get_db(mdata, writable))
+ if (adata && get_db(m, writable))
{
- mdata->longrun = true;
+ adata->longrun = true;
mutt_debug(2, "nm: long run initialized\n");
}
}
*/
void nm_longrun_done(struct Mailbox *m)
{
- struct NmMboxData *mdata = nm_mdata_get(m);
+ struct NmAccountData *adata = nm_adata_get(m);
- if (mdata && (release_db(mdata) == 0))
+ if (adata && (release_db(m) == 0))
mutt_debug(2, "nm: long run deinitialized\n");
}
*/
void nm_debug_check(struct Mailbox *m)
{
- struct NmMboxData *mdata = nm_mdata_get(m);
- if (!mdata)
+ struct NmAccountData *adata = nm_adata_get(m);
+ if (!adata || !adata->db)
return;
- if (mdata->db)
+ if (adata->db)
{
mutt_debug(1, "nm: ERROR: db is open, closing\n");
- release_db(mdata);
+ release_db(m);
}
}
notmuch_message_t *msg = NULL;
int rc = -1;
- if (!(db = get_db(mdata, false)) || !(msg = get_nm_message(db, e)))
+ if (!(db = get_db(ctx->mailbox, false)) || !(msg = get_nm_message(db, e)))
goto done;
mutt_debug(1, "nm: reading entire-thread messages...[current count=%d]\n",
done:
if (q)
notmuch_query_destroy(q);
- if (!is_longrun(mdata))
- release_db(mdata);
+ if (!is_longrun(ctx->mailbox))
+ release_db(ctx->mailbox);
if (ctx->mailbox->msg_count == mdata->oldmsgcount)
mutt_message(_("No more messages in the thread"));
if (get_limit(mdata) != NmDbLimit)
{
added = snprintf(uri, sizeof(uri),
- "notmuch://%s?type=%s&limit=%d&query=", get_db_filename(mdata),
+ "notmuch://%s?type=%s&limit=%d&query=", get_db_filename(m),
query_type_to_string(mdata->query_type), get_limit(mdata));
}
else
{
added = snprintf(uri, sizeof(uri),
- "notmuch://%s?type=%s&query=", get_db_filename(mdata),
+ "notmuch://%s?type=%s&query=", get_db_filename(m),
query_type_to_string(mdata->query_type));
}
}
bool nm_message_is_still_queried(struct Mailbox *m, struct Email *e)
{
struct NmMboxData *mdata = nm_mdata_get(m);
- notmuch_database_t *db = get_db(mdata, false);
+ notmuch_database_t *db = get_db(m, false);
char *orig_str = get_query_string(mdata, true);
if (!db || !orig_str)
old = buf;
}
- int rc = rename_filename(mdata, old, new, e);
+ int rc = rename_filename(m, old, new, e);
- if (!is_longrun(mdata))
- release_db(mdata);
+ if (!is_longrun(m))
+ release_db(m);
m->mtime.tv_sec = time(NULL);
m->mtime.tv_nsec = 0;
return rc;
if (!path || !mdata || (access(path, F_OK) != 0))
return 0;
- db = get_db(mdata, true);
+ db = get_db(m, true);
if (!db)
return -1;
mutt_debug(1, "nm: record message: %s\n", path);
- int trans = db_trans_begin(mdata);
+ int trans = db_trans_begin(m);
if (trans < 0)
goto done;
if (msg)
notmuch_message_destroy(msg);
if (trans == 1)
- db_trans_end(mdata);
- if (!is_longrun(mdata))
- release_db(mdata);
+ db_trans_end(m);
+ if (!is_longrun(m))
+ release_db(m);
return rc;
}
const char *tag = NULL;
int rc = -1;
- if (!(db = get_db(mdata, false)) || !(tags = notmuch_database_get_all_tags(db)))
+ if (!(db = get_db(m, false)) || !(tags = notmuch_database_get_all_tags(db)))
goto done;
*tag_count = 0;
if (tags)
notmuch_tags_destroy(tags);
- if (!is_longrun(mdata))
- release_db(mdata);
+ if (!is_longrun(m))
+ release_db(m);
mutt_debug(1, "nm: get all tags done [rc=%d tag_count=%u]\n", rc, *tag_count);
return rc;
mx_alloc_memory(ctx->mailbox);
}
- notmuch_query_t *q = get_query(mdata, false);
+ notmuch_query_t *q = get_query(ctx->mailbox, false);
if (q)
{
rc = 0;
notmuch_query_destroy(q);
}
- if (!is_longrun(mdata))
- release_db(mdata);
+ if (!is_longrun(ctx->mailbox))
+ release_db(ctx->mailbox);
ctx->mailbox->mtime.tv_sec = time(NULL);
ctx->mailbox->mtime.tv_nsec = 0;
{
struct NmMboxData *mdata = nm_mdata_get(ctx->mailbox);
time_t mtime = 0;
- if (!mdata || (get_database_mtime(mdata, &mtime) != 0))
+ if (!mdata || (get_database_mtime(ctx->mailbox, &mtime) != 0))
return -1;
int new_flags = 0;
mutt_debug(1, "nm: checking (db=%lu mailbox=%lu)\n", mtime, ctx->mailbox->mtime);
- notmuch_query_t *q = get_query(mdata, false);
+ notmuch_query_t *q = get_query(ctx->mailbox, false);
if (!q)
goto done;
if (q)
notmuch_query_destroy(q);
- if (!is_longrun(mdata))
- release_db(mdata);
+ if (!is_longrun(ctx->mailbox))
+ release_db(ctx->mailbox);
ctx->mailbox->mtime.tv_sec = time(NULL);
ctx->mailbox->mtime.tv_nsec = 0;
if (e->deleted || (strcmp(old, new) != 0))
{
- if (e->deleted && (remove_filename(mdata, old) == 0))
+ if (e->deleted && (remove_filename(ctx->mailbox, old) == 0))
changed = true;
- else if (*new &&*old && (rename_filename(mdata, old, new, e) == 0))
+ else if (*new &&*old && (rename_filename(ctx->mailbox, old, new, e) == 0))
changed = true;
}
mutt_str_strfcpy(ctx->mailbox->path, uri, sizeof(ctx->mailbox->path));
ctx->mailbox->magic = MUTT_NOTMUCH;
- if (!is_longrun(mdata))
- release_db(mdata);
+ if (!is_longrun(ctx->mailbox))
+ release_db(ctx->mailbox);
if (changed)
{
ctx->mailbox->mtime.tv_sec = time(NULL);
notmuch_message_t *msg = NULL;
int rc = -1;
- if (!(db = get_db(mdata, true)) || !(msg = get_nm_message(db, e)))
+ if (!(db = get_db(ctx->mailbox, true)) || !(msg = get_nm_message(db, e)))
goto done;
mutt_debug(1, "nm: tags modify: '%s'\n", buf);
rc = 0;
e->changed = true;
done:
- if (!is_longrun(mdata))
- release_db(mdata);
+ if (!is_longrun(ctx->mailbox))
+ release_db(ctx->mailbox);
if (e->changed)
{
ctx->mailbox->mtime.tv_sec = time(NULL);