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