2001-09-29 00:14:13 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: NPL 1.1/GPL 2.0/LGPL 2.1
|
2001-03-01 10:22:19 +03:00
|
|
|
*
|
2001-09-29 00:14:13 +04:00
|
|
|
* The contents of this file are subject to the Netscape Public License
|
|
|
|
* Version 1.1 (the "License"); you may not use this file except in
|
|
|
|
* compliance with the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/NPL/
|
2001-03-01 10:22:19 +03:00
|
|
|
*
|
2001-09-29 00:14:13 +04:00
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
2001-03-01 10:22:19 +03:00
|
|
|
*
|
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
2001-09-29 00:14:13 +04:00
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Netscape Communications Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 1998
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
2001-03-01 10:22:19 +03:00
|
|
|
*
|
2001-10-03 04:43:30 +04:00
|
|
|
* Contributor(s): Bradley Baetz <bbaetz@cs.mcgill.ca>
|
2002-04-10 14:28:57 +04:00
|
|
|
* Christopher A. Aillon <christopher@aillon.com>
|
2001-09-29 00:14:13 +04:00
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the NPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the NPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
2001-03-01 10:22:19 +03:00
|
|
|
|
|
|
|
#include "nsIndexedToHTML.h"
|
|
|
|
#include "nsNetUtil.h"
|
|
|
|
#include "nsIStringStream.h"
|
2002-03-06 10:48:55 +03:00
|
|
|
#include "nsIFileURL.h"
|
2001-03-01 10:22:19 +03:00
|
|
|
#include "nsEscape.h"
|
2001-10-03 04:43:30 +04:00
|
|
|
#include "nsIDirIndex.h"
|
|
|
|
#include "prtime.h"
|
|
|
|
#include "nsDateTimeFormatCID.h"
|
2002-09-13 23:32:45 +04:00
|
|
|
#include "nsURLHelper.h"
|
2002-05-15 22:55:21 +04:00
|
|
|
#include "nsCRT.h"
|
2001-03-01 10:22:19 +03:00
|
|
|
|
2001-10-03 04:43:30 +04:00
|
|
|
NS_IMPL_THREADSAFE_ISUPPORTS4(nsIndexedToHTML,
|
|
|
|
nsIDirIndexListener,
|
2001-03-01 10:22:19 +03:00
|
|
|
nsIStreamConverter,
|
2001-04-10 10:01:08 +04:00
|
|
|
nsIRequestObserver,
|
2001-10-03 04:43:30 +04:00
|
|
|
nsIStreamListener)
|
2001-03-01 10:22:19 +03:00
|
|
|
|
2001-10-03 04:43:30 +04:00
|
|
|
static NS_DEFINE_CID(kDateTimeFormatCID, NS_DATETIMEFORMAT_CID);
|
2001-03-01 10:22:19 +03:00
|
|
|
|
2002-08-20 07:32:28 +04:00
|
|
|
static void ConvertNonAsciiToNCR(const nsAString& in, nsAFlatString& out)
|
|
|
|
{
|
|
|
|
nsAString::const_iterator start, end;
|
|
|
|
|
|
|
|
in.BeginReading(start);
|
|
|
|
in.EndReading(end);
|
|
|
|
|
|
|
|
out.Truncate();
|
|
|
|
|
|
|
|
while (start != end) {
|
|
|
|
if (*start < 128) {
|
|
|
|
out.Append(*start++);
|
|
|
|
} else {
|
|
|
|
out.Append(NS_LITERAL_STRING("&#x"));
|
|
|
|
nsAutoString hex;
|
|
|
|
hex.AppendInt(*start++, 16);
|
|
|
|
out.Append(hex);
|
|
|
|
out.Append((PRUnichar)';');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-02-05 02:23:21 +03:00
|
|
|
NS_METHOD
|
|
|
|
nsIndexedToHTML::Create(nsISupports *aOuter, REFNSIID aIID, void **aResult) {
|
|
|
|
nsresult rv;
|
|
|
|
if (aOuter)
|
|
|
|
return NS_ERROR_NO_AGGREGATION;
|
|
|
|
|
|
|
|
nsIndexedToHTML* _s = new nsIndexedToHTML();
|
|
|
|
if (_s == nsnull)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
rv = _s->QueryInterface(aIID, aResult);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2001-10-03 05:18:58 +04:00
|
|
|
nsresult
|
2001-10-03 04:43:30 +04:00
|
|
|
nsIndexedToHTML::Init(nsIStreamListener* aListener) {
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
|
|
|
mListener = aListener;
|
|
|
|
|
|
|
|
mDateTime = do_CreateInstance(kDateTimeFormatCID, &rv);
|
|
|
|
|
2001-10-19 07:03:30 +04:00
|
|
|
nsCOMPtr<nsIStringBundleService> sbs =
|
|
|
|
do_GetService(NS_STRINGBUNDLE_CONTRACTID, &rv);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
rv = sbs->CreateBundle("chrome://necko/locale/necko.properties",
|
|
|
|
getter_AddRefs(mBundle));
|
|
|
|
|
2001-12-06 03:10:11 +03:00
|
|
|
mRowCount = 0;
|
|
|
|
|
2001-10-03 04:43:30 +04:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsIndexedToHTML::Convert(nsIInputStream* aFromStream,
|
|
|
|
const PRUnichar* aFromType,
|
|
|
|
const PRUnichar* aToType,
|
|
|
|
nsISupports* aCtxt,
|
|
|
|
nsIInputStream** res) {
|
2001-03-01 10:22:19 +03:00
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsIndexedToHTML::AsyncConvertData(const PRUnichar *aFromType,
|
|
|
|
const PRUnichar *aToType,
|
|
|
|
nsIStreamListener *aListener,
|
|
|
|
nsISupports *aCtxt) {
|
2001-10-03 04:43:30 +04:00
|
|
|
return Init(aListener);
|
2001-03-01 10:22:19 +03:00
|
|
|
}
|
|
|
|
|
2002-04-10 14:28:57 +04:00
|
|
|
static NS_NAMED_LITERAL_STRING(tableHeading,"<table>\n");
|
2001-12-06 03:10:11 +03:00
|
|
|
|
2001-03-01 10:22:19 +03:00
|
|
|
NS_IMETHODIMP
|
2001-10-03 04:43:30 +04:00
|
|
|
nsIndexedToHTML::OnStartRequest(nsIRequest* request, nsISupports *aContext) {
|
|
|
|
nsresult rv;
|
|
|
|
|
2001-03-01 10:22:19 +03:00
|
|
|
nsCOMPtr<nsIChannel> channel = do_QueryInterface(request);
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
2001-10-03 04:43:30 +04:00
|
|
|
rv = channel->GetURI(getter_AddRefs(uri));
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2002-09-21 02:43:15 +04:00
|
|
|
channel->SetContentType(NS_LITERAL_CSTRING("text/html"));
|
|
|
|
|
2001-10-03 04:43:30 +04:00
|
|
|
mParser = do_CreateInstance("@mozilla.org/dirIndexParser;1",&rv);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
2001-03-01 10:22:19 +03:00
|
|
|
|
2001-10-03 04:43:30 +04:00
|
|
|
rv = mParser->SetListener(this);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
rv = mParser->OnStartRequest(request, aContext);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2002-04-17 05:45:15 +04:00
|
|
|
nsCAutoString baseUri,titleUri;
|
2002-03-06 10:48:55 +03:00
|
|
|
rv = uri->GetAsciiSpec(baseUri);
|
2001-10-03 04:43:30 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
2002-04-17 05:45:15 +04:00
|
|
|
titleUri = baseUri;
|
2001-10-23 04:56:41 +04:00
|
|
|
|
|
|
|
nsCString parentStr;
|
|
|
|
|
|
|
|
// XXX - should be using the 300: line from the parser.
|
|
|
|
// We can't guarantee that that comes before any entry, so we'd have to
|
|
|
|
// buffer, and do other painful stuff.
|
|
|
|
// I'll deal with this when I make the changes to handle welcome messages
|
|
|
|
// The .. stuff should also come from the lower level protocols, but that
|
|
|
|
// would muck up the XUL display
|
|
|
|
// - bbaetz
|
|
|
|
|
|
|
|
// ftp urls don't always end in a /
|
|
|
|
PRBool isScheme = PR_FALSE;
|
|
|
|
if (NS_SUCCEEDED(uri->SchemeIs("ftp", &isScheme)) && isScheme) {
|
2002-04-17 05:45:15 +04:00
|
|
|
// strip out the password here, so it doesn't show in the page title
|
|
|
|
// This is done by the 300: line generation in ftp, but we don't use
|
|
|
|
// that - see above
|
|
|
|
|
|
|
|
// if there was a password, strip it out
|
|
|
|
nsCAutoString pw;
|
|
|
|
rv = uri->GetPassword(pw);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
if (!pw.IsEmpty()) {
|
|
|
|
nsCOMPtr<nsIURI> newUri;
|
|
|
|
rv = uri->Clone(getter_AddRefs(newUri));
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
rv = newUri->SetPassword(NS_LITERAL_CSTRING(""));
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
rv = newUri->GetAsciiSpec(titleUri);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
if (titleUri.Last() != '/')
|
|
|
|
titleUri.Append('/');
|
|
|
|
}
|
|
|
|
|
2001-10-23 04:56:41 +04:00
|
|
|
if (baseUri.Last() != '/')
|
|
|
|
baseUri.Append('/');
|
|
|
|
|
|
|
|
nsCString::const_iterator start, finish;
|
|
|
|
baseUri.BeginReading(start);
|
|
|
|
baseUri.EndReading(finish);
|
|
|
|
finish.advance(-2); // don't count the last /
|
|
|
|
|
|
|
|
// No RFindChar
|
|
|
|
while(finish != start && *finish != '/')
|
|
|
|
--finish;
|
|
|
|
|
|
|
|
if (Distance(start, finish) > (sizeof("ftp://") - 1)) {
|
|
|
|
++finish; // include the end '/'
|
|
|
|
parentStr = Substring(start, finish);
|
|
|
|
}
|
|
|
|
|
|
|
|
} else if (NS_SUCCEEDED(uri->SchemeIs("file", &isScheme)) && isScheme) {
|
|
|
|
nsCOMPtr<nsIFileURL> fileUrl = do_QueryInterface(uri);
|
|
|
|
nsCOMPtr<nsIFile> file;
|
|
|
|
rv = fileUrl->GetFile(getter_AddRefs(file));
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2002-03-06 10:48:55 +03:00
|
|
|
nsCAutoString url;
|
2002-09-13 23:32:45 +04:00
|
|
|
rv = net_GetURLSpecFromFile(file, url);
|
2001-10-23 04:56:41 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
baseUri.Assign(url);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIFile> parent;
|
|
|
|
rv = file->GetParent(getter_AddRefs(parent));
|
2002-05-01 00:53:09 +04:00
|
|
|
|
|
|
|
if (parent && NS_SUCCEEDED(rv)) {
|
2002-09-13 23:32:45 +04:00
|
|
|
net_GetURLSpecFromFile(parent, url);
|
2001-10-23 04:56:41 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
parentStr.Assign(url);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-03-01 10:22:19 +03:00
|
|
|
nsString buffer;
|
2002-08-20 07:32:28 +04:00
|
|
|
buffer.Assign(NS_LITERAL_STRING("<?xml version=\"1.0\""));
|
|
|
|
|
|
|
|
// Get the encoding from the parser
|
|
|
|
// XXX - this won't work for any encoding set via a 301: line in the
|
|
|
|
// format - this output stuff would need to move to OnDataAvailable
|
|
|
|
// for that. However, currently only file:// sends that, and file's HTML
|
|
|
|
// index mode isn't enabled yet (bug 102812)
|
2002-04-10 14:28:57 +04:00
|
|
|
|
2002-08-20 07:32:28 +04:00
|
|
|
nsXPIDLCString encoding;
|
|
|
|
rv = mParser->GetEncoding(getter_Copies(encoding));
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
buffer.Append(NS_LITERAL_STRING(" encoding=\"") +
|
|
|
|
NS_ConvertASCIItoUCS2(encoding) +
|
|
|
|
NS_LITERAL_STRING("\""));
|
|
|
|
|
|
|
|
buffer.Append(NS_LITERAL_STRING("?>\n") +
|
2002-04-10 14:28:57 +04:00
|
|
|
NS_LITERAL_STRING("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" ") +
|
|
|
|
NS_LITERAL_STRING("\"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">\n"));
|
|
|
|
|
2001-10-03 04:43:30 +04:00
|
|
|
// Anything but a gopher url needs to end in a /,
|
|
|
|
// otherwise we end up linking to file:///foo/dirfile
|
|
|
|
|
2002-04-10 14:28:57 +04:00
|
|
|
buffer.Append(NS_LITERAL_STRING("<html xmlns=\"http://www.w3.org/1999/xhtml\">\n<head><title>"));
|
2001-10-19 07:03:30 +04:00
|
|
|
|
|
|
|
nsXPIDLString title;
|
2002-05-17 01:00:48 +04:00
|
|
|
|
|
|
|
if (!mTextToSubURI) {
|
|
|
|
mTextToSubURI = do_GetService(NS_ITEXTTOSUBURI_CONTRACTID, &rv);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
}
|
|
|
|
|
2002-06-25 11:51:05 +04:00
|
|
|
nsXPIDLString unEscapeSpec;
|
|
|
|
rv = mTextToSubURI->UnEscapeAndConvert(encoding, titleUri.get(),
|
|
|
|
getter_Copies(unEscapeSpec));
|
2002-05-17 01:00:48 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2002-06-25 11:51:05 +04:00
|
|
|
nsXPIDLString htmlEscSpec;
|
|
|
|
htmlEscSpec.Adopt(nsEscapeHTML2(unEscapeSpec.get(),
|
|
|
|
unEscapeSpec.Length()));
|
2001-10-19 07:03:30 +04:00
|
|
|
|
|
|
|
const PRUnichar* formatTitle[] = {
|
2002-06-25 11:51:05 +04:00
|
|
|
htmlEscSpec.get()
|
2001-10-19 07:03:30 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
rv = mBundle->FormatStringFromName(NS_LITERAL_STRING("DirTitle").get(),
|
2002-03-12 08:56:29 +03:00
|
|
|
formatTitle,
|
|
|
|
sizeof(formatTitle)/sizeof(PRUnichar*),
|
2001-10-19 07:03:30 +04:00
|
|
|
getter_Copies(title));
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
2002-08-20 07:32:28 +04:00
|
|
|
|
|
|
|
// we want to convert string bundle to NCR
|
|
|
|
// to ensure they're shown in any charsets
|
|
|
|
nsAutoString strNCR;
|
|
|
|
ConvertNonAsciiToNCR(title, strNCR);
|
|
|
|
buffer.Append(strNCR);
|
2001-10-19 07:03:30 +04:00
|
|
|
|
2002-06-25 11:51:05 +04:00
|
|
|
buffer.Append(NS_LITERAL_STRING("</title><base href=\""));
|
2001-10-23 04:56:41 +04:00
|
|
|
buffer.Append(NS_ConvertASCIItoUCS2(baseUri));
|
2002-04-10 14:28:57 +04:00
|
|
|
buffer.Append(NS_LITERAL_STRING("\"/>\n"));
|
|
|
|
|
|
|
|
buffer.Append(NS_LITERAL_STRING("<style type=\"text/css\">\n") +
|
|
|
|
NS_LITERAL_STRING("img { border: 0; padding: 0 2px; vertical-align: text-bottom; }\n") +
|
|
|
|
NS_LITERAL_STRING("td { font-family: monospace; padding: 2px 3px; text-align: right; vertical-align: bottom; }\n") +
|
|
|
|
NS_LITERAL_STRING("td:first-child { text-align: left; padding: 2px 10px 2px 3px; }\n") +
|
|
|
|
NS_LITERAL_STRING("table { border: 0; }\n") +
|
|
|
|
NS_LITERAL_STRING("a.symlink { font-style: italic; }\n") +
|
|
|
|
NS_LITERAL_STRING("</style>\n"));
|
|
|
|
|
2001-11-06 04:46:18 +03:00
|
|
|
buffer.Append(NS_LITERAL_STRING("</head>\n<body>\n<h1>"));
|
2001-10-19 07:03:30 +04:00
|
|
|
|
|
|
|
const PRUnichar* formatHeading[] = {
|
2002-06-25 11:51:05 +04:00
|
|
|
htmlEscSpec.get()
|
2001-10-19 07:03:30 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
rv = mBundle->FormatStringFromName(NS_LITERAL_STRING("DirTitle").get(),
|
2002-03-12 08:56:29 +03:00
|
|
|
formatHeading,
|
|
|
|
sizeof(formatHeading)/sizeof(PRUnichar*),
|
2001-10-19 07:03:30 +04:00
|
|
|
getter_Copies(title));
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2002-08-20 07:32:28 +04:00
|
|
|
ConvertNonAsciiToNCR(title, strNCR);
|
|
|
|
buffer.Append(strNCR);
|
2002-04-10 14:28:57 +04:00
|
|
|
buffer.Append(NS_LITERAL_STRING("</h1>\n<hr/>") + tableHeading);
|
2001-03-01 10:22:19 +03:00
|
|
|
|
2001-10-19 07:03:30 +04:00
|
|
|
//buffer.Append(NS_LITERAL_STRING("<tr><th>Name</th><th>Size</th><th>Last modified</th></tr>\n"));
|
2001-03-13 05:20:55 +03:00
|
|
|
|
2001-10-23 04:56:41 +04:00
|
|
|
if (!parentStr.IsEmpty()) {
|
|
|
|
nsXPIDLString parentText;
|
|
|
|
rv = mBundle->GetStringFromName(NS_LITERAL_STRING("DirGoUp").get(),
|
|
|
|
getter_Copies(parentText));
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2002-08-20 07:32:28 +04:00
|
|
|
ConvertNonAsciiToNCR(parentText, strNCR);
|
2001-10-23 04:56:41 +04:00
|
|
|
buffer.Append(NS_LITERAL_STRING("<tr><td colspan=\"3\"><a href=\"") +
|
|
|
|
NS_ConvertASCIItoUCS2(parentStr) +
|
|
|
|
NS_LITERAL_STRING("\">") +
|
2002-08-20 07:32:28 +04:00
|
|
|
strNCR +
|
2001-10-23 04:56:41 +04:00
|
|
|
NS_LITERAL_STRING("</a></td></tr>\n"));
|
|
|
|
}
|
|
|
|
|
2001-03-01 10:22:19 +03:00
|
|
|
// Push buffer to the listener now, so the initial HTML will not
|
|
|
|
// be parsed in OnDataAvailable().
|
|
|
|
|
2001-10-03 04:43:30 +04:00
|
|
|
rv = mListener->OnStartRequest(request, aContext);
|
2001-03-01 10:22:19 +03:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2002-05-17 01:00:48 +04:00
|
|
|
rv = FormatInputStream(request, aContext, buffer);
|
2001-03-01 10:22:19 +03:00
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsIndexedToHTML::OnStopRequest(nsIRequest* request, nsISupports *aContext,
|
2001-04-10 10:01:08 +04:00
|
|
|
nsresult aStatus) {
|
2001-03-01 10:22:19 +03:00
|
|
|
nsresult rv = NS_OK;
|
|
|
|
nsString buffer;
|
2002-04-10 14:28:57 +04:00
|
|
|
buffer.Assign(NS_LITERAL_STRING("</table><hr/></body></html>\n"));
|
|
|
|
|
2002-05-17 01:00:48 +04:00
|
|
|
rv = FormatInputStream(request, aContext, buffer);
|
2001-03-01 10:22:19 +03:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2001-10-03 04:43:30 +04:00
|
|
|
rv = mParser->OnStopRequest(request, aContext, aStatus);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
mParser = 0;
|
|
|
|
|
2001-04-10 10:01:08 +04:00
|
|
|
return mListener->OnStopRequest(request, aContext, aStatus);
|
2001-03-01 10:22:19 +03:00
|
|
|
}
|
|
|
|
|
2002-05-17 01:00:48 +04:00
|
|
|
nsresult
|
|
|
|
nsIndexedToHTML::FormatInputStream(nsIRequest* aRequest, nsISupports *aContext, const nsAString &aBuffer)
|
|
|
|
{
|
|
|
|
nsresult rv = NS_OK;
|
2002-08-20 07:32:28 +04:00
|
|
|
|
|
|
|
// set up unicode encoder
|
|
|
|
if (!mUnicodeEncoder) {
|
|
|
|
nsXPIDLCString encoding;
|
|
|
|
rv = mParser->GetEncoding(getter_Copies(encoding));
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
nsCOMPtr<nsICharsetConverterManager2> charsetConverterManager;
|
|
|
|
charsetConverterManager = do_GetService(NS_CHARSETCONVERTERMANAGER_CONTRACTID, &rv);
|
|
|
|
nsCOMPtr<nsIAtom> charsetAtom;
|
|
|
|
rv = charsetConverterManager->GetCharsetAtom2(encoding.get(), getter_AddRefs(charsetAtom));
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
rv = charsetConverterManager->GetUnicodeEncoder(charsetAtom,
|
|
|
|
getter_AddRefs(mUnicodeEncoder));
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
rv = mUnicodeEncoder->SetOutputErrorBehavior(nsIUnicodeEncoder::kOnError_Replace,
|
|
|
|
nsnull, (PRUnichar)'?');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// convert the data with unicode encoder
|
|
|
|
char *buffer = nsnull;
|
|
|
|
PRInt32 dstLength;
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
PRInt32 unicharLength = aBuffer.Length();
|
|
|
|
rv = mUnicodeEncoder->GetMaxLength(PromiseFlatString(aBuffer).get(),
|
|
|
|
unicharLength, &dstLength);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
buffer = (char *) nsMemory::Alloc(dstLength);
|
|
|
|
NS_ENSURE_TRUE(buffer, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
|
|
|
|
rv = mUnicodeEncoder->Convert(PromiseFlatString(aBuffer).get(), &unicharLength,
|
|
|
|
buffer, &dstLength);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
PRInt32 finLen = 0;
|
|
|
|
rv = mUnicodeEncoder->Finish(buffer + dstLength, &finLen);
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
dstLength += finLen;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// if conversion error then fallback to UTF-8
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
rv = NS_OK;
|
|
|
|
if (buffer) {
|
|
|
|
nsMemory::Free(buffer);
|
|
|
|
buffer = nsnull;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-05-17 01:00:48 +04:00
|
|
|
nsCOMPtr<nsIInputStream> inputData;
|
2002-08-20 07:32:28 +04:00
|
|
|
if (buffer) {
|
2002-08-24 04:48:58 +04:00
|
|
|
rv = NS_NewCStringInputStream(getter_AddRefs(inputData), Substring(buffer, buffer + dstLength));
|
2002-08-20 07:32:28 +04:00
|
|
|
nsMemory::Free(buffer);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
rv = mListener->OnDataAvailable(aRequest, aContext,
|
|
|
|
inputData, 0, dstLength);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
NS_ConvertUCS2toUTF8 utf8Buffer(aBuffer);
|
|
|
|
rv = NS_NewCStringInputStream(getter_AddRefs(inputData), utf8Buffer);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
rv = mListener->OnDataAvailable(aRequest, aContext,
|
|
|
|
inputData, 0, utf8Buffer.Length());
|
|
|
|
}
|
2002-05-17 01:00:48 +04:00
|
|
|
return (rv);
|
|
|
|
}
|
|
|
|
|
2001-10-03 04:43:30 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsIndexedToHTML::OnDataAvailable(nsIRequest *aRequest,
|
|
|
|
nsISupports *aCtxt,
|
|
|
|
nsIInputStream* aInput,
|
|
|
|
PRUint32 aOffset,
|
|
|
|
PRUint32 aCount) {
|
|
|
|
return mParser->OnDataAvailable(aRequest, aCtxt, aInput, aOffset, aCount);
|
|
|
|
}
|
2001-03-01 10:22:19 +03:00
|
|
|
|
2001-12-06 03:10:11 +03:00
|
|
|
// This defines the number of rows we are going to have per table
|
|
|
|
// splitting this up makes things faster, by helping layout
|
|
|
|
#define ROWS_PER_TABLE 250
|
|
|
|
|
2001-10-03 04:43:30 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsIndexedToHTML::OnIndexAvailable(nsIRequest *aRequest,
|
|
|
|
nsISupports *aCtxt,
|
|
|
|
nsIDirIndex *aIndex) {
|
2002-09-04 01:51:24 +04:00
|
|
|
nsresult rv;
|
2001-10-03 04:43:30 +04:00
|
|
|
if (!aIndex)
|
2001-03-01 10:22:19 +03:00
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
|
2001-10-03 04:43:30 +04:00
|
|
|
nsString pushBuffer;
|
2002-04-10 14:28:57 +04:00
|
|
|
pushBuffer.Append(NS_LITERAL_STRING("<tr>\n <td><a"));
|
|
|
|
|
|
|
|
PRUint32 type;
|
|
|
|
aIndex->GetType(&type);
|
|
|
|
if (type == nsIDirIndex::TYPE_SYMLINK) {
|
|
|
|
pushBuffer.Append(NS_LITERAL_STRING(" class=\"symlink\""));
|
|
|
|
}
|
|
|
|
|
|
|
|
pushBuffer.Append(NS_LITERAL_STRING(" href=\""));
|
2001-10-03 04:43:30 +04:00
|
|
|
|
|
|
|
nsXPIDLCString loc;
|
|
|
|
aIndex->GetLocation(getter_Copies(loc));
|
2001-03-01 10:22:19 +03:00
|
|
|
|
2002-09-04 01:51:24 +04:00
|
|
|
if (!mTextToSubURI) {
|
|
|
|
mTextToSubURI = do_GetService(NS_ITEXTTOSUBURI_CONTRACTID, &rv);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsXPIDLCString encoding;
|
|
|
|
rv = mParser->GetEncoding(getter_Copies(encoding));
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
nsXPIDLString unEscapeSpec;
|
|
|
|
rv = mTextToSubURI->UnEscapeAndConvert(encoding, loc,
|
|
|
|
getter_Copies(unEscapeSpec));
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
pushBuffer.Append(unEscapeSpec);
|
2001-10-03 04:43:30 +04:00
|
|
|
|
2002-04-10 14:28:57 +04:00
|
|
|
pushBuffer.Append(NS_LITERAL_STRING("\"><img src=\""));
|
2001-10-03 04:43:30 +04:00
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case nsIDirIndex::TYPE_DIRECTORY:
|
|
|
|
case nsIDirIndex::TYPE_SYMLINK:
|
2002-04-10 14:28:57 +04:00
|
|
|
pushBuffer.Append(NS_LITERAL_STRING("internal-gopher-menu\" alt=\"Directory: "));
|
2001-10-03 04:43:30 +04:00
|
|
|
break;
|
|
|
|
case nsIDirIndex::TYPE_FILE:
|
|
|
|
case nsIDirIndex::TYPE_UNKNOWN:
|
2002-04-10 14:28:57 +04:00
|
|
|
pushBuffer.Append(NS_LITERAL_STRING("internal-gopher-unknown\" alt=\"File: "));
|
2001-10-03 04:43:30 +04:00
|
|
|
break;
|
2001-04-27 05:51:28 +04:00
|
|
|
}
|
2002-04-10 14:28:57 +04:00
|
|
|
pushBuffer.Append(NS_LITERAL_STRING("\"/>"));
|
2001-04-27 05:51:28 +04:00
|
|
|
|
2001-10-03 04:43:30 +04:00
|
|
|
nsXPIDLString tmp;
|
|
|
|
aIndex->GetDescription(getter_Copies(tmp));
|
|
|
|
PRUnichar* escaped = nsEscapeHTML2(tmp.get(), tmp.Length());
|
|
|
|
pushBuffer.Append(escaped);
|
|
|
|
nsMemory::Free(escaped);
|
2001-03-01 10:22:19 +03:00
|
|
|
|
2002-04-10 14:28:57 +04:00
|
|
|
pushBuffer.Append(NS_LITERAL_STRING("</a></td>\n <td>"));
|
2001-03-01 10:22:19 +03:00
|
|
|
|
2001-10-03 04:43:30 +04:00
|
|
|
PRUint32 size;
|
|
|
|
aIndex->GetSize(&size);
|
|
|
|
|
2001-10-19 07:03:30 +04:00
|
|
|
if (size != PRUint32(-1) &&
|
|
|
|
type != nsIDirIndex::TYPE_DIRECTORY &&
|
|
|
|
type != nsIDirIndex::TYPE_SYMLINK) {
|
2001-11-06 04:46:18 +03:00
|
|
|
nsAutoString sizeString;
|
|
|
|
FormatSizeString(size, sizeString);
|
|
|
|
pushBuffer.Append(sizeString);
|
2001-03-01 10:22:19 +03:00
|
|
|
}
|
|
|
|
|
2002-04-10 14:28:57 +04:00
|
|
|
pushBuffer.Append(NS_LITERAL_STRING("</td>\n <td>"));
|
2001-03-01 10:22:19 +03:00
|
|
|
|
2001-10-03 04:43:30 +04:00
|
|
|
PRTime t;
|
|
|
|
aIndex->GetLastModified(&t);
|
2001-03-01 10:22:19 +03:00
|
|
|
|
2001-10-03 04:43:30 +04:00
|
|
|
if (t == -1) {
|
2002-04-10 14:28:57 +04:00
|
|
|
pushBuffer.Append(NS_LITERAL_STRING("</td>\n <td>"));
|
2001-10-03 04:43:30 +04:00
|
|
|
} else {
|
|
|
|
nsAutoString formatted;
|
2001-11-06 04:46:18 +03:00
|
|
|
mDateTime->FormatPRTime(nsnull,
|
2001-10-03 04:43:30 +04:00
|
|
|
kDateFormatShort,
|
2001-11-06 04:46:18 +03:00
|
|
|
kTimeFormatNone,
|
|
|
|
t,
|
|
|
|
formatted);
|
|
|
|
pushBuffer.Append(formatted);
|
2002-04-10 14:28:57 +04:00
|
|
|
pushBuffer.Append(NS_LITERAL_STRING("</td>\n <td>"));
|
2001-11-06 04:46:18 +03:00
|
|
|
mDateTime->FormatPRTime(nsnull,
|
|
|
|
kDateFormatNone,
|
2001-10-03 04:43:30 +04:00
|
|
|
kTimeFormatSeconds,
|
|
|
|
t,
|
|
|
|
formatted);
|
|
|
|
pushBuffer.Append(formatted);
|
2001-03-01 10:22:19 +03:00
|
|
|
}
|
|
|
|
|
2002-04-10 14:28:57 +04:00
|
|
|
pushBuffer.Append(NS_LITERAL_STRING("</td>\n</tr>\n"));
|
2001-12-06 03:10:11 +03:00
|
|
|
|
|
|
|
// Split this up to avoid slow layout performance with large tables
|
|
|
|
// - bug 85381
|
|
|
|
if (++mRowCount > ROWS_PER_TABLE) {
|
|
|
|
pushBuffer.Append(NS_LITERAL_STRING("</table>\n") + tableHeading);
|
|
|
|
mRowCount = 0;
|
|
|
|
}
|
2001-10-03 04:43:30 +04:00
|
|
|
|
2002-05-17 01:00:48 +04:00
|
|
|
return FormatInputStream(aRequest, aCtxt, pushBuffer);
|
2001-10-03 04:43:30 +04:00
|
|
|
}
|
2001-03-01 10:22:19 +03:00
|
|
|
|
2001-11-06 04:46:18 +03:00
|
|
|
void nsIndexedToHTML::FormatSizeString(PRUint32 inSize, nsString& outSizeString)
|
|
|
|
{
|
|
|
|
outSizeString.Truncate();
|
2002-04-10 14:28:57 +04:00
|
|
|
if (inSize > 0) {
|
2001-11-06 04:46:18 +03:00
|
|
|
// round up to the nearest Kilobyte
|
|
|
|
PRUint32 upperSize = (inSize + 1023) / 1024;
|
|
|
|
outSizeString.AppendInt(upperSize);
|
|
|
|
outSizeString.Append(NS_LITERAL_STRING(" KB"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-03-01 10:22:19 +03:00
|
|
|
nsIndexedToHTML::nsIndexedToHTML() {
|
2001-11-06 04:46:18 +03:00
|
|
|
NS_INIT_ISUPPORTS();
|
2001-03-01 10:22:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsIndexedToHTML::~nsIndexedToHTML() {
|
|
|
|
}
|