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