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 int usercmp(const void *a, const void *b) {
31 return strcmp(*(char **)a, *(char **)b);
34 static void users_got_list(void attribute((unused)) *v, int nvec, char **vec) {
38 /* Present users in alphabetical order */
39 qsort(vec, nvec, sizeof (char *), usercmp);
40 /* Set the list contents */
41 gtk_list_store_clear(users_list);
42 for(n = 0; n < nvec; ++n)
43 gtk_list_store_insert_with_values(users_list, &iter, n/*position*/,
44 0, vec[n], /* column 0 */
45 -1); /* no more columns */
46 /* Only show the window when the list is populated */
47 gtk_widget_show_all(users_window);
50 static void users_add(GtkButton attribute((unused)) *button,
51 gpointer attribute((unused)) userdata) {
54 static void users_deleted_error(struct callbackdata attribute((unused)) *cbd,
55 int attribute((unused)) code,
57 popup_msg(GTK_MESSAGE_ERROR, msg);
60 static void users_deleted(void *v) {
61 const struct callbackdata *const cbd = v;
66 if(!gtk_tree_model_get_iter_first(GTK_TREE_MODEL(users_list), &iter))
69 gtk_tree_model_get(GTK_TREE_MODEL(users_list), &iter,
71 if(!strcmp(who, cbd->u.user))
75 } while(gtk_tree_model_iter_next(GTK_TREE_MODEL(users_list), &iter));
77 gtk_list_store_remove(users_list, &iter);
81 static void users_delete(GtkButton attribute((unused)) *button,
82 gpointer attribute((unused)) userdata) {
87 struct callbackdata *cbd;
89 if(gtk_tree_selection_get_selected(users_selection, 0, &iter)) {
90 gtk_tree_model_get(GTK_TREE_MODEL(users_list), &iter,
92 yesno = gtk_message_dialog_new(GTK_WINDOW(users_window),
96 "Do you really want to delete user %s?"
97 " This action cannot be undone.", who);
98 res = gtk_dialog_run(GTK_DIALOG(yesno));
99 gtk_widget_destroy(yesno);
100 if(res == GTK_RESPONSE_YES) {
101 cbd = xmalloc(sizeof *cbd);
102 cbd->onerror = users_deleted_error;
103 cbd->u.user = xstrdup(who);
104 disorder_eclient_deluser(client, users_deleted, cbd->u.user, cbd);
110 static void users_edit(GtkButton attribute((unused)) *button,
111 gpointer attribute((unused)) userdata) {
114 static const struct button users_buttons[] = {
131 #define NUSERS_BUTTONS (sizeof users_buttons / sizeof *users_buttons)
133 void manage_users(void) {
134 GtkWidget *tree, *buttons, *hbox;
136 GtkTreeViewColumn *col;
138 /* If the window already exists just raise it */
140 gtk_window_present(GTK_WINDOW(users_window));
143 /* Create the window */
144 users_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
145 gtk_widget_set_style(users_window, tool_style);
146 g_signal_connect(users_window, "destroy",
147 G_CALLBACK(gtk_widget_destroyed), &users_window);
148 gtk_window_set_title(GTK_WINDOW(users_window), "User Management");
149 /* default size is too small */
150 gtk_window_set_default_size(GTK_WINDOW(users_window), 240, 240);
152 /* Create the list of users and populate it asynchronously */
153 users_list = gtk_list_store_new(1, G_TYPE_STRING);
154 disorder_eclient_users(client, users_got_list, 0);
155 /* Create the view */
156 tree = gtk_tree_view_new_with_model(GTK_TREE_MODEL(users_list));
157 /* ...and the renderers for it */
158 cr = gtk_cell_renderer_text_new();
159 col = gtk_tree_view_column_new_with_attributes("Username",
163 gtk_tree_view_append_column(GTK_TREE_VIEW(tree), col);
164 /* Get the selection for the view and set its mode */
165 users_selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(tree));
166 gtk_tree_selection_set_mode(users_selection, GTK_SELECTION_BROWSE);
167 /* Create the control buttons */
168 buttons = create_buttons_box(users_buttons,
170 gtk_vbox_new(FALSE, 1));
171 /* Put it all together in an hbox */
172 hbox = gtk_hbox_new(FALSE, 2);
173 gtk_box_pack_start(GTK_BOX(hbox), tree, TRUE/*expand*/, TRUE/*fill*/, 0);
174 gtk_box_pack_start(GTK_BOX(hbox), buttons, FALSE, FALSE, 0);
175 gtk_container_add(GTK_CONTAINER(users_window), hbox);