Debian Codegraph @

src/programs/pkaction.c

raw
1 /*
2 * Copyright (C) 2009 Red Hat, Inc.
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General
15 * Public License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
17 * Boston, MA 02111-1307, USA.
18 *
19 * Author: David Zeuthen <[email protected]>
20 */
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <glib/gi18n.h>
25 #include <polkit/polkit.h>
26
27 static void
28 print_action (PolkitActionDescription *action,
29 gboolean opt_verbose)
30 {
31
32 if (!opt_verbose)
33 {
34 g_print ("%s\n", polkit_action_description_get_action_id (action));
35 }
36 else
37 {
38 const gchar *vendor;
39 const gchar *vendor_url;
40 const gchar *icon_name;
41 const gchar* const *annotation_keys;
42 guint n;
43
44 vendor = polkit_action_description_get_vendor_name (action);
45 vendor_url = polkit_action_description_get_vendor_url (action);
46 icon_name = polkit_action_description_get_icon_name (action);
47
48 g_print ("%s:\n", polkit_action_description_get_action_id (action));
49 g_print (" description: %s\n", polkit_action_description_get_description (action));
50 g_print (" message: %s\n", polkit_action_description_get_message (action));
51 if (vendor != NULL)
52 g_print (" vendor: %s\n", vendor);
53 if (vendor_url != NULL)
54 g_print (" vendor_url: %s\n", vendor_url);
55
56 if (icon_name != NULL)
57 g_print (" icon: %s\n", icon_name);
58
59 g_print (" implicit any: %s\n", polkit_implicit_authorization_to_string (polkit_action_description_get_implicit_any (action)));
60 g_print (" implicit inactive: %s\n", polkit_implicit_authorization_to_string (polkit_action_description_get_implicit_inactive (action)));
61 g_print (" implicit active: %s\n", polkit_implicit_authorization_to_string (polkit_action_description_get_implicit_active (action)));
62
63 annotation_keys = polkit_action_description_get_annotation_keys (action);
64 for (n = 0; annotation_keys[n] != NULL; n++)
65 {
66 const gchar *key;
67 const gchar *value;
68
69 key = annotation_keys[n];
70 value = polkit_action_description_get_annotation (action, key);
71 g_print (" annotation: %s -> %s\n", key, value);
72 }
73 g_print ("\n");
74 }
75 }
76
77 static gint
78 action_desc_compare_by_action_id_func (PolkitActionDescription *a,
79 PolkitActionDescription *b)
80 {
81 return g_strcmp0 (polkit_action_description_get_action_id (a),
82 polkit_action_description_get_action_id (b));
83 }
84
85 int
86 main (int argc, char *argv[])
87 {
88 guint ret;
89 gchar *opt_action_id;
90 gchar *s;
91 gboolean opt_show_version;
92 gboolean opt_verbose;
93 GOptionEntry options[] =
94 {
95 {
96 "action-id", 'a', 0, G_OPTION_ARG_STRING, &opt_action_id,
97 N_("Only output information about ACTION"), N_("ACTION")
98 },
99 {
100 "verbose", 'v', 0, G_OPTION_ARG_NONE, &opt_verbose,
101 N_("Output detailed action information"), NULL
102 },
103 {
104 "version", 0, 0, G_OPTION_ARG_NONE, &opt_show_version,
105 N_("Show version"), NULL
106 },
107 { NULL, 0, 0, 0, NULL, NULL, NULL }
108 };
109 GOptionContext *context;
110 PolkitAuthority *authority;
111 GList *l;
112 GList *actions;
113 GError *error;
114
115 opt_action_id = NULL;
116 context = NULL;
117 authority = NULL;
118 actions = NULL;
119 ret = 1;
120
121 /* Disable remote file access from GIO. */
122 setenv ("GIO_USE_VFS", "local", 1);
123
124 opt_show_version = FALSE;
125 opt_verbose = FALSE;
126
127 error = NULL;
128 context = g_option_context_new (N_("[--action-id ACTION]"));
129 s = g_strdup_printf (_("Report bugs to: %s\n"
130 "%s home page: <%s>"), PACKAGE_BUGREPORT,
131 PACKAGE_NAME, PACKAGE_URL);
132 g_option_context_set_description (context, s);
133 g_free (s);
134 g_option_context_add_main_entries (context, options, GETTEXT_PACKAGE);
135 if (!g_option_context_parse (context, &argc, &argv, &error))
136 {
137 g_printerr ("%s: %s\n", g_get_prgname (), error->message);
138 g_error_free (error);
139 goto out;
140 }
141 if (argc > 1)
142 {
143 g_printerr (_("%s: Unexpected argument `%s'\n"), g_get_prgname (),
144 argv[1]);
145 goto out;
146 }
147 if (opt_show_version)
148 {
149 g_print ("pkaction version %s\n", PACKAGE_VERSION);
150 ret = 0;
151 goto out;
152 }
153
154 authority = polkit_authority_get_sync (NULL /* GCancellable* */, &error);
155 if (authority == NULL)
156 {
157 g_printerr ("Error getting authority: %s\n", error->message);
158 g_error_free (error);
159 goto out;
160 }
161
162 error = NULL;
163 actions = polkit_authority_enumerate_actions_sync (authority,
164 NULL, /* GCancellable */
165 &error);
166 if (error != NULL)
167 {
168 g_printerr ("Error enumerating actions: %s\n", error->message);
169 g_error_free (error);
170 goto out;
171 }
172
173 if (opt_action_id != NULL)
174 {
175 for (l = actions; l != NULL; l = l->next)
176 {
177 PolkitActionDescription *action = POLKIT_ACTION_DESCRIPTION (l->data);
178 const gchar *id;
179
180 id = polkit_action_description_get_action_id (action);
181
182 if (g_strcmp0 (id, opt_action_id) == 0)
183 {
184 print_action (action, opt_verbose);
185 break;
186 }
187 }
188
189 if (l == NULL)
190 {
191 g_printerr ("No action with action id %s\n", opt_action_id);
192 goto out;
193 }
194 }
195 else
196 {
197 actions = g_list_sort (actions,
198 (GCompareFunc) action_desc_compare_by_action_id_func);
199
200 for (l = actions; l != NULL; l = l->next)
201 {
202 PolkitActionDescription *action = POLKIT_ACTION_DESCRIPTION (l->data);
203
204 print_action (action, opt_verbose);
205 }
206 }
207
208 ret = 0;
209
210 out:
211 g_list_foreach (actions, (GFunc) g_object_unref, NULL);
212 g_list_free (actions);
213
214 g_free (opt_action_id);
215
216 if (authority != NULL)
217 g_object_unref (authority);
218
219 g_option_context_free (context);
220
221 return ret;
222 }
223