]> Pileus Git - ~andy/gtk/blob - tests/testlogout.c
Simplify logout notification api
[~andy/gtk] / tests / testlogout.c
1 #include <gtk/gtk.h>
2
3 static GtkWidget *inhibit_entry;
4 static GtkWidget *inhibit_logout;
5 static GtkWidget *inhibit_switch;
6 static GtkWidget *inhibit_suspend;
7 static GtkWidget *inhibit_idle;
8 static GtkWidget *inhibit_label;
9 static GtkWidget *end_combo;
10 static GtkWidget *end_confirm;
11
12 static void
13 end_session (GtkButton *button, GtkApplication *app)
14 {
15   const gchar *id;
16   GtkApplicationEndStyle style;
17   gboolean confirm;
18
19   id = gtk_combo_box_get_active_id (GTK_COMBO_BOX (end_combo));
20   if (g_strcmp0 (id, "logout") == 0)
21     style = GTK_APPLICATION_LOGOUT;
22   else if (g_strcmp0 (id, "reboot") == 0)
23     style = GTK_APPLICATION_REBOOT;
24   else if (g_strcmp0 (id, "shutdown") == 0)
25     style = GTK_APPLICATION_SHUTDOWN;
26   else
27     style = GTK_APPLICATION_LOGOUT;
28
29   confirm = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (end_confirm));
30
31   g_print ("Calling gtk_application_end_session: %d, %d\n", style, confirm);
32
33   gtk_application_end_session (app, style, confirm);
34 }
35
36 static void
37 inhibitor_toggled (GtkToggleButton *button, GtkApplication *app)
38 {
39   gboolean active;
40   const gchar *reason;
41   GtkApplicationInhibitFlags flags;
42   GtkWidget *toplevel;
43   guint cookie;
44
45   active = gtk_toggle_button_get_active (button);
46   reason = gtk_entry_get_text (GTK_ENTRY (inhibit_entry));
47
48   flags = 0;
49   if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (inhibit_logout)))
50     flags |= GTK_APPLICATION_INHIBIT_LOGOUT;
51   if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (inhibit_switch)))
52     flags |= GTK_APPLICATION_INHIBIT_SWITCH;
53   if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (inhibit_suspend)))
54     flags |= GTK_APPLICATION_INHIBIT_SUSPEND;
55   if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (inhibit_idle)))
56     flags |= GTK_APPLICATION_INHIBIT_IDLE;
57
58   toplevel = gtk_widget_get_toplevel (GTK_WIDGET (button));
59
60   if (active)
61     {
62       gchar *text;
63
64       g_print ("Calling gtk_application_inhibit: %d, '%s'\n", flags, reason);
65
66       cookie = gtk_application_inhibit (app, GTK_WINDOW (toplevel), flags, reason);
67       g_object_set_data (G_OBJECT (button), "cookie", GUINT_TO_POINTER (cookie));
68       if (cookie == 0)
69         {
70           g_signal_handlers_block_by_func (button, inhibitor_toggled, app);
71           gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), FALSE);
72           g_signal_handlers_unblock_by_func (button, inhibitor_toggled, app);
73           active = FALSE;
74         }
75       else
76         {
77           text = g_strdup_printf ("%#x", cookie);
78           gtk_label_set_label (GTK_LABEL (inhibit_label), text);
79           g_free (text);
80         }
81     }
82   else
83     {
84       cookie = GPOINTER_TO_UINT (g_object_get_data (G_OBJECT (button), "cookie"));
85       g_print ("Calling gtk_application_uninhibit: %#x\n", cookie);
86       gtk_application_uninhibit (app, cookie);
87       gtk_label_set_label (GTK_LABEL (inhibit_label), "");
88     }
89
90   gtk_widget_set_sensitive (inhibit_entry, !active);
91   gtk_widget_set_sensitive (inhibit_logout, !active);
92   gtk_widget_set_sensitive (inhibit_switch, !active);
93   gtk_widget_set_sensitive (inhibit_suspend, !active);
94   gtk_widget_set_sensitive (inhibit_idle, !active);
95 }
96
97 static void
98 activate (GtkApplication *app,
99           gpointer        data)
100 {
101   GtkWidget *win;
102   GtkWidget *box;
103   GtkWidget *separator;
104   GtkWidget *grid;
105   GtkWidget *button;
106   GtkWidget *label;
107
108   win = gtk_window_new (GTK_WINDOW_TOPLEVEL);
109
110   box = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12);
111   g_object_set (box, "margin", 12, NULL);
112   gtk_container_add (GTK_CONTAINER (win), box);
113
114   grid = gtk_grid_new ();
115   gtk_grid_set_row_spacing (GTK_GRID (grid), 6);
116   gtk_grid_set_column_spacing (GTK_GRID (grid), 6);
117
118   gtk_container_add (GTK_CONTAINER (box), grid);
119
120   label = gtk_label_new ("Inhibitor");
121   gtk_grid_attach (GTK_GRID (grid), label, 0, 0, 1, 1);
122
123   inhibit_label = gtk_label_new ("");
124   gtk_grid_attach (GTK_GRID (grid), inhibit_label, 1, 0, 1, 1);
125
126   inhibit_logout = gtk_check_button_new_with_label ("Logout");
127   gtk_grid_attach (GTK_GRID (grid), inhibit_logout, 1, 1, 1, 1);
128
129   inhibit_switch = gtk_check_button_new_with_label ("User switching");
130   gtk_grid_attach (GTK_GRID (grid), inhibit_switch, 1, 2, 1, 1);
131
132   inhibit_suspend = gtk_check_button_new_with_label ("Suspend");
133   gtk_grid_attach (GTK_GRID (grid), inhibit_suspend, 1, 4, 1, 1);
134
135   inhibit_idle = gtk_check_button_new_with_label ("Idle");
136   gtk_grid_attach (GTK_GRID (grid), inhibit_idle, 1, 5, 1, 1);
137
138   inhibit_entry = gtk_entry_new ();
139   gtk_grid_attach (GTK_GRID (grid), inhibit_entry, 1, 6, 1, 1);
140
141   button = gtk_toggle_button_new_with_label ("Inhibit");
142   g_signal_connect (button, "toggled",
143                     G_CALLBACK (inhibitor_toggled), app);
144   gtk_grid_attach (GTK_GRID (grid), button, 2, 6, 1, 1);
145
146   separator = gtk_separator_new (GTK_ORIENTATION_HORIZONTAL);
147   gtk_container_add (GTK_CONTAINER (box), separator);
148
149   grid = gtk_grid_new ();
150   gtk_grid_set_row_spacing (GTK_GRID (grid), 6);
151   gtk_grid_set_column_spacing (GTK_GRID (grid), 6);
152
153   gtk_container_add (GTK_CONTAINER (box), grid);
154
155   end_combo = gtk_combo_box_text_new ();
156   gtk_combo_box_text_append (GTK_COMBO_BOX_TEXT (end_combo), "logout", "Logout");
157   gtk_combo_box_text_append (GTK_COMBO_BOX_TEXT (end_combo), "reboot", "Reboot");
158   gtk_combo_box_text_append (GTK_COMBO_BOX_TEXT (end_combo), "shutdown", "Shutdown");
159   gtk_combo_box_set_active_id (GTK_COMBO_BOX (end_combo), "logout");
160   gtk_grid_attach (GTK_GRID (grid), end_combo, 0, 0, 1, 1);
161
162   end_confirm = gtk_check_button_new_with_label ("Confirm");
163   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (end_confirm), TRUE);
164   gtk_grid_attach (GTK_GRID (grid), end_confirm, 1, 0, 1, 1);
165
166   button = gtk_button_new_with_label ("Go");
167   g_signal_connect (button, "clicked",
168                     G_CALLBACK (end_session), app);
169
170   gtk_grid_attach (GTK_GRID (grid), button, 2, 0, 1, 1);
171
172   gtk_widget_show_all (win);
173
174   gtk_application_add_window (app, GTK_WINDOW (win));
175 }
176
177 static void
178 quit (GtkApplication *app,
179       gpointer        data)
180 {
181   g_print ("Received quit\n");
182 }
183
184 int
185 main (int argc, char *argv[])
186 {
187   GtkApplication *app;
188
189   app = gtk_application_new ("org.gtk.Test.session", 0);
190   g_object_set (app, "register-session", TRUE, NULL);
191
192   g_signal_connect (app, "activate",
193                     G_CALLBACK (activate), NULL);
194   g_signal_connect (app, "quit",
195                     G_CALLBACK (quit), NULL);
196
197   g_application_run (G_APPLICATION (app), argc, argv);
198
199   return 0;
200 }