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