Debian Codegraph @

src/polkit/polkitdetails.c

raw
1 /*
2 * Copyright (C) 2008 Red Hat, Inc.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General
15 * Public License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
17 * Boston, MA 02111-1307, USA.
18 *
19 * Author: David Zeuthen <[email protected]>
20 */
21
22 #include <string.h>
23 #include "polkitimplicitauthorization.h"
24 #include "polkitdetails.h"
25
26 #include "polkitprivate.h"
27
28 /**
29 * SECTION:polkitdetails
30 * @title: PolkitDetails
31 * @short_description: Object used for passing details
32 * @stability: Stable
33 *
34 * An object used for passing details around.
35 */
36
37 /**
38 * PolkitDetails:
39 *
40 * The #PolkitDetails struct should not be accessed directly.
41 */
42 struct _PolkitDetails
43 {
44 GObject parent_instance;
45
46 GHashTable *hash;
47 };
48
49 struct _PolkitDetailsClass
50 {
51 GObjectClass parent_class;
52 };
53
54 G_DEFINE_TYPE (PolkitDetails, polkit_details, G_TYPE_OBJECT);
55
56 static void
57 polkit_details_init (PolkitDetails *details)
58 {
59 }
60
61 static void
62 polkit_details_finalize (GObject *object)
63 {
64 PolkitDetails *details;
65
66 details = POLKIT_DETAILS (object);
67
68 if (details->hash != NULL)
69 g_hash_table_unref (details->hash);
70
71 if (G_OBJECT_CLASS (polkit_details_parent_class)->finalize != NULL)
72 G_OBJECT_CLASS (polkit_details_parent_class)->finalize (object);
73 }
74
75 static void
76 polkit_details_class_init (PolkitDetailsClass *klass)
77 {
78 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
79
80 gobject_class->finalize = polkit_details_finalize;
81 }
82
83 /**
84 * polkit_details_new:
85 *
86 * Creates a new #PolkitDetails object.
87 *
88 * Returns: A #PolkitDetails object. Free with g_object_unref().
89 */
90 PolkitDetails *
91 polkit_details_new (void)
92 {
93 PolkitDetails *details;
94
95 details = POLKIT_DETAILS (g_object_new (POLKIT_TYPE_DETAILS, NULL));
96
97 return details;
98 }
99
100 /* private */
101 static PolkitDetails *
102 polkit_details_new_for_hash (GHashTable *hash)
103 {
104 PolkitDetails *details;
105
106 details = POLKIT_DETAILS (g_object_new (POLKIT_TYPE_DETAILS, NULL));
107 if (hash != NULL)
108 details->hash = g_hash_table_ref (hash);
109
110 return details;
111 }
112
113 /**
114 * polkit_details_lookup:
115 * @details: A #PolkitDetails.
116 * @key: A key.
117 *
118 * Gets the value for @key on @details.
119 *
120 * Returns: (allow-none): %NULL if there is no value for @key, otherwise a string owned by @details.
121 */
122 const gchar *
123 polkit_details_lookup (PolkitDetails *details,
124 const gchar *key)
125 {
126 g_return_val_if_fail (POLKIT_IS_DETAILS (details), NULL);
127 g_return_val_if_fail (key != NULL, NULL);
128 if (details->hash == NULL)
129 return NULL;
130 else
131 return g_hash_table_lookup (details->hash, key);
132 }
133
134 /**
135 * polkit_details_insert:
136 * @details: A #PolkitDetails.
137 * @key: A key.
138 * @value: (allow-none): A value.
139 *
140 * Inserts a copy of @key and @value on @details.
141 *
142 * If @value is %NULL, the key will be removed.
143 */
144 void
145 polkit_details_insert (PolkitDetails *details,
146 const gchar *key,
147 const gchar *value)
148 {
149 g_return_if_fail (POLKIT_IS_DETAILS (details));
150 g_return_if_fail (key != NULL);
151 if (details->hash == NULL)
152 details->hash = g_hash_table_new_full (g_str_hash,
153 g_str_equal,
154 g_free,
155 g_free);
156 if (value != NULL)
157 g_hash_table_insert (details->hash, g_strdup (key), g_strdup (value));
158 else
159 g_hash_table_remove (details->hash, key);
160 }
161
162 /**
163 * polkit_details_get_keys:
164 * @details: A #PolkitDetails.
165 *
166 * Gets a list of all keys on @details.
167 *
168 * Returns: (transfer full) (allow-none): %NULL if there are no keys
169 * otherwise an array of strings that should be freed with
170 * g_strfreev().
171 */
172 gchar **
173 polkit_details_get_keys (PolkitDetails *details)
174 {
175 GList *keys, *l;
176 gchar **ret;
177 guint n;
178
179 g_return_val_if_fail (POLKIT_IS_DETAILS (details), NULL);
180
181 if (details->hash == NULL)
182 return NULL;
183
184 keys = g_hash_table_get_keys (details->hash);
185 ret = g_new0 (gchar*, g_list_length (keys) + 1);
186 for (l = keys, n = 0; l != NULL; l = l->next, n++)
187 ret[n] = g_strdup (l->data);
188
189 g_list_free (keys);
190
191 return ret;
192 }
193
194 /* Note that this returns a floating value. */
195 GVariant *
196 polkit_details_to_gvariant (PolkitDetails *details)
197 {
198 GVariantBuilder builder;
199
200 g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{ss}"));
201 if (details != NULL && details->hash != NULL)
202 {
203 GHashTableIter hash_iter;
204 const gchar *key;
205 const gchar *value;
206
207 g_hash_table_iter_init (&hash_iter, details->hash);
208 while (g_hash_table_iter_next (&hash_iter, (gpointer) &key, (gpointer) &value))
209 g_variant_builder_add (&builder, "{ss}", key, value);
210 }
211 return g_variant_builder_end (&builder);
212 }
213
214 PolkitDetails *
215 polkit_details_new_for_gvariant (GVariant *value)
216 {
217 PolkitDetails *ret;
218 GHashTable *hash;
219 GVariantIter iter;
220 gchar *hash_key;
221 gchar *hash_value;
222
223 hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
224 g_variant_iter_init (&iter, value);
225 while (g_variant_iter_next (&iter, "{ss}", &hash_key, &hash_value))
226 g_hash_table_insert (hash, hash_key, hash_value);
227 ret = polkit_details_new_for_hash (hash);
228 g_hash_table_unref (hash);
229 return ret;
230 }
231