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