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