NetSurf
login.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2008 François Revol <mmu_man@users.sourceforge.net>
3 *
4 * This file is part of NetSurf, http://www.netsurf-browser.org/
5 *
6 * NetSurf is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * NetSurf 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.
14 *
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/>.
17 */
18
19#define __STDBOOL_H__ 1
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23#include <assert.h>
24#include <Alert.h>
25#include <String.h>
26#include <TextControl.h>
27#include <View.h>
28#include <Window.h>
29
30extern "C" {
31#include "utils/log.h"
32#include "utils/messages.h"
33#include "utils/url.h"
34#include "utils/utils.h"
35#include "utils/nsurl.h"
38#include "netsurf/clipboard.h"
39}
40
41#include "beos/gui.h"
42#include "beos/scaffolding.h"
43#include "beos/window.h"
44
45class LoginAlert : public BAlert {
46public:
47 LoginAlert(nserror (*callback)(const char *username,
48 const char *password,
49 void *pw),
50 void *callbaclpw,
51 nsurl *url,
52 const char *host,
53 const char *realm,
54 const char *text);
55 virtual ~LoginAlert();
56 void MessageReceived(BMessage *message);
57
58private:
59 nsurl* fUrl; /**< URL being fetched */
60 BString fHost; /**< Host for user display */
61 BString fRealm; /**< Authentication realm */
62 nserror (*fCallback)(const char *username,
63 const char *password,
64 void *pw);
66
67 BTextControl *fUserControl;
68 BTextControl *fPassControl;
69};
70
71static void create_login_window(nsurl *host,
72 lwc_string *realm, const char *fetchurl,
73 nserror (*cb)(const char *username,
74 const char *password,
75 void *pw), void *cbpw);
76
77
78#define TC_H 25
79#define TC_MARGIN 10
80
81LoginAlert::LoginAlert(nserror (*callback)(const char *username,
82 const char *password,
83 void *pw),
84 void *callbackpw,
85 nsurl *url,
86 const char *host,
87 const char *realm,
88 const char *text)
89 : BAlert("Login", text, "Cancel", "Ok", NULL,
90 B_WIDTH_AS_USUAL, B_WARNING_ALERT)
91{
92 fCallback = callback;
93 fCallbackPw = callbackpw;
94 fUrl = url;
95 fHost = host;
96 fRealm = realm;
97
98 SetFeel(B_MODAL_SUBSET_WINDOW_FEEL);
99 /*
100 // XXX: can't do that anymore
101 nsbeos_scaffolding *s = nsbeos_get_scaffold(bw->window);
102 if (s) {
103 NSBrowserWindow *w = nsbeos_get_bwindow_for_scaffolding(s);
104 if (w)
105 AddToSubset(w);
106 }*/
107
108 // make space for controls
109 ResizeBy(0, 2 * TC_H);
110 MoveTo(AlertPosition(Frame().Width() + 1,
111 Frame().Height() + 1));
112
113
114 BTextView *tv = TextView();
115 BRect r(TC_MARGIN, tv->Bounds().bottom - 2 * TC_H,
116 tv->Bounds().right - TC_MARGIN, tv->Bounds().bottom - TC_H);
117
118 fUserControl = new BTextControl(r, "user", "Username", "",
119 new BMessage(), B_FOLLOW_BOTTOM | B_FOLLOW_RIGHT);
120 fUserControl->SetDivider(60);
121 tv->AddChild(fUserControl);
122
123 r.OffsetBySelf(0, TC_H);
124
125 fPassControl = new BTextControl(r, "pass", "Password", "",
126 new BMessage(), B_FOLLOW_BOTTOM | B_FOLLOW_RIGHT);
127 fPassControl->TextView()->HideTyping(true);
128 fPassControl->SetDivider(60);
129 tv->AddChild(fPassControl);
130
131 SetShortcut(0, B_ESCAPE);
132}
133
135{
136}
137
138void
140{
141 switch (message->what) {
142 case 'ALTB':
143 {
144 int32 which;
145 if (message->FindInt32("which", &which) < B_OK)
146 break;
147 // not 'Ok'
148 if (which != 1)
149 break;
150 BMessage *m = new BMessage(*message);
151 m->what = 'nsLO';
152 m->AddPointer("URL", fUrl);
153 m->AddString("Host", fHost.String());
154 m->AddString("Realm", fRealm.String());
155 m->AddPointer("callback", (void *)fCallback);
156 m->AddPointer("callback_pw", (void *)fCallbackPw);
157 m->AddString("User", fUserControl->Text());
158 m->AddString("Pass", fPassControl->Text());
159 BString auth(fUserControl->Text());
160 auth << ":" << fPassControl->Text();
161 m->AddString("Auth", auth.String());
162
163 // notify the main thread
164 // the event dispatcher will handle it
165 nsbeos_pipe_message(m, NULL, NULL);
166 }
167 break;
168 default:
169 break;
170 }
171 BAlert::MessageReceived(message);
172}
173
174
175extern "C" nserror gui_401login_open(nsurl *url, const char *realm,
176 const char *username, const char *password,
177 nserror (*cb)(const char *username,
178 const char *password,
179 void *pw),
180 void *cbpw)
181{
182 lwc_string *host;
183
184 host = nsurl_get_component(url, NSURL_HOST);
185
186 create_login_window(url, host, realm, cb, cbpw);
187
188 free(host);
189
190 return NSERROR_OK;
191}
192
193//void create_login_window(struct browser_window *bw, const char *host,
194// const char *realm, const char *fetchurl)
195static void create_login_window(nsurl *url, lwc_string *host,
196 const char *realm, nserror (*cb)(
197 const char *username,
198 const char *password,
199 void *pw),
200 void *cbpw)
201{
202 BString r("Secure Area");
203 if (realm)
204 r = realm;
205 BString text(/*messages_get(*/"Please login\n");
206 text << "Realm: " << r << "\n";
207 text << "Host: " << host << "\n";
208 //text << "\n";
209
210 LoginAlert *a = new LoginAlert(cb, cbpw, url, lwc_string_data(host),
211 r.String(), text.String());
212 // asynchronously
213 a->Go(NULL);
214
215}
Browser window creation and manipulation interface.
void MessageReceived(BMessage *message)
Definition: login.cpp:139
BTextControl * fPassControl
Definition: login.cpp:68
void * fCallbackPw
Definition: login.cpp:65
LoginAlert(nserror(*callback)(const char *username, const char *password, void *pw), void *callbaclpw, nsurl *url, const char *host, const char *realm, const char *text)
Definition: login.cpp:81
BString fRealm
Authentication realm.
Definition: login.cpp:61
virtual ~LoginAlert()
Definition: login.cpp:134
BTextControl * fUserControl
Definition: login.cpp:67
BString fHost
Host for user display.
Definition: login.cpp:60
nsurl * fUrl
URL being fetched.
Definition: login.cpp:59
nserror(* fCallback)(const char *username, const char *password, void *pw)
Definition: login.cpp:62
Declaration of content enumerations.
nserror
Enumeration of error codes.
Definition: errors.h:29
@ NSERROR_OK
No error.
Definition: errors.h:30
void nsbeos_pipe_message(BMessage *message, BView *_this, struct gui_window *gui)
Definition: gui.cpp:717
Interface to platform-specific clipboard operations.
static void create_login_window(nsurl *host, lwc_string *realm, const char *fetchurl, nserror(*cb)(const char *username, const char *password, void *pw), void *cbpw)
Definition: login.cpp:195
#define TC_MARGIN
Definition: login.cpp:79
nserror gui_401login_open(nsurl *url, const char *realm, const char *username, const char *password, nserror(*cb)(const char *username, const char *password, void *pw), void *cbpw)
Definition: login.cpp:175
#define TC_H
Definition: login.cpp:78
Localised message support (interface).
NetSurf URL handling (interface).
lwc_string * nsurl_get_component(const nsurl *url, nsurl_component part)
Get part of a URL as a lwc_string, from a NetSurf URL object.
@ NSURL_HOST
Definition: nsurl.h:49
struct nsurl nsurl
NetSurf URL object.
Definition: nsurl.h:31
int32_t int32
Definition: os3support.h:183
Interface to utility string handling.
Interface to URL parsing and joining operations.
Interface to a number of general purpose functionality.
static nserror text(const struct redraw_context *ctx, const struct plot_font_style *fstyle, int x, int y, const char *text, size_t length)
Text plotting.
Definition: plot.c:978