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