chiark / gitweb /
User deletion in Disobedience
[disorder] / disobedience / users.c
CommitLineData
ffc4dbaf
RK
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
26static GtkWidget *users_window;
27static GtkListStore *users_list;
4aa8a0a4 28static GtkTreeSelection *users_selection;
ffc4dbaf
RK
29
30static int usercmp(const void *a, const void *b) {
31 return strcmp(*(char **)a, *(char **)b);
32}
33
34static void users_got_list(void attribute((unused)) *v, int nvec, char **vec) {
35 int n;
36 GtkTreeIter iter;
37
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);
48}
49
50static void users_add(GtkButton attribute((unused)) *button,
51 gpointer attribute((unused)) userdata) {
52}
53
4aa8a0a4
RK
54static void users_deleted_error(struct callbackdata attribute((unused)) *cbd,
55 int attribute((unused)) code,
56 const char *msg) {
57 popup_msg(GTK_MESSAGE_ERROR, msg);
58}
59
60static void users_deleted(void *v) {
61 const struct callbackdata *const cbd = v;
62 GtkTreeIter iter;
63 char *who;
64
65 /* Find the user */
66 if(!gtk_tree_model_get_iter_first(GTK_TREE_MODEL(users_list), &iter))
67 return;
68 do {
69 gtk_tree_model_get(GTK_TREE_MODEL(users_list), &iter,
70 0, &who, -1);
71 if(!strcmp(who, cbd->u.user))
72 break;
73 g_free(who);
74 who = 0;
75 } while(gtk_tree_model_iter_next(GTK_TREE_MODEL(users_list), &iter));
76 /* Remove them */
77 gtk_list_store_remove(users_list, &iter);
78 g_free(who);
79}
80
ffc4dbaf
RK
81static void users_delete(GtkButton attribute((unused)) *button,
82 gpointer attribute((unused)) userdata) {
4aa8a0a4
RK
83 GtkTreeIter iter;
84 GtkWidget *yesno;
85 char *who;
86 int res;
87 struct callbackdata *cbd;
88
89 if(gtk_tree_selection_get_selected(users_selection, 0, &iter)) {
90 gtk_tree_model_get(GTK_TREE_MODEL(users_list), &iter,
91 0, &who, -1);
92 yesno = gtk_message_dialog_new(GTK_WINDOW(users_window),
93 GTK_DIALOG_MODAL,
94 GTK_MESSAGE_QUESTION,
95 GTK_BUTTONS_YES_NO,
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);
105 }
106 g_free(who);
107 }
ffc4dbaf
RK
108}
109
110static void users_edit(GtkButton attribute((unused)) *button,
111 gpointer attribute((unused)) userdata) {
112}
113
114static const struct button users_buttons[] = {
115 {
116 "Add user",
117 users_add,
118 "Create a new user"
119 },
120 {
121 "Edit user",
122 users_edit,
123 "Edit a user"
124 },
125 {
126 "Delete user",
127 users_delete,
128 "Delete a user"
129 },
130};
131#define NUSERS_BUTTONS (sizeof users_buttons / sizeof *users_buttons)
132
133void manage_users(void) {
134 GtkWidget *tree, *buttons, *hbox;
135 GtkCellRenderer *cr;
136 GtkTreeViewColumn *col;
137
138 /* If the window already exists just raise it */
139 if(users_window) {
140 gtk_window_present(GTK_WINDOW(users_window));
141 return;
142 }
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);
151
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",
160 cr,
161 "text", 0,
162 NULL);
163 gtk_tree_view_append_column(GTK_TREE_VIEW(tree), col);
4aa8a0a4
RK
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);
ffc4dbaf
RK
167 /* Create the control buttons */
168 buttons = create_buttons_box(users_buttons,
169 NUSERS_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);
176}
177
178/*
179Local Variables:
180c-basic-offset:2
181comment-column:40
182fill-column:79
183indent-tabs-mode:nil
184End:
185*/