summaryrefslogtreecommitdiff
path: root/beos/beos_login.cpp
blob: 10b04afa654be90f8b762976fa0fe65fbb67a8b0 (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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*
 * Copyright 2008 Fran├žois Revol <mmu_man@users.sourceforge.net>
 * 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/>.
 */

#define __STDBOOL_H__	1
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <Alert.h>
#include <String.h>
#include <TextControl.h>
#include <View.h>
#include <Window.h>
extern "C" {
#include "utils/log.h"
#include "content/content.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"
}
#include "beos/beos_gui.h"
#include "beos/beos_scaffolding.h"
#include "beos/beos_window.h"

class LoginAlert : public BAlert {
public:
			LoginAlert(struct browser_window *bw,
				const char *url, 
				const char *host, 
				const char *realm, 
				const char *text);
	virtual	~LoginAlert();
	void	MessageReceived(BMessage *message);

private:
	BString 	fUrl;				/**< URL being fetched */
	BString		fHost;				/**< Host for user display */
	BString		fRealm;				/**< Authentication realm */
	struct gui_window *fW;			/**< GUI window handle */

	BTextControl	*fUserControl;
	BTextControl	*fPassControl;
};

static void create_login_window(struct browser_window *bw, const char *host,
                const char *realm, const char *fetchurl);


#define TC_H 25
#define TC_MARGIN 10

LoginAlert::LoginAlert(struct browser_window *bw,
				const char *url, 
				const char *host, 
				const char *realm, 
				const char *text)
	: BAlert("Login", text, "Cancel", "Ok", NULL, 
		B_WIDTH_AS_USUAL, B_WARNING_ALERT)
{
	fUrl = url;
	fHost = host;
	fRealm = realm;
	// dereference now as we can't be sure 
	// the main thread won't delete from under our feet
	fW = bw->window;

	SetFeel(B_MODAL_SUBSET_WINDOW_FEEL);
	nsbeos_scaffolding *s = nsbeos_get_scaffold(bw->window);
	if (s) {
		NSBrowserWindow *w = nsbeos_get_bwindow_for_scaffolding(s);
		if (w)
			AddToSubset(w);
	}

	// make space for controls
	ResizeBy(0, 2 * TC_H);
	MoveTo(AlertPosition(Frame().Width() + 1, 
		Frame().Height() + 1));


	BTextView *tv = TextView();
	BRect r(TC_MARGIN, tv->Bounds().bottom - 2 * TC_H, 
		tv->Bounds().right - TC_MARGIN, tv->Bounds().bottom - TC_H);

	fUserControl = new BTextControl(r, "user", "Username", "", 
		new BMessage(), B_FOLLOW_BOTTOM | B_FOLLOW_RIGHT);
	fUserControl->SetDivider(60);
	tv->AddChild(fUserControl);

	r.OffsetBySelf(0, TC_H);

	fPassControl = new BTextControl(r, "pass", "Password", "", 
		new BMessage(), B_FOLLOW_BOTTOM | B_FOLLOW_RIGHT);
	fPassControl->TextView()->HideTyping(true);
	fPassControl->SetDivider(60);
	tv->AddChild(fPassControl);
	
	SetShortcut(0, B_ESCAPE);
}

LoginAlert::~LoginAlert()
{
}

void
LoginAlert::MessageReceived(BMessage *message)
{
	switch (message->what) {
	case 'ALTB':
	{
		int32 which;
		if (message->FindInt32("which", &which) < B_OK)
			break;
		// not 'Ok'
		if (which != 1)
			break;
		BMessage *m = new BMessage(*message);
		m->what = 'nsLO';
		m->AddString("URL", fUrl.String());
		m->AddString("Host", fHost.String());
		m->AddString("Realm", fRealm.String());
		m->AddPointer("gui_window", fW);
		m->AddString("User", fUserControl->Text());
		m->AddString("Pass", fPassControl->Text());
		BString auth(fUserControl->Text());
		auth << ":" << fPassControl->Text();
		m->AddString("Auth", auth.String());
		
		// notify the main thread
		// the event dispatcher will handle it
		nsbeos_pipe_message(m, NULL, fW);
	}
		break;
	default:
		break;
	}
	BAlert::MessageReceived(message);
}


void gui_401login_open(struct browser_window *bw, struct content *c,
		const char *realm)
{
	char *host;
	url_func_result res;

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

	create_login_window(bw, host, realm, c->url);

	free(host);
}

void create_login_window(struct browser_window *bw, const char *host,
		const char *realm, const char *fetchurl)
{
	BString r("Secure Area");
	if (realm)
		r = realm;
	BString text(/*messages_get(*/"Please login\n");
	text << "Realm:	" << r << "\n";
	text << "Host:	" << host << "\n";
	//text << "\n";

	LoginAlert *a = new LoginAlert(bw, fetchurl, host, r.String(), 
		text.String());
	// asynchronously
	a->Go(NULL);

}

#warning WRITEME
#if 0 /* GTK */
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);

	browser_window_go(session->bw, session->url, 0, true);

	destroy_login_window(session);
}

void nsgtk_login_cancel_clicked(GtkButton *w, gpointer data)
{
	/* just close and destroy the window */
	destroy_login_window((struct session_401 *)data);
}
#endif