NeoMutt  2021-02-05-89-gabe350
Teaching an old dog new tricks
DOXYGEN
private.h File Reference
#include <notmuch.h>
#include <stdbool.h>
#include <time.h>
#include "core/lib.h"
#include "progress.h"
+ Include dependency graph for private.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define LIBNOTMUCH_CHECK_VERSION(major, minor, micro)
 

Enumerations

enum  NmQueryType { NM_QUERY_TYPE_MESGS = 1, NM_QUERY_TYPE_THREADS }
 Notmuch Query Types. More...
 

Functions

notmuch_database_t * nm_db_do_open (const char *filename, bool writable, bool verbose)
 Open a Notmuch database. More...
 
void nm_db_free (notmuch_database_t *db)
 decoupled way to close a Notmuch database More...
 
const char * nm_db_get_filename (struct Mailbox *m)
 Get the filename of the Notmuch database. More...
 
int nm_db_get_mtime (struct Mailbox *m, time_t *mtime)
 Get the database modification time. More...
 
notmuch_database_t * nm_db_get (struct Mailbox *m, bool writable)
 Get the Notmuch database. More...
 
bool nm_db_is_longrun (struct Mailbox *m)
 Is Notmuch in the middle of a long-running transaction. More...
 
int nm_db_release (struct Mailbox *m)
 Close the Notmuch database. More...
 
int nm_db_trans_begin (struct Mailbox *m)
 Start a Notmuch database transaction. More...
 
int nm_db_trans_end (struct Mailbox *m)
 End a database transaction. More...
 
enum NmQueryType string_to_query_type (const char *str)
 Lookup a query type. More...
 

Variables

const int NmUrlProtocolLen
 
int C_NmDbLimit
 Config: (notmuch) Default limit for Notmuch queries. More...
 
char * C_NmDefaultUrl
 Config: (notmuch) Path to the Notmuch database. More...
 
char * C_NmExcludeTags
 Config: (notmuch) Exclude messages with these tags. More...
 
char * C_NmFlaggedTag
 Config: (notmuch) Tag to use for flagged messages. More...
 
int C_NmOpenTimeout
 Config: (notmuch) Database timeout. More...
 
char * C_NmQueryType
 Config: (notmuch) Default query type: 'threads' or 'messages'. More...
 
int C_NmQueryWindowCurrentPosition
 Config: (notmuch) Position of current search window. More...
 
char * C_NmQueryWindowTimebase
 Config: (notmuch) Units for the time duration. More...
 
char * C_NmRecordTags
 Config: (notmuch) Tags to apply to the 'record' mailbox (sent mail) More...
 
char * C_NmRepliedTag
 Config: (notmuch) Tag to use for replied messages. More...
 
char * C_NmUnreadTag
 Config: (notmuch) Tag to use for unread messages. More...
 

Detailed Description

Notmuch private types

Authors
  • Richard Russon

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

Definition in file private.h.

Macro Definition Documentation

◆ LIBNOTMUCH_CHECK_VERSION

#define LIBNOTMUCH_CHECK_VERSION (   major,
  minor,
  micro 
)
Value:
(LIBNOTMUCH_MAJOR_VERSION > (major) || \
(LIBNOTMUCH_MAJOR_VERSION == (major) && LIBNOTMUCH_MINOR_VERSION > (minor)) || \
(LIBNOTMUCH_MAJOR_VERSION == (major) && \
LIBNOTMUCH_MINOR_VERSION == (minor) && LIBNOTMUCH_MICRO_VERSION >= (micro)))

Definition at line 37 of file private.h.

Enumeration Type Documentation

◆ NmQueryType

Notmuch Query Types.

Read whole-thread or matching messages only?

Enumerator
NM_QUERY_TYPE_MESGS 

Default: Messages only.

NM_QUERY_TYPE_THREADS 

Whole threads.

Definition at line 50 of file private.h.

51 {
54 };

Function Documentation

◆ nm_db_do_open()

notmuch_database_t* nm_db_do_open ( const char *  filename,
bool  writable,
bool  verbose 
)

Open a Notmuch database.

Parameters
filenameDatabase filename
writableRead/write?
verboseShow errors on failure?
Return values
ptrNotmuch database

Definition at line 85 of file db.c.

