chiark / gitweb /
Add a new function to compute the leftmost bit of a uint32_t
[disorder] / disobedience / users.c
CommitLineData
ffc4dbaf
RK
1/*
2 * This file is part of DisOrder
3 * Copyright (C) 2008 Richard Kettlewell
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18 * USA
19 */
20/** @file disobedience/users.c
21 * @brief User management for Disobedience
22 */
23
24#include "disobedience.h"
25
26static GtkWidget *users_window;
27static GtkListStore *users_list;
4aa8a0a4 28static GtkTreeSelection *users_selection;
ffc4dbaf 29
54e4791e
RK
30static GtkWidget *users_details_window;
31static GtkWidget *users_details_name;
32static GtkWidget *users_details_email;
33static GtkWidget *users_details_password;
34static GtkWidget *users_details_password2;
35//static GtkWidget *users_details_rights;
36
1bcd69c4
RK
37static const char *users_who, *users_email, *users_rights, *users_password;
38
ffc4dbaf
RK
39static int usercmp(const void *a, const void *b) {
40 return strcmp(*(char **)a, *(char **)b);
41}
42
43static void users_got_list(void attribute((unused)) *v, int nvec, char **vec) {
44 int n;
45 GtkTreeIter iter;
46
47 /* Present users in alphabetical order */
48 qsort(vec, nvec, sizeof (char *), usercmp);
49 /* Set the list contents */
50 gtk_list_store_clear(users_list);
51 for(n = 0; n < nvec; ++n)
52 gtk_list_store_insert_with_values(users_list, &iter, n/*position*/,
53 0, vec[n], /* column 0 */
54 -1); /* no more columns */
55 /* Only show the window when the list is populated */
56 gtk_widget_show_all(users_window);
57}
58
6faa6239
RK
59static char *users_getuser(void) {
60 GtkTreeIter iter;
61 char *who, *c;
62
63 if(gtk_tree_selection_get_selected(users_selection, 0, &iter)) {
64 gtk_tree_model_get(GTK_TREE_MODEL(users_list), &iter,
65 0, &who, -1);
66 if(who) {
67 c = xstrdup(who);
68 g_free(who);
69 return c;
70 }
71 }
72 return 0;
73}
74
95ceca70
RK
75/** @brief Text should be visible */
76#define DETAIL_VISIBLE 1
77
78/** @brief Text should be editable */
79#define DETAIL_EDITABLE 2
80
fbadea5c
RK
81static GtkWidget *users_detail_generic(GtkWidget *table,
82 int *rowp,
83 const char *title,
84 GtkWidget *selector) {
85 const int row = (*rowp)++;
86 GtkWidget *const label = gtk_label_new(title);
87 gtk_misc_set_alignment(GTK_MISC(label), 1, 0);
88 gtk_table_attach(GTK_TABLE(table),
89 label,
90 0, 1, /* left/right_attach */
91 row, row+1, /* top/bottom_attach */
92 GTK_FILL, /* xoptions */
93 0, /* yoptions */
94 1, 1); /* x/ypadding */
95 gtk_table_attach(GTK_TABLE(table),
96 selector,
97 1, 2, /* left/right_attach */
98 row, row + 1, /* top/bottom_attach */
99 GTK_EXPAND|GTK_FILL, /* xoptions */
100 GTK_FILL, /* yoptions */
101 1, 1); /* x/ypadding */
102 return selector;
103}
104
95ceca70
RK
105/** @brief Add a row to the user details table
106 * @param table Containin table widget
107 * @param rowp Pointer to row number, incremented
108 * @param title Label for this row
109 * @param value Initial value or NULL
110 * @param flags Flags word
95ceca70
RK
111 * @return The text entry widget
112 */
113static GtkWidget *users_add_detail(GtkWidget *table,
114 int *rowp,
115 const char *title,
116 const char *value,
117 unsigned flags) {
fbadea5c
RK
118 GtkWidget *entry = gtk_entry_new();
119
95ceca70
RK
120 gtk_entry_set_visibility(GTK_ENTRY(entry),
121 !!(flags & DETAIL_VISIBLE));
122 gtk_editable_set_editable(GTK_EDITABLE(entry),
123 !!(flags & DETAIL_EDITABLE));
124 if(value)
125 gtk_entry_set_text(GTK_ENTRY(entry), value);
fbadea5c
RK
126 return users_detail_generic(table, rowp, title, entry);
127}
128
c49dfc50
RK
129/** @brief Add a checkbox for a right
130 * @param table Containing table
131 * @param rowp Pointer to row number, incremented
132 * @param title Label for this row
133 * @param value Right bit (masked but not necessarily normalized)
134 * @return Checkbox widget
135 */
fbadea5c
RK
136static GtkWidget *users_add_right(GtkWidget *table,
137 int *rowp,
138 const char *title,
139 rights_type value) {
140 GtkWidget *check = gtk_check_button_new();
141
142 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check), !!value);
143 return users_detail_generic(table, rowp, title, check);
95ceca70 144}
c49dfc50
RK
145
146/** @brief Add a checkbox for a three-right group
147 * @param table Containing table
148 * @param rowp Pointer to row number, incremented
149 * @param title Label for this row
150 * @param bits Rights bits (not masked or normalized)
151 * @param mask Mask for this group (must be 7.2^n)
152 * @param checks Where to store triple of check widgets
153 * @return Checkbox widget
154 */
155static void users_add_right_group(GtkWidget *table,
156 int *rowp,
157 const char *title,
158 rights_type bits,
159 rights_type mask,
160 GtkWidget *checks[3]) {
161 GtkWidget *any = gtk_check_button_new_with_label("Any");
162 GtkWidget *mine = gtk_check_button_new_with_label("Own");
163 GtkWidget *random = gtk_check_button_new_with_label("Random");
164 GtkWidget *hbox = gtk_hbox_new(FALSE, 2);
165
166 /* Discard irrelevant bits */
167 bits &= mask;
168 /* Shift down to bits 0-2; the mask is always 3 contiguous bits */
169 bits /= (mask / 7);
170 /* If _ANY is set then the other two are implied; we'll take them out when we
171 * set. */
172 if(bits & 1)
173 bits = 7;
174 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(any), !!(bits & 1));
175 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(mine), !!(bits & 2));
176 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(random), !!(bits & 4));
177 gtk_box_pack_start(GTK_BOX(hbox), any, FALSE, FALSE, 0);
178 gtk_box_pack_start(GTK_BOX(hbox), mine, FALSE, FALSE, 0);
179 gtk_box_pack_start(GTK_BOX(hbox), random, FALSE, FALSE, 0);
180 if(checks) {
181 checks[0] = any;
182 checks[1] = mine;
183 checks[2] = random;
184 }
185 users_detail_generic(table, rowp, title, hbox);
186}
95ceca70 187
54e4791e
RK
188/** @brief Create the user details window
189 * @param title Window title
190 * @param name User name (users_edit()) or NULL (users_add())
191 * @param email Email address
192 * @param rights User rights string
193 * @param password Password
194 *
195 * This is used both by users_add() and users_edit().
196 *
197 * The window contains:
198 * - display or edit fields for the username
199 * - edit fields for the email address
200 * - two hidden edit fields for the password (they must match)
201 * - check boxes for the rights
202 */
203static void users_makedetails(const char *title,
204 const char *name,
205 const char *email,
fbadea5c 206 const char *rights,
54e4791e 207 const char *password) {
95ceca70
RK
208 GtkWidget *table;
209 int row = 0;
fbadea5c 210 rights_type r = 0;
54e4791e
RK
211
212 /* Create the window */
213 users_details_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
214 gtk_widget_set_style(users_details_window, tool_style);
215 g_signal_connect(users_details_window, "destroy",
216 G_CALLBACK(gtk_widget_destroyed), &users_details_window);
217 gtk_window_set_title(GTK_WINDOW(users_details_window), title);
218 gtk_window_set_transient_for(GTK_WINDOW(users_details_window),
219 GTK_WINDOW(users_window));
220 table = gtk_table_new(4, 2, FALSE/*!homogeneous*/);
221
95ceca70
RK
222 users_details_name = users_add_detail(table, &row, "Username", name,
223 (name ? 0 : DETAIL_EDITABLE)
224 |DETAIL_VISIBLE);
54e4791e 225
95ceca70
RK
226 users_details_email = users_add_detail(table, &row, "Email", email,
227 DETAIL_EDITABLE|DETAIL_VISIBLE);
54e4791e 228
95ceca70
RK
229 users_details_password = users_add_detail(table, &row, "Password",
230 password,
231 DETAIL_EDITABLE);
232 users_details_password2 = users_add_detail(table, &row, "Password",
233 password,
234 DETAIL_EDITABLE);
54e4791e 235
fbadea5c
RK
236 parse_rights(rights, &r, 1);
237 users_add_right(table, &row, "Read operations", r & RIGHT_READ);
238 users_add_right(table, &row, "Play track", r & RIGHT_PLAY);
c49dfc50
RK
239 users_add_right_group(table, &row, "Move", r, RIGHT_MOVE__MASK, NULL);
240 users_add_right_group(table, &row, "Remove", r, RIGHT_REMOVE__MASK, NULL);
241 users_add_right_group(table, &row, "Scratch", r, RIGHT_SCRATCH__MASK, NULL);
fbadea5c
RK
242 users_add_right(table, &row, "Set volume", r & RIGHT_VOLUME);
243 users_add_right(table, &row, "Admin operations", r & RIGHT_ADMIN);
244 users_add_right(table, &row, "Rescan", r & RIGHT_RESCAN);
245 users_add_right(table, &row, "Register new users", r & RIGHT_REGISTER);
246 users_add_right(table, &row, "Modify own userinfo", r & RIGHT_USERINFO);
247 users_add_right(table, &row, "Modify track preferences", r & RIGHT_PREFS);
248 users_add_right(table, &row, "Modify global preferences", r & RIGHT_GLOBAL_PREFS);
249 users_add_right(table, &row, "Pause/resume tracks", r & RIGHT_PAUSE);
95ceca70 250
54e4791e
RK
251 gtk_container_add(GTK_CONTAINER(users_details_window), table);
252 gtk_widget_show_all(users_details_window);
253}
254
ffc4dbaf
RK
255static void users_add(GtkButton attribute((unused)) *button,
256 gpointer attribute((unused)) userdata) {
257}
258
4aa8a0a4
RK
259static void users_deleted_error(struct callbackdata attribute((unused)) *cbd,
260 int attribute((unused)) code,
261 const char *msg) {
262 popup_msg(GTK_MESSAGE_ERROR, msg);
263}
264
265static void users_deleted(void *v) {
266 const struct callbackdata *const cbd = v;
267 GtkTreeIter iter;
268 char *who;
269
270 /* Find the user */
271 if(!gtk_tree_model_get_iter_first(GTK_TREE_MODEL(users_list), &iter))
272 return;
273 do {
274 gtk_tree_model_get(GTK_TREE_MODEL(users_list), &iter,
275 0, &who, -1);
276 if(!strcmp(who, cbd->u.user))
277 break;
278 g_free(who);
279 who = 0;
280 } while(gtk_tree_model_iter_next(GTK_TREE_MODEL(users_list), &iter));
281 /* Remove them */
282 gtk_list_store_remove(users_list, &iter);
283 g_free(who);
284}
285
ffc4dbaf
RK
286static void users_delete(GtkButton attribute((unused)) *button,
287 gpointer attribute((unused)) userdata) {
4aa8a0a4
RK
288 GtkWidget *yesno;
289 char *who;
290 int res;
291 struct callbackdata *cbd;
292
6faa6239
RK
293 if(!(who = users_getuser()))
294 return;
295 yesno = gtk_message_dialog_new(GTK_WINDOW(users_window),
296 GTK_DIALOG_MODAL,
297 GTK_MESSAGE_QUESTION,
298 GTK_BUTTONS_YES_NO,
299 "Do you really want to delete user %s?"
300 " This action cannot be undone.", who);
301 res = gtk_dialog_run(GTK_DIALOG(yesno));
302 gtk_widget_destroy(yesno);
303 if(res == GTK_RESPONSE_YES) {
304 cbd = xmalloc(sizeof *cbd);
305 cbd->onerror = users_deleted_error;
306 cbd->u.user = who;
307 disorder_eclient_deluser(client, users_deleted, cbd->u.user, cbd);
4aa8a0a4 308 }
ffc4dbaf
RK
309}
310
1bcd69c4
RK
311static void users_got_email(void attribute((unused)) *v, const char *value) {
312 users_email = value;
313}
314
315static void users_got_rights(void attribute((unused)) *v, const char *value) {
316 users_rights = value;
317}
318
319static void users_got_password(void attribute((unused)) *v, const char *value) {
320 users_password = value;
321 users_makedetails("editing user details",
322 users_who,
323 users_email,
324 users_rights,
325 users_password);
326}
327
ffc4dbaf
RK
328static void users_edit(GtkButton attribute((unused)) *button,
329 gpointer attribute((unused)) userdata) {
1bcd69c4 330 if(!(users_who = users_getuser()))
54e4791e 331 return;
1bcd69c4
RK
332 /* Schedule user lookups for all the properties we know about. This is all a
333 * bit ad-hoc but will do for now. */
334 disorder_eclient_userinfo(client, users_got_email, users_who, "email", 0);
335 disorder_eclient_userinfo(client, users_got_rights, users_who, "rights", 0);
336 disorder_eclient_userinfo(client, users_got_password, users_who, "password", 0);
ffc4dbaf
RK
337}
338
339static const struct button users_buttons[] = {
340 {
341 "Add user",
342 users_add,
343 "Create a new user"
344 },
345 {
346 "Edit user",
347 users_edit,
348 "Edit a user"
349 },
350 {
351 "Delete user",
352 users_delete,
353 "Delete a user"
354 },
355};
356#define NUSERS_BUTTONS (sizeof users_buttons / sizeof *users_buttons)
357
358void manage_users(void) {
359 GtkWidget *tree, *buttons, *hbox;
360 GtkCellRenderer *cr;
361 GtkTreeViewColumn *col;
362
363 /* If the window already exists just raise it */
364 if(users_window) {
365 gtk_window_present(GTK_WINDOW(users_window));
366 return;
367 }
368 /* Create the window */
369 users_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
370 gtk_widget_set_style(users_window, tool_style);
371 g_signal_connect(users_window, "destroy",
372 G_CALLBACK(gtk_widget_destroyed), &users_window);
373 gtk_window_set_title(GTK_WINDOW(users_window), "User Management");
374 /* default size is too small */
375 gtk_window_set_default_size(GTK_WINDOW(users_window), 240, 240);
376
377 /* Create the list of users and populate it asynchronously */
378 users_list = gtk_list_store_new(1, G_TYPE_STRING);
379 disorder_eclient_users(client, users_got_list, 0);
380 /* Create the view */
381 tree = gtk_tree_view_new_with_model(GTK_TREE_MODEL(users_list));
382 /* ...and the renderers for it */
383 cr = gtk_cell_renderer_text_new();
384 col = gtk_tree_view_column_new_with_attributes("Username",
385 cr,
386 "text", 0,
387 NULL);
388 gtk_tree_view_append_column(GTK_TREE_VIEW(tree), col);
4aa8a0a4
RK
389 /* Get the selection for the view and set its mode */
390 users_selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(tree));
391 gtk_tree_selection_set_mode(users_selection, GTK_SELECTION_BROWSE);
ffc4dbaf
RK
392 /* Create the control buttons */
393 buttons = create_buttons_box(users_buttons,
394 NUSERS_BUTTONS,
395 gtk_vbox_new(FALSE, 1));
396 /* Put it all together in an hbox */
397 hbox = gtk_hbox_new(FALSE, 2);
398 gtk_box_pack_start(GTK_BOX(hbox), tree, TRUE/*expand*/, TRUE/*fill*/, 0);
399 gtk_box_pack_start(GTK_BOX(hbox), buttons, FALSE, FALSE, 0);
400 gtk_container_add(GTK_CONTAINER(users_window), hbox);
401}
402
403/*
404Local Variables:
405c-basic-offset:2
406comment-column:40
407fill-column:79
408indent-tabs-mode:nil
409End:
410*/