2 * This file is part of DisOrder
3 * Copyright (C) 2008 Richard Kettlewell
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.
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.
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
20 /** @file disobedience/users.c
21 * @brief User management for Disobedience
23 * The user management window contains:
24 * - a list of all the users
27 * - a user details panel
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.
33 * When you select 'add' a new empty set of details are displayed to be edited.
34 * Again Apply will commit them.
37 #include "disobedience.h"
40 static GtkWidget
*users_window
;
41 static GtkListStore
*users_list
;
42 static GtkTreeSelection
*users_selection
;
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 static const char *users_deferred_select
;
56 static int users_mode
;
61 #define mode(X) do { \
62 users_mode = MODE_##X; \
63 if(0) fprintf(stderr, "%s:%d: %s(): mode -> %s\n", \
64 __FILE__, __LINE__, __FUNCTION__, #X); \
65 users_details_sensitize_all(); \
68 static const char *users_email
, *users_rights
, *users_password
;
70 /** @brief qsort() callback for username comparison */
71 static int usercmp(const void *a
, const void *b
) {
72 return strcmp(*(char **)a
, *(char **)b
);
75 /** @brief Find a user
76 * @param user User to find
77 * @param iter Iterator to point at user
78 * @return 0 on success, -1 if not found
80 static int users_find_user(const char *user
,
85 if(!gtk_tree_model_get_iter_first(GTK_TREE_MODEL(users_list
), iter
))
88 gtk_tree_model_get(GTK_TREE_MODEL(users_list
), iter
,
90 if(!strcmp(who
, user
)) {
95 } while(gtk_tree_model_iter_next(GTK_TREE_MODEL(users_list
), iter
));
99 /** @brief Called with the list of users
102 * - at startup to populate the initial list
103 * - when we add a user
104 * - maybe in the future when we delete a user
106 * If users_deferred_select is set then that user is selected.
108 static void users_got_list(void attribute((unused
)) *v
, int nvec
, char **vec
) {
112 /* Present users in alphabetical order */
113 qsort(vec
, nvec
, sizeof (char *), usercmp
);
114 /* Set the list contents */
115 gtk_list_store_clear(users_list
);
116 for(n
= 0; n
< nvec
; ++n
)
117 gtk_list_store_insert_with_values(users_list
, &iter
, n
/*position*/,
118 0, vec
[n
], /* column 0 */
119 -1); /* no more columns */
120 /* Only show the window when the list is populated */
121 gtk_widget_show_all(users_window
);
122 if(users_deferred_select
) {
123 if(!users_find_user(users_deferred_select
, &iter
))
124 gtk_tree_selection_select_iter(users_selection
, &iter
);
125 users_deferred_select
= 0;
129 /** @brief Text should be visible */
130 #define DETAIL_VISIBLE 1
132 /** @brief Text should be editable */
133 #define DETAIL_EDITABLE 2
135 /** @brief Add a row to the user detail table */
136 static void users_detail_generic(const char *title
,
137 GtkWidget
*selector
) {
138 const int row
= users_details_row
++;
139 GtkWidget
*const label
= gtk_label_new(title
);
140 gtk_misc_set_alignment(GTK_MISC(label
), 1, 0);
141 gtk_table_attach(GTK_TABLE(users_details_table
),
143 0, 1, /* left/right_attach */
144 row
, row
+1, /* top/bottom_attach */
145 GTK_FILL
, /* xoptions */
147 1, 1); /* x/ypadding */
148 gtk_table_attach(GTK_TABLE(users_details_table
),
150 1, 2, /* left/right_attach */
151 row
, row
+ 1, /* top/bottom_attach */
152 GTK_EXPAND
|GTK_FILL
, /* xoptions */
153 GTK_FILL
, /* yoptions */
154 1, 1); /* x/ypadding */
157 /** @brief Add a row to the user details table
158 * @param entryp Where to put GtkEntry
159 * @param title Label for this row
160 * @param value Initial value or NULL
161 * @param flags Flags word
163 static void users_add_detail(GtkWidget
**entryp
,
169 if(!(entry
= *entryp
)) {
170 *entryp
= entry
= gtk_entry_new();
171 users_detail_generic(title
, entry
);
173 gtk_entry_set_visibility(GTK_ENTRY(entry
),
174 !!(flags
& DETAIL_VISIBLE
));
175 gtk_editable_set_editable(GTK_EDITABLE(entry
),
176 !!(flags
& DETAIL_EDITABLE
));
177 gtk_entry_set_text(GTK_ENTRY(entry
), value ? value
: "");
180 /** @brief Add a checkbox for a right
181 * @param title Label for this row
182 * @param value Current value
183 * @param right Right bit
185 static void users_add_right(const char *title
,
189 GtkWidget
**checkp
= &users_details_rights
[leftmost_bit(right
)];
191 if(!(check
= *checkp
)) {
192 *checkp
= check
= gtk_check_button_new();
193 users_detail_generic(title
, check
);
195 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check
), !!(value
& right
));
198 /** @brief Set sensitivity of particular mine/random rights bits */
199 static void users_details_sensitize(rights_type r
) {
200 const int bit
= leftmost_bit(r
);
201 const GtkWidget
*all
= users_details_rights
[bit
];
202 const int sensitive
= (!gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(all
))
203 && users_mode
!= MODE_NONE
);
205 gtk_widget_set_sensitive(users_details_rights
[bit
+ 1], sensitive
);
206 gtk_widget_set_sensitive(users_details_rights
[bit
+ 2], sensitive
);
209 /** @brief Set sensitivity of everything in sight */
210 static void users_details_sensitize_all(void) {
213 for(n
= 0; n
< 32; ++n
)
214 if(users_details_rights
[n
])
215 gtk_widget_set_sensitive(users_details_rights
[n
], users_mode
!= MODE_NONE
);
216 gtk_widget_set_sensitive(users_details_name
, users_mode
!= MODE_NONE
);
217 gtk_widget_set_sensitive(users_details_email
, users_mode
!= MODE_NONE
);
218 gtk_widget_set_sensitive(users_details_password
, users_mode
!= MODE_NONE
);
219 gtk_widget_set_sensitive(users_details_password2
, users_mode
!= MODE_NONE
);
220 users_details_sensitize(RIGHT_MOVE_ANY
);
221 users_details_sensitize(RIGHT_REMOVE_ANY
);
222 users_details_sensitize(RIGHT_SCRATCH_ANY
);
223 gtk_widget_set_sensitive(users_apply_button
, users_mode
!= MODE_NONE
);
224 gtk_widget_set_sensitive(users_delete_button
, !!users_selected
);
227 /** @brief Called when an _ALL widget is toggled
229 * Modifies sensitivity of the corresponding _MINE and _RANDOM widgets. We
230 * just do the lot rather than trying to figure out which one changed,
232 static void users_any_toggled(GtkToggleButton
attribute((unused
)) *togglebutton
,
233 gpointer
attribute((unused
)) user_data
) {
234 users_details_sensitize_all();
237 /** @brief Add a checkbox for a three-right group
238 * @param title Label for this row
239 * @param bits Rights bits (not masked or normalized)
240 * @param mask Mask for this group (must be 7*2^n)
242 static void users_add_right_group(const char *title
,
245 const uint32_t first
= mask
/ 7;
246 const int bit
= leftmost_bit(first
);
247 GtkWidget
**widgets
= &users_details_rights
[bit
], *any
, *mine
, *random
;
250 GtkWidget
*hbox
= gtk_hbox_new(FALSE
, 2);
252 any
= widgets
[0] = gtk_check_button_new_with_label("Any");
253 mine
= widgets
[1] = gtk_check_button_new_with_label("Own");
254 random
= widgets
[2] = gtk_check_button_new_with_label("Random");
255 gtk_box_pack_start(GTK_BOX(hbox
), any
, FALSE
, FALSE
, 0);
256 gtk_box_pack_start(GTK_BOX(hbox
), mine
, FALSE
, FALSE
, 0);
257 gtk_box_pack_start(GTK_BOX(hbox
), random
, FALSE
, FALSE
, 0);
258 users_detail_generic(title
, hbox
);
259 g_signal_connect(any
, "toggled", G_CALLBACK(users_any_toggled
), NULL
);
260 users_details_rights
[bit
] = any
;
261 users_details_rights
[bit
+ 1] = mine
;
262 users_details_rights
[bit
+ 2] = random
;
268 /* Discard irrelevant bits */
270 /* Shift down to bits 0-2; the mask is always 3 contiguous bits */
272 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(any
), !!(bits
& 1));
273 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(mine
), !!(bits
& 2));
274 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(random
), !!(bits
& 4));
277 /** @brief Create or modify the user details table
278 * @param name User name (users_edit()) or NULL (users_add())
279 * @param email Email address
280 * @param rights User rights string
281 * @param password Password
283 static void users_makedetails(const char *name
,
286 const char *password
,
291 /* Create the table if it doesn't already exist */
292 if(!users_details_table
)
293 users_details_table
= gtk_table_new(4, 2, FALSE
/*!homogeneous*/);
295 /* Create or update the widgets */
296 users_add_detail(&users_details_name
, "Username", name
,
297 (DETAIL_EDITABLE
|DETAIL_VISIBLE
) & nameflags
);
299 users_add_detail(&users_details_email
, "Email", email
,
300 (DETAIL_EDITABLE
|DETAIL_VISIBLE
) & flags
);
302 users_add_detail(&users_details_password
, "Password", password
,
303 DETAIL_EDITABLE
& flags
);
304 users_add_detail(&users_details_password2
, "Password", password
,
305 DETAIL_EDITABLE
& flags
);
307 parse_rights(rights
, &r
, 0);
308 users_add_right("Read operations", r
, RIGHT_READ
);
309 users_add_right("Play track", r
, RIGHT_PLAY
);
310 users_add_right_group("Move", r
, RIGHT_MOVE__MASK
);
311 users_add_right_group("Remove", r
, RIGHT_REMOVE__MASK
);
312 users_add_right_group("Scratch", r
, RIGHT_SCRATCH__MASK
);
313 users_add_right("Set volume", r
, RIGHT_VOLUME
);
314 users_add_right("Admin operations", r
, RIGHT_ADMIN
);
315 users_add_right("Rescan", r
, RIGHT_RESCAN
);
316 users_add_right("Register new users", r
, RIGHT_REGISTER
);
317 users_add_right("Modify own userinfo", r
, RIGHT_USERINFO
);
318 users_add_right("Modify track preferences", r
, RIGHT_PREFS
);
319 users_add_right("Modify global preferences", r
, RIGHT_GLOBAL_PREFS
);
320 users_add_right("Pause/resume tracks", r
, RIGHT_PAUSE
);
321 users_details_sensitize_all();
324 /** @brief Called when the 'add' button is pressed */
325 static void users_add(GtkButton
attribute((unused
)) *button
,
326 gpointer
attribute((unused
)) userdata
) {
327 /* Unselect whatever is selected */
328 gtk_tree_selection_unselect_all(users_selection
);
330 /* TODO it would be better to use the server default_rights if there's no
332 users_makedetails("",
334 config
->default_rights
,
336 DETAIL_EDITABLE
|DETAIL_VISIBLE
,
337 DETAIL_EDITABLE
|DETAIL_VISIBLE
);
338 /* Remember we're adding a user */
342 static rights_type
users_get_rights(void) {
346 /* Extract the rights value */
347 for(n
= 0; n
< 32; ++n
) {
348 if(users_details_rights
[n
])
349 if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(users_details_rights
[n
])))
352 /* Throw out redundant bits */
353 if(r
& RIGHT_REMOVE_ANY
)
354 r
&= ~(rights_type
)(RIGHT_REMOVE_MINE
|RIGHT_REMOVE_RANDOM
);
355 if(r
& RIGHT_MOVE_ANY
)
356 r
&= ~(rights_type
)(RIGHT_MOVE_MINE
|RIGHT_MOVE_RANDOM
);
357 if(r
& RIGHT_SCRATCH_ANY
)
358 r
&= ~(rights_type
)(RIGHT_SCRATCH_MINE
|RIGHT_SCRATCH_RANDOM
);
362 /** @brief Called when various things fail */
363 static void users_op_failed(struct callbackdata
attribute((unused
)) *cbd
,
364 int attribute((unused
)) code
,
366 popup_submsg(users_window
, GTK_MESSAGE_ERROR
, msg
);
369 /** @brief Called when a new user has been created */
370 static void users_adduser_completed(void *v
) {
371 struct callbackdata
*cbd
= v
;
373 /* Now the user is created we can go ahead and set the email address */
374 if(*cbd
->u
.edituser
.email
) {
375 struct callbackdata
*ncbd
= xmalloc(sizeof *cbd
);
376 ncbd
->onerror
= users_op_failed
;
377 disorder_eclient_edituser(client
, NULL
, cbd
->u
.edituser
.user
,
378 "email", cbd
->u
.edituser
.email
, ncbd
);
380 /* Refresh the list of users */
381 disorder_eclient_users(client
, users_got_list
, 0);
382 /* We'll select the newly created user */
383 users_deferred_select
= cbd
->u
.edituser
.user
;
386 /** @brief Called if creating a new user fails */
387 static void users_adduser_failed(struct callbackdata
attribute((unused
)) *cbd
,
388 int attribute((unused
)) code
,
390 popup_submsg(users_window
, GTK_MESSAGE_ERROR
, msg
);
391 mode(ADD
); /* Let the user try again */
394 /** @brief Called when the 'Apply' button is pressed */
395 static void users_apply(GtkButton
attribute((unused
)) *button
,
396 gpointer
attribute((unused
)) userdata
) {
397 struct callbackdata
*cbd
;
398 const char *password
;
399 const char *password2
;
407 name
= xstrdup(gtk_entry_get_text(GTK_ENTRY(users_details_name
)));
408 email
= xstrdup(gtk_entry_get_text(GTK_ENTRY(users_details_email
)));
409 password
= xstrdup(gtk_entry_get_text(GTK_ENTRY(users_details_password
)));
410 password2
= xstrdup(gtk_entry_get_text(GTK_ENTRY(users_details_password2
)));
412 /* No username. Really we wanted to desensitize the Apply button when
413 * there's no userame but there doesn't seem to be a signal to detect
414 * changes to the entry text. Consequently we have error messages
416 popup_submsg(users_window
, GTK_MESSAGE_ERROR
, "Must enter a username");
419 if(strcmp(password
, password2
)) {
420 popup_submsg(users_window
, GTK_MESSAGE_ERROR
, "Passwords do not match");
423 if(*email
&& !strchr(email
, '@')) {
424 /* The server will complain about this but we can give a better error
425 * message this way */
426 popup_submsg(users_window
, GTK_MESSAGE_ERROR
, "Invalid email address");
429 cbd
= xmalloc(sizeof *cbd
);
430 cbd
->onerror
= users_adduser_failed
;
431 cbd
->u
.edituser
.user
= name
;
432 cbd
->u
.edituser
.email
= email
;
433 disorder_eclient_adduser(client
, users_adduser_completed
,
436 rights_string(users_get_rights()),
438 /* We switch to no-op mode while creating the user */
442 /* Ugh, can we de-dupe with above? */
443 email
= xstrdup(gtk_entry_get_text(GTK_ENTRY(users_details_email
)));
444 password
= xstrdup(gtk_entry_get_text(GTK_ENTRY(users_details_password
)));
445 password2
= xstrdup(gtk_entry_get_text(GTK_ENTRY(users_details_password2
)));
446 if(strcmp(password
, password2
)) {
447 popup_submsg(users_window
, GTK_MESSAGE_ERROR
, "Passwords do not match");
450 if(*email
&& !strchr(email
, '@')) {
451 popup_submsg(users_window
, GTK_MESSAGE_ERROR
, "Invalid email address");
454 cbd
= xmalloc(sizeof *cbd
);
455 cbd
->onerror
= users_op_failed
;
456 disorder_eclient_edituser(client
, NULL
, users_selected
,
457 "email", email
, cbd
);
458 disorder_eclient_edituser(client
, NULL
, users_selected
,
459 "password", password
, cbd
);
460 disorder_eclient_edituser(client
, NULL
, users_selected
,
461 "rights", rights_string(users_get_rights()), cbd
);
462 /* We remain in edit mode */
467 /** @brief Called when a user has been deleted */
468 static void users_deleted(void *v
) {
469 const struct callbackdata
*const cbd
= v
;
472 if(!users_find_user(cbd
->u
.user
, iter
)) /* Find the user... */
473 gtk_list_store_remove(users_list
, iter
); /* ...and remove them */
476 /** @brief Called when the 'Delete' button is pressed */
477 static void users_delete(GtkButton
attribute((unused
)) *button
,
478 gpointer
attribute((unused
)) userdata
) {
481 struct callbackdata
*cbd
;
485 yesno
= gtk_message_dialog_new(GTK_WINDOW(users_window
),
487 GTK_MESSAGE_QUESTION
,
489 "Do you really want to delete user %s?"
490 " This action cannot be undone.",
492 res
= gtk_dialog_run(GTK_DIALOG(yesno
));
493 gtk_widget_destroy(yesno
);
494 if(res
== GTK_RESPONSE_YES
) {
495 cbd
= xmalloc(sizeof *cbd
);
496 cbd
->onerror
= users_op_failed
;
497 cbd
->u
.user
= users_selected
;
498 disorder_eclient_deluser(client
, users_deleted
, cbd
->u
.user
, cbd
);
502 static void users_got_email(void attribute((unused
)) *v
, const char *value
) {
506 static void users_got_rights(void attribute((unused
)) *v
, const char *value
) {
507 users_rights
= value
;
510 static void users_got_password(void attribute((unused
)) *v
, const char *value
) {
511 users_password
= value
;
512 users_makedetails(users_selected
,
517 DETAIL_EDITABLE
|DETAIL_VISIBLE
);
521 /** @brief Called when the selection MIGHT have changed */
522 static void users_selection_changed(GtkTreeSelection
attribute((unused
)) *treeselection
,
523 gpointer
attribute((unused
)) user_data
) {
525 char *gselected
, *selected
;
527 /* Identify the current selection */
528 if(gtk_tree_selection_get_selected(users_selection
, 0, &iter
)) {
529 gtk_tree_model_get(GTK_TREE_MODEL(users_list
), &iter
,
531 selected
= xstrdup(gselected
);
535 /* Eliminate no-change cases */
536 if(!selected
&& !users_selected
)
538 if(selected
&& users_selected
&& !strcmp(selected
, users_selected
))
540 /* There's been a change; junk the old data and fetch new data in
542 users_selected
= selected
;
543 users_makedetails("", "", "", "",
547 disorder_eclient_userinfo(client
, users_got_email
, users_selected
,
549 disorder_eclient_userinfo(client
, users_got_rights
, users_selected
,
551 disorder_eclient_userinfo(client
, users_got_password
, users_selected
,
554 mode(NONE
); /* not editing *yet* */
557 /** @brief Table of buttons below the user list */
558 static struct button users_buttons
[] = {
572 #define NUSERS_BUTTONS (sizeof users_buttons / sizeof *users_buttons)
574 /** @brief Pop up the user management window */
575 void manage_users(void) {
576 GtkWidget
*tree
, *buttons
, *hbox
, *hbox2
, *vbox
, *vbox2
;
578 GtkTreeViewColumn
*col
;
580 /* If the window already exists just raise it */
582 gtk_window_present(GTK_WINDOW(users_window
));
585 /* Create the window */
586 users_window
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
587 gtk_widget_set_style(users_window
, tool_style
);
588 g_signal_connect(users_window
, "destroy",
589 G_CALLBACK(gtk_widget_destroyed
), &users_window
);
590 gtk_window_set_title(GTK_WINDOW(users_window
), "User Management");
591 /* default size is too small */
592 gtk_window_set_default_size(GTK_WINDOW(users_window
), 240, 240);
594 /* Create the list of users and populate it asynchronously */
595 users_list
= gtk_list_store_new(1, G_TYPE_STRING
);
596 disorder_eclient_users(client
, users_got_list
, 0);
597 /* Create the view */
598 tree
= gtk_tree_view_new_with_model(GTK_TREE_MODEL(users_list
));
599 /* ...and the renderers for it */
600 cr
= gtk_cell_renderer_text_new();
601 col
= gtk_tree_view_column_new_with_attributes("Username",
605 gtk_tree_view_append_column(GTK_TREE_VIEW(tree
), col
);
606 /* Get the selection for the view; set its mode; arrange for a callback when
608 users_selection
= gtk_tree_view_get_selection(GTK_TREE_VIEW(tree
));
609 gtk_tree_selection_set_mode(users_selection
, GTK_SELECTION_BROWSE
);
610 g_signal_connect(users_selection
, "changed",
611 G_CALLBACK(users_selection_changed
), NULL
);
613 /* Create the control buttons */
614 buttons
= create_buttons_box(users_buttons
,
616 gtk_hbox_new(FALSE
, 1));
617 users_delete_button
= users_buttons
[1].widget
;
619 /* Buttons live below the list */
620 vbox
= gtk_vbox_new(FALSE
, 2);
621 gtk_box_pack_start(GTK_BOX(vbox
), tree
, TRUE
/*expand*/, TRUE
/*fill*/, 0);
622 gtk_box_pack_start(GTK_BOX(vbox
), buttons
, FALSE
/*expand*/, FALSE
, 0);
624 /* Create an empty user details table, and put an apply button below it */
625 users_apply_button
= gtk_button_new_from_stock(GTK_STOCK_APPLY
);
626 users_makedetails("", "", "", "",
629 g_signal_connect(users_apply_button
, "clicked",
630 G_CALLBACK(users_apply
), NULL
);
631 hbox2
= gtk_hbox_new(FALSE
, 0);
632 gtk_box_pack_end(GTK_BOX(hbox2
), users_apply_button
,
633 FALSE
/*expand*/, FALSE
, 0);
635 vbox2
= gtk_vbox_new(FALSE
, 0);
636 gtk_box_pack_start(GTK_BOX(vbox2
), users_details_table
,
637 TRUE
/*expand*/, TRUE
/*fill*/, 0);
638 gtk_box_pack_start(GTK_BOX(vbox2
), hbox2
,
639 FALSE
/*expand*/, FALSE
, 0);
641 /* User details are to the right of the list */
642 hbox
= gtk_hbox_new(FALSE
, 2);
643 gtk_box_pack_start(GTK_BOX(hbox
), vbox
, FALSE
/*expand*/, FALSE
, 2);
644 gtk_box_pack_start(GTK_BOX(hbox
), vbox2
, TRUE
/*expand*/, TRUE
/*fill*/, 2);
645 gtk_container_add(GTK_CONTAINER(users_window
), hbox
);