86 {
87  notmuch_database_t *db = NULL;
88  int ct = 0;
89  notmuch_status_t st = NOTMUCH_STATUS_SUCCESS;
90 #if LIBNOTMUCH_CHECK_VERSION(4, 2, 0)
91  char *msg = NULL;
92 #endif
93 
94  mutt_debug(LL_DEBUG1, "nm: db open '%s' %s (timeout %d)\n", filename,
95  writable ? "[WRITE]" : "[READ]", C_NmOpenTimeout);
96 
97  const notmuch_database_mode_t mode =
98  writable ? NOTMUCH_DATABASE_MODE_READ_WRITE : NOTMUCH_DATABASE_MODE_READ_ONLY;
99 
100  do
101  {
102 #if LIBNOTMUCH_CHECK_VERSION(4, 2, 0)
103  st = notmuch_database_open_verbose(filename, mode, &db, &msg);
104 #elif defined(NOTMUCH_API_3)
105  st = notmuch_database_open(filename, mode, &db);
106 #else
107  db = notmuch_database_open(filename, mode);
108 #endif
109  if ((st == NOTMUCH_STATUS_FILE_ERROR) || db || !C_NmOpenTimeout || ((ct / 2) > C_NmOpenTimeout))
110  break;
111 
112  if (verbose && ct && ((ct % 2) == 0))
113  mutt_error(_("Waiting for notmuch DB... (%d sec)"), ct / 2);
114  mutt_date_sleep_ms(500000); /* Half a second */
115  ct++;
116  } while (true);
117 
118  if (verbose)
119  {
120  if (!db)
121  {
122 #if LIBNOTMUCH_CHECK_VERSION(4, 2, 0)
123  if (msg)
124  {
125  mutt_error(msg);
126  FREE(&msg);
127  }
128  else
129 #endif
130  {
131  mutt_error(_("Can't open notmuch database: %s: %s"), filename,
132  st ? notmuch_status_to_string(st) : _("unknown reason"));
133  }
134  }
135  else if (ct > 1)
136  {
138  }
139  }
140  return db;
141 }
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ nm_db_free()

void nm_db_free ( notmuch_database_t *  db)

decoupled way to close a Notmuch database

Parameters
dbNotmuch database

Definition at line 190 of file db.c.

191 {
192 #ifdef NOTMUCH_API_3
193  notmuch_database_destroy(db);
194 #else
195  notmuch_database_close(db);
196 #endif
197 }
+ Here is the caller graph for this function:

◆ nm_db_get_filename()

const char* nm_db_get_filename ( struct Mailbox m)

Get the filename of the Notmuch database.

Parameters
mMailbox
Return values
ptrFilename
Note
The return value is a pointer into the $nm_default_url global variable. If that variable changes, the result will be invalid. It must not be freed.

Definition at line 55 of file db.c.

56 {
57  struct NmMboxData *mdata = nm_mdata_get(m);
58  char *db_filename = NULL;
59 
60  if (mdata && mdata->db_url && mdata->db_url->path)
61  db_filename = mdata->db_url->path;
62  else
63  db_filename = C_NmDefaultUrl;
64 
65  if (!db_filename && !C_Folder)
66  return NULL;
67 
68  if (!db_filename)
69  db_filename = C_Folder;
70 
71  if (nm_path_probe(db_filename, NULL) == MUTT_NOTMUCH)
72  db_filename += NmUrlProtocolLen;
73 
74  mutt_debug(LL_DEBUG2, "nm: db filename '%s'\n", db_filename);
75  return db_filename;
76 }
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ nm_db_get_mtime()

int nm_db_get_mtime ( struct Mailbox m,
time_t *  mtime 
)

Get the database modification time.

Parameters
[in]mMailbox
[out]mtimeSave the modification time
Return values
0Success (result in mtime)
-1Error

Get the "mtime" (modification time) of the database file. This is the time of the last update.

Definition at line 255 of file db.c.

256 {
257  if (!m || !mtime)
258  return -1;
259 
260  char path[PATH_MAX];
261  snprintf(path, sizeof(path), "%s/.notmuch/xapian", nm_db_get_filename(m));
262  mutt_debug(LL_DEBUG2, "nm: checking '%s' mtime\n", path);
263 
264  struct stat st;
265  if (stat(path, &st) != 0)
266  return -1;
267 
268  *mtime = st.st_mtime;
269  return 0;
270 }
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ nm_db_get()

notmuch_database_t* nm_db_get ( struct Mailbox m,
bool  writable 
)

Get the Notmuch database.

