chiark / gitweb /
Fix queue/etc popup menu
[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.
73f1b9f3
RK
30 */
31
32#include "disobedience.h"
33#include "split.h"
34#include "filepart.h"
e9e8a16d 35#include "client.h"
73f1b9f3
RK
36#include <sys/types.h>
37#include <sys/stat.h>
043d60b1 38#include <unistd.h>
73f1b9f3
RK
39
40/** @brief One field in the login window */
41struct login_window_item {
42 /** @brief Description label */
43 const char *description;
44
45 /** @brief Return the current value */
46 const char *(*get)(void);
47
48 /** @brief Set a new value */
49 void (*set)(const char *value);
50
51 /** @brief Flags
52 *
53 * - @ref LWI_HIDDEN - this is a password
54 */
55 unsigned flags;
56
57};
58
59/** @brief This is a password */
60#define LWI_HIDDEN 0x0001
61
62/** @brief Current login window */
e9e8a16d 63GtkWidget *login_window;
73f1b9f3
RK
64
65/** @brief Set connection defaults */
66static void default_connect(void) {
67 if(!config->connect.n) {
68 config->connect.n = 2;
69 config->connect.s = xcalloc(2, sizeof (char *));
70 config->connect.s[0] = xstrdup("localhost");
71 config->connect.s[1] = xstrdup("9999"); /* whatever */
72 }
73}
74
75static const char *get_hostname(void) { return config->connect.s[0]; }
76static const char *get_service(void) { return config->connect.s[1]; }
77static const char *get_username(void) { return config->username; }
6398c3b7 78static const char *get_password(void) { return config->password ? config->password : ""; }
73f1b9f3
RK
79
80static void set_hostname(const char *s) { config->connect.s[0] = (char *)s; }
81static void set_service(const char *s) { config->connect.s[1] = (char *)s; }
82static void set_username(const char *s) { config->username = s; }
83static void set_password(const char *s) { config->password = s; }
84
85/** @brief Table used to generate the form */
86static const struct login_window_item lwis[] = {
87 { "Hostname", get_hostname, set_hostname, 0 },
88 { "Service", get_service, set_service, 0 },
89 { "User name", get_username, set_username, 0 },
90 { "Password", get_password, set_password, LWI_HIDDEN },
91};
92#define NLWIS (sizeof lwis / sizeof *lwis)
93
94static GtkWidget *lwi_entry[NLWIS];
95
e9e8a16d 96static void login_update_config(void) {
73f1b9f3
RK
97 size_t n;
98
99 for(n = 0; n < NLWIS; ++n)
043d60b1 100 lwis[n].set(xstrdup(gtk_entry_get_text(GTK_ENTRY(lwi_entry[n]))));
73f1b9f3
RK
101}
102
e9e8a16d
RK
103/** @brief Save current login details */
104static void login_save_config(void) {
73f1b9f3
RK
105 char *path = config_userconf(0, 0), *tmp;
106 FILE *fp;
e9e8a16d 107
73f1b9f3
RK
108 byte_xasprintf(&tmp, "%s.tmp", path);
109 /* Make sure the directory exists; don't care if it already exists. */
110 mkdir(d_dirname(tmp), 02700);
111 /* Write out the file */
112 if(!(fp = fopen(tmp, "w"))) {
043d60b1
RK
113 fpopup_msg(GTK_MESSAGE_ERROR, "error opening %s: %s",
114 tmp, strerror(errno));
115 goto done;
73f1b9f3
RK
116 }
117 if(fprintf(fp, "username %s\n"
118 "password %s\n"
119 "connect %s %s\n",
120 quoteutf8(config->username),
121 quoteutf8(config->password),
122 quoteutf8(config->connect.s[0]),
123 quoteutf8(config->connect.s[1])) < 0) {
043d60b1
RK
124 fpopup_msg(GTK_MESSAGE_ERROR, "error writing to %s: %s",
125 tmp, strerror(errno));
73f1b9f3 126 fclose(fp);
043d60b1 127 goto done;
73f1b9f3
RK
128 }
129 if(fclose(fp) < 0) {
043d60b1
RK
130 fpopup_msg(GTK_MESSAGE_ERROR, "error closing %s: %s",
131 tmp, strerror(errno));
132 goto done;
73f1b9f3
RK
133 }
134 /* Rename into place */
135 if(rename(tmp, path) < 0) {
043d60b1
RK
136 fpopup_msg(GTK_MESSAGE_ERROR, "error renaming %s: %s",
137 tmp, strerror(errno));
138 goto done;
73f1b9f3 139 }
043d60b1 140done:
e9e8a16d 141 ;
73f1b9f3
RK
142}
143
e9e8a16d
RK
144/** @brief User pressed OK in login window */
145static void login_ok(GtkButton attribute((unused)) *button,
146 gpointer attribute((unused)) userdata) {
147 disorder_client *c;
148
149 /* Copy the new config into @ref config */
150 login_update_config();
151 /* Attempt a login with the new details */
152 c = disorder_new(0);
153 if(!disorder_connect(c)) {
154 /* Success; save the config and start using it */
155 login_save_config();
7c30fc75 156 logged_in();
e9e8a16d
RK
157 /* Pop down login window */
158 gtk_widget_destroy(login_window);
159 } else {
160 /* Failed to connect - report the error */
161 popup_msg(GTK_MESSAGE_ERROR, disorder_last(c));
162 }
163 disorder_close(c); /* no use for this any more */
164}
165
166/** @brief User pressed cancel in the login window */
73f1b9f3
RK
167static void login_cancel(GtkButton attribute((unused)) *button,
168 gpointer attribute((unused)) userdata) {
169 gtk_widget_destroy(login_window);
170}
171
172/* Buttons that appear at the bottom of the window */
f44417cf 173static struct button buttons[] = {
73f1b9f3 174 {
043d60b1 175 "Login",
73f1b9f3 176 login_ok,
0f24b368 177 "(Re-)connect using these settings",
f44417cf 178 0
73f1b9f3 179 },
73f1b9f3 180 {
0f24b368 181 GTK_STOCK_CLOSE,
73f1b9f3 182 login_cancel,
f44417cf
RK
183 "Discard changes and close window",
184 0
73f1b9f3
RK
185 },
186};
187
188#define NBUTTONS (int)(sizeof buttons / sizeof *buttons)
189
190/** @brief Pop up a login box */
191void login_box(void) {
192 GtkWidget *table, *label, *entry, *buttonbox, *vbox;
193 size_t n;
194
195 /* If there's one already then bring it to the front */
196 if(login_window) {
197 gtk_window_present(GTK_WINDOW(login_window));
198 return;
199 }
200 default_connect();
201 /* Create a new login window */
202 login_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
33288048 203 gtk_widget_set_style(login_window, tool_style);
73f1b9f3
RK
204 g_signal_connect(login_window, "destroy",
205 G_CALLBACK(gtk_widget_destroyed), &login_window);
206 gtk_window_set_title(GTK_WINDOW(login_window), "Login Details");
207 /* Construct the form */
208 table = gtk_table_new(NLWIS + 1/*rows*/, 2/*columns*/, FALSE/*homogenous*/);
33288048 209 gtk_widget_set_style(table, tool_style);
73f1b9f3
RK
210 for(n = 0; n < NLWIS; ++n) {
211 label = gtk_label_new(lwis[n].description);
33288048 212 gtk_widget_set_style(label, tool_style);
73f1b9f3
RK
213 gtk_misc_set_alignment(GTK_MISC(label), 1/*right*/, 0/*bottom*/);
214 gtk_table_attach(GTK_TABLE(table), label,
215 0, 1, /* left/right_attach */
216 n, n+1, /* top/bottom_attach */
217 GTK_FILL, 0, /* x/yoptions */
218 1, 1); /* x/ypadding */
219 entry = gtk_entry_new();
33288048 220 gtk_widget_set_style(entry, tool_style);
73f1b9f3
RK
221 gtk_entry_set_visibility(GTK_ENTRY(entry),
222 lwis[n].flags & LWI_HIDDEN ? FALSE : TRUE);
223 gtk_entry_set_text(GTK_ENTRY(entry), lwis[n].get());
224 gtk_table_attach(GTK_TABLE(table), entry,
225 1, 2, /* left/right_attach */
226 n, n+1, /* top/bottom_attach */
227 GTK_EXPAND|GTK_FILL, 0, /* x/yoptions */
228 1, 1); /* x/ypadding */
229 lwi_entry[n] = entry;
230 }
231 buttonbox = create_buttons(buttons, NBUTTONS);
232 vbox = gtk_vbox_new(FALSE, 1);
233 gtk_box_pack_start(GTK_BOX(vbox), table,
234 TRUE/*expand*/, TRUE/*fill*/, 1/*padding*/);
235 gtk_box_pack_start(GTK_BOX(vbox), buttonbox,
236 FALSE/*expand*/, FALSE/*fill*/, 1/*padding*/);
e18c4734 237 gtk_container_add(GTK_CONTAINER(login_window), frame_widget(vbox, NULL));
458620c5
RK
238 gtk_window_set_transient_for(GTK_WINDOW(login_window),
239 GTK_WINDOW(toplevel));
73f1b9f3
RK
240 gtk_widget_show_all(login_window);
241}
242
243/*
244Local Variables:
245c-basic-offset:2
246comment-column:40
247fill-column:79
248indent-tabs-mode:nil
249End:
250*/