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, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
24 /* a fake permission implementation */
26 #define G_TYPE_TEST_PERMISSION (g_test_permission_get_type ())
27 #define G_TEST_PERMISSION(inst) (G_TYPE_CHECK_INSTANCE_CAST ((inst), \
28 G_TYPE_TEST_PERMISSION, \
30 #define G_IS_TEST_PERMISSION(inst) (G_TYPE_CHECK_INSTANCE_TYPE ((inst), \
31 G_TYPE_TEST_PERMISSION))
33 typedef struct _GTestPermission GTestPermission;
34 typedef struct _GTestPermissionClass GTestPermissionClass;
36 struct _GTestPermission
43 struct _GTestPermissionClass
45 GPermissionClass parent_class;
48 G_DEFINE_TYPE (GTestPermission, g_test_permission, G_TYPE_PERMISSION)
51 g_test_permission_init (GTestPermission *test)
56 update_allowed (GTestPermission *test,
60 gboolean can_acquire, can_release;
63 "can-acquire", &can_acquire,
64 "can-release", &can_release,
69 g_permission_impl_update (G_PERMISSION (test),
70 allowed, can_acquire, can_release);
75 g_set_error_literal (error,
76 G_IO_ERROR, G_IO_ERROR_FAILED, "Sorry, no luck");
82 acquire (GPermission *permission,
83 GCancellable *cancellable,
86 GTestPermission *test = G_TEST_PERMISSION (permission);
87 return update_allowed (test, TRUE, error);
91 acquire_async (GPermission *permission,
92 GCancellable *cancellable,
93 GAsyncReadyCallback callback,
96 GSimpleAsyncResult *result;
97 g_print ("GTestPermission::acquire_async\n");
98 result = g_simple_async_result_new ((GObject*)permission,
102 g_simple_async_result_complete (result);
103 g_object_unref (result);
107 acquire_finish (GPermission *permission,
108 GAsyncResult *result,
111 GTestPermission *test = G_TEST_PERMISSION (permission);
112 g_print ("GTestPermission::acquire_finish\n");
113 return update_allowed (test, TRUE, error);
117 release (GPermission *permission,
118 GCancellable *cancellable,
121 GTestPermission *test = G_TEST_PERMISSION (permission);
122 return update_allowed (test, FALSE, error);
126 release_async (GPermission *permission,
127 GCancellable *cancellable,
128 GAsyncReadyCallback callback,
131 GSimpleAsyncResult *result;
132 result = g_simple_async_result_new ((GObject*)permission,
136 g_simple_async_result_complete (result);
137 g_object_unref (result);
141 release_finish (GPermission *permission,
142 GAsyncResult *result,
145 GTestPermission *test = G_TEST_PERMISSION (permission);
146 return update_allowed (test, FALSE, error);
150 g_test_permission_class_init (GTestPermissionClass *class)
152 GPermissionClass *permission_class = G_PERMISSION_CLASS (class);
154 permission_class->acquire = acquire;
155 permission_class->acquire_async = acquire_async;
156 permission_class->acquire_finish = acquire_finish;
158 permission_class->release = release;
159 permission_class->release_async = release_async;
160 permission_class->release_finish = release_finish;
164 g_test_permission_set_success (GTestPermission *permission,
167 permission->success = success;
170 static GtkWidget *allowed_button;
171 static GtkWidget *can_acquire_button;
172 static GtkWidget *can_release_button;
173 static GtkWidget *success_button;
176 update_clicked (GtkButton *button, GtkLockButton *lockbutton)
178 GPermission *permission;
179 gboolean allowed, can_acquire, can_release;
182 permission = gtk_lock_button_get_permission (lockbutton);
184 allowed = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (allowed_button));
185 can_acquire = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (can_acquire_button));
186 can_release = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (can_release_button));
187 success = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (success_button));
188 g_permission_impl_update (permission, allowed, can_acquire, can_release);
189 g_test_permission_set_success (G_TEST_PERMISSION (permission), success);
192 static GtkWidget *content;
195 permission_changed (GPermission *permission,
198 gboolean allowed, can_acquire, can_release;
200 g_object_get (permission,
202 "can-acquire", &can_acquire,
203 "can-release", &can_release,
206 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (allowed_button), allowed);
207 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (can_acquire_button), can_acquire);
208 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (can_release_button), can_release);
210 gtk_widget_set_sensitive (content, allowed);
214 main (int argc, char *argv[])
222 GPermission *permission;
224 gtk_init (&argc, &argv);
226 permission = g_object_new (G_TYPE_TEST_PERMISSION, NULL);
228 window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
229 gtk_window_set_resizable (GTK_WINDOW (window), FALSE);
230 gtk_container_set_border_width (GTK_CONTAINER (window), 12);
232 box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 5);
233 gtk_container_add (GTK_CONTAINER (window), box);
235 allowed_button = gtk_check_button_new_with_label ("Allowed");
236 gtk_container_add (GTK_CONTAINER (box), allowed_button);
237 can_acquire_button = gtk_check_button_new_with_label ("Can acquire");
238 gtk_container_add (GTK_CONTAINER (box), can_acquire_button);
239 can_release_button = gtk_check_button_new_with_label ("Can release");
240 gtk_container_add (GTK_CONTAINER (box), can_release_button);
241 success_button = gtk_check_button_new_with_label ("Will succeed");
242 gtk_container_add (GTK_CONTAINER (box), success_button);
243 update = gtk_button_new_with_label ("Update");
244 gtk_container_add (GTK_CONTAINER (box), update);
245 g_signal_connect (permission, "notify",
246 G_CALLBACK (permission_changed), NULL);
248 button = gtk_lock_button_new (permission);
250 g_signal_connect (update, "clicked",
251 G_CALLBACK (update_clicked), button);
253 dialog = gtk_dialog_new_with_buttons ("Dialog", NULL, 0,
254 "Close", GTK_RESPONSE_CLOSE,
255 "Some other action", GTK_RESPONSE_APPLY,
257 gtk_window_set_resizable (GTK_WINDOW (dialog), FALSE);
259 bbox = gtk_dialog_get_action_area (GTK_DIALOG (dialog));
260 gtk_container_add (GTK_CONTAINER (bbox), button);
261 gtk_button_box_set_child_secondary (GTK_BUTTON_BOX (bbox), button, TRUE);
262 gtk_button_box_set_child_non_homogeneous (GTK_BUTTON_BOX (bbox), button, TRUE);
264 content = gtk_box_new (GTK_ORIENTATION_VERTICAL, 5);
265 gtk_container_add (GTK_CONTAINER (content), gtk_check_button_new_with_label ("Control 1"));
266 gtk_container_add (GTK_CONTAINER (content), gtk_check_button_new_with_label ("Control 2"));
267 gtk_widget_set_sensitive (content, FALSE);
269 gtk_container_add (GTK_CONTAINER (gtk_dialog_get_content_area (GTK_DIALOG (dialog))), content);
271 gtk_widget_show_all (window);
272 gtk_widget_show_all (dialog);