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;
48 Xapian::WritableDatabase *db;
50 status = _notmuch_database_ensure_writable (notmuch);
55 db = static_cast <Xapian::WritableDatabase *> (notmuch->xapian_db);
56 db->set_metadata (CONFIG_PREFIX + key, value);
57 } catch (const Xapian::Error &error) {
58 status = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
59 notmuch->exception_reported = true;
60 _notmuch_database_log (notmuch, "Error: A Xapian exception occurred setting metadata: %s\n",
61 error.get_msg().c_str());
63 return NOTMUCH_STATUS_SUCCESS;
66 static notmuch_status_t
67 _metadata_value (notmuch_database_t *notmuch,
71 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
74 value = notmuch->xapian_db->get_metadata (CONFIG_PREFIX + key);
75 } catch (const Xapian::Error &error) {
76 status = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
77 notmuch->exception_reported = true;
78 _notmuch_database_log (notmuch, "Error: A Xapian exception occurred getting metadata: %s\n",
79 error.get_msg().c_str());
85 notmuch_database_get_config (notmuch_database_t *notmuch,
90 notmuch_status_t status;
93 return NOTMUCH_STATUS_NULL_POINTER;
95 status = _metadata_value (notmuch, key, strval);
99 *value = strdup (strval.c_str ());
101 return NOTMUCH_STATUS_SUCCESS;
105 notmuch_database_get_config_list (notmuch_database_t *notmuch,
107 notmuch_config_list_t **out)
109 notmuch_config_list_t *list = NULL;
110 notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
112 list = talloc (notmuch, notmuch_config_list_t);
114 status = NOTMUCH_STATUS_OUT_OF_MEMORY;
118 talloc_set_destructor (list, _notmuch_config_list_destroy);
119 list->notmuch = notmuch;
120 list->current_key = NULL;
121 list->current_val = NULL;
125 new(&(list->iterator)) Xapian::TermIterator (notmuch->xapian_db->metadata_keys_begin
126 (CONFIG_PREFIX + (prefix ? prefix : "")));
128 } catch (const Xapian::Error &error) {
129 _notmuch_database_log (notmuch, "A Xapian exception occurred getting metadata iterator: %s.\n",
130 error.get_msg().c_str());
131 notmuch->exception_reported = true;
132 status = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
145 notmuch_config_list_valid (notmuch_config_list_t *metadata)
147 if (metadata->iterator == metadata->notmuch->xapian_db->metadata_keys_end ())
154 notmuch_config_list_key (notmuch_config_list_t *list)
156 if (list->current_key)
157 talloc_free (list->current_key);
159 list->current_key = talloc_strdup (list, (*list->iterator).c_str () + CONFIG_PREFIX.length ());
161 return list->current_key;
165 notmuch_config_list_value (notmuch_config_list_t *list)
168 notmuch_status_t status;
169 const char *key = notmuch_config_list_key (list);
171 /* TODO: better error reporting?? */
172 status = _metadata_value (list->notmuch, key, strval);
176 if (list->current_val)
177 talloc_free (list->current_val);
179 list->current_val = talloc_strdup (list, strval.c_str ());
180 return list->current_val;
184 notmuch_config_list_move_to_next (notmuch_config_list_t *list)
190 notmuch_config_list_destroy (notmuch_config_list_t *list)