| 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 "polkitactiondescription.h" |
| 25 |
|
| 26 |
#include "polkitprivate.h" |
| 27 |
|
| 28 |
/** |
| 29 |
* SECTION:polkitactiondescription |
| 30 |
* @title: PolkitActionDescription |
| 31 |
* @short_description: Description of Actions |
| 32 |
* |
| 33 |
* Object used to encapsulate a registered action. |
| 34 |
*/ |
| 35 |
|
| 36 |
/** |
| 37 |
* PolkitActionDescription: |
| 38 |
* |
| 39 |
* The #PolkitActionDescription struct should not be accessed directly. |
| 40 |
*/ |
| 41 |
struct _PolkitActionDescription |
| 42 |
{ |
| 43 |
GObject parent_instance; |
| 44 |
gchar *action_id; |
| 45 |
gchar *description; |
| 46 |
gchar *message; |
| 47 |
gchar *vendor_name; |
| 48 |
gchar *vendor_url; |
| 49 |
gchar *icon_name; |
| 50 |
PolkitImplicitAuthorization implicit_any; |
| 51 |
PolkitImplicitAuthorization implicit_inactive; |
| 52 |
PolkitImplicitAuthorization implicit_active; |
| 53 |
GHashTable *annotations; |
| 54 |
gchar **annotation_keys; |
| 55 |
}; |
| 56 |
|
| 57 |
struct _PolkitActionDescriptionClass |
| 58 |
{ |
| 59 |
GObjectClass parent_class; |
| 60 |
}; |
| 61 |
|
| 62 |
G_DEFINE_TYPE (PolkitActionDescription, polkit_action_description, G_TYPE_OBJECT); |
| 63 |
|
| 64 |
static void |
| 65 |
polkit_action_description_init (PolkitActionDescription *action_description) |
| 66 |
{ |
| 67 |
action_description->annotations = g_hash_table_new_full (g_str_hash, |
| 68 |
g_str_equal, |
| 69 |
g_free, |
| 70 |
g_free); |
| 71 |
} |
| 72 |
|
| 73 |
static void |
| 74 |
polkit_action_description_finalize (GObject *object) |
| 75 |
{ |
| 76 |
PolkitActionDescription *action_description; |
| 77 |
|
| 78 |
action_description = POLKIT_ACTION_DESCRIPTION (object); |
| 79 |
|
| 80 |
g_free (action_description->action_id); |
| 81 |
g_free (action_description->description); |
| 82 |
g_free (action_description->message); |
| 83 |
g_free (action_description->vendor_name); |
| 84 |
g_free (action_description->vendor_url); |
| 85 |
g_free (action_description->icon_name); |
| 86 |
g_hash_table_unref (action_description->annotations); |
| 87 |
g_strfreev (action_description->annotation_keys); |
| 88 |
|
| 89 |
if (G_OBJECT_CLASS (polkit_action_description_parent_class)->finalize != NULL) |
| 90 |
G_OBJECT_CLASS (polkit_action_description_parent_class)->finalize (object); |
| 91 |
} |
| 92 |
|
| 93 |
static void |
| 94 |
polkit_action_description_class_init (PolkitActionDescriptionClass *klass) |
| 95 |
{ |
| 96 |
GObjectClass *gobject_class = G_OBJECT_CLASS (klass); |
| 97 |
gobject_class->finalize = polkit_action_description_finalize; |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* polkit_action_description_get_action_id: |
| 102 |
* @action_description: A #PolkitActionDescription. |
| 103 |
* |
| 104 |
* Gets the action id for @action_description. |
| 105 |
* |
| 106 |
* Returns: A string owned by @action_description. Do not free. |
| 107 |
*/ |
| 108 |
const gchar * |
| 109 |
polkit_action_description_get_action_id (PolkitActionDescription *action_description) |
| 110 |
{ |
| 111 |
g_return_val_if_fail (POLKIT_IS_ACTION_DESCRIPTION (action_description), NULL); |
| 112 |
return action_description->action_id; |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* polkit_action_description_get_description: |
| 117 |
* @action_description: A #PolkitActionDescription. |
| 118 |
* |
| 119 |
* Gets the description used for @action_description. |
| 120 |
* |
| 121 |
* Returns: A string owned by @action_description. Do not free. |
| 122 |
*/ |
| 123 |
const gchar * |
| 124 |
polkit_action_description_get_description (PolkitActionDescription *action_description) |
| 125 |
{ |
| 126 |
g_return_val_if_fail (POLKIT_IS_ACTION_DESCRIPTION (action_description), NULL); |
| 127 |
return action_description->description; |
| 128 |
} |
| 129 |
|
| 130 |
/** |
| 131 |
* polkit_action_description_get_message: |
| 132 |
* @action_description: A #PolkitActionDescription. |
| 133 |
* |
| 134 |
* Gets the message used for @action_description. |
| 135 |
* |
| 136 |
* Returns: A string owned by @action_description. Do not free. |
| 137 |
*/ |
| 138 |
const gchar * |
| 139 |
polkit_action_description_get_message (PolkitActionDescription *action_description) |
| 140 |
{ |
| 141 |
g_return_val_if_fail (POLKIT_IS_ACTION_DESCRIPTION (action_description), NULL); |
| 142 |
return action_description->message; |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* polkit_action_description_get_vendor_name: |
| 147 |
* @action_description: A #PolkitActionDescription. |
| 148 |
* |
| 149 |
* Gets the vendor name for @action_description, if any. |
| 150 |
* |
| 151 |
* Returns: A string owned by @action_description. Do not free. |
| 152 |
*/ |
| 153 |
const gchar * |
| 154 |
polkit_action_description_get_vendor_name (PolkitActionDescription *action_description) |
| 155 |
{ |
| 156 |
g_return_val_if_fail (POLKIT_IS_ACTION_DESCRIPTION (action_description), NULL); |
| 157 |
return action_description->vendor_name; |
| 158 |
} |
| 159 |
|
| 160 |
/** |
| 161 |
* polkit_action_description_get_vendor_url: |
| 162 |
* @action_description: A #PolkitActionDescription. |
| 163 |
* |
| 164 |
* Gets the vendor URL for @action_description, if any. |
| 165 |
* |
| 166 |
* Returns: A string owned by @action_description. Do not free. |
| 167 |
*/ |
| 168 |
const gchar * |
| 169 |
polkit_action_description_get_vendor_url (PolkitActionDescription *action_description) |
| 170 |
{ |
| 171 |
g_return_val_if_fail (POLKIT_IS_ACTION_DESCRIPTION (action_description), NULL); |
| 172 |
return action_description->vendor_url; |
| 173 |
} |
| 174 |
|
| 175 |
/** |
| 176 |
* polkit_action_description_get_implicit_any: |
| 177 |
* @action_description: A #PolkitActionDescription. |
| 178 |
* |
| 179 |
* Gets the implicit authorization for @action_description used for |
| 180 |
* any subject. |
| 181 |
* |
| 182 |
* Returns: A value from the #PolkitImplicitAuthorization enumeration. |
| 183 |
*/ |
| 184 |
PolkitImplicitAuthorization |
| 185 |
polkit_action_description_get_implicit_any (PolkitActionDescription *action_description) |
| 186 |
{ |
| 187 |
g_return_val_if_fail (POLKIT_IS_ACTION_DESCRIPTION (action_description), 0); |
| 188 |
return action_description->implicit_any; |
| 189 |
} |
| 190 |
|
| 191 |
/** |
| 192 |
* polkit_action_description_get_implicit_inactive: |
| 193 |
* @action_description: A #PolkitActionDescription. |
| 194 |
* |
| 195 |
* Gets the implicit authorization for @action_description used for |
| 196 |
* subjects in inactive sessions on a local console. |
| 197 |
* |
| 198 |
* Returns: A value from the #PolkitImplicitAuthorization enumeration. |
| 199 |
*/ |
| 200 |
PolkitImplicitAuthorization |
| 201 |
polkit_action_description_get_implicit_inactive (PolkitActionDescription *action_description) |
| 202 |
{ |
| 203 |
g_return_val_if_fail (POLKIT_IS_ACTION_DESCRIPTION (action_description), 0); |
| 204 |
return action_description->implicit_inactive; |
| 205 |
} |
| 206 |
|
| 207 |
/** |
| 208 |
* polkit_action_description_get_implicit_active: |
| 209 |
* @action_description: A #PolkitActionDescription. |
| 210 |
* |
| 211 |
* Gets the implicit authorization for @action_description used for |
| 212 |
* subjects in active sessions on a local console. |
| 213 |
* |
| 214 |
* Returns: A value from the #PolkitImplicitAuthorization enumeration. |
| 215 |
*/ |
| 216 |
PolkitImplicitAuthorization |
| 217 |
polkit_action_description_get_implicit_active (PolkitActionDescription *action_description) |
| 218 |
{ |
| 219 |
g_return_val_if_fail (POLKIT_IS_ACTION_DESCRIPTION (action_description), 0); |
| 220 |
return action_description->implicit_active; |
| 221 |
} |
| 222 |
|
| 223 |
|
| 224 |
/** |
| 225 |
* polkit_action_description_get_icon_name: |
| 226 |
* @action_description: A #PolkitActionDescription. |
| 227 |
* |
| 228 |
* Gets the icon name for @action_description, if any. |
| 229 |
* |
| 230 |
* Returns: A string owned by @action_description. Do not free. |
| 231 |
*/ |
| 232 |
const gchar * |
| 233 |
polkit_action_description_get_icon_name (PolkitActionDescription *action_description) |
| 234 |
{ |
| 235 |
g_return_val_if_fail (POLKIT_IS_ACTION_DESCRIPTION (action_description), NULL); |
| 236 |
return action_description->icon_name; |
| 237 |
} |
| 238 |
|
| 239 |
/** |
| 240 |
* polkit_action_description_get_annotation: |
| 241 |
* @action_description: A #PolkitActionDescription. |
| 242 |
* @key: An annotation key. |
| 243 |
* |
| 244 |
* Get the value of the annotation with @key. |
| 245 |
* |
| 246 |
* Returns: (allow-none): %NULL if there is no annoation with @key, |
| 247 |
* otherwise the annotation value owned by @action_description. Do not |
| 248 |
* free. |
| 249 |
*/ |
| 250 |
const gchar * |
| 251 |
polkit_action_description_get_annotation (PolkitActionDescription *action_description, |
| 252 |
const gchar *key) |
| 253 |
{ |
| 254 |
g_return_val_if_fail (POLKIT_IS_ACTION_DESCRIPTION (action_description), NULL); |
| 255 |
return g_hash_table_lookup (action_description->annotations, key); |
| 256 |
} |
| 257 |
|
| 258 |
/** |
| 259 |
* polkit_action_description_get_annotation_keys: |
| 260 |
* @action_description: A #PolkitActionDescription. |
| 261 |
* |
| 262 |
* Gets the keys of annotations defined in @action_description. |
| 263 |
* |
| 264 |
* Returns: (transfer none): The annotation keys owned by @action_description. Do not free. |
| 265 |
*/ |
| 266 |
const gchar * const * |
| 267 |
polkit_action_description_get_annotation_keys (PolkitActionDescription *action_description) |
| 268 |
{ |
| 269 |
GPtrArray *p; |
| 270 |
GHashTableIter iter; |
| 271 |
const gchar *key; |
| 272 |
|
| 273 |
g_return_val_if_fail (POLKIT_IS_ACTION_DESCRIPTION (action_description), NULL); |
| 274 |
|
| 275 |
if (action_description->annotation_keys != NULL) |
| 276 |
goto out; |
| 277 |
|
| 278 |
p = g_ptr_array_new (); |
| 279 |
|
| 280 |
g_hash_table_iter_init (&iter, action_description->annotations); |
| 281 |
while (g_hash_table_iter_next (&iter, (gpointer) &key, NULL)) |
| 282 |
g_ptr_array_add (p, g_strdup (key)); |
| 283 |
|
| 284 |
g_ptr_array_add (p, NULL); |
| 285 |
action_description->annotation_keys = (gchar **) g_ptr_array_free (p, FALSE); |
| 286 |
|
| 287 |
out: |
| 288 |
return (const gchar * const *) action_description->annotation_keys; |
| 289 |
} |
| 290 |
|
| 291 |
PolkitActionDescription * |
| 292 |
polkit_action_description_new (const gchar *action_id, |
| 293 |
const gchar *description, |
| 294 |
const gchar *message, |
| 295 |
const gchar *vendor_name, |
| 296 |
const gchar *vendor_url, |
| 297 |
const gchar *icon_name, |
| 298 |
PolkitImplicitAuthorization implicit_any, |
| 299 |
PolkitImplicitAuthorization implicit_inactive, |
| 300 |
PolkitImplicitAuthorization implicit_active, |
| 301 |
GHashTable *annotations) |
| 302 |
{ |
| 303 |
PolkitActionDescription *ret; |
| 304 |
g_return_val_if_fail (annotations != NULL, NULL); |
| 305 |
ret = POLKIT_ACTION_DESCRIPTION (g_object_new (POLKIT_TYPE_ACTION_DESCRIPTION, NULL)); |
| 306 |
ret->action_id = g_strdup (action_id); |
| 307 |
ret->description = g_strdup (description); |
| 308 |
ret->message = g_strdup (message); |
| 309 |
ret->vendor_name = g_strdup (vendor_name); |
| 310 |
ret->vendor_url = g_strdup (vendor_url); |
| 311 |
ret->icon_name = g_strdup (icon_name); |
| 312 |
ret->implicit_any = implicit_any; |
| 313 |
ret->implicit_inactive = implicit_inactive; |
| 314 |
ret->implicit_active = implicit_active; |
| 315 |
if (ret->annotations != NULL) |
| 316 |
g_hash_table_unref (ret->annotations); |
| 317 |
ret->annotations = g_hash_table_ref (annotations); |
| 318 |
return ret; |
| 319 |
} |
| 320 |
|
| 321 |
PolkitActionDescription * |
| 322 |
polkit_action_description_new_for_gvariant (GVariant *value) |
| 323 |
{ |
| 324 |
PolkitActionDescription *action_description; |
| 325 |
GVariantIter iter; |
| 326 |
GVariant *annotations_dict; |
| 327 |
gchar *a_key; |
| 328 |
gchar *a_value; |
| 329 |
|
| 330 |
action_description = POLKIT_ACTION_DESCRIPTION (g_object_new (POLKIT_TYPE_ACTION_DESCRIPTION, NULL)); |
| 331 |
g_variant_get (value, |
| 332 |
"(ssssssuuu@a{ss})", |
| 333 |
&action_description->action_id, |
| 334 |
&action_description->description, |
| 335 |
&action_description->message, |
| 336 |
&action_description->vendor_name, |
| 337 |
&action_description->vendor_url, |
| 338 |
&action_description->icon_name, |
| 339 |
&action_description->implicit_any, |
| 340 |
&action_description->implicit_inactive, |
| 341 |
&action_description->implicit_active, |
| 342 |
&annotations_dict); |
| 343 |
g_variant_iter_init (&iter, annotations_dict); |
| 344 |
while (g_variant_iter_next (&iter, "{ss}", &a_key, &a_value)) |
| 345 |
g_hash_table_insert (action_description->annotations, a_key, a_value); /* adopts a_key and a_value */ |
| 346 |
g_variant_unref (annotations_dict); |
| 347 |
|
| 348 |
return action_description; |
| 349 |
} |
| 350 |
|
| 351 |
/* Note that this returns a floating value. */ |
| 352 |
GVariant * |
| 353 |
polkit_action_description_to_gvariant (PolkitActionDescription *action_description) |
| 354 |
{ |
| 355 |
GVariantBuilder builder; |
| 356 |
GHashTableIter iter; |
| 357 |
const gchar *a_key; |
| 358 |
const gchar *a_value; |
| 359 |
|
| 360 |
g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{ss}")); |
| 361 |
|
| 362 |
g_hash_table_iter_init (&iter, action_description->annotations); |
| 363 |
while (g_hash_table_iter_next (&iter, (gpointer) &a_key, (gpointer) &a_value)) |
| 364 |
g_variant_builder_add (&builder, "{ss}", a_key, a_value); |
| 365 |
|
| 366 |
/* TODO: note 'foo ? : ""' is a gcc specific extension (it's a short-hand for 'foo ? foo : ""') */ |
| 367 |
return g_variant_new ("(ssssssuuua{ss})", |
| 368 |
action_description->action_id ? : "", |
| 369 |
action_description->description ? : "", |
| 370 |
action_description->message ? : "", |
| 371 |
action_description->vendor_name ? : "", |
| 372 |
action_description->vendor_url ? : "", |
| 373 |
action_description->icon_name ? : "", |
| 374 |
action_description->implicit_any, |
| 375 |
action_description->implicit_inactive, |
| 376 |
action_description->implicit_active, |
| 377 |
&builder); |
| 378 |
} |