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 struct _notmuch_config_values {
38 void *children; /* talloc_context */
41 static const char * _notmuch_config_key_to_string (notmuch_config_key_t key);
44 _notmuch_config_list_destroy (notmuch_config_list_t *list)
46 /* invoke destructor w/o deallocating memory */
47 list->iterator.~TermIterator();
52 notmuch_database_set_config (notmuch_database_t *notmuch,
56 notmuch_status_t status;
58 status = _notmuch_database_ensure_writable (notmuch);
62 if (! notmuch->config) {
63 if ((status = _notmuch_config_load_from_database (notmuch)))
68 notmuch->writable_xapian_db->set_metadata (CONFIG_PREFIX + key, value);
69 } catch (const Xapian::Error &error) {
70 status = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
71 notmuch->exception_reported = true;
72 _notmuch_database_log (notmuch, "Error: A Xapian exception occurred setting metadata: %s\n",
73 error.get_msg ().c_str ());
79 _notmuch_string_map_set (notmuch->config, key, value);
81 return NOTMUCH_STATUS_SUCCESS;
84 static notmuch_status_t
85 _metadata_value (notmuch_database_t *notmuch,
89 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
92 value = notmuch->xapian_db->get_metadata (CONFIG_PREFIX + key);
93 } catch (const Xapian::Error &error) {
94 status = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
95 notmuch->exception_reported = true;
96 _notmuch_database_log (notmuch, "Error: A Xapian exception occurred getting metadata: %s\n",
97 error.get_msg ().c_str ());
103 notmuch_database_get_config (notmuch_database_t *notmuch,
107 const char* stored_val;
108 notmuch_status_t status;
110 if (! notmuch->config) {
111 if ((status = _notmuch_config_load_from_database (notmuch)))
116 return NOTMUCH_STATUS_NULL_POINTER;
118 stored_val = _notmuch_string_map_get (notmuch->config, key);
120 /* XXX in principle this API should be fixed so empty string
121 * is distinguished from not found */
124 *value = strdup (stored_val);
127 return NOTMUCH_STATUS_SUCCESS;
131 notmuch_database_get_config_list (notmuch_database_t *notmuch,
133 notmuch_config_list_t **out)
135 notmuch_config_list_t *list = NULL;
136 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
138 list = talloc (notmuch, notmuch_config_list_t);
140 status = NOTMUCH_STATUS_OUT_OF_MEMORY;
144 list->notmuch = notmuch;
145 list->current_key = NULL;
146 list->current_val = NULL;
150 new(&(list->iterator)) Xapian::TermIterator (notmuch->xapian_db->metadata_keys_begin
151 (CONFIG_PREFIX + (prefix ? prefix : "")));
152 talloc_set_destructor (list, _notmuch_config_list_destroy);
154 } catch (const Xapian::Error &error) {
155 _notmuch_database_log (notmuch, "A Xapian exception occurred getting metadata iterator: %s.\n",
156 error.get_msg ().c_str ());
157 notmuch->exception_reported = true;
158 status = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
167 if (status != NOTMUCH_STATUS_XAPIAN_EXCEPTION)
168 _notmuch_config_list_destroy (list);
171 talloc_set_destructor (list, _notmuch_config_list_destroy);
178 notmuch_config_list_valid (notmuch_config_list_t *metadata)
180 if (metadata->iterator == metadata->notmuch->xapian_db->metadata_keys_end ())
186 static inline char * _key_from_iterator (notmuch_config_list_t *list) {
187 return talloc_strdup (list, (*list->iterator).c_str () + CONFIG_PREFIX.length ());
191 notmuch_config_list_key (notmuch_config_list_t *list)
193 if (list->current_key)
194 talloc_free (list->current_key);
196 list->current_key = _key_from_iterator (list);
198 return list->current_key;
202 notmuch_config_list_value (notmuch_config_list_t *list)
205 notmuch_status_t status;
206 char *key = _key_from_iterator (list);
208 /* TODO: better error reporting?? */
209 status = _metadata_value (list->notmuch, key, strval);
213 if (list->current_val)
214 talloc_free (list->current_val);
216 list->current_val = talloc_strdup (list, strval.c_str ());
218 return list->current_val;
222 notmuch_config_list_move_to_next (notmuch_config_list_t *list)
228 notmuch_config_list_destroy (notmuch_config_list_t *list)
234 _notmuch_config_load_from_database (notmuch_database_t *notmuch)
236 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
237 notmuch_config_list_t *list;
239 if (notmuch->config == NULL)
240 notmuch->config = _notmuch_string_map_create (notmuch);
242 if (unlikely(notmuch->config == NULL))
243 return NOTMUCH_STATUS_OUT_OF_MEMORY;
245 status = notmuch_database_get_config_list (notmuch, "", &list);
249 for (; notmuch_config_list_valid (list); notmuch_config_list_move_to_next (list)) {
250 _notmuch_string_map_append (notmuch->config,
251 notmuch_config_list_key (list),
252 notmuch_config_list_value (list));
258 notmuch_config_values_t *
259 notmuch_config_get_values (notmuch_database_t *notmuch, notmuch_config_key_t key)
261 notmuch_config_values_t *values = NULL;
264 const char *key_str = _notmuch_config_key_to_string (key);
269 values = talloc (notmuch, notmuch_config_values_t);
270 if (unlikely(! values))
273 values->children = talloc_new (values);
275 values->string = _notmuch_string_map_get (notmuch->config, key_str);
276 if (! values->string)
279 values->iterator = strsplit_len (values->string, ';', &(values->tok_len));
292 notmuch_config_values_valid (notmuch_config_values_t *values) {
296 return (values->iterator != NULL);
300 notmuch_config_values_get (notmuch_config_values_t *values) {
301 return talloc_strndup (values, values->iterator, values->tok_len);
305 notmuch_config_values_start (notmuch_config_values_t *values) {
308 if (values->children) {
309 talloc_free (values->children);
312 values->children = talloc_new (values);
314 values->iterator = strsplit_len (values->string, ';', &(values->tok_len));
318 notmuch_config_values_move_to_next (notmuch_config_values_t *values) {
319 values->iterator += values->tok_len;
320 values->iterator = strsplit_len (values->iterator, ';', &(values->tok_len));
324 notmuch_config_values_destroy (notmuch_config_values_t *values) {
325 talloc_free (values);
329 _notmuch_config_load_from_file (notmuch_database_t *notmuch,
332 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
333 gchar **groups, **keys, *val;
335 if (notmuch->config == NULL)
336 notmuch->config = _notmuch_string_map_create (notmuch);
338 if (unlikely(notmuch->config == NULL)) {
339 status = NOTMUCH_STATUS_OUT_OF_MEMORY;
343 for (groups = g_key_file_get_groups (file, NULL); *groups; groups++) {
344 for (keys = g_key_file_get_keys (file, *groups, NULL, NULL); *keys; keys++) {
345 char *absolute_key = talloc_asprintf(notmuch, "%s.%s", *groups, *keys);
346 val = g_key_file_get_value (file, *groups, *keys, NULL);
348 status = NOTMUCH_STATUS_FILE_ERROR;
351 _notmuch_string_map_set (notmuch->config, absolute_key, val);
352 talloc_free (absolute_key);
363 notmuch_config_get_bool (notmuch_database_t *notmuch, notmuch_config_key_t key, notmuch_bool_t *val)
365 const char *key_string, *val_string;
367 key_string = _notmuch_config_key_to_string (key);
369 return NOTMUCH_STATUS_ILLEGAL_ARGUMENT;
372 val_string = _notmuch_string_map_get (notmuch->config, key_string);
375 return NOTMUCH_STATUS_SUCCESS;
378 if (strcase_equal (val_string, "false") || strcase_equal (val_string, "no"))
380 else if (strcase_equal (val_string, "true") || strcase_equal (val_string, "yes"))
383 return NOTMUCH_STATUS_ILLEGAL_ARGUMENT;
385 return NOTMUCH_STATUS_SUCCESS;
389 _notmuch_config_key_to_string (notmuch_config_key_t key) {
391 case NOTMUCH_CONFIG_DATABASE_PATH:
392 return "database.path";
393 case NOTMUCH_CONFIG_EXCLUDE_TAGS:
394 return "search.exclude_tags";
395 case NOTMUCH_CONFIG_NEW_TAGS:
397 case NOTMUCH_CONFIG_SYNC_MAILDIR_FLAGS:
398 return "maildir.synchronize_flags";
399 case NOTMUCH_CONFIG_PRIMARY_EMAIL:
400 return "user.primary_email";
401 case NOTMUCH_CONFIG_OTHER_EMAIL:
402 return "user.other_email";
403 case NOTMUCH_CONFIG_USER_NAME:
411 _notmuch_config_default (void *ctx, notmuch_config_key_t key) {
415 case NOTMUCH_CONFIG_DATABASE_PATH:
416 path = getenv ("MAILDIR");
418 path = talloc_strdup (ctx, path);
420 path = talloc_asprintf (ctx, "%s/mail",
423 case NOTMUCH_CONFIG_EXCLUDE_TAGS:
425 case NOTMUCH_CONFIG_NEW_TAGS:
426 return "inbox;unread";
427 case NOTMUCH_CONFIG_SYNC_MAILDIR_FLAGS:
429 case NOTMUCH_CONFIG_USER_NAME:
430 case NOTMUCH_CONFIG_PRIMARY_EMAIL:
431 case NOTMUCH_CONFIG_OTHER_EMAIL:
434 case NOTMUCH_CONFIG_LAST:
435 INTERNAL_ERROR ("illegal key enum %d", key);
440 _notmuch_config_load_defaults (notmuch_database_t *notmuch) {
441 notmuch_config_key_t key;
442 for (key = NOTMUCH_CONFIG_FIRST;
443 key < NOTMUCH_CONFIG_LAST;
444 key = notmuch_config_key_t(key + 1)) {
445 const char *val = notmuch_config_get (notmuch, key);
446 const char *key_string = _notmuch_config_key_to_string (key);
448 val = _notmuch_string_map_get (notmuch->config, key_string);
450 _notmuch_string_map_set (notmuch->config, key_string, _notmuch_config_default (notmuch, key));
453 return NOTMUCH_STATUS_SUCCESS;
457 notmuch_config_get (notmuch_database_t *notmuch, notmuch_config_key_t key) {
459 return _notmuch_string_map_get (notmuch->config, _notmuch_config_key_to_string (key));
463 notmuch_config_set (notmuch_database_t *notmuch, notmuch_config_key_t key, const char *val) {
465 return notmuch_database_set_config (notmuch, _notmuch_config_key_to_string (key), val);