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