2 * This file is part of DisOrder
3 * Copyright (C) 2008 Richard Kettlewell
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.
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.
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
20 /** @file disobedience/users.c
21 * @brief User management for Disobedience
24 #include "disobedience.h"
26 static GtkWidget *users_window;
27 static GtkListStore *users_list;
28 static GtkTreeSelection *users_selection;
30 static GtkWidget *users_details_window;
31 static GtkWidget *users_details_name;
32 static GtkWidget *users_details_email;
33 static GtkWidget *users_details_password;
34 static GtkWidget *users_details_password2;
35 //static GtkWidget *users_details_rights;
37 static const char *users_who, *users_email, *users_rights, *users_password;
39 static int usercmp(const void *a, const void *b) {
40 return strcmp(*(char **)a, *(char **)b);
43 static void users_got_list(void attribute((unused)) *v, int nvec, char **vec) {
47 /* Present users in alphabetical order */
48 qsort(vec, nvec, sizeof (char *), usercmp);
49 /* Set the list contents */
50 gtk_list_store_clear(users_list);
51 for(n = 0; n < nvec; ++n)
52 gtk_list_store_insert_with_values(users_list, &iter, n/*position*/,
53 0, vec[n], /* column 0 */
54 -1); /* no more columns */
55 /* Only show the window when the list is populated */
56 gtk_widget_show_all(users_window);
59 static char *users_getuser(void) {
63 if(gtk_tree_selection_get_selected(users_selection, 0, &iter)) {
64 gtk_tree_model_get(GTK_TREE_MODEL(users_list), &iter,
75 /** @brief Text should be visible */
76 #define DETAIL_VISIBLE 1
78 /** @brief Text should be editable */
79 #define DETAIL_EDITABLE 2
81 static GtkWidget *users_detail_generic(GtkWidget *table,
84 GtkWidget *selector) {
85 const int row = (*rowp)++;
86 GtkWidget *const label = gtk_label_new(title);
87 gtk_misc_set_alignment(GTK_MISC(label), 1, 0);
88 gtk_table_attach(GTK_TABLE(table),
90 0, 1, /* left/right_attach */
91 row, row+1, /* top/bottom_attach */
92 GTK_FILL, /* xoptions */
94 1, 1); /* x/ypadding */
95 gtk_table_attach(GTK_TABLE(table),
97 1, 2, /* left/right_attach */
98 row, row + 1, /* top/bottom_attach */
99 GTK_EXPAND|GTK_FILL, /* xoptions */
100 GTK_FILL, /* yoptions */
101 1, 1); /* x/ypadding */
105 /** @brief Add a row to the user details table
106 * @param table Containin table widget
107 * @param rowp Pointer to row number, incremented
108 * @param title Label for this row
109 * @param value Initial value or NULL
110 * @param flags Flags word
111 * @return The text entry widget
113 static GtkWidget *users_add_detail(GtkWidget *table,
118 GtkWidget *entry = gtk_entry_new();
120 gtk_entry_set_visibility(GTK_ENTRY(entry),
121 !!(flags & DETAIL_VISIBLE));
122 gtk_editable_set_editable(GTK_EDITABLE(entry),
123 !!(flags & DETAIL_EDITABLE));
125 gtk_entry_set_text(GTK_ENTRY(entry), value);
126 return users_detail_generic(table, rowp, title, entry);
129 /** @brief Add a checkbox for a right
130 * @param table Containing table
131 * @param rowp Pointer to row number, incremented
132 * @param title Label for this row
133 * @param value Right bit (masked but not necessarily normalized)
134 * @return Checkbox widget
136 static GtkWidget *users_add_right(GtkWidget *table,
140 GtkWidget *check = gtk_check_button_new();
142 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check), !!value);
143 return users_detail_generic(table, rowp, title, check);
146 /** @brief Add a checkbox for a three-right group
147 * @param table Containing table
148 * @param rowp Pointer to row number, incremented
149 * @param title Label for this row
150 * @param bits Rights bits (not masked or normalized)
151 * @param mask Mask for this group (must be 7.2^n)
152 * @param checks Where to store triple of check widgets
153 * @return Checkbox widget
155 static void users_add_right_group(GtkWidget *table,
160 GtkWidget *checks[3]) {
161 GtkWidget *any = gtk_check_button_new_with_label("Any");
162 GtkWidget *mine = gtk_check_button_new_with_label("Own");
163 GtkWidget *random = gtk_check_button_new_with_label("Random");
164 GtkWidget *hbox = gtk_hbox_new(FALSE, 2);
166 /* Discard irrelevant bits */
168 /* Shift down to bits 0-2; the mask is always 3 contiguous bits */
170 /* If _ANY is set then the other two are implied; we'll take them out when we
174 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(any), !!(bits & 1));
175 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(mine), !!(bits & 2));
176 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(random), !!(bits & 4));
177 gtk_box_pack_start(GTK_BOX(hbox), any, FALSE, FALSE, 0);
178 gtk_box_pack_start(GTK_BOX(hbox), mine, FALSE, FALSE, 0);
179 gtk_box_pack_start(GTK_BOX(hbox), random, FALSE, FALSE, 0);
185 users_detail_generic(table, rowp, title, hbox);
188 /** @brief Create the user details window
189 * @param title Window title
190 * @param name User name (users_edit()) or NULL (users_add())
191 * @param email Email address
192 * @param rights User rights string
193 * @param password Password
195 * This is used both by users_add() and users_edit().
197 * The window contains:
198 * - display or edit fields for the username
199 * - edit fields for the email address
200 * - two hidden edit fields for the password (they must match)
201 * - check boxes for the rights
203 static void users_makedetails(const char *title,
207 const char *password) {
212 /* Create the window */
213 users_details_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
214 gtk_widget_set_style(users_details_window, tool_style);
215 g_signal_connect(users_details_window, "destroy",
216 G_CALLBACK(gtk_widget_destroyed), &users_details_window);
217 gtk_window_set_title(GTK_WINDOW(users_details_window), title);
218 gtk_window_set_transient_for(GTK_WINDOW(users_details_window),
219 GTK_WINDOW(users_window));
220 table = gtk_table_new(4, 2, FALSE/*!homogeneous*/);
222 users_details_name = users_add_detail(table, &row, "Username", name,
223 (name ? 0 : DETAIL_EDITABLE)
226 users_details_email = users_add_detail(table, &row, "Email", email,
227 DETAIL_EDITABLE|DETAIL_VISIBLE);
229 users_details_password = users_add_detail(table, &row, "Password",
232 users_details_password2 = users_add_detail(table, &row, "Password",
236 parse_rights(rights, &r, 1);
237 users_add_right(table, &row, "Read operations", r & RIGHT_READ);
238 users_add_right(table, &row, "Play track", r & RIGHT_PLAY);
239 users_add_right_group(table, &row, "Move", r, RIGHT_MOVE__MASK, NULL);
240 users_add_right_group(table, &row, "Remove", r, RIGHT_REMOVE__MASK, NULL);
241 users_add_right_group(table, &row, "Scratch", r, RIGHT_SCRATCH__MASK, NULL);
242 users_add_right(table, &row, "Set volume", r & RIGHT_VOLUME);
243 users_add_right(table, &row, "Admin operations", r & RIGHT_ADMIN);
244 users_add_right(table, &row, "Rescan", r & RIGHT_RESCAN);
245 users_add_right(table, &row, "Register new users", r & RIGHT_REGISTER);
246 users_add_right(table, &row, "Modify own userinfo", r & RIGHT_USERINFO);
247 users_add_right(table, &row, "Modify track preferences", r & RIGHT_PREFS);
248 users_add_right(table, &row, "Modify global preferences", r & RIGHT_GLOBAL_PREFS);
249 users_add_right(table, &row, "Pause/resume tracks", r & RIGHT_PAUSE);
251 gtk_container_add(GTK_CONTAINER(users_details_window), table);
252 gtk_widget_show_all(users_details_window);
255 static void users_add(GtkButton attribute((unused)) *button,
256 gpointer attribute((unused)) userdata) {
259 static void users_deleted_error(struct callbackdata attribute((unused)) *cbd,
260 int attribute((unused)) code,
262 popup_msg(GTK_MESSAGE_ERROR, msg);
265 static void users_deleted(void *v) {
266 const struct callbackdata *const cbd = v;
271 if(!gtk_tree_model_get_iter_first(GTK_TREE_MODEL(users_list), &iter))
274 gtk_tree_model_get(GTK_TREE_MODEL(users_list), &iter,
276 if(!strcmp(who, cbd->u.user))
280 } while(gtk_tree_model_iter_next(GTK_TREE_MODEL(users_list), &iter));
282 gtk_list_store_remove(users_list, &iter);
286 static void users_delete(GtkButton attribute((unused)) *button,
287 gpointer attribute((unused)) userdata) {
291 struct callbackdata *cbd;
293 if(!(who = users_getuser()))
295 yesno = gtk_message_dialog_new(GTK_WINDOW(users_window),
297 GTK_MESSAGE_QUESTION,
299 "Do you really want to delete user %s?"
300 " This action cannot be undone.", who);
301 res = gtk_dialog_run(GTK_DIALOG(yesno));
302 gtk_widget_destroy(yesno);
303 if(res == GTK_RESPONSE_YES) {
304 cbd = xmalloc(sizeof *cbd);
305 cbd->onerror = users_deleted_error;
307 disorder_eclient_deluser(client, users_deleted, cbd->u.user, cbd);
311 static void users_got_email(void attribute((unused)) *v, const char *value) {
315 static void users_got_rights(void attribute((unused)) *v, const char *value) {
316 users_rights = value;
319 static void users_got_password(void attribute((unused)) *v, const char *value) {
320 users_password = value;
321 users_makedetails("editing user details",
328 static void users_edit(GtkButton attribute((unused)) *button,
329 gpointer attribute((unused)) userdata) {
330 if(!(users_who = users_getuser()))
332 /* Schedule user lookups for all the properties we know about. This is all a
333 * bit ad-hoc but will do for now. */
334 disorder_eclient_userinfo(client, users_got_email, users_who, "email", 0);
335 disorder_eclient_userinfo(client, users_got_rights, users_who, "rights", 0);
336 disorder_eclient_userinfo(client, users_got_password, users_who, "password", 0);
339 static const struct button users_buttons[] = {
356 #define NUSERS_BUTTONS (sizeof users_buttons / sizeof *users_buttons)
358 void manage_users(void) {
359 GtkWidget *tree, *buttons, *hbox;
361 GtkTreeViewColumn *col;
363 /* If the window already exists just raise it */
365 gtk_window_present(GTK_WINDOW(users_window));
368 /* Create the window */
369 users_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
370 gtk_widget_set_style(users_window, tool_style);
371 g_signal_connect(users_window, "destroy",
372 G_CALLBACK(gtk_widget_destroyed), &users_window);
373 gtk_window_set_title(GTK_WINDOW(users_window), "User Management");
374 /* default size is too small */
375 gtk_window_set_default_size(GTK_WINDOW(users_window), 240, 240);
377 /* Create the list of users and populate it asynchronously */
378 users_list = gtk_list_store_new(1, G_TYPE_STRING);
379 disorder_eclient_users(client, users_got_list, 0);
380 /* Create the view */
381 tree = gtk_tree_view_new_with_model(GTK_TREE_MODEL(users_list));
382 /* ...and the renderers for it */
383 cr = gtk_cell_renderer_text_new();
384 col = gtk_tree_view_column_new_with_attributes("Username",
388 gtk_tree_view_append_column(GTK_TREE_VIEW(tree), col);
389 /* Get the selection for the view and set its mode */
390 users_selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(tree));
391 gtk_tree_selection_set_mode(users_selection, GTK_SELECTION_BROWSE);
392 /* Create the control buttons */
393 buttons = create_buttons_box(users_buttons,
395 gtk_vbox_new(FALSE, 1));
396 /* Put it all together in an hbox */
397 hbox = gtk_hbox_new(FALSE, 2);
398 gtk_box_pack_start(GTK_BOX(hbox), tree, TRUE/*expand*/, TRUE/*fill*/, 0);
399 gtk_box_pack_start(GTK_BOX(hbox), buttons, FALSE, FALSE, 0);
400 gtk_container_add(GTK_CONTAINER(users_window), hbox);