7cb64c9d38cc9efce7260356a9ac9f82476a8467
[disorder] / disobedience / login.c
1 /*
2 * This file is part of DisOrder
3 * Copyright (C) 2007, 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/login.c
21 * @brief Login box for Disobedience
22 *
23 * As of 2.1 we have only two buttons: Login and Cancel.
24 *
25 * If you hit Login then a login is attempted. If it works the window
26 * disappears and the settings are saved, otherwise they are NOT saved and the
27 * window remains.
28 *
29 * It you hit Cancel then the window disappears without saving anything.
30 *
31 * TODO
32 * - cancel/close should be consistent with properties
33 */
34
35 #include "disobedience.h"
36 #include "split.h"
37 #include "filepart.h"
38 #include "client.h"
39 #include <sys/types.h>
40 #include <sys/stat.h>
41 #include <unistd.h>
42
43 /** @brief One field in the login window */
44 struct login_window_item {
45 /** @brief Description label */
46 const char *description;
47
48 /** @brief Return the current value */
49 const char *(*get)(void);
50
51 /** @brief Set a new value */
52 void (*set)(const char *value);
53
54 /** @brief Flags
55 *
56 * - @ref LWI_HIDDEN - this is a password
57 */
58 unsigned flags;
59
60 };
61
62 /** @brief This is a password */
63 #define LWI_HIDDEN 0x0001
64
65 /** @brief Current login window */
66 GtkWidget *login_window;
67
68 /** @brief Set connection defaults */
69 static void default_connect(void) {
70 if(!config->connect.n) {
71 config->connect.n = 2;
72 config->connect.s = xcalloc(2, sizeof (char *));
73 config->connect.s[0] = xstrdup("localhost");
74 config->connect.s[1] = xstrdup("9999"); /* whatever */
75 }
76 }
77
78 static const char *get_hostname(void) { return config->connect.s[0]; }
79 static const char *get_service(void) { return config->connect.s[1]; }
80 static const char *get_username(void) { return config->username; }
81 static const char *get_password(void) { return config->password ? config->password : ""; }
82
83 static void set_hostname(const char *s) { config->connect.s[0] = (char *)s; }
84 static void set_service(const char *s) { config->connect.s[1] = (char *)s; }
85 static void set_username(const char *s) { config->username = s; }
86 static void set_password(const char *s) { config->password = s; }
87
88 /** @brief Table used to generate the form */
89 static const struct login_window_item lwis[] = {
90 { "Hostname", get_hostname, set_hostname, 0 },
91 { "Service", get_service, set_service, 0 },
92 { "User name", get_username, set_username, 0 },
93 { "Password", get_password, set_password, LWI_HIDDEN },
94 };
95 #define NLWIS (sizeof lwis / sizeof *lwis)
96
97 static GtkWidget *lwi_entry[NLWIS];
98
99 static void login_update_config(void) {
100 size_t n;
101
102 for(n = 0; n < NLWIS; ++n)
103 lwis[n].set(xstrdup(gtk_entry_get_text(GTK_ENTRY(lwi_entry[n]))));
104 }
105
106 /** @brief Save current login details */
107 static void login_save_config(void) {
108 char *path = config_userconf(0, 0), *tmp;
109 FILE *fp;
110
111 byte_xasprintf(&tmp, "%s.tmp", path);
112 /* Make sure the directory exists; don't care if it already exists. */
113 mkdir(d_dirname(tmp), 02700);
114 /* Write out the file */
115 if(!(fp = fopen(tmp, "w"))) {
116 fpopup_msg(GTK_MESSAGE_ERROR, "error opening %s: %s",
117 tmp, strerror(errno));
118 goto done;
119 }
120 if(fprintf(fp, "username %s\n"
121 "password %s\n"
122 "connect %s %s\n",
123 quoteutf8(config->username),
124 quoteutf8(config->password),
125 quoteutf8(config->connect.s[0]),
126 quoteutf8(config->connect.s[1])) < 0) {
127 fpopup_msg(GTK_MESSAGE_ERROR, "error writing to %s: %s",
128 tmp, strerror(errno));
129 fclose(fp);
130 goto done;
131 }
132 if(fclose(fp) < 0) {
133 fpopup_msg(GTK_MESSAGE_ERROR, "error closing %s: %s",
134 tmp, strerror(errno));
135 goto done;
136 }
137 /* Rename into place */
138 if(rename(tmp, path) < 0) {
139 fpopup_msg(GTK_MESSAGE_ERROR, "error renaming %s: %s",
140 tmp, strerror(errno));
141 goto done;
142 }
143 done:
144 ;
145 }
146
147 /** @brief User pressed OK in login window */
148 static void login_ok(GtkButton attribute((unused)) *button,
149 gpointer attribute((unused)) userdata) {
150 disorder_client *c;
151
152 /* Copy the new config into @ref config */
153 login_update_config();
154 /* Attempt a login with the new details */
155 c = disorder_new(0);
156 if(!disorder_connect(c)) {
157 /* Success; save the config and start using it */
158 login_save_config();
159 logged_in();
160 /* Pop down login window */
161 gtk_widget_destroy(login_window);
162 } else {
163 /* Failed to connect - report the error */
164 popup_msg(GTK_MESSAGE_ERROR, disorder_last(c));
165 /* TODO it would be nice to restore the config (not the entry contents!) to
166 * the last known good one if we were already connected to something. */
167 }
168 disorder_close(c); /* no use for this any more */
169 }
170
171 /** @brief User pressed cancel in the login window */
172 static void login_cancel(GtkButton attribute((unused)) *button,
173 gpointer attribute((unused)) userdata) {
174 gtk_widget_destroy(login_window);
175 }
176
177 /** @brief Keypress handler */
178 static gboolean login_keypress(GtkWidget attribute((unused)) *widget,
179 GdkEventKey *event,
180 gpointer attribute((unused)) user_data) {
181 if(event->state)
182 return FALSE;
183 switch(event->keyval) {
184 case GDK_Return:
185 login_ok(0, 0);
186 return TRUE;
187 case GDK_Escape:
188 login_cancel(0, 0);
189 return TRUE;
190 default:
191 return FALSE;
192 }
193 }
194
195 /* Buttons that appear at the bottom of the window */
196 static struct button buttons[] = {
197 {
198 "Login",
199 login_ok,
200 "(Re-)connect using these settings",
201 0
202 },
203 {
204 GTK_STOCK_CLOSE,
205 login_cancel,
206 "Discard changes and close window",
207 0
208 },
209 };
210
211 #define NBUTTONS (int)(sizeof buttons / sizeof *buttons)
212
213 /** @brief Pop up a login box */
214 void login_box(void) {
215 GtkWidget *table, *label, *entry, *buttonbox, *vbox;
216 size_t n;
217
218 /* If there's one already then bring it to the front */
219 if(login_window) {
220 gtk_window_present(GTK_WINDOW(login_window));
221 return;
222 }
223 default_connect();
224 /* Create a new login window */
225 login_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
226 gtk_widget_set_style(login_window, tool_style);
227 g_signal_connect(login_window, "destroy",
228 G_CALLBACK(gtk_widget_destroyed), &login_window);
229 gtk_window_set_title(GTK_WINDOW(login_window), "Login Details");
230 /* Construct the form */
231 table = gtk_table_new(NLWIS/*rows*/, 2/*columns*/, FALSE/*homogenous*/);
232 gtk_widget_set_style(table, tool_style);
233 for(n = 0; n < NLWIS; ++n) {
234 label = gtk_label_new(lwis[n].description);
235 gtk_widget_set_style(label, tool_style);
236 gtk_misc_set_alignment(GTK_MISC(label), 1/*right*/, 0/*bottom*/);
237 gtk_table_attach(GTK_TABLE(table), label,
238 0, 1, /* left/right_attach */
239 n, n+1, /* top/bottom_attach */
240 GTK_FILL, 0, /* x/yoptions */
241 1, 1); /* x/ypadding */
242 entry = gtk_entry_new();
243 gtk_widget_set_style(entry, tool_style);
244 gtk_entry_set_visibility(GTK_ENTRY(entry),
245 lwis[n].flags & LWI_HIDDEN ? FALSE : TRUE);
246 gtk_entry_set_text(GTK_ENTRY(entry), lwis[n].get());
247 gtk_table_attach(GTK_TABLE(table), entry,
248 1, 2, /* left/right_attach */
249 n, n+1, /* top/bottom_attach */
250 GTK_EXPAND|GTK_FILL, 0, /* x/yoptions */
251 1, 1); /* x/ypadding */
252 lwi_entry[n] = entry;
253 }
254 buttonbox = create_buttons(buttons, NBUTTONS);
255 vbox = gtk_vbox_new(FALSE, 1);
256 gtk_box_pack_start(GTK_BOX(vbox),
257 gtk_image_new_from_pixbuf(find_image("logo256.png")),
258 TRUE/*expand*/,
259 TRUE/*fill*/,
260 4/*padding*/);
261 gtk_box_pack_start(GTK_BOX(vbox), table,
262 TRUE/*expand*/, TRUE/*fill*/, 1/*padding*/);
263 gtk_box_pack_start(GTK_BOX(vbox), buttonbox,
264 FALSE/*expand*/, FALSE/*fill*/, 1/*padding*/);
265 gtk_container_add(GTK_CONTAINER(login_window), frame_widget(vbox, NULL));
266 gtk_window_set_transient_for(GTK_WINDOW(login_window),
267 GTK_WINDOW(toplevel));
268 /* Keyboard shortcuts */
269 g_signal_connect(login_window, "key-press-event",
270 G_CALLBACK(login_keypress), 0);
271 gtk_widget_show_all(login_window);
272 }
273
274 /*
275 Local Variables:
276 c-basic-offset:2
277 comment-column:40
278 fill-column:79
279 indent-tabs-mode:nil
280 End:
281 */