2 * Copyright (C) 2011 Red Hat, Inc.
3 * Authors: Matthias Clasen
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
22 /* a fake permission implementation */
24 #define G_TYPE_TEST_PERMISSION (g_test_permission_get_type ())
25 #define G_TEST_PERMISSION(inst) (G_TYPE_CHECK_INSTANCE_CAST ((inst), \
26 G_TYPE_TEST_PERMISSION, \
28 #define G_IS_TEST_PERMISSION(inst) (G_TYPE_CHECK_INSTANCE_TYPE ((inst), \
29 G_TYPE_TEST_PERMISSION))
31 typedef struct _GTestPermission GTestPermission;
32 typedef struct _GTestPermissionClass GTestPermissionClass;
34 struct _GTestPermission
41 struct _GTestPermissionClass
43 GPermissionClass parent_class;
46 G_DEFINE_TYPE (GTestPermission, g_test_permission, G_TYPE_PERMISSION)
49 g_test_permission_init (GTestPermission *test)
54 update_allowed (GTestPermission *test,
58 gboolean can_acquire, can_release;
61 "can-acquire", &can_acquire,
62 "can-release", &can_release,
67 g_permission_impl_update (G_PERMISSION (test),
68 allowed, can_acquire, can_release);
73 g_set_error_literal (error,
74 G_IO_ERROR, G_IO_ERROR_FAILED, "Sorry, no luck");
80 acquire (GPermission *permission,
81 GCancellable *cancellable,
84 GTestPermission *test = G_TEST_PERMISSION (permission);
85 return update_allowed (test, TRUE, error);
89 acquire_async (GPermission *permission,
90 GCancellable *cancellable,
91 GAsyncReadyCallback callback,
94 GSimpleAsyncResult *result;
95 g_print ("GTestPermission::acquire_async\n");
96 result = g_simple_async_result_new ((GObject*)permission,
100 g_simple_async_result_complete (result);
101 g_object_unref (result);
105 acquire_finish (GPermission *permission,
106 GAsyncResult *result,
109 GTestPermission *test = G_TEST_PERMISSION (permission);
110 g_print ("GTestPermission::acquire_finish\n");
111 return update_allowed (test, TRUE, error);
115 release (GPermission *permission,
116 GCancellable *cancellable,
119 GTestPermission *test = G_TEST_PERMISSION (permission);
120 return update_allowed (test, FALSE, error);
124 release_async (GPermission *permission,
125 GCancellable *cancellable,
126 GAsyncReadyCallback callback,
129 GSimpleAsyncResult *result;
130 result = g_simple_async_result_new ((GObject*)permission,
134 g_simple_async_result_complete (result);
135 g_object_unref (result);
139 release_finish (GPermission *permission,
140 GAsyncResult *result,
143 GTestPermission *test = G_TEST_PERMISSION (permission);
144 return update_allowed (test, FALSE, error);
148 g_test_permission_class_init (GTestPermissionClass *class)
150 GPermissionClass *permission_class = G_PERMISSION_CLASS (class);
152 permission_class->acquire = acquire;
153 permission_class->acquire_async = acquire_async;
154 permission_class->acquire_finish = acquire_finish;
156 permission_class->release = release;
157 permission_class->release_async = release_async;
158 permission_class->release_finish = release_finish;
162 g_test_permission_set_success (GTestPermission *permission,
165 permission->success = success;
168 static GtkWidget *allowed_button;
169 static GtkWidget *can_acquire_button;
170 static GtkWidget *can_release_button;
171 static GtkWidget *success_button;
174 update_clicked (GtkButton *button, GtkLockButton *lockbutton)
176 GPermission *permission;
177 gboolean allowed, can_acquire, can_release;
180 permission = gtk_lock_button_get_permission (lockbutton);
182 allowed = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (allowed_button));
183 can_acquire = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (can_acquire_button));
184 can_release = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (can_release_button));
185 success = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (success_button));
186 g_permission_impl_update (permission, allowed, can_acquire, can_release);
187 g_test_permission_set_success (G_TEST_PERMISSION (permission), success);
190 static GtkWidget *content;
193 permission_changed (GPermission *permission,
196 gboolean allowed, can_acquire, can_release;
198 g_object_get (permission,
200 "can-acquire", &can_acquire,
201 "can-release", &can_release,
204 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (allowed_button), allowed);
205 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (can_acquire_button), can_acquire);
206 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (can_release_button), can_release);
208 gtk_widget_set_sensitive (content, allowed);
212 main (int argc, char *argv[])
220 GPermission *permission;
222 gtk_init (&argc, &argv);
224 permission = g_object_new (G_TYPE_TEST_PERMISSION, NULL);
226 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
227 gtk_window_set_resizable (GTK_WINDOW (window), FALSE);
228 gtk_container_set_border_width (GTK_CONTAINER (window), 12);
230 box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 5);
231 gtk_container_add (GTK_CONTAINER (window), box);
233 allowed_button = gtk_check_button_new_with_label ("Allowed");
234 gtk_container_add (GTK_CONTAINER (box), allowed_button);
235 can_acquire_button = gtk_check_button_new_with_label ("Can acquire");
236 gtk_container_add (GTK_CONTAINER (box), can_acquire_button);
237 can_release_button = gtk_check_button_new_with_label ("Can release");
238 gtk_container_add (GTK_CONTAINER (box), can_release_button);
239 success_button = gtk_check_button_new_with_label ("Will succeed");
240 gtk_container_add (GTK_CONTAINER (box), success_button);
241 update = gtk_button_new_with_label ("Update");
242 gtk_container_add (GTK_CONTAINER (box), update);
243 g_signal_connect (permission, "notify",
244 G_CALLBACK (permission_changed), NULL);
246 button = gtk_lock_button_new (permission);
248 g_signal_connect (update, "clicked",
249 G_CALLBACK (update_clicked), button);
251 dialog = gtk_dialog_new_with_buttons ("Dialog", NULL, 0,
252 "Close", GTK_RESPONSE_CLOSE,
253 "Some other action", GTK_RESPONSE_APPLY,
255 gtk_window_set_resizable (GTK_WINDOW (dialog), FALSE);
257 bbox = gtk_dialog_get_action_area (GTK_DIALOG (dialog));
258 gtk_container_add (GTK_CONTAINER (bbox), button);
259 gtk_button_box_set_child_secondary (GTK_BUTTON_BOX (bbox), button, TRUE);
260 gtk_button_box_set_child_non_homogeneous (GTK_BUTTON_BOX (bbox), button, TRUE);
262 content = gtk_box_new (GTK_ORIENTATION_VERTICAL, 5);
263 gtk_container_add (GTK_CONTAINER (content), gtk_check_button_new_with_label ("Control 1"));
264 gtk_container_add (GTK_CONTAINER (content), gtk_check_button_new_with_label ("Control 2"));
265 gtk_widget_set_sensitive (content, FALSE);
267 gtk_container_add (GTK_CONTAINER (gtk_dialog_get_content_area (GTK_DIALOG (dialog))), content);
269 gtk_widget_show_all (window);
270 gtk_widget_show_all (dialog);