1 /* dbus-asv-util.c - utility functions for a{sv}
3 * Copyright © 2011-2012 Nokia Corporation
4 * Copyright © 2012-2013 Collabora Ltd.
6 * Licensed under the Academic Free License version 2.1
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 2 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, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
26 #include <dbus/dbus.h>
28 #include "dbus/dbus-asv-util.h"
31 * Convenience function to create a method-call reply whose type is a{sv}
32 * (map from string to variant).
34 * Append values with 0 or more sequences of _dbus_asv_open_entry(),
35 * appending a value to var_iter, and _dbus_asv_close_entry(),
36 * then close the a{sv} with _dbus_asv_close() or _dbus_asv_abandon().
38 * This must be paired with a call to _dbus_asv_close() or _dbus_asv_abandon().
40 * @param message a method call message
41 * @param iter an iterator which will be initialized to append to the message
42 * @param arr_iter an iterator which will be initialized to append to the array
43 * @returns a new message, or #NULL if not enough memory
46 _dbus_asv_new_method_return (DBusMessage *message,
47 DBusMessageIter *iter,
48 DBusMessageIter *arr_iter)
50 DBusMessage *reply = dbus_message_new_method_return (message);
55 dbus_message_iter_init_append (reply, iter);
57 if (!dbus_message_iter_open_container (iter, DBUS_TYPE_ARRAY, "{sv}",
60 dbus_message_unref (reply);
68 * Open a new entry in an a{sv} (map from string to variant).
70 * This must be paired with a call to either _dbus_asv_close_entry()
71 * or _dbus_asv_abandon_entry().
73 * If this function fails, the a{sv} must be abandoned, for instance
74 * with _dbus_asv_abandon().
76 * @param arr_iter the iterator which is appending to the array
77 * @param entry_iter will be initialized to append to the dict-entry
78 * @param key a UTF-8 key for the map
79 * @param type the type of the variant value, e.g. DBUS_TYPE_STRING_AS_STRING
80 * @param var_iter will be initialized to append (i.e. write) to the variant
81 * @returns #TRUE on success, or #FALSE if not enough memory
84 _dbus_asv_open_entry (DBusMessageIter *arr_iter,
85 DBusMessageIter *entry_iter,
88 DBusMessageIter *var_iter)
90 if (!dbus_message_iter_open_container (arr_iter, DBUS_TYPE_DICT_ENTRY,
94 if (!dbus_message_iter_append_basic (entry_iter, DBUS_TYPE_STRING, &key))
96 dbus_message_iter_abandon_container (arr_iter, entry_iter);
100 if (!dbus_message_iter_open_container (entry_iter, DBUS_TYPE_VARIANT,
103 dbus_message_iter_abandon_container (arr_iter, entry_iter);
111 * Closes an a{sv} entry after successfully appending the value.
113 * If this function fails, the a{sv} must be abandoned, for instance
114 * with _dbus_asv_abandon().
116 * @param arr_iter the iterator which is appending to the array
117 * @param entry_iter the iterator appending to the dict-entry, will be closed
118 * @param var_iter the iterator appending to the variant, will be closed
119 * @returns #TRUE on success, or #FALSE if not enough memory
122 _dbus_asv_close_entry (DBusMessageIter *arr_iter,
123 DBusMessageIter *entry_iter,
124 DBusMessageIter *var_iter)
126 if (!dbus_message_iter_close_container (entry_iter, var_iter))
128 dbus_message_iter_abandon_container (arr_iter, entry_iter);
132 if (!dbus_message_iter_close_container (arr_iter, entry_iter))
139 * Closes an a{sv} after successfully appending all values.
141 * If this function fails, you must abandon iter and whatever
142 * larger data structure (message, etc.) the a{sv} was embedded in.
144 * @param iter the iterator which is appending to the message or other data structure containing the a{sv}
145 * @param arr_iter the iterator appending to the array, will be closed
146 * @returns #TRUE on success, or #FALSE if not enough memory
149 _dbus_asv_close (DBusMessageIter *iter,
150 DBusMessageIter *arr_iter)
152 return dbus_message_iter_close_container (iter, arr_iter);
156 * Closes an a{sv} entry after unsuccessfully appending a value.
157 * You must also abandon the a{sv} itself (for instance with
158 * _dbus_asv_abandon()), and abandon whatever larger data structure
159 * the a{sv} was embedded in.
161 * @param iter the iterator which is appending to the message or other data structure containing the a{sv}
162 * @param arr_iter the iterator appending to the array, will be closed
163 * @returns #TRUE on success, or #FALSE if not enough memory
166 _dbus_asv_abandon_entry (DBusMessageIter *arr_iter,
167 DBusMessageIter *entry_iter,
168 DBusMessageIter *var_iter)
170 dbus_message_iter_abandon_container (entry_iter, var_iter);
171 dbus_message_iter_abandon_container (arr_iter, entry_iter);
175 * Closes an a{sv} after unsuccessfully appending a value.
177 * You must also abandon whatever larger data structure (message, etc.)
178 * the a{sv} was embedded in.
180 * @param iter the iterator which is appending to the message or other data structure containing the a{sv}
181 * @param arr_iter the iterator appending to the array, will be closed
184 _dbus_asv_abandon (DBusMessageIter *iter,
185 DBusMessageIter *arr_iter)
187 dbus_message_iter_abandon_container (iter, arr_iter);
191 * Create a new entry in an a{sv} (map from string to variant)
192 * with a 32-bit unsigned integer value.
194 * If this function fails, the a{sv} must be abandoned, for instance
195 * with _dbus_asv_abandon().
197 * @param arr_iter the iterator which is appending to the array
198 * @param key a UTF-8 key for the map
199 * @param value the value
200 * @returns #TRUE on success, or #FALSE if not enough memory
203 _dbus_asv_add_uint32 (DBusMessageIter *arr_iter,
207 DBusMessageIter entry_iter, var_iter;
209 if (!_dbus_asv_open_entry (arr_iter, &entry_iter, key,
210 DBUS_TYPE_UINT32_AS_STRING, &var_iter))
213 if (!dbus_message_iter_append_basic (&var_iter, DBUS_TYPE_UINT32,
216 _dbus_asv_abandon_entry (arr_iter, &entry_iter, &var_iter);
220 if (!_dbus_asv_close_entry (arr_iter, &entry_iter, &var_iter))
227 * Create a new entry in an a{sv} (map from string to variant)
228 * with a UTF-8 string value.
230 * If this function fails, the a{sv} must be abandoned, for instance
231 * with _dbus_asv_abandon().
233 * @param arr_iter the iterator which is appending to the array
234 * @param key a UTF-8 key for the map
235 * @param value the value
236 * @returns #TRUE on success, or #FALSE if not enough memory
239 _dbus_asv_add_string (DBusMessageIter *arr_iter,
243 DBusMessageIter entry_iter, var_iter;
245 if (!_dbus_asv_open_entry (arr_iter, &entry_iter, key,
246 DBUS_TYPE_STRING_AS_STRING, &var_iter))
249 if (!dbus_message_iter_append_basic (&var_iter, DBUS_TYPE_STRING,
252 _dbus_asv_abandon_entry (arr_iter, &entry_iter, &var_iter);
256 if (!_dbus_asv_close_entry (arr_iter, &entry_iter, &var_iter))