2 * This file is part of DisOrder
3 * Copyright (C) 2007, 2008 Richard Kettlewell
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 3 of the License, or
8 * (at your option) any later version.
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.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 /** @file disobedience/login.c
19 * @brief Login box for Disobedience
21 * As of 2.1 we have only two buttons: Login and Cancel.
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
27 * It you hit Cancel then the window disappears without saving anything.
30 * - cancel/close should be consistent with properties
33 #include "disobedience.h"
37 #include <sys/types.h>
41 /** @brief One field in the login window */
42 struct login_window_item
{
43 /** @brief Description label */
44 const char *description
;
46 /** @brief Return the current value */
47 const char *(*get
)(void);
49 /** @brief Set a new value */
50 void (*set
)(struct config
*c
, const char *value
);
54 * - @ref LWI_HIDDEN - this is a password
55 * - @ref LWI_REMOTE - this is for remote connections
61 /** @brief This is a password */
62 #define LWI_HIDDEN 0x0001
64 /** @brief This is for remote connections */
65 #define LWI_REMOTE 0x0002
67 /** @brief Current login window */
68 GtkWidget
*login_window
;
70 /** @brief Set connection defaults */
71 static void default_connect(void) {
72 /* If a password is set assume we're good */
75 /* If we already have a host and/or port that's good too */
76 if(config
->connect
.af
!= -1)
78 /* If there's a suitable socket that's probably what we wanted */
79 const char *s
= config_get_file("socket");
81 if(s
&& *s
&& stat(s
, &st
) == 0 && S_ISSOCK(st
.st_mode
))
83 /* TODO can we use some mdns thing to find a DisOrder server? */
86 static const char *get_hostname(void) {
87 if(config
->connect
.af
== -1 || !config
->connect
.address
)
90 return config
->connect
.address
;
93 static const char *get_service(void) {
94 if(config
->connect
.af
== -1)
99 byte_xasprintf(&s
, "%d", config
->connect
.port
);
104 static const char *get_username(void) {
105 return config
->username
;
108 static const char *get_password(void) {
109 return config
->password ? config
->password
: "";
112 static void set_hostname(struct config
*c
, const char *s
) {
113 if(c
->connect
.af
== -1)
114 c
->connect
.af
= AF_UNSPEC
;
115 c
->connect
.address
= xstrdup(s
);
118 static void set_service(struct config
*c
, const char *s
) {
119 c
->connect
.port
= atoi(s
);
122 static void set_username(struct config
*c
, const char *s
) {
126 static void set_password(struct config
*c
, const char *s
) {
130 /** @brief Table used to generate the form */
131 static const struct login_window_item lwis
[] = {
132 { "Hostname", get_hostname
, set_hostname
, LWI_REMOTE
},
133 { "Service", get_service
, set_service
, LWI_REMOTE
},
134 { "User name", get_username
, set_username
, 0 },
135 { "Password", get_password
, set_password
, LWI_HIDDEN
},
137 #define NLWIS (sizeof lwis / sizeof *lwis)
139 static GtkWidget
*lwi_remote
;
140 static GtkWidget
*lwi_entry
[NLWIS
];
142 static void login_update_config(struct config
*c
) {
144 const gboolean remote
= gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(lwi_remote
));
147 c
->connect
.af
= AF_UNSPEC
;
150 for(n
= 0; n
< NLWIS
; ++n
)
151 if(remote
|| !(lwis
[n
].flags
& LWI_REMOTE
))
152 lwis
[n
].set(c
, xstrdup(gtk_entry_get_text(GTK_ENTRY(lwi_entry
[n
]))));
155 /** @brief Save current login details */
156 static void login_save_config(void) {
157 char *path
= config_userconf(0, 0), *tmp
;
160 byte_xasprintf(&tmp
, "%s.tmp", path
);
161 /* Make sure the directory exists; don't care if it already exists. */
162 mkdir(d_dirname(tmp
), 02700);
163 /* Write out the file */
164 if(!(fp
= fopen(tmp
, "w"))) {
165 fpopup_msg(GTK_MESSAGE_ERROR
, "error opening %s: %s",
166 tmp
, strerror(errno
));
169 int rc
= fprintf(fp
, "username %s\n"
171 quoteutf8(config
->username
),
172 quoteutf8(config
->password
));
173 if(rc
>= 0 && config
->connect
.af
!= -1) {
176 netaddress_format(&config
->connect
, NULL
, &vec
);
177 rc
= fprintf(fp
, "connect %s %s %s\n", vec
[0], vec
[1], vec
[2]);
180 fpopup_msg(GTK_MESSAGE_ERROR
, "error writing to %s: %s",
181 tmp
, strerror(errno
));
186 fpopup_msg(GTK_MESSAGE_ERROR
, "error closing %s: %s",
187 tmp
, strerror(errno
));
190 /* Rename into place */
191 if(rename(tmp
, path
) < 0) {
192 fpopup_msg(GTK_MESSAGE_ERROR
, "error renaming %s: %s",
193 tmp
, strerror(errno
));
200 /** @brief User pressed OK in login window */
201 static void login_ok(GtkButton
attribute((unused
)) *button
,
202 gpointer
attribute((unused
)) userdata
) {
204 struct config
*tmpconfig
= xmalloc(sizeof *tmpconfig
);
206 tmpconfig
->home
= xstrdup(pkgstatedir
);
207 /* Copy the new config into @ref config */
208 login_update_config(tmpconfig
);
209 /* Attempt a login with the new details */
211 if(!disorder_connect_generic(tmpconfig
, c
,
212 tmpconfig
->username
, tmpconfig
->password
,
214 /* Success; save the config and start using it */
215 login_update_config(config
);
218 /* Pop down login window */
219 gtk_widget_destroy(login_window
);
221 /* Failed to connect - report the error */
222 popup_msg(GTK_MESSAGE_ERROR
, disorder_last(c
));
224 disorder_close(c
); /* no use for this any more */
227 /** @brief User pressed cancel in the login window */
228 static void login_cancel(GtkButton
attribute((unused
)) *button
,
229 gpointer
attribute((unused
)) userdata
) {
230 gtk_widget_destroy(login_window
);
233 /** @brief Keypress handler */
234 static gboolean
login_keypress(GtkWidget
attribute((unused
)) *widget
,
236 gpointer
attribute((unused
)) user_data
) {
239 switch(event
->keyval
) {
251 /* Buttons that appear at the bottom of the window */
252 static struct button buttons
[] = {
256 "(Re-)connect using these settings",
262 "Discard changes and close window",
267 #define NBUTTONS (int)(sizeof buttons / sizeof *buttons)
269 /** @brief Called when the remote/local button is toggled (and initially)
271 * Sets the sensitivity of the host/port entries.
273 static void lwi_remote_toggled(GtkToggleButton
*togglebutton
,
274 gpointer
attribute((unused
)) user_data
) {
275 const gboolean remote
= gtk_toggle_button_get_active(togglebutton
);
277 for(unsigned n
= 0; n
< NLWIS
; ++n
)
278 if(lwis
[n
].flags
& LWI_REMOTE
)
279 gtk_widget_set_sensitive(lwi_entry
[n
], remote
);
282 /** @brief Pop up a login box */
283 void login_box(void) {
284 GtkWidget
*table
, *label
, *entry
, *buttonbox
, *vbox
;
287 /* If there's one already then bring it to the front */
289 gtk_window_present(GTK_WINDOW(login_window
));
293 /* Create a new login window */
294 login_window
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
295 gtk_widget_set_style(login_window
, tool_style
);
296 g_signal_connect(login_window
, "destroy",
297 G_CALLBACK(gtk_widget_destroyed
), &login_window
);
298 gtk_window_set_title(GTK_WINDOW(login_window
), "Login Details");
299 /* Construct the form */
300 table
= gtk_table_new(1 + NLWIS
/*rows*/, 2/*columns*/, FALSE
/*homogenous*/);
301 gtk_widget_set_style(table
, tool_style
);
302 label
= gtk_label_new("Remote");
303 gtk_widget_set_style(label
, tool_style
);
304 gtk_misc_set_alignment(GTK_MISC(label
), 1/*right*/, 0/*bottom*/);
305 gtk_table_attach(GTK_TABLE(table
), label
,
306 0, 1, /* left/right_attach */
307 0, 1, /* top/bottom_attach */
308 GTK_FILL
, 0, /* x/yoptions */
309 1, 1); /* x/ypadding */
310 lwi_remote
= gtk_check_button_new();
311 gtk_widget_set_style(lwi_remote
, tool_style
);
312 gtk_table_attach(GTK_TABLE(table
), lwi_remote
,
313 1, 2, /* left/right_attach */
314 0, 1, /* top/bottom_attach */
315 GTK_EXPAND
|GTK_FILL
, 0, /* x/yoptions */
316 1, 1); /* x/ypadding */
317 g_signal_connect(lwi_remote
, "toggled", G_CALLBACK(lwi_remote_toggled
), 0);
318 for(n
= 0; n
< NLWIS
; ++n
) {
319 label
= gtk_label_new(lwis
[n
].description
);
320 gtk_widget_set_style(label
, tool_style
);
321 gtk_misc_set_alignment(GTK_MISC(label
), 1/*right*/, 0/*bottom*/);
322 gtk_table_attach(GTK_TABLE(table
), label
,
323 0, 1, /* left/right_attach */
324 n
+1, n
+2, /* top/bottom_attach */
325 GTK_FILL
, 0, /* x/yoptions */
326 1, 1); /* x/ypadding */
327 entry
= gtk_entry_new();
328 gtk_widget_set_style(entry
, tool_style
);
329 gtk_entry_set_visibility(GTK_ENTRY(entry
),
330 lwis
[n
].flags
& LWI_HIDDEN ? FALSE
: TRUE
);
331 gtk_entry_set_text(GTK_ENTRY(entry
), lwis
[n
].get());
332 gtk_table_attach(GTK_TABLE(table
), entry
,
333 1, 2, /* left/right_attach */
334 n
+1, n
+2, /* top/bottom_attach */
335 GTK_EXPAND
|GTK_FILL
, 0, /* x/yoptions */
336 1, 1); /* x/ypadding */
337 lwi_entry
[n
] = entry
;
339 /* Initial settings */
340 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(lwi_remote
),
341 config
->connect
.af
!= -1);
342 lwi_remote_toggled(GTK_TOGGLE_BUTTON(lwi_remote
), 0);
343 buttonbox
= create_buttons(buttons
, NBUTTONS
);
344 vbox
= gtk_vbox_new(FALSE
, 1);
345 gtk_box_pack_start(GTK_BOX(vbox
),
346 gtk_image_new_from_pixbuf(find_image("logo256.png")),
350 gtk_box_pack_start(GTK_BOX(vbox
), table
,
351 TRUE
/*expand*/, TRUE
/*fill*/, 1/*padding*/);
352 gtk_box_pack_start(GTK_BOX(vbox
), buttonbox
,
353 FALSE
/*expand*/, FALSE
/*fill*/, 1/*padding*/);
354 gtk_container_add(GTK_CONTAINER(login_window
), frame_widget(vbox
, NULL
));
355 gtk_window_set_transient_for(GTK_WINDOW(login_window
),
356 GTK_WINDOW(toplevel
));
357 /* Keyboard shortcuts */
358 g_signal_connect(login_window
, "key-press-event",
359 G_CALLBACK(login_keypress
), 0);
360 gtk_widget_show_all(login_window
);