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