chiark / gitweb /
903188733139892e36dfb6d9513dac23e6197fda
[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: @ref RIGHT_ADMIN and @ref RIGHT_USERINFO should be applied here, so we
37  * can give decent error messages.
38  *
39  * TODO: it would be really nice if the Username entry could be removed and new
40  * user names entered in the list, rather off in the details panel.  This may
41  * be possible with a sufficiently clever GtkCellRenderer.
42  */
43
44 #include "disobedience.h"
45 #include "bits.h"
46
47 static GtkWidget *users_window;
48 static GtkListStore *users_list;
49 static GtkTreeSelection *users_selection;
50
51 static GtkWidget *users_details_table;
52 static GtkWidget *users_apply_button;
53 static GtkWidget *users_delete_button;
54 static GtkWidget *users_details_name;
55 static GtkWidget *users_details_email;
56 static GtkWidget *users_details_password;
57 static GtkWidget *users_details_password2;
58 static GtkWidget *users_details_rights[32];
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, int nvec, char **vec) {
116   int n;
117   GtkTreeIter iter;
118
119   /* Present users in alphabetical order */
120   qsort(vec, nvec, sizeof (char *), usercmp);
121   /* Set the list contents */
122   gtk_list_store_clear(users_list);
123   for(n = 0; n < nvec; ++n)
124     gtk_list_store_insert_with_values(users_list, &iter, n/*position*/,
125                                       0, vec[n], /* column 0 */
126                                       -1);       /* no more columns */
127   /* Only show the window when the list is populated */
128   gtk_widget_show_all(users_window);
129   if(users_deferred_select) {
130     if(!users_find_user(users_deferred_select, &iter))
131       gtk_tree_selection_select_iter(users_selection, &iter);
132     users_deferred_select = 0;
133   }
134 }
135
136 /** @brief Text should be visible */
137 #define DETAIL_VISIBLE 1
138
139 /** @brief Text should be editable */
140 #define DETAIL_EDITABLE 2
141
142 /** @brief Add a row to the user detail table */
143 static void users_detail_generic(const char *title,
144                                  GtkWidget *selector) {
145   const int row = users_details_row++;
146   GtkWidget *const label = gtk_label_new(title);
147   gtk_misc_set_alignment(GTK_MISC(label), 1, 0);
148   gtk_table_attach(GTK_TABLE(users_details_table),
149                    label,
150                    0, 1,                /* left/right_attach */
151                    row, row+1,          /* top/bottom_attach */
152                    GTK_FILL,            /* xoptions */
153                    0,                   /* yoptions */
154                    1, 1);               /* x/ypadding */
155   gtk_table_attach(GTK_TABLE(users_details_table),
156                    selector,
157                    1, 2,                /* left/right_attach */
158                    row, row + 1,        /* top/bottom_attach */
159                    GTK_EXPAND|GTK_FILL, /* xoptions */
160                    GTK_FILL,            /* yoptions */
161                    1, 1);               /* x/ypadding */
162 }
163
164 /** @brief Add a row to the user details table
165  * @param entryp Where to put GtkEntry
166  * @param title Label for this row
167  * @param value Initial value or NULL
168  * @param flags Flags word
169  */
170 static void users_add_detail(GtkWidget **entryp,
171                              const char *title,
172                              const char *value,
173                              unsigned flags) {
174   GtkWidget *entry;
175
176   if(!(entry = *entryp)) {
177     *entryp = entry = gtk_entry_new();
178     users_detail_generic(title, entry);
179   }
180   gtk_entry_set_visibility(GTK_ENTRY(entry),
181                            !!(flags & DETAIL_VISIBLE));
182   gtk_editable_set_editable(GTK_EDITABLE(entry),
183                             !!(flags & DETAIL_EDITABLE));
184   gtk_entry_set_text(GTK_ENTRY(entry), value ? value : "");
185 }
186
187 /** @brief Add a checkbox for a right
188  * @param title Label for this row
189  * @param value Current value
190  * @param right Right bit
191  */
192 static void users_add_right(const char *title,
193                             rights_type value,
194                             rights_type right) {
195   GtkWidget *check;
196   GtkWidget **checkp = &users_details_rights[leftmost_bit(right)];
197
198   if(!(check = *checkp)) {
199     *checkp = check = gtk_check_button_new();
200     users_detail_generic(title, check);
201   }
202   gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check), !!(value & right));
203 }
204
205 /** @brief Set sensitivity of particular mine/random rights bits */
206 static void users_details_sensitize(rights_type r) {
207   const int bit = leftmost_bit(r);
208   const GtkWidget *all = users_details_rights[bit];
209   const int sensitive = (!gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(all))
210                          && users_mode != MODE_NONE);
211
212   gtk_widget_set_sensitive(users_details_rights[bit + 1], sensitive);
213   gtk_widget_set_sensitive(users_details_rights[bit + 2], sensitive);
214 }
215
216 /** @brief Set sensitivity of everything in sight */
217 static void users_details_sensitize_all(void) {
218   int n;
219
220   for(n = 0; n < 32; ++n)
221     if(users_details_rights[n])
222       gtk_widget_set_sensitive(users_details_rights[n], users_mode != MODE_NONE);
223   gtk_widget_set_sensitive(users_details_name, users_mode != MODE_NONE);
224   gtk_widget_set_sensitive(users_details_email, users_mode != MODE_NONE);
225   gtk_widget_set_sensitive(users_details_password, users_mode != MODE_NONE);
226   gtk_widget_set_sensitive(users_details_password2, users_mode != MODE_NONE);
227   users_details_sensitize(RIGHT_MOVE_ANY);
228   users_details_sensitize(RIGHT_REMOVE_ANY);
229   users_details_sensitize(RIGHT_SCRATCH_ANY);
230   gtk_widget_set_sensitive(users_apply_button, users_mode != MODE_NONE);
231   gtk_widget_set_sensitive(users_delete_button, !!users_selected);
232 }
233
234 /** @brief Called when an _ALL widget is toggled
235  *
236  * Modifies sensitivity of the corresponding _MINE and _RANDOM widgets.  We
237  * just do the lot rather than trying to figure out which one changed,
238  */
239 static void users_any_toggled(GtkToggleButton attribute((unused)) *togglebutton,
240                               gpointer attribute((unused)) user_data) {
241   users_details_sensitize_all();
242 }
243
244 /** @brief Add a checkbox for a three-right group
245  * @param title Label for this row
246  * @param bits Rights bits (not masked or normalized)
247  * @param mask Mask for this group (must be 7*2^n)
248  */
249 static void users_add_right_group(const char *title,
250                                   rights_type bits,
251                                   rights_type mask) {
252   const uint32_t first = mask / 7;
253   const int bit = leftmost_bit(first);
254   GtkWidget **widgets = &users_details_rights[bit], *any, *mine, *random;
255
256   if(!*widgets) {
257     GtkWidget *hbox = gtk_hbox_new(FALSE, 2);
258
259     any = widgets[0] = gtk_check_button_new_with_label("Any");
260     mine = widgets[1] = gtk_check_button_new_with_label("Own");
261     random = widgets[2] = gtk_check_button_new_with_label("Random");
262     gtk_box_pack_start(GTK_BOX(hbox), any, FALSE, FALSE, 0);
263     gtk_box_pack_start(GTK_BOX(hbox), mine, FALSE, FALSE, 0);
264     gtk_box_pack_start(GTK_BOX(hbox), random, FALSE, FALSE, 0);
265     users_detail_generic(title, hbox);
266     g_signal_connect(any, "toggled", G_CALLBACK(users_any_toggled), NULL);
267     users_details_rights[bit] = any;
268     users_details_rights[bit + 1] = mine;
269     users_details_rights[bit + 2] = random;
270   } else {
271     any = widgets[0];
272     mine = widgets[1];
273     random = widgets[2];
274   }
275   /* Discard irrelevant bits */
276   bits &= mask;
277   /* Shift down to bits 0-2; the mask is always 3 contiguous bits */
278   bits >>= bit;
279   gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(any), !!(bits & 1));
280   gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(mine), !!(bits & 2));
281   gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(random), !!(bits & 4));
282 }
283
284 /** @brief Called when the details table is destroyed */
285 static void users_details_destroyed(GtkWidget attribute((unused)) *widget,
286                                     GtkWidget attribute((unused)) **wp) {
287   users_details_table = 0;
288   g_object_unref(users_list);
289   users_list = 0;
290   users_details_name = 0;
291   users_details_email = 0;
292   users_details_password = 0;
293   users_details_password2 = 0;
294   memset(users_details_rights, 0, sizeof users_details_rights);
295   /* also users_selection?  Not AFAICT; _get_selection does upref */
296 }
297
298 /** @brief Create or modify the user details table
299  * @param name User name (users_edit()) or NULL (users_add())
300  * @param email Email address
301  * @param rights User rights string
302  * @param password Password
303  */
304 static void users_makedetails(const char *name,
305                               const char *email,
306                               const char *rights,
307                               const char *password,
308                               unsigned nameflags,
309                               unsigned flags) {
310   rights_type r = 0;
311   
312   /* Create the table if it doesn't already exist */
313   if(!users_details_table) {
314     users_details_table = gtk_table_new(4, 2, FALSE/*!homogeneous*/);
315     g_signal_connect(users_details_table, "destroy",
316                      G_CALLBACK(users_details_destroyed), 0);
317   }
318
319   /* Create or update the widgets */
320   users_add_detail(&users_details_name, "Username", name,
321                    (DETAIL_EDITABLE|DETAIL_VISIBLE) & nameflags);
322
323   users_add_detail(&users_details_email, "Email", email,
324                    (DETAIL_EDITABLE|DETAIL_VISIBLE) & flags);
325
326   users_add_detail(&users_details_password, "Password", password,
327                    DETAIL_EDITABLE & flags);
328   users_add_detail(&users_details_password2, "Password", password,
329                    DETAIL_EDITABLE & flags);
330
331   parse_rights(rights, &r, 0);
332   users_add_right("Read operations", r, RIGHT_READ);
333   users_add_right("Play track", r, RIGHT_PLAY);
334   users_add_right_group("Move", r, RIGHT_MOVE__MASK);
335   users_add_right_group("Remove", r, RIGHT_REMOVE__MASK);
336   users_add_right_group("Scratch", r, RIGHT_SCRATCH__MASK);
337   users_add_right("Set volume", r, RIGHT_VOLUME);
338   users_add_right("Admin operations", r, RIGHT_ADMIN);
339   users_add_right("Rescan", r, RIGHT_RESCAN);
340   users_add_right("Register new users", r, RIGHT_REGISTER);
341   users_add_right("Modify own userinfo", r, RIGHT_USERINFO);
342   users_add_right("Modify track preferences", r, RIGHT_PREFS);
343   users_add_right("Modify global preferences", r, RIGHT_GLOBAL_PREFS);
344   users_add_right("Pause/resume tracks", r, RIGHT_PAUSE);
345   users_details_sensitize_all();
346 }
347
348 /** @brief Called when the 'add' button is pressed */
349 static void users_add(GtkButton attribute((unused)) *button,
350                       gpointer attribute((unused)) userdata) {
351   /* Unselect whatever is selected */
352   gtk_tree_selection_unselect_all(users_selection);
353   /* Reset the form */
354   /* TODO it would be better to use the server default_rights if there's no
355    * client setting. */
356   users_makedetails("",
357                     "",
358                     config->default_rights,
359                     "",
360                     DETAIL_EDITABLE|DETAIL_VISIBLE,
361                     DETAIL_EDITABLE|DETAIL_VISIBLE);
362   /* Remember we're adding a user */
363   mode(ADD);
364 }
365
366 static rights_type users_get_rights(void) {
367   rights_type r = 0;
368   int n;
369
370   /* Extract the rights value */
371   for(n = 0; n < 32; ++n) {
372     if(users_details_rights[n])
373       if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(users_details_rights[n])))
374          r |= 1 << n;
375   }
376   /* Throw out redundant bits */
377   if(r & RIGHT_REMOVE_ANY)
378     r &= ~(rights_type)(RIGHT_REMOVE_MINE|RIGHT_REMOVE_RANDOM);
379   if(r & RIGHT_MOVE_ANY)
380     r &= ~(rights_type)(RIGHT_MOVE_MINE|RIGHT_MOVE_RANDOM);
381   if(r & RIGHT_SCRATCH_ANY)
382     r &= ~(rights_type)(RIGHT_SCRATCH_MINE|RIGHT_SCRATCH_RANDOM);
383   return r;
384 }
385
386 /** @brief Called when various things fail */
387 static void users_op_failed(struct callbackdata attribute((unused)) *cbd,
388                             int attribute((unused)) code,
389                             const char *msg) {
390   popup_submsg(users_window, GTK_MESSAGE_ERROR, msg);
391 }
392
393 /** @brief Called when a new user has been created */
394 static void users_adduser_completed(void *v) {
395   struct callbackdata *cbd = v;
396
397   /* Now the user is created we can go ahead and set the email address */
398   if(*cbd->u.edituser.email) {
399     struct callbackdata *ncbd = xmalloc(sizeof *cbd);
400     ncbd->onerror = users_op_failed;
401     disorder_eclient_edituser(client, NULL, cbd->u.edituser.user,
402                               "email", cbd->u.edituser.email, ncbd);
403   }
404   /* Refresh the list of users */
405   disorder_eclient_users(client, users_got_list, 0);
406   /* We'll select the newly created user */
407   users_deferred_select = cbd->u.edituser.user;
408 }
409
410 /** @brief Called if creating a new user fails */
411 static void users_adduser_failed(struct callbackdata attribute((unused)) *cbd,
412                                  int attribute((unused)) code,
413                                  const char *msg) {
414   popup_submsg(users_window, GTK_MESSAGE_ERROR, msg);
415   mode(ADD);                            /* Let the user try again */
416 }
417
418 /** @brief Called when the 'Apply' button is pressed */
419 static void users_apply(GtkButton attribute((unused)) *button,
420                         gpointer attribute((unused)) userdata) {
421   struct callbackdata *cbd;
422   const char *password;
423   const char *password2;
424   const char *name;
425   const char *email;
426
427   switch(users_mode) {
428   case MODE_NONE:
429     return;
430   case MODE_ADD:
431     name = xstrdup(gtk_entry_get_text(GTK_ENTRY(users_details_name)));
432     email = xstrdup(gtk_entry_get_text(GTK_ENTRY(users_details_email)));
433     password = xstrdup(gtk_entry_get_text(GTK_ENTRY(users_details_password)));
434     password2 = xstrdup(gtk_entry_get_text(GTK_ENTRY(users_details_password2)));
435     if(!*name) {
436       /* No username.  Really we wanted to desensitize the Apply button when
437        * there's no userame but there doesn't seem to be a signal to detect
438        * changes to the entry text.  Consequently we have error messages
439        * instead.  */
440       popup_submsg(users_window, GTK_MESSAGE_ERROR, "Must enter a username");
441       return;
442     }
443     if(strcmp(password, password2)) {
444       popup_submsg(users_window, GTK_MESSAGE_ERROR, "Passwords do not match");
445       return;
446     }
447     if(*email && !strchr(email, '@')) {
448       /* The server will complain about this but we can give a better error
449        * message this way */
450       popup_submsg(users_window, GTK_MESSAGE_ERROR, "Invalid email address");
451       return;
452     }
453     cbd = xmalloc(sizeof *cbd);
454     cbd->onerror = users_adduser_failed;
455     cbd->u.edituser.user = name;
456     cbd->u.edituser.email = email;
457     disorder_eclient_adduser(client, users_adduser_completed,
458                              name,
459                              password,
460                              rights_string(users_get_rights()),
461                              cbd);
462     /* We switch to no-op mode while creating the user */
463     mode(NONE);
464     break;
465   case MODE_EDIT:
466     /* Ugh, can we de-dupe with above? */
467     email = xstrdup(gtk_entry_get_text(GTK_ENTRY(users_details_email)));
468     password = xstrdup(gtk_entry_get_text(GTK_ENTRY(users_details_password)));
469     password2 = xstrdup(gtk_entry_get_text(GTK_ENTRY(users_details_password2)));
470     if(strcmp(password, password2)) {
471       popup_submsg(users_window, GTK_MESSAGE_ERROR, "Passwords do not match");
472       return;
473     }
474     if(*email && !strchr(email, '@')) {
475       popup_submsg(users_window, GTK_MESSAGE_ERROR, "Invalid email address");
476       return;
477     }
478     cbd = xmalloc(sizeof *cbd);
479     cbd->onerror = users_op_failed;
480     disorder_eclient_edituser(client, NULL, users_selected,
481                               "email", email, cbd);
482     disorder_eclient_edituser(client, NULL, users_selected,
483                               "password", password, cbd);
484     disorder_eclient_edituser(client, NULL, users_selected,
485                               "rights", rights_string(users_get_rights()), cbd);
486     /* We remain in edit mode */
487     break;
488   }
489 }
490
491 /** @brief Called when a user has been deleted */
492 static void users_deleted(void *v) {
493   const struct callbackdata *const cbd = v;
494   GtkTreeIter iter[1];
495
496   if(!users_find_user(cbd->u.user, iter))    /* Find the user... */
497     gtk_list_store_remove(users_list, iter); /* ...and remove them */
498 }
499
500 /** @brief Called when the 'Delete' button is pressed */
501 static void users_delete(GtkButton attribute((unused)) *button,
502                          gpointer attribute((unused)) userdata) {
503   GtkWidget *yesno;
504   int res;
505   struct callbackdata *cbd;
506
507   if(!users_selected)
508     return;
509   yesno = gtk_message_dialog_new(GTK_WINDOW(users_window),
510                                  GTK_DIALOG_MODAL,
511                                  GTK_MESSAGE_QUESTION,
512                                  GTK_BUTTONS_YES_NO,
513                                  "Do you really want to delete user %s?"
514                                  " This action cannot be undone.",
515                                  users_selected);
516   res = gtk_dialog_run(GTK_DIALOG(yesno));
517   gtk_widget_destroy(yesno);
518   if(res == GTK_RESPONSE_YES) {
519     cbd = xmalloc(sizeof *cbd);
520     cbd->onerror = users_op_failed;
521     cbd->u.user = users_selected;
522     disorder_eclient_deluser(client, users_deleted, cbd->u.user, cbd);
523   }
524 }
525
526 static void users_got_email(void attribute((unused)) *v, const char *value) {
527   users_email = value;
528 }
529
530 static void users_got_rights(void attribute((unused)) *v, const char *value) {
531   users_rights = value;
532 }
533
534 static void users_got_password(void attribute((unused)) *v, const char *value) {
535   users_password = value;
536   users_makedetails(users_selected,
537                     users_email,
538                     users_rights,
539                     users_password,
540                     DETAIL_VISIBLE,
541                     DETAIL_EDITABLE|DETAIL_VISIBLE);
542   mode(EDIT);
543 }
544
545 /** @brief Called when the selection MIGHT have changed */
546 static void users_selection_changed(GtkTreeSelection attribute((unused)) *treeselection,
547                                     gpointer attribute((unused)) user_data) {
548   GtkTreeIter iter;
549   char *gselected, *selected;
550   
551   /* Identify the current selection */
552   if(gtk_tree_selection_get_selected(users_selection, 0, &iter)) {
553     gtk_tree_model_get(GTK_TREE_MODEL(users_list), &iter,
554                        0, &gselected, -1);
555     selected = xstrdup(gselected);
556     g_free(gselected);
557   } else
558     selected = 0;
559   /* Eliminate no-change cases */
560   if(!selected && !users_selected)
561     return;
562   if(selected && users_selected && !strcmp(selected, users_selected))
563     return;
564   /* There's been a change; junk the old data and fetch new data in
565    * background. */
566   users_selected = selected;
567   users_makedetails("", "", "", "",
568                     DETAIL_VISIBLE,
569                     DETAIL_VISIBLE);
570   if(users_selected) {
571     disorder_eclient_userinfo(client, users_got_email, users_selected,
572                               "email", 0);
573     disorder_eclient_userinfo(client, users_got_rights, users_selected,
574                               "rights", 0);
575     disorder_eclient_userinfo(client, users_got_password, users_selected,
576                               "password", 0);
577   }
578   mode(NONE);                           /* not editing *yet* */
579 }
580
581 /** @brief Table of buttons below the user list */
582 static struct button users_buttons[] = {
583   {
584     GTK_STOCK_ADD,
585     users_add,
586     "Create a new user",
587     0
588   },
589   {
590     GTK_STOCK_REMOVE,
591     users_delete,
592     "Delete a user",
593     0
594   },
595 };
596 #define NUSERS_BUTTONS (sizeof users_buttons / sizeof *users_buttons)
597
598 /** @brief Pop up the user management window */
599 void manage_users(void) {
600   GtkWidget *tree, *buttons, *hbox, *hbox2, *vbox, *vbox2;
601   GtkCellRenderer *cr;
602   GtkTreeViewColumn *col;
603   
604   /* If the window already exists just raise it */
605   if(users_window) {
606     gtk_window_present(GTK_WINDOW(users_window));
607     return;
608   }
609   /* Create the window */
610   users_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
611   gtk_widget_set_style(users_window, tool_style);
612   g_signal_connect(users_window, "destroy",
613                    G_CALLBACK(gtk_widget_destroyed), &users_window);
614   gtk_window_set_title(GTK_WINDOW(users_window), "User Management");
615   /* default size is too small */
616   gtk_window_set_default_size(GTK_WINDOW(users_window), 240, 240);
617
618   /* Create the list of users and populate it asynchronously */
619   users_list = gtk_list_store_new(1, G_TYPE_STRING);
620   disorder_eclient_users(client, users_got_list, 0);
621   /* Create the view */
622   tree = gtk_tree_view_new_with_model(GTK_TREE_MODEL(users_list));
623   /* ...and the renderers for it */
624   cr = gtk_cell_renderer_text_new();
625   col = gtk_tree_view_column_new_with_attributes("Username",
626                                                  cr,
627                                                  "text", 0,
628                                                  NULL);
629   gtk_tree_view_append_column(GTK_TREE_VIEW(tree), col);
630   /* Get the selection for the view; set its mode; arrange for a callback when
631    * it changes */
632   users_selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(tree));
633   gtk_tree_selection_set_mode(users_selection, GTK_SELECTION_BROWSE);
634   g_signal_connect(users_selection, "changed",
635                    G_CALLBACK(users_selection_changed), NULL);
636
637   /* Create the control buttons */
638   buttons = create_buttons_box(users_buttons,
639                                NUSERS_BUTTONS,
640                                gtk_hbox_new(FALSE, 1));
641   users_delete_button = users_buttons[1].widget;
642
643   /* Buttons live below the list */
644   vbox = gtk_vbox_new(FALSE, 2);
645   gtk_box_pack_start(GTK_BOX(vbox), tree, TRUE/*expand*/, TRUE/*fill*/, 0);
646   gtk_box_pack_start(GTK_BOX(vbox), buttons, FALSE/*expand*/, FALSE, 0);
647
648   /* Create an empty user details table, and put an apply button below it */
649   users_apply_button = gtk_button_new_from_stock(GTK_STOCK_APPLY);
650   users_makedetails("", "", "", "",
651                     DETAIL_VISIBLE,
652                     DETAIL_VISIBLE);
653   g_signal_connect(users_apply_button, "clicked",
654                    G_CALLBACK(users_apply), NULL);
655   hbox2 = gtk_hbox_new(FALSE, 0);
656   gtk_box_pack_end(GTK_BOX(hbox2), users_apply_button,
657                    FALSE/*expand*/, FALSE, 0);
658   
659   vbox2 = gtk_vbox_new(FALSE, 0);
660   gtk_box_pack_start(GTK_BOX(vbox2), users_details_table,
661                      TRUE/*expand*/, TRUE/*fill*/, 0);
662   gtk_box_pack_start(GTK_BOX(vbox2), hbox2,
663                      FALSE/*expand*/, FALSE, 0);
664   
665   /* User details are to the right of the list */
666   hbox = gtk_hbox_new(FALSE, 2);
667   gtk_box_pack_start(GTK_BOX(hbox), vbox, FALSE/*expand*/, FALSE, 2);
668   gtk_box_pack_start(GTK_BOX(hbox), vbox2, TRUE/*expand*/, TRUE/*fill*/, 2);
669   gtk_container_add(GTK_CONTAINER(users_window), hbox);
670 }
671
672 /*
673 Local Variables:
674 c-basic-offset:2
675 comment-column:40
676 fill-column:79
677 indent-tabs-mode:nil
678 End:
679 */