chiark / gitweb /
login details box for disobedience. a bit unfriendly but does work.
[disorder] / disobedience / login.c
... / ...
CommitLineData
1/*
2 * This file is part of DisOrder
3 * Copyright (C) 2007 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
24#include "disobedience.h"
25#include "split.h"
26#include "filepart.h"
27#include <sys/types.h>
28#include <sys/stat.h>
29
30/** @brief One field in the login window */
31struct login_window_item {
32 /** @brief Description label */
33 const char *description;
34
35 /** @brief Return the current value */
36 const char *(*get)(void);
37
38 /** @brief Set a new value */
39 void (*set)(const char *value);
40
41 /** @brief Flags
42 *
43 * - @ref LWI_HIDDEN - this is a password
44 */
45 unsigned flags;
46
47};
48
49/** @brief This is a password */
50#define LWI_HIDDEN 0x0001
51
52/** @brief Current login window */
53static GtkWidget *login_window;
54
55/** @brief Set connection defaults */
56static void default_connect(void) {
57 if(!config->connect.n) {
58 config->connect.n = 2;
59 config->connect.s = xcalloc(2, sizeof (char *));
60 config->connect.s[0] = xstrdup("localhost");
61 config->connect.s[1] = xstrdup("9999"); /* whatever */
62 }
63}
64
65static const char *get_hostname(void) { return config->connect.s[0]; }
66static const char *get_service(void) { return config->connect.s[1]; }
67static const char *get_username(void) { return config->username; }
68static const char *get_password(void) { return config->password; }
69
70static void set_hostname(const char *s) { config->connect.s[0] = (char *)s; }
71static void set_service(const char *s) { config->connect.s[1] = (char *)s; }
72static void set_username(const char *s) { config->username = s; }
73static void set_password(const char *s) { config->password = s; }
74
75/** @brief Table used to generate the form */
76static const struct login_window_item lwis[] = {
77 { "Hostname", get_hostname, set_hostname, 0 },
78 { "Service", get_service, set_service, 0 },
79 { "User name", get_username, set_username, 0 },
80 { "Password", get_password, set_password, LWI_HIDDEN },
81};
82#define NLWIS (sizeof lwis / sizeof *lwis)
83
84static GtkWidget *lwi_entry[NLWIS];
85
86static void update_config(void) {
87 size_t n;
88
89 for(n = 0; n < NLWIS; ++n)
90 lwis[n].set(gtk_entry_get_text(GTK_ENTRY(lwi_entry[n])));
91}
92
93static void login_ok(GtkButton attribute((unused)) *button,
94 gpointer attribute((unused)) userdata) {
95 update_config();
96 reset();
97}
98
99static void login_save(GtkButton attribute((unused)) *button,
100 gpointer attribute((unused)) userdata) {
101 char *path = config_userconf(0, 0), *tmp;
102 FILE *fp;
103
104 update_config();
105 byte_xasprintf(&tmp, "%s.tmp", path);
106 /* Make sure the directory exists; don't care if it already exists. */
107 mkdir(d_dirname(tmp), 02700);
108 /* Write out the file */
109 if(!(fp = fopen(tmp, "w"))) {
110 fpopup_error("error opening %s: %s", tmp, strerror(errno));
111 return;
112 }
113 if(fprintf(fp, "username %s\n"
114 "password %s\n"
115 "connect %s %s\n",
116 quoteutf8(config->username),
117 quoteutf8(config->password),
118 quoteutf8(config->connect.s[0]),
119 quoteutf8(config->connect.s[1])) < 0) {
120 fpopup_error("error writing to %s: %s", tmp, strerror(errno));
121 fclose(fp);
122 return;
123 }
124 if(fclose(fp) < 0) {
125 fpopup_error("error closing %s: %s", tmp, strerror(errno));
126 return;
127 }
128 /* Rename into place */
129 if(rename(tmp, path) < 0) {
130 fpopup_error("error renaming %s: %s", tmp, strerror(errno));
131 return;
132 }
133}
134
135static void login_cancel(GtkButton attribute((unused)) *button,
136 gpointer attribute((unused)) userdata) {
137 gtk_widget_destroy(login_window);
138}
139
140/* Buttons that appear at the bottom of the window */
141static const struct button buttons[] = {
142 {
143 GTK_STOCK_OK,
144 login_ok,
145 "Login with these settings",
146 },
147 {
148 GTK_STOCK_SAVE,
149 login_save,
150 "Save these settings",
151 },
152 {
153 GTK_STOCK_CANCEL,
154 login_cancel,
155 "Discard all changes and close window"
156 },
157};
158
159#define NBUTTONS (int)(sizeof buttons / sizeof *buttons)
160
161/** @brief Pop up a login box */
162void login_box(void) {
163 GtkWidget *table, *label, *entry, *buttonbox, *vbox;
164 size_t n;
165
166 /* If there's one already then bring it to the front */
167 if(login_window) {
168 gtk_window_present(GTK_WINDOW(login_window));
169 return;
170 }
171 default_connect();
172 /* Create a new login window */
173 login_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
174 g_signal_connect(login_window, "destroy",
175 G_CALLBACK(gtk_widget_destroyed), &login_window);
176 gtk_window_set_title(GTK_WINDOW(login_window), "Login Details");
177 /* Construct the form */
178 table = gtk_table_new(NLWIS + 1/*rows*/, 2/*columns*/, FALSE/*homogenous*/);
179 for(n = 0; n < NLWIS; ++n) {
180 label = gtk_label_new(lwis[n].description);
181 gtk_misc_set_alignment(GTK_MISC(label), 1/*right*/, 0/*bottom*/);
182 gtk_table_attach(GTK_TABLE(table), label,
183 0, 1, /* left/right_attach */
184 n, n+1, /* top/bottom_attach */
185 GTK_FILL, 0, /* x/yoptions */
186 1, 1); /* x/ypadding */
187 entry = gtk_entry_new();
188 gtk_entry_set_visibility(GTK_ENTRY(entry),
189 lwis[n].flags & LWI_HIDDEN ? FALSE : TRUE);
190 gtk_entry_set_text(GTK_ENTRY(entry), lwis[n].get());
191 gtk_table_attach(GTK_TABLE(table), entry,
192 1, 2, /* left/right_attach */
193 n, n+1, /* top/bottom_attach */
194 GTK_EXPAND|GTK_FILL, 0, /* x/yoptions */
195 1, 1); /* x/ypadding */
196 lwi_entry[n] = entry;
197 }
198 buttonbox = create_buttons(buttons, NBUTTONS);
199 vbox = gtk_vbox_new(FALSE, 1);
200 gtk_box_pack_start(GTK_BOX(vbox), table,
201 TRUE/*expand*/, TRUE/*fill*/, 1/*padding*/);
202 gtk_box_pack_start(GTK_BOX(vbox), buttonbox,
203 FALSE/*expand*/, FALSE/*fill*/, 1/*padding*/);
204 gtk_container_add(GTK_CONTAINER(login_window), vbox);
205 gtk_widget_show_all(login_window);
206}
207
208/*
209Local Variables:
210c-basic-offset:2
211comment-column:40
212fill-column:79
213indent-tabs-mode:nil
214End:
215*/