Parameters
mMailbox
writableRead/write?
Return values
ptrNotmuch database

Definition at line 149 of file db.c.

150 {
151  struct NmAccountData *adata = nm_adata_get(m);
152 
153  if (!adata)
154  return NULL;
155 
156  // Use an existing open db if we have one.
157  if (adata->db)
158  return adata->db;
159 
160  const char *db_filename = nm_db_get_filename(m);
161  if (db_filename)
162  adata->db = nm_db_do_open(db_filename, writable, true);
163 
164  return adata->db;
165 }
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ nm_db_is_longrun()

bool nm_db_is_longrun ( struct Mailbox m)

Is Notmuch in the middle of a long-running transaction.

Parameters
mMailbox
Return values
trueif it is

Definition at line 277 of file db.c.

278 {
279  struct NmAccountData *adata = nm_adata_get(m);
280  if (!adata)
281  return false;
282 
283  return adata->longrun;
284 }
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ nm_db_release()

int nm_db_release ( struct Mailbox m)

Close the Notmuch database.

Parameters
mMailbox
Return values
0Success
-1Failure

Definition at line 173 of file db.c.

174 {
175  struct NmAccountData *adata = nm_adata_get(m);
176  if (!adata || !adata->db || nm_db_is_longrun(m))
177  return -1;
178 
179  mutt_debug(LL_DEBUG1, "nm: db close\n");
180  nm_db_free(adata->db);
181  adata->db = NULL;
182  adata->longrun = false;
183  return 0;
184 }
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ nm_db_trans_begin()

int nm_db_trans_begin ( struct Mailbox m)

Start a Notmuch database transaction.

Parameters
mMailbox
Return values
<0error
1new transaction started
0already within transaction

Definition at line 206 of file db.c.

207 {
208  struct NmAccountData *adata = nm_adata_get(m);
209  if (!adata || !adata->db)
210  return -1;
211 
212  if (adata->trans)
213  return 0;
214 
215  mutt_debug(LL_DEBUG2, "nm: db trans start\n");
216  if (notmuch_database_begin_atomic(adata->db))
217  return -1;
218  adata->trans = true;
219  return 1;
220 }
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ nm_db_trans_end()

int nm_db_trans_end ( struct Mailbox m)

End a database transaction.

Parameters
mMailbox
Return values
0Success
-1Failure

Definition at line 228 of file db.c.

229 {
230  struct NmAccountData *adata = nm_adata_get(m);
231  if (!adata || !adata->db)
232  return -1;
233 
234  if (!adata->trans)
235  return 0;
236 
237  mutt_debug(LL_DEBUG2, "nm: db trans end\n");
238  adata->trans = false;
239  if (notmuch_database_end_atomic(adata->db))
240  return -1;
241 
242  return 0;
243 }
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ string_to_query_type()

enum NmQueryType string_to_query_type ( const char *  str)

Lookup a query type.

Parameters
strString to lookup
Return values
numQuery type, e.g. NM_QUERY_TYPE_MESGS

Definition at line 126 of file notmuch.c.

127 {
128  if (mutt_str_equal(str, "threads"))
129  return NM_QUERY_TYPE_THREADS;
130  if (mutt_str_equal(str, "messages"))
131  return NM_QUERY_TYPE_MESGS;
132 
133  mutt_error(_("failed to parse notmuch query type: %s"), NONULL(str));
134  return NM_QUERY_TYPE_MESGS;
135 }
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Variable Documentation

◆ NmUrlProtocolLen

const int NmUrlProtocolLen

Definition at line 86 of file notmuch.c.

◆ C_NmDbLimit

int C_NmDbLimit

Config: (notmuch) Default limit for Notmuch queries.

Definition at line 37 of file config.c.

◆ C_NmDefaultUrl

char* C_NmDefaultUrl

Config: (notmuch) Path to the Notmuch database.

Definition at line 38 of file config.c.

◆ C_NmExcludeTags

char* C_NmExcludeTags

Config: (notmuch) Exclude messages with these tags.

Definition at line 39 of file config.c.

◆ C_NmFlaggedTag

char* C_NmFlaggedTag

Config: (notmuch) Tag to use for flagged messages.

Definition at line 40 of file config.c.

◆ C_NmOpenTimeout

int C_NmOpenTimeout

Config: (notmuch) Database timeout.

Definition at line 41 of file config.c.

◆ C_NmQueryType

char* C_NmQueryType

