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