chiark / gitweb /
6910d43f2fd0a40a6d554d5adcc8c2f141ccb5bb
[disorder] / disobedience / users.c
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
26 static GtkWidget *users_window;
27 static GtkListStore *users_list;
28 static GtkTreeSelection *users_selection;
29
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;
36
37 static int usercmp(const void *a, const void *b) {
38   return strcmp(*(char **)a, *(char **)b);
39 }
40
41 static 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
57 static 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
73 /** @brief Text should be visible */
74 #define DETAIL_VISIBLE 1
75
76 /** @brief Text should be editable */
77 #define DETAIL_EDITABLE 2
78
79 static 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
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
109  * @return The text entry widget
110  */
111 static GtkWidget *users_add_detail(GtkWidget *table,
112                                    int *rowp,
113                                    const char *title,
114                                    const char *value,
115                                    unsigned flags) {
116   GtkWidget *entry = gtk_entry_new();
117
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);
124   return users_detail_generic(table, rowp, title, entry);
125 }
126
127 static GtkWidget *users_add_right(GtkWidget *table,
128                                   int *rowp,
129                                   const char *title,
130                                   rights_type value) {
131   GtkWidget *check = gtk_check_button_new();
132   
133   gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(check), !!value);
134   return users_detail_generic(table, rowp, title, check);
135 }
136                                                    
137
138 /** @brief Create the user details window
139  * @param title Window title
140  * @param name User name (users_edit()) or NULL (users_add())
141  * @param email Email address
142  * @param rights User rights string
143  * @param password Password
144  *
145  * This is used both by users_add() and users_edit().
146  *
147  * The window contains:
148  * - display or edit fields for the username
149  * - edit fields for the email address
150  * - two hidden edit fields for the password (they must match)
151  * - check boxes for the rights
152  */
153 static void users_makedetails(const char *title,
154                               const char *name,
155                               const char *email,
156                               const char *rights,
157                               const char *password) {
158   GtkWidget *table;
159   int row = 0;
160   rights_type r = 0;
161   
162   /* Create the window */
163   users_details_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
164   gtk_widget_set_style(users_details_window, tool_style);
165   g_signal_connect(users_details_window, "destroy",
166                    G_CALLBACK(gtk_widget_destroyed), &users_details_window);
167   gtk_window_set_title(GTK_WINDOW(users_details_window), title);
168   gtk_window_set_transient_for(GTK_WINDOW(users_details_window),
169                                GTK_WINDOW(users_window));
170   table = gtk_table_new(4, 2, FALSE/*!homogeneous*/);
171
172   users_details_name = users_add_detail(table, &row, "Username", name,
173                                         (name ? 0 : DETAIL_EDITABLE)
174                                         |DETAIL_VISIBLE);
175
176   users_details_email = users_add_detail(table, &row, "Email", email,
177                                          DETAIL_EDITABLE|DETAIL_VISIBLE);
178
179   users_details_password = users_add_detail(table, &row, "Password",
180                                             password,
181                                             DETAIL_EDITABLE);
182   users_details_password2 = users_add_detail(table, &row, "Password",
183                                              password,
184                                              DETAIL_EDITABLE);
185
186   parse_rights(rights, &r, 1);
187   users_add_right(table, &row, "Read operations", r & RIGHT_READ);
188   users_add_right(table, &row, "Play track", r & RIGHT_PLAY);
189   /* TODO move */
190   /* TODO remove */
191   /* TODO scratch */
192   users_add_right(table, &row, "Set volume", r & RIGHT_VOLUME);
193   users_add_right(table, &row, "Admin operations", r & RIGHT_ADMIN);
194   users_add_right(table, &row, "Rescan", r & RIGHT_RESCAN);
195   users_add_right(table, &row, "Register new users", r & RIGHT_REGISTER);
196   users_add_right(table, &row, "Modify own userinfo", r & RIGHT_USERINFO);
197   users_add_right(table, &row, "Modify track preferences", r & RIGHT_PREFS);
198   users_add_right(table, &row, "Modify global preferences", r & RIGHT_GLOBAL_PREFS);
199   users_add_right(table, &row, "Pause/resume tracks", r & RIGHT_PAUSE);
200
201   gtk_container_add(GTK_CONTAINER(users_details_window), table);
202   gtk_widget_show_all(users_details_window);
203 }
204
205 static void users_add(GtkButton attribute((unused)) *button,
206                       gpointer attribute((unused)) userdata) {
207 }
208
209 static void users_deleted_error(struct callbackdata attribute((unused)) *cbd,
210                                 int attribute((unused)) code,
211                                 const char *msg) {
212   popup_msg(GTK_MESSAGE_ERROR, msg);
213 }
214
215 static void users_deleted(void *v) {
216   const struct callbackdata *const cbd = v;
217   GtkTreeIter iter;
218   char *who;
219
220   /* Find the user */
221   if(!gtk_tree_model_get_iter_first(GTK_TREE_MODEL(users_list), &iter))
222     return;
223   do {
224     gtk_tree_model_get(GTK_TREE_MODEL(users_list), &iter,
225                        0, &who, -1);
226     if(!strcmp(who, cbd->u.user))
227       break;
228     g_free(who);
229     who = 0;
230   } while(gtk_tree_model_iter_next(GTK_TREE_MODEL(users_list), &iter));
231   /* Remove them */
232   gtk_list_store_remove(users_list, &iter);
233   g_free(who);
234 }
235
236 static void users_delete(GtkButton attribute((unused)) *button,
237                          gpointer attribute((unused)) userdata) {
238   GtkWidget *yesno;
239   char *who;
240   int res;
241   struct callbackdata *cbd;
242
243   if(!(who = users_getuser()))
244     return;
245   yesno = gtk_message_dialog_new(GTK_WINDOW(users_window),
246                                  GTK_DIALOG_MODAL,
247                                  GTK_MESSAGE_QUESTION,
248                                  GTK_BUTTONS_YES_NO,
249                                  "Do you really want to delete user %s?"
250                                  " This action cannot be undone.", who);
251   res = gtk_dialog_run(GTK_DIALOG(yesno));
252   gtk_widget_destroy(yesno);
253   if(res == GTK_RESPONSE_YES) {
254     cbd = xmalloc(sizeof *cbd);
255     cbd->onerror = users_deleted_error;
256     cbd->u.user = who;
257     disorder_eclient_deluser(client, users_deleted, cbd->u.user, cbd);
258   }
259 }
260
261 static void users_edit(GtkButton attribute((unused)) *button,
262                        gpointer attribute((unused)) userdata) {
263   char *who;
264   
265   if(!(who = users_getuser()))
266     return;
267   users_makedetails("editing user details", who, "foo@bar", "play", "wobble");
268 }
269
270 static const struct button users_buttons[] = {
271   {
272     "Add user",
273     users_add,
274     "Create a new user"
275   },
276   {
277     "Edit user",
278     users_edit,
279     "Edit a user"
280   },
281   {
282     "Delete user",
283     users_delete,
284     "Delete a user"
285   },
286 };
287 #define NUSERS_BUTTONS (sizeof users_buttons / sizeof *users_buttons)
288
289 void manage_users(void) {
290   GtkWidget *tree, *buttons, *hbox;
291   GtkCellRenderer *cr;
292   GtkTreeViewColumn *col;
293   
294   /* If the window already exists just raise it */
295   if(users_window) {
296     gtk_window_present(GTK_WINDOW(users_window));
297     return;
298   }
299   /* Create the window */
300   users_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
301   gtk_widget_set_style(users_window, tool_style);
302   g_signal_connect(users_window, "destroy",
303                    G_CALLBACK(gtk_widget_destroyed), &users_window);
304   gtk_window_set_title(GTK_WINDOW(users_window), "User Management");
305   /* default size is too small */
306   gtk_window_set_default_size(GTK_WINDOW(users_window), 240, 240);
307
308   /* Create the list of users and populate it asynchronously */
309   users_list = gtk_list_store_new(1, G_TYPE_STRING);
310   disorder_eclient_users(client, users_got_list, 0);
311   /* Create the view */
312   tree = gtk_tree_view_new_with_model(GTK_TREE_MODEL(users_list));
313   /* ...and the renderers for it */
314   cr = gtk_cell_renderer_text_new();
315   col = gtk_tree_view_column_new_with_attributes("Username",
316                                                  cr,
317                                                  "text", 0,
318                                                  NULL);
319   gtk_tree_view_append_column(GTK_TREE_VIEW(tree), col);
320   /* Get the selection for the view and set its mode */
321   users_selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(tree));
322   gtk_tree_selection_set_mode(users_selection, GTK_SELECTION_BROWSE);
323   /* Create the control buttons */
324   buttons = create_buttons_box(users_buttons,
325                                NUSERS_BUTTONS,
326                                gtk_vbox_new(FALSE, 1));
327   /* Put it all together in an hbox */
328   hbox = gtk_hbox_new(FALSE, 2);
329   gtk_box_pack_start(GTK_BOX(hbox), tree, TRUE/*expand*/, TRUE/*fill*/, 0);
330   gtk_box_pack_start(GTK_BOX(hbox), buttons, FALSE, FALSE, 0);
331   gtk_container_add(GTK_CONTAINER(users_window), hbox);
332 }
333
334 /*
335 Local Variables:
336 c-basic-offset:2
337 comment-column:40
338 fill-column:79
339 indent-tabs-mode:nil
340 End:
341 */