Config: (notmuch) Default query type: 'threads' or 'messages'.

Definition at line 42 of file config.c.

◆ C_NmQueryWindowCurrentPosition

int C_NmQueryWindowCurrentPosition

Config: (notmuch) Position of current search window.

Definition at line 43 of file config.c.

◆ C_NmQueryWindowTimebase

char* C_NmQueryWindowTimebase

Config: (notmuch) Units for the time duration.

Definition at line 46 of file config.c.

◆ C_NmRecordTags

char* C_NmRecordTags

Config: (notmuch) Tags to apply to the 'record' mailbox (sent mail)

Definition at line 47 of file config.c.

◆ C_NmRepliedTag

char* C_NmRepliedTag

Config: (notmuch) Tag to use for replied messages.

Definition at line 48 of file config.c.

◆ C_NmUnreadTag

char* C_NmUnreadTag

Config: (notmuch) Tag to use for unread messages.

Definition at line 49 of file config.c.

_
#define _(a)
Definition: message.h:28
NONULL
#define NONULL(x)
Definition: string2.h:37
nm_db_get_filename
const char * nm_db_get_filename(struct Mailbox *m)
Get the filename of the Notmuch database.
Definition: db.c:55
LL_DEBUG1
@ LL_DEBUG1
Log at debug level 1.
Definition: logging.h:40
FREE
#define FREE(x)
Definition: memory.h:40
nm_db_do_open
notmuch_database_t * nm_db_do_open(const char *filename, bool writable, bool verbose)
Open a Notmuch database.
Definition: db.c:85
PATH_MAX
#define PATH_MAX
Definition: mutt.h:44
nm_mdata_get
struct NmMboxData * nm_mdata_get(struct Mailbox *m)
Get the Notmuch Mailbox data.
Definition: mdata.c:89
nm_adata_get
struct NmAccountData * nm_adata_get(struct Mailbox *m)
Get the Notmuch Account data.
Definition: adata.c:69
mutt_str_equal
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:871
Mailbox::mdata
void * mdata
Driver specific data.
Definition: mailbox.h:136
C_Folder
WHERE char * C_Folder
Config: Base folder for a set of mailboxes.
Definition: mutt_globals.h:96
mutt_clear_error
void mutt_clear_error(void)
Clear the message line (bottom line of screen)
Definition: mutt_logging.c:113
Mailbox::mtime
struct timespec mtime
Time Mailbox was last changed.
Definition: mailbox.h:107
NmAccountData
Notmuch-specific Account data -.
Definition: adata.h:34
MUTT_NOTMUCH
@ MUTT_NOTMUCH
'Notmuch' (virtual) Mailbox type
Definition: mailbox.h:54
mutt_date_sleep_ms
void mutt_date_sleep_ms(size_t ms)
Sleep for milliseconds.
Definition: date.c:693
NM_QUERY_TYPE_THREADS
@ NM_QUERY_TYPE_THREADS
Whole threads.
Definition: private.h:53
nm_path_probe
enum MailboxType nm_path_probe(const char *path, const struct stat *st)
Is this a Notmuch Mailbox? - Implements MxOps::path_probe()
Definition: notmuch.c:2522
C_NmOpenTimeout
int C_NmOpenTimeout
Config: (notmuch) Database timeout.
Definition: config.c:41
mutt_debug
#define mutt_debug(LEVEL,...)
Definition: logging.h:81
nm_db_free
void nm_db_free(notmuch_database_t *db)
decoupled way to close a Notmuch database
Definition: db.c:190
NM_QUERY_TYPE_MESGS
@ NM_QUERY_TYPE_MESGS
Default: Messages only.
Definition: private.h:52
Account::adata
void * adata
Private data (for Mailbox backends)
Definition: account.h:43
NmUrlProtocolLen
const int NmUrlProtocolLen
Definition: notmuch.c:86
C_NmDefaultUrl
char * C_NmDefaultUrl
Config: (notmuch) Path to the Notmuch database.
Definition: config.c:38
NmMboxData
Notmuch-specific Mailbox data -.
Definition: mdata.h:35
nm_db_is_longrun
bool nm_db_is_longrun(struct Mailbox *m)
Is Notmuch in the middle of a long-running transaction.
Definition: db.c:277
LL_DEBUG2
@ LL_DEBUG2
Log at debug level 2.
Definition: logging.h:41
mutt_error
#define mutt_error(...)
Definition: logging.h:84