1 /* config.cc - API for database metadata
3 * Copyright © 2016 David Bremner
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.
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.
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/ .
18 * Author: David Bremner <david@tethera.net>
22 #include "notmuch-private.h"
23 #include "database-private.h"
25 static const std::string CONFIG_PREFIX = "C";
27 struct _notmuch_config_list {
28 notmuch_database_t *notmuch;
29 Xapian::TermIterator iterator;
34 static const char * _notmuch_config_key_to_string (notmuch_config_key_t key);
37 _notmuch_config_list_destroy (notmuch_config_list_t *list)
39 /* invoke destructor w/o deallocating memory */
40 list->iterator.~TermIterator();
45 notmuch_database_set_config (notmuch_database_t *notmuch,
49 notmuch_status_t status;
51 status = _notmuch_database_ensure_writable (notmuch);
55 if (! notmuch->config) {
56 if ((status = _notmuch_config_load_from_database (notmuch)))
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 ());
72 _notmuch_string_map_set (notmuch->config, key, value);
74 return NOTMUCH_STATUS_SUCCESS;
77 static notmuch_status_t
78 _metadata_value (notmuch_database_t *notmuch,
82 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
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 ());
96 notmuch_database_get_config (notmuch_database_t *notmuch,
100 const char* stored_val;
101 notmuch_status_t status;
103 if (! notmuch->config) {
104 if ((status = _notmuch_config_load_from_database (notmuch)))
109 return NOTMUCH_STATUS_NULL_POINTER;
111 stored_val = _notmuch_string_map_get (notmuch->config, key);
113 /* XXX in principle this API should be fixed so empty string
114 * is distinguished from not found */
117 *value = strdup (stored_val);
120 return NOTMUCH_STATUS_SUCCESS;
124 notmuch_database_get_config_list (notmuch_database_t *notmuch,
126 notmuch_config_list_t **out)
128 notmuch_config_list_t *list = NULL;
129 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
131 list = talloc (notmuch, notmuch_config_list_t);
133 status = NOTMUCH_STATUS_OUT_OF_MEMORY;
137 list->notmuch = notmuch;
138 list->current_key = NULL;
139 list->current_val = NULL;
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);
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;
160 if (status != NOTMUCH_STATUS_XAPIAN_EXCEPTION)
161 _notmuch_config_list_destroy (list);
164 talloc_set_destructor (list, _notmuch_config_list_destroy);
171 notmuch_config_list_valid (notmuch_config_list_t *metadata)
173 if (metadata->iterator == metadata->notmuch->xapian_db->metadata_keys_end ())
179 static inline char * _key_from_iterator (notmuch_config_list_t *list) {
180 return talloc_strdup (list, (*list->iterator).c_str () + CONFIG_PREFIX.length ());
184 notmuch_config_list_key (notmuch_config_list_t *list)
186 if (list->current_key)
187 talloc_free (list->current_key);
189 list->current_key = _key_from_iterator (list);
191 return list->current_key;
195 notmuch_config_list_value (notmuch_config_list_t *list)
198 notmuch_status_t status;
199 char *key = _key_from_iterator (list);
201 /* TODO: better error reporting?? */
202 status = _metadata_value (list->notmuch, key, strval);
206 if (list->current_val)
207 talloc_free (list->current_val);
209 list->current_val = talloc_strdup (list, strval.c_str ());
211 return list->current_val;
215 notmuch_config_list_move_to_next (notmuch_config_list_t *list)
221 notmuch_config_list_destroy (notmuch_config_list_t *list)
227 _notmuch_config_load_from_database (notmuch_database_t *notmuch)
229 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
230 notmuch_config_list_t *list;
232 if (notmuch->config == NULL)
233 notmuch->config = _notmuch_string_map_create (notmuch);
235 if (unlikely(notmuch->config == NULL))
236 return NOTMUCH_STATUS_OUT_OF_MEMORY;
238 status = notmuch_database_get_config_list (notmuch, "", &list);
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));
252 _notmuch_config_load_from_file (notmuch_database_t *notmuch,
255 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
256 gchar **groups, **keys, *val;
258 if (notmuch->config == NULL)
259 notmuch->config = _notmuch_string_map_create (notmuch);
261 if (unlikely(notmuch->config == NULL)) {
262 status = NOTMUCH_STATUS_OUT_OF_MEMORY;
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);
271 status = NOTMUCH_STATUS_FILE_ERROR;
274 _notmuch_string_map_set (notmuch->config, absolute_key, val);
275 talloc_free (absolute_key);
286 _notmuch_config_key_to_string (notmuch_config_key_t 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:
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:
308 _notmuch_config_default (void *ctx, notmuch_config_key_t key) {
312 case NOTMUCH_CONFIG_DATABASE_PATH:
313 path = getenv ("MAILDIR");
315 path = talloc_strdup (ctx, path);
317 path = talloc_asprintf (ctx, "%s/mail",
320 case NOTMUCH_CONFIG_EXCLUDE_TAGS:
322 case NOTMUCH_CONFIG_NEW_TAGS:
323 return "inbox;unread";
324 case NOTMUCH_CONFIG_SYNC_MAILDIR_FLAGS:
326 case NOTMUCH_CONFIG_USER_NAME:
327 case NOTMUCH_CONFIG_PRIMARY_EMAIL:
328 case NOTMUCH_CONFIG_OTHER_EMAIL:
331 case NOTMUCH_CONFIG_LAST:
332 INTERNAL_ERROR ("illegal key enum %d", key);
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);
345 val = _notmuch_string_map_get (notmuch->config, key_string);
347 _notmuch_string_map_set (notmuch->config, key_string, _notmuch_config_default (notmuch, key));
350 return NOTMUCH_STATUS_SUCCESS;
354 notmuch_config_get (notmuch_database_t *notmuch, notmuch_config_key_t key) {
356 return _notmuch_string_map_get (notmuch->config, _notmuch_config_key_to_string (key));
360 notmuch_config_set (notmuch_database_t *notmuch, notmuch_config_key_t key, const char *val) {
362 return notmuch_database_set_config (notmuch, _notmuch_config_key_to_string (key), val);