2 * Copyright (C) 2013 Wim Taymans <wim.taymans at gmail.com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library 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.
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 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
22 #include "rtsp-permissions.h"
24 typedef struct _GstRTSPPermissionsImpl
26 GstRTSPPermissions permissions;
28 /* Roles, array of RoleEntry */
30 } GstRTSPPermissionsImpl;
35 GstStructure *structure;
39 clear_role_entry (RoleEntry * role)
42 gst_structure_set_parent_refcount (role->structure, NULL);
43 gst_structure_free (role->structure);
46 //GST_DEBUG_CATEGORY_STATIC (rtsp_permissions_debug);
47 //#define GST_CAT_DEFAULT rtsp_permissions_debug
49 GST_DEFINE_MINI_OBJECT_TYPE (GstRTSPPermissions, gst_rtsp_permissions);
51 static void gst_rtsp_permissions_init (GstRTSPPermissionsImpl * permissions,
52 GstStructure * structure);
55 _gst_rtsp_permissions_free (GstRTSPPermissions * permissions)
57 GstRTSPPermissionsImpl *impl = (GstRTSPPermissionsImpl *) permissions;
59 g_array_free (impl->roles, TRUE);
61 g_slice_free1 (sizeof (GstRTSPPermissionsImpl), permissions);
64 static GstRTSPPermissions *
65 _gst_rtsp_permissions_copy (GstRTSPPermissionsImpl * permissions)
67 GstRTSPPermissionsImpl *copy;
68 GstStructure *structure;
70 copy = g_slice_new0 (GstRTSPPermissionsImpl);
71 gst_rtsp_permissions_init (copy, structure);
73 return GST_RTSP_PERMISSIONS (copy);
77 gst_rtsp_permissions_init (GstRTSPPermissionsImpl * permissions,
78 GstStructure * structure)
80 gst_mini_object_init (GST_MINI_OBJECT_CAST (permissions), 0,
81 GST_TYPE_RTSP_PERMISSIONS,
82 (GstMiniObjectCopyFunction) _gst_rtsp_permissions_copy, NULL,
83 (GstMiniObjectFreeFunction) _gst_rtsp_permissions_free);
85 permissions->roles = g_array_new (FALSE, TRUE, sizeof (RoleEntry));
86 g_array_set_clear_func (permissions->roles,
87 (GDestroyNotify) clear_role_entry);
91 * gst_rtsp_permissions_new:
93 * Create a new empty Authorization permissions.
95 * Returns: (transfer full): a new empty authorization permissions.
98 gst_rtsp_permissions_new (void)
100 GstRTSPPermissionsImpl *permissions;
102 permissions = g_slice_new0 (GstRTSPPermissionsImpl);
104 gst_rtsp_permissions_init (permissions,
105 gst_structure_new_empty ("GstRTSPPermissions"));
107 return GST_RTSP_PERMISSIONS (permissions);
111 * gst_rtsp_permissions_add_role:
112 * @permissions: a #GstRTSPPermissions
114 * @structure: the permissions structure
116 * Add the configuration in @structure to @permissions for @role.
119 gst_rtsp_permissions_add_role (GstRTSPPermissions * permissions,
120 const gchar * role, GstStructure * structure)
122 GstRTSPPermissionsImpl *impl = (GstRTSPPermissionsImpl *) permissions;
127 g_return_if_fail (GST_IS_RTSP_PERMISSIONS (permissions));
128 g_return_if_fail (gst_mini_object_is_writable (&permissions->mini_object));
129 g_return_if_fail (role != NULL);
130 g_return_if_fail (structure != NULL);
132 len = impl->roles->len;
134 for (i = 0; i < len; i++) {
135 RoleEntry *entry = &g_array_index (impl->roles, RoleEntry, i);
137 if (g_str_equal (entry->role, role)) {
138 gst_structure_free (entry->structure);
139 entry->structure = structure;
145 item.role = g_strdup (role);
146 item.structure = structure;
147 gst_structure_set_parent_refcount (structure,
148 &impl->permissions.mini_object.refcount);
149 g_array_append_val (impl->roles, item);
154 * gst_rtsp_permissions_remove_role:
155 * @permissions: a #GstRTSPPermissions
158 * Remove all permissions for @role in @permissions.
161 gst_rtsp_permissions_remove_role (GstRTSPPermissions * permissions,
164 g_return_if_fail (GST_IS_RTSP_PERMISSIONS (permissions));
165 g_return_if_fail (gst_mini_object_is_writable (&permissions->mini_object));
166 g_return_if_fail (role != NULL);
170 * gst_rtsp_permissions_get_role:
171 * @permissions: a #GstRTSPPermissions
174 * Get all permissions for @role in @permissions.
176 * Returns: the structure with permissions for @role.
179 gst_rtsp_permissions_get_role (GstRTSPPermissions * permissions,
182 GstRTSPPermissionsImpl *impl = (GstRTSPPermissionsImpl *) permissions;
185 g_return_val_if_fail (GST_IS_RTSP_PERMISSIONS (permissions), NULL);
186 g_return_val_if_fail (role != NULL, NULL);
188 len = impl->roles->len;
189 for (i = 0; i < len; i++) {
190 RoleEntry *entry = &g_array_index (impl->roles, RoleEntry, i);
192 if (g_str_equal (entry->role, role))
193 return entry->structure;
199 * gst_rtsp_permissions_is_allowed:
200 * @permissions: a #GstRTSPPermissions
202 * @permission: a permission
204 * Check if @role in @permissions is given permission for @permission.
206 * Returns: %TRUE if @role is allowed @permission.
209 gst_rtsp_permissions_is_allowed (GstRTSPPermissions * permissions,
210 const gchar * role, const gchar * permission)
212 const GstStructure *str;
215 g_return_val_if_fail (GST_IS_RTSP_PERMISSIONS (permissions), FALSE);
216 g_return_val_if_fail (role != NULL, FALSE);
217 g_return_val_if_fail (permission != NULL, FALSE);
219 str = gst_rtsp_permissions_get_role (permissions, role);
223 if (!gst_structure_get_boolean (str, permission, &result))