Automate sensitization when mode changes.
[disorder] / disobedience / users.c
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 * The user management window contains:
24 * - a list of all the users
25 * - an add button
26 * - a delete button
27 * - a user details panel
28 * - an apply button
29 *
30 * When you select a user that user's details are displayed to the right of the
31 * list. Hit the Apply button and any changes are applied.
32 *
33 * When you select 'add' a new empty set of details are displayed to be edited.
34 * Again Apply will commit them.
35 */
36
37 #include "disobedience.h"
38 #include "bits.h"
39
40 static GtkWidget *users_window;
41 static GtkListStore *users_list;
42 static GtkTreeSelection *users_selection;
43
44 static GtkWidget *users_details_table;
45 static GtkWidget *users_apply_button;
46 static GtkWidget *users_delete_button;
47 static GtkWidget *users_details_name;
48 static GtkWidget *users_details_email;
49 static GtkWidget *users_details_password;
50 static GtkWidget *users_details_password2;
51 static GtkWidget *users_details_rights[32];
52 static int users_details_row;
53 static const char *users_selected;
54
55 static int users_mode;
56 #define MODE_NONE 0
57 #define MODE_ADD 1
58 #define MODE_EDIT 2
59
60 #define mode(X) do { \
61 users_mode = MODE_##X; \
62 fprintf(stderr, "%s:%d: %s(): mode -> %s\n", \
63 __FILE__, __LINE__, __FUNCTION__, #X); \
64 users_details_sensitize_all(); \
65 } while(0)
66
67 static const char *users_email, *users_rights, *users_password;
68
69 /** @brief qsort() callback for username comparison */
70 static int usercmp(const void *a, const void *b) {
71 return strcmp(*(char **)a, *(char **)b);
72 }
73
74 /** @brief Called with the list of users
75 *
76 * Currently this is called when the window is created, and is responsible for
77 * showing it. There's currently no facility for refreshing the list, which
78 * hopefuly would preserve the select user (if any).
79 */
80 static void users_got_list(void attribute((unused)) *v, int nvec, char **vec) {
81 int n;
82 GtkTreeIter iter;
83
84 /* Present users in alphabetical order */
85 qsort(vec, nvec, sizeof (char *), usercmp);
86 /* Set the list contents */
87 gtk_list_store_clear(users_list);
88 for(n = 0; n < nvec; ++n)
89 gtk_list_store_insert_with_values(users_list, &iter, n/*position*/,
90 0, vec[n], /* column 0 */
91 -1); /* no more columns */
92 /* Only show the window when the list is populated */
93 gtk_widget_show_all(users_window);
94 }
95
96 /** @brief Text should be visible */
97 #define DETAIL_VISIBLE 1
98
99 /** @brief Text should be editable */
100 #define DETAIL_EDITABLE 2
101
102 /** @brief Add a row to the user detail table */
103 static void users_detail_generic(const char *title,
104 GtkWidget *selector) {
105 const int row = users_details_row++;
106 GtkWidget *const label = gtk_label_new(title);
107 gtk_misc_set_alignment(GTK_MISC(label), 1, 0);
108 gtk_table_attach(GTK_TABLE(users_details_table),
109 label,
110 0, 1, /* left/right_attach */
111 row, row+1, /* top/bottom_attach */
112 GTK_FILL, /* xoptions */
113 0, /* yoptions */
114 1, 1); /* x/ypadding */
115 gtk_table_attach(GTK_TABLE(users_details_table),
116 selector,
117 1, 2, /* left/right_attach */
118 row, row + 1, /* top/bottom_attach */
119 GTK_EXPAND|GTK_FILL, /* xoptions */
120 GTK_FILL, /* yoptions */
121 1, 1); /* x/ypadding */
122 }
123
124 /** @brief Add a row to the user details table
125 * @param entryp Where to put GtkEntry
126 * @param title Label for this row
127 * @param value Initial value or NULL
128 * @param flags Flags word
129 */
130 static void users_add_detail(GtkWidget **entryp,
131 const char *title,
132 const char *value,
133 unsigned flags) {
134 GtkWidget *entry;
135
136 if(!(entry = *entryp)) {
137 *entryp = entry = gtk_entry_new();
138 users_detail_generic(title, entry);
139 }
140 gtk_entry_set_visibility(GTK_ENTRY(entry),
141 !!(flags & DETAIL_VISIBLE));
142 gtk_editable_set_editable(GTK_EDITABLE(entry),
143 !!(flags & DETAIL_EDITABLE));
144 gtk_entry_set_text(GTK_ENTRY(entry), value ? value : "");
145 }
146
147 /** @brief Add a checkbox for a right
148 * @param title Label for this row
149 * @param value Current value
150 * @param right Right bit
151 */
152 static void users_add_right(const char *title,
153 rights_type value,
154 rights_type right) {
155 GtkWidget *check;
156 GtkWidget **checkp = &users_details_rights[leftmost_bit(right)];
157
158 if(!(check = *checkp)) {
159 *checkp = check = gtk_check_button_new();
160 users_detail_generic(title, check);
161 }
162 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check), !!(value & right));
163 }
164
165 /** @brief Set sensitivity of particular mine/random rights bits */
166 static void users_details_sensitize(rights_type r) {
167 const int bit = leftmost_bit(r);
168 const GtkWidget *all = users_details_rights[bit];
169 const int sensitive = (!gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(all))
170 && users_mode != MODE_ADD);
171
172 gtk_widget_set_sensitive(users_details_rights[bit + 1], sensitive);
173 gtk_widget_set_sensitive(users_details_rights[bit + 2], sensitive);
174 }
175
176 /** @brief Set sensitivity of everything in sight */
177 static void users_details_sensitize_all(void) {
178 int n;
179
180 for(n = 0; n < 32; ++n)
181 if(users_details_rights[n])
182 gtk_widget_set_sensitive(users_details_rights[n], users_mode != MODE_NONE);
183 gtk_widget_set_sensitive(users_details_name, users_mode != MODE_NONE);
184 gtk_widget_set_sensitive(users_details_email, users_mode != MODE_NONE);
185 gtk_widget_set_sensitive(users_details_password, users_mode != MODE_NONE);
186 gtk_widget_set_sensitive(users_details_password2, users_mode != MODE_NONE);
187 users_details_sensitize(RIGHT_MOVE_ANY);
188 users_details_sensitize(RIGHT_REMOVE_ANY);
189 users_details_sensitize(RIGHT_SCRATCH_ANY);
190 gtk_widget_set_sensitive(users_apply_button, users_mode != MODE_NONE);
191 gtk_widget_set_sensitive(users_delete_button, !!users_selected);
192 }
193
194 /** @brief Called when an _ALL widget is toggled
195 *
196 * Modifies sensitivity of the corresponding _MINE and _RANDOM widgets. We
197 * just do the lot rather than trying to figure out which one changed,
198 */
199 static void users_any_toggled(GtkToggleButton attribute((unused)) *togglebutton,
200 gpointer attribute((unused)) user_data) {
201 users_details_sensitize_all();
202 }
203
204 /** @brief Add a checkbox for a three-right group
205 * @param title Label for this row
206 * @param bits Rights bits (not masked or normalized)
207 * @param mask Mask for this group (must be 7*2^n)
208 */
209 static void users_add_right_group(const char *title,
210 rights_type bits,
211 rights_type mask) {
212 const uint32_t first = mask / 7;
213 const int bit = leftmost_bit(first);
214 GtkWidget **widgets = &users_details_rights[bit], *any, *mine, *random;
215
216 if(!*widgets) {
217 GtkWidget *hbox = gtk_hbox_new(FALSE, 2);
218
219 any = widgets[0] = gtk_check_button_new_with_label("Any");
220 mine = widgets[1] = gtk_check_button_new_with_label("Own");
221 random = widgets[2] = gtk_check_button_new_with_label("Random");
222 gtk_box_pack_start(GTK_BOX(hbox), any, FALSE, FALSE, 0);
223 gtk_box_pack_start(GTK_BOX(hbox), mine, FALSE, FALSE, 0);
224 gtk_box_pack_start(GTK_BOX(hbox), random, FALSE, FALSE, 0);
225 users_detail_generic(title, hbox);
226 g_signal_connect(any, "toggled", G_CALLBACK(users_any_toggled), NULL);
227 users_details_rights[bit] = any;
228 users_details_rights[bit + 1] = mine;
229 users_details_rights[bit + 2] = random;
230 } else {
231 any = widgets[0];
232 mine = widgets[1];
233 random = widgets[2];
234 }
235 /* Discard irrelevant bits */
236 bits &= mask;
237 /* Shift down to bits 0-2; the mask is always 3 contiguous bits */
238 bits >>= bit;
239 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(any), !!(bits & 1));
240 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(mine), !!(bits & 2));
241 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(random), !!(bits & 4));
242 }
243
244 /** @brief Create or modify the user details table
245 * @param name User name (users_edit()) or NULL (users_add())
246 * @param email Email address
247 * @param rights User rights string
248 * @param password Password
249 */
250 static void users_makedetails(const char *name,
251 const char *email,
252 const char *rights,
253 const char *password,
254 unsigned nameflags,
255 unsigned flags) {
256 rights_type r = 0;
257
258 /* Create the table if it doesn't already exist */
259 if(!users_details_table)
260 users_details_table = gtk_table_new(4, 2, FALSE/*!homogeneous*/);
261
262 /* Create or update the widgets */
263 users_add_detail(&users_details_name, "Username", name,
264 (DETAIL_EDITABLE|DETAIL_VISIBLE) & nameflags);
265
266 users_add_detail(&users_details_email, "Email", email,
267 (DETAIL_EDITABLE|DETAIL_VISIBLE) & flags);
268
269 users_add_detail(&users_details_password, "Password", password,
270 DETAIL_EDITABLE & flags);
271 users_add_detail(&users_details_password2, "Password", password,
272 DETAIL_EDITABLE & flags);
273
274 parse_rights(rights, &r, 0);
275 users_add_right("Read operations", r, RIGHT_READ);
276 users_add_right("Play track", r, RIGHT_PLAY);
277 users_add_right_group("Move", r, RIGHT_MOVE__MASK);
278 users_add_right_group("Remove", r, RIGHT_REMOVE__MASK);
279 users_add_right_group("Scratch", r, RIGHT_SCRATCH__MASK);
280 users_add_right("Set volume", r, RIGHT_VOLUME);
281 users_add_right("Admin operations", r, RIGHT_ADMIN);
282 users_add_right("Rescan", r, RIGHT_RESCAN);
283 users_add_right("Register new users", r, RIGHT_REGISTER);
284 users_add_right("Modify own userinfo", r, RIGHT_USERINFO);
285 users_add_right("Modify track preferences", r, RIGHT_PREFS);
286 users_add_right("Modify global preferences", r, RIGHT_GLOBAL_PREFS);
287 users_add_right("Pause/resume tracks", r, RIGHT_PAUSE);
288 users_details_sensitize_all();
289 }
290
291 /** @brief Called when the 'add' button is pressed */
292 static void users_add(GtkButton attribute((unused)) *button,
293 gpointer attribute((unused)) userdata) {
294 /* Unselect whatever is selected */
295 gtk_tree_selection_unselect_all(users_selection);
296 /* Reset the form */
297 /* TODO it would be better to use the server default_rights if there's no
298 * client setting. */
299 users_makedetails("",
300 "",
301 config->default_rights,
302 "",
303 DETAIL_EDITABLE|DETAIL_VISIBLE,
304 DETAIL_EDITABLE|DETAIL_VISIBLE);
305 /* Remember we're adding a user */
306 mode(ADD);
307 }
308
309 /** @brief Called when the 'Apply' button is pressed */
310 static void users_apply(GtkButton attribute((unused)) *button,
311 gpointer attribute((unused)) userdata) {
312 switch(users_mode) {
313 case MODE_NONE:
314 return;
315 case MODE_ADD:
316 if(!*gtk_entry_get_text(GTK_ENTRY(users_details_name))) {
317 /* No username. Really we wanted to desensitize the Apply button when
318 * there's no userame but there doesn't seem to be a signal to detect
319 * changes to the entry text. Consequently we have error messages
320 * instead. */
321 popup_submsg(users_window, GTK_MESSAGE_ERROR, "Must enter a username");
322 return;
323 }
324 if(strcmp(gtk_entry_get_text(GTK_ENTRY(users_details_password)),
325 gtk_entry_get_text(GTK_ENTRY(users_details_password2)))) {
326 popup_submsg(users_window, GTK_MESSAGE_ERROR, "Passwords do not match");
327 return;
328 }
329 /* TODO create user */
330 mode(NONE);
331 popup_submsg(users_window, GTK_MESSAGE_INFO, "Would create user");
332 break;
333 case MODE_EDIT:
334 if(strcmp(gtk_entry_get_text(GTK_ENTRY(users_details_password)),
335 gtk_entry_get_text(GTK_ENTRY(users_details_password2)))) {
336 popup_submsg(users_window, GTK_MESSAGE_ERROR, "Passwords do not match");
337 return;
338 }
339 /* TODO */
340 mode(NONE);
341 popup_submsg(users_window, GTK_MESSAGE_INFO, "Would edit user");
342 break;
343 }
344 }
345
346 /** @brief Called when user deletion goes wrong */
347 static void users_deleted_error(struct callbackdata attribute((unused)) *cbd,
348 int attribute((unused)) code,
349 const char *msg) {
350 popup_submsg(users_window, GTK_MESSAGE_ERROR, msg);
351 }
352
353 /** @brief Called when a user has been deleted */
354 static void users_deleted(void *v) {
355 const struct callbackdata *const cbd = v;
356 GtkTreeIter iter;
357 char *who;
358
359 /* Find the user */
360 if(!gtk_tree_model_get_iter_first(GTK_TREE_MODEL(users_list), &iter))
361 return;
362 do {
363 gtk_tree_model_get(GTK_TREE_MODEL(users_list), &iter,
364 0, &who, -1);
365 if(!strcmp(who, cbd->u.user))
366 break;
367 g_free(who);
368 who = 0;
369 } while(gtk_tree_model_iter_next(GTK_TREE_MODEL(users_list), &iter));
370 /* Remove them */
371 gtk_list_store_remove(users_list, &iter);
372 g_free(who);
373 }
374
375 /** @brief Called when the 'Delete' button is pressed */
376 static void users_delete(GtkButton attribute((unused)) *button,
377 gpointer attribute((unused)) userdata) {
378 GtkWidget *yesno;
379 int res;
380 struct callbackdata *cbd;
381
382 if(!users_selected)
383 return;
384 yesno = gtk_message_dialog_new(GTK_WINDOW(users_window),
385 GTK_DIALOG_MODAL,
386 GTK_MESSAGE_QUESTION,
387 GTK_BUTTONS_YES_NO,
388 "Do you really want to delete user %s?"
389 " This action cannot be undone.",
390 users_selected);
391 res = gtk_dialog_run(GTK_DIALOG(yesno));
392 gtk_widget_destroy(yesno);
393 if(res == GTK_RESPONSE_YES) {
394 cbd = xmalloc(sizeof *cbd);
395 cbd->onerror = users_deleted_error;
396 cbd->u.user = users_selected;
397 disorder_eclient_deluser(client, users_deleted, cbd->u.user, cbd);
398 }
399 }
400
401 static void users_got_email(void attribute((unused)) *v, const char *value) {
402 users_email = value;
403 }
404
405 static void users_got_rights(void attribute((unused)) *v, const char *value) {
406 users_rights = value;
407 }
408
409 static void users_got_password(void attribute((unused)) *v, const char *value) {
410 users_password = value;
411 users_makedetails(users_selected,
412 users_email,
413 users_rights,
414 users_password,
415 DETAIL_VISIBLE,
416 DETAIL_EDITABLE|DETAIL_VISIBLE);
417 mode(EDIT);
418 }
419
420 /** @brief Called when the selection MIGHT have changed */
421 static void users_selection_changed(GtkTreeSelection attribute((unused)) *treeselection,
422 gpointer attribute((unused)) user_data) {
423 GtkTreeIter iter;
424 char *gselected, *selected;
425
426 /* Identify the current selection */
427 if(gtk_tree_selection_get_selected(users_selection, 0, &iter)) {
428 gtk_tree_model_get(GTK_TREE_MODEL(users_list), &iter,
429 0, &gselected, -1);
430 selected = xstrdup(gselected);
431 g_free(gselected);
432 } else
433 selected = 0;
434 /* Eliminate no-change cases */
435 if(!selected && !users_selected)
436 return;
437 if(selected && users_selected && !strcmp(selected, users_selected))
438 return;
439 /* There's been a change; junk the old data and fetch new data in
440 * background. */
441 users_selected = selected;
442 users_makedetails("", "", "", "",
443 DETAIL_VISIBLE,
444 DETAIL_VISIBLE);
445 if(users_selected) {
446 disorder_eclient_userinfo(client, users_got_email, users_selected,
447 "email", 0);
448 disorder_eclient_userinfo(client, users_got_rights, users_selected,
449 "rights", 0);
450 disorder_eclient_userinfo(client, users_got_password, users_selected,
451 "password", 0);
452 }
453 mode(NONE); /* not editing *yet* */
454 }
455
456 /** @brief Table of buttons below the user list */
457 static struct button users_buttons[] = {
458 {
459 "Add user",
460 users_add,
461 "Create a new user",
462 0
463 },
464 {
465 "Delete user",
466 users_delete,
467 "Delete a user",
468 0
469 },
470 };
471 #define NUSERS_BUTTONS (sizeof users_buttons / sizeof *users_buttons)
472
473 /** @brief Pop up the user management window */
474 void manage_users(void) {
475 GtkWidget *tree, *buttons, *hbox, *vbox, *vbox2;
476 GtkCellRenderer *cr;
477 GtkTreeViewColumn *col;
478
479 /* If the window already exists just raise it */
480 if(users_window) {
481 gtk_window_present(GTK_WINDOW(users_window));
482 return;
483 }
484 /* Create the window */
485 users_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
486 gtk_widget_set_style(users_window, tool_style);
487 g_signal_connect(users_window, "destroy",
488 G_CALLBACK(gtk_widget_destroyed), &users_window);
489 gtk_window_set_title(GTK_WINDOW(users_window), "User Management");
490 /* default size is too small */
491 gtk_window_set_default_size(GTK_WINDOW(users_window), 240, 240);
492
493 /* Create the list of users and populate it asynchronously */
494 users_list = gtk_list_store_new(1, G_TYPE_STRING);
495 disorder_eclient_users(client, users_got_list, 0);
496 /* Create the view */
497 tree = gtk_tree_view_new_with_model(GTK_TREE_MODEL(users_list));
498 /* ...and the renderers for it */
499 cr = gtk_cell_renderer_text_new();
500 col = gtk_tree_view_column_new_with_attributes("Username",
501 cr,
502 "text", 0,
503 NULL);
504 gtk_tree_view_append_column(GTK_TREE_VIEW(tree), col);
505 /* Get the selection for the view; set its mode; arrange for a callback when
506 * it changes */
507 users_selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(tree));
508 gtk_tree_selection_set_mode(users_selection, GTK_SELECTION_BROWSE);
509 g_signal_connect(users_selection, "changed",
510 G_CALLBACK(users_selection_changed), NULL);
511
512 /* Create the control buttons */
513 buttons = create_buttons_box(users_buttons,
514 NUSERS_BUTTONS,
515 gtk_hbox_new(FALSE, 1));
516 users_delete_button = users_buttons[1].widget;
517
518 /* Buttons live below the list */
519 vbox = gtk_vbox_new(FALSE, 2);
520 gtk_box_pack_start(GTK_BOX(vbox), tree, TRUE/*expand*/, TRUE/*fill*/, 0);
521 gtk_box_pack_start(GTK_BOX(vbox), buttons, FALSE/*expand*/, FALSE, 0);
522
523 /* Create an empty user details table, and put an apply button below it */
524 users_apply_button = gtk_button_new_from_stock(GTK_STOCK_APPLY);
525 users_makedetails("", "", "", "",
526 DETAIL_VISIBLE,
527 DETAIL_VISIBLE);
528 /* TODO apply button is much too wide right now... */
529 g_signal_connect(users_apply_button, "clicked",
530 G_CALLBACK(users_apply), NULL);
531 vbox2 = gtk_vbox_new(FALSE, 2);
532 gtk_box_pack_start(GTK_BOX(vbox2), users_details_table,
533 TRUE/*expand*/, TRUE/*fill*/, 0);
534 gtk_box_pack_start(GTK_BOX(vbox2), users_apply_button,
535 FALSE/*expand*/, FALSE, 0);
536
537 /* User details are to the right of the list */
538 hbox = gtk_hbox_new(FALSE, 2);
539 gtk_box_pack_start(GTK_BOX(hbox), vbox, FALSE/*expand*/, FALSE, 0);
540 gtk_box_pack_start(GTK_BOX(hbox), vbox2, TRUE/*expand*/, TRUE/*fill*/, 0);
541 gtk_container_add(GTK_CONTAINER(users_window), hbox);
542 }
543
544 /*
545 Local Variables:
546 c-basic-offset:2
547 comment-column:40
548 fill-column:79
549 indent-tabs-mode:nil
550 End:
551 */