ntl-web-browser.c 11.7 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <WWWCore.h>
#include <WWWStream.h>
#include <WWWTrans.h>
#include <WWWHTTP.h>
#include <WWWMIME.h>
#include <WWWFTP.h>
#include <WWWFile.h>
#include <WWWGophe.h>
#include <WWWZip.h>

/* clean up the cpp namespace -- libwww is particularly dirty */
#undef PACKAGE
#undef VERSION
#undef _

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "glibwww.h"
21
22
23
#ifdef HAVE_ALLOCA_H
#include <alloca.h>
#endif
24
25
26
#include <gnome.h>
#include <libnautilus/libnautilus.h>
#include <gtkhtml/gtkhtml.h>
27
#include <libgnorba/gnorba.h>
28
29
30
31
32

extern GtkHTMLStreamHandle gtk_html_stream_ref(GtkHTMLStreamHandle handle);
extern void gtk_html_stream_unref(GtkHTMLStreamHandle handle);

typedef struct {
33
  NautilusViewFrame *view_frame;
34
35
  GtkWidget *htmlw;
  char *base_url, *base_target_url;
36
37
38
39
40

  int prevsel;

  HTMethod method;
  char *post_data;
41
42
} BrowserInfo;

43
static char *
44
45
canonicalize_url (const char *in_url, const char *base_url)
{
46
  char *ctmp, *ctmp2, *retval, *removebegin, *removeend, *curpos;
47

48
  g_return_val_if_fail(in_url, NULL);
49

50
51
52
53
54
55
56
57
58
59
  ctmp = strstr(in_url, "://");
  if(ctmp)
    {
      retval = g_strdup(in_url);
      goto out;
    }
  else if(*in_url == '/')
    {
      ctmp = base_url?strstr(base_url, "://"):NULL;
      if(!ctmp)
60
	{
61
62
	  retval = g_strconcat("file://", in_url, NULL);
	  goto out;
63
64
	}

65
      ctmp2 = strchr(ctmp + 3, '/');
66

67
68
69
      retval = g_strconcat(base_url, in_url, NULL);
      goto out;
    }
70

71
  /* XXX TODO - We should really do processing of .. and . in URLs */
72

73
74
75
76
  ctmp = base_url?strstr(base_url, "://"):NULL;
  if(!ctmp)
    {
      char *cwd;
77

78
79
80
      cwd = g_get_current_dir();
      ctmp = g_strconcat("file://", cwd, "/", in_url, NULL);
      g_free(cwd);
81

82
83
84
      retval = ctmp;
      goto out;
    }
85

86
  retval = g_strconcat(base_url, "/", in_url, NULL);
87
88

 out:
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
  /* Now fix up the /. and /.. pieces */

  ctmp = strstr(retval, "://");
  g_assert(ctmp);
  ctmp += 3;
  ctmp = strchr(ctmp, '/');
  if(!ctmp) {
    ctmp = retval;
    retval = g_strconcat(retval, "/", NULL);
    g_free(ctmp);
    return retval;
  }

  removebegin = removeend = NULL;
  do {
    if(removebegin && removeend)
      {
	memmove(removebegin, removeend, strlen(removeend) + 1);
107
	removebegin = removeend = NULL;
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
      }
    curpos = ctmp;

  redo:
    ctmp2 = strstr(curpos, "/.");
    if(!ctmp2)
      break;

    if(*(ctmp2 + 2) == '.') /* We have to skip over stuff like /...blahblah or /.foo */
      {
	if(*(ctmp2 + 3) != '/'
	   && *(ctmp2 + 3) != '\0')
	  {
	    curpos = ctmp2 + 3;
	    goto redo;
	  }
      }
    else if(*(ctmp2 + 2) != '/' && *(ctmp2 + 2) != '\0')
      {
	curpos = ctmp2 + 2;
	goto redo;
      }

    switch(*(ctmp2+2))
      {
      case '/':
      case '\0':
	removebegin = ctmp2;
	removeend = ctmp2 + 2;
	break;
      case '.':
	removeend = ctmp2 + 3;
	ctmp2--;
	while((ctmp2 >= ctmp) && *ctmp2 != '/')
	  ctmp2--;
	if(*ctmp2 == '/')
	  removebegin = ctmp2;
	break;
      }

  } while(removebegin);

  return retval;
151
152
153
154
155
}

