1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
|
/*
* Copyright (C) 2009 Red Hat, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General
* Public License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*
* Author: David Zeuthen <davidz@redhat.com>
*/
#include <polkitgtkmate/polkitgtkmate.h>
static PolkitAuthority* authority = NULL;
const gchar* action_id = NULL;
static PolkitSubject* system_bus_name_subject = NULL;
static PolkitSubject* unix_process_subject = NULL;
static GtkWidget* system_bus_name_authorized_label = NULL;
static GtkWidget* unix_process_authorized_label = NULL;
static void update_one(PolkitSubject* subject, GtkWidget* label)
{
PolkitAuthorizationResult* result;
GError* error;
GString* s;
gchar* subject_str;
s = g_string_new(NULL);
subject_str = polkit_subject_to_string(subject);
g_string_append_printf(s, "Result for subject `%s': ", subject_str);
g_free(subject_str);
error = NULL;
result = polkit_authority_check_authorization_sync(authority, subject, action_id, NULL, POLKIT_CHECK_AUTHORIZATION_FLAGS_NONE, NULL, &error);
if (result == NULL)
{
g_string_append_printf(s, "failed: %s", error->message);
g_error_free(error);
}
else
{
g_string_append_printf(s, "authorized=%d challenge=%d retains=%d temporary=%d", polkit_authorization_result_get_is_authorized(result), polkit_authorization_result_get_is_challenge(result), polkit_authorization_result_get_retains_authorization(result), polkit_authorization_result_get_temporary_authorization_id(result) != NULL);
g_object_unref(result);
}
gtk_label_set_text(GTK_LABEL(label), s->str);
g_string_free(s, TRUE);
}
static void update_labels(void)
{
update_one(system_bus_name_subject, system_bus_name_authorized_label);
update_one(unix_process_subject, unix_process_authorized_label);
}
static void on_authority_changed(PolkitAuthority* authority, gpointer user_data)
{
update_labels();
}
static void on_close(PolkitLockButton* button, gpointer user_data)
{
gtk_main_quit();
}
static void on_button_changed(PolkitLockButton* button, gpointer user_data)
{
GtkWidget* entry = GTK_WIDGET(user_data);
gtk_widget_set_sensitive(entry, polkit_lock_button_get_is_authorized(button));
}
int main(int argc, char* argv[])
{
GDBusConnection* bus;
GtkWidget* window;
GtkWidget* label;
GtkWidget* button;
GtkWidget* entry;
GtkWidget* vbox;
GError* error;
gchar* s;
gtk_init(&argc, &argv);
if (argc != 2)
{
g_printerr("usage: %s <action_id>\n", argv[0]);
goto out;
}
action_id = argv[1];
error = NULL;
bus = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL /* GCancellable* */, &error);
if (bus == NULL)
{
g_printerr("Failed connecting to system bus: %s\n", error->message);
g_error_free(error);
goto out;
}
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_resizable(GTK_WINDOW(window), TRUE);
vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12);
gtk_container_set_border_width(GTK_CONTAINER(window), 12);
gtk_container_add(GTK_CONTAINER(window), vbox);
s = g_strdup_printf("Showing PolkitLockButton for action id: %s", action_id);
label = gtk_label_new(s);
#if GTK_CHECK_VERSION (3, 16, 0)
gtk_label_set_xalign (GTK_LABEL (label), 0.0);
gtk_label_set_yalign (GTK_LABEL (label), 0.5);
#else
gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);
#endif
gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
g_free(s);
label = gtk_label_new(NULL);
#if GTK_CHECK_VERSION (3, 16, 0)
gtk_label_set_xalign (GTK_LABEL (label), 0.0);
gtk_label_set_yalign (GTK_LABEL (label), 0.5);
#else
gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);
#endif
gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
system_bus_name_authorized_label = label;
label = gtk_label_new(NULL);
#if GTK_CHECK_VERSION (3, 16, 0)
gtk_label_set_xalign (GTK_LABEL (label), 0.0);
gtk_label_set_yalign (GTK_LABEL (label), 0.5);
#else
gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.5);
#endif
gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
unix_process_authorized_label = label;
entry = gtk_entry_new();
gtk_box_pack_start(GTK_BOX(vbox), entry, FALSE, FALSE, 0);
button = polkit_lock_button_new(action_id);
g_signal_connect(button, "changed", G_CALLBACK(on_button_changed), entry);
gtk_box_pack_start(GTK_BOX(vbox), button, FALSE, FALSE, 0);
system_bus_name_subject = polkit_system_bus_name_new(g_dbus_connection_get_unique_name(bus));
unix_process_subject = polkit_unix_process_new_for_owner(getpid(), 0, -1);
error = NULL;
authority = polkit_authority_get_sync(NULL /* GCancellable* */, &error);
if (authority == NULL)
{
g_printerr("Failed getting the authority: %s\n", error->message);
g_error_free(error);
goto out;
}
g_debug("backend: name=`%s' version=`%s' features=%d", polkit_authority_get_backend_name(authority), polkit_authority_get_backend_version(authority), polkit_authority_get_backend_features(authority));
g_signal_connect(authority, "changed", G_CALLBACK(on_authority_changed), NULL);
update_labels();
gtk_widget_set_sensitive(entry, polkit_lock_button_get_is_authorized(POLKIT_LOCK_BUTTON(button)));
gtk_widget_show_all(window);
gtk_window_present(GTK_WINDOW(window));
g_signal_connect(button, "destroy", G_CALLBACK(on_close), NULL);
gtk_main();
out:
return 0;
}
|