chiark / gitweb /
Disobedience login window now has a 'remote' switch. When off it will
[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  * The user management window contains:
24  * - a list of all the users
25  * - an add button
26  * - a delete button
27  * - a user details panel
28  * - an apply button
29  *
30  * When you select a user that user's details are displayed to the right of the
31  * list.  Hit the Apply button and any changes are applied.
32  *
33  * When you select 'add' a new empty set of details are displayed to be edited.
34  * Again Apply will commit them.
35  *
36  * TODO:
37  * - enter new username in the GtkTreeView
38  * - should have a cancel or close button, consistent with properties and login
39  */
40
41 #include "disobedience.h"
42 #include "bits.h"
43 #include "sendmail.h"
44
45 static void users_details_sensitize_all(void);
46 static void users_set_report(const char *msg);
47
48 static GtkWidget *users_window;
49 static GtkListStore *users_list;
50 static GtkTreeSelection *users_selection;
51
52 static GtkWidget *users_details_table;
53 static GtkWidget *users_apply_button;
54 static GtkWidget *users_delete_button;
55 static GtkWidget *users_details_name;
56 static GtkWidget *users_details_email;
57 static GtkWidget *users_details_password;
58 static GtkWidget *users_details_password2;
59 static GtkWidget *users_details_rights[32];
60 static GtkWidget *users_reporter;
61 static int users_details_row;
62 static const char *users_selected;
63 static const char *users_deferred_select;
64
65 static int users_mode;
66 #define MODE_NONE 0
67 #define MODE_ADD 1
68 #define MODE_EDIT 2
69
70 #define mode(X) do {                                    \
71   users_mode = MODE_##X;                                \
72   if(0) fprintf(stderr, "%s:%d: %s(): mode -> %s\n",    \
73           __FILE__, __LINE__, __FUNCTION__, #X);        \
74   users_details_sensitize_all();                        \
75 } while(0)
76
77 static const char *users_email, *users_rights, *users_password;
78
79 /** @brief qsort() callback for username comparison */
80 static int usercmp(const void *a, const void *b) {
81   return strcmp(*(char **)a, *(char **)b);
82 }
83
84 /** @brief Find a user
85  * @param user User to find
86  * @param iter Iterator to point at user
87  * @return 0 on success, -1 if not found
88  */
89 static int users_find_user(const char *user,
90                            GtkTreeIter *iter) {
91   char *who;
92
93   /* Find the user */
94   if(!gtk_tree_model_get_iter_first(GTK_TREE_MODEL(users_list), iter))
95     return -1;
96   do {
97     gtk_tree_model_get(GTK_TREE_MODEL(users_list), iter,
98                        0, &who, -1);
99     if(!strcmp(who, user)) {
100       g_free(who);
101       return 0;
102     }
103     g_free(who);
104   } while(gtk_tree_model_iter_next(GTK_TREE_MODEL(users_list), iter));
105   return -1;
106 }
107
108 /** @brief Called with the list of users
109  *
110  * Called:
111  * - at startup to populate the initial list
112  * - when we add a user
113  * - maybe in the future when we delete a user
114  *
115  * If users_deferred_select is set then that user is selected.
116  */
117 static void users_got_list(void attribute((unused)) *v,
118                            const char *err,
119                            int nvec, char **vec) {
120   int n;
121   GtkTreeIter iter;
122
123   if(err) {
124     popup_protocol_error(0, err);
125     return;
126   }
127   /* Present users in alphabetical order */
128   qsort(vec, nvec, sizeof (char *), usercmp);
129   /* Set the list contents */
130   gtk_list_store_clear(users_list);
131   for(n = 0; n < nvec; ++n)
132     gtk_list_store_insert_with_values(users_list, &iter, n/*position*/,
133                                       0, vec[n], /* column 0 */
134                                       -1);       /* no more columns */
135   /* Only show the window when the list is populated */
136   gtk_widget_show_all(users_window);
137   if(users_deferred_select) {
138     if(!users_find_user(users_deferred_select, &iter))
139       gtk_tree_selection_select_iter(users_selection, &iter);
140     users_deferred_select = 0;
141   }
142 }
143
144 /** @brief Text should be visible */
145 #define DETAIL_VISIBLE 1
146
147 /** @brief Text should be editable */
148 #define DETAIL_EDITABLE 2
149
150 /** @brief Add a row to the user detail table */
151 static void users_detail_generic(const char *title,
152                                  GtkWidget *selector) {
153   const int row = users_details_row++;
154   GtkWidget *const label = gtk_label_new(title);
155   gtk_misc_set_alignment(GTK_MISC(label), 1, 0);
156   gtk_table_attach(GTK_TABLE(users_details_table),
157                    label,
158                    0, 1,                /* left/right_attach */
159                    row, row+1,          /* top/bottom_attach */
160                    GTK_FILL,            /* xoptions */
161                    0,                   /* yoptions */
162                    1, 1);               /* x/ypadding */
163   gtk_table_attach(GTK_TABLE(users_details_table),
164                    selector,
165                    1, 2,                /* left/right_attach */
166                    row, row + 1,        /* top/bottom_attach */
167                    GTK_EXPAND|GTK_FILL, /* xoptions */
168                    GTK_FILL,            /* yoptions */
169                    1, 1);               /* x/ypadding */
170 }
171
172 static void users_entry_changed(GtkEditable attribute((unused)) *editable,
173                                 gpointer attribute((unused)) user_data) {
174   users_details_sensitize_all();
175 }
176
177 /** @brief Add a row to the user details table
178  * @param entryp Where to put GtkEntry
179  * @param title Label for this row
180  * @param value Initial value or NULL
181  * @param flags Flags word
182  */
183 static void users_add_detail(GtkWidget **entryp,
184                              const char *title,
185                              const char *value,
186                              unsigned flags) {
187   GtkWidget *entry;
188
189   if(!(entry = *entryp)) {
190     *entryp = entry = gtk_entry_new();
191     g_signal_connect(entry, "changed",
192                      G_CALLBACK(users_entry_changed), 0);
193     users_detail_generic(title, entry);
194   }
195   gtk_entry_set_visibility(GTK_ENTRY(entry),
196                            !!(flags & DETAIL_VISIBLE));
197   gtk_editable_set_editable(GTK_EDITABLE(entry),
198                             !!(flags & DETAIL_EDITABLE));
199   gtk_entry_set_text(GTK_ENTRY(entry), value ? value : "");
200 }
201
202 /** @brief Add a checkbox for a right
203  * @param title Label for this row
204  * @param value Current value
205  * @param right Right bit
206  */
207 static void users_add_right(const char *title,
208                             rights_type value,
209                             rights_type right) {
210   GtkWidget *check;
211   GtkWidget **checkp = &users_details_rights[leftmost_bit(right)];
212
213   if(!(check = *checkp)) {
214     *checkp = check = gtk_check_button_new_with_label("");
215     users_detail_generic(title, check);
216   }
217   gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check), !!(value & right));
218 }
219
220 /** @brief Set sensitivity of particular mine/random rights bits */
221 static void users_details_sensitize(rights_type r) {
222   const int bit = leftmost_bit(r);
223   const GtkWidget *all = users_details_rights[bit];
224   const int sensitive = (!gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(all))
225                          && users_mode != MODE_NONE);
226
227   gtk_widget_set_sensitive(users_details_rights[bit + 1], sensitive);
228   gtk_widget_set_sensitive(users_details_rights[bit + 2], sensitive);
229 }
230
231 /** @brief Set sensitivity of everything in sight */
232 static void users_details_sensitize_all(void) {
233   int n;
234   const char *report = 0;
235
236   for(n = 0; n < 32; ++n)
237     if(users_details_rights[n])
238       gtk_widget_set_sensitive(users_details_rights[n], users_mode != MODE_NONE);
239   gtk_widget_set_sensitive(users_details_name, users_mode != MODE_NONE);
240   gtk_widget_set_sensitive(users_details_email, users_mode != MODE_NONE);
241   gtk_widget_set_sensitive(users_details_password, users_mode != MODE_NONE);
242   gtk_widget_set_sensitive(users_details_password2, users_mode != MODE_NONE);
243   users_details_sensitize(RIGHT_MOVE_ANY);
244   users_details_sensitize(RIGHT_REMOVE_ANY);
245   users_details_sensitize(RIGHT_SCRATCH_ANY);
246   int apply_sensitive = 1;
247   if(users_mode == MODE_NONE)
248     apply_sensitive = 0;
249   else {
250     const char *name = gtk_entry_get_text(GTK_ENTRY(users_details_name));
251     const char *email = gtk_entry_get_text(GTK_ENTRY(users_details_email));
252     const char *pw = gtk_entry_get_text(GTK_ENTRY(users_details_password));
253     const char *pw2 = gtk_entry_get_text(GTK_ENTRY(users_details_password2));
254     /* Username must be filled in */
255     if(!*name) {
256       apply_sensitive = 0;
257       if(!report)
258         report = "Must fill in username";
259     }
260     /* Passwords must be nontrivial and match */
261     if(!*pw) {
262       apply_sensitive = 0;
263       if(!report)
264         report = "Must fill in password";
265     }
266     if(strcmp(pw, pw2)) {
267       apply_sensitive = 0;
268       if(!report)
269         report = "Passwords must match";
270     }
271     /* Email address must be somewhat valid */
272     if(*email) {
273       if(!email_valid(email)) {
274         apply_sensitive = 0;
275         report = "Invalid email address";
276       }
277     }
278   }
279   gtk_widget_set_sensitive(users_apply_button, apply_sensitive);
280   gtk_widget_set_sensitive(users_delete_button, !!users_selected);
281   users_set_report(report);
282 }
283
284 /** @brief Called when an _ALL widget is toggled
285  *
286  * Modifies sensitivity of the corresponding _MINE and _RANDOM widgets.  We
287  * just do the lot rather than trying to figure out which one changed,
288  */
289 static void users_any_toggled(GtkToggleButton attribute((unused)) *togglebutton,
290                               gpointer attribute((unused)) user_data) {
291   users_details_sensitize_all();
292 }
293
294 /** @brief Add a checkbox for a three-right group
295  * @param title Label for this row
296  * @param bits Rights bits (not masked or normalized)
297  * @param mask Mask for this group (must be 7*2^n)
298  */
299 static void users_add_right_group(const char *title,
300                                   rights_type bits,
301                                   rights_type mask) {
302   const uint32_t first = mask / 7;
303   const int bit = leftmost_bit(first);
304   GtkWidget **widgets = &users_details_rights[bit], *any, *mine, *rnd;
305
306   if(!*widgets) {
307     GtkWidget *hbox = gtk_hbox_new(FALSE, 2);
308
309     any = widgets[0] = gtk_check_button_new_with_label("Any");
310     mine = widgets[1] = gtk_check_button_new_with_label("Own");
311     rnd = widgets[2] = gtk_check_button_new_with_label("Random");
312     gtk_box_pack_start(GTK_BOX(hbox), any, FALSE, FALSE, 0);
313     gtk_box_pack_start(GTK_BOX(hbox), mine, FALSE, FALSE, 0);
314     gtk_box_pack_start(GTK_BOX(hbox), rnd, FALSE, FALSE, 0);
315     users_detail_generic(title, hbox);
316     g_signal_connect(any, "toggled", G_CALLBACK(users_any_toggled), NULL);
317     users_details_rights[bit] = any;
318     users_details_rights[bit + 1] = mine;
319     users_details_rights[bit + 2] = rnd;
320   } else {
321     any = widgets[0];
322     mine = widgets[1];
323     rnd = widgets[2];
324   }
325   /* Discard irrelevant bits */
326   bits &= mask;
327   /* Shift down to bits 0-2; the mask is always 3 contiguous bits */
328   bits >>= bit;
329   gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(any), !!(bits & 1));
330   gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(mine), !!(bits & 2));
331   gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(rnd), !!(bits & 4));
332 }
333
334 /** @brief Called when the details table is destroyed */
335 static void users_details_destroyed(GtkWidget attribute((unused)) *widget,
336                                     GtkWidget attribute((unused)) **wp) {
337   users_details_table = 0;
338   g_object_unref(users_list);
339   users_list = 0;
340   users_details_name = 0;
341   users_details_email = 0;
342   users_details_password = 0;
343   users_details_password2 = 0;
344   memset(users_details_rights, 0, sizeof users_details_rights);
345   /* also users_selection?  Not AFAICT; _get_selection does upref */
346 }
347
348 /** @brief Create or modify the user details table
349  * @param name User name (users_edit()) or NULL (users_add())
350  * @param email Email address
351  * @param rights User rights string
352  * @param password Password
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   },
665   {
666     GTK_STOCK_REMOVE,
667     users_delete,
668     "Delete a user",
669     0
670   },
671 };
672 #define NUSERS_BUTTONS (sizeof users_buttons / sizeof *users_buttons)
673
674 /** @brief Keypress handler */
675 static gboolean users_keypress(GtkWidget attribute((unused)) *widget,
676                                GdkEventKey *event,
677                                gpointer attribute((unused)) user_data) {
678   if(event->state)
679     return FALSE;
680   switch(event->keyval) {
681   case GDK_Escape:
682     gtk_widget_destroy(users_window);
683     return TRUE;
684   default:
685     return FALSE;
686   }
687 }
688
689 /** @brief Pop up the user management window */
690 void manage_users(void) {
691   GtkWidget *tree, *buttons, *hbox, *hbox2, *vbox, *vbox2;
692   GtkCellRenderer *cr;
693   GtkTreeViewColumn *col;
694   
695   /* If the window already exists just raise it */
696   if(users_window) {
697     gtk_window_present(GTK_WINDOW(users_window));
698     return;
699   }
700   /* Destroy old widgets */
701   if(users_reporter)
702     gtk_widget_destroy(users_reporter);
703   /* Create the window */
704   users_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
705   gtk_widget_set_style(users_window, tool_style);
706   g_signal_connect(users_window, "destroy",
707                    G_CALLBACK(gtk_widget_destroyed), &users_window);
708   gtk_window_set_title(GTK_WINDOW(users_window), "User Management");
709   /* Keyboard shortcuts */
710   g_signal_connect(users_window, "key-press-event",
711                    G_CALLBACK(users_keypress), 0);
712   /* default size is too small */
713   gtk_window_set_default_size(GTK_WINDOW(users_window), 240, 240);
714
715   /* Create the list of users and populate it asynchronously */
716   users_list = gtk_list_store_new(1, G_TYPE_STRING);
717   disorder_eclient_users(client, users_got_list, 0);
718   /* Create the view */
719   tree = gtk_tree_view_new_with_model(GTK_TREE_MODEL(users_list));
720   /* ...and the renderers for it */
721   cr = gtk_cell_renderer_text_new();
722   col = gtk_tree_view_column_new_with_attributes("Username",
723                                                  cr,
724                                                  "text", 0,
725                                                  NULL);
726   gtk_tree_view_append_column(GTK_TREE_VIEW(tree), col);
727   /* Get the selection for the view; set its mode; arrange for a callback when
728    * it changes */
729   users_selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(tree));
730   gtk_tree_selection_set_mode(users_selection, GTK_SELECTION_BROWSE);
731   g_signal_connect(users_selection, "changed",
732                    G_CALLBACK(users_selection_changed), NULL);
733
734   /* Create the control buttons */
735   buttons = create_buttons_box(users_buttons,
736                                NUSERS_BUTTONS,
737                                gtk_hbox_new(FALSE, 1));
738   users_delete_button = users_buttons[1].widget;
739
740   /* Buttons live below the list */
741   vbox = gtk_vbox_new(FALSE, 0);
742   gtk_box_pack_start(GTK_BOX(vbox), scroll_widget(tree), TRUE/*expand*/, TRUE/*fill*/, 0);
743   gtk_box_pack_start(GTK_BOX(vbox), buttons, FALSE/*expand*/, FALSE, 0);
744
745   /* Create an empty user details table, and put an apply button below it */
746   users_apply_button = gtk_button_new_from_stock(GTK_STOCK_APPLY);
747   users_makedetails("", "", "", "",
748                     DETAIL_VISIBLE,
749                     DETAIL_VISIBLE);
750   g_signal_connect(users_apply_button, "clicked",
751                    G_CALLBACK(users_apply), NULL);
752   hbox2 = gtk_hbox_new(FALSE, 0);
753   gtk_box_pack_end(GTK_BOX(hbox2), users_apply_button,
754                    FALSE/*expand*/, FALSE, 0);
755   
756   vbox2 = gtk_vbox_new(FALSE, 0);
757   gtk_box_pack_start(GTK_BOX(vbox2), users_details_table,
758                      TRUE/*expand*/, TRUE/*fill*/, 0);
759   gtk_box_pack_start(GTK_BOX(vbox2), gtk_hseparator_new(),
760                      FALSE/*expand*/, FALSE, 0);
761   gtk_box_pack_start(GTK_BOX(vbox2), users_make_reporter(),
762                      FALSE/*expand*/, FALSE, 0);
763   gtk_box_pack_start(GTK_BOX(vbox2), hbox2,
764                      FALSE/*expand*/, FALSE, 0);
765   
766   /* User details are to the right of the list.  We put in a pointless event
767    * box as as spacer, so that the longest label in the user details isn't
768    * cuddled up to the user list. */
769   hbox = gtk_hbox_new(FALSE, 0);
770   gtk_box_pack_start(GTK_BOX(hbox), vbox, FALSE/*expand*/, FALSE, 0);
771   gtk_box_pack_start(GTK_BOX(hbox), gtk_event_box_new(), FALSE/*expand*/, FALSE, 2);
772   gtk_box_pack_start(GTK_BOX(hbox), vbox2, TRUE/*expand*/, TRUE/*fill*/, 0);
773   gtk_container_add(GTK_CONTAINER(users_window), frame_widget(hbox, NULL));
774 }
775
776 /*
777 Local Variables:
778 c-basic-offset:2
779 comment-column:40
780 fill-column:79
781 indent-tabs-mode:nil
782 End:
783 */