Commit | Line | Data |
---|---|---|
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 | ||
26 | static GtkWidget *users_window; | |
27 | static GtkListStore *users_list; | |
4aa8a0a4 | 28 | static GtkTreeSelection *users_selection; |
ffc4dbaf RK |
29 | |
30 | static int usercmp(const void *a, const void *b) { | |
31 | return strcmp(*(char **)a, *(char **)b); | |
32 | } | |
33 | ||
34 | static 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 | ||
6faa6239 RK |
50 | static char *users_getuser(void) { |
51 | GtkTreeIter iter; | |
52 | char *who, *c; | |
53 | ||
54 | if(gtk_tree_selection_get_selected(users_selection, 0, &iter)) { | |
55 | gtk_tree_model_get(GTK_TREE_MODEL(users_list), &iter, | |
56 | 0, &who, -1); | |
57 | if(who) { | |
58 | c = xstrdup(who); | |
59 | g_free(who); | |
60 | return c; | |
61 | } | |
62 | } | |
63 | return 0; | |
64 | } | |
65 | ||
ffc4dbaf RK |
66 | static void users_add(GtkButton attribute((unused)) *button, |
67 | gpointer attribute((unused)) userdata) { | |
68 | } | |
69 | ||
4aa8a0a4 RK |
70 | static void users_deleted_error(struct callbackdata attribute((unused)) *cbd, |
71 | int attribute((unused)) code, | |
72 | const char *msg) { | |
73 | popup_msg(GTK_MESSAGE_ERROR, msg); | |
74 | } | |
75 | ||
76 | static void users_deleted(void *v) { | |
77 | const struct callbackdata *const cbd = v; | |
78 | GtkTreeIter iter; | |
79 | char *who; | |
80 | ||
81 | /* Find the user */ | |
82 | if(!gtk_tree_model_get_iter_first(GTK_TREE_MODEL(users_list), &iter)) | |
83 | return; | |
84 | do { | |
85 | gtk_tree_model_get(GTK_TREE_MODEL(users_list), &iter, | |
86 | 0, &who, -1); | |
87 | if(!strcmp(who, cbd->u.user)) | |
88 | break; | |
89 | g_free(who); | |
90 | who = 0; | |
91 | } while(gtk_tree_model_iter_next(GTK_TREE_MODEL(users_list), &iter)); | |
92 | /* Remove them */ | |
93 | gtk_list_store_remove(users_list, &iter); | |
94 | g_free(who); | |
95 | } | |
96 | ||
ffc4dbaf RK |
97 | static void users_delete(GtkButton attribute((unused)) *button, |
98 | gpointer attribute((unused)) userdata) { | |
4aa8a0a4 RK |
99 | GtkWidget *yesno; |
100 | char *who; | |
101 | int res; | |
102 | struct callbackdata *cbd; | |
103 | ||
6faa6239 RK |
104 | if(!(who = users_getuser())) |
105 | return; | |
106 | yesno = gtk_message_dialog_new(GTK_WINDOW(users_window), | |
107 | GTK_DIALOG_MODAL, | |
108 | GTK_MESSAGE_QUESTION, | |
109 | GTK_BUTTONS_YES_NO, | |
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; | |
117 | cbd->u.user = who; | |
118 | disorder_eclient_deluser(client, users_deleted, cbd->u.user, cbd); | |
4aa8a0a4 | 119 | } |
ffc4dbaf RK |
120 | } |
121 | ||
122 | static void users_edit(GtkButton attribute((unused)) *button, | |
123 | gpointer attribute((unused)) userdata) { | |
6faa6239 | 124 | |
ffc4dbaf RK |
125 | } |
126 | ||
127 | static const struct button users_buttons[] = { | |
128 | { | |
129 | "Add user", | |
130 | users_add, | |
131 | "Create a new user" | |
132 | }, | |
133 | { | |
134 | "Edit user", | |
135 | users_edit, | |
136 | "Edit a user" | |
137 | }, | |
138 | { | |
139 | "Delete user", | |
140 | users_delete, | |
141 | "Delete a user" | |
142 | }, | |
143 | }; | |
144 | #define NUSERS_BUTTONS (sizeof users_buttons / sizeof *users_buttons) | |
145 | ||
146 | void manage_users(void) { | |
147 | GtkWidget *tree, *buttons, *hbox; | |
148 | GtkCellRenderer *cr; | |
149 | GtkTreeViewColumn *col; | |
150 | ||
151 | /* If the window already exists just raise it */ | |
152 | if(users_window) { | |
153 | gtk_window_present(GTK_WINDOW(users_window)); | |
154 | return; | |
155 | } | |
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); | |
164 | ||
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", | |
173 | cr, | |
174 | "text", 0, | |
175 | NULL); | |
176 | gtk_tree_view_append_column(GTK_TREE_VIEW(tree), col); | |
4aa8a0a4 RK |
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); | |
ffc4dbaf RK |
180 | /* Create the control buttons */ |
181 | buttons = create_buttons_box(users_buttons, | |
182 | NUSERS_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); | |
189 | } | |
190 | ||
191 | /* | |
192 | Local Variables: | |
193 | c-basic-offset:2 | |
194 | comment-column:40 | |
195 | fill-column:79 | |
196 | indent-tabs-mode:nil | |
197 | End: | |
198 | */ |