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