chiark / gitweb /
New compact mode for Disobediene.
[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 3 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,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU 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, see <http://www.gnu.org/licenses/>.
17  */
18 /** @file disobedience/users.c
19  * @brief User management for Disobedience
20  *
21  * The user management window contains:
22  * - a list of all the users
23  * - an add button
24  * - a delete button
25  * - a user details panel
26  * - an apply button
27  *
28  * When you select a user that user's details are displayed to the right of the
29  * list.  Hit the Apply button and any changes are applied.
30  *
31  * When you select 'add' a new empty set of details are displayed to be edited.
32  * Again Apply will commit them.
33  *
34  * TODO:
35  * - enter new username in the GtkTreeView
36  * - should have a cancel or close button, consistent with properties and login
37  */
38
39 #include "disobedience.h"
40 #include "bits.h"
41 #include "sendmail.h"
42
43 static void users_details_sensitize_all(void);
44 static void users_set_report(const char *msg);
45
46 static GtkWidget *users_window;
47 static GtkListStore *users_list;
48 static GtkTreeSelection *users_selection;
49
50 static GtkWidget *users_details_table;
51 static GtkWidget *users_apply_button;
52 static GtkWidget *users_delete_button;
53 static GtkWidget *users_details_name;
54 static GtkWidget *users_details_email;
55 static GtkWidget *users_details_password;
56 static GtkWidget *users_details_password2;
57 static GtkWidget *users_details_rights[32];
58 static GtkWidget *users_reporter;
59 static int users_details_row;
60 static const char *users_selected;
61 static const char *users_deferred_select;
62
63 static int users_mode;
64 #define MODE_NONE 0
65 #define MODE_ADD 1
66 #define MODE_EDIT 2
67
68 #define mode(X) do {                                    \
69   users_mode = MODE_##X;                                \
70   if(0) fprintf(stderr, "%s:%d: %s(): mode -> %s\n",    \
71           __FILE__, __LINE__, __FUNCTION__, #X);        \
72   users_details_sensitize_all();                        \
73 } while(0)
74
75 static const char *users_email, *users_rights, *users_password;
76
77 /** @brief qsort() callback for username comparison */
78 static int usercmp(const void *a, const void *b) {
79   return strcmp(*(char **)a, *(char **)b);
80 }
81
82 /** @brief Find a user
83  * @param user User to find
84  * @param iter Iterator to point at user
85  * @return 0 on success, -1 if not found
86  */
87 static int users_find_user(const char *user,
88                            GtkTreeIter *iter) {
89   char *who;
90
91   /* Find the user */
92   if(!gtk_tree_model_get_iter_first(GTK_TREE_MODEL(users_list), iter))
93     return -1;
94   do {
95     gtk_tree_model_get(GTK_TREE_MODEL(users_list), iter,
96                        0, &who, -1);
97     if(!strcmp(who, user)) {
98       g_free(who);
99       return 0;
100     }
101     g_free(who);
102   } while(gtk_tree_model_iter_next(GTK_TREE_MODEL(users_list), iter));
103   return -1;
104 }
105
106 /** @brief Called with the list of users
107  *
108  * Called:
109  * - at startup to populate the initial list
110  * - when we add a user
111  * - maybe in the future when we delete a user
112  *
113  * If users_deferred_select is set then that user is selected.
114  */
115 static void users_got_list(void attribute((unused)) *v,
116                            const char *err,
117                            int nvec, char **vec) {
118   int n;
119   GtkTreeIter iter;
120
121   if(err) {
122     popup_protocol_error(0, err);
123     return;
124   }
125   /* Present users in alphabetical order */
126   qsort(vec, nvec, sizeof (char *), usercmp);
127   /* Set the list contents */
128   gtk_list_store_clear(users_list);
129   for(n = 0; n < nvec; ++n)
130     gtk_list_store_insert_with_values(users_list, &iter, n/*position*/,
131                                       0, vec[n], /* column 0 */
132                                       -1);       /* no more columns */
133   /* Only show the window when the list is populated */
134   gtk_widget_show_all(users_window);
135   if(users_deferred_select) {
136     if(!users_find_user(users_deferred_select, &iter))
137       gtk_tree_selection_select_iter(users_selection, &iter);
138     users_deferred_select = 0;
139   }
140 }
141
142 /** @brief Text should be visible */
143 #define DETAIL_VISIBLE 1
144
145 /** @brief Text should be editable */
146 #define DETAIL_EDITABLE 2
147
148 /** @brief Add a row to the user detail table */
149 static void users_detail_generic(const char *title,
150                                  GtkWidget *selector) {
151   const int row = users_details_row++;
152   GtkWidget *const label = gtk_label_new(title);
153   gtk_misc_set_alignment(GTK_MISC(label), 1, 0);
154   gtk_table_attach(GTK_TABLE(users_details_table),
155                    label,
156                    0, 1,                /* left/right_attach */
157                    row, row+1,          /* top/bottom_attach */
158                    GTK_FILL,            /* xoptions */
159                    0,                   /* yoptions */
160                    1, 1);               /* x/ypadding */
161   gtk_table_attach(GTK_TABLE(users_details_table),
162                    selector,
163                    1, 2,                /* left/right_attach */
164                    row, row + 1,        /* top/bottom_attach */
165                    GTK_EXPAND|GTK_FILL, /* xoptions */
166                    GTK_FILL,            /* yoptions */
167                    1, 1);               /* x/ypadding */
168 }
169
170 static void users_entry_changed(GtkEditable attribute((unused)) *editable,
171                                 gpointer attribute((unused)) user_data) {
172   users_details_sensitize_all();
173 }
174
175 /** @brief Add a row to the user details table
176  * @param entryp Where to put GtkEntry
177  * @param title Label for this row
178  * @param value Initial value or NULL
179  * @param flags Flags word
180  */
181 static void users_add_detail(GtkWidget **entryp,
182                              const char *title,
183                              const char *value,
184                              unsigned flags) {
185   GtkWidget *entry;
186
187   if(!(entry = *entryp)) {
188     *entryp = entry = gtk_entry_new();
189     g_signal_connect(entry, "changed",
190                      G_CALLBACK(users_entry_changed), 0);
191     users_detail_generic(title, entry);
192   }
193   gtk_entry_set_visibility(GTK_ENTRY(entry),
194                            !!(flags & DETAIL_VISIBLE));
195   gtk_editable_set_editable(GTK_EDITABLE(entry),
196                             !!(flags & DETAIL_EDITABLE));
197   gtk_entry_set_text(GTK_ENTRY(entry), value ? value : "");
198 }
199
200 /** @brief Add a checkbox for a right
201  * @param title Label for this row
202  * @param value Current value
203  * @param right Right bit
204  */
205 static void users_add_right(const char *title,
206                             rights_type value,
207                             rights_type right) {
208   GtkWidget *check;
209   GtkWidget **checkp = &users_details_rights[leftmost_bit(right)];
210
211   if(!(check = *checkp)) {
212     *checkp = check = gtk_check_button_new_with_label("");
213     users_detail_generic(title, check);
214   }
215   gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check), !!(value & right));
216 }
217
218 /** @brief Set sensitivity of particular mine/random rights bits */
219 static void users_details_sensitize(rights_type r) {
220   const int bit = leftmost_bit(r);
221   const GtkWidget *all = users_details_rights[bit];
222   const int sensitive = (!gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(all))
223                          && users_mode != MODE_NONE);
224
225   gtk_widget_set_sensitive(users_details_rights[bit + 1], sensitive);
226   gtk_widget_set_sensitive(users_details_rights[bit + 2], sensitive);
227 }
228
229 /** @brief Set sensitivity of everything in sight */
230 static void users_details_sensitize_all(void) {
231   int n;
232   const char *report = 0;
233
234   for(n = 0; n < 32; ++n)
235     if(users_details_rights[n])
236       gtk_widget_set_sensitive(users_details_rights[n], users_mode != MODE_NONE);
237   gtk_widget_set_sensitive(users_details_name, users_mode != MODE_NONE);
238   gtk_widget_set_sensitive(users_details_email, users_mode != MODE_NONE);
239   gtk_widget_set_sensitive(users_details_password, users_mode != MODE_NONE);
240   gtk_widget_set_sensitive(users_details_password2, users_mode != MODE_NONE);
241   users_details_sensitize(RIGHT_MOVE_ANY);
242   users_details_sensitize(RIGHT_REMOVE_ANY);
243   users_details_sensitize(RIGHT_SCRATCH_ANY);
244   int apply_sensitive = 1;
245   if(users_mode == MODE_NONE)
246     apply_sensitive = 0;
247   else {
248     const char *name = gtk_entry_get_text(GTK_ENTRY(users_details_name));
249     const char *email = gtk_entry_get_text(GTK_ENTRY(users_details_email));
250     const char *pw = gtk_entry_get_text(GTK_ENTRY(users_details_password));
251     const char *pw2 = gtk_entry_get_text(GTK_ENTRY(users_details_password2));
252     /* Username must be filled in */
253     if(!*name) {
254       apply_sensitive = 0;
255       if(!report)
256         report = "Must fill in username";
257     }
258     /* Passwords must be nontrivial and match */
259     if(!*pw) {
260       apply_sensitive = 0;
261       if(!report)
262         report = "Must fill in password";
263     }
264     if(strcmp(pw, pw2)) {
265       apply_sensitive = 0;
266       if(!report)
267         report = "Passwords must match";
268     }
269     /* Email address must be somewhat valid */
270     if(*email) {
271       if(!email_valid(email)) {
272         apply_sensitive = 0;
273         report = "Invalid email address";
274       }
275     }
276   }
277   gtk_widget_set_sensitive(users_apply_button, apply_sensitive);
278   gtk_widget_set_sensitive(users_delete_button, !!users_selected);
279   users_set_report(report);
280 }
281
282 /** @brief Called when an _ALL widget is toggled
283  *
284  * Modifies sensitivity of the corresponding _MINE and _RANDOM widgets.  We
285  * just do the lot rather than trying to figure out which one changed,
286  */
287 static void users_any_toggled(GtkToggleButton attribute((unused)) *togglebutton,
288                               gpointer attribute((unused)) user_data) {
289   users_details_sensitize_all();
290 }
291
292 /** @brief Add a checkbox for a three-right group
293  * @param title Label for this row
294  * @param bits Rights bits (not masked or normalized)
295  * @param mask Mask for this group (must be 7*2^n)
296  */
297 static void users_add_right_group(const char *title,
298                                   rights_type bits,
299                                   rights_type mask) {
300   const uint32_t first = mask / 7;
301   const int bit = leftmost_bit(first);
302   GtkWidget **widgets = &users_details_rights[bit], *any, *mine, *rnd;
303
304   if(!*widgets) {
305     GtkWidget *hbox = gtk_hbox_new(FALSE, 2);
306
307     any = widgets[0] = gtk_check_button_new_with_label("Any");
308     mine = widgets[1] = gtk_check_button_new_with_label("Own");
309     rnd = widgets[2] = gtk_check_button_new_with_label("Random");
310     gtk_box_pack_start(GTK_BOX(hbox), any, FALSE, FALSE, 0);
311     gtk_box_pack_start(GTK_BOX(hbox), mine, FALSE, FALSE, 0);
312     gtk_box_pack_start(GTK_BOX(hbox), rnd, FALSE, FALSE, 0);
313     users_detail_generic(title, hbox);
314     g_signal_connect(any, "toggled", G_CALLBACK(users_any_toggled), NULL);
315     users_details_rights[bit] = any;
316     users_details_rights[bit + 1] = mine;
317     users_details_rights[bit + 2] = rnd;
318   } else {
319     any = widgets[0];
320     mine = widgets[1];
321     rnd = widgets[2];
322   }
323   /* Discard irrelevant bits */
324   bits &= mask;
325   /* Shift down to bits 0-2; the mask is always 3 contiguous bits */
326   bits >>= bit;
327   gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(any), !!(bits & 1));
328   gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(mine), !!(bits & 2));
329   gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(rnd), !!(bits & 4));
330 }
331
332 /** @brief Called when the details table is destroyed */
333 static void users_details_destroyed(GtkWidget attribute((unused)) *widget,
334                                     GtkWidget attribute((unused)) **wp) {
335   users_details_table = 0;
336   g_object_unref(users_list);
337   users_list = 0;
338   users_details_name = 0;
339   users_details_email = 0;
340   users_details_password = 0;
341   users_details_password2 = 0;
342   memset(users_details_rights, 0, sizeof users_details_rights);
343   /* also users_selection?  Not AFAICT; _get_selection does upref */
344 }
345
346 /** @brief Create or modify the user details table
347  * @param name User name (users_edit()) or NULL (users_add())
348  * @param email Email address
349  * @param rights User rights string
350  * @param password Password
351  * @param nameflags Visibility/editability for username
352  * @param flags Visibility/editability for other fields
353  */
354 static void users_makedetails(const char *name,
355                               const char *email,
356                               const char *rights,
357                               const char *password,
358                               unsigned nameflags,
359                               unsigned flags) {
360   rights_type r = 0;
361   
362   /* Create the table if it doesn't already exist */
363   if(!users_details_table) {
364     users_details_table = gtk_table_new(4, 2, FALSE/*!homogeneous*/);
365     g_signal_connect(users_details_table, "destroy",
366                      G_CALLBACK(users_details_destroyed), 0);
367   }
368
369   /* Create or update the widgets */
370   users_add_detail(&users_details_name, "Username", name,
371                    (DETAIL_EDITABLE|DETAIL_VISIBLE) & nameflags);
372
373   users_add_detail(&users_details_email, "Email", email,
374                    (DETAIL_EDITABLE|DETAIL_VISIBLE) & flags);
375
376   users_add_detail(&users_details_password, "Password", password,
377                    DETAIL_EDITABLE & flags);
378   users_add_detail(&users_details_password2, "Password", password,
379                    DETAIL_EDITABLE & flags);
380
381   parse_rights(rights, &r, 0);
382   users_add_right("Read operations", r, RIGHT_READ);
383   users_add_right("Play track", r, RIGHT_PLAY);
384   users_add_right_group("Move", r, RIGHT_MOVE__MASK);
385   users_add_right_group("Remove", r, RIGHT_REMOVE__MASK);
386   users_add_right_group("Scratch", r, RIGHT_SCRATCH__MASK);
387   users_add_right("Set volume", r, RIGHT_VOLUME);
388   users_add_right("Admin operations", r, RIGHT_ADMIN);
389   users_add_right("Rescan", r, RIGHT_RESCAN);
390   users_add_right("Register new users", r, RIGHT_REGISTER);
391   users_add_right("Modify own userinfo", r, RIGHT_USERINFO);
392   users_add_right("Modify track preferences", r, RIGHT_PREFS);
393   users_add_right("Modify global preferences", r, RIGHT_GLOBAL_PREFS);
394   users_add_right("Pause/resume tracks", r, RIGHT_PAUSE);
395   users_details_sensitize_all();
396 }
397
398 /** @brief Called when the 'add' button is pressed */
399 static void users_add(GtkButton attribute((unused)) *button,
400                       gpointer attribute((unused)) userdata) {
401   /* Unselect whatever is selected */
402   gtk_tree_selection_unselect_all(users_selection);
403   /* Reset the form */
404   /* TODO it would be better to use the server default_rights if there's no
405    * client setting. */
406   users_makedetails("",
407                     "",
408                     config->default_rights,
409                     "",
410                     DETAIL_EDITABLE|DETAIL_VISIBLE,
411                     DETAIL_EDITABLE|DETAIL_VISIBLE);
412   /* Remember we're adding a user */
413   mode(ADD);
414 }
415
416 static rights_type users_get_rights(void) {
417   rights_type r = 0;
418   int n;
419
420   /* Extract the rights value */
421   for(n = 0; n < 32; ++n) {
422     if(users_details_rights[n])
423       if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(users_details_rights[n])))
424          r |= 1 << n;
425   }
426   /* Throw out redundant bits */
427   if(r & RIGHT_REMOVE_ANY)
428     r &= ~(rights_type)(RIGHT_REMOVE_MINE|RIGHT_REMOVE_RANDOM);
429   if(r & RIGHT_MOVE_ANY)
430     r &= ~(rights_type)(RIGHT_MOVE_MINE|RIGHT_MOVE_RANDOM);
431   if(r & RIGHT_SCRATCH_ANY)
432     r &= ~(rights_type)(RIGHT_SCRATCH_MINE|RIGHT_SCRATCH_RANDOM);
433   return r;
434 }
435
436 /** @brief Called when a user setting has been edited */
437 static void users_edituser_completed(void attribute((unused)) *v,
438                                      const char *err) {
439   if(err)
440     popup_submsg(users_window, GTK_MESSAGE_ERROR, err);
441 }
442
443 /** @brief Called when a new user has been created */
444 static void users_adduser_completed(void *v,
445                                     const char *err) {
446   if(err) {
447     popup_submsg(users_window, GTK_MESSAGE_ERROR, err);
448     mode(ADD);                          /* Let the user try again */
449   } else {
450     const struct kvp *const kvp = v;
451     const char *user = kvp_get(kvp, "user");
452     const char *email = kvp_get(kvp, "email"); /* maybe NULL */
453
454     /* Now the user is created we can go ahead and set the email address */
455     if(email)
456       disorder_eclient_edituser(client, users_edituser_completed, user,
457                                 "email", email, NULL);
458     /* Refresh the list of users */
459     disorder_eclient_users(client, users_got_list, 0);
460     /* We'll select the newly created user */
461     users_deferred_select = user;
462   }
463 }
464
465 /** @brief Called when the 'Apply' button is pressed */
466 static void users_apply(GtkButton attribute((unused)) *button,
467                         gpointer attribute((unused)) userdata) {
468   const char *password;
469   const char *password2;
470   const char *name;
471   const char *email;
472
473   switch(users_mode) {
474   case MODE_NONE:
475     return;
476   case MODE_ADD:
477     name = xstrdup(gtk_entry_get_text(GTK_ENTRY(users_details_name)));
478     email = xstrdup(gtk_entry_get_text(GTK_ENTRY(users_details_email)));
479     password = xstrdup(gtk_entry_get_text(GTK_ENTRY(users_details_password)));
480     password2 = xstrdup(gtk_entry_get_text(GTK_ENTRY(users_details_password2)));
481     if(!*name) {
482       /* No username.  Really we wanted to desensitize the Apply button when
483        * there's no userame but there doesn't seem to be a signal to detect
484        * changes to the entry text.  Consequently we have error messages
485        * instead.  */
486       popup_submsg(users_window, GTK_MESSAGE_ERROR, "Must enter a username");
487       return;
488     }
489     if(strcmp(password, password2)) {
490       popup_submsg(users_window, GTK_MESSAGE_ERROR, "Passwords do not match");
491       return;
492     }
493     if(*email && !strchr(email, '@')) {
494       /* The server will complain about this but we can give a better error
495        * message this way */
496       popup_submsg(users_window, GTK_MESSAGE_ERROR, "Invalid email address");
497       return;
498     }
499     disorder_eclient_adduser(client,
500                              users_adduser_completed,
501                              name,
502                              password,
503                              rights_string(users_get_rights()),
504                              kvp_make("user", name,
505                                       "email", email,
506                                       (char *)0));
507     /* We switch to no-op mode while creating the user */
508     mode(NONE);
509     break;
510   case MODE_EDIT:
511     /* Ugh, can we de-dupe with above? */
512     email = xstrdup(gtk_entry_get_text(GTK_ENTRY(users_details_email)));
513     password = xstrdup(gtk_entry_get_text(GTK_ENTRY(users_details_password)));
514     password2 = xstrdup(gtk_entry_get_text(GTK_ENTRY(users_details_password2)));
515     if(strcmp(password, password2)) {
516       popup_submsg(users_window, GTK_MESSAGE_ERROR, "Passwords do not match");
517       return;
518     }
519     if(*email && !strchr(email, '@')) {
520       popup_submsg(users_window, GTK_MESSAGE_ERROR, "Invalid email address");
521       return;
522     }
523     disorder_eclient_edituser(client, users_edituser_completed, users_selected,
524                               "email", email, NULL);
525     disorder_eclient_edituser(client, users_edituser_completed, users_selected,
526                               "password", password, NULL);
527     disorder_eclient_edituser(client, users_edituser_completed, users_selected,
528                               "rights", rights_string(users_get_rights()), NULL);
529     /* We remain in edit mode */
530     break;
531   }
532 }
533
534 /** @brief Called when a user has been deleted */
535 static void users_delete_completed(void *v,
536                                    const char *err) {
537   if(err)
538     popup_submsg(users_window, GTK_MESSAGE_ERROR, err);
539   else {
540     const struct kvp *const kvp = v;
541     const char *const user = kvp_get(kvp, "user");
542     GtkTreeIter iter[1];
543     
544     if(!users_find_user(user, iter))           /* Find the user... */
545       gtk_list_store_remove(users_list, iter); /* ...and remove them */
546   }
547 }
548
549 /** @brief Called when the 'Delete' button is pressed */
550 static void users_delete(GtkButton attribute((unused)) *button,
551                          gpointer attribute((unused)) userdata) {
552   GtkWidget *yesno;
553   int res;
554
555   if(!users_selected)
556     return;
557   yesno = gtk_message_dialog_new(GTK_WINDOW(users_window),
558                                  GTK_DIALOG_MODAL,
559                                  GTK_MESSAGE_QUESTION,
560                                  GTK_BUTTONS_YES_NO,
561                                  "Do you really want to delete user %s?"
562                                  " This action cannot be undone.",
563                                  users_selected);
564   res = gtk_dialog_run(GTK_DIALOG(yesno));
565   gtk_widget_destroy(yesno);
566   if(res == GTK_RESPONSE_YES) {
567     disorder_eclient_deluser(client, users_delete_completed, users_selected,
568                              kvp_make("user", users_selected,
569                                       (char *)0));
570   }
571 }
572
573 static void users_got_email(void attribute((unused)) *v,
574                             const char *err,
575                             const char *value) {
576   if(err)
577     popup_protocol_error(0, err);
578   users_email = value;
579 }
580
581 static void users_got_rights(void attribute((unused)) *v,
582                              const char *err,
583                              const char *value) {
584   if(err)
585     popup_protocol_error(0, err);
586   users_rights = value;
587 }
588
589 static void users_got_password(void attribute((unused)) *v,
590                                const char *err,
591                                const char *value) {
592   if(err)
593     popup_protocol_error(0, err);
594   /* TODO if an error occurred gathering user info, we should react in some
595    * different way */
596   users_password = value;
597   users_makedetails(users_selected,
598                     users_email,
599                     users_rights,
600                     users_password,
601                     DETAIL_VISIBLE,
602                     DETAIL_EDITABLE|DETAIL_VISIBLE);
603   mode(EDIT);
604 }
605
606 /** @brief Called when the selection MIGHT have changed */
607 static void users_selection_changed(GtkTreeSelection attribute((unused)) *treeselection,
608                                     gpointer attribute((unused)) user_data) {
609   GtkTreeIter iter;
610   char *gselected, *selected;
611   
612   /* Identify the current selection */
613   if(gtk_tree_selection_get_selected(users_selection, 0, &iter)) {
614     gtk_tree_model_get(GTK_TREE_MODEL(users_list), &iter,
615                        0, &gselected, -1);
616     selected = xstrdup(gselected);
617     g_free(gselected);
618   } else
619     selected = 0;
620   /* Eliminate no-change cases */
621   if(!selected && !users_selected)
622     return;
623   if(selected && users_selected && !strcmp(selected, users_selected))
624     return;
625   /* There's been a change; junk the old data and fetch new data in
626    * background. */
627   users_selected = selected;
628   users_makedetails("", "", "", "",
629                     DETAIL_VISIBLE,
630                     DETAIL_VISIBLE);
631   if(users_selected) {
632     disorder_eclient_userinfo(client, users_got_email, users_selected,
633                               "email", 0);
634     disorder_eclient_userinfo(client, users_got_rights, users_selected,
635                               "rights", 0);
636     disorder_eclient_userinfo(client, users_got_password, users_selected,
637                               "password", 0);
638   }
639   mode(NONE);                           /* not editing *yet* */
640 }
641
642 static GtkWidget *users_make_reporter() {
643   if(!users_reporter) {
644     users_reporter = gtk_label_new("");
645     gtk_label_set_ellipsize(GTK_LABEL(users_reporter), PANGO_ELLIPSIZE_END);
646     gtk_misc_set_alignment(GTK_MISC(users_reporter), 0.99, 0);
647     g_signal_connect(users_reporter, "destroy",
648                      G_CALLBACK(gtk_widget_destroyed), &users_reporter);
649   }
650   return users_reporter;
651 }
652
653 static void users_set_report(const char *msg) {
654   gtk_label_set_text(GTK_LABEL(users_make_reporter()), msg ? msg : "");
655 }
656
657 /** @brief Table of buttons below the user list */
658 static struct button users_buttons[] = {
659   {
660     GTK_STOCK_ADD,
661     users_add,
662     "Create a new user",
663     0,
664     NULL,
665   },
666   {
667     GTK_STOCK_REMOVE,
668     users_delete,
669     "Delete a user",
670     0,
671     NULL,
672   },
673 };
674 #define NUSERS_BUTTONS (sizeof users_buttons / sizeof *users_buttons)
675
676 /** @brief Keypress handler */
677 static gboolean users_keypress(GtkWidget attribute((unused)) *widget,
678                                GdkEventKey *event,
679                                gpointer attribute((unused)) user_data) {
680   if(event->state)
681     return FALSE;
682   switch(event->keyval) {
683   case GDK_Escape:
684     gtk_widget_destroy(users_window);
685     return TRUE;
686   default:
687     return FALSE;
688   }
689 }
690
691 /** @brief Pop up the user management window */
692 void manage_users(void) {
693   GtkWidget *tree, *buttons, *hbox, *hbox2, *vbox, *vbox2;
694   GtkCellRenderer *cr;
695   GtkTreeViewColumn *col;
696   
697   /* If the window already exists just raise it */
698   if(users_window) {
699     gtk_window_present(GTK_WINDOW(users_window));
700     return;
701   }
702   /* Destroy old widgets */
703   if(users_reporter)
704     gtk_widget_destroy(users_reporter);
705   /* Create the window */
706   users_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
707   gtk_widget_set_style(users_window, tool_style);
708   g_signal_connect(users_window, "destroy",
709                    G_CALLBACK(gtk_widget_destroyed), &users_window);
710   gtk_window_set_title(GTK_WINDOW(users_window), "User Management");
711   /* Keyboard shortcuts */
712   g_signal_connect(users_window, "key-press-event",
713                    G_CALLBACK(users_keypress), 0);
714   /* default size is too small */
715   gtk_window_set_default_size(GTK_WINDOW(users_window), 240, 240);
716
717   /* Create the list of users and populate it asynchronously */
718   users_list = gtk_list_store_new(1, G_TYPE_STRING);
719   disorder_eclient_users(client, users_got_list, 0);
720   /* Create the view */
721   tree = gtk_tree_view_new_with_model(GTK_TREE_MODEL(users_list));
722   /* ...and the renderers for it */
723   cr = gtk_cell_renderer_text_new();
724   col = gtk_tree_view_column_new_with_attributes("Username",
725                                                  cr,
726                                                  "text", 0,
727                                                  NULL);
728   gtk_tree_view_append_column(GTK_TREE_VIEW(tree), col);
729   /* Get the selection for the view; set its mode; arrange for a callback when
730    * it changes */
731   users_selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(tree));
732   gtk_tree_selection_set_mode(users_selection, GTK_SELECTION_BROWSE);
733   g_signal_connect(users_selection, "changed",
734                    G_CALLBACK(users_selection_changed), NULL);
735
736   /* Create the control buttons */
737   buttons = create_buttons_box(users_buttons,
738                                NUSERS_BUTTONS,
739                                gtk_hbox_new(FALSE, 1));
740   users_delete_button = users_buttons[1].widget;
741
742   /* Buttons live below the list */
743   vbox = gtk_vbox_new(FALSE, 0);
744   gtk_box_pack_start(GTK_BOX(vbox), scroll_widget(tree), TRUE/*expand*/, TRUE/*fill*/, 0);
745   gtk_box_pack_start(GTK_BOX(vbox), buttons, FALSE/*expand*/, FALSE, 0);
746
747   /* Create an empty user details table, and put an apply button below it */
748   users_apply_button = gtk_button_new_from_stock(GTK_STOCK_APPLY);
749   users_makedetails("", "", "", "",
750                     DETAIL_VISIBLE,
751                     DETAIL_VISIBLE);
752   g_signal_connect(users_apply_button, "clicked",
753                    G_CALLBACK(users_apply), NULL);
754   hbox2 = gtk_hbox_new(FALSE, 0);
755   gtk_box_pack_end(GTK_BOX(hbox2), users_apply_button,
756                    FALSE/*expand*/, FALSE, 0);
757   
758   vbox2 = gtk_vbox_new(FALSE, 0);
759   gtk_box_pack_start(GTK_BOX(vbox2), users_details_table,
760                      TRUE/*expand*/, TRUE/*fill*/, 0);
761   gtk_box_pack_start(GTK_BOX(vbox2), gtk_hseparator_new(),
762                      FALSE/*expand*/, FALSE, 0);
763   gtk_box_pack_start(GTK_BOX(vbox2), users_make_reporter(),
764                      FALSE/*expand*/, FALSE, 0);
765   gtk_box_pack_start(GTK_BOX(vbox2), hbox2,
766                      FALSE/*expand*/, FALSE, 0);
767   
768   /* User details are to the right of the list.  We put in a pointless event
769    * box as as spacer, so that the longest label in the user details isn't
770    * cuddled up to the user list. */
771   hbox = gtk_hbox_new(FALSE, 0);
772   gtk_box_pack_start(GTK_BOX(hbox), vbox, FALSE/*expand*/, FALSE, 0);
773   gtk_box_pack_start(GTK_BOX(hbox), gtk_event_box_new(), FALSE/*expand*/, FALSE, 2);
774   gtk_box_pack_start(GTK_BOX(hbox), vbox2, TRUE/*expand*/, TRUE/*fill*/, 0);
775   gtk_container_add(GTK_CONTAINER(users_window), frame_widget(hbox, NULL));
776 }
777
778 /*
779 Local Variables:
780 c-basic-offset:2
781 comment-column:40
782 fill-column:79
783 indent-tabs-mode:nil
784 End:
785 */