chiark / gitweb /
Desensitive _MINE and _RANDOM if _ALL is checked
[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 static int usercmp(const void *a, const void *b) {
41   return strcmp(*(char **)a, *(char **)b);
42 }
43
44 static void users_got_list(void attribute((unused)) *v, int nvec, char **vec) {
45   int n;
46   GtkTreeIter iter;
47
48   /* Present users in alphabetical order */
49   qsort(vec, nvec, sizeof (char *), usercmp);
50   /* Set the list contents */
51   gtk_list_store_clear(users_list);
52   for(n = 0; n < nvec; ++n)
53     gtk_list_store_insert_with_values(users_list, &iter, n/*position*/,
54                                       0, vec[n], /* column 0 */
55                                       -1);       /* no more columns */
56   /* Only show the window when the list is populated */
57   gtk_widget_show_all(users_window);
58 }
59
60 static char *users_getuser(void) {
61   GtkTreeIter iter;
62   char *who, *c;
63
64   if(gtk_tree_selection_get_selected(users_selection, 0, &iter)) {
65     gtk_tree_model_get(GTK_TREE_MODEL(users_list), &iter,
66                        0, &who, -1);
67     if(who) {
68       c = xstrdup(who);
69       g_free(who);
70       return c;
71     }
72   }
73   return 0;
74 }
75
76 /** @brief Text should be visible */
77 #define DETAIL_VISIBLE 1
78
79 /** @brief Text should be editable */
80 #define DETAIL_EDITABLE 2
81
82 static GtkWidget *users_detail_generic(GtkWidget *table,
83                                        int *rowp,
84                                        const char *title,
85                                        GtkWidget *selector) {
86   const int row = (*rowp)++;
87   GtkWidget *const label = gtk_label_new(title);
88   gtk_misc_set_alignment(GTK_MISC(label), 1, 0);
89   gtk_table_attach(GTK_TABLE(table),
90                    label,
91                    0, 1,                /* left/right_attach */
92                    row, row+1,          /* top/bottom_attach */
93                    GTK_FILL,            /* xoptions */
94                    0,                   /* yoptions */
95                    1, 1);               /* x/ypadding */
96   gtk_table_attach(GTK_TABLE(table),
97                    selector,
98                    1, 2,                /* left/right_attach */
99                    row, row + 1,        /* top/bottom_attach */
100                    GTK_EXPAND|GTK_FILL, /* xoptions */
101                    GTK_FILL,            /* yoptions */
102                    1, 1);               /* x/ypadding */
103   return selector;
104 }
105
106 /** @brief Add a row to the user details table
107  * @param table Containin table widget
108  * @param rowp Pointer to row number, incremented
109  * @param title Label for this row
110  * @param value Initial value or NULL
111  * @param flags Flags word
112  * @return The text entry widget
113  */
114 static GtkWidget *users_add_detail(GtkWidget *table,
115                                    int *rowp,
116                                    const char *title,
117                                    const char *value,
118                                    unsigned flags) {
119   GtkWidget *entry = gtk_entry_new();
120
121   gtk_entry_set_visibility(GTK_ENTRY(entry),
122                            !!(flags & DETAIL_VISIBLE));
123   gtk_editable_set_editable(GTK_EDITABLE(entry),
124                             !!(flags & DETAIL_EDITABLE));
125   if(value)
126     gtk_entry_set_text(GTK_ENTRY(entry), value);
127   return users_detail_generic(table, rowp, title, entry);
128 }
129
130 /** @brief Add a checkbox for a right
131  * @param table Containing table
132  * @param rowp Pointer to row number, incremented
133  * @param title Label for this row
134  * @param value Right bit (masked but not necessarily normalized)
135  */
136 static void users_add_right(GtkWidget *table,
137                             int *rowp,
138                             const char *title,
139                             rights_type value) {
140   GtkWidget *check = gtk_check_button_new();
141   
142   gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check), !!value);
143   users_details_rights[leftmost_bit(value)] = check;
144   users_detail_generic(table, rowp, title, check);
145 }
146
147 /** @brief Set sensitivity of particular mine/random rights bits */
148 static void users_details_sensitize(rights_type r) {
149   const int bit = leftmost_bit(r);
150   const GtkWidget *all = users_details_rights[bit];
151   const int sensitive = !gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(all));
152   
153   gtk_widget_set_sensitive(users_details_rights[bit + 1], sensitive);
154   gtk_widget_set_sensitive(users_details_rights[bit + 2], sensitive);
155 }
156
157 /** @brief Set sensitivity of all mine/random rights bits */
158 static void users_details_sensitize_all(void) {
159   users_details_sensitize(RIGHT_MOVE_ANY);
160   users_details_sensitize(RIGHT_REMOVE_ANY);
161   users_details_sensitize(RIGHT_SCRATCH_ANY);
162 }
163
164 /** @brief Called when an _ALL widget is toggled
165  *
166  * Modifies sensitivity of the corresponding _MINE and _RANDOM widgets.  We
167  * just do the lot rather than trying to figure out which one changed,
168  */
169 static void users_any_toggled(GtkToggleButton attribute((unused)) *togglebutton,
170                               gpointer attribute((unused)) user_data) {
171   users_details_sensitize_all();
172 }
173
174 /** @brief Add a checkbox for a three-right group
175  * @param table Containing table
176  * @param rowp Pointer to row number, incremented
177  * @param title Label for this row
178  * @param bits Rights bits (not masked or normalized)
179  * @param mask Mask for this group (must be 7.2^n)
180  */
181 static void users_add_right_group(GtkWidget *table,
182                                   int *rowp,
183                                   const char *title,
184                                   rights_type bits,
185                                   rights_type mask) {
186   GtkWidget *any = gtk_check_button_new_with_label("Any");
187   GtkWidget *mine = gtk_check_button_new_with_label("Own");
188   GtkWidget *random = gtk_check_button_new_with_label("Random");
189   GtkWidget *hbox = gtk_hbox_new(FALSE, 2);
190   const uint32_t first = mask / 7;
191   const int bit = leftmost_bit(first);
192
193   /* Discard irrelevant bits */
194   bits &= mask;
195   /* Shift down to bits 0-2; the mask is always 3 contiguous bits */
196   bits >>= bit;
197   gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(any), !!(bits & 1));
198   gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(mine), !!(bits & 2));
199   gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(random), !!(bits & 4));
200   gtk_box_pack_start(GTK_BOX(hbox), any, FALSE, FALSE, 0);
201   gtk_box_pack_start(GTK_BOX(hbox), mine, FALSE, FALSE, 0);
202   gtk_box_pack_start(GTK_BOX(hbox), random, FALSE, FALSE, 0);
203   users_details_rights[bit] = any;
204   users_details_rights[bit + 1] = mine;
205   users_details_rights[bit + 2] = random;
206   users_detail_generic(table, rowp, title, hbox);
207   g_signal_connect(any, "toggled", G_CALLBACK(users_any_toggled), NULL);
208 }
209
210 /** @brief Create the user details window
211  * @param title Window title
212  * @param name User name (users_edit()) or NULL (users_add())
213  * @param email Email address
214  * @param rights User rights string
215  * @param password Password
216  *
217  * This is used both by users_add() and users_edit().
218  *
219  * The window contains:
220  * - display or edit fields for the username
221  * - edit fields for the email address
222  * - two hidden edit fields for the password (they must match)
223  * - check boxes for the rights
224  */
225 static void users_makedetails(const char *title,
226                               const char *name,
227                               const char *email,
228                               const char *rights,
229                               const char *password) {
230   GtkWidget *table;
231   int row = 0;
232   rights_type r = 0;
233   
234   /* Create the window */
235   users_details_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
236   gtk_widget_set_style(users_details_window, tool_style);
237   g_signal_connect(users_details_window, "destroy",
238                    G_CALLBACK(gtk_widget_destroyed), &users_details_window);
239   gtk_window_set_title(GTK_WINDOW(users_details_window), title);
240   gtk_window_set_transient_for(GTK_WINDOW(users_details_window),
241                                GTK_WINDOW(users_window));
242   table = gtk_table_new(4, 2, FALSE/*!homogeneous*/);
243
244   users_details_name = users_add_detail(table, &row, "Username", name,
245                                         (name ? 0 : DETAIL_EDITABLE)
246                                         |DETAIL_VISIBLE);
247
248   users_details_email = users_add_detail(table, &row, "Email", email,
249                                          DETAIL_EDITABLE|DETAIL_VISIBLE);
250
251   users_details_password = users_add_detail(table, &row, "Password",
252                                             password,
253                                             DETAIL_EDITABLE);
254   users_details_password2 = users_add_detail(table, &row, "Password",
255                                              password,
256                                              DETAIL_EDITABLE);
257
258   parse_rights(rights, &r, 1);
259   users_add_right(table, &row, "Read operations", r & RIGHT_READ);
260   users_add_right(table, &row, "Play track", r & RIGHT_PLAY);
261   users_add_right_group(table, &row, "Move", r, RIGHT_MOVE__MASK);
262   users_add_right_group(table, &row, "Remove", r, RIGHT_REMOVE__MASK);
263   users_add_right_group(table, &row, "Scratch", r, RIGHT_SCRATCH__MASK);
264   users_add_right(table, &row, "Set volume", r & RIGHT_VOLUME);
265   users_add_right(table, &row, "Admin operations", r & RIGHT_ADMIN);
266   users_add_right(table, &row, "Rescan", r & RIGHT_RESCAN);
267   users_add_right(table, &row, "Register new users", r & RIGHT_REGISTER);
268   users_add_right(table, &row, "Modify own userinfo", r & RIGHT_USERINFO);
269   users_add_right(table, &row, "Modify track preferences", r & RIGHT_PREFS);
270   users_add_right(table, &row, "Modify global preferences", r & RIGHT_GLOBAL_PREFS);
271   users_add_right(table, &row, "Pause/resume tracks", r & RIGHT_PAUSE);
272   users_details_sensitize_all();
273
274   gtk_container_add(GTK_CONTAINER(users_details_window), table);
275   gtk_widget_show_all(users_details_window);
276 }
277
278 static void users_add(GtkButton attribute((unused)) *button,
279                       gpointer attribute((unused)) userdata) {
280   /* TODO */
281 }
282
283 static void users_deleted_error(struct callbackdata attribute((unused)) *cbd,
284                                 int attribute((unused)) code,
285                                 const char *msg) {
286   popup_msg(GTK_MESSAGE_ERROR, msg);
287 }
288
289 static void users_deleted(void *v) {
290   const struct callbackdata *const cbd = v;
291   GtkTreeIter iter;
292   char *who;
293
294   /* Find the user */
295   if(!gtk_tree_model_get_iter_first(GTK_TREE_MODEL(users_list), &iter))
296     return;
297   do {
298     gtk_tree_model_get(GTK_TREE_MODEL(users_list), &iter,
299                        0, &who, -1);
300     if(!strcmp(who, cbd->u.user))
301       break;
302     g_free(who);
303     who = 0;
304   } while(gtk_tree_model_iter_next(GTK_TREE_MODEL(users_list), &iter));
305   /* Remove them */
306   gtk_list_store_remove(users_list, &iter);
307   g_free(who);
308 }
309
310 static void users_delete(GtkButton attribute((unused)) *button,
311                          gpointer attribute((unused)) userdata) {
312   GtkWidget *yesno;
313   char *who;
314   int res;
315   struct callbackdata *cbd;
316
317   if(!(who = users_getuser()))
318     return;
319   yesno = gtk_message_dialog_new(GTK_WINDOW(users_window),
320                                  GTK_DIALOG_MODAL,
321                                  GTK_MESSAGE_QUESTION,
322                                  GTK_BUTTONS_YES_NO,
323                                  "Do you really want to delete user %s?"
324                                  " This action cannot be undone.", who);
325   res = gtk_dialog_run(GTK_DIALOG(yesno));
326   gtk_widget_destroy(yesno);
327   if(res == GTK_RESPONSE_YES) {
328     cbd = xmalloc(sizeof *cbd);
329     cbd->onerror = users_deleted_error;
330     cbd->u.user = who;
331     disorder_eclient_deluser(client, users_deleted, cbd->u.user, cbd);
332   }
333 }
334
335 static void users_got_email(void attribute((unused)) *v, const char *value) {
336   users_email = value;
337 }
338
339 static void users_got_rights(void attribute((unused)) *v, const char *value) {
340   users_rights = value;
341 }
342
343 static void users_got_password(void attribute((unused)) *v, const char *value) {
344   users_password = value;
345   users_makedetails("editing user details",
346                     users_who,
347                     users_email,
348                     users_rights,
349                     users_password);
350 }
351
352 static void users_edit(GtkButton attribute((unused)) *button,
353                        gpointer attribute((unused)) userdata) {
354   if(!(users_who = users_getuser()))
355     return;
356   /* Schedule user lookups for all the properties we know about.  This is all a
357    * bit ad-hoc but will do for now. */
358   disorder_eclient_userinfo(client, users_got_email, users_who, "email", 0);
359   disorder_eclient_userinfo(client, users_got_rights, users_who, "rights", 0);
360   disorder_eclient_userinfo(client, users_got_password, users_who, "password", 0);
361 }
362
363 static const struct button users_buttons[] = {
364   {
365     "Add user",
366     users_add,
367     "Create a new user"
368   },
369   {
370     "Edit user",
371     users_edit,
372     "Edit a user"
373   },
374   {
375     "Delete user",
376     users_delete,
377     "Delete a user"
378   },
379 };
380 #define NUSERS_BUTTONS (sizeof users_buttons / sizeof *users_buttons)
381
382 void manage_users(void) {
383   GtkWidget *tree, *buttons, *hbox;
384   GtkCellRenderer *cr;
385   GtkTreeViewColumn *col;
386   
387   /* If the window already exists just raise it */
388   if(users_window) {
389     gtk_window_present(GTK_WINDOW(users_window));
390     return;
391   }
392   /* Create the window */
393   users_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
394   gtk_widget_set_style(users_window, tool_style);
395   g_signal_connect(users_window, "destroy",
396                    G_CALLBACK(gtk_widget_destroyed), &users_window);
397   gtk_window_set_title(GTK_WINDOW(users_window), "User Management");
398   /* default size is too small */
399   gtk_window_set_default_size(GTK_WINDOW(users_window), 240, 240);
400
401   /* Create the list of users and populate it asynchronously */
402   users_list = gtk_list_store_new(1, G_TYPE_STRING);
403   disorder_eclient_users(client, users_got_list, 0);
404   /* Create the view */
405   tree = gtk_tree_view_new_with_model(GTK_TREE_MODEL(users_list));
406   /* ...and the renderers for it */
407   cr = gtk_cell_renderer_text_new();
408   col = gtk_tree_view_column_new_with_attributes("Username",
409                                                  cr,
410                                                  "text", 0,
411                                                  NULL);
412   gtk_tree_view_append_column(GTK_TREE_VIEW(tree), col);
413   /* Get the selection for the view and set its mode */
414   users_selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(tree));
415   gtk_tree_selection_set_mode(users_selection, GTK_SELECTION_BROWSE);
416   /* Create the control buttons */
417   buttons = create_buttons_box(users_buttons,
418                                NUSERS_BUTTONS,
419                                gtk_vbox_new(FALSE, 1));
420   /* Put it all together in an hbox */
421   hbox = gtk_hbox_new(FALSE, 2);
422   gtk_box_pack_start(GTK_BOX(hbox), tree, TRUE/*expand*/, TRUE/*fill*/, 0);
423   gtk_box_pack_start(GTK_BOX(hbox), buttons, FALSE, FALSE, 0);
424   gtk_container_add(GTK_CONTAINER(users_window), hbox);
425 }
426
427 /*
428 Local Variables:
429 c-basic-offset:2
430 comment-column:40
431 fill-column:79
432 indent-tabs-mode:nil
433 End:
434 */