chiark / gitweb /
De-dupe user details form layout code
[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 /** @brief Add a row to the user details table
80  * @param table Containin table widget
81  * @param rowp Pointer to row number, incremented
82  * @param title Label for this row
83  * @param value Initial value or NULL
84  * @param flags Flags word
85  *
86  * @return The text entry widget
87  */
88 static GtkWidget *users_add_detail(GtkWidget *table,
89                                    int *rowp,
90                                    const char *title,
91                                    const char *value,
92                                    unsigned flags) {
93   const int row = (*rowp)++;
94   GtkWidget *label, *entry;
95   
96   label = gtk_label_new(title);
97   gtk_misc_set_alignment(GTK_MISC(label), 1, 0);
98   gtk_table_attach(GTK_TABLE(table),
99                    label,
100                    0, 1,                /* left/right_attach */
101                    row, row+1,          /* top/bottom_attach */
102                    GTK_FILL,            /* xoptions */
103                    0,                   /* yoptions */
104                    1, 1);               /* x/ypadding */
105   entry = gtk_entry_new();
106   gtk_entry_set_visibility(GTK_ENTRY(entry),
107                            !!(flags & DETAIL_VISIBLE));
108   gtk_editable_set_editable(GTK_EDITABLE(entry),
109                             !!(flags & DETAIL_EDITABLE));
110   if(value)
111     gtk_entry_set_text(GTK_ENTRY(entry), value);
112   gtk_table_attach(GTK_TABLE(table),
113                    entry,
114                    1, 2,                /* left/right_attach */
115                    row, row + 1,        /* top/bottom_attach */
116                    GTK_EXPAND|GTK_FILL, /* xoptions */
117                    GTK_FILL,            /* yoptions */
118                    1, 1);               /* x/ypadding */
119   return entry;
120 }
121                                                    
122
123 /** @brief Create the user details window
124  * @param title Window title
125  * @param name User name (users_edit()) or NULL (users_add())
126  * @param email Email address
127  * @param rights User rights string
128  * @param password Password
129  *
130  * This is used both by users_add() and users_edit().
131  *
132  * The window contains:
133  * - display or edit fields for the username
134  * - edit fields for the email address
135  * - two hidden edit fields for the password (they must match)
136  * - check boxes for the rights
137  */
138 static void users_makedetails(const char *title,
139                               const char *name,
140                               const char *email,
141                               const char attribute((unused)) *rights,
142                               const char *password) {
143   GtkWidget *table;
144   int row = 0;
145   
146   /* Create the window */
147   users_details_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
148   gtk_widget_set_style(users_details_window, tool_style);
149   g_signal_connect(users_details_window, "destroy",
150                    G_CALLBACK(gtk_widget_destroyed), &users_details_window);
151   gtk_window_set_title(GTK_WINDOW(users_details_window), title);
152   gtk_window_set_transient_for(GTK_WINDOW(users_details_window),
153                                GTK_WINDOW(users_window));
154   table = gtk_table_new(4, 2, FALSE/*!homogeneous*/);
155
156   users_details_name = users_add_detail(table, &row, "Username", name,
157                                         (name ? 0 : DETAIL_EDITABLE)
158                                         |DETAIL_VISIBLE);
159
160   users_details_email = users_add_detail(table, &row, "Email", email,
161                                          DETAIL_EDITABLE|DETAIL_VISIBLE);
162
163   users_details_password = users_add_detail(table, &row, "Password",
164                                             password,
165                                             DETAIL_EDITABLE);
166   users_details_password2 = users_add_detail(table, &row, "Password",
167                                              password,
168                                              DETAIL_EDITABLE);
169
170   /* TODO rights */
171
172   gtk_container_add(GTK_CONTAINER(users_details_window), table);
173   gtk_widget_show_all(users_details_window);
174 }
175
176 static void users_add(GtkButton attribute((unused)) *button,
177                       gpointer attribute((unused)) userdata) {
178 }
179
180 static void users_deleted_error(struct callbackdata attribute((unused)) *cbd,
181                                 int attribute((unused)) code,
182                                 const char *msg) {
183   popup_msg(GTK_MESSAGE_ERROR, msg);
184 }
185
186 static void users_deleted(void *v) {
187   const struct callbackdata *const cbd = v;
188   GtkTreeIter iter;
189   char *who;
190
191   /* Find the user */
192   if(!gtk_tree_model_get_iter_first(GTK_TREE_MODEL(users_list), &iter))
193     return;
194   do {
195     gtk_tree_model_get(GTK_TREE_MODEL(users_list), &iter,
196                        0, &who, -1);
197     if(!strcmp(who, cbd->u.user))
198       break;
199     g_free(who);
200     who = 0;
201   } while(gtk_tree_model_iter_next(GTK_TREE_MODEL(users_list), &iter));
202   /* Remove them */
203   gtk_list_store_remove(users_list, &iter);
204   g_free(who);
205 }
206
207 static void users_delete(GtkButton attribute((unused)) *button,
208                          gpointer attribute((unused)) userdata) {
209   GtkWidget *yesno;
210   char *who;
211   int res;
212   struct callbackdata *cbd;
213
214   if(!(who = users_getuser()))
215     return;
216   yesno = gtk_message_dialog_new(GTK_WINDOW(users_window),
217                                  GTK_DIALOG_MODAL,
218                                  GTK_MESSAGE_QUESTION,
219                                  GTK_BUTTONS_YES_NO,
220                                  "Do you really want to delete user %s?"
221                                  " This action cannot be undone.", who);
222   res = gtk_dialog_run(GTK_DIALOG(yesno));
223   gtk_widget_destroy(yesno);
224   if(res == GTK_RESPONSE_YES) {
225     cbd = xmalloc(sizeof *cbd);
226     cbd->onerror = users_deleted_error;
227     cbd->u.user = who;
228     disorder_eclient_deluser(client, users_deleted, cbd->u.user, cbd);
229   }
230 }
231
232 static void users_edit(GtkButton attribute((unused)) *button,
233                        gpointer attribute((unused)) userdata) {
234   char *who;
235   
236   if(!(who = users_getuser()))
237     return;
238   users_makedetails("editing user details", who, "foo@bar", "wibble", "wobble");
239 }
240
241 static const struct button users_buttons[] = {
242   {
243     "Add user",
244     users_add,
245     "Create a new user"
246   },
247   {
248     "Edit user",
249     users_edit,
250     "Edit a user"
251   },
252   {
253     "Delete user",
254     users_delete,
255     "Delete a user"
256   },
257 };
258 #define NUSERS_BUTTONS (sizeof users_buttons / sizeof *users_buttons)
259
260 void manage_users(void) {
261   GtkWidget *tree, *buttons, *hbox;
262   GtkCellRenderer *cr;
263   GtkTreeViewColumn *col;
264   
265   /* If the window already exists just raise it */
266   if(users_window) {
267     gtk_window_present(GTK_WINDOW(users_window));
268     return;
269   }
270   /* Create the window */
271   users_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
272   gtk_widget_set_style(users_window, tool_style);
273   g_signal_connect(users_window, "destroy",
274                    G_CALLBACK(gtk_widget_destroyed), &users_window);
275   gtk_window_set_title(GTK_WINDOW(users_window), "User Management");
276   /* default size is too small */
277   gtk_window_set_default_size(GTK_WINDOW(users_window), 240, 240);
278
279   /* Create the list of users and populate it asynchronously */
280   users_list = gtk_list_store_new(1, G_TYPE_STRING);
281   disorder_eclient_users(client, users_got_list, 0);
282   /* Create the view */
283   tree = gtk_tree_view_new_with_model(GTK_TREE_MODEL(users_list));
284   /* ...and the renderers for it */
285   cr = gtk_cell_renderer_text_new();
286   col = gtk_tree_view_column_new_with_attributes("Username",
287                                                  cr,
288                                                  "text", 0,
289                                                  NULL);
290   gtk_tree_view_append_column(GTK_TREE_VIEW(tree), col);
291   /* Get the selection for the view and set its mode */
292   users_selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(tree));
293   gtk_tree_selection_set_mode(users_selection, GTK_SELECTION_BROWSE);
294   /* Create the control buttons */
295   buttons = create_buttons_box(users_buttons,
296                                NUSERS_BUTTONS,
297                                gtk_vbox_new(FALSE, 1));
298   /* Put it all together in an hbox */
299   hbox = gtk_hbox_new(FALSE, 2);
300   gtk_box_pack_start(GTK_BOX(hbox), tree, TRUE/*expand*/, TRUE/*fill*/, 0);
301   gtk_box_pack_start(GTK_BOX(hbox), buttons, FALSE, FALSE, 0);
302   gtk_container_add(GTK_CONTAINER(users_window), hbox);
303 }
304
305 /*
306 Local Variables:
307 c-basic-offset:2
308 comment-column:40
309 fill-column:79
310 indent-tabs-mode:nil
311 End:
312 */