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