]> git.notmuchmail.org Git - notmuch/blob - lib/config.cc
lib: provide config API
[notmuch] / lib / config.cc
1 /* config.cc - API for database metadata
2  *
3  * Copyright © 2016 David Bremner
4  *
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.
9  *
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.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see http://www.gnu.org/licenses/ .
17  *
18  * Author: David Bremner <david@tethera.net>
19  */
20
21 #include "notmuch.h"
22 #include "notmuch-private.h"
23 #include "database-private.h"
24
25 static const std::string CONFIG_PREFIX = "C";
26
27 notmuch_status_t
28 notmuch_database_set_config (notmuch_database_t *notmuch,
29                              const char *key,
30                              const char *value)
31 {
32     notmuch_status_t status;
33     Xapian::WritableDatabase *db;
34
35     status = _notmuch_database_ensure_writable (notmuch);
36     if (status)
37         return status;
38
39     try {
40         db = static_cast <Xapian::WritableDatabase *> (notmuch->xapian_db);
41         db->set_metadata (CONFIG_PREFIX + key, value);
42     } catch (const Xapian::Error &error) {
43         status = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
44         notmuch->exception_reported = TRUE;
45         _notmuch_database_log (notmuch, "Error: A Xapian exception occurred setting metadata: %s\n",
46                                error.get_msg().c_str());
47     }
48     return NOTMUCH_STATUS_SUCCESS;
49 }
50
51 static notmuch_status_t
52 _metadata_value (notmuch_database_t *notmuch,
53                  const char *key,
54                  std::string &value)
55 {
56     notmuch_status_t status = NOTMUCH_STATUS_SUCCESS;
57
58     try {
59         value = notmuch->xapian_db->get_metadata (CONFIG_PREFIX + key);
60     } catch (const Xapian::Error &error) {
61         status = NOTMUCH_STATUS_XAPIAN_EXCEPTION;
62         notmuch->exception_reported = TRUE;
63         _notmuch_database_log (notmuch, "Error: A Xapian exception occurred getting metadata: %s\n",
64                                error.get_msg().c_str());
65     }
66     return status;
67 }
68
69 notmuch_status_t
70 notmuch_database_get_config (notmuch_database_t *notmuch,
71                              const char *key,
72                              char **value)
73 {
74     std::string strval;
75     notmuch_status_t status;
76
77     if (! value)
78         return NOTMUCH_STATUS_NULL_POINTER;
79
80     status = _metadata_value (notmuch, key, strval);
81     if (status)
82         return status;
83
84     *value = strdup (strval.c_str ());
85
86     return NOTMUCH_STATUS_SUCCESS;
87 }