chiark / gitweb /
eclient now passes NULL for 555 responses rather than calling the
[disorder] / disobedience / users.c
CommitLineData
ffc4dbaf
RK
1/*
2 * This file is part of DisOrder
3 * Copyright (C) 2008 Richard Kettlewell
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18 * USA
19 */
20/** @file disobedience/users.c
21 * @brief User management for Disobedience
22 */
23
24#include "disobedience.h"
25
26static GtkWidget *users_window;
27static GtkListStore *users_list;
4aa8a0a4 28static GtkTreeSelection *users_selection;
ffc4dbaf 29
54e4791e
RK
30static GtkWidget *users_details_window;
31static GtkWidget *users_details_name;
32static GtkWidget *users_details_email;
33static GtkWidget *users_details_password;
34static GtkWidget *users_details_password2;
35//static GtkWidget *users_details_rights;
36
ffc4dbaf
RK
37static int usercmp(const void *a, const void *b) {
38 return strcmp(*(char **)a, *(char **)b);
39}
40
41static void users_got_list(void attribute((unused)) *v, int nvec, char **vec) {
42 int n;
43 GtkTreeIter iter;
44
45 /* Present users in alphabetical order */
46 qsort(vec, nvec, sizeof (char *), usercmp);
47 /* Set the list contents */
48 gtk_list_store_clear(users_list);
49 for(n = 0; n < nvec; ++n)
50 gtk_list_store_insert_with_values(users_list, &iter, n/*position*/,
51 0, vec[n], /* column 0 */
52 -1); /* no more columns */
53 /* Only show the window when the list is populated */
54 gtk_widget_show_all(users_window);
55}
56
6faa6239
RK
57static char *users_getuser(void) {
58 GtkTreeIter iter;
59 char *who, *c;
60
61 if(gtk_tree_selection_get_selected(users_selection, 0, &iter)) {
62 gtk_tree_model_get(GTK_TREE_MODEL(users_list), &iter,
63 0, &who, -1);
64 if(who) {
65 c = xstrdup(who);
66 g_free(who);
67 return c;
68 }
69 }
70 return 0;
71}
72
95ceca70
RK
73/** @brief Text should be visible */
74#define DETAIL_VISIBLE 1
75
76/** @brief Text should be editable */
77#define DETAIL_EDITABLE 2
78
fbadea5c
RK
79static GtkWidget *users_detail_generic(GtkWidget *table,
80 int *rowp,
81 const char *title,
82 GtkWidget *selector) {
83 const int row = (*rowp)++;
84 GtkWidget *const label = gtk_label_new(title);
85 gtk_misc_set_alignment(GTK_MISC(label), 1, 0);
86 gtk_table_attach(GTK_TABLE(table),
87 label,
88 0, 1, /* left/right_attach */
89 row, row+1, /* top/bottom_attach */
90 GTK_FILL, /* xoptions */
91 0, /* yoptions */
92 1, 1); /* x/ypadding */
93 gtk_table_attach(GTK_TABLE(table),
94 selector,
95 1, 2, /* left/right_attach */
96 row, row + 1, /* top/bottom_attach */
97 GTK_EXPAND|GTK_FILL, /* xoptions */
98 GTK_FILL, /* yoptions */
99 1, 1); /* x/ypadding */
100 return selector;
101}
102
95ceca70
RK
103/** @brief Add a row to the user details table
104 * @param table Containin table widget
105 * @param rowp Pointer to row number, incremented
106 * @param title Label for this row
107 * @param value Initial value or NULL
108 * @param flags Flags word
95ceca70
RK
109 * @return The text entry widget
110 */
111static GtkWidget *users_add_detail(GtkWidget *table,
112 int *rowp,
113 const char *title,
114 const char *value,
115 unsigned flags) {
fbadea5c
RK
116 GtkWidget *entry = gtk_entry_new();
117
95ceca70
RK
118 gtk_entry_set_visibility(GTK_ENTRY(entry),
119 !!(flags & DETAIL_VISIBLE));
120 gtk_editable_set_editable(GTK_EDITABLE(entry),
121 !!(flags & DETAIL_EDITABLE));
122 if(value)
123 gtk_entry_set_text(GTK_ENTRY(entry), value);
fbadea5c
RK
124 return users_detail_generic(table, rowp, title, entry);
125}
126
c49dfc50
RK
127/** @brief Add a checkbox for a right
128 * @param table Containing table
129 * @param rowp Pointer to row number, incremented
130 * @param title Label for this row
131 * @param value Right bit (masked but not necessarily normalized)
132 * @return Checkbox widget
133 */
fbadea5c
RK
134static GtkWidget *users_add_right(GtkWidget *table,
135 int *rowp,
136 const char *title,
137 rights_type value) {
138 GtkWidget *check = gtk_check_button_new();
139
140 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check), !!value);
141 return users_detail_generic(table, rowp, title, check);
95ceca70 142}
c49dfc50
RK
143
144/** @brief Add a checkbox for a three-right group
145 * @param table Containing table
146 * @param rowp Pointer to row number, incremented
147 * @param title Label for this row
148 * @param bits Rights bits (not masked or normalized)
149 * @param mask Mask for this group (must be 7.2^n)
150 * @param checks Where to store triple of check widgets
151 * @return Checkbox widget
152 */
153static void users_add_right_group(GtkWidget *table,
154 int *rowp,
155 const char *title,
156 rights_type bits,
157 rights_type mask,
158 GtkWidget *checks[3]) {
159 GtkWidget *any = gtk_check_button_new_with_label("Any");
160 GtkWidget *mine = gtk_check_button_new_with_label("Own");
161 GtkWidget *random = gtk_check_button_new_with_label("Random");
162 GtkWidget *hbox = gtk_hbox_new(FALSE, 2);
163
164 /* Discard irrelevant bits */
165 bits &= mask;
166 /* Shift down to bits 0-2; the mask is always 3 contiguous bits */
167 bits /= (mask / 7);
168 /* If _ANY is set then the other two are implied; we'll take them out when we
169 * set. */
170 if(bits & 1)
171 bits = 7;
172 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(any), !!(bits & 1));
173 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(mine), !!(bits & 2));
174 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(random), !!(bits & 4));
175 gtk_box_pack_start(GTK_BOX(hbox), any, FALSE, FALSE, 0);
176 gtk_box_pack_start(GTK_BOX(hbox), mine, FALSE, FALSE, 0);
177 gtk_box_pack_start(GTK_BOX(hbox), random, FALSE, FALSE, 0);
178 if(checks) {
179 checks[0] = any;
180 checks[1] = mine;
181 checks[2] = random;
182 }
183 users_detail_generic(table, rowp, title, hbox);
184}
95ceca70
RK
185
186
54e4791e
RK
187/** @brief Create the user details window
188 * @param title Window title
189 * @param name User name (users_edit()) or NULL (users_add())
190 * @param email Email address
191 * @param rights User rights string
192 * @param password Password
193 *
194 * This is used both by users_add() and users_edit().
195 *
196 * The window contains:
197 * - display or edit fields for the username
198 * - edit fields for the email address
199 * - two hidden edit fields for the password (they must match)
200 * - check boxes for the rights
201 */
202static void users_makedetails(const char *title,
203 const char *name,
204 const char *email,
fbadea5c 205 const char *rights,
54e4791e 206 const char *password) {
95ceca70
RK
207 GtkWidget *table;
208 int row = 0;
fbadea5c 209 rights_type r = 0;
54e4791e
RK
210
211 /* Create the window */
212 users_details_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
213 gtk_widget_set_style(users_details_window, tool_style);
214 g_signal_connect(users_details_window, "destroy",
215 G_CALLBACK(gtk_widget_destroyed), &users_details_window);
216 gtk_window_set_title(GTK_WINDOW(users_details_window), title);
217 gtk_window_set_transient_for(GTK_WINDOW(users_details_window),
218 GTK_WINDOW(users_window));
219 table = gtk_table_new(4, 2, FALSE/*!homogeneous*/);
220
95ceca70
RK
221 users_details_name = users_add_detail(table, &row, "Username", name,
222 (name ? 0 : DETAIL_EDITABLE)
223 |DETAIL_VISIBLE);
54e4791e 224
95ceca70
RK
225 users_details_email = users_add_detail(table, &row, "Email", email,
226 DETAIL_EDITABLE|DETAIL_VISIBLE);
54e4791e 227
95ceca70
RK
228 users_details_password = users_add_detail(table, &row, "Password",
229 password,
230 DETAIL_EDITABLE);
231 users_details_password2 = users_add_detail(table, &row, "Password",
232 password,
233 DETAIL_EDITABLE);
54e4791e 234
fbadea5c
RK
235 parse_rights(rights, &r, 1);
236 users_add_right(table, &row, "Read operations", r & RIGHT_READ);
237 users_add_right(table, &row, "Play track", r & RIGHT_PLAY);
c49dfc50
RK
238 users_add_right_group(table, &row, "Move", r, RIGHT_MOVE__MASK, NULL);
239 users_add_right_group(table, &row, "Remove", r, RIGHT_REMOVE__MASK, NULL);
240 users_add_right_group(table, &row, "Scratch", r, RIGHT_SCRATCH__MASK, NULL);
fbadea5c
RK
241 users_add_right(table, &row, "Set volume", r & RIGHT_VOLUME);
242 users_add_right(table, &row, "Admin operations", r & RIGHT_ADMIN);
243 users_add_right(table, &row, "Rescan", r & RIGHT_RESCAN);
244 users_add_right(table, &row, "Register new users", r & RIGHT_REGISTER);
245 users_add_right(table, &row, "Modify own userinfo", r & RIGHT_USERINFO);
246 users_add_right(table, &row, "Modify track preferences", r & RIGHT_PREFS);
247 users_add_right(table, &row, "Modify global preferences", r & RIGHT_GLOBAL_PREFS);
248 users_add_right(table, &row, "Pause/resume tracks", r & RIGHT_PAUSE);
95ceca70 249
54e4791e
RK
250 gtk_container_add(GTK_CONTAINER(users_details_window), table);
251 gtk_widget_show_all(users_details_window);
252}
253
ffc4dbaf
RK
254static void users_add(GtkButton attribute((unused)) *button,
255 gpointer attribute((unused)) userdata) {
256}
257
4aa8a0a4
RK
258static void users_deleted_error(struct callbackdata attribute((unused)) *cbd,
259 int attribute((unused)) code,
260 const char *msg) {
261 popup_msg(GTK_MESSAGE_ERROR, msg);
262}
263
264static void users_deleted(void *v) {
265 const struct callbackdata *const cbd = v;
266 GtkTreeIter iter;
267 char *who;
268
269 /* Find the user */
270 if(!gtk_tree_model_get_iter_first(GTK_TREE_MODEL(users_list), &iter))
271 return;
272 do {
273 gtk_tree_model_get(GTK_TREE_MODEL(users_list), &iter,
274 0, &who, -1);
275 if(!strcmp(who, cbd->u.user))
276 break;
277 g_free(who);
278 who = 0;
279 } while(gtk_tree_model_iter_next(GTK_TREE_MODEL(users_list), &iter));
280 /* Remove them */
281 gtk_list_store_remove(users_list, &iter);
282 g_free(who);
283}
284
ffc4dbaf
RK
285static void users_delete(GtkButton attribute((unused)) *button,
286 gpointer attribute((unused)) userdata) {
4aa8a0a4
RK
287 GtkWidget *yesno;
288 char *who;
289 int res;
290 struct callbackdata *cbd;
291
6faa6239
RK
292 if(!(who = users_getuser()))
293 return;
294 yesno = gtk_message_dialog_new(GTK_WINDOW(users_window),
295 GTK_DIALOG_MODAL,
296 GTK_MESSAGE_QUESTION,
297 GTK_BUTTONS_YES_NO,
298 "Do you really want to delete user %s?"
299 " This action cannot be undone.", who);
300 res = gtk_dialog_run(GTK_DIALOG(yesno));
301 gtk_widget_destroy(yesno);
302 if(res == GTK_RESPONSE_YES) {
303 cbd = xmalloc(sizeof *cbd);
304 cbd->onerror = users_deleted_error;
305 cbd->u.user = who;
306 disorder_eclient_deluser(client, users_deleted, cbd->u.user, cbd);
4aa8a0a4 307 }
ffc4dbaf
RK
308}
309
310static void users_edit(GtkButton attribute((unused)) *button,
311 gpointer attribute((unused)) userdata) {
54e4791e 312 char *who;
6faa6239 313
54e4791e
RK
314 if(!(who = users_getuser()))
315 return;
fbadea5c 316 users_makedetails("editing user details", who, "foo@bar", "play", "wobble");
ffc4dbaf
RK
317}
318
319static const struct button users_buttons[] = {
320 {
321 "Add user",
322 users_add,
323 "Create a new user"
324 },
325 {
326 "Edit user",
327 users_edit,
328 "Edit a user"
329 },
330 {
331 "Delete user",
332 users_delete,
333 "Delete a user"
334 },
335};
336#define NUSERS_BUTTONS (sizeof users_buttons / sizeof *users_buttons)
337
338void manage_users(void) {
339 GtkWidget *tree, *buttons, *hbox;
340 GtkCellRenderer *cr;
341 GtkTreeViewColumn *col;
342
343 /* If the window already exists just raise it */
344 if(users_window) {
345 gtk_window_present(GTK_WINDOW(users_window));
346 return;
347 }
348 /* Create the window */
349 users_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
350 gtk_widget_set_style(users_window, tool_style);
351 g_signal_connect(users_window, "destroy",
352 G_CALLBACK(gtk_widget_destroyed), &users_window);
353 gtk_window_set_title(GTK_WINDOW(users_window), "User Management");
354 /* default size is too small */
355 gtk_window_set_default_size(GTK_WINDOW(users_window), 240, 240);
356
357 /* Create the list of users and populate it asynchronously */
358 users_list = gtk_list_store_new(1, G_TYPE_STRING);
359 disorder_eclient_users(client, users_got_list, 0);
360 /* Create the view */
361 tree = gtk_tree_view_new_with_model(GTK_TREE_MODEL(users_list));
362 /* ...and the renderers for it */
363 cr = gtk_cell_renderer_text_new();
364 col = gtk_tree_view_column_new_with_attributes("Username",
365 cr,
366 "text", 0,
367 NULL);
368 gtk_tree_view_append_column(GTK_TREE_VIEW(tree), col);
4aa8a0a4
RK
369 /* Get the selection for the view and set its mode */
370 users_selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(tree));
371 gtk_tree_selection_set_mode(users_selection, GTK_SELECTION_BROWSE);
ffc4dbaf
RK
372 /* Create the control buttons */
373 buttons = create_buttons_box(users_buttons,
374 NUSERS_BUTTONS,
375 gtk_vbox_new(FALSE, 1));
376 /* Put it all together in an hbox */
377 hbox = gtk_hbox_new(FALSE, 2);
378 gtk_box_pack_start(GTK_BOX(hbox), tree, TRUE/*expand*/, TRUE/*fill*/, 0);
379 gtk_box_pack_start(GTK_BOX(hbox), buttons, FALSE, FALSE, 0);
380 gtk_container_add(GTK_CONTAINER(users_window), hbox);
381}
382
383/*
384Local Variables:
385c-basic-offset:2
386comment-column:40
387fill-column:79
388indent-tabs-mode:nil
389End:
390*/