chiark / gitweb /
Disobedience checks server version and expects rights-changed
[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"
f66203a2
RK
44#include "sendmail.h"
45
46static void users_details_sensitize_all(void);
47static void users_set_report(const char *msg);
ffc4dbaf
RK
48
49static GtkWidget *users_window;
50static GtkListStore *users_list;
4aa8a0a4 51static GtkTreeSelection *users_selection;
ffc4dbaf 52
f44417cf
RK
53static GtkWidget *users_details_table;
54static GtkWidget *users_apply_button;
55static GtkWidget *users_delete_button;
54e4791e
RK
56static GtkWidget *users_details_name;
57static GtkWidget *users_details_email;
58static GtkWidget *users_details_password;
59static GtkWidget *users_details_password2;
08874c06 60static GtkWidget *users_details_rights[32];
f66203a2 61static GtkWidget *users_reporter;
f44417cf
RK
62static int users_details_row;
63static const char *users_selected;
6f23269f 64static const char *users_deferred_select;
54e4791e 65
f44417cf
RK
66static int users_mode;
67#define MODE_NONE 0
68#define MODE_ADD 1
69#define MODE_EDIT 2
70
ca462378
RK
71#define mode(X) do { \
72 users_mode = MODE_##X; \
0d719587 73 if(0) fprintf(stderr, "%s:%d: %s(): mode -> %s\n", \
ca462378 74 __FILE__, __LINE__, __FUNCTION__, #X); \
34239ce4 75 users_details_sensitize_all(); \
ca462378
RK
76} while(0)
77
f44417cf 78static const char *users_email, *users_rights, *users_password;
1bcd69c4 79
61682944 80/** @brief qsort() callback for username comparison */
ffc4dbaf
RK
81static int usercmp(const void *a, const void *b) {
82 return strcmp(*(char **)a, *(char **)b);
83}
84
6f23269f
RK
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 */
90static 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
f44417cf
RK
109/** @brief Called with the list of users
110 *
0d719587
RK
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
6f23269f
RK
115 *
116 * If users_deferred_select is set then that user is selected.
f44417cf 117 */
4190a4d9 118static void users_got_list(void attribute((unused)) *v,
abf99697 119 const char *err,
4190a4d9 120 int nvec, char **vec) {
ffc4dbaf
RK
121 int n;
122 GtkTreeIter iter;
123
abf99697
RK
124 if(err) {
125 popup_protocol_error(0, err);
4190a4d9
RK
126 return;
127 }
ffc4dbaf
RK
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);
6f23269f
RK
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 }
ffc4dbaf
RK
143}
144
95ceca70
RK
145/** @brief Text should be visible */
146#define DETAIL_VISIBLE 1
147
148/** @brief Text should be editable */
149#define DETAIL_EDITABLE 2
150
61682944 151/** @brief Add a row to the user detail table */
f44417cf
RK
152static void users_detail_generic(const char *title,
153 GtkWidget *selector) {
154 const int row = users_details_row++;
fbadea5c
RK
155 GtkWidget *const label = gtk_label_new(title);
156 gtk_misc_set_alignment(GTK_MISC(label), 1, 0);
f44417cf 157 gtk_table_attach(GTK_TABLE(users_details_table),
fbadea5c
RK
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 */
f44417cf 164 gtk_table_attach(GTK_TABLE(users_details_table),
fbadea5c
RK
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 */
fbadea5c
RK
171}
172
f66203a2
RK
173static void users_entry_changed(GtkEditable attribute((unused)) *editable,
174 gpointer attribute((unused)) user_data) {
175 users_details_sensitize_all();
176}
177
95ceca70 178/** @brief Add a row to the user details table
f44417cf 179 * @param entryp Where to put GtkEntry
95ceca70
RK
180 * @param title Label for this row
181 * @param value Initial value or NULL
182 * @param flags Flags word
95ceca70 183 */
f44417cf
RK
184static 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();
f66203a2
RK
192 g_signal_connect(entry, "changed",
193 G_CALLBACK(users_entry_changed), 0);
f44417cf
RK
194 users_detail_generic(title, entry);
195 }
95ceca70
RK
196 gtk_entry_set_visibility(GTK_ENTRY(entry),
197 !!(flags & DETAIL_VISIBLE));
198 gtk_editable_set_editable(GTK_EDITABLE(entry),
199 !!(flags & DETAIL_EDITABLE));
f44417cf 200 gtk_entry_set_text(GTK_ENTRY(entry), value ? value : "");
fbadea5c
RK
201}
202
c49dfc50 203/** @brief Add a checkbox for a right
c49dfc50 204 * @param title Label for this row
f44417cf
RK
205 * @param value Current value
206 * @param right Right bit
c49dfc50 207 */
f44417cf
RK
208static 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)) {
1b20de5a 215 *checkp = check = gtk_check_button_new_with_label("");
f44417cf
RK
216 users_detail_generic(title, check);
217 }
218 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check), !!(value & right));
95ceca70 219}
c49dfc50 220
d49df20c
RK
221/** @brief Set sensitivity of particular mine/random rights bits */
222static void users_details_sensitize(rights_type r) {
223 const int bit = leftmost_bit(r);
224 const GtkWidget *all = users_details_rights[bit];
34239ce4 225 const int sensitive = (!gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(all))
0d719587 226 && users_mode != MODE_NONE);
ca462378 227
d49df20c
RK
228 gtk_widget_set_sensitive(users_details_rights[bit + 1], sensitive);
229 gtk_widget_set_sensitive(users_details_rights[bit + 2], sensitive);
230}
231
f44417cf 232/** @brief Set sensitivity of everything in sight */
d49df20c 233static void users_details_sensitize_all(void) {
34239ce4 234 int n;
f66203a2 235 const char *report = 0;
f44417cf 236
34239ce4
RK
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);
d49df20c
RK
244 users_details_sensitize(RIGHT_MOVE_ANY);
245 users_details_sensitize(RIGHT_REMOVE_ANY);
246 users_details_sensitize(RIGHT_SCRATCH_ANY);
f66203a2
RK
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);
f44417cf 281 gtk_widget_set_sensitive(users_delete_button, !!users_selected);
f66203a2 282 users_set_report(report);
d49df20c
RK
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 */
290static void users_any_toggled(GtkToggleButton attribute((unused)) *togglebutton,
291 gpointer attribute((unused)) user_data) {
292 users_details_sensitize_all();
293}
294
c49dfc50 295/** @brief Add a checkbox for a three-right group
c49dfc50
RK
296 * @param title Label for this row
297 * @param bits Rights bits (not masked or normalized)
f44417cf 298 * @param mask Mask for this group (must be 7*2^n)
c49dfc50 299 */
f44417cf 300static void users_add_right_group(const char *title,
c49dfc50 301 rights_type bits,
08874c06 302 rights_type mask) {
08874c06
RK
303 const uint32_t first = mask / 7;
304 const int bit = leftmost_bit(first);
1942ffd7 305 GtkWidget **widgets = &users_details_rights[bit], *any, *mine, *rnd;
f44417cf
RK
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");
1942ffd7 312 rnd = widgets[2] = gtk_check_button_new_with_label("Random");
f44417cf
RK
313 gtk_box_pack_start(GTK_BOX(hbox), any, FALSE, FALSE, 0);
314 gtk_box_pack_start(GTK_BOX(hbox), mine, FALSE, FALSE, 0);
1942ffd7 315 gtk_box_pack_start(GTK_BOX(hbox), rnd, FALSE, FALSE, 0);
f44417cf
RK
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;
1942ffd7 320 users_details_rights[bit + 2] = rnd;
f44417cf
RK
321 } else {
322 any = widgets[0];
323 mine = widgets[1];
1942ffd7 324 rnd = widgets[2];
f44417cf 325 }
c49dfc50
RK
326 /* Discard irrelevant bits */
327 bits &= mask;
328 /* Shift down to bits 0-2; the mask is always 3 contiguous bits */
08874c06 329 bits >>= bit;
c49dfc50
RK
330 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(any), !!(bits & 1));
331 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(mine), !!(bits & 2));
1942ffd7 332 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(rnd), !!(bits & 4));
c49dfc50 333}
95ceca70 334
b7831daf
RK
335/** @brief Called when the details table is destroyed */
336static 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
f44417cf 349/** @brief Create or modify the user details table
54e4791e
RK
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
54e4791e 354 */
f44417cf 355static void users_makedetails(const char *name,
54e4791e 356 const char *email,
fbadea5c 357 const char *rights,
f44417cf
RK
358 const char *password,
359 unsigned nameflags,
360 unsigned flags) {
fbadea5c 361 rights_type r = 0;
54e4791e 362
f44417cf 363 /* Create the table if it doesn't already exist */
b7831daf 364 if(!users_details_table) {
f44417cf 365 users_details_table = gtk_table_new(4, 2, FALSE/*!homogeneous*/);
b7831daf
RK
366 g_signal_connect(users_details_table, "destroy",
367 G_CALLBACK(users_details_destroyed), 0);
368 }
f44417cf
RK
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);
d49df20c 396 users_details_sensitize_all();
54e4791e
RK
397}
398
61682944 399/** @brief Called when the 'add' button is pressed */
ffc4dbaf
RK
400static void users_add(GtkButton attribute((unused)) *button,
401 gpointer attribute((unused)) userdata) {
f44417cf
RK
402 /* Unselect whatever is selected */
403 gtk_tree_selection_unselect_all(users_selection);
404 /* Reset the form */
34239ce4
RK
405 /* TODO it would be better to use the server default_rights if there's no
406 * client setting. */
f44417cf
RK
407 users_makedetails("",
408 "",
34239ce4 409 config->default_rights,
f44417cf
RK
410 "",
411 DETAIL_EDITABLE|DETAIL_VISIBLE,
412 DETAIL_EDITABLE|DETAIL_VISIBLE);
413 /* Remember we're adding a user */
ca462378 414 mode(ADD);
f44417cf
RK
415}
416
0d719587
RK
417static 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
53fa91bb
RK
437/** @brief Called when a user setting has been edited */
438static void users_edituser_completed(void attribute((unused)) *v,
abf99697
RK
439 const char *err) {
440 if(err)
441 popup_submsg(users_window, GTK_MESSAGE_ERROR, err);
6f23269f
RK
442}
443
444/** @brief Called when a new user has been created */
53fa91bb 445static void users_adduser_completed(void *v,
abf99697
RK
446 const char *err) {
447 if(err) {
448 popup_submsg(users_window, GTK_MESSAGE_ERROR, err);
53fa91bb
RK
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;
6f23269f 463 }
0d719587
RK
464}
465
f44417cf
RK
466/** @brief Called when the 'Apply' button is pressed */
467static void users_apply(GtkButton attribute((unused)) *button,
468 gpointer attribute((unused)) userdata) {
6f23269f
RK
469 const char *password;
470 const char *password2;
471 const char *name;
472 const char *email;
0d719587 473
f44417cf
RK
474 switch(users_mode) {
475 case MODE_NONE:
476 return;
477 case MODE_ADD:
6f23269f
RK
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) {
34239ce4
RK
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");
f44417cf 488 return;
34239ce4 489 }
6f23269f 490 if(strcmp(password, password2)) {
34239ce4
RK
491 popup_submsg(users_window, GTK_MESSAGE_ERROR, "Passwords do not match");
492 return;
493 }
6f23269f 494 if(*email && !strchr(email, '@')) {
0d719587
RK
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 }
53fa91bb
RK
500 disorder_eclient_adduser(client,
501 users_adduser_completed,
6f23269f
RK
502 name,
503 password,
0d719587 504 rights_string(users_get_rights()),
53fa91bb
RK
505 kvp_make("user", name,
506 "email", email,
507 (char *)0));
6f23269f 508 /* We switch to no-op mode while creating the user */
34239ce4 509 mode(NONE);
f44417cf
RK
510 break;
511 case MODE_EDIT:
6f23269f
RK
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)) {
34239ce4
RK
517 popup_submsg(users_window, GTK_MESSAGE_ERROR, "Passwords do not match");
518 return;
519 }
6f23269f
RK
520 if(*email && !strchr(email, '@')) {
521 popup_submsg(users_window, GTK_MESSAGE_ERROR, "Invalid email address");
522 return;
523 }
53fa91bb
RK
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);
6f23269f 530 /* We remain in edit mode */
f44417cf
RK
531 break;
532 }
ffc4dbaf
RK
533}
534
61682944 535/** @brief Called when a user has been deleted */
53fa91bb 536static void users_delete_completed(void *v,
abf99697
RK
537 const char *err) {
538 if(err)
539 popup_submsg(users_window, GTK_MESSAGE_ERROR, err);
53fa91bb
RK
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 }
4aa8a0a4
RK
548}
549
61682944 550/** @brief Called when the 'Delete' button is pressed */
ffc4dbaf
RK
551static void users_delete(GtkButton attribute((unused)) *button,
552 gpointer attribute((unused)) userdata) {
4aa8a0a4 553 GtkWidget *yesno;
4aa8a0a4 554 int res;
4aa8a0a4 555
f44417cf 556 if(!users_selected)
6faa6239
RK
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?"
f44417cf
RK
563 " This action cannot be undone.",
564 users_selected);
6faa6239
RK
565 res = gtk_dialog_run(GTK_DIALOG(yesno));
566 gtk_widget_destroy(yesno);
567 if(res == GTK_RESPONSE_YES) {
53fa91bb
RK
568 disorder_eclient_deluser(client, users_delete_completed, users_selected,
569 kvp_make("user", users_selected,
570 (char *)0));
4aa8a0a4 571 }
ffc4dbaf
RK
572}
573
06bfbba4 574static void users_got_email(void attribute((unused)) *v,
abf99697 575 const char *err,
06bfbba4 576 const char *value) {
abf99697
RK
577 if(err)
578 popup_protocol_error(0, err);
1bcd69c4
RK
579 users_email = value;
580}
581
06bfbba4 582static void users_got_rights(void attribute((unused)) *v,
abf99697 583 const char *err,
06bfbba4 584 const char *value) {
abf99697
RK
585 if(err)
586 popup_protocol_error(0, err);
1bcd69c4
RK
587 users_rights = value;
588}
589
06bfbba4 590static void users_got_password(void attribute((unused)) *v,
abf99697 591 const char *err,
06bfbba4 592 const char *value) {
abf99697
RK
593 if(err)
594 popup_protocol_error(0, err);
06bfbba4
RK
595 /* TODO if an error occurred gathering user info, we should react in some
596 * different way */
1bcd69c4 597 users_password = value;
f44417cf 598 users_makedetails(users_selected,
1bcd69c4
RK
599 users_email,
600 users_rights,
f44417cf
RK
601 users_password,
602 DETAIL_VISIBLE,
603 DETAIL_EDITABLE|DETAIL_VISIBLE);
ca462378 604 mode(EDIT);
1bcd69c4
RK
605}
606
f44417cf
RK
607/** @brief Called when the selection MIGHT have changed */
608static 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))
54e4791e 625 return;
f44417cf
RK
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);
ca462378
RK
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* */
ffc4dbaf
RK
641}
642
f66203a2
RK
643static 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 }
649 return users_reporter;
650}
651
652static void users_set_report(const char *msg) {
653 gtk_label_set_text(GTK_LABEL(users_make_reporter()), msg ? msg : "");
654}
655
f44417cf
RK
656/** @brief Table of buttons below the user list */
657static struct button users_buttons[] = {
ffc4dbaf 658 {
14f8878f 659 GTK_STOCK_ADD,
ffc4dbaf 660 users_add,
f44417cf
RK
661 "Create a new user",
662 0
ffc4dbaf
RK
663 },
664 {
14f8878f 665 GTK_STOCK_REMOVE,
ffc4dbaf 666 users_delete,
f44417cf
RK
667 "Delete a user",
668 0
ffc4dbaf
RK
669 },
670};
671#define NUSERS_BUTTONS (sizeof users_buttons / sizeof *users_buttons)
672
61682944 673/** @brief Pop up the user management window */
ffc4dbaf 674void manage_users(void) {
14f8878f 675 GtkWidget *tree, *buttons, *hbox, *hbox2, *vbox, *vbox2;
ffc4dbaf
RK
676 GtkCellRenderer *cr;
677 GtkTreeViewColumn *col;
678
679 /* If the window already exists just raise it */
680 if(users_window) {
681 gtk_window_present(GTK_WINDOW(users_window));
682 return;
683 }
684 /* Create the window */
685 users_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
686 gtk_widget_set_style(users_window, tool_style);
687 g_signal_connect(users_window, "destroy",
688 G_CALLBACK(gtk_widget_destroyed), &users_window);
689 gtk_window_set_title(GTK_WINDOW(users_window), "User Management");
690 /* default size is too small */
691 gtk_window_set_default_size(GTK_WINDOW(users_window), 240, 240);
692
693 /* Create the list of users and populate it asynchronously */
694 users_list = gtk_list_store_new(1, G_TYPE_STRING);
695 disorder_eclient_users(client, users_got_list, 0);
696 /* Create the view */
697 tree = gtk_tree_view_new_with_model(GTK_TREE_MODEL(users_list));
698 /* ...and the renderers for it */
699 cr = gtk_cell_renderer_text_new();
700 col = gtk_tree_view_column_new_with_attributes("Username",
701 cr,
702 "text", 0,
703 NULL);
704 gtk_tree_view_append_column(GTK_TREE_VIEW(tree), col);
f44417cf
RK
705 /* Get the selection for the view; set its mode; arrange for a callback when
706 * it changes */
4aa8a0a4
RK
707 users_selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(tree));
708 gtk_tree_selection_set_mode(users_selection, GTK_SELECTION_BROWSE);
f44417cf
RK
709 g_signal_connect(users_selection, "changed",
710 G_CALLBACK(users_selection_changed), NULL);
711
ffc4dbaf
RK
712 /* Create the control buttons */
713 buttons = create_buttons_box(users_buttons,
714 NUSERS_BUTTONS,
f44417cf
RK
715 gtk_hbox_new(FALSE, 1));
716 users_delete_button = users_buttons[1].widget;
717
718 /* Buttons live below the list */
e18c4734 719 vbox = gtk_vbox_new(FALSE, 0);
ed464350 720 gtk_box_pack_start(GTK_BOX(vbox), scroll_widget(tree), TRUE/*expand*/, TRUE/*fill*/, 0);
f44417cf
RK
721 gtk_box_pack_start(GTK_BOX(vbox), buttons, FALSE/*expand*/, FALSE, 0);
722
723 /* Create an empty user details table, and put an apply button below it */
ca462378 724 users_apply_button = gtk_button_new_from_stock(GTK_STOCK_APPLY);
f44417cf
RK
725 users_makedetails("", "", "", "",
726 DETAIL_VISIBLE,
727 DETAIL_VISIBLE);
f44417cf
RK
728 g_signal_connect(users_apply_button, "clicked",
729 G_CALLBACK(users_apply), NULL);
14f8878f
RK
730 hbox2 = gtk_hbox_new(FALSE, 0);
731 gtk_box_pack_end(GTK_BOX(hbox2), users_apply_button,
732 FALSE/*expand*/, FALSE, 0);
733
734 vbox2 = gtk_vbox_new(FALSE, 0);
f44417cf
RK
735 gtk_box_pack_start(GTK_BOX(vbox2), users_details_table,
736 TRUE/*expand*/, TRUE/*fill*/, 0);
f66203a2
RK
737 gtk_box_pack_start(GTK_BOX(vbox2), gtk_hseparator_new(),
738 FALSE/*expand*/, FALSE, 0);
739 gtk_box_pack_start(GTK_BOX(vbox2), users_make_reporter(),
740 FALSE/*expand*/, FALSE, 0);
14f8878f 741 gtk_box_pack_start(GTK_BOX(vbox2), hbox2,
f44417cf
RK
742 FALSE/*expand*/, FALSE, 0);
743
e18c4734
RK
744 /* User details are to the right of the list. We put in a pointless event
745 * box as as spacer, so that the longest label in the user details isn't
746 * cuddled up to the user list. */
747 hbox = gtk_hbox_new(FALSE, 0);
748 gtk_box_pack_start(GTK_BOX(hbox), vbox, FALSE/*expand*/, FALSE, 0);
749 gtk_box_pack_start(GTK_BOX(hbox), gtk_event_box_new(), FALSE/*expand*/, FALSE, 2);
750 gtk_box_pack_start(GTK_BOX(hbox), vbox2, TRUE/*expand*/, TRUE/*fill*/, 0);
751 gtk_container_add(GTK_CONTAINER(users_window), frame_widget(hbox, NULL));
ffc4dbaf
RK
752}
753
754/*
755Local Variables:
756c-basic-offset:2
757comment-column:40
758fill-column:79
759indent-tabs-mode:nil
760End:
761*/