| 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 | |
| 29 | static int usercmp(const void *a, const void *b) { |
| 30 | return strcmp(*(char **)a, *(char **)b); |
| 31 | } |
| 32 | |
| 33 | static void users_got_list(void attribute((unused)) *v, int nvec, char **vec) { |
| 34 | int n; |
| 35 | GtkTreeIter iter; |
| 36 | |
| 37 | /* Present users in alphabetical order */ |
| 38 | qsort(vec, nvec, sizeof (char *), usercmp); |
| 39 | /* Set the list contents */ |
| 40 | gtk_list_store_clear(users_list); |
| 41 | for(n = 0; n < nvec; ++n) |
| 42 | gtk_list_store_insert_with_values(users_list, &iter, n/*position*/, |
| 43 | 0, vec[n], /* column 0 */ |
| 44 | -1); /* no more columns */ |
| 45 | /* Only show the window when the list is populated */ |
| 46 | gtk_widget_show_all(users_window); |
| 47 | } |
| 48 | |
| 49 | static void users_add(GtkButton attribute((unused)) *button, |
| 50 | gpointer attribute((unused)) userdata) { |
| 51 | } |
| 52 | |
| 53 | static void users_delete(GtkButton attribute((unused)) *button, |
| 54 | gpointer attribute((unused)) userdata) { |
| 55 | } |
| 56 | |
| 57 | static void users_edit(GtkButton attribute((unused)) *button, |
| 58 | gpointer attribute((unused)) userdata) { |
| 59 | } |
| 60 | |
| 61 | static const struct button users_buttons[] = { |
| 62 | { |
| 63 | "Add user", |
| 64 | users_add, |
| 65 | "Create a new user" |
| 66 | }, |
| 67 | { |
| 68 | "Edit user", |
| 69 | users_edit, |
| 70 | "Edit a user" |
| 71 | }, |
| 72 | { |
| 73 | "Delete user", |
| 74 | users_delete, |
| 75 | "Delete a user" |
| 76 | }, |
| 77 | }; |
| 78 | #define NUSERS_BUTTONS (sizeof users_buttons / sizeof *users_buttons) |
| 79 | |
| 80 | void manage_users(void) { |
| 81 | GtkWidget *tree, *buttons, *hbox; |
| 82 | GtkCellRenderer *cr; |
| 83 | GtkTreeViewColumn *col; |
| 84 | |
| 85 | /* If the window already exists just raise it */ |
| 86 | if(users_window) { |
| 87 | gtk_window_present(GTK_WINDOW(users_window)); |
| 88 | return; |
| 89 | } |
| 90 | /* Create the window */ |
| 91 | users_window = gtk_window_new(GTK_WINDOW_TOPLEVEL); |
| 92 | gtk_widget_set_style(users_window, tool_style); |
| 93 | g_signal_connect(users_window, "destroy", |
| 94 | G_CALLBACK(gtk_widget_destroyed), &users_window); |
| 95 | gtk_window_set_title(GTK_WINDOW(users_window), "User Management"); |
| 96 | /* default size is too small */ |
| 97 | gtk_window_set_default_size(GTK_WINDOW(users_window), 240, 240); |
| 98 | |
| 99 | /* Create the list of users and populate it asynchronously */ |
| 100 | users_list = gtk_list_store_new(1, G_TYPE_STRING); |
| 101 | disorder_eclient_users(client, users_got_list, 0); |
| 102 | /* Create the view */ |
| 103 | tree = gtk_tree_view_new_with_model(GTK_TREE_MODEL(users_list)); |
| 104 | /* ...and the renderers for it */ |
| 105 | cr = gtk_cell_renderer_text_new(); |
| 106 | col = gtk_tree_view_column_new_with_attributes("Username", |
| 107 | cr, |
| 108 | "text", 0, |
| 109 | NULL); |
| 110 | gtk_tree_view_append_column(GTK_TREE_VIEW(tree), col); |
| 111 | /* Create the control buttons */ |
| 112 | buttons = create_buttons_box(users_buttons, |
| 113 | NUSERS_BUTTONS, |
| 114 | gtk_vbox_new(FALSE, 1)); |
| 115 | /* Put it all together in an hbox */ |
| 116 | hbox = gtk_hbox_new(FALSE, 2); |
| 117 | gtk_box_pack_start(GTK_BOX(hbox), tree, TRUE/*expand*/, TRUE/*fill*/, 0); |
| 118 | gtk_box_pack_start(GTK_BOX(hbox), buttons, FALSE, FALSE, 0); |
| 119 | gtk_container_add(GTK_CONTAINER(users_window), hbox); |
| 120 | } |
| 121 | |
| 122 | /* |
| 123 | Local Variables: |
| 124 | c-basic-offset:2 |
| 125 | comment-column:40 |
| 126 | fill-column:79 |
| 127 | indent-tabs-mode:nil |
| 128 | End: |
| 129 | */ |