chiark / gitweb /
Comments
[disorder] / disobedience / users.c
CommitLineData
ffc4dbaf
RK
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
24#include "disobedience.h"
08874c06 25#include "bits.h"
ffc4dbaf
RK
26
27static GtkWidget *users_window;
28static GtkListStore *users_list;
4aa8a0a4 29static GtkTreeSelection *users_selection;
ffc4dbaf 30
54e4791e
RK
31static GtkWidget *users_details_window;
32static GtkWidget *users_details_name;
33static GtkWidget *users_details_email;
34static GtkWidget *users_details_password;
35static GtkWidget *users_details_password2;
08874c06 36static GtkWidget *users_details_rights[32];
54e4791e 37
1bcd69c4
RK
38static const char *users_who, *users_email, *users_rights, *users_password;
39
61682944 40/** @brief qsort() callback for username comparison */
ffc4dbaf
RK
41static int usercmp(const void *a, const void *b) {
42 return strcmp(*(char **)a, *(char **)b);
43}
44
61682944 45/** @brief Called with the list of users */
ffc4dbaf
RK
46static void users_got_list(void attribute((unused)) *v, int nvec, char **vec) {
47 int n;
48 GtkTreeIter iter;
49
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);
60}
61
61682944 62/** @brief Pick the selected user from the list */
6faa6239
RK
63static char *users_getuser(void) {
64 GtkTreeIter iter;
65 char *who, *c;
66
67 if(gtk_tree_selection_get_selected(users_selection, 0, &iter)) {
68 gtk_tree_model_get(GTK_TREE_MODEL(users_list), &iter,
69 0, &who, -1);
70 if(who) {
71 c = xstrdup(who);
72 g_free(who);
73 return c;
74 }
75 }
76 return 0;
77}
78
95ceca70
RK
79/** @brief Text should be visible */
80#define DETAIL_VISIBLE 1
81
82/** @brief Text should be editable */
83#define DETAIL_EDITABLE 2
84
61682944 85/** @brief Add a row to the user detail table */
fbadea5c
RK
86static GtkWidget *users_detail_generic(GtkWidget *table,
87 int *rowp,
88 const char *title,
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),
94 label,
95 0, 1, /* left/right_attach */
96 row, row+1, /* top/bottom_attach */
97 GTK_FILL, /* xoptions */
98 0, /* yoptions */
99 1, 1); /* x/ypadding */
100 gtk_table_attach(GTK_TABLE(table),
101 selector,
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 */
107 return selector;
108}
109
95ceca70
RK
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
95ceca70
RK
116 * @return The text entry widget
117 */
118static GtkWidget *users_add_detail(GtkWidget *table,
119 int *rowp,
120 const char *title,
121 const char *value,
122 unsigned flags) {
fbadea5c
RK
123 GtkWidget *entry = gtk_entry_new();
124
95ceca70
RK
125 gtk_entry_set_visibility(GTK_ENTRY(entry),
126 !!(flags & DETAIL_VISIBLE));
127 gtk_editable_set_editable(GTK_EDITABLE(entry),
128 !!(flags & DETAIL_EDITABLE));
129 if(value)
130 gtk_entry_set_text(GTK_ENTRY(entry), value);
fbadea5c
RK
131 return users_detail_generic(table, rowp, title, entry);
132}
133
c49dfc50
RK
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)
c49dfc50 139 */
08874c06
RK
140static void users_add_right(GtkWidget *table,
141 int *rowp,
142 const char *title,
143 rights_type value) {
fbadea5c
RK
144 GtkWidget *check = gtk_check_button_new();
145
146 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check), !!value);
08874c06
RK
147 users_details_rights[leftmost_bit(value)] = check;
148 users_detail_generic(table, rowp, title, check);
95ceca70 149}
c49dfc50 150
d49df20c
RK
151/** @brief Set sensitivity of particular mine/random rights bits */
152static 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));
156
157 gtk_widget_set_sensitive(users_details_rights[bit + 1], sensitive);
158 gtk_widget_set_sensitive(users_details_rights[bit + 2], sensitive);
159}
160
161/** @brief Set sensitivity of all mine/random rights bits */
162static 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);
166}
167
168/** @brief Called when an _ALL widget is toggled
169 *
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,
172 */
173static void users_any_toggled(GtkToggleButton attribute((unused)) *togglebutton,
174 gpointer attribute((unused)) user_data) {
175 users_details_sensitize_all();
176}
177
c49dfc50
RK
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)
c49dfc50
RK
184 */
185static void users_add_right_group(GtkWidget *table,
186 int *rowp,
187 const char *title,
188 rights_type bits,
08874c06 189 rights_type mask) {
c49dfc50
RK
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);
08874c06
RK
194 const uint32_t first = mask / 7;
195 const int bit = leftmost_bit(first);
c49dfc50
RK
196
197 /* Discard irrelevant bits */
198 bits &= mask;
199 /* Shift down to bits 0-2; the mask is always 3 contiguous bits */
08874c06 200 bits >>= bit;
c49dfc50
RK
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);
08874c06
RK
207 users_details_rights[bit] = any;
208 users_details_rights[bit + 1] = mine;
209 users_details_rights[bit + 2] = random;
c49dfc50 210 users_detail_generic(table, rowp, title, hbox);
d49df20c 211 g_signal_connect(any, "toggled", G_CALLBACK(users_any_toggled), NULL);
c49dfc50 212}
95ceca70 213
54e4791e
RK
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
220 *
221 * This is used both by users_add() and users_edit().
222 *
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
228 */
229static void users_makedetails(const char *title,
230 const char *name,
231 const char *email,
fbadea5c 232 const char *rights,
54e4791e 233 const char *password) {
95ceca70
RK
234 GtkWidget *table;
235 int row = 0;
fbadea5c 236 rights_type r = 0;
54e4791e
RK
237
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*/);
247
95ceca70
RK
248 users_details_name = users_add_detail(table, &row, "Username", name,
249 (name ? 0 : DETAIL_EDITABLE)
250 |DETAIL_VISIBLE);
54e4791e 251
95ceca70
RK
252 users_details_email = users_add_detail(table, &row, "Email", email,
253 DETAIL_EDITABLE|DETAIL_VISIBLE);
54e4791e 254
95ceca70
RK
255 users_details_password = users_add_detail(table, &row, "Password",
256 password,
257 DETAIL_EDITABLE);
258 users_details_password2 = users_add_detail(table, &row, "Password",
259 password,
260 DETAIL_EDITABLE);
54e4791e 261
fbadea5c
RK
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);
08874c06
RK
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);
fbadea5c
RK
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);
d49df20c 276 users_details_sensitize_all();
95ceca70 277
54e4791e
RK
278 gtk_container_add(GTK_CONTAINER(users_details_window), table);
279 gtk_widget_show_all(users_details_window);
280}
281
61682944 282/** @brief Called when the 'add' button is pressed */
ffc4dbaf
RK
283static void users_add(GtkButton attribute((unused)) *button,
284 gpointer attribute((unused)) userdata) {
08874c06 285 /* TODO */
ffc4dbaf
RK
286}
287
61682944 288/** @brief Called when user deletion goes wrong */
4aa8a0a4
RK
289static void users_deleted_error(struct callbackdata attribute((unused)) *cbd,
290 int attribute((unused)) code,
291 const char *msg) {
292 popup_msg(GTK_MESSAGE_ERROR, msg);
293}
294
61682944 295/** @brief Called when a user has been deleted */
4aa8a0a4
RK
296static void users_deleted(void *v) {
297 const struct callbackdata *const cbd = v;
298 GtkTreeIter iter;
299 char *who;
300
301 /* Find the user */
302 if(!gtk_tree_model_get_iter_first(GTK_TREE_MODEL(users_list), &iter))
303 return;
304 do {
305 gtk_tree_model_get(GTK_TREE_MODEL(users_list), &iter,
306 0, &who, -1);
307 if(!strcmp(who, cbd->u.user))
308 break;
309 g_free(who);
310 who = 0;
311 } while(gtk_tree_model_iter_next(GTK_TREE_MODEL(users_list), &iter));
312 /* Remove them */
313 gtk_list_store_remove(users_list, &iter);
314 g_free(who);
315}
316
61682944 317/** @brief Called when the 'Delete' button is pressed */
ffc4dbaf
RK
318static void users_delete(GtkButton attribute((unused)) *button,
319 gpointer attribute((unused)) userdata) {
4aa8a0a4
RK
320 GtkWidget *yesno;
321 char *who;
322 int res;
323 struct callbackdata *cbd;
324
6faa6239
RK
325 if(!(who = users_getuser()))
326 return;
327 yesno = gtk_message_dialog_new(GTK_WINDOW(users_window),
328 GTK_DIALOG_MODAL,
329 GTK_MESSAGE_QUESTION,
330 GTK_BUTTONS_YES_NO,
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;
338 cbd->u.user = who;
339 disorder_eclient_deluser(client, users_deleted, cbd->u.user, cbd);
4aa8a0a4 340 }
ffc4dbaf
RK
341}
342
1bcd69c4
RK
343static void users_got_email(void attribute((unused)) *v, const char *value) {
344 users_email = value;
345}
346
347static void users_got_rights(void attribute((unused)) *v, const char *value) {
348 users_rights = value;
349}
350
351static void users_got_password(void attribute((unused)) *v, const char *value) {
352 users_password = value;
353 users_makedetails("editing user details",
354 users_who,
355 users_email,
356 users_rights,
357 users_password);
358}
359
61682944 360/** @brief Called when the 'Edit' button is pressed */
ffc4dbaf
RK
361static void users_edit(GtkButton attribute((unused)) *button,
362 gpointer attribute((unused)) userdata) {
1bcd69c4 363 if(!(users_who = users_getuser()))
54e4791e 364 return;
1bcd69c4
RK
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);
ffc4dbaf
RK
370}
371
61682944 372/** @brief Table of buttons for the user window */
ffc4dbaf
RK
373static const struct button users_buttons[] = {
374 {
375 "Add user",
376 users_add,
377 "Create a new user"
378 },
379 {
380 "Edit user",
381 users_edit,
382 "Edit a user"
383 },
384 {
385 "Delete user",
386 users_delete,
387 "Delete a user"
388 },
389};
390#define NUSERS_BUTTONS (sizeof users_buttons / sizeof *users_buttons)
391
61682944 392/** @brief Pop up the user management window */
ffc4dbaf
RK
393void manage_users(void) {
394 GtkWidget *tree, *buttons, *hbox;
395 GtkCellRenderer *cr;
396 GtkTreeViewColumn *col;
397
398 /* If the window already exists just raise it */
399 if(users_window) {
400 gtk_window_present(GTK_WINDOW(users_window));
401 return;
402 }
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);
411
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",
420 cr,
421 "text", 0,
422 NULL);
423 gtk_tree_view_append_column(GTK_TREE_VIEW(tree), col);
4aa8a0a4
RK
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);
ffc4dbaf
RK
427 /* Create the control buttons */
428 buttons = create_buttons_box(users_buttons,
429 NUSERS_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);
436}
437
438/*
439Local Variables:
440c-basic-offset:2
441comment-column:40
442fill-column:79
443indent-tabs-mode:nil
444End:
445*/