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