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 {
39 static const char * _notmuch_config_key_to_string (notmuch_config_key_t key);
42 _notmuch_config_list_destroy (notmuch_config_list_t *list)
44 /* invoke destructor w/o deallocating memory */
45 list->iterator.~TermIterator();
50 notmuch_database_set_config (notmuch_database_t *notmuch,
54 notmuch_status_t status;
56 status = _notmuch_database_ensure_writable (notmuch);
60 if (! notmuch->config) {
61 if ((status = _notmuch_config_load_from_database (notmuch)))
66 notmuch->writable_xapian_db->set_metadata (CONFIG_PREFIX + key, value);
67 } catch (const Xapian::Error &error) {
68 status = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
69 notmuch->exception_reported = true;
70 _notmuch_database_log (notmuch, "Error: A Xapian exception occurred setting metadata: %s\n",
71 error.get_msg ().c_str ());
77 _notmuch_string_map_set (notmuch->config, key, value);
79 return NOTMUCH_STATUS_SUCCESS;
82 static notmuch_status_t
83 _metadata_value (notmuch_database_t *notmuch,
87 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
90 value = notmuch->xapian_db->get_metadata (CONFIG_PREFIX + key);
91 } catch (const Xapian::Error &error) {
92 status = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
93 notmuch->exception_reported = true;
94 _notmuch_database_log (notmuch, "Error: A Xapian exception occurred getting metadata: %s\n",
95 error.get_msg ().c_str ());
101 notmuch_database_get_config (notmuch_database_t *notmuch,
105 const char* stored_val;
106 notmuch_status_t status;
108 if (! notmuch->config) {
109 if ((status = _notmuch_config_load_from_database (notmuch)))
114 return NOTMUCH_STATUS_NULL_POINTER;
116 stored_val = _notmuch_string_map_get (notmuch->config, key);
118 /* XXX in principle this API should be fixed so empty string
119 * is distinguished from not found */
122 *value = strdup (stored_val);
125 return NOTMUCH_STATUS_SUCCESS;
129 notmuch_database_get_config_list (notmuch_database_t *notmuch,
131 notmuch_config_list_t **out)
133 notmuch_config_list_t *list = NULL;
134 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
136 list = talloc (notmuch, notmuch_config_list_t);
138 status = NOTMUCH_STATUS_OUT_OF_MEMORY;
142 list->notmuch = notmuch;
143 list->current_key = NULL;
144 list->current_val = NULL;
148 new(&(list->iterator)) Xapian::TermIterator (notmuch->xapian_db->metadata_keys_begin
149 (CONFIG_PREFIX + (prefix ? prefix : "")));
150 talloc_set_destructor (list, _notmuch_config_list_destroy);
152 } catch (const Xapian::Error &error) {
153 _notmuch_database_log (notmuch, "A Xapian exception occurred getting metadata iterator: %s.\n",
154 error.get_msg ().c_str ());
155 notmuch->exception_reported = true;
156 status = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
165 if (status != NOTMUCH_STATUS_XAPIAN_EXCEPTION)
166 _notmuch_config_list_destroy (list);
169 talloc_set_destructor (list, _notmuch_config_list_destroy);
176 notmuch_config_list_valid (notmuch_config_list_t *metadata)
178 if (metadata->iterator == metadata->notmuch->xapian_db->metadata_keys_end ())
184 static inline char * _key_from_iterator (notmuch_config_list_t *list) {
185 return talloc_strdup (list, (*list->iterator).c_str () + CONFIG_PREFIX.length ());
189 notmuch_config_list_key (notmuch_config_list_t *list)
191 if (list->current_key)
192 talloc_free (list->current_key);
194 list->current_key = _key_from_iterator (list);
196 return list->current_key;
200 notmuch_config_list_value (notmuch_config_list_t *list)
203 notmuch_status_t status;
204 char *key = _key_from_iterator (list);
206 /* TODO: better error reporting?? */
207 status = _metadata_value (list->notmuch, key, strval);
211 if (list->current_val)
212 talloc_free (list->current_val);
214 list->current_val = talloc_strdup (list, strval.c_str ());
216 return list->current_val;
220 notmuch_config_list_move_to_next (notmuch_config_list_t *list)
226 notmuch_config_list_destroy (notmuch_config_list_t *list)
232 _notmuch_config_load_from_database (notmuch_database_t *notmuch)
234 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
235 notmuch_config_list_t *list;
237 if (notmuch->config == NULL)
238 notmuch->config = _notmuch_string_map_create (notmuch);
240 if (unlikely(notmuch->config == NULL))
241 return NOTMUCH_STATUS_OUT_OF_MEMORY;
243 status = notmuch_database_get_config_list (notmuch, "", &list);
247 for (; notmuch_config_list_valid (list); notmuch_config_list_move_to_next (list)) {
248 _notmuch_string_map_append (notmuch->config,
249 notmuch_config_list_key (list),
250 notmuch_config_list_value (list));
256 notmuch_config_values_t *
257 notmuch_config_get_values (notmuch_database_t *notmuch, notmuch_config_key_t key)
259 notmuch_config_values_t *values;
262 const char *key_str = _notmuch_config_key_to_string (key);
267 str = _notmuch_string_map_get (notmuch->config, key_str);
271 values = talloc (notmuch, notmuch_config_values_t);
272 if (unlikely(! values))
275 values->iterator = strsplit_len (str, ';', &(values->tok_len));
280 notmuch_config_values_valid (notmuch_config_values_t *values) {
284 return (values->iterator != NULL);
288 notmuch_config_values_get (notmuch_config_values_t *values) {
289 return talloc_strndup (values, values->iterator, values->tok_len);
293 notmuch_config_values_move_to_next (notmuch_config_values_t *values) {
294 values->iterator += values->tok_len;
295 values->iterator = strsplit_len (values->iterator, ';', &(values->tok_len));
299 notmuch_config_values_destroy (notmuch_config_values_t *values) {
300 talloc_free (values);
304 _notmuch_config_load_from_file (notmuch_database_t *notmuch,
307 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
308 gchar **groups, **keys, *val;
310 if (notmuch->config == NULL)
311 notmuch->config = _notmuch_string_map_create (notmuch);
313 if (unlikely(notmuch->config == NULL)) {
314 status = NOTMUCH_STATUS_OUT_OF_MEMORY;
318 for (groups = g_key_file_get_groups (file, NULL); *groups; groups++) {
319 for (keys = g_key_file_get_keys (file, *groups, NULL, NULL); *keys; keys++) {
320 char *absolute_key = talloc_asprintf(notmuch, "%s.%s", *groups, *keys);
321 val = g_key_file_get_value (file, *groups, *keys, NULL);
323 status = NOTMUCH_STATUS_FILE_ERROR;
326 _notmuch_string_map_set (notmuch->config, absolute_key, val);
327 talloc_free (absolute_key);
338 _notmuch_config_key_to_string (notmuch_config_key_t key) {
340 case NOTMUCH_CONFIG_DATABASE_PATH:
341 return "database.path";
342 case NOTMUCH_CONFIG_EXCLUDE_TAGS:
343 return "search.exclude_tags";
344 case NOTMUCH_CONFIG_NEW_TAGS:
346 case NOTMUCH_CONFIG_SYNC_MAILDIR_FLAGS:
347 return "maildir.synchronize_flags";
348 case NOTMUCH_CONFIG_PRIMARY_EMAIL:
349 return "user.primary_email";
350 case NOTMUCH_CONFIG_OTHER_EMAIL:
351 return "user.other_email";
352 case NOTMUCH_CONFIG_USER_NAME:
360 _notmuch_config_default (void *ctx, notmuch_config_key_t key) {
364 case NOTMUCH_CONFIG_DATABASE_PATH:
365 path = getenv ("MAILDIR");
367 path = talloc_strdup (ctx, path);
369 path = talloc_asprintf (ctx, "%s/mail",
372 case NOTMUCH_CONFIG_EXCLUDE_TAGS:
374 case NOTMUCH_CONFIG_NEW_TAGS:
375 return "inbox;unread";
376 case NOTMUCH_CONFIG_SYNC_MAILDIR_FLAGS:
378 case NOTMUCH_CONFIG_USER_NAME:
379 case NOTMUCH_CONFIG_PRIMARY_EMAIL:
380 case NOTMUCH_CONFIG_OTHER_EMAIL:
383 case NOTMUCH_CONFIG_LAST:
384 INTERNAL_ERROR ("illegal key enum %d", key);
389 _notmuch_config_load_defaults (notmuch_database_t *notmuch) {
390 notmuch_config_key_t key;
391 for (key = NOTMUCH_CONFIG_FIRST;
392 key < NOTMUCH_CONFIG_LAST;
393 key = notmuch_config_key_t(key + 1)) {
394 const char *val = notmuch_config_get (notmuch, key);
395 const char *key_string = _notmuch_config_key_to_string (key);
397 val = _notmuch_string_map_get (notmuch->config, key_string);
399 _notmuch_string_map_set (notmuch->config, key_string, _notmuch_config_default (notmuch, key));
402 return NOTMUCH_STATUS_SUCCESS;
406 notmuch_config_get (notmuch_database_t *notmuch, notmuch_config_key_t key) {
408 return _notmuch_string_map_get (notmuch->config, _notmuch_config_key_to_string (key));
412 notmuch_config_set (notmuch_database_t *notmuch, notmuch_config_key_t key, const char *val) {
414 return notmuch_database_set_config (notmuch, _notmuch_config_key_to_string (key), val);