NetSurf
login.c
Go to the documentation of this file.
1 /*
2  * Copyright 2008 Chris Young <chris@unsatisfactorysoftware.co.uk>
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 #include "amiga/os3support.h"
20 
21 #include <assert.h>
22 #include <stdlib.h>
23 #include <string.h>
24 
25 #include <proto/exec.h>
26 #include <proto/intuition.h>
27 #include <proto/utility.h>
28 
29 #include <proto/window.h>
30 #include <proto/layout.h>
31 #include <proto/string.h>
32 #include <proto/button.h>
33 #include <proto/label.h>
34 #include <classes/window.h>
35 #include <gadgets/layout.h>
36 #include <gadgets/string.h>
37 #include <gadgets/button.h>
38 #include <images/label.h>
39 #include <reaction/reaction_macros.h>
40 
41 #include "utils/messages.h"
42 #include "utils/nsurl.h"
43 #include "netsurf/mouse.h"
44 #include "netsurf/window.h"
45 #include "netsurf/url_db.h"
46 
47 #include "amiga/gui.h"
48 #include "amiga/libs.h"
49 #include "amiga/login.h"
50 #include "amiga/misc.h"
51 #include "amiga/object.h"
52 #include "amiga/utf8.h"
53 
54 enum {
62 };
63 
64 enum {
72 };
73 
76  struct Window *win;
77  Object *objects[GID_L_LAST];
78  nserror (*cb)(const char *username, const char *password, void *pw);
79  void *cbpw;
81  char *realm;
82  lwc_string *host;
83  char uname[256];
84  char pwd[256];
86 };
87 
88 static BOOL ami_401login_event(void *w);
89 static void ami_401login_close(void *w);
90 
91 static const struct ami_win_event_table ami_login_table = {
93  ami_401login_close, /* TODO: investigate why this doesn't get called on exit */
94 };
95 
97  const char *username, const char *password,
98  nserror (*cb)(const char *username,
99  const char *password,
100  void *pw),
101  void *cbpw)
102 {
103  struct gui_login_window *lw = calloc(1, sizeof(struct gui_login_window));
104  lwc_string *host = nsurl_get_component(url, NSURL_HOST);
105  size_t len;
106 
107  assert(host != NULL);
108  assert(username != NULL);
109  assert(password != NULL);
110 
111  lw->host = host;
112  lw->url = nsurl_ref(url);
113  lw->realm = (realm != NULL) ? strdup(realm) : NULL;
114  lw->cb = cb;
115  lw->cbpw = cbpw;
116 
123 
124  /* Convert existing username and password to local charset */
125  char *user_utf8 = ami_utf8_easy(username);
126  if(user_utf8 != NULL) {
127  len = strlen(user_utf8);
128  assert(len < sizeof(lw->uname));
129  memcpy(lw->uname, user_utf8, len + 1);
130  ami_utf8_free(user_utf8);
131  }
132 
133  char *pass_utf8 = ami_utf8_easy(password);
134  if(pass_utf8 != NULL) {
135  len = strlen(pass_utf8);
136  assert(len < sizeof(lw->pwd));
137  memcpy(lw->pwd, pass_utf8, len + 1);
138  ami_utf8_free(pass_utf8);
139  }
140 
142  WA_ScreenTitle, ami_gui_get_screen_title(),
143  WA_Title, nsurl_access(lw->url),
144  WA_Activate, TRUE,
145  WA_DepthGadget, TRUE,
146  WA_DragBar, TRUE,
147  WA_CloseGadget, FALSE,
148  WA_SizeGadget, TRUE,
149  WA_PubScreen, ami_gui_get_screen(),
150  WINDOW_SharedPort, ami_gui_get_shared_msgport(),
151  WINDOW_UserData,lw,
152  WINDOW_IconifyGadget, FALSE,
153  WINDOW_LockHeight,TRUE,
154  WINDOW_Position, WPOS_CENTERSCREEN,
155  WINDOW_ParentGroup, lw->objects[GID_L_MAIN] = LayoutVObj,
156  LAYOUT_AddChild, StringObj,
157  STRINGA_TextVal, lwc_string_data(lw->host),
158  GA_ReadOnly,TRUE,
159  StringEnd,
160  CHILD_Label, LabelObj,
161  LABEL_Text, lw->messages[AMI_LOGIN_MSG_HOST],
162  LabelEnd,
163  CHILD_WeightedHeight,0,
164  LAYOUT_AddChild, StringObj,
165  STRINGA_TextVal,lw->realm,
166  GA_ReadOnly,TRUE,
167  StringEnd,
168  CHILD_Label, LabelObj,
169  LABEL_Text, lw->messages[AMI_LOGIN_MSG_REALM],
170  LabelEnd,
171  CHILD_WeightedHeight,0,
172  LAYOUT_AddChild, lw->objects[GID_L_USER] = StringObj,
173  GA_ID,GID_L_USER,
174  GA_TabCycle,TRUE,
175  STRINGA_TextVal, lw->uname,
176  StringEnd,
177  CHILD_Label, LabelObj,
178  LABEL_Text, lw->messages[AMI_LOGIN_MSG_USER],
179  LabelEnd,
180  CHILD_WeightedHeight,0,
181  LAYOUT_AddChild, lw->objects[GID_L_PASS] = StringObj,
182  GA_ID,GID_L_PASS,
183  STRINGA_HookType,SHK_PASSWORD,
184  GA_TabCycle,TRUE,
185  STRINGA_TextVal, lw->pwd,
186  StringEnd,
187  CHILD_Label, LabelObj,
188  LABEL_Text, lw->messages[AMI_LOGIN_MSG_PASS],
189  LabelEnd,
190  CHILD_WeightedHeight,0,
191  LAYOUT_AddChild, LayoutHObj,
192  LAYOUT_AddChild, lw->objects[GID_L_LOGIN] = ButtonObj,
193  GA_ID,GID_L_LOGIN,
194  GA_RelVerify,TRUE,
195  GA_Text, lw->messages[AMI_LOGIN_MSG_LOGIN],
196  GA_TabCycle,TRUE,
197  ButtonEnd,
198  CHILD_WeightedHeight,0,
199  LAYOUT_AddChild, lw->objects[GID_L_CANCEL] = ButtonObj,
200  GA_ID,GID_L_CANCEL,
201  GA_RelVerify,TRUE,
202  GA_Text, lw->messages[AMI_LOGIN_MSG_CANCEL],
203  GA_TabCycle,TRUE,
204  ButtonEnd,
205  LayoutEnd,
206  CHILD_WeightedHeight,0,
207  EndGroup,
208  EndWindow;
209 
210  lw->win = (struct Window *)RA_OpenWindow(lw->objects[OID_L_MAIN]);
211  ami_gui_win_list_add(lw, AMINS_LOGINWINDOW, &ami_login_table);
212 
213  return NSERROR_OK;
214 }
215 
216 static void ami_401login_close(void *w)
217 {
218  struct gui_login_window *lw = (struct gui_login_window *)w;
219 
220  /* If continuation exists, then forbid refetch */
221  if (lw->cb != NULL)
222  lw->cb(NULL, NULL, lw->cbpw);
223 
224  DisposeObject(lw->objects[OID_L_MAIN]);
225  lwc_string_unref(lw->host);
226  nsurl_unref(lw->url);
227  free(lw->realm);
228 
229  /* Free local charset version of messages */
230  for(int i = 0; i < AMI_LOGIN_MSG_MAX; i++) {
231  ami_utf8_free(lw->messages[i]);
232  }
233 
235 }
236 
237 static void ami_401login_login(struct gui_login_window *lw)
238 {
239  char *user;
240  char *pass;
241 
242  /* Get username and password from string gadgets */
243  GetAttr(STRINGA_TextVal,lw->objects[GID_L_USER],(ULONG *)&user);
244  GetAttr(STRINGA_TextVal,lw->objects[GID_L_PASS],(ULONG *)&pass);
245 
246  /* Convert from local charset to UTF-8 */
247  char *user_utf8 = ami_to_utf8_easy(user);
248  char *pass_utf8 = ami_to_utf8_easy(pass);
249 
250  if(user_utf8 && pass_utf8) {
251  lw->cb(user_utf8, pass_utf8, lw->cbpw);
252 
253  ami_utf8_free(user_utf8);
254  ami_utf8_free(pass_utf8);
255  } else {
256  amiga_warn_user("NoMemory", "");
257  }
258 
259  /* Invalidate continuation */
260  lw->cb = NULL;
261  lw->cbpw = NULL;
262 
263  ami_401login_close(lw);
264 }
265 
266 static BOOL ami_401login_event(void *w)
267 {
268  /* return TRUE if window destroyed */
269  struct gui_login_window *lw = (struct gui_login_window *)w;
270  ULONG result;
271  uint16 code;
272 
273  while((result = RA_HandleInput(lw->objects[OID_L_MAIN], &code)) != WMHI_LASTMSG)
274  {
275  switch(result & WMHI_CLASSMASK) // class
276  {
277  case WMHI_GADGETUP:
278  switch(result & WMHI_GADGETMASK)
279  {
280  case GID_L_LOGIN:
281  ami_401login_login(lw);
282  return TRUE;
283  break;
284 
285  case GID_L_CANCEL:
286  ami_401login_close(lw);
287  return TRUE;
288  break;
289  }
290  break;
291  }
292  }
293  return FALSE;
294 }
295 
char pwd[256]
Definition: login.c:84
char * ami_utf8_easy(const char *string)
Definition: utf8.c:55
#define ButtonObj
Definition: libs.h:54
STATIC char result[100]
Definition: arexx.c:77
nserror(* cb)(const char *username, const char *password, void *pw)
Definition: login.c:78
lwc_string * host
Definition: login.c:82
Interface to utility string handling.
static const struct ami_win_event_table ami_login_table
Definition: login.c:91
Localised message support (interface).
#define WindowObj
Definition: libs.h:77
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)
login window request.
Definition: login.c:96
Core mouse and pointer states.
Interface to platform-specific graphical user interface window operations.
static BOOL ami_401login_event(void *w)
Definition: login.c:266
STRPTR ami_gui_get_screen_title(void)
Get the string for NetSurf&#39;s screen titlebar.
Definition: gui.c:969
void ami_utf8_free(char *ptr)
Definition: utf8.c:50
struct Screen * ami_gui_get_screen(void)
Get a pointer to the screen NetSurf is running on.
Definition: gui.c:398
nserror
Enumeration of error codes.
Definition: errors.h:29
Object * objects[GID_L_LAST]
Definition: login.c:77
No error.
Definition: errors.h:30
#define StringObj
Definition: libs.h:76
char * realm
Definition: login.c:81
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.
#define LabelObj
Definition: libs.h:63
void * cbpw
Definition: login.c:79
nsurl * url
Definition: login.c:80
static void ami_401login_login(struct gui_login_window *lw)
Definition: login.c:237
nserror amiga_warn_user(const char *warning, const char *detail)
Warn the user of an event.
Definition: misc.c:79
static void ami_401login_close(void *w)
Definition: login.c:216
const char * messages_get(const char *key)
Fast lookup of a message by key from the standard Messages hash.
Definition: messages.c:187
Unified URL information database public interface.
#define LayoutVObj
Definition: libs.h:65
uint16_t uint16
Definition: os3support.h:181
struct Window * win
Definition: login.c:76
#define LayoutHObj
Definition: libs.h:64
nsurl * nsurl_ref(nsurl *url)
Increment the reference count to a NetSurf URL object.
char * ami_to_utf8_easy(const char *string)
Definition: utf8.c:65
struct ami_generic_window w
Definition: login.c:75
const char * nsurl_access(const nsurl *url)
Access a NetSurf URL object as a string.
char * messages[AMI_LOGIN_MSG_MAX]
Definition: login.c:85
void ami_gui_win_list_remove(void *win)
Remove a window from the NetSurf window list.
Definition: gui.c:4505
Minimal compatibility header for AmigaOS 3.
nserror ami_gui_win_list_add(void *win, int type, const struct ami_win_event_table *table)
Add a window to the NetSurf window list (to enable event processing)
Definition: gui.c:4491
NetSurf URL handling (interface).
struct nsurl nsurl
NetSurf URL object.
Definition: nsurl.h:31
void nsurl_unref(nsurl *url)
Drop a reference to a NetSurf URL object.
char uname[256]
Definition: login.c:83
struct MsgPort * ami_gui_get_shared_msgport(void)
Get shared message port.