static void
browser_url_load_done(GtkWidget *htmlw, BrowserInfo *bi)
{
156
  Nautilus_ProgressRequestInfo pri;
157
158
159

  gtk_html_calc_scrollbars(GTK_HTML(bi->htmlw));

160
161
162
163
  memset(&pri, 0, sizeof(pri));

  pri.type = Nautilus_PROGRESS_DONE_OK;
  pri.amount = 100.0;
164
  nautilus_view_frame_request_progress_change(bi->view_frame, &pri);
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
}

struct _HTStream {
  const HTStreamClass *	isa;
  BrowserInfo *bi;
  GtkHTMLStreamHandle handle;
};

static int netin_stream_write (HTStream * me, const char * s, int l)
{
  gtk_html_write(GTK_HTML(me->bi->htmlw), me->handle, s, l);

  return HT_OK;
}

static int netin_stream_put_character (HTStream * me, char c)
{
  return netin_stream_write(me, &c, 1);
}

static int netin_stream_put_string (HTStream * me, const char * s)
{
  return netin_stream_write(me, s, strlen(s));
}

static int netin_stream_flush (HTStream * me)
{
  return HT_OK;
}

static int netin_stream_free (HTStream * me)
{
197
198
199
  g_return_val_if_fail(me->handle, HT_ERROR);

  g_message("netin_stream_free");
200
  gtk_html_end(GTK_HTML(me->bi->htmlw), me->handle, GTK_HTML_STREAM_OK);
201
  me->handle = NULL;
202
203
204
205
206
207
208
  g_free(me);

  return HT_OK;
}

static int netin_stream_abort (HTStream * me, HTList * e)
{
209
210
  g_message("netin_stream_abort");
  gtk_html_end(GTK_HTML(me->bi->htmlw), me->handle, GTK_HTML_STREAM_ERROR);
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
  g_free(me);

  return HT_OK;
}

static const HTStreamClass netin_stream_class =
{		
    "netin_stream",
    netin_stream_flush,
    netin_stream_free,
    netin_stream_abort,
    netin_stream_put_character,
    netin_stream_put_string,
    netin_stream_write
}; 

static HTStream *
netin_stream_new (BrowserInfo *bi, GtkHTMLStreamHandle handle)
{
  HTStream *retval;

  retval = g_new0(HTStream, 1);

  retval->isa = &netin_stream_class;
  retval->bi = bi;
236
  retval->handle = handle;
237
238
239
240

  return retval;
}

241
242
243
244
245
246
247
248
249
static gboolean
do_request_delete(gpointer req)
{
  HTRequest_delete(req);

  return FALSE;
}

static int
250
251
252
253
request_terminator (HTRequest * request, HTResponse * response, void * param, int status)
{
  gpointer d;

254
255
256
  if (status != HT_LOADED) 
    g_print("Load couldn't be completed successfully (%p)\n", request);

257
258
259
260
  d = HTRequest_context(request);
  g_return_val_if_fail(d, HT_OK);

  HTRequest_setContext(request, NULL);
261
262
263
264
265
  g_idle_add(do_request_delete, request);

  return HT_OK;
}

266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
static int
browser_do_post(HTRequest *request, HTStream *stream)
{
  BrowserInfo *bi = HTRequest_context(request);
  int status;

  g_assert(bi);

  status = (*stream->isa->put_block)(stream, bi->post_data, strlen(bi->post_data));

  g_message("browser_do_post got status %d", status);

  switch(status)
    {
    case HT_LOADED:
    case HT_OK:
      g_free(bi->post_data); bi->post_data = NULL;
      (*stream->isa->flush)(stream);
    default:
      return status;
      break;
    }
}
289

290
291
292
293
294
295
296
297
298
299
300
static void
browser_url_requested(GtkWidget *htmlw, const char *url, GtkHTMLStreamHandle handle, BrowserInfo *bi)
{
  char *real_url;
  HTRequest *request;
  HTStream *writer;

  real_url = canonicalize_url(url, bi->base_url);

  request = HTRequest_new();
  writer = netin_stream_new(bi, handle);
301
  HTRequest_setContext(request, bi);
302
303
  HTRequest_setOutputFormat(request, WWW_SOURCE);
  HTRequest_setOutputStream(request, writer);
304
305
  if(bi->method == METHOD_POST)
    HTRequest_setPostCallback(request, browser_do_post);
306
  HTRequest_setAnchor(request, HTAnchor_findAddress(real_url));
307
308
  HTRequest_setMethod(request, bi->method);
  bi->method = METHOD_GET;
309
310
311
312

  if(HTLoad(request, NO) == NO)
    {
      HTRequest_delete(request);
313
314
      /* I think deleting the request will end the stream, too */
      /* gtk_html_end(GTK_HTML(bi->htmlw), handle, GTK_HTML_STREAM_ERROR); */
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
    }

  g_free(real_url);
}

static void
browser_set_base(GtkWidget *htmlw, const char *base_url, BrowserInfo *bi)
{
  if(bi->base_url)
    g_free(bi->base_url);
  bi->base_url = g_strdup(base_url);
}

static void
browser_set_base_target(GtkWidget *htmlw, const char *base_target_url, BrowserInfo *bi)
{
  if(bi->base_target_url)
    g_free(bi->base_target_url);
  bi->base_target_url = g_strdup(base_target_url);
}

static void
browser_goto_url_real(GtkWidget *htmlw, const char *url, BrowserInfo *bi)
{
339
340
341
342
343
  Nautilus_ProgressRequestInfo pri;

  pri.type = Nautilus_PROGRESS_UNDERWAY;
  pri.amount = 0.0;

344
  HTNet_killAll();
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
  g_free(bi->base_url);
  g_free(bi->base_target_url);

  if(url[strlen(url) - 1] == '/')
    {
      bi->base_url = g_strdup(url);
      bi->base_target_url = g_strdup(url);
    }
  else
    {
      bi->base_url = g_dirname(url);
      bi->base_target_url = g_dirname(url);
    }

  gtk_html_begin(GTK_HTML(bi->htmlw), url);
360
  nautilus_view_frame_request_progress_change(bi->view_frame, &pri);
361
362
363
364
365
366
367
368
369
370
371
372
373
374
}

static void
browser_goto_url(GtkWidget *htmlw, const char *url, BrowserInfo *bi)
{
  Nautilus_NavigationRequestInfo nri;
  char *real_url;

  real_url = canonicalize_url(url, bi->base_target_url);

  g_return_if_fail(real_url);

  memset(&nri, 0, sizeof(nri));
  nri.requested_uri = real_url;
375
  nautilus_view_frame_request_location_change(bi->view_frame, &nri);
376
377
378
379
  browser_goto_url_real(htmlw, real_url, bi);
  g_free(real_url);
}

380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
static void
browser_select_url(GtkWidget *htmlw, const char *url, BrowserInfo *bi)
{
  Nautilus_SelectionRequestInfo si;
  char *real_url = NULL;

  memset(&si, 0, sizeof(si));
  if(url && !bi->prevsel)
    {
      si.selected_uris._length = 1;
      real_url = canonicalize_url(url, bi->base_target_url);
      si.selected_uris._buffer = &real_url;
    }
  else if(!url && bi->prevsel)
    {
      si.selected_uris._length = 0;
    }

398
  nautilus_view_frame_request_selection_change(bi->view_frame, &si);
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
  g_free(real_url);
  bi->prevsel = url?1:0;
}

static void
browser_submit(GtkWidget *htmlw, const char *method, const char *url, const char *encoding, BrowserInfo *bi)
{
  g_free(bi->post_data); bi->post_data = NULL;

  if(!strcasecmp(method, "POST"))
    {
      char **pieces = g_strsplit(encoding, "&", -1);

      if(pieces)
	{
	  char *ctmp;
	  ctmp = g_strjoinv("\r\n", pieces);
	  bi->post_data = g_strconcat(ctmp, "\r\n", NULL);
	  g_free(ctmp);
	  g_strfreev(pieces);
	  bi->method = METHOD_POST;
	}

      browser_goto_url(htmlw, url, bi);
    }
  else
    {
      char tmp_url[4096];

      g_snprintf(tmp_url, sizeof(tmp_url), "%s?%s", url, encoding);
      browser_goto_url(htmlw, tmp_url, bi);
    }
}

433
static void
434
browser_notify_location_change(NautilusViewFrame *view_frame, Nautilus_NavigationInfo *ni, BrowserInfo *bi)
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
{
  if(ni->self_originated)
    return;

  browser_goto_url_real(NULL, ni->requested_uri, bi);
}

static int object_count = 0;

static void
browser_do_destroy(GtkObject *obj)
{
  object_count--;
  if(object_count <= 0)
    gtk_main_quit();
}

452
453
static BonoboObject *
make_obj(BonoboGenericFactory *Factory, const char *goad_id, void *closure)
454
455
456
457
458
459
460
461
{
  BrowserInfo *bi;
  GtkWidget *wtmp;

  if(strcmp(goad_id, "ntl_web_browser"))
    return NULL;

  bi = g_new0(BrowserInfo, 1);
462
463
  bi->view_frame = NAUTILUS_VIEW_FRAME(gtk_widget_new(nautilus_content_view_frame_get_type(), NULL));
  gtk_signal_connect(GTK_OBJECT(bi->view_frame), "notify_location_change", browser_notify_location_change,
464
		     bi);
465
  gtk_signal_connect(GTK_OBJECT(bi->view_frame), "destroy", browser_do_destroy, NULL);
466
467
468
469
470
471
472
473
474
  object_count++;

  bi->htmlw = gtk_html_new();
  gtk_signal_connect(GTK_OBJECT(bi->htmlw), "link_clicked", browser_goto_url, bi);
  gtk_signal_connect(GTK_OBJECT(bi->htmlw), "set_base", browser_set_base, bi);
  gtk_signal_connect(GTK_OBJECT(bi->htmlw), "load_done", browser_url_load_done, bi);
  gtk_signal_connect(GTK_OBJECT(bi->htmlw), "set_base", browser_set_base, bi);
  gtk_signal_connect(GTK_OBJECT(bi->htmlw), "set_base_target", browser_set_base_target, bi);
  gtk_signal_connect(GTK_OBJECT(bi->htmlw), "url_requested", browser_url_requested, bi);
475
476
  gtk_signal_connect(GTK_OBJECT(bi->htmlw), "on_url", browser_select_url, bi);
  gtk_signal_connect(GTK_OBJECT(bi->htmlw), "submit", browser_submit, bi);
477
478
479

  wtmp = gtk_scrolled_window_new(NULL, NULL);
  gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(wtmp), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
480
  gtk_container_add(GTK_CONTAINER(bi->view_frame), wtmp);
481
482
483
484

  gtk_container_add(GTK_CONTAINER(wtmp), bi->htmlw);
  gtk_widget_show(bi->htmlw);
  gtk_widget_show(wtmp);
485
  gtk_widget_show(GTK_WIDGET(bi->view_frame));
486

487
  return nautilus_view_frame_get_bonobo_object(bi->view_frame);
488
489
490
491
}

int main(int argc, char *argv[])
{
492
  BonoboGenericFactory *factory;
493
494
495
496
497
498
499
500
  CORBA_ORB orb;
  CORBA_Environment ev;

  CORBA_exception_init(&ev);
  orb = gnome_CORBA_init_with_popt_table("ntl-web-browser", VERSION, &argc, argv, NULL, 0, NULL,
					 GNORBA_INIT_SERVER_FUNC, &ev);
  gdk_rgb_init();
  glibwww_init("ntl-web-browser", VERSION);
501
  HTNet_addAfter(request_terminator, NULL, NULL, HT_ALL, HT_FILTER_LAST);
502
503
  bonobo_init(orb, CORBA_OBJECT_NIL, CORBA_OBJECT_NIL);

504
  factory = bonobo_generic_factory_new_multi("ntl_web_browser_factory", make_obj, NULL);
505
506
507
508
509
510
511

  do {
    bonobo_main();
  } while(object_count > 0);

  return 0;
}