1 /* message-property.cc - Properties are like tags, but (key,value) pairs.
2 * keys are allowed to repeat.
4 * This file is part of notmuch.
6 * Copyright © 2016 David Bremner
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see http://www.gnu.org/licenses/ .
21 * Author: David Bremner <david@tethera.net>
24 #include "notmuch-private.h"
25 #include "database-private.h"
26 #include "message-private.h"
29 notmuch_message_get_property (notmuch_message_t *message, const char *key, const char **value)
32 return NOTMUCH_STATUS_NULL_POINTER;
34 *value = _notmuch_string_map_get (_notmuch_message_property_map (message), key);
36 return NOTMUCH_STATUS_SUCCESS;
39 static notmuch_status_t
40 _notmuch_message_modify_property (notmuch_message_t *message, const char *key, const char *value,
41 notmuch_bool_t delete_it)
43 notmuch_private_status_t private_status;
44 notmuch_status_t status;
47 status = _notmuch_database_ensure_writable (_notmuch_message_database (message));
51 if (key == NULL || value == NULL)
52 return NOTMUCH_STATUS_NULL_POINTER;
55 return NOTMUCH_STATUS_ILLEGAL_ARGUMENT;
57 term = talloc_asprintf (message, "%s=%s", key, value);
60 private_status = _notmuch_message_remove_term (message, "property", term);
62 private_status = _notmuch_message_add_term (message, "property", term);
65 return COERCE_STATUS (private_status,
66 "Unhandled error modifying message property");
67 if (! _notmuch_message_frozen (message))
68 _notmuch_message_sync (message);
73 return NOTMUCH_STATUS_SUCCESS;
77 notmuch_message_add_property (notmuch_message_t *message, const char *key, const char *value)
79 return _notmuch_message_modify_property (message, key, value, FALSE);
83 notmuch_message_remove_property (notmuch_message_t *message, const char *key, const char *value)
85 return _notmuch_message_modify_property (message, key, value, TRUE);
89 notmuch_message_remove_all_properties (notmuch_message_t *message, const char *key)
91 notmuch_status_t status;
92 const char * term_prefix;
94 status = _notmuch_database_ensure_writable (_notmuch_message_database (message));
98 _notmuch_message_invalidate_metadata (message, "property");
100 term_prefix = talloc_asprintf (message, "%s%s=", _find_prefix ("property"), key);
102 term_prefix = _find_prefix ("property");
104 /* XXX better error reporting ? */
105 _notmuch_message_remove_terms (message, term_prefix);
107 return NOTMUCH_STATUS_SUCCESS;
110 notmuch_message_properties_t *
111 notmuch_message_get_properties (notmuch_message_t *message, const char *key, notmuch_bool_t exact)
113 notmuch_string_map_t *map;
114 map = _notmuch_message_property_map (message);
115 return _notmuch_string_map_iterator_create (map, key, exact);
119 notmuch_message_properties_valid (notmuch_message_properties_t *properties)
121 return _notmuch_string_map_iterator_valid (properties);
125 notmuch_message_properties_move_to_next (notmuch_message_properties_t *properties)
127 return _notmuch_string_map_iterator_move_to_next (properties);
131 notmuch_message_properties_key (notmuch_message_properties_t *properties)
133 return _notmuch_string_map_iterator_key (properties);
137 notmuch_message_properties_value (notmuch_message_properties_t *properties)
139 return _notmuch_string_map_iterator_value (properties);
143 notmuch_message_properties_destroy (notmuch_message_properties_t *properties)
145 _notmuch_string_map_iterator_destroy (properties);