File: MyportalProtocolHandler.cpp

package info (click to toggle)
galeon 2.0.2-4
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 19,212 kB
  • ctags: 9,891
  • sloc: ansic: 77,793; cpp: 15,301; sh: 8,961; xml: 5,761; makefile: 888
file content (896 lines) | stat: -rw-r--r-- 20,010 bytes parent folder | download
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
/*
 *  Copyright (C) 2001 Matt Aubury, Philip Langdale
 *  Copyright (C) 2004 Crispin Flowerday
 *
 *  This program 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; either version 2, or (at your option)
 *  any later version.
 *
 *  This program 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, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 */

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

#include <nscore.h>
#define MOZILLA_INTERNAL_API
#include <nsIServiceManager.h>
#undef MOZILLA_INTERNAL_API
#include <nsCOMPtr.h>
#include <nsIIOService.h>
#include <nsIURI.h>
#include <nsIChannel.h>
#include <nsIOutputStream.h>
#include <nsIInputStream.h>
#include <nsILoadGroup.h>
#include <nsIInterfaceRequestor.h>

#include <nsIStorageStream.h>
#include <nsIInputStreamChannel.h>
#include <nsNetCID.h>

#include <libgnomevfs/gnome-vfs-utils.h>
#include <glib/gi18n.h>

#include "MyportalProtocolHandler.h"
#include "GaleonUtils.h"
#include "GulString.h"

#include "bookmarks.h"
#include "bookmarks-iterator.h"
#include "gul-general.h"
#include "src/galeon-shell.h"

#ifdef HAVE_CHECKLOADURI_CAPABILITIES
# define FILE_PREFIX ""
#else
# define FILE_PREFIX "myportal:url:"
#endif

static NS_DEFINE_CID(kSimpleURICID, NS_SIMPLEURI_CID);
static NS_DEFINE_CID(kInputStreamChannelCID, NS_INPUTSTREAMCHANNEL_CID);

/* Implementation file */
NS_IMPL_ISUPPORTS2 (GMyportalProtocolHandler, nsIProtocolHandler, nsIAboutModule)

GMyportalProtocolHandler::GMyportalProtocolHandler (void)
{
}

GMyportalProtocolHandler::~GMyportalProtocolHandler()
{
	/* destructor code */
}


/* readonly attribute string scheme; */
NS_IMETHODIMP
GMyportalProtocolHandler::GetScheme(nsACString &aScheme)
{
	aScheme = NS_LITERAL_CSTRING("myportal");
	return NS_OK;
}

/* readonly attribute long defaultPort; */
NS_IMETHODIMP
GMyportalProtocolHandler::GetDefaultPort(PRInt32 *aDefaultPort)
{
	NS_ENSURE_ARG_POINTER (aDefaultPort);

	*aDefaultPort = -1;
	return NS_OK;
}

/* readonly attribute short protocolFlags; */
NS_IMETHODIMP
GMyportalProtocolHandler::GetProtocolFlags(PRUint32 *aProtocolFlags)
{
	NS_ENSURE_ARG_POINTER (aProtocolFlags);

	/* Myportal urls don't have an authority component (user:pass) */
	*aProtocolFlags = nsIProtocolHandler::URI_NOAUTH;
	return NS_OK;
} 

/* nsIURI newURI (in string aSpec, in nsIURI aBaseURI); */
NS_IMETHODIMP
GMyportalProtocolHandler::NewURI(const nsACString &aSpec,
				 const char *aOriginCharset,
				 nsIURI *aBaseURI,
				 nsIURI **_retval)
{
	nsresult rv;

	nsCOMPtr<nsIURI> uri = do_CreateInstance(kSimpleURICID);
	NS_ENSURE_TRUE (uri, NS_ERROR_FAILURE);

	rv = uri->SetSpec (aSpec);
	NS_ENSURE_SUCCESS (rv, rv);

	NS_ADDREF(*_retval = uri);
	return NS_OK;
}

/* nsIChannel newChannel (in nsIURI aURI); */
NS_IMETHODIMP
GMyportalProtocolHandler::NewChannel(nsIURI *aURI,
				     nsIChannel **_retval)
{
	nsresult rv;

	PRBool isabout;
	rv =  aURI->SchemeIs ("about", &isabout);
	NS_ENSURE_SUCCESS (rv, rv);

	if (isabout)
	{
		rv = RedirectURL ("myportal:", _retval);
	}
	else
	{
		GulCString path;
		rv = aURI->GetPath (path);
		NS_ENSURE_SUCCESS (rv, rv);

		if (path.StartsWith ("form:"))
		{
			rv = HandleSmartBMSubmit (path.get() + 5, _retval);
			if (NS_SUCCEEDED (rv)) return NS_OK;
		}

#ifndef HAVE_CHECKLOADURI_CAPABILITIES
		if (path.StartsWith ("url:"))
		{
			rv = RedirectURL (path.get() + 4, _retval);
		}
		else
#endif
		{
			rv = CreateMyportalPage (path.get(), aURI, _retval);
		}
	}
	return rv;
}

/* boolean allowPort (in long port, in string scheme); */
NS_IMETHODIMP 
GMyportalProtocolHandler::AllowPort(PRInt32 port,
				    const char *scheme,
				    PRBool *_retval)
{
	*_retval = PR_FALSE;
	return NS_OK;
}

#ifdef HAVE_NSIABOUTMODULE_GETURIFLAGS
/* unsigned long getURIFlags (in nsIURI aURI); */
NS_IMETHODIMP
GMyportalProtocolHandler::GetURIFlags(nsIURI *aURI,
                                      PRUint32 *_retval)
{
	*_retval = 0;
	return NS_OK;
}
#endif

// ------------------------------------------------------------
/**
 * An nsIChannel implementation that just redirects to a
 * different url */
class GRedirectChannel : public nsIChannel
{
public:
	GRedirectChannel (const nsACString &aURL)
	: mURL (aURL), mLoadFlags (LOAD_NORMAL) {}

	NS_DECL_ISUPPORTS
	NS_DECL_NSICHANNEL
	NS_DECL_NSIREQUEST

private:
	const GulCString mURL;
	nsCOMPtr<nsILoadGroup>  mLoadGroup;
	nsCOMPtr<nsIInterfaceRequestor> mCallbacks;
	PRUint32 mLoadFlags;
};

NS_IMPL_ISUPPORTS2 (GRedirectChannel, nsIChannel, nsIRequest)

NS_IMETHODIMP
GRedirectChannel::GetNotificationCallbacks(nsIInterfaceRequestor * *aCallbacks)
{
	NS_IF_ADDREF(*aCallbacks = mCallbacks);
	return NS_OK;
}

NS_IMETHODIMP 
GRedirectChannel::SetNotificationCallbacks(nsIInterfaceRequestor * aCallbacks)
{
	mCallbacks = aCallbacks;
	return NS_OK;
}

NS_IMETHODIMP
GRedirectChannel::GetLoadGroup(nsILoadGroup * *aLoadGroup)
{
	NS_IF_ADDREF(*aLoadGroup = mLoadGroup);
	return NS_OK;
}

NS_IMETHODIMP
GRedirectChannel::SetLoadGroup(nsILoadGroup * aLoadGroup)
{
	mLoadGroup =  aLoadGroup;
	return NS_OK;
}

NS_IMETHODIMP
GRedirectChannel::GetLoadFlags(nsLoadFlags *aLoadFlags)
{
	*aLoadFlags = mLoadFlags;
	return NS_OK;
}

NS_IMETHODIMP
GRedirectChannel::SetLoadFlags(nsLoadFlags aLoadFlags)
{
	mLoadFlags = aLoadFlags;
	return NS_OK;
}

NS_IMETHODIMP
GRedirectChannel::GetOriginalURI(nsIURI * *aOriginalURI)
{
	return GaleonUtils::NewURI (aOriginalURI, mURL);
}


/* void asyncOpen (in nsIStreamListener aListener, in nsISupports aContext); */
NS_IMETHODIMP 
GRedirectChannel::AsyncOpen (nsIStreamListener *aListener, nsISupports *aContext)
{
	nsresult rv;

	nsCOMPtr<nsIIOService> ioService;

	/* Create a new Channel based off the url we want to redirect to
	 * then everthing appears to magically work, and the smb forms
	 * end up with the correct url */
	rv = GaleonUtils::GetIOService (getter_AddRefs (ioService));
	NS_ENSURE_SUCCESS (rv, rv);

	nsCOMPtr<nsIChannel> newChannel;

	rv = ioService->NewChannel (mURL, nsnull, nsnull, getter_AddRefs (newChannel));
	NS_ENSURE_SUCCESS (rv, rv);

	newChannel->SetLoadGroup (mLoadGroup); 
	newChannel->SetNotificationCallbacks (mCallbacks);
	newChannel->SetLoadFlags (mLoadFlags | LOAD_REPLACE);

	rv = newChannel->AsyncOpen (aListener, aContext);
	NS_ENSURE_SUCCESS (rv, rv);

	return NS_OK;
}

// ------------------------------------------------------------

nsresult
GMyportalProtocolHandler::RedirectURL (const char *path,
				       nsIChannel **aChannel)
{
	nsCOMPtr<nsIChannel> channel;

	/* Use our own channel implementation so that url in the
	 * location bar is correct after a form submit */

	channel = new GRedirectChannel (GulDependentCString (path));

	NS_ADDREF(*aChannel = channel);

	return NS_OK;
}

/**
 * Smart bookmarks are rendered using a form that points back
 * to the magic myportal:form: url. The rest of the path is the
 * bookmark id, with the query string being the smart bookmark
 * arguments.
 *
 * The arguments are always submitted in UTF-8 and so we can just
 * extract them from the query string, and use the normal
 * bookmark code to create the final url, which we then
 * redirect to.
 */
nsresult
GMyportalProtocolHandler::HandleSmartBMSubmit (const char *path,
					       nsIChannel **aChannel)
{
	GbBookmarkSet *bmset;
	GbBookmark *b;
	int i, nargs, count;
	gchar **raw_args, **args;
	gchar *url;
	
	/* Path includes the query string, so lets strip that off */
	const char *qs = strchr (path, '?');
	if (!qs) return NS_ERROR_FAILURE;

	gchar *id = g_strndup (path, qs - path);
	bmset = galeon_shell_get_bookmark_set (galeon_shell);
	b = gb_bookmark_set_get_bookmark_for_id (bmset, id);
	g_free (id);

	if (!b || ! GB_IS_SMART_SITE (b))
	{
		return NS_ERROR_FAILURE;
	}

	args = g_strsplit (qs+1, "&", -1);
	for (nargs = 0; args[nargs]; nargs++) ;

	raw_args = g_new0 (gchar *, nargs + 1);
	count = 0;
	for (i = 0; i < nargs; i++)
	{
		/* Ignore things like x= and y= (from image submits) */
		if (!g_str_has_prefix (args[i], "arg="))
		{
			continue;
		}

		/* Replace '+''s with ' ' (gnome_vfs doesn't do this) */
		gchar * arg = args[i] +4;
		gchar *tmp = arg;
		for (; *tmp; tmp++)
		{
			if (*tmp == '+') *tmp = ' ';
		}

		raw_args[count++] = gnome_vfs_unescape_string (arg, "");

	}
	raw_args[count] = NULL;

	url = gb_smart_site_subst_args (GB_SMART_SITE (b), raw_args);

	g_strfreev (args);
	g_strfreev (raw_args);

	nsresult rv;
	rv = RedirectURL (url, aChannel);
	g_free (url);

	return rv;
}

nsresult
GMyportalProtocolHandler::Write (const char * aStr)
{
	NS_ENSURE_TRUE (mStream, NS_ERROR_FAILURE);

	PRUint32 bytesWritten;
	if (aStr)
	{
		nsresult rv;
		rv = mStream->Write (aStr, strlen (aStr), &bytesWritten);
		NS_ENSURE_SUCCESS (rv, rv);
	}
	return NS_OK;
}


nsresult
GMyportalProtocolHandler::WriteHTMLEscape (const char * aStr)
{
	nsresult rv = NS_OK;
	if (aStr)
	{
		char *str;
		
		str = g_markup_escape_text (aStr, strlen (aStr));
		rv = Write (str);
		g_free (str);
	}
	return rv;
}

nsresult
GMyportalProtocolHandler::CreateMyportalPage (const char *path,
					      nsIURI *aURI,
					      nsIChannel **result)
{
	nsresult rv;

	if (!path || path[0] == '\0')
	{
		path = "/";
	}

	/* open the rendering stream */
	nsCOMPtr<nsIStorageStream> sStream;

	rv = NS_NewStorageStream(16384, (PRUint32)-1, getter_AddRefs(sStream));
	NS_ENSURE_SUCCESS (rv, rv);

	rv = sStream->GetOutputStream(0, getter_AddRefs(mStream));
	NS_ENSURE_SUCCESS (rv, rv);

	/* render the complete portal */
	Write ("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" "
	       "\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n");
	Write ("<html xmlns=\"http://www.w3.org/1999/xhtml\" lang=\"en\" xml:lang=\"en\">\n");
	Write ("<head>\n");
	gchar *cssPath = gul_general_user_file ("myportal.css", FALSE);	
	if (cssPath)
	{
		Write ("<link rel=\"stylesheet\" href=\"" FILE_PREFIX "file://");
		Write (cssPath);
		Write ("\" type=\"text/css\" />\n");
		g_free (cssPath);
	}
	Write ("<title>");

	gchar *tmp = gnome_vfs_unescape_string (path, "");
	/* Skip multiple leading '/' (it's guaranteed to be at least 1 char long due
	 * to the path being set to '/' at the top of the function */
	const gchar *q = tmp;
	while (q[1] == '/') q++;

	/* Translators: the %s is the bookmark path, e.g. "/Programs/Galeon" */
	gchar *str = g_strdup_printf (_("My Portal: %s"), q);
	WriteHTMLEscape (str);
	g_free (str);
	g_free (tmp);
	Write ("</title>");
	Write ("</head>\n");
	Write ("<body>\n");
	Write ("<div class=\"top_bar\">\n");
	Write ("<img src=\"" FILE_PREFIX "file://" SHARE_DIR "/galeon.png\" alt=\"\" />myportal:\n");
	Write ("</div>\n");
	Write ("<div class=\"top2\">\n");
	Write ("<a href=\"http://galeon.sourceforge.net\">");
	Write ("<img class=\"logo\" src=\"" FILE_PREFIX "file://" SHARE_DIR "/logo.png\" alt=\"\" />");
	Write ("</a>\n</div>\n");
	Write ("<!-- BOOKMARKS -->\n\n");
	RenderFromPath (path);
	Write ("\n\n<!-- END -->");
	Write ("<div class=\"bottom_bar\">\n");
	Write ("<b>Galeon</b> : <i>the web, only the web</i>\n");
	Write ("</div>");
	Write ("</body></html>\n");
 
	mStream = nsnull;

	/* finish the rendering */
	nsCOMPtr<nsIInputStream> iStream;
	rv = sStream->NewInputStream(0, getter_AddRefs(iStream));
	NS_ENSURE_SUCCESS (rv, rv);

	nsCOMPtr<nsIInputStreamChannel> isc =
		do_CreateInstance(kInputStreamChannelCID, &rv);
	NS_ENSURE_SUCCESS (rv, rv);

        rv |= isc->SetURI (aURI);
	rv |= isc->SetContentStream (iStream);

	nsCOMPtr<nsIChannel> channel = do_QueryInterface (isc, &rv);
	NS_ENSURE_SUCCESS (rv, rv);

        rv |= channel->SetContentType (NS_LITERAL_CSTRING ("application/xhtml+xml"));
        rv |= channel->SetContentCharset (NS_LITERAL_CSTRING ("utf-8"));
	NS_ENSURE_SUCCESS (rv, rv);

	NS_ADDREF(*result = channel);
	return rv;
}

////////////////////////////////////////////////////////////////////////////////
/* This is just used for finding, not sorting, so we only worry about
 * zero vs non-zero */ 
static gint 
folder_name_compare (const GbBookmark *a, const gchar *name)
{
	if (a->name == NULL)
		return -1;

	if (!GB_IS_FOLDER (a))
		return -1;

	return strcmp (name, a->name);
}

nsresult
GMyportalProtocolHandler::RenderFromPath (const gchar *path)
{
	GbBookmark *root;

	root = GB_BOOKMARK(galeon_shell_get_bookmark_set(galeon_shell)->root);
	if (!root) return NS_OK;
	g_return_val_if_fail (GB_IS_FOLDER (root), NS_ERROR_FAILURE);

	GString *clippedpath = g_string_new (path);
	const gchar *anch = strchr (path, '#');
	if (anch)
	{
		g_string_truncate (clippedpath, anch-path);
	}

	gchar **paths = g_strsplit (clippedpath->str, "/", 0);

	/* Draw the first folder heading (might have extra 
	 * path components) */
	Write ("<div class=\"folder\">\n");
	Write ("<h1 class=\"folderheading\">\n");

	GString *prefix = g_string_new ("myportal://");
	int count = 0;
	while (root)
	{
		if (count != 0 )
		{
			Write ("/");
		}

		Write ("<a href=\"");

		WriteHTMLEscape (prefix->str);

		Write ("\">");
		WriteHTMLEscape (root->name);
		Write ("</a>");

		/* Find the next child */
		while (paths[count] && *paths[count] == 0) count++;

		/* reached end of the paths, so break out */
		if (!paths[count])
		{
			break;
		}

		gchar *name = gnome_vfs_unescape_string (paths[count], "");

		GSList *children = gb_folder_list_children (GB_FOLDER (root));

		GSList *match = g_slist_find_custom (children, name,
						     (GCompareFunc)folder_name_compare);
		if (!match)
		{
			root = NULL;
			break;
		}

		char *escaped = gnome_vfs_escape_string (name);
		g_string_append (prefix, escaped);
		g_string_append_c (prefix, '/');
		g_free (name);
		g_free (escaped);
		
		count++;
		root = GB_BOOKMARK (match->data);
	}
	g_strfreev (paths);

	Write ("</h1>\n");

	if (root)
	{
		RenderFolderContents (GB_FOLDER (root), prefix->str, 1);
	}
	else
	{
		Write ("<div class=\"foldercontents\">\n");

		gchar *unescaped = gnome_vfs_unescape_string (clippedpath->str, "");
		gchar *buf = g_markup_printf_escaped (_("%s not found"), 
						      unescaped);
		Write (buf);
		Write ("</div>\n");

		g_free (buf);
		g_free (unescaped);
	}		

	g_string_free (clippedpath, TRUE);
	g_string_free (prefix, TRUE);
	Write ("</div>\n");

	return NS_OK;
}

/**
 * render_from_item: render a single bookmark item
 */
nsresult
GMyportalProtocolHandler::RenderItem (GbBookmark *b, const gchar *prefix, 
				      gint depth)
{
	/* could be the case if there are no bookmarks */
	if (b == NULL)
	{
		return NS_OK;
	}

	/* otherwise do the appropriate thing */
	if (GB_IS_FOLDER (b))
	{
		RenderFolder (GB_FOLDER (b), prefix, depth);
	} 
	else if (GB_IS_SMART_SITE (b))
	{
		RenderSmartURL (GB_SMART_SITE (b));
	}
	else if (GB_IS_SITE (b))
	{
		RenderURL (GB_SITE (b));
	}
	else if (GB_IS_SEPARATOR (b))
	{		
		Write ("<div class=\"separator\"></div>\n");
	}

	return NS_OK;
}

nsresult
GMyportalProtocolHandler::RenderFolderContents (GbFolder *b, const gchar *prefix, 
						gint depth)
{
	GbBookmark *bi;
	GbIterator *i;

	Write ("<div class=\"foldercontents\">\n");

	i = gb_iterator_new_folder (b, FALSE);
	bi = gb_iterator_next(i);	

	if (bi)
	{
		do
		{
			RenderItem (bi, prefix, depth);
		} while ((bi = gb_iterator_next(i)));

	}
	else
	{
		Write ("<i>");
		Write (_("Empty Folder"));
		Write ("</i>");
	}
	g_object_unref (i);

	Write ("</div>\n");

	return NS_OK;
}

/**
 * render_from_list: render a list of bookmark items
 */
nsresult
GMyportalProtocolHandler::RenderFolder (GbFolder *b, const gchar *prefix, gint depth)
{
	const gchar *name;
	gchar *newprefix;

	name = GB_BOOKMARK (b)->name;

	if (depth == 0)
	{
		newprefix = g_strdup (prefix);
	}
	else
	{
		char *escaped = gnome_vfs_escape_string (name);
		newprefix = g_strconcat (prefix, escaped, "/", NULL);
		g_free (escaped);
	}

	Write ("<div class=\"folder\">\n");
	Write ("<h1 class=\"folderheading\">\n");

	Write ("<a href=\"");
	WriteHTMLEscape (newprefix);
	Write ("\">");

	WriteHTMLEscape (name);
	Write ("</a>\n");
	Write ("</h1>\n");

	RenderFolderContents (b, newprefix, depth+1);

	Write ("</div>\n");

	g_free (newprefix);

	return NS_OK;
}


/**
 * render_url: render a bookmark url
 */
nsresult
GMyportalProtocolHandler::RenderURL (GbSite *b)
{
	Write ("<a href=\"");

	/* If url has no preceding "http://" or "ftp://" presume "http://" */
	if (!strchr (b->url, ':'))
	{
		Write ("http://");
	}

	if (g_str_has_prefix (b->url, "file:"))
	{
		Write (FILE_PREFIX);
	}

	WriteHTMLEscape (b->url);
	Write ("\">");

	if (gb_bookmark_get_image(GB_BOOKMARK(b)) == NULL)
	{
		/* use name if we have one, and otherwise, url */
		if (GB_BOOKMARK(b)->name && GB_BOOKMARK(b)->name[0] != '\0')
		{
			WriteHTMLEscape (GB_BOOKMARK(b)->name);
		}
		else
		{
			WriteHTMLEscape (b->url);
		}
	}
	else
	{
		Write ("<img src=\"" FILE_PREFIX "file://");
		Write (GB_BOOKMARK(b)->pixmap_file);
		Write ("\" />");
	}
	Write ("</a>\n");

	return NS_OK;
}

/**
 * render_search_form: render a search form based on a smart bookmark,
 * this posts back to myportal to avoid problems with %s's not
 * inside a query string
 */
nsresult
GMyportalProtocolHandler::RenderSmartURL (GbSmartSite *b)
{
	Write ("<form action=\"myportal:form:");
	WriteHTMLEscape (gb_bookmark_get_id (GB_BOOKMARK (b)));
	Write ("\" method=\"get\"><p>");

	gint num_inputs = gb_smart_site_get_num_fields (b);

	for (int i = 0 ; i < num_inputs ; i++)
	{
		Write ("<input type=\"text\" name=\"arg\" size=\"");
		Write (num_inputs>1?"10\" />":"30\" />");
	}

	if (gb_bookmark_get_image(GB_BOOKMARK(b)) == NULL)
	{
		Write ("<input type=\"submit\" value=\"");
		WriteHTMLEscape (GB_BOOKMARK (b)->name);
	}
	else
	{
		Write ("<input type=\"image\" src=\"" FILE_PREFIX "file://");
		WriteHTMLEscape (GB_BOOKMARK(b)->pixmap_file);
	}
	Write ("\" /></p></form>\n");

	return NS_OK;
}


/** ------------------------------------------------------------ *
 *  Other nsIChannel functions */
#define MOZ_NOT_IMPLEMENTED return NS_ERROR_NOT_IMPLEMENTED;

NS_IMETHODIMP GRedirectChannel::GetName(nsACString & aName)
{
    MOZ_NOT_IMPLEMENTED;
}

NS_IMETHODIMP GRedirectChannel::IsPending(PRBool *_retval)
{
    MOZ_NOT_IMPLEMENTED;
}

NS_IMETHODIMP GRedirectChannel::GetStatus(nsresult *aStatus)
{
    MOZ_NOT_IMPLEMENTED;
}

NS_IMETHODIMP GRedirectChannel::Cancel(nsresult aStatus)
{
    MOZ_NOT_IMPLEMENTED;
}

NS_IMETHODIMP GRedirectChannel::Suspend()
{
    MOZ_NOT_IMPLEMENTED;
}

NS_IMETHODIMP GRedirectChannel::Resume()
{
    MOZ_NOT_IMPLEMENTED;
}

NS_IMETHODIMP GRedirectChannel::SetOriginalURI(nsIURI * aOriginalURI)
{
    MOZ_NOT_IMPLEMENTED;
}

NS_IMETHODIMP GRedirectChannel::GetURI(nsIURI * *aURI)
{
    MOZ_NOT_IMPLEMENTED;
}

NS_IMETHODIMP GRedirectChannel::GetOwner(nsISupports * *aOwner)
{
    MOZ_NOT_IMPLEMENTED;
}

NS_IMETHODIMP GRedirectChannel::SetOwner(nsISupports * aOwner)
{
    MOZ_NOT_IMPLEMENTED;
}

NS_IMETHODIMP GRedirectChannel::GetSecurityInfo(nsISupports * *aSecurityInfo)
{
    MOZ_NOT_IMPLEMENTED;
}

NS_IMETHODIMP GRedirectChannel::GetContentType(nsACString & aContentType)
{
    MOZ_NOT_IMPLEMENTED;
}

NS_IMETHODIMP GRedirectChannel::SetContentType(const nsACString & aContentType)
{
    MOZ_NOT_IMPLEMENTED;
}

NS_IMETHODIMP GRedirectChannel::GetContentCharset(nsACString & aContentCharset)
{
    MOZ_NOT_IMPLEMENTED;
}

NS_IMETHODIMP GRedirectChannel::SetContentCharset(const nsACString & aContentCharset)
{
    MOZ_NOT_IMPLEMENTED;
}

NS_IMETHODIMP GRedirectChannel::GetContentLength(PRInt32 *aContentLength)
{
    MOZ_NOT_IMPLEMENTED;
}

NS_IMETHODIMP GRedirectChannel::SetContentLength(PRInt32 aContentLength)
{
    MOZ_NOT_IMPLEMENTED;
}

NS_IMETHODIMP GRedirectChannel::Open(nsIInputStream **_retval)
{
    MOZ_NOT_IMPLEMENTED;
}