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
24 #include "disobedience.h"
27 static GtkWidget
*users_window
;
28 static GtkListStore
*users_list
;
29 static GtkTreeSelection
*users_selection
;
31 static GtkWidget
*users_details_window
;
32 static GtkWidget
*users_details_name
;
33 static GtkWidget
*users_details_email
;
34 static GtkWidget
*users_details_password
;
35 static GtkWidget
*users_details_password2
;
36 static GtkWidget
*users_details_rights
[32];
38 static const char *users_who
, *users_email
, *users_rights
, *users_password
;
40 /** @brief qsort() callback for username comparison */
41 static int usercmp(const void *a
, const void *b
) {
42 return strcmp(*(char **)a
, *(char **)b
);
45 /** @brief Called with the list of users */
46 static void users_got_list(void attribute((unused
)) *v
, int nvec
, char **vec
) {
50 /* Present users in alphabetical order */
51 qsort(vec
, nvec
, sizeof (char *), usercmp
);
52 /* Set the list contents */
53 gtk_list_store_clear(users_list
);
54 for(n
= 0; n
< nvec
; ++n
)
55 gtk_list_store_insert_with_values(users_list
, &iter
, n
/*position*/,
56 0, vec
[n
], /* column 0 */
57 -1); /* no more columns */
58 /* Only show the window when the list is populated */
59 gtk_widget_show_all(users_window
);
62 /** @brief Pick the selected user from the list */
63 static char *users_getuser(void) {
67 if(gtk_tree_selection_get_selected(users_selection
, 0, &iter
)) {
68 gtk_tree_model_get(GTK_TREE_MODEL(users_list
), &iter
,
79 /** @brief Text should be visible */
80 #define DETAIL_VISIBLE 1
82 /** @brief Text should be editable */
83 #define DETAIL_EDITABLE 2
85 /** @brief Add a row to the user detail table */
86 static GtkWidget
*users_detail_generic(GtkWidget
*table
,
89 GtkWidget
*selector
) {
90 const int row
= (*rowp
)++;
91 GtkWidget
*const label
= gtk_label_new(title
);
92 gtk_misc_set_alignment(GTK_MISC(label
), 1, 0);
93 gtk_table_attach(GTK_TABLE(table
),
95 0, 1, /* left/right_attach */
96 row
, row
+1, /* top/bottom_attach */
97 GTK_FILL
, /* xoptions */
99 1, 1); /* x/ypadding */
100 gtk_table_attach(GTK_TABLE(table
),
102 1, 2, /* left/right_attach */
103 row
, row
+ 1, /* top/bottom_attach */
104 GTK_EXPAND
|GTK_FILL
, /* xoptions */
105 GTK_FILL
, /* yoptions */
106 1, 1); /* x/ypadding */
110 /** @brief Add a row to the user details table
111 * @param table Containin table widget
112 * @param rowp Pointer to row number, incremented
113 * @param title Label for this row
114 * @param value Initial value or NULL
115 * @param flags Flags word
116 * @return The text entry widget
118 static GtkWidget
*users_add_detail(GtkWidget
*table
,
123 GtkWidget
*entry
= gtk_entry_new();
125 gtk_entry_set_visibility(GTK_ENTRY(entry
),
126 !!(flags
& DETAIL_VISIBLE
));
127 gtk_editable_set_editable(GTK_EDITABLE(entry
),
128 !!(flags
& DETAIL_EDITABLE
));
130 gtk_entry_set_text(GTK_ENTRY(entry
), value
);
131 return users_detail_generic(table
, rowp
, title
, entry
);
134 /** @brief Add a checkbox for a right
135 * @param table Containing table
136 * @param rowp Pointer to row number, incremented
137 * @param title Label for this row
138 * @param value Right bit (masked but not necessarily normalized)
140 static void users_add_right(GtkWidget
*table
,
144 GtkWidget
*check
= gtk_check_button_new();
146 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check
), !!value
);
147 users_details_rights
[leftmost_bit(value
)] = check
;
148 users_detail_generic(table
, rowp
, title
, check
);
151 /** @brief Set sensitivity of particular mine/random rights bits */
152 static void users_details_sensitize(rights_type r
) {
153 const int bit
= leftmost_bit(r
);
154 const GtkWidget
*all
= users_details_rights
[bit
];
155 const int sensitive
= !gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(all
));
157 gtk_widget_set_sensitive(users_details_rights
[bit
+ 1], sensitive
);
158 gtk_widget_set_sensitive(users_details_rights
[bit
+ 2], sensitive
);
161 /** @brief Set sensitivity of all mine/random rights bits */
162 static void users_details_sensitize_all(void) {
163 users_details_sensitize(RIGHT_MOVE_ANY
);
164 users_details_sensitize(RIGHT_REMOVE_ANY
);
165 users_details_sensitize(RIGHT_SCRATCH_ANY
);
168 /** @brief Called when an _ALL widget is toggled
170 * Modifies sensitivity of the corresponding _MINE and _RANDOM widgets. We
171 * just do the lot rather than trying to figure out which one changed,
173 static void users_any_toggled(GtkToggleButton
attribute((unused
)) *togglebutton
,
174 gpointer
attribute((unused
)) user_data
) {
175 users_details_sensitize_all();
178 /** @brief Add a checkbox for a three-right group
179 * @param table Containing table
180 * @param rowp Pointer to row number, incremented
181 * @param title Label for this row
182 * @param bits Rights bits (not masked or normalized)
183 * @param mask Mask for this group (must be 7.2^n)
185 static void users_add_right_group(GtkWidget
*table
,
190 GtkWidget
*any
= gtk_check_button_new_with_label("Any");
191 GtkWidget
*mine
= gtk_check_button_new_with_label("Own");
192 GtkWidget
*random
= gtk_check_button_new_with_label("Random");
193 GtkWidget
*hbox
= gtk_hbox_new(FALSE
, 2);
194 const uint32_t first
= mask
/ 7;
195 const int bit
= leftmost_bit(first
);
197 /* Discard irrelevant bits */
199 /* Shift down to bits 0-2; the mask is always 3 contiguous bits */
201 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(any
), !!(bits
& 1));
202 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(mine
), !!(bits
& 2));
203 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(random
), !!(bits
& 4));
204 gtk_box_pack_start(GTK_BOX(hbox
), any
, FALSE
, FALSE
, 0);
205 gtk_box_pack_start(GTK_BOX(hbox
), mine
, FALSE
, FALSE
, 0);
206 gtk_box_pack_start(GTK_BOX(hbox
), random
, FALSE
, FALSE
, 0);
207 users_details_rights
[bit
] = any
;
208 users_details_rights
[bit
+ 1] = mine
;
209 users_details_rights
[bit
+ 2] = random
;
210 users_detail_generic(table
, rowp
, title
, hbox
);
211 g_signal_connect(any
, "toggled", G_CALLBACK(users_any_toggled
), NULL
);
214 /** @brief Create the user details window
215 * @param title Window title
216 * @param name User name (users_edit()) or NULL (users_add())
217 * @param email Email address
218 * @param rights User rights string
219 * @param password Password
221 * This is used both by users_add() and users_edit().
223 * The window contains:
224 * - display or edit fields for the username
225 * - edit fields for the email address
226 * - two hidden edit fields for the password (they must match)
227 * - check boxes for the rights
229 static void users_makedetails(const char *title
,
233 const char *password
) {
238 /* Create the window */
239 users_details_window
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
240 gtk_widget_set_style(users_details_window
, tool_style
);
241 g_signal_connect(users_details_window
, "destroy",
242 G_CALLBACK(gtk_widget_destroyed
), &users_details_window
);
243 gtk_window_set_title(GTK_WINDOW(users_details_window
), title
);
244 gtk_window_set_transient_for(GTK_WINDOW(users_details_window
),
245 GTK_WINDOW(users_window
));
246 table
= gtk_table_new(4, 2, FALSE
/*!homogeneous*/);
248 users_details_name
= users_add_detail(table
, &row
, "Username", name
,
249 (name ?
0 : DETAIL_EDITABLE
)
252 users_details_email
= users_add_detail(table
, &row
, "Email", email
,
253 DETAIL_EDITABLE
|DETAIL_VISIBLE
);
255 users_details_password
= users_add_detail(table
, &row
, "Password",
258 users_details_password2
= users_add_detail(table
, &row
, "Password",
262 parse_rights(rights
, &r
, 1);
263 users_add_right(table
, &row
, "Read operations", r
& RIGHT_READ
);
264 users_add_right(table
, &row
, "Play track", r
& RIGHT_PLAY
);
265 users_add_right_group(table
, &row
, "Move", r
, RIGHT_MOVE__MASK
);
266 users_add_right_group(table
, &row
, "Remove", r
, RIGHT_REMOVE__MASK
);
267 users_add_right_group(table
, &row
, "Scratch", r
, RIGHT_SCRATCH__MASK
);
268 users_add_right(table
, &row
, "Set volume", r
& RIGHT_VOLUME
);
269 users_add_right(table
, &row
, "Admin operations", r
& RIGHT_ADMIN
);
270 users_add_right(table
, &row
, "Rescan", r
& RIGHT_RESCAN
);
271 users_add_right(table
, &row
, "Register new users", r
& RIGHT_REGISTER
);
272 users_add_right(table
, &row
, "Modify own userinfo", r
& RIGHT_USERINFO
);
273 users_add_right(table
, &row
, "Modify track preferences", r
& RIGHT_PREFS
);
274 users_add_right(table
, &row
, "Modify global preferences", r
& RIGHT_GLOBAL_PREFS
);
275 users_add_right(table
, &row
, "Pause/resume tracks", r
& RIGHT_PAUSE
);
276 users_details_sensitize_all();
278 gtk_container_add(GTK_CONTAINER(users_details_window
), table
);
279 gtk_widget_show_all(users_details_window
);
282 /** @brief Called when the 'add' button is pressed */
283 static void users_add(GtkButton
attribute((unused
)) *button
,
284 gpointer
attribute((unused
)) userdata
) {
288 /** @brief Called when user deletion goes wrong */
289 static void users_deleted_error(struct callbackdata
attribute((unused
)) *cbd
,
290 int attribute((unused
)) code
,
292 popup_msg(GTK_MESSAGE_ERROR
, msg
);
295 /** @brief Called when a user has been deleted */
296 static void users_deleted(void *v
) {
297 const struct callbackdata
*const cbd
= v
;
302 if(!gtk_tree_model_get_iter_first(GTK_TREE_MODEL(users_list
), &iter
))
305 gtk_tree_model_get(GTK_TREE_MODEL(users_list
), &iter
,
307 if(!strcmp(who
, cbd
->u
.user
))
311 } while(gtk_tree_model_iter_next(GTK_TREE_MODEL(users_list
), &iter
));
313 gtk_list_store_remove(users_list
, &iter
);
317 /** @brief Called when the 'Delete' button is pressed */
318 static void users_delete(GtkButton
attribute((unused
)) *button
,
319 gpointer
attribute((unused
)) userdata
) {
323 struct callbackdata
*cbd
;
325 if(!(who
= users_getuser()))
327 yesno
= gtk_message_dialog_new(GTK_WINDOW(users_window
),
329 GTK_MESSAGE_QUESTION
,
331 "Do you really want to delete user %s?"
332 " This action cannot be undone.", who
);
333 res
= gtk_dialog_run(GTK_DIALOG(yesno
));
334 gtk_widget_destroy(yesno
);
335 if(res
== GTK_RESPONSE_YES
) {
336 cbd
= xmalloc(sizeof *cbd
);
337 cbd
->onerror
= users_deleted_error
;
339 disorder_eclient_deluser(client
, users_deleted
, cbd
->u
.user
, cbd
);
343 static void users_got_email(void attribute((unused
)) *v
, const char *value
) {
347 static void users_got_rights(void attribute((unused
)) *v
, const char *value
) {
348 users_rights
= value
;
351 static void users_got_password(void attribute((unused
)) *v
, const char *value
) {
352 users_password
= value
;
353 users_makedetails("editing user details",
360 /** @brief Called when the 'Edit' button is pressed */
361 static void users_edit(GtkButton
attribute((unused
)) *button
,
362 gpointer
attribute((unused
)) userdata
) {
363 if(!(users_who
= users_getuser()))
365 /* Schedule user lookups for all the properties we know about. This is all a
366 * bit ad-hoc but will do for now. */
367 disorder_eclient_userinfo(client
, users_got_email
, users_who
, "email", 0);
368 disorder_eclient_userinfo(client
, users_got_rights
, users_who
, "rights", 0);
369 disorder_eclient_userinfo(client
, users_got_password
, users_who
, "password", 0);
372 /** @brief Table of buttons for the user window */
373 static const struct button users_buttons
[] = {
390 #define NUSERS_BUTTONS (sizeof users_buttons / sizeof *users_buttons)
392 /** @brief Pop up the user management window */
393 void manage_users(void) {
394 GtkWidget
*tree
, *buttons
, *hbox
;
396 GtkTreeViewColumn
*col
;
398 /* If the window already exists just raise it */
400 gtk_window_present(GTK_WINDOW(users_window
));
403 /* Create the window */
404 users_window
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
405 gtk_widget_set_style(users_window
, tool_style
);
406 g_signal_connect(users_window
, "destroy",
407 G_CALLBACK(gtk_widget_destroyed
), &users_window
);
408 gtk_window_set_title(GTK_WINDOW(users_window
), "User Management");
409 /* default size is too small */
410 gtk_window_set_default_size(GTK_WINDOW(users_window
), 240, 240);
412 /* Create the list of users and populate it asynchronously */
413 users_list
= gtk_list_store_new(1, G_TYPE_STRING
);
414 disorder_eclient_users(client
, users_got_list
, 0);
415 /* Create the view */
416 tree
= gtk_tree_view_new_with_model(GTK_TREE_MODEL(users_list
));
417 /* ...and the renderers for it */
418 cr
= gtk_cell_renderer_text_new();
419 col
= gtk_tree_view_column_new_with_attributes("Username",
423 gtk_tree_view_append_column(GTK_TREE_VIEW(tree
), col
);
424 /* Get the selection for the view and set its mode */
425 users_selection
= gtk_tree_view_get_selection(GTK_TREE_VIEW(tree
));
426 gtk_tree_selection_set_mode(users_selection
, GTK_SELECTION_BROWSE
);
427 /* Create the control buttons */
428 buttons
= create_buttons_box(users_buttons
,
430 gtk_vbox_new(FALSE
, 1));
431 /* Put it all together in an hbox */
432 hbox
= gtk_hbox_new(FALSE
, 2);
433 gtk_box_pack_start(GTK_BOX(hbox
), tree
, TRUE
/*expand*/, TRUE
/*fill*/, 0);
434 gtk_box_pack_start(GTK_BOX(hbox
), buttons
, FALSE
, FALSE
, 0);
435 gtk_container_add(GTK_CONTAINER(users_window
), hbox
);