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;
35 _notmuch_config_list_destroy (notmuch_config_list_t *list)
37 /* invoke destructor w/o deallocating memory */
38 list->iterator.~TermIterator();
43 notmuch_database_set_config (notmuch_database_t *notmuch,
47 notmuch_status_t status;
49 status = _notmuch_database_ensure_writable (notmuch);
54 notmuch->writable_xapian_db->set_metadata (CONFIG_PREFIX + key, value);
55 } catch (const Xapian::Error &error) {
56 status = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
57 notmuch->exception_reported = true;
58 _notmuch_database_log (notmuch, "Error: A Xapian exception occurred setting metadata: %s\n",
59 error.get_msg ().c_str ());
64 static notmuch_status_t
65 _metadata_value (notmuch_database_t *notmuch,
69 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
72 value = notmuch->xapian_db->get_metadata (CONFIG_PREFIX + key);
73 } catch (const Xapian::Error &error) {
74 status = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
75 notmuch->exception_reported = true;
76 _notmuch_database_log (notmuch, "Error: A Xapian exception occurred getting metadata: %s\n",
77 error.get_msg ().c_str ());
83 notmuch_database_get_config (notmuch_database_t *notmuch,
88 notmuch_status_t status;
91 return NOTMUCH_STATUS_NULL_POINTER;
93 status = _metadata_value (notmuch, key, strval);
97 *value = strdup (strval.c_str ());
99 return NOTMUCH_STATUS_SUCCESS;
103 notmuch_database_get_config_list (notmuch_database_t *notmuch,
105 notmuch_config_list_t **out)
107 notmuch_config_list_t *list = NULL;
108 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
110 list = talloc (notmuch, notmuch_config_list_t);
112 status = NOTMUCH_STATUS_OUT_OF_MEMORY;
116 list->notmuch = notmuch;
117 list->current_key = NULL;
118 list->current_val = NULL;
122 new(&(list->iterator)) Xapian::TermIterator (notmuch->xapian_db->metadata_keys_begin
123 (CONFIG_PREFIX + (prefix ? prefix : "")));
124 talloc_set_destructor (list, _notmuch_config_list_destroy);
126 } catch (const Xapian::Error &error) {
127 _notmuch_database_log (notmuch, "A Xapian exception occurred getting metadata iterator: %s.\n",
128 error.get_msg ().c_str ());
129 notmuch->exception_reported = true;
130 status = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
143 notmuch_config_list_valid (notmuch_config_list_t *metadata)
145 if (metadata->iterator == metadata->notmuch->xapian_db->metadata_keys_end ())
151 static inline char * _key_from_iterator (notmuch_config_list_t *list) {
152 return talloc_strdup (list, (*list->iterator).c_str () + CONFIG_PREFIX.length ());
156 notmuch_config_list_key (notmuch_config_list_t *list)
158 if (list->current_key)
159 talloc_free (list->current_key);
161 list->current_key = _key_from_iterator (list);
163 return list->current_key;
167 notmuch_config_list_value (notmuch_config_list_t *list)
170 notmuch_status_t status;
171 char *key = _key_from_iterator (list);
173 /* TODO: better error reporting?? */
174 status = _metadata_value (list->notmuch, key, strval);
178 if (list->current_val)
179 talloc_free (list->current_val);
181 list->current_val = talloc_strdup (list, strval.c_str ());
183 return list->current_val;
187 notmuch_config_list_move_to_next (notmuch_config_list_t *list)
193 notmuch_config_list_destroy (notmuch_config_list_t *list)