chiark / gitweb /
Disobedience login window now has a 'remote' switch. When off it will
[disorder] / disobedience / login.c
CommitLineData
73f1b9f3
RK
1/*
2 * This file is part of DisOrder
6398c3b7 3 * Copyright (C) 2007, 2008 Richard Kettlewell
73f1b9f3
RK
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
e9e8a16d
RK
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.
d62c03cd
RK
30 *
31 * TODO
d62c03cd 32 * - cancel/close should be consistent with properties
73f1b9f3
RK
33 */
34
35#include "disobedience.h"
36#include "split.h"
37#include "filepart.h"
e9e8a16d 38#include "client.h"
73f1b9f3
RK
39#include <sys/types.h>
40#include <sys/stat.h>
043d60b1 41#include <unistd.h>
73f1b9f3
RK
42
43/** @brief One field in the login window */
44struct 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 */
319d7107 52 void (*set)(struct config *c, const char *value);
73f1b9f3
RK
53
54 /** @brief Flags
55 *
56 * - @ref LWI_HIDDEN - this is a password
ec7109f3 57 * - @ref LWI_REMOTE - this is for remote connections
73f1b9f3
RK
58 */
59 unsigned flags;
60
61};
62
63/** @brief This is a password */
64#define LWI_HIDDEN 0x0001
65
ec7109f3
RK
66/** @brief This is for remote connections */
67#define LWI_REMOTE 0x0002
68
73f1b9f3 69/** @brief Current login window */
e9e8a16d 70GtkWidget *login_window;
73f1b9f3
RK
71
72/** @brief Set connection defaults */
73static void default_connect(void) {
74 if(!config->connect.n) {
75 config->connect.n = 2;
76 config->connect.s = xcalloc(2, sizeof (char *));
77 config->connect.s[0] = xstrdup("localhost");
78 config->connect.s[1] = xstrdup("9999"); /* whatever */
79 }
80}
81
ec7109f3
RK
82static const char *get_hostname(void) {
83 return config->connect.n >= 2 ? config->connect.s[0] : "";
84}
85
86static const char *get_service(void) {
87 return config->connect.n >= 2 ? config->connect.s[1] : "";
88}
89
90static const char *get_username(void) {
91 return config->username;
92}
93
94static const char *get_password(void) {
95 return config->password ? config->password : "";
96}
73f1b9f3 97
319d7107
RK
98static void set_hostname(struct config *c, const char *s) {
99 c->connect.s[0] = (char *)s;
100}
101
102static void set_service(struct config *c, const char *s) {
103 c->connect.s[1] = (char *)s;
104}
105
106static void set_username(struct config *c, const char *s) {
107 c->username = s;
108}
109
110static void set_password(struct config *c, const char *s) {
111 c->password = s;
112}
73f1b9f3
RK
113
114/** @brief Table used to generate the form */
115static const struct login_window_item lwis[] = {
ec7109f3
RK
116 { "Hostname", get_hostname, set_hostname, LWI_REMOTE },
117 { "Service", get_service, set_service, LWI_REMOTE },
73f1b9f3
RK
118 { "User name", get_username, set_username, 0 },
119 { "Password", get_password, set_password, LWI_HIDDEN },
120};
121#define NLWIS (sizeof lwis / sizeof *lwis)
122
ec7109f3 123static GtkWidget *lwi_remote;
73f1b9f3
RK
124static GtkWidget *lwi_entry[NLWIS];
125
319d7107 126static void login_update_config(struct config *c) {
73f1b9f3 127 size_t n;
ec7109f3 128 const gboolean remote = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(lwi_remote));
73f1b9f3 129
ec7109f3 130 if(remote) {
319d7107
RK
131 c->connect.n = 2;
132 c->connect.s = xcalloc(2, sizeof (char *));
ec7109f3
RK
133 } else {
134 c->connect.n = 0;
135 c->connect.s = 0;
319d7107 136 }
73f1b9f3 137 for(n = 0; n < NLWIS; ++n)
ec7109f3
RK
138 if(remote || !(lwis[n].flags & LWI_REMOTE))
139 lwis[n].set(c, xstrdup(gtk_entry_get_text(GTK_ENTRY(lwi_entry[n]))));
73f1b9f3
RK
140}
141
e9e8a16d
RK
142/** @brief Save current login details */
143static void login_save_config(void) {
73f1b9f3
RK
144 char *path = config_userconf(0, 0), *tmp;
145 FILE *fp;
e9e8a16d 146
73f1b9f3
RK
147 byte_xasprintf(&tmp, "%s.tmp", path);
148 /* Make sure the directory exists; don't care if it already exists. */
149 mkdir(d_dirname(tmp), 02700);
150 /* Write out the file */
151 if(!(fp = fopen(tmp, "w"))) {
043d60b1
RK
152 fpopup_msg(GTK_MESSAGE_ERROR, "error opening %s: %s",
153 tmp, strerror(errno));
154 goto done;
73f1b9f3 155 }
ec7109f3
RK
156 int rc = fprintf(fp, "username %s\n"
157 "password %s\n",
158 quoteutf8(config->username),
159 quoteutf8(config->password));
160 if(rc >= 0 && config->connect.n)
161 rc = fprintf(fp, "connect %s %s\n",
162 quoteutf8(config->connect.s[0]),
163 quoteutf8(config->connect.s[1]));
164 if(rc < 0) {
043d60b1
RK
165 fpopup_msg(GTK_MESSAGE_ERROR, "error writing to %s: %s",
166 tmp, strerror(errno));
73f1b9f3 167 fclose(fp);
043d60b1 168 goto done;
73f1b9f3
RK
169 }
170 if(fclose(fp) < 0) {
043d60b1
RK
171 fpopup_msg(GTK_MESSAGE_ERROR, "error closing %s: %s",
172 tmp, strerror(errno));
173 goto done;
73f1b9f3
RK
174 }
175 /* Rename into place */
176 if(rename(tmp, path) < 0) {
043d60b1
RK
177 fpopup_msg(GTK_MESSAGE_ERROR, "error renaming %s: %s",
178 tmp, strerror(errno));
179 goto done;
73f1b9f3 180 }
043d60b1 181done:
e9e8a16d 182 ;
73f1b9f3
RK
183}
184
e9e8a16d
RK
185/** @brief User pressed OK in login window */
186static void login_ok(GtkButton attribute((unused)) *button,
187 gpointer attribute((unused)) userdata) {
188 disorder_client *c;
319d7107 189 struct config *tmpconfig = xmalloc(sizeof *tmpconfig);
e9e8a16d 190
ec7109f3 191 tmpconfig->home = xstrdup(pkgstatedir);
e9e8a16d 192 /* Copy the new config into @ref config */
319d7107 193 login_update_config(tmpconfig);
e9e8a16d
RK
194 /* Attempt a login with the new details */
195 c = disorder_new(0);
319d7107
RK
196 if(!disorder_connect_generic(tmpconfig, c,
197 tmpconfig->username, tmpconfig->password,
198 NULL/*cookie*/)) {
e9e8a16d 199 /* Success; save the config and start using it */
319d7107 200 login_update_config(config);
e9e8a16d 201 login_save_config();
7c30fc75 202 logged_in();
e9e8a16d
RK
203 /* Pop down login window */
204 gtk_widget_destroy(login_window);
205 } else {
206 /* Failed to connect - report the error */
207 popup_msg(GTK_MESSAGE_ERROR, disorder_last(c));
208 }
209 disorder_close(c); /* no use for this any more */
210}
211
212/** @brief User pressed cancel in the login window */
73f1b9f3
RK
213static void login_cancel(GtkButton attribute((unused)) *button,
214 gpointer attribute((unused)) userdata) {
215 gtk_widget_destroy(login_window);
216}
217
453bdfe0
RK
218/** @brief Keypress handler */
219static gboolean login_keypress(GtkWidget attribute((unused)) *widget,
220 GdkEventKey *event,
221 gpointer attribute((unused)) user_data) {
222 if(event->state)
223 return FALSE;
224 switch(event->keyval) {
225 case GDK_Return:
226 login_ok(0, 0);
227 return TRUE;
228 case GDK_Escape:
229 login_cancel(0, 0);
230 return TRUE;
231 default:
232 return FALSE;
233 }
234}
235
73f1b9f3 236/* Buttons that appear at the bottom of the window */
f44417cf 237static struct button buttons[] = {
73f1b9f3 238 {
043d60b1 239 "Login",
73f1b9f3 240 login_ok,
0f24b368 241 "(Re-)connect using these settings",
f44417cf 242 0
73f1b9f3 243 },
73f1b9f3 244 {
0f24b368 245 GTK_STOCK_CLOSE,
73f1b9f3 246 login_cancel,
f44417cf
RK
247 "Discard changes and close window",
248 0
73f1b9f3
RK
249 },
250};
251
252#define NBUTTONS (int)(sizeof buttons / sizeof *buttons)
253
ec7109f3
RK
254/** @brief Called when the remote/local button is toggled (and initially)
255 *
256 * Sets the sensitivity of the host/port entries.
257 */
258static void lwi_remote_toggled(GtkToggleButton *togglebutton,
259 gpointer attribute((unused)) user_data) {
260 const gboolean remote = gtk_toggle_button_get_active(togglebutton);
261
262 for(unsigned n = 0; n < NLWIS; ++n)
263 if(lwis[n].flags & LWI_REMOTE)
264 gtk_widget_set_sensitive(lwi_entry[n], remote);
265}
266
73f1b9f3
RK
267/** @brief Pop up a login box */
268void login_box(void) {
269 GtkWidget *table, *label, *entry, *buttonbox, *vbox;
270 size_t n;
271
272 /* If there's one already then bring it to the front */
273 if(login_window) {
274 gtk_window_present(GTK_WINDOW(login_window));
275 return;
276 }
277 default_connect();
278 /* Create a new login window */
279 login_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
33288048 280 gtk_widget_set_style(login_window, tool_style);
73f1b9f3
RK
281 g_signal_connect(login_window, "destroy",
282 G_CALLBACK(gtk_widget_destroyed), &login_window);
283 gtk_window_set_title(GTK_WINDOW(login_window), "Login Details");
284 /* Construct the form */
ec7109f3 285 table = gtk_table_new(1 + NLWIS/*rows*/, 2/*columns*/, FALSE/*homogenous*/);
33288048 286 gtk_widget_set_style(table, tool_style);
ec7109f3
RK
287 label = gtk_label_new("Remote");
288 gtk_widget_set_style(label, tool_style);
289 gtk_misc_set_alignment(GTK_MISC(label), 1/*right*/, 0/*bottom*/);
290 gtk_table_attach(GTK_TABLE(table), label,
291 0, 1, /* left/right_attach */
292 0, 1, /* top/bottom_attach */
293 GTK_FILL, 0, /* x/yoptions */
294 1, 1); /* x/ypadding */
295 lwi_remote = gtk_check_button_new();
296 gtk_widget_set_style(lwi_remote, tool_style);
297 gtk_table_attach(GTK_TABLE(table), lwi_remote,
298 1, 2, /* left/right_attach */
299 0, 1, /* top/bottom_attach */
300 GTK_EXPAND|GTK_FILL, 0, /* x/yoptions */
301 1, 1); /* x/ypadding */
302 g_signal_connect(lwi_remote, "toggled", G_CALLBACK(lwi_remote_toggled), 0);
73f1b9f3
RK
303 for(n = 0; n < NLWIS; ++n) {
304 label = gtk_label_new(lwis[n].description);
33288048 305 gtk_widget_set_style(label, tool_style);
73f1b9f3
RK
306 gtk_misc_set_alignment(GTK_MISC(label), 1/*right*/, 0/*bottom*/);
307 gtk_table_attach(GTK_TABLE(table), label,
308 0, 1, /* left/right_attach */
ec7109f3 309 n+1, n+2, /* top/bottom_attach */
73f1b9f3
RK
310 GTK_FILL, 0, /* x/yoptions */
311 1, 1); /* x/ypadding */
312 entry = gtk_entry_new();
33288048 313 gtk_widget_set_style(entry, tool_style);
73f1b9f3
RK
314 gtk_entry_set_visibility(GTK_ENTRY(entry),
315 lwis[n].flags & LWI_HIDDEN ? FALSE : TRUE);
316 gtk_entry_set_text(GTK_ENTRY(entry), lwis[n].get());
317 gtk_table_attach(GTK_TABLE(table), entry,
318 1, 2, /* left/right_attach */
ec7109f3 319 n+1, n+2, /* top/bottom_attach */
73f1b9f3
RK
320 GTK_EXPAND|GTK_FILL, 0, /* x/yoptions */
321 1, 1); /* x/ypadding */
322 lwi_entry[n] = entry;
323 }
ec7109f3
RK
324 /* Initial settings */
325 lwi_remote_toggled(GTK_TOGGLE_BUTTON(lwi_remote), 0);
326 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(lwi_remote),
327 config->connect.n >= 2);
73f1b9f3
RK
328 buttonbox = create_buttons(buttons, NBUTTONS);
329 vbox = gtk_vbox_new(FALSE, 1);
b2ea2aa6
RK
330 gtk_box_pack_start(GTK_BOX(vbox),
331 gtk_image_new_from_pixbuf(find_image("logo256.png")),
332 TRUE/*expand*/,
333 TRUE/*fill*/,
334 4/*padding*/);
73f1b9f3
RK
335 gtk_box_pack_start(GTK_BOX(vbox), table,
336 TRUE/*expand*/, TRUE/*fill*/, 1/*padding*/);
337 gtk_box_pack_start(GTK_BOX(vbox), buttonbox,
338 FALSE/*expand*/, FALSE/*fill*/, 1/*padding*/);
e18c4734 339 gtk_container_add(GTK_CONTAINER(login_window), frame_widget(vbox, NULL));
458620c5
RK
340 gtk_window_set_transient_for(GTK_WINDOW(login_window),
341 GTK_WINDOW(toplevel));
453bdfe0
RK
342 /* Keyboard shortcuts */
343 g_signal_connect(login_window, "key-press-event",
344 G_CALLBACK(login_keypress), 0);
73f1b9f3
RK
345 gtk_widget_show_all(login_window);
346}
347
348/*
349Local Variables:
350c-basic-offset:2
351comment-column:40
352fill-column:79
353indent-tabs-mode:nil
354End:
355*/