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 char *users_getuser(void) {
54 if(gtk_tree_selection_get_selected(users_selection
, 0, &iter
)) {
55 gtk_tree_model_get(GTK_TREE_MODEL(users_list
), &iter
,
66 static void users_add(GtkButton
attribute((unused
)) *button
,
67 gpointer
attribute((unused
)) userdata
) {
70 static void users_deleted_error(struct callbackdata
attribute((unused
)) *cbd
,
71 int attribute((unused
)) code
,
73 popup_msg(GTK_MESSAGE_ERROR
, msg
);
76 static void users_deleted(void *v
) {
77 const struct callbackdata
*const cbd
= v
;
82 if(!gtk_tree_model_get_iter_first(GTK_TREE_MODEL(users_list
), &iter
))
85 gtk_tree_model_get(GTK_TREE_MODEL(users_list
), &iter
,
87 if(!strcmp(who
, cbd
->u
.user
))
91 } while(gtk_tree_model_iter_next(GTK_TREE_MODEL(users_list
), &iter
));
93 gtk_list_store_remove(users_list
, &iter
);
97 static void users_delete(GtkButton
attribute((unused
)) *button
,
98 gpointer
attribute((unused
)) userdata
) {
102 struct callbackdata
*cbd
;
104 if(!(who
= users_getuser()))
106 yesno
= gtk_message_dialog_new(GTK_WINDOW(users_window
),
108 GTK_MESSAGE_QUESTION
,
110 "Do you really want to delete user %s?"
111 " This action cannot be undone.", who
);
112 res
= gtk_dialog_run(GTK_DIALOG(yesno
));
113 gtk_widget_destroy(yesno
);
114 if(res
== GTK_RESPONSE_YES
) {
115 cbd
= xmalloc(sizeof *cbd
);
116 cbd
->onerror
= users_deleted_error
;
118 disorder_eclient_deluser(client
, users_deleted
, cbd
->u
.user
, cbd
);
122 static void users_edit(GtkButton
attribute((unused
)) *button
,
123 gpointer
attribute((unused
)) userdata
) {
127 static const struct button users_buttons
[] = {
144 #define NUSERS_BUTTONS (sizeof users_buttons / sizeof *users_buttons)
146 void manage_users(void) {
147 GtkWidget
*tree
, *buttons
, *hbox
;
149 GtkTreeViewColumn
*col
;
151 /* If the window already exists just raise it */
153 gtk_window_present(GTK_WINDOW(users_window
));
156 /* Create the window */
157 users_window
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
158 gtk_widget_set_style(users_window
, tool_style
);
159 g_signal_connect(users_window
, "destroy",
160 G_CALLBACK(gtk_widget_destroyed
), &users_window
);
161 gtk_window_set_title(GTK_WINDOW(users_window
), "User Management");
162 /* default size is too small */
163 gtk_window_set_default_size(GTK_WINDOW(users_window
), 240, 240);
165 /* Create the list of users and populate it asynchronously */
166 users_list
= gtk_list_store_new(1, G_TYPE_STRING
);
167 disorder_eclient_users(client
, users_got_list
, 0);
168 /* Create the view */
169 tree
= gtk_tree_view_new_with_model(GTK_TREE_MODEL(users_list
));
170 /* ...and the renderers for it */
171 cr
= gtk_cell_renderer_text_new();
172 col
= gtk_tree_view_column_new_with_attributes("Username",
176 gtk_tree_view_append_column(GTK_TREE_VIEW(tree
), col
);
177 /* Get the selection for the view and set its mode */
178 users_selection
= gtk_tree_view_get_selection(GTK_TREE_VIEW(tree
));
179 gtk_tree_selection_set_mode(users_selection
, GTK_SELECTION_BROWSE
);
180 /* Create the control buttons */
181 buttons
= create_buttons_box(users_buttons
,
183 gtk_vbox_new(FALSE
, 1));
184 /* Put it all together in an hbox */
185 hbox
= gtk_hbox_new(FALSE
, 2);
186 gtk_box_pack_start(GTK_BOX(hbox
), tree
, TRUE
/*expand*/, TRUE
/*fill*/, 0);
187 gtk_box_pack_start(GTK_BOX(hbox
), buttons
, FALSE
, FALSE
, 0);
188 gtk_container_add(GTK_CONTAINER(users_window
), hbox
);