chiark / gitweb /
Comments
[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
24 #include "disobedience.h"
25 #include "bits.h"
26
27 static GtkWidget *users_window;
28 static GtkListStore *users_list;
29 static GtkTreeSelection *users_selection;
30
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];
37
38 static const char *users_who, *users_email, *users_rights, *users_password;
39
40 /** @brief qsort() callback for username comparison */
41 static int usercmp(const void *a, const void *b) {
42   return strcmp(*(char **)a, *(char **)b);
43 }
44
45 /** @brief Called with the list of users */
46 static 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
62 /** @brief Pick the selected user from the list */
63 static 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
79 /** @brief Text should be visible */
80 #define DETAIL_VISIBLE 1
81
82 /** @brief Text should be editable */
83 #define DETAIL_EDITABLE 2
84
85 /** @brief Add a row to the user detail table */
86 static 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
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
117  */
118 static GtkWidget *users_add_detail(GtkWidget *table,
119                                    int *rowp,
120                                    const char *title,
121                                    const char *value,
122                                    unsigned flags) {
123   GtkWidget *entry = gtk_entry_new();
124
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);
131   return users_detail_generic(table, rowp, title, entry);
132 }
133
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)
139  */
140 static void users_add_right(GtkWidget *table,
141                             int *rowp,
142                             const char *title,
143                             rights_type value) {
144   GtkWidget *check = gtk_check_button_new();
145   
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);
149 }
150
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));
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 */
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);
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  */
173 static void users_any_toggled(GtkToggleButton attribute((unused)) *togglebutton,
174                               gpointer attribute((unused)) user_data) {
175   users_details_sensitize_all();
176 }
177
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)
184  */
185 static void users_add_right_group(GtkWidget *table,
186                                   int *rowp,
187                                   const char *title,
188                                   rights_type bits,
189                                   rights_type mask) {
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);
196
197   /* Discard irrelevant bits */
198   bits &= mask;
199   /* Shift down to bits 0-2; the mask is always 3 contiguous bits */
200   bits >>= bit;
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);
212 }
213
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  */
229 static void users_makedetails(const char *title,
230                               const char *name,
231                               const char *email,
232                               const char *rights,
233                               const char *password) {
234   GtkWidget *table;
235   int row = 0;
236   rights_type r = 0;
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
248   users_details_name = users_add_detail(table, &row, "Username", name,
249                                         (name ? 0 : DETAIL_EDITABLE)
250                                         |DETAIL_VISIBLE);
251
252   users_details_email = users_add_detail(table, &row, "Email", email,
253                                          DETAIL_EDITABLE|DETAIL_VISIBLE);
254
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);
261
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();
277
278   gtk_container_add(GTK_CONTAINER(users_details_window), table);
279   gtk_widget_show_all(users_details_window);
280 }
281
282 /** @brief Called when the 'add' button is pressed */
283 static void users_add(GtkButton attribute((unused)) *button,
284                       gpointer attribute((unused)) userdata) {
285   /* TODO */
286 }
287
288 /** @brief Called when user deletion goes wrong */
289 static 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
295 /** @brief Called when a user has been deleted */
296 static 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
317 /** @brief Called when the 'Delete' button is pressed */
318 static void users_delete(GtkButton attribute((unused)) *button,
319                          gpointer attribute((unused)) userdata) {
320   GtkWidget *yesno;
321   char *who;
322   int res;
323   struct callbackdata *cbd;
324
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);
340   }
341 }
342
343 static void users_got_email(void attribute((unused)) *v, const char *value) {
344   users_email = value;
345 }
346
347 static void users_got_rights(void attribute((unused)) *v, const char *value) {
348   users_rights = value;
349 }
350
351 static 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
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()))
364     return;
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);
370 }
371
372 /** @brief Table of buttons for the user window */
373 static 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
392 /** @brief Pop up the user management window */
393 void 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);
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,
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 /*
439 Local Variables:
440 c-basic-offset:2
441 comment-column:40
442 fill-column:79
443 indent-tabs-mode:nil
444 End:
445 */