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