summaryrefslogtreecommitdiff
path: root/gtk/login.c
blob: 41f66de204effd6f85d5e7c11fe920704e817970 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
 * Copyright 2006 Rob Kendrick <rjek@rjek.com>
 *
 * This file is part of NetSurf, http://www.netsurf-browser.org/
 *
 * NetSurf is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * NetSurf is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <gtk/gtk.h>

#include "utils/log.h"
#include "gtk/gui.h"
#include "content/content.h"
#include "content/hlcache.h"
#include "content/urldb.h"
#include "desktop/browser.h"
#include "desktop/401login.h"
#include "desktop/gui.h"
#include "utils/messages.h"
#include "utils/url.h"
#include "utils/utils.h"

struct session_401 {
	char *url;				/**< URL being fetched */
	char *host;				/**< Host for user display */
	char *realm;				/**< Authentication realm */
	nserror (*cb)(bool proceed, void *pw);	/**< Continuation callback */
	void *cbpw;				/**< Continuation data */
	GtkBuilder *x;				/**< Our glade windows */
	GtkWindow *wnd;				/**< The login window itself */
	GtkEntry *user;				/**< Widget with username */
	GtkEntry *pass;				/**< Widget with password */
};

static void create_login_window(const char *url, const char *host,
                const char *realm, nserror (*cb)(bool proceed, void *pw),
		void *cbpw);
static void destroy_login_window(struct session_401 *session);
static void nsgtk_login_next(GtkWidget *w, gpointer data);
static void nsgtk_login_ok_clicked(GtkButton *w, gpointer data);
static void nsgtk_login_cancel_clicked(GtkButton *w, gpointer data);

void gui_401login_open(const char *url, const char *realm,
		nserror (*cb)(bool proceed, void *pw), void *cbpw)
{
	char *host;
	url_func_result res;

	res = url_host(url, &host);
	assert(res == URL_FUNC_OK);

	create_login_window(url, host, realm, cb, cbpw);

	free(host);
}

void create_login_window(const char *url, const char *host, const char *realm, 
		nserror (*cb)(bool proceed, void *pw), void *cbpw)
{
	struct session_401 *session;

	/* create a new instance of the login window, and get handles to all
	 * the widgets we're interested in.
	 */

	GtkWindow *wnd;
	GtkLabel *lhost, *lrealm;
	GtkEntry *euser, *epass;
	GtkButton *bok, *bcan;
	GError* error = NULL;
	GtkBuilder* builder; 

	builder = gtk_builder_new ();
	if (!gtk_builder_add_from_file(builder, glade_file_location->login, &error)) {
		g_warning ("Couldn't load builder file: %s", error->message);
		g_error_free (error);
	}

	wnd = GTK_WINDOW(gtk_builder_get_object(builder, "wndLogin"));
	lhost = GTK_LABEL(gtk_builder_get_object(builder, "labelLoginHost"));
	lrealm = GTK_LABEL(gtk_builder_get_object(builder, "labelLoginRealm"));
	euser = GTK_ENTRY(gtk_builder_get_object(builder, "entryLoginUser"));
	epass = GTK_ENTRY(gtk_builder_get_object(builder, "entryLoginPass"));
	bok = GTK_BUTTON(gtk_builder_get_object(builder, "buttonLoginOK"));
	bcan = GTK_BUTTON(gtk_builder_get_object(builder, "buttonLoginCan"));

	/* create and fill in our session structure */

	session = calloc(1, sizeof(struct session_401));
	session->url = strdup(url);
	session->host = strdup(host);
	session->realm = strdup(realm ? realm : "Secure Area");
	session->cb = cb;
	session->cbpw = cbpw;
	session->x = builder;
	session->wnd = wnd;
	session->user = euser;
	session->pass = epass;

	/* fill in our new login window */

	gtk_label_set_text(GTK_LABEL(lhost), host);
	gtk_label_set_text(lrealm, realm);
	gtk_entry_set_text(euser, "");
	gtk_entry_set_text(epass, "");

	/* attach signal handlers to the Login and Cancel buttons in our new
	 * window to call functions in this file to process the login
	 */
	g_signal_connect(G_OBJECT(bok), "clicked",
			G_CALLBACK(nsgtk_login_ok_clicked), (gpointer)session);
	g_signal_connect(G_OBJECT(bcan), "clicked",
			G_CALLBACK(nsgtk_login_cancel_clicked),
			(gpointer)session);

	/* attach signal handlers to the entry boxes such that pressing
	 * enter in one progresses the focus onto the next widget.
	 */

	g_signal_connect(G_OBJECT(euser), "activate",
			G_CALLBACK(nsgtk_login_next), (gpointer)epass);
	g_signal_connect(G_OBJECT(epass), "activate",
			G_CALLBACK(nsgtk_login_next), (gpointer)bok);

	/* make sure the username entry box currently has the focus */
	gtk_widget_grab_focus(GTK_WIDGET(euser));

	/* finally, show the window */
	gtk_widget_show(GTK_WIDGET(wnd));
}

void destroy_login_window(struct session_401 *session)
{
	free(session->url);
	free(session->host);
	free(session->realm);
	gtk_widget_destroy(GTK_WIDGET(session->wnd));
	g_object_unref(G_OBJECT(session->x));
	free(session);
}

void nsgtk_login_next(GtkWidget *w, gpointer data)
{
	gtk_widget_grab_focus(GTK_WIDGET(data));
}

void nsgtk_login_ok_clicked(GtkButton *w, gpointer data)
{
	/* close the window and destroy it, having continued the fetch
	 * assoicated with it.
	 */

  	struct session_401 *session = (struct session_401 *)data;
	const gchar *user = gtk_entry_get_text(session->user);
	const gchar *pass = gtk_entry_get_text(session->pass);
	char *auth;

	auth = malloc(strlen(user) + strlen(pass) + 2);
	sprintf(auth, "%s:%s", user, pass);
	urldb_set_auth_details(session->url, session->realm, auth);
	free(auth);

	session->cb(true, session->cbpw);

	destroy_login_window(session);
}

void nsgtk_login_cancel_clicked(GtkButton *w, gpointer data)
{
	struct session_401 *session = (struct session_401 *) data;

	session->cb(false, session->cbpw);

	/* close and destroy the window */
	destroy_login_window(session);
}