]> git.notmuchmail.org Git - notmuch/blob - lib/config.cc
lib/open: load default values for known configuration keys.
[notmuch] / lib / config.cc
1 /* config.cc - API for database metadata
2  *
3  * Copyright © 2016 David Bremner
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see https://www.gnu.org/licenses/ .
17  *
18  * Author: David Bremner <david@tethera.net>
19  */
20
21 #include "notmuch.h"
22 #include "notmuch-private.h"
23 #include "database-private.h"
24
25 static const std::string CONFIG_PREFIX = "C";
26
27 struct _notmuch_config_list {
28     notmuch_database_t *notmuch;
29     Xapian::TermIterator iterator;
30     char *current_key;
31     char *current_val;
32 };
33
34 static const char * _notmuch_config_key_to_string (notmuch_config_key_t key);
35
36 static int
37 _notmuch_config_list_destroy (notmuch_config_list_t *list)
38 {
39     /* invoke destructor w/o deallocating memory */
40     list->iterator.~TermIterator();
41     return 0;
42 }
43
44 notmuch_status_t
45 notmuch_database_set_config (notmuch_database_t *notmuch,
46                              const char *key,
47                              const char *value)
48 {
49     notmuch_status_t status;
50
51     status = _notmuch_database_ensure_writable (notmuch);
52     if (status)
53         return status;
54
55     if (! notmuch->config) {
56         if ((status = _notmuch_config_load_from_database (notmuch)))
57             return status;
58     }
59
60     try {
61         notmuch->writable_xapian_db->set_metadata (CONFIG_PREFIX + key, value);
62     } catch (const Xapian::Error &error) {
63         status = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
64         notmuch->exception_reported = true;
65         _notmuch_database_log (notmuch, "Error: A Xapian exception occurred setting metadata: %s\n",
66                                error.get_msg ().c_str ());
67     }
68
69     if (status)
70         return status;
71
72     _notmuch_string_map_set (notmuch->config, key, value);
73
74     return NOTMUCH_STATUS_SUCCESS;
75 }
76
77 static notmuch_status_t
78 _metadata_value (notmuch_database_t *notmuch,
79                  const char *key,
80                  std::string &value)
81 {
82     notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
83
84     try {
85         value = notmuch->xapian_db->get_metadata (CONFIG_PREFIX + key);
86     } catch (const Xapian::Error &error) {
87         status = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
88         notmuch->exception_reported = true;
89         _notmuch_database_log (notmuch, "Error: A Xapian exception occurred getting metadata: %s\n",
90                                error.get_msg ().c_str ());
91     }
92     return status;
93 }
94
95 notmuch_status_t
96 notmuch_database_get_config (notmuch_database_t *notmuch,
97                              const char *key,
98                              char **value)
99 {
100     const char* stored_val;
101     notmuch_status_t status;
102
103     if (! notmuch->config) {
104         if ((status = _notmuch_config_load_from_database (notmuch)))
105             return status;
106     }
107
108     if (! value)
109         return NOTMUCH_STATUS_NULL_POINTER;
110
111     stored_val = _notmuch_string_map_get (notmuch->config, key);
112     if (! stored_val) {
113         /* XXX in principle this API should be fixed so empty string
114          * is distinguished from not found */
115         *value = strdup("");
116     } else {
117         *value = strdup (stored_val);
118     }
119
120     return NOTMUCH_STATUS_SUCCESS;
121 }
122
123 notmuch_status_t
124 notmuch_database_get_config_list (notmuch_database_t *notmuch,
125                                   const char *prefix,
126                                   notmuch_config_list_t **out)
127 {
128     notmuch_config_list_t *list = NULL;
129     notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
130
131     list = talloc (notmuch, notmuch_config_list_t);
132     if (! list) {
133         status = NOTMUCH_STATUS_OUT_OF_MEMORY;
134         goto DONE;
135     }
136
137     list->notmuch = notmuch;
138     list->current_key = NULL;
139     list->current_val = NULL;
140
141     try {
142
143         new(&(list->iterator)) Xapian::TermIterator (notmuch->xapian_db->metadata_keys_begin
144                                                          (CONFIG_PREFIX + (prefix ? prefix : "")));
145         talloc_set_destructor (list, _notmuch_config_list_destroy);
146
147     } catch (const Xapian::Error &error) {
148         _notmuch_database_log (notmuch, "A Xapian exception occurred getting metadata iterator: %s.\n",
149                                error.get_msg ().c_str ());
150         notmuch->exception_reported = true;
151         status = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
152     }
153
154     *out = list;
155
156   DONE:
157     if (status) {
158         if (list) {
159             talloc_free (list);
160             if (status != NOTMUCH_STATUS_XAPIAN_EXCEPTION)
161                 _notmuch_config_list_destroy (list);
162         }
163     }  else {
164         talloc_set_destructor (list, _notmuch_config_list_destroy);
165     }
166
167     return status;
168 }
169
170 notmuch_bool_t
171 notmuch_config_list_valid (notmuch_config_list_t *metadata)
172 {
173     if (metadata->iterator == metadata->notmuch->xapian_db->metadata_keys_end ())
174         return false;
175
176     return true;
177 }
178
179 static inline char * _key_from_iterator (notmuch_config_list_t *list) {
180     return talloc_strdup (list, (*list->iterator).c_str () + CONFIG_PREFIX.length ());
181 }
182
183 const char *
184 notmuch_config_list_key (notmuch_config_list_t *list)
185 {
186     if (list->current_key)
187         talloc_free (list->current_key);
188
189     list->current_key = _key_from_iterator (list);
190
191     return list->current_key;
192 }
193
194 const char *
195 notmuch_config_list_value (notmuch_config_list_t *list)
196 {
197     std::string strval;
198     notmuch_status_t status;
199     char *key = _key_from_iterator (list);
200
201     /* TODO: better error reporting?? */
202     status = _metadata_value (list->notmuch, key, strval);
203     if (status)
204         return NULL;
205
206     if (list->current_val)
207         talloc_free (list->current_val);
208
209     list->current_val = talloc_strdup (list, strval.c_str ());
210     talloc_free (key);
211     return list->current_val;
212 }
213
214 void
215 notmuch_config_list_move_to_next (notmuch_config_list_t *list)
216 {
217     list->iterator++;
218 }
219
220 void
221 notmuch_config_list_destroy (notmuch_config_list_t *list)
222 {
223     talloc_free (list);
224 }
225
226 notmuch_status_t
227 _notmuch_config_load_from_database (notmuch_database_t *notmuch)
228 {
229     notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
230     notmuch_config_list_t *list;
231
232     if (notmuch->config == NULL)
233         notmuch->config = _notmuch_string_map_create (notmuch);
234
235     if (unlikely(notmuch->config == NULL))
236         return NOTMUCH_STATUS_OUT_OF_MEMORY;
237
238     status = notmuch_database_get_config_list (notmuch, "", &list);
239     if (status)
240         return status;
241
242     for (; notmuch_config_list_valid (list); notmuch_config_list_move_to_next (list)) {
243         _notmuch_string_map_append (notmuch->config,
244                                     notmuch_config_list_key (list),
245                                     notmuch_config_list_value (list));
246     }
247
248     return status;
249 }
250
251 notmuch_status_t
252 _notmuch_config_load_from_file (notmuch_database_t *notmuch,
253                                 GKeyFile *file)
254 {
255     notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
256     gchar **groups, **keys, *val;
257
258     if (notmuch->config == NULL)
259         notmuch->config = _notmuch_string_map_create (notmuch);
260
261     if (unlikely(notmuch->config == NULL)) {
262         status = NOTMUCH_STATUS_OUT_OF_MEMORY;
263         goto DONE;
264     }
265
266     for (groups = g_key_file_get_groups (file, NULL); *groups; groups++) {
267         for (keys = g_key_file_get_keys (file, *groups, NULL, NULL); *keys; keys++) {
268             char *absolute_key = talloc_asprintf(notmuch, "%s.%s", *groups,  *keys);
269             val = g_key_file_get_value (file, *groups, *keys, NULL);
270             if (! val) {
271                 status = NOTMUCH_STATUS_FILE_ERROR;
272                 goto DONE;
273             }
274             _notmuch_string_map_set (notmuch->config, absolute_key, val);
275             talloc_free (absolute_key);
276             if (status)
277                 goto DONE;
278         }
279     }
280
281  DONE:
282     return status;
283 }
284
285 static const char *
286 _notmuch_config_key_to_string (notmuch_config_key_t key) {
287     switch (key) {
288     case NOTMUCH_CONFIG_DATABASE_PATH:
289         return "database.path";
290     case NOTMUCH_CONFIG_EXCLUDE_TAGS:
291         return "search.exclude_tags";
292     case NOTMUCH_CONFIG_NEW_TAGS:
293         return "new.tags";
294     case NOTMUCH_CONFIG_SYNC_MAILDIR_FLAGS:
295         return "maildir.synchronize_flags";
296     case NOTMUCH_CONFIG_PRIMARY_EMAIL:
297         return "user.primary_email";
298     case NOTMUCH_CONFIG_OTHER_EMAIL:
299         return "user.other_email";
300     case NOTMUCH_CONFIG_USER_NAME:
301         return "user.name";
302     default:
303         return NULL;
304     }
305 }
306
307 static const char *
308 _notmuch_config_default (void *ctx, notmuch_config_key_t key) {
309     char *path;
310
311     switch (key) {
312     case NOTMUCH_CONFIG_DATABASE_PATH:
313         path = getenv ("MAILDIR");
314         if (path)
315             path = talloc_strdup (ctx, path);
316         else
317             path = talloc_asprintf (ctx, "%s/mail",
318                                     getenv ("HOME"));
319         return path;
320     case NOTMUCH_CONFIG_EXCLUDE_TAGS:
321         return "";
322     case NOTMUCH_CONFIG_NEW_TAGS:
323         return "inbox;unread";
324     case NOTMUCH_CONFIG_SYNC_MAILDIR_FLAGS:
325         return "true";
326     case NOTMUCH_CONFIG_USER_NAME:
327     case NOTMUCH_CONFIG_PRIMARY_EMAIL:
328     case NOTMUCH_CONFIG_OTHER_EMAIL:
329         return NULL;
330     default:
331     case NOTMUCH_CONFIG_LAST:
332         INTERNAL_ERROR ("illegal key enum %d", key);
333    }
334 }
335
336 notmuch_status_t
337 _notmuch_config_load_defaults (notmuch_database_t *notmuch) {
338     notmuch_config_key_t key;
339     for (key = NOTMUCH_CONFIG_FIRST;
340          key < NOTMUCH_CONFIG_LAST;
341          key = notmuch_config_key_t(key + 1)) {
342         const char *val = notmuch_config_get (notmuch, key);
343         const char *key_string = _notmuch_config_key_to_string (key);
344
345         val = _notmuch_string_map_get (notmuch->config, key_string);
346         if (! val) {
347             _notmuch_string_map_set (notmuch->config, key_string, _notmuch_config_default (notmuch, key));
348         }
349     }
350     return NOTMUCH_STATUS_SUCCESS;
351 }
352
353 const char *
354 notmuch_config_get (notmuch_database_t *notmuch, notmuch_config_key_t key) {
355
356     return _notmuch_string_map_get (notmuch->config, _notmuch_config_key_to_string (key));
357 }
358
359 notmuch_status_t
360 notmuch_config_set (notmuch_database_t *notmuch, notmuch_config_key_t key, const char *val) {
361
362     return notmuch_database_set_config (notmuch, _notmuch_config_key_to_string (key), val);
363 }