1/* testlockbutton.c
2 * Copyright (C) 2011 Red Hat, Inc.
3 * Authors: Matthias Clasen
4 *
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.
9 *
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.
14 *
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/>.
17 */
18
19#include <gtk/gtk.h>
20#include <gio/gio.h>
21
22/* a fake permission implementation */
23
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, \
27 GTestPermission))
28#define G_IS_TEST_PERMISSION(inst) (G_TYPE_CHECK_INSTANCE_TYPE ((inst), \
29 G_TYPE_TEST_PERMISSION))
30
31typedef struct _GTestPermission GTestPermission;
32typedef struct _GTestPermissionClass GTestPermissionClass;
33
34struct _GTestPermission
35{
36 GPermission parent;
37
38 gboolean success;
39};
40
41struct _GTestPermissionClass
42{
43 GPermissionClass parent_class;
44};
45
46static GType g_test_permission_get_type (void);
47G_DEFINE_TYPE (GTestPermission, g_test_permission, G_TYPE_PERMISSION)
48
49static void
50g_test_permission_init (GTestPermission *test)
51{
52}
53
54static gboolean
55update_allowed (GTestPermission *test,
56 gboolean allowed,
57 GError **error)
58{
59 gboolean can_acquire, can_release;
60
61 g_object_get (object: test,
62 first_property_name: "can-acquire", &can_acquire,
63 "can-release", &can_release,
64 NULL);
65
66 if (test->success)
67 {
68 g_permission_impl_update (G_PERMISSION (test),
69 allowed, can_acquire, can_release);
70 return TRUE;
71 }
72 else
73 {
74 g_set_error_literal (err: error,
75 G_IO_ERROR, code: G_IO_ERROR_FAILED, message: "Sorry, no luck");
76 return FALSE;
77 }
78}
79
80static gboolean
81acquire (GPermission *permission,
82 GCancellable *cancellable,
83 GError **error)
84{
85 GTestPermission *test = G_TEST_PERMISSION (permission);
86 return update_allowed (test, TRUE, error);
87}
88
89static void
90acquire_async (GPermission *permission,
91 GCancellable *cancellable,
92 GAsyncReadyCallback callback,
93 gpointer user_data)
94{
95 GTask *result;
96 g_print (format: "GTestPermission::acquire_async\n");
97 result = g_task_new (source_object: (GObject*)permission,
98 cancellable,
99 callback,
100 callback_data: user_data);
101 g_task_return_boolean (task: result, TRUE);
102 g_object_unref (object: result);
103}
104
105static gboolean
106acquire_finish (GPermission *permission,
107 GAsyncResult *result,
108 GError **error)
109{
110 GTestPermission *test = G_TEST_PERMISSION (permission);
111 g_print (format: "GTestPermission::acquire_finish\n");
112 return update_allowed (test, TRUE, error);
113}
114
115static gboolean
116release (GPermission *permission,
117 GCancellable *cancellable,
118 GError **error)
119{
120 GTestPermission *test = G_TEST_PERMISSION (permission);
121 return update_allowed (test, FALSE, error);
122}
123
124static void
125release_async (GPermission *permission,
126 GCancellable *cancellable,
127 GAsyncReadyCallback callback,
128 gpointer user_data)
129{
130 GTask *result;
131 result = g_task_new (source_object: (GObject*)permission,
132 cancellable,
133 callback,
134 callback_data: user_data);
135 g_task_return_boolean (task: result, TRUE);
136 g_object_unref (object: result);
137}
138
139static gboolean
140release_finish (GPermission *permission,
141 GAsyncResult *result,
142 GError **error)
143{
144 GTestPermission *test = G_TEST_PERMISSION (permission);
145 return update_allowed (test, FALSE, error);
146}
147
148static void
149g_test_permission_class_init (GTestPermissionClass *class)
150{
151 GPermissionClass *permission_class = G_PERMISSION_CLASS (class);
152
153 permission_class->acquire = acquire;
154 permission_class->acquire_async = acquire_async;
155 permission_class->acquire_finish = acquire_finish;
156
157 permission_class->release = release;
158 permission_class->release_async = release_async;
159 permission_class->release_finish = release_finish;
160}
161
162static void
163g_test_permission_set_success (GTestPermission *permission,
164 gboolean success)
165{
166 permission->success = success;
167}
168
169static GtkWidget *allowed_button;
170static GtkWidget *can_acquire_button;
171static GtkWidget *can_release_button;
172static GtkWidget *success_button;
173
174static void
175update_clicked (GtkButton *button, GtkLockButton *lockbutton)
176{
177 GPermission *permission;
178 gboolean allowed, can_acquire, can_release;
179 gboolean success;
180
181 permission = gtk_lock_button_get_permission (button: lockbutton);
182
183 allowed = gtk_check_button_get_active (GTK_CHECK_BUTTON (allowed_button));
184 can_acquire = gtk_check_button_get_active (GTK_CHECK_BUTTON (can_acquire_button));
185 can_release = gtk_check_button_get_active (GTK_CHECK_BUTTON (can_release_button));
186 success = gtk_check_button_get_active (GTK_CHECK_BUTTON (success_button));
187 g_permission_impl_update (permission, allowed, can_acquire, can_release);
188 g_test_permission_set_success (G_TEST_PERMISSION (permission), success);
189}
190
191static GtkWidget *content;
192
193static void
194permission_changed (GPermission *permission,
195 GParamSpec *pspec)
196{
197 gboolean allowed, can_acquire, can_release;
198
199 g_object_get (object: permission,
200 first_property_name: "allowed", &allowed,
201 "can-acquire", &can_acquire,
202 "can-release", &can_release,
203 NULL);
204
205 gtk_check_button_set_active (GTK_CHECK_BUTTON (allowed_button), setting: allowed);
206 gtk_check_button_set_active (GTK_CHECK_BUTTON (can_acquire_button), setting: can_acquire);
207 gtk_check_button_set_active (GTK_CHECK_BUTTON (can_release_button), setting: can_release);
208
209 gtk_widget_set_sensitive (widget: content, sensitive: allowed);
210}
211
212int
213main (int argc, char *argv[])
214{
215 GtkWidget *window;
216 GtkWidget *dialog;
217 GtkWidget *button;
218 GtkWidget *box;
219 GtkWidget *update;
220 GPermission *permission;
221
222 gtk_init ();
223
224 permission = g_object_new (G_TYPE_TEST_PERMISSION, NULL);
225
226 window = gtk_window_new ();
227 gtk_window_set_resizable (GTK_WINDOW (window), FALSE);
228
229 box = gtk_box_new (orientation: GTK_ORIENTATION_VERTICAL, spacing: 5);
230 gtk_window_set_child (GTK_WINDOW (window), child: box);
231
232 allowed_button = gtk_check_button_new_with_label (label: "Allowed");
233 gtk_box_append (GTK_BOX (box), child: allowed_button);
234 can_acquire_button = gtk_check_button_new_with_label (label: "Can acquire");
235 gtk_box_append (GTK_BOX (box), child: can_acquire_button);
236 can_release_button = gtk_check_button_new_with_label (label: "Can release");
237 gtk_box_append (GTK_BOX (box), child: can_release_button);
238 success_button = gtk_check_button_new_with_label (label: "Will succeed");
239 gtk_box_append (GTK_BOX (box), child: success_button);
240 update = gtk_button_new_with_label (label: "Update");
241 gtk_box_append (GTK_BOX (box), child: update);
242 g_signal_connect (permission, "notify",
243 G_CALLBACK (permission_changed), NULL);
244
245 button = gtk_lock_button_new (permission);
246
247 g_signal_connect (update, "clicked",
248 G_CALLBACK (update_clicked), button);
249
250 dialog = gtk_dialog_new_with_buttons (title: "Dialog", NULL, flags: 0,
251 first_button_text: "Close", GTK_RESPONSE_CLOSE,
252 "Some other action", GTK_RESPONSE_APPLY,
253 NULL);
254 gtk_window_set_resizable (GTK_WINDOW (dialog), FALSE);
255
256 content = gtk_box_new (orientation: GTK_ORIENTATION_VERTICAL, spacing: 5);
257 gtk_box_append (GTK_BOX (content), child: gtk_check_button_new_with_label (label: "Control 1"));
258 gtk_box_append (GTK_BOX (content), child: gtk_check_button_new_with_label (label: "Control 2"));
259 gtk_widget_set_sensitive (widget: content, FALSE);
260
261 gtk_box_append (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (dialog))), child: content);
262 gtk_box_append (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (dialog))), child: button);
263
264 gtk_widget_show (widget: window);
265 gtk_widget_show (widget: dialog);
266
267 while (TRUE)
268 g_main_context_iteration (NULL, TRUE);
269
270 return 0;
271}
272

source code of gtk/tests/testlockbutton.c