2 * This file is part of DisOrder
3 * Copyright (C) 2007 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 2 of the License, or
8 * (at your option) any later version.
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.
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
20 /** @file disobedience/login.c
21 * @brief Login box for Disobedience
24 #include "disobedience.h"
27 #include <sys/types.h>
30 /** @brief One field in the login window */
31 struct login_window_item
{
32 /** @brief Description label */
33 const char *description
;
35 /** @brief Return the current value */
36 const char *(*get
)(void);
38 /** @brief Set a new value */
39 void (*set
)(const char *value
);
43 * - @ref LWI_HIDDEN - this is a password
49 /** @brief This is a password */
50 #define LWI_HIDDEN 0x0001
52 /** @brief Current login window */
53 static GtkWidget
*login_window
;
55 /** @brief Set connection defaults */
56 static 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 */
65 static const char *get_hostname(void) { return config
->connect
.s
[0]; }
66 static const char *get_service(void) { return config
->connect
.s
[1]; }
67 static const char *get_username(void) { return config
->username
; }
68 static const char *get_password(void) { return config
->password
; }
70 static void set_hostname(const char *s
) { config
->connect
.s
[0] = (char *)s
; }
71 static void set_service(const char *s
) { config
->connect
.s
[1] = (char *)s
; }
72 static void set_username(const char *s
) { config
->username
= s
; }
73 static void set_password(const char *s
) { config
->password
= s
; }
75 /** @brief Table used to generate the form */
76 static 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
},
82 #define NLWIS (sizeof lwis / sizeof *lwis)
84 static GtkWidget
*lwi_entry
[NLWIS
];
86 static void update_config(void) {
89 for(n
= 0; n
< NLWIS
; ++n
)
90 lwis
[n
].set(gtk_entry_get_text(GTK_ENTRY(lwi_entry
[n
])));
93 static void login_ok(GtkButton
attribute((unused
)) *button
,
94 gpointer
attribute((unused
)) userdata
) {
99 static void login_save(GtkButton
attribute((unused
)) *button
,
100 gpointer
attribute((unused
)) userdata
) {
101 char *path
= config_userconf(0, 0), *tmp
;
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
));
113 if(fprintf(fp
, "username %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
));
125 fpopup_error("error closing %s: %s", tmp
, strerror(errno
));
128 /* Rename into place */
129 if(rename(tmp
, path
) < 0) {
130 fpopup_error("error renaming %s: %s", tmp
, strerror(errno
));
135 static void login_cancel(GtkButton
attribute((unused
)) *button
,
136 gpointer
attribute((unused
)) userdata
) {
137 gtk_widget_destroy(login_window
);
140 /* Buttons that appear at the bottom of the window */
141 static const struct button buttons
[] = {
145 "Login with these settings",
150 "Save these settings",
155 "Discard all changes and close window"
159 #define NBUTTONS (int)(sizeof buttons / sizeof *buttons)
161 /** @brief Pop up a login box */
162 void login_box(void) {
163 GtkWidget
*table
, *label
, *entry
, *buttonbox
, *vbox
;
166 /* If there's one already then bring it to the front */
168 gtk_window_present(GTK_WINDOW(login_window
));
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
;
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
);