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