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