2008-01-10 01:24:07 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set sw=2 sts=2 et cin: */
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
2004-04-17 20:52:41 +04:00
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla 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/MPL/
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
2000-06-26 02:42:16 +04:00
|
|
|
* The Original Code is the Mozilla browser.
|
2004-04-17 20:52:41 +04:00
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Netscape Communications, Inc.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 1999
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
2000-06-26 02:42:16 +04:00
|
|
|
* Contributor(s):
|
|
|
|
* Scott MacGregor <mscott@netscape.com>
|
2002-05-16 18:32:19 +04:00
|
|
|
* Boris Zbarsky <bzbarsky@mit.edu> (Added mailcap and mime.types support)
|
2004-11-22 22:28:24 +03:00
|
|
|
* Peter Weilbacher <mozilla@Weilbacher.org>
|
2008-01-10 01:24:07 +03:00
|
|
|
* Rich Walsh <dragtext@e-vertise.com>
|
2008-02-20 07:46:36 +03:00
|
|
|
* Dan Mosedale <dmose@mozilla.org>
|
2004-04-17 20:52:41 +04:00
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either of 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 MPL, 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 MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
2000-06-26 02:42:16 +04:00
|
|
|
|
|
|
|
#include "nsOSHelperAppService.h"
|
|
|
|
#include "nsISupports.h"
|
|
|
|
#include "nsString.h"
|
2001-09-29 12:28:41 +04:00
|
|
|
#include "nsReadableUtils.h"
|
2002-05-16 18:32:19 +04:00
|
|
|
#include "nsUnicharUtils.h"
|
2000-06-26 02:42:16 +04:00
|
|
|
#include "nsXPIDLString.h"
|
|
|
|
#include "nsIURL.h"
|
2002-05-16 18:32:19 +04:00
|
|
|
#include "nsIFileStreams.h"
|
|
|
|
#include "nsILineInputStream.h"
|
2000-06-26 02:42:16 +04:00
|
|
|
#include "nsILocalFile.h"
|
2002-05-16 18:32:19 +04:00
|
|
|
#include "nsIProcess.h"
|
2003-01-12 07:19:02 +03:00
|
|
|
#include "nsXPCOM.h"
|
|
|
|
#include "nsISupportsPrimitives.h"
|
2002-05-16 18:32:19 +04:00
|
|
|
#include "nsHashtable.h"
|
|
|
|
#include "nsCRT.h"
|
|
|
|
#include "prenv.h" // for PR_GetEnv()
|
2004-02-24 01:49:28 +03:00
|
|
|
#include "nsMIMEInfoOS2.h"
|
|
|
|
#include "nsAutoPtr.h"
|
2008-01-10 01:24:07 +03:00
|
|
|
#include "nsIRwsService.h"
|
|
|
|
#include "nsIStringBundle.h"
|
|
|
|
#include "nsLocalHandlerApp.h"
|
2007-08-03 23:06:33 +04:00
|
|
|
#include <stdlib.h> // for system()
|
2003-01-12 07:19:02 +03:00
|
|
|
|
2008-01-10 01:24:07 +03:00
|
|
|
//------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// reduces overhead by preventing calls to nsRws when it isn't present
|
|
|
|
static PRBool sUseRws = PR_TRUE;
|
|
|
|
|
2003-01-12 07:19:02 +03:00
|
|
|
static nsresult
|
2002-05-16 18:32:19 +04:00
|
|
|
FindSemicolon(nsAString::const_iterator& aSemicolon_iter,
|
|
|
|
const nsAString::const_iterator& aEnd_iter);
|
2003-01-12 07:19:02 +03:00
|
|
|
static nsresult
|
2002-05-16 18:32:19 +04:00
|
|
|
ParseMIMEType(const nsAString::const_iterator& aStart_iter,
|
|
|
|
nsAString::const_iterator& aMajorTypeStart,
|
|
|
|
nsAString::const_iterator& aMajorTypeEnd,
|
|
|
|
nsAString::const_iterator& aMinorTypeStart,
|
|
|
|
nsAString::const_iterator& aMinorTypeEnd,
|
|
|
|
const nsAString::const_iterator& aEnd_iter);
|
2000-06-26 02:42:16 +04:00
|
|
|
|
2002-05-16 18:32:19 +04:00
|
|
|
inline PRBool
|
2004-04-22 19:32:54 +04:00
|
|
|
IsNetscapeFormat(const nsACString& aBuffer);
|
2000-06-26 02:42:16 +04:00
|
|
|
|
2008-01-10 01:24:07 +03:00
|
|
|
//------------------------------------------------------------------------
|
|
|
|
|
2002-05-16 18:32:19 +04:00
|
|
|
nsOSHelperAppService::nsOSHelperAppService() : nsExternalHelperAppService()
|
|
|
|
{
|
2001-01-16 03:09:58 +03:00
|
|
|
}
|
|
|
|
|
2002-05-16 18:32:19 +04:00
|
|
|
nsOSHelperAppService::~nsOSHelperAppService()
|
|
|
|
{}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Take a command with all the mailcap escapes in it and unescape it
|
|
|
|
* Ideally this needs the mime type, mime type options, and location of the
|
|
|
|
* temporary file, but this last can't be got from here
|
|
|
|
*/
|
2003-07-25 20:48:00 +04:00
|
|
|
// static
|
|
|
|
nsresult
|
|
|
|
nsOSHelperAppService::UnescapeCommand(const nsAString& aEscapedCommand,
|
|
|
|
const nsAString& aMajorType,
|
|
|
|
const nsAString& aMinorType,
|
|
|
|
nsHashtable& aTypeOptions,
|
|
|
|
nsACString& aUnEscapedCommand) {
|
2003-01-12 07:19:02 +03:00
|
|
|
LOG(("-- UnescapeCommand"));
|
|
|
|
LOG(("Command to escape: '%s'\n",
|
2006-02-03 17:18:39 +03:00
|
|
|
NS_LossyConvertUTF16toASCII(aEscapedCommand).get()));
|
2002-05-16 18:32:19 +04:00
|
|
|
// XXX This function will need to get the mime type and various stuff like that being passed in to work properly
|
|
|
|
|
2003-01-12 07:19:02 +03:00
|
|
|
LOG(("UnescapeCommand really needs some work -- it should actually do some unescaping\n"));
|
|
|
|
|
2003-12-23 19:48:40 +03:00
|
|
|
CopyUTF16toUTF8(aEscapedCommand, aUnEscapedCommand);
|
2003-01-12 07:19:02 +03:00
|
|
|
LOG(("Escaped command: '%s'\n",
|
|
|
|
PromiseFlatCString(aUnEscapedCommand).get()));
|
2002-05-16 18:32:19 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Put aSemicolon_iter at the first non-escaped semicolon after
|
|
|
|
* aStart_iter but before aEnd_iter
|
|
|
|
*/
|
2001-01-16 03:09:58 +03:00
|
|
|
|
2003-01-12 07:19:02 +03:00
|
|
|
static nsresult
|
2002-05-16 18:32:19 +04:00
|
|
|
FindSemicolon(nsAString::const_iterator& aSemicolon_iter,
|
|
|
|
const nsAString::const_iterator& aEnd_iter) {
|
|
|
|
PRBool semicolonFound = PR_FALSE;
|
|
|
|
while (aSemicolon_iter != aEnd_iter && !semicolonFound) {
|
|
|
|
switch(*aSemicolon_iter) {
|
|
|
|
case '\\':
|
|
|
|
aSemicolon_iter.advance(2);
|
|
|
|
break;
|
|
|
|
case ';':
|
|
|
|
semicolonFound = PR_TRUE;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
++aSemicolon_iter;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2003-01-12 07:19:02 +03:00
|
|
|
static nsresult
|
2002-05-16 18:32:19 +04:00
|
|
|
ParseMIMEType(const nsAString::const_iterator& aStart_iter,
|
|
|
|
nsAString::const_iterator& aMajorTypeStart,
|
|
|
|
nsAString::const_iterator& aMajorTypeEnd,
|
|
|
|
nsAString::const_iterator& aMinorTypeStart,
|
|
|
|
nsAString::const_iterator& aMinorTypeEnd,
|
|
|
|
const nsAString::const_iterator& aEnd_iter) {
|
|
|
|
nsAString::const_iterator iter(aStart_iter);
|
|
|
|
|
|
|
|
// skip leading whitespace
|
|
|
|
while (iter != aEnd_iter && nsCRT::IsAsciiSpace(*iter)) {
|
|
|
|
++iter;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (iter == aEnd_iter) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
aMajorTypeStart = iter;
|
|
|
|
|
|
|
|
// find major/minor separator ('/')
|
|
|
|
while (iter != aEnd_iter && *iter != '/') {
|
|
|
|
++iter;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (iter == aEnd_iter) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
aMajorTypeEnd = iter;
|
|
|
|
|
|
|
|
// skip '/'
|
|
|
|
++iter;
|
|
|
|
|
|
|
|
if (iter == aEnd_iter) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
aMinorTypeStart = iter;
|
|
|
|
|
|
|
|
// find end of minor type, delimited by whitespace or ';'
|
|
|
|
while (iter != aEnd_iter && !nsCRT::IsAsciiSpace(*iter) && *iter != ';') {
|
|
|
|
++iter;
|
|
|
|
}
|
|
|
|
|
|
|
|
aMinorTypeEnd = iter;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2003-07-25 20:48:00 +04:00
|
|
|
// static
|
|
|
|
nsresult
|
|
|
|
nsOSHelperAppService::GetFileLocation(const char* aPrefName,
|
|
|
|
const char* aEnvVarName,
|
|
|
|
PRUnichar** aFileLocation) {
|
2003-01-12 07:19:02 +03:00
|
|
|
LOG(("-- GetFileLocation. Pref: '%s' EnvVar: '%s'\n",
|
|
|
|
aPrefName,
|
|
|
|
aEnvVarName));
|
|
|
|
NS_PRECONDITION(aPrefName, "Null pref name passed; don't do that!");
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
*aFileLocation = nsnull;
|
|
|
|
/* The lookup order is:
|
|
|
|
1) user pref
|
|
|
|
2) env var
|
|
|
|
3) pref
|
|
|
|
*/
|
|
|
|
nsCOMPtr<nsIPrefService> prefService(do_GetService(NS_PREFSERVICE_CONTRACTID, &rv));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
nsCOMPtr<nsIPrefBranch> prefBranch;
|
|
|
|
rv = prefService->GetBranch(nsnull, getter_AddRefs(prefBranch));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
/*
|
|
|
|
If we have an env var we should check whether the pref is a user
|
|
|
|
pref. If we do not, we don't care.
|
|
|
|
*/
|
|
|
|
nsCOMPtr<nsISupportsString> prefFileName;
|
|
|
|
PRBool isUserPref = PR_FALSE;
|
|
|
|
prefBranch->PrefHasUserValue(aPrefName, &isUserPref);
|
|
|
|
if (isUserPref) {
|
|
|
|
rv = prefBranch->GetComplexValue(aPrefName,
|
|
|
|
NS_GET_IID(nsISupportsString),
|
|
|
|
getter_AddRefs(prefFileName));
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
return prefFileName->ToString(aFileLocation);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aEnvVarName && *aEnvVarName) {
|
|
|
|
char* prefValue = PR_GetEnv(aEnvVarName);
|
|
|
|
if (prefValue && *prefValue) {
|
|
|
|
// the pref is in the system charset and it's a filepath... The
|
|
|
|
// natural way to do the charset conversion is by just initing
|
|
|
|
// an nsIFile with the native path and asking it for the Unicode
|
|
|
|
// version.
|
|
|
|
nsCOMPtr<nsILocalFile> file(do_CreateInstance(NS_LOCAL_FILE_CONTRACTID, &rv));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = file->InitWithNativePath(nsDependentCString(prefValue));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsAutoString unicodePath;
|
|
|
|
rv = file->GetPath(unicodePath);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
*aFileLocation = ToNewUnicode(unicodePath);
|
|
|
|
if (!*aFileLocation)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = prefBranch->GetComplexValue(aPrefName,
|
|
|
|
NS_GET_IID(nsISupportsString),
|
|
|
|
getter_AddRefs(prefFileName));
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
return prefFileName->ToString(aFileLocation);
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-05-16 18:32:19 +04:00
|
|
|
/* Get the mime.types file names from prefs and look up info in them
|
|
|
|
based on extension */
|
2003-07-25 20:48:00 +04:00
|
|
|
// static
|
|
|
|
nsresult
|
|
|
|
nsOSHelperAppService::LookUpTypeAndDescription(const nsAString& aFileExtension,
|
2004-02-24 01:49:28 +03:00
|
|
|
nsAString& aMajorType,
|
|
|
|
nsAString& aMinorType,
|
|
|
|
nsAString& aDescription) {
|
2003-01-12 07:19:02 +03:00
|
|
|
LOG(("-- LookUpTypeAndDescription for extension '%s'\n",
|
2006-02-03 17:18:39 +03:00
|
|
|
NS_LossyConvertUTF16toASCII(aFileExtension).get()));
|
2002-05-16 18:32:19 +04:00
|
|
|
nsresult rv = NS_OK;
|
|
|
|
nsXPIDLString mimeFileName;
|
|
|
|
|
2003-01-12 07:19:02 +03:00
|
|
|
rv = GetFileLocation("helpers.private_mime_types_file",
|
|
|
|
nsnull,
|
|
|
|
getter_Copies(mimeFileName));
|
|
|
|
if (NS_SUCCEEDED(rv) && !mimeFileName.IsEmpty()) {
|
|
|
|
rv = GetTypeAndDescriptionFromMimetypesFile(mimeFileName,
|
|
|
|
aFileExtension,
|
|
|
|
aMajorType,
|
|
|
|
aMinorType,
|
|
|
|
aDescription);
|
|
|
|
} else {
|
|
|
|
rv = NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
if (NS_FAILED(rv) || aMajorType.IsEmpty()) {
|
|
|
|
rv = GetFileLocation("helpers.global_mime_types_file",
|
|
|
|
nsnull,
|
|
|
|
getter_Copies(mimeFileName));
|
2002-05-16 18:32:19 +04:00
|
|
|
if (NS_SUCCEEDED(rv) && !mimeFileName.IsEmpty()) {
|
|
|
|
rv = GetTypeAndDescriptionFromMimetypesFile(mimeFileName,
|
|
|
|
aFileExtension,
|
|
|
|
aMajorType,
|
|
|
|
aMinorType,
|
|
|
|
aDescription);
|
2003-01-12 07:19:02 +03:00
|
|
|
} else {
|
|
|
|
rv = NS_ERROR_NOT_AVAILABLE;
|
2002-05-16 18:32:19 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline PRBool
|
2004-04-22 19:32:54 +04:00
|
|
|
IsNetscapeFormat(const nsACString& aBuffer) {
|
|
|
|
return StringBeginsWith(aBuffer, NS_LITERAL_CSTRING("#--Netscape Communications Corporation MIME Information")) ||
|
|
|
|
StringBeginsWith(aBuffer, NS_LITERAL_CSTRING("#--MCOM MIME Information"));
|
2002-05-16 18:32:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create a file stream and line input stream for the filename.
|
|
|
|
* Leaves the first line of the file in aBuffer and sets the format to
|
|
|
|
* PR_TRUE for netscape files and false for normail ones
|
|
|
|
*/
|
2003-07-25 20:48:00 +04:00
|
|
|
// static
|
|
|
|
nsresult
|
|
|
|
nsOSHelperAppService::CreateInputStream(const nsAString& aFilename,
|
|
|
|
nsIFileInputStream ** aFileInputStream,
|
|
|
|
nsILineInputStream ** aLineInputStream,
|
2004-04-22 19:32:54 +04:00
|
|
|
nsACString& aBuffer,
|
2003-07-25 20:48:00 +04:00
|
|
|
PRBool * aNetscapeFormat,
|
|
|
|
PRBool * aMore) {
|
2003-01-12 07:19:02 +03:00
|
|
|
LOG(("-- CreateInputStream"));
|
2002-05-16 18:32:19 +04:00
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
|
|
|
nsCOMPtr<nsILocalFile> file(do_CreateInstance(NS_LOCAL_FILE_CONTRACTID, &rv));
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
rv = file->InitWithPath(aFilename);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIFileInputStream> fileStream(do_CreateInstance(NS_LOCALFILEINPUTSTREAM_CONTRACTID, &rv));
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
rv = fileStream->Init(file, -1, -1, PR_FALSE);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
nsCOMPtr<nsILineInputStream> lineStream(do_QueryInterface(fileStream, &rv));
|
|
|
|
|
|
|
|
if (NS_FAILED(rv)) {
|
2003-01-12 07:19:02 +03:00
|
|
|
LOG(("Interface trouble in stream land!"));
|
2002-05-16 18:32:19 +04:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = lineStream->ReadLine(aBuffer, aMore);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
fileStream->Close();
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
*aNetscapeFormat = IsNetscapeFormat(aBuffer);
|
|
|
|
|
|
|
|
*aFileInputStream = fileStream;
|
|
|
|
NS_ADDREF(*aFileInputStream);
|
|
|
|
*aLineInputStream = lineStream;
|
|
|
|
NS_ADDREF(*aLineInputStream);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Open the file, read the first line, decide what type of file it is,
|
|
|
|
then get info based on extension */
|
2003-07-25 20:48:00 +04:00
|
|
|
// static
|
|
|
|
nsresult
|
|
|
|
nsOSHelperAppService::GetTypeAndDescriptionFromMimetypesFile(const nsAString& aFilename,
|
|
|
|
const nsAString& aFileExtension,
|
|
|
|
nsAString& aMajorType,
|
|
|
|
nsAString& aMinorType,
|
|
|
|
nsAString& aDescription) {
|
2003-01-12 07:19:02 +03:00
|
|
|
LOG(("-- GetTypeAndDescriptionFromMimetypesFile\n"));
|
|
|
|
LOG(("Getting type and description from types file '%s'\n",
|
2006-02-03 17:18:39 +03:00
|
|
|
NS_LossyConvertUTF16toASCII(aFilename).get()));
|
2003-01-12 07:19:02 +03:00
|
|
|
LOG(("Using extension '%s'\n",
|
2006-02-03 17:18:39 +03:00
|
|
|
NS_LossyConvertUTF16toASCII(aFileExtension).get()));
|
2002-05-16 18:32:19 +04:00
|
|
|
nsresult rv = NS_OK;
|
|
|
|
nsCOMPtr<nsIFileInputStream> mimeFile;
|
|
|
|
nsCOMPtr<nsILineInputStream> mimeTypes;
|
|
|
|
PRBool netscapeFormat;
|
|
|
|
nsAutoString buf;
|
2004-04-22 19:32:54 +04:00
|
|
|
nsCAutoString cBuf;
|
2002-05-16 18:32:19 +04:00
|
|
|
PRBool more = PR_FALSE;
|
|
|
|
rv = CreateInputStream(aFilename, getter_AddRefs(mimeFile), getter_AddRefs(mimeTypes),
|
2004-04-22 19:32:54 +04:00
|
|
|
cBuf, &netscapeFormat, &more);
|
2002-05-16 18:32:19 +04:00
|
|
|
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2004-04-22 19:32:54 +04:00
|
|
|
|
2002-05-16 18:32:19 +04:00
|
|
|
nsAutoString extensions;
|
|
|
|
nsString entry;
|
|
|
|
entry.SetCapacity(100);
|
|
|
|
nsAString::const_iterator majorTypeStart, majorTypeEnd,
|
|
|
|
minorTypeStart, minorTypeEnd,
|
|
|
|
descriptionStart, descriptionEnd;
|
|
|
|
|
|
|
|
do {
|
2004-04-22 19:32:54 +04:00
|
|
|
CopyASCIItoUTF16(cBuf, buf);
|
2002-05-16 18:32:19 +04:00
|
|
|
// read through, building up an entry. If we finish an entry, check for
|
|
|
|
// a match and return out of the loop if we match
|
|
|
|
|
|
|
|
// skip comments and empty lines
|
|
|
|
if (!buf.IsEmpty() && buf.First() != '#') {
|
|
|
|
entry.Append(buf);
|
|
|
|
if (entry.Last() == '\\') {
|
|
|
|
entry.Truncate(entry.Length() - 1);
|
|
|
|
entry.Append(PRUnichar(' ')); // in case there is no trailing whitespace on this line
|
|
|
|
} else { // we have a full entry
|
2003-01-12 07:19:02 +03:00
|
|
|
LOG(("Current entry: '%s'\n",
|
2006-02-03 17:18:39 +03:00
|
|
|
NS_LossyConvertUTF16toASCII(entry).get()));
|
2002-05-16 18:32:19 +04:00
|
|
|
if (netscapeFormat) {
|
|
|
|
rv = ParseNetscapeMIMETypesEntry(entry,
|
|
|
|
majorTypeStart, majorTypeEnd,
|
|
|
|
minorTypeStart, minorTypeEnd,
|
|
|
|
extensions,
|
|
|
|
descriptionStart, descriptionEnd);
|
|
|
|
if (NS_FAILED(rv)) {
|
2003-01-12 07:19:02 +03:00
|
|
|
// We sometimes get things like RealPlayer appending
|
|
|
|
// "normal" entries to "Netscape" .mime.types files. Try
|
|
|
|
// to handle that. Bug 106381.
|
|
|
|
LOG(("Bogus entry; trying 'normal' mode\n"));
|
2002-05-16 18:32:19 +04:00
|
|
|
rv = ParseNormalMIMETypesEntry(entry,
|
|
|
|
majorTypeStart, majorTypeEnd,
|
|
|
|
minorTypeStart, minorTypeEnd,
|
|
|
|
extensions,
|
|
|
|
descriptionStart, descriptionEnd);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
rv = ParseNormalMIMETypesEntry(entry,
|
|
|
|
majorTypeStart, majorTypeEnd,
|
|
|
|
minorTypeStart, minorTypeEnd,
|
|
|
|
extensions,
|
|
|
|
descriptionStart, descriptionEnd);
|
2003-01-12 07:19:02 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
// We sometimes get things like StarOffice prepending
|
|
|
|
// "normal" entries to "Netscape" .mime.types files. Try
|
|
|
|
// to handle that. Bug 136670.
|
|
|
|
LOG(("Bogus entry; trying 'Netscape' mode\n"));
|
|
|
|
rv = ParseNetscapeMIMETypesEntry(entry,
|
|
|
|
majorTypeStart, majorTypeEnd,
|
|
|
|
minorTypeStart, minorTypeEnd,
|
|
|
|
extensions,
|
|
|
|
descriptionStart, descriptionEnd);
|
|
|
|
}
|
2002-05-16 18:32:19 +04:00
|
|
|
}
|
2003-01-12 07:19:02 +03:00
|
|
|
|
2002-05-16 18:32:19 +04:00
|
|
|
if (NS_SUCCEEDED(rv)) { // entry parses
|
|
|
|
nsAString::const_iterator start, end;
|
|
|
|
extensions.BeginReading(start);
|
|
|
|
extensions.EndReading(end);
|
|
|
|
nsAString::const_iterator iter(start);
|
|
|
|
|
|
|
|
while (start != end) {
|
|
|
|
FindCharInReadable(',', iter, end);
|
|
|
|
if (Substring(start, iter).Equals(aFileExtension,
|
|
|
|
nsCaseInsensitiveStringComparator())) {
|
|
|
|
// it's a match. Assign the type and description and run
|
|
|
|
aMajorType.Assign(Substring(majorTypeStart, majorTypeEnd));
|
|
|
|
aMinorType.Assign(Substring(minorTypeStart, minorTypeEnd));
|
|
|
|
aDescription.Assign(Substring(descriptionStart, descriptionEnd));
|
|
|
|
mimeFile->Close();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
if (iter != end) {
|
|
|
|
++iter;
|
|
|
|
}
|
|
|
|
start = iter;
|
|
|
|
}
|
2003-01-12 07:19:02 +03:00
|
|
|
} else {
|
2006-02-03 17:18:39 +03:00
|
|
|
LOG(("Failed to parse entry: %s\n", NS_LossyConvertUTF16toASCII(entry).get()));
|
2002-05-16 18:32:19 +04:00
|
|
|
}
|
|
|
|
// truncate the entry for the next iteration
|
|
|
|
entry.Truncate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!more) {
|
2003-01-12 07:19:02 +03:00
|
|
|
rv = NS_ERROR_NOT_AVAILABLE;
|
2002-05-16 18:32:19 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
// read the next line
|
2004-04-22 19:32:54 +04:00
|
|
|
rv = mimeTypes->ReadLine(cBuf, &more);
|
2002-05-16 18:32:19 +04:00
|
|
|
} while (NS_SUCCEEDED(rv));
|
|
|
|
|
|
|
|
mimeFile->Close();
|
|
|
|
return rv;
|
2001-01-16 03:09:58 +03:00
|
|
|
}
|
|
|
|
|
2002-05-16 18:32:19 +04:00
|
|
|
/* Get the mime.types file names from prefs and look up info in them
|
|
|
|
based on mimetype */
|
2003-07-25 20:48:00 +04:00
|
|
|
// static
|
|
|
|
nsresult
|
|
|
|
nsOSHelperAppService::LookUpExtensionsAndDescription(const nsAString& aMajorType,
|
|
|
|
const nsAString& aMinorType,
|
|
|
|
nsAString& aFileExtensions,
|
|
|
|
nsAString& aDescription) {
|
2003-01-12 07:19:02 +03:00
|
|
|
LOG(("-- LookUpExtensionsAndDescription for type '%s/%s'\n",
|
2006-02-03 17:18:39 +03:00
|
|
|
NS_LossyConvertUTF16toASCII(aMajorType).get(),
|
|
|
|
NS_LossyConvertUTF16toASCII(aMinorType).get()));
|
2002-05-16 18:32:19 +04:00
|
|
|
nsresult rv = NS_OK;
|
|
|
|
nsXPIDLString mimeFileName;
|
|
|
|
|
2003-01-12 07:19:02 +03:00
|
|
|
rv = GetFileLocation("helpers.private_mime_types_file",
|
|
|
|
nsnull,
|
|
|
|
getter_Copies(mimeFileName));
|
|
|
|
if (NS_SUCCEEDED(rv) && !mimeFileName.IsEmpty()) {
|
|
|
|
rv = GetExtensionsAndDescriptionFromMimetypesFile(mimeFileName,
|
|
|
|
aMajorType,
|
|
|
|
aMinorType,
|
|
|
|
aFileExtensions,
|
|
|
|
aDescription);
|
|
|
|
} else {
|
|
|
|
rv = NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
if (NS_FAILED(rv) || aFileExtensions.IsEmpty()) {
|
|
|
|
rv = GetFileLocation("helpers.global_mime_types_file",
|
|
|
|
nsnull,
|
|
|
|
getter_Copies(mimeFileName));
|
2002-05-16 18:32:19 +04:00
|
|
|
if (NS_SUCCEEDED(rv) && !mimeFileName.IsEmpty()) {
|
|
|
|
rv = GetExtensionsAndDescriptionFromMimetypesFile(mimeFileName,
|
|
|
|
aMajorType,
|
|
|
|
aMinorType,
|
|
|
|
aFileExtensions,
|
|
|
|
aDescription);
|
2003-01-12 07:19:02 +03:00
|
|
|
} else {
|
|
|
|
rv = NS_ERROR_NOT_AVAILABLE;
|
2002-05-16 18:32:19 +04:00
|
|
|
}
|
2001-01-16 03:09:58 +03:00
|
|
|
}
|
2002-05-16 18:32:19 +04:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Open the file, read the first line, decide what type of file it is,
|
|
|
|
then get info based on extension */
|
2003-07-25 20:48:00 +04:00
|
|
|
// static
|
|
|
|
nsresult
|
|
|
|
nsOSHelperAppService::GetExtensionsAndDescriptionFromMimetypesFile(const nsAString& aFilename,
|
|
|
|
const nsAString& aMajorType,
|
|
|
|
const nsAString& aMinorType,
|
|
|
|
nsAString& aFileExtensions,
|
|
|
|
nsAString& aDescription) {
|
2003-01-12 07:19:02 +03:00
|
|
|
LOG(("-- GetExtensionsAndDescriptionFromMimetypesFile\n"));
|
|
|
|
LOG(("Getting extensions and description from types file '%s'\n",
|
2006-02-03 17:18:39 +03:00
|
|
|
NS_LossyConvertUTF16toASCII(aFilename).get()));
|
2003-01-12 07:19:02 +03:00
|
|
|
LOG(("Using type '%s/%s'\n",
|
2006-02-03 17:18:39 +03:00
|
|
|
NS_LossyConvertUTF16toASCII(aMajorType).get(),
|
|
|
|
NS_LossyConvertUTF16toASCII(aMinorType).get()));
|
2003-01-12 07:19:02 +03:00
|
|
|
|
2002-05-16 18:32:19 +04:00
|
|
|
nsresult rv = NS_OK;
|
|
|
|
nsCOMPtr<nsIFileInputStream> mimeFile;
|
|
|
|
nsCOMPtr<nsILineInputStream> mimeTypes;
|
|
|
|
PRBool netscapeFormat;
|
|
|
|
nsAutoString buf;
|
2004-04-22 19:32:54 +04:00
|
|
|
nsCAutoString cBuf;
|
2002-05-16 18:32:19 +04:00
|
|
|
PRBool more = PR_FALSE;
|
|
|
|
rv = CreateInputStream(aFilename, getter_AddRefs(mimeFile), getter_AddRefs(mimeTypes),
|
2004-04-22 19:32:54 +04:00
|
|
|
cBuf, &netscapeFormat, &more);
|
2002-05-16 18:32:19 +04:00
|
|
|
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoString extensions;
|
|
|
|
nsString entry;
|
|
|
|
entry.SetCapacity(100);
|
|
|
|
nsAString::const_iterator majorTypeStart, majorTypeEnd,
|
|
|
|
minorTypeStart, minorTypeEnd,
|
|
|
|
descriptionStart, descriptionEnd;
|
|
|
|
|
|
|
|
do {
|
2004-04-22 19:32:54 +04:00
|
|
|
CopyASCIItoUTF16(cBuf, buf);
|
2002-05-16 18:32:19 +04:00
|
|
|
// read through, building up an entry. If we finish an entry, check for
|
|
|
|
// a match and return out of the loop if we match
|
|
|
|
|
|
|
|
// skip comments and empty lines
|
|
|
|
if (!buf.IsEmpty() && buf.First() != '#') {
|
|
|
|
entry.Append(buf);
|
|
|
|
if (entry.Last() == '\\') {
|
|
|
|
entry.Truncate(entry.Length() - 1);
|
|
|
|
entry.Append(PRUnichar(' ')); // in case there is no trailing whitespace on this line
|
|
|
|
} else { // we have a full entry
|
2003-01-12 07:19:02 +03:00
|
|
|
LOG(("Current entry: '%s'\n",
|
2006-02-03 17:18:39 +03:00
|
|
|
NS_LossyConvertUTF16toASCII(entry).get()));
|
2002-05-16 18:32:19 +04:00
|
|
|
if (netscapeFormat) {
|
|
|
|
rv = ParseNetscapeMIMETypesEntry(entry,
|
|
|
|
majorTypeStart, majorTypeEnd,
|
|
|
|
minorTypeStart, minorTypeEnd,
|
|
|
|
extensions,
|
|
|
|
descriptionStart, descriptionEnd);
|
|
|
|
|
2003-01-12 07:19:02 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
// We sometimes get things like RealPlayer appending
|
|
|
|
// "normal" entries to "Netscape" .mime.types files. Try
|
|
|
|
// to handle that. Bug 106381.
|
|
|
|
LOG(("Bogus entry; trying 'normal' mode\n"));
|
|
|
|
rv = ParseNormalMIMETypesEntry(entry,
|
|
|
|
majorTypeStart, majorTypeEnd,
|
|
|
|
minorTypeStart, minorTypeEnd,
|
|
|
|
extensions,
|
|
|
|
descriptionStart, descriptionEnd);
|
|
|
|
}
|
2002-05-16 18:32:19 +04:00
|
|
|
} else {
|
|
|
|
rv = ParseNormalMIMETypesEntry(entry,
|
|
|
|
majorTypeStart, majorTypeEnd,
|
|
|
|
minorTypeStart,
|
|
|
|
minorTypeEnd, extensions,
|
|
|
|
descriptionStart, descriptionEnd);
|
|
|
|
|
2003-01-12 07:19:02 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
// We sometimes get things like StarOffice prepending
|
|
|
|
// "normal" entries to "Netscape" .mime.types files. Try
|
|
|
|
// to handle that. Bug 136670.
|
|
|
|
LOG(("Bogus entry; trying 'Netscape' mode\n"));
|
|
|
|
rv = ParseNetscapeMIMETypesEntry(entry,
|
|
|
|
majorTypeStart, majorTypeEnd,
|
|
|
|
minorTypeStart, minorTypeEnd,
|
|
|
|
extensions,
|
|
|
|
descriptionStart, descriptionEnd);
|
|
|
|
}
|
2002-05-16 18:32:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(rv) &&
|
|
|
|
Substring(majorTypeStart,
|
|
|
|
majorTypeEnd).Equals(aMajorType,
|
|
|
|
nsCaseInsensitiveStringComparator())&&
|
|
|
|
Substring(minorTypeStart,
|
|
|
|
minorTypeEnd).Equals(aMinorType,
|
|
|
|
nsCaseInsensitiveStringComparator())) {
|
|
|
|
// it's a match
|
|
|
|
aFileExtensions.Assign(extensions);
|
|
|
|
aDescription.Assign(Substring(descriptionStart, descriptionEnd));
|
|
|
|
mimeFile->Close();
|
|
|
|
return NS_OK;
|
2003-01-12 07:19:02 +03:00
|
|
|
} else if (NS_FAILED(rv)) {
|
2006-02-03 17:18:39 +03:00
|
|
|
LOG(("Failed to parse entry: %s\n", NS_LossyConvertUTF16toASCII(entry).get()));
|
2002-05-16 18:32:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
entry.Truncate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!more) {
|
2003-01-12 07:19:02 +03:00
|
|
|
rv = NS_ERROR_NOT_AVAILABLE;
|
2002-05-16 18:32:19 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
// read the next line
|
2004-04-22 19:32:54 +04:00
|
|
|
rv = mimeTypes->ReadLine(cBuf, &more);
|
2002-05-16 18:32:19 +04:00
|
|
|
} while (NS_SUCCEEDED(rv));
|
|
|
|
|
|
|
|
mimeFile->Close();
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This parses a Netscape format mime.types entry. There are two
|
|
|
|
* possible formats:
|
|
|
|
*
|
|
|
|
* type=foo/bar; options exts="baz" description="Some type"
|
|
|
|
*
|
|
|
|
* and
|
|
|
|
*
|
|
|
|
* type=foo/bar; options description="Some type" exts="baz"
|
|
|
|
*/
|
2003-07-25 20:48:00 +04:00
|
|
|
// static
|
|
|
|
nsresult
|
|
|
|
nsOSHelperAppService::ParseNetscapeMIMETypesEntry(const nsAString& aEntry,
|
|
|
|
nsAString::const_iterator& aMajorTypeStart,
|
|
|
|
nsAString::const_iterator& aMajorTypeEnd,
|
|
|
|
nsAString::const_iterator& aMinorTypeStart,
|
|
|
|
nsAString::const_iterator& aMinorTypeEnd,
|
|
|
|
nsAString& aExtensions,
|
|
|
|
nsAString::const_iterator& aDescriptionStart,
|
|
|
|
nsAString::const_iterator& aDescriptionEnd) {
|
2003-01-12 07:19:02 +03:00
|
|
|
LOG(("-- ParseNetscapeMIMETypesEntry\n"));
|
2002-05-16 18:32:19 +04:00
|
|
|
NS_ASSERTION(!aEntry.IsEmpty(), "Empty Netscape MIME types entry being parsed.");
|
|
|
|
|
|
|
|
nsAString::const_iterator start_iter, end_iter, match_start, match_end;
|
|
|
|
|
|
|
|
aEntry.BeginReading(start_iter);
|
|
|
|
aEntry.EndReading(end_iter);
|
|
|
|
|
|
|
|
// skip trailing whitespace
|
|
|
|
do {
|
|
|
|
--end_iter;
|
|
|
|
} while (end_iter != start_iter &&
|
|
|
|
nsCRT::IsAsciiSpace(*end_iter));
|
|
|
|
// if we're pointing to a quote, don't advance -- we don't want to
|
|
|
|
// include the quote....
|
|
|
|
if (*end_iter != '"')
|
|
|
|
++end_iter;
|
|
|
|
match_start = start_iter;
|
|
|
|
match_end = end_iter;
|
|
|
|
|
|
|
|
// Get the major and minor types
|
|
|
|
// First the major type
|
|
|
|
if (! FindInReadable(NS_LITERAL_STRING("type="), match_start, match_end)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
match_start = match_end;
|
|
|
|
|
|
|
|
while (match_end != end_iter &&
|
|
|
|
*match_end != '/') {
|
|
|
|
++match_end;
|
|
|
|
}
|
|
|
|
if (match_end == end_iter) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
aMajorTypeStart = match_start;
|
|
|
|
aMajorTypeEnd = match_end;
|
|
|
|
|
|
|
|
// now the minor type
|
|
|
|
if (++match_end == end_iter) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
match_start = match_end;
|
|
|
|
|
|
|
|
while (match_end != end_iter &&
|
|
|
|
!nsCRT::IsAsciiSpace(*match_end) &&
|
|
|
|
*match_end != ';') {
|
|
|
|
++match_end;
|
|
|
|
}
|
|
|
|
if (match_end == end_iter) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
aMinorTypeStart = match_start;
|
|
|
|
aMinorTypeEnd = match_end;
|
|
|
|
|
|
|
|
// ignore everything up to the end of the mime type from here on
|
|
|
|
start_iter = match_end;
|
|
|
|
|
|
|
|
// get the extensions
|
|
|
|
match_start = match_end;
|
|
|
|
match_end = end_iter;
|
|
|
|
if (FindInReadable(NS_LITERAL_STRING("exts="), match_start, match_end)) {
|
|
|
|
nsAString::const_iterator extStart, extEnd;
|
|
|
|
|
|
|
|
if (match_end == end_iter ||
|
|
|
|
(*match_end == '"' && ++match_end == end_iter)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
extStart = match_end;
|
|
|
|
match_start = extStart;
|
|
|
|
match_end = end_iter;
|
|
|
|
if (FindInReadable(NS_LITERAL_STRING("desc=\""), match_start, match_end)) {
|
|
|
|
// exts= before desc=, so we have to find the actual end of the extensions
|
|
|
|
extEnd = match_start;
|
|
|
|
if (extEnd == extStart) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
|
|
|
--extEnd;
|
|
|
|
} while (extEnd != extStart &&
|
|
|
|
nsCRT::IsAsciiSpace(*extEnd));
|
|
|
|
|
|
|
|
if (extEnd != extStart && *extEnd == '"') {
|
|
|
|
--extEnd;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// desc= before exts=, so we can use end_iter as the end of the extensions
|
|
|
|
extEnd = end_iter;
|
|
|
|
}
|
|
|
|
aExtensions = Substring(extStart, extEnd);
|
|
|
|
} else {
|
|
|
|
// no extensions
|
|
|
|
aExtensions.Truncate();
|
|
|
|
}
|
|
|
|
|
|
|
|
// get the description
|
|
|
|
match_start = start_iter;
|
|
|
|
match_end = end_iter;
|
|
|
|
if (FindInReadable(NS_LITERAL_STRING("desc=\""), match_start, match_end)) {
|
|
|
|
aDescriptionStart = match_end;
|
|
|
|
match_start = aDescriptionStart;
|
|
|
|
match_end = end_iter;
|
|
|
|
if (FindInReadable(NS_LITERAL_STRING("exts="), match_start, match_end)) {
|
|
|
|
// exts= after desc=, so have to find actual end of description
|
|
|
|
aDescriptionEnd = match_start;
|
|
|
|
if (aDescriptionEnd == aDescriptionStart) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
|
|
|
--aDescriptionEnd;
|
|
|
|
} while (aDescriptionEnd != aDescriptionStart &&
|
|
|
|
nsCRT::IsAsciiSpace(*aDescriptionEnd));
|
|
|
|
|
|
|
|
if (aDescriptionStart != aDescriptionStart && *aDescriptionEnd == '"') {
|
|
|
|
--aDescriptionEnd;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// desc= after exts=, so use end_iter for the description end
|
|
|
|
aDescriptionEnd = end_iter;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// no description
|
|
|
|
aDescriptionStart = start_iter;
|
|
|
|
aDescriptionEnd = start_iter;
|
|
|
|
}
|
2001-01-16 03:09:58 +03:00
|
|
|
|
2000-06-26 02:42:16 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2002-05-16 18:32:19 +04:00
|
|
|
/*
|
|
|
|
* This parses a normal format mime.types entry. The format is:
|
|
|
|
*
|
|
|
|
* major/minor ext1 ext2 ext3
|
|
|
|
*/
|
2003-07-25 20:48:00 +04:00
|
|
|
// static
|
|
|
|
nsresult
|
|
|
|
nsOSHelperAppService::ParseNormalMIMETypesEntry(const nsAString& aEntry,
|
|
|
|
nsAString::const_iterator& aMajorTypeStart,
|
|
|
|
nsAString::const_iterator& aMajorTypeEnd,
|
|
|
|
nsAString::const_iterator& aMinorTypeStart,
|
|
|
|
nsAString::const_iterator& aMinorTypeEnd,
|
|
|
|
nsAString& aExtensions,
|
|
|
|
nsAString::const_iterator& aDescriptionStart,
|
|
|
|
nsAString::const_iterator& aDescriptionEnd) {
|
2003-01-12 07:19:02 +03:00
|
|
|
LOG(("-- ParseNormalMIMETypesEntry\n"));
|
2002-05-16 18:32:19 +04:00
|
|
|
NS_ASSERTION(!aEntry.IsEmpty(), "Empty Normal MIME types entry being parsed.");
|
|
|
|
|
|
|
|
nsAString::const_iterator start_iter, end_iter, iter;
|
|
|
|
|
|
|
|
aEntry.BeginReading(start_iter);
|
|
|
|
aEntry.EndReading(end_iter);
|
|
|
|
|
|
|
|
// no description
|
|
|
|
aDescriptionStart = start_iter;
|
|
|
|
aDescriptionEnd = start_iter;
|
|
|
|
|
|
|
|
// skip leading whitespace
|
|
|
|
while (start_iter != end_iter && nsCRT::IsAsciiSpace(*start_iter)) {
|
|
|
|
++start_iter;
|
|
|
|
}
|
|
|
|
if (start_iter == end_iter) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
// skip trailing whitespace
|
|
|
|
do {
|
|
|
|
--end_iter;
|
|
|
|
} while (end_iter != start_iter && nsCRT::IsAsciiSpace(*end_iter));
|
|
|
|
|
|
|
|
++end_iter; // point to first whitespace char (or to end of string)
|
|
|
|
iter = start_iter;
|
|
|
|
|
|
|
|
// get the major type
|
2003-01-12 07:19:02 +03:00
|
|
|
if (! FindCharInReadable('/', iter, end_iter))
|
2002-05-16 18:32:19 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
2003-01-12 07:19:02 +03:00
|
|
|
|
|
|
|
nsAString::const_iterator equals_sign_iter(start_iter);
|
|
|
|
if (FindCharInReadable('=', equals_sign_iter, iter))
|
|
|
|
return NS_ERROR_FAILURE; // see bug 136670
|
|
|
|
|
2002-05-16 18:32:19 +04:00
|
|
|
aMajorTypeStart = start_iter;
|
|
|
|
aMajorTypeEnd = iter;
|
|
|
|
|
|
|
|
// get the minor type
|
|
|
|
if (++iter == end_iter) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
start_iter = iter;
|
|
|
|
|
|
|
|
while (iter != end_iter && !nsCRT::IsAsciiSpace(*iter)) {
|
|
|
|
++iter;
|
|
|
|
}
|
|
|
|
aMinorTypeStart = start_iter;
|
|
|
|
aMinorTypeEnd = iter;
|
|
|
|
|
|
|
|
// get the extensions
|
|
|
|
aExtensions.Truncate();
|
|
|
|
while (iter != end_iter) {
|
|
|
|
while (iter != end_iter && nsCRT::IsAsciiSpace(*iter)) {
|
|
|
|
++iter;
|
|
|
|
}
|
|
|
|
|
|
|
|
start_iter = iter;
|
|
|
|
while (iter != end_iter && !nsCRT::IsAsciiSpace(*iter)) {
|
|
|
|
++iter;
|
|
|
|
}
|
|
|
|
aExtensions.Append(Substring(start_iter, iter));
|
|
|
|
if (iter != end_iter) { // not the last extension
|
2003-01-12 07:19:02 +03:00
|
|
|
aExtensions.Append(PRUnichar(','));
|
2002-05-16 18:32:19 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2003-07-25 20:48:00 +04:00
|
|
|
// static
|
|
|
|
nsresult
|
|
|
|
nsOSHelperAppService::LookUpHandlerAndDescription(const nsAString& aMajorType,
|
|
|
|
const nsAString& aMinorType,
|
|
|
|
nsHashtable& aTypeOptions,
|
|
|
|
nsAString& aHandler,
|
|
|
|
nsAString& aDescription,
|
|
|
|
nsAString& aMozillaFlags) {
|
2003-01-12 07:19:02 +03:00
|
|
|
LOG(("-- LookUpHandlerAndDescription for type '%s/%s'\n",
|
2006-02-03 17:18:39 +03:00
|
|
|
NS_LossyConvertUTF16toASCII(aMajorType).get(),
|
|
|
|
NS_LossyConvertUTF16toASCII(aMinorType).get()));
|
2002-05-16 18:32:19 +04:00
|
|
|
nsresult rv = NS_OK;
|
|
|
|
nsXPIDLString mailcapFileName;
|
|
|
|
|
2003-01-12 07:19:02 +03:00
|
|
|
rv = GetFileLocation("helpers.private_mailcap_file",
|
|
|
|
"PERSONAL_MAILCAP",
|
|
|
|
getter_Copies(mailcapFileName));
|
|
|
|
if (NS_SUCCEEDED(rv) && !mailcapFileName.IsEmpty()) {
|
|
|
|
rv = GetHandlerAndDescriptionFromMailcapFile(mailcapFileName,
|
|
|
|
aMajorType,
|
|
|
|
aMinorType,
|
|
|
|
aTypeOptions,
|
|
|
|
aHandler,
|
|
|
|
aDescription,
|
|
|
|
aMozillaFlags);
|
|
|
|
} else {
|
|
|
|
rv = NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
if (NS_FAILED(rv) || aHandler.IsEmpty()) {
|
|
|
|
rv = GetFileLocation("helpers.global_mailcap_file",
|
|
|
|
"MAILCAP",
|
|
|
|
getter_Copies(mailcapFileName));
|
2002-05-16 18:32:19 +04:00
|
|
|
if (NS_SUCCEEDED(rv) && !mailcapFileName.IsEmpty()) {
|
|
|
|
rv = GetHandlerAndDescriptionFromMailcapFile(mailcapFileName,
|
|
|
|
aMajorType,
|
|
|
|
aMinorType,
|
|
|
|
aTypeOptions,
|
|
|
|
aHandler,
|
|
|
|
aDescription,
|
|
|
|
aMozillaFlags);
|
2003-01-12 07:19:02 +03:00
|
|
|
} else {
|
|
|
|
rv = NS_ERROR_NOT_AVAILABLE;
|
2002-05-16 18:32:19 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2003-07-25 20:48:00 +04:00
|
|
|
// static
|
|
|
|
nsresult
|
|
|
|
nsOSHelperAppService::GetHandlerAndDescriptionFromMailcapFile(const nsAString& aFilename,
|
|
|
|
const nsAString& aMajorType,
|
|
|
|
const nsAString& aMinorType,
|
|
|
|
nsHashtable& aTypeOptions,
|
|
|
|
nsAString& aHandler,
|
|
|
|
nsAString& aDescription,
|
|
|
|
nsAString& aMozillaFlags) {
|
2002-05-16 18:32:19 +04:00
|
|
|
|
2003-01-12 07:19:02 +03:00
|
|
|
LOG(("-- GetHandlerAndDescriptionFromMailcapFile\n"));
|
|
|
|
LOG(("Getting handler and description from mailcap file '%s'\n",
|
2006-02-03 17:18:39 +03:00
|
|
|
NS_LossyConvertUTF16toASCII(aFilename).get()));
|
2003-01-12 07:19:02 +03:00
|
|
|
LOG(("Using type '%s/%s'\n",
|
2006-02-03 17:18:39 +03:00
|
|
|
NS_LossyConvertUTF16toASCII(aMajorType).get(),
|
|
|
|
NS_LossyConvertUTF16toASCII(aMinorType).get()));
|
2003-01-12 07:19:02 +03:00
|
|
|
|
2002-05-16 18:32:19 +04:00
|
|
|
nsresult rv = NS_OK;
|
|
|
|
PRBool more = PR_FALSE;
|
|
|
|
|
|
|
|
nsCOMPtr<nsILocalFile> file(do_CreateInstance(NS_LOCAL_FILE_CONTRACTID, &rv));
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
rv = file->InitWithPath(aFilename);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIFileInputStream> mailcapFile(do_CreateInstance(NS_LOCALFILEINPUTSTREAM_CONTRACTID, &rv));
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
rv = mailcapFile->Init(file, -1, -1, PR_FALSE);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
nsCOMPtr<nsILineInputStream> mailcap (do_QueryInterface(mailcapFile, &rv));
|
|
|
|
|
|
|
|
if (NS_FAILED(rv)) {
|
2003-01-12 07:19:02 +03:00
|
|
|
LOG(("Interface trouble in stream land!"));
|
2002-05-16 18:32:19 +04:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsString entry, buffer;
|
2004-04-22 19:32:54 +04:00
|
|
|
nsCAutoString cBuffer;
|
2002-05-16 18:32:19 +04:00
|
|
|
entry.SetCapacity(128);
|
2004-04-22 19:32:54 +04:00
|
|
|
cBuffer.SetCapacity(80);
|
|
|
|
rv = mailcap->ReadLine(cBuffer, &more);
|
2002-05-16 18:32:19 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
mailcapFile->Close();
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
do { // return on end-of-file in the loop
|
|
|
|
|
2004-04-22 19:32:54 +04:00
|
|
|
CopyASCIItoUTF16(cBuffer, buffer);
|
2002-05-16 18:32:19 +04:00
|
|
|
if (!buffer.IsEmpty() && buffer.First() != '#') {
|
|
|
|
entry.Append(buffer);
|
|
|
|
if (entry.Last() == '\\') { // entry continues on next line
|
|
|
|
entry.Truncate(entry.Length()-1);
|
|
|
|
entry.Append(PRUnichar(' ')); // in case there is no trailing whitespace on this line
|
|
|
|
} else { // we have a full entry in entry. Check it for the type
|
2003-01-12 07:19:02 +03:00
|
|
|
LOG(("Current entry: '%s'\n",
|
2006-02-03 17:18:39 +03:00
|
|
|
NS_LossyConvertUTF16toASCII(entry).get()));
|
2003-01-12 07:19:02 +03:00
|
|
|
|
2002-05-16 18:32:19 +04:00
|
|
|
nsAString::const_iterator semicolon_iter,
|
|
|
|
start_iter, end_iter,
|
|
|
|
majorTypeStart, majorTypeEnd,
|
|
|
|
minorTypeStart, minorTypeEnd;
|
|
|
|
entry.BeginReading(start_iter);
|
|
|
|
entry.EndReading(end_iter);
|
|
|
|
semicolon_iter = start_iter;
|
|
|
|
FindSemicolon(semicolon_iter, end_iter);
|
|
|
|
if (semicolon_iter != end_iter) { // we have something resembling a valid entry
|
|
|
|
rv = ParseMIMEType(start_iter, majorTypeStart, majorTypeEnd,
|
|
|
|
minorTypeStart, minorTypeEnd, semicolon_iter);
|
|
|
|
if (NS_SUCCEEDED(rv) &&
|
|
|
|
Substring(majorTypeStart,
|
|
|
|
majorTypeEnd).Equals(aMajorType,
|
|
|
|
nsCaseInsensitiveStringComparator()) &&
|
2003-01-12 07:19:02 +03:00
|
|
|
Substring(minorTypeStart,
|
|
|
|
minorTypeEnd).Equals(aMinorType,
|
|
|
|
nsCaseInsensitiveStringComparator())) { // we have a match
|
2002-05-16 18:32:19 +04:00
|
|
|
PRBool match = PR_TRUE;
|
|
|
|
++semicolon_iter; // point at the first char past the semicolon
|
|
|
|
start_iter = semicolon_iter; // handler string starts here
|
|
|
|
FindSemicolon(semicolon_iter, end_iter);
|
|
|
|
while (start_iter != semicolon_iter &&
|
|
|
|
nsCRT::IsAsciiSpace(*start_iter)) {
|
|
|
|
++start_iter;
|
|
|
|
}
|
|
|
|
|
2003-01-12 07:19:02 +03:00
|
|
|
LOG(("The real handler is: '%s'\n",
|
2006-02-03 17:18:39 +03:00
|
|
|
NS_LossyConvertUTF16toASCII(Substring(start_iter,
|
2003-01-12 07:19:02 +03:00
|
|
|
semicolon_iter)).get()));
|
2002-05-16 18:32:19 +04:00
|
|
|
|
|
|
|
// XXX ugly hack. Just grab the executable name
|
|
|
|
nsAString::const_iterator end_handler_iter = semicolon_iter;
|
|
|
|
nsAString::const_iterator end_executable_iter = start_iter;
|
|
|
|
while (end_executable_iter != end_handler_iter &&
|
|
|
|
!nsCRT::IsAsciiSpace(*end_executable_iter)) {
|
|
|
|
++end_executable_iter;
|
|
|
|
}
|
|
|
|
// XXX End ugly hack
|
|
|
|
|
|
|
|
aHandler = Substring(start_iter, end_executable_iter);
|
|
|
|
|
|
|
|
nsAString::const_iterator start_option_iter, end_optionname_iter, equal_sign_iter;
|
|
|
|
PRBool equalSignFound;
|
|
|
|
while (match &&
|
|
|
|
semicolon_iter != end_iter &&
|
|
|
|
++semicolon_iter != end_iter) { // there are options left and we still match
|
|
|
|
start_option_iter = semicolon_iter;
|
|
|
|
// skip over leading whitespace
|
|
|
|
while (start_option_iter != end_iter &&
|
|
|
|
nsCRT::IsAsciiSpace(*start_option_iter)) {
|
|
|
|
++start_option_iter;
|
|
|
|
}
|
|
|
|
if (start_option_iter == end_iter) { // nothing actually here
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
semicolon_iter = start_option_iter;
|
|
|
|
FindSemicolon(semicolon_iter, end_iter);
|
|
|
|
equal_sign_iter = start_option_iter;
|
|
|
|
equalSignFound = PR_FALSE;
|
|
|
|
while (equal_sign_iter != semicolon_iter && !equalSignFound) {
|
|
|
|
switch(*equal_sign_iter) {
|
|
|
|
case '\\':
|
|
|
|
equal_sign_iter.advance(2);
|
|
|
|
break;
|
|
|
|
case '=':
|
|
|
|
equalSignFound = PR_TRUE;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
++equal_sign_iter;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end_optionname_iter = start_option_iter;
|
|
|
|
// find end of option name
|
|
|
|
while (end_optionname_iter != equal_sign_iter &&
|
|
|
|
!nsCRT::IsAsciiSpace(*end_optionname_iter)) {
|
|
|
|
++end_optionname_iter;
|
|
|
|
}
|
|
|
|
nsDependentSubstring optionName(start_option_iter, end_optionname_iter);
|
|
|
|
if (equalSignFound) {
|
|
|
|
// This is an option that has a name and value
|
2004-05-23 02:15:22 +04:00
|
|
|
if (optionName.EqualsLiteral("description")) {
|
2002-05-16 18:32:19 +04:00
|
|
|
aDescription = Substring(++equal_sign_iter, semicolon_iter);
|
2004-05-23 02:15:22 +04:00
|
|
|
} else if (optionName.EqualsLiteral("x-mozilla-flags")) {
|
2002-05-16 18:32:19 +04:00
|
|
|
aMozillaFlags = Substring(++equal_sign_iter, semicolon_iter);
|
2004-05-23 02:15:22 +04:00
|
|
|
} else if (optionName.EqualsLiteral("test")) {
|
2002-05-16 18:32:19 +04:00
|
|
|
nsCAutoString testCommand;
|
|
|
|
rv = UnescapeCommand(Substring(++equal_sign_iter, semicolon_iter),
|
|
|
|
aMajorType,
|
|
|
|
aMinorType,
|
|
|
|
aTypeOptions,
|
|
|
|
testCommand);
|
2003-01-12 07:19:02 +03:00
|
|
|
LOG(("Running Test: %s\n", testCommand.get()));
|
|
|
|
// XXX this should not use system(), since that can block the UI thread!
|
2002-05-16 18:32:19 +04:00
|
|
|
if (NS_SUCCEEDED(rv) && system(testCommand.get()) != 0) {
|
|
|
|
match = PR_FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// This is an option that just has a name but no value (eg "copiousoutput")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (match) { // we did not fail any test clauses; all is good
|
|
|
|
// get out of here
|
|
|
|
mailcapFile->Close();
|
|
|
|
return NS_OK;
|
|
|
|
} else { // pretend that this match never happened
|
|
|
|
aDescription.Truncate();
|
|
|
|
aMozillaFlags.Truncate();
|
|
|
|
aHandler.Truncate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// zero out the entry for the next cycle
|
|
|
|
entry.Truncate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!more) {
|
2003-01-12 07:19:02 +03:00
|
|
|
rv = NS_ERROR_NOT_AVAILABLE;
|
2002-05-16 18:32:19 +04:00
|
|
|
break;
|
|
|
|
}
|
2004-04-22 19:32:54 +04:00
|
|
|
rv = mailcap->ReadLine(cBuffer, &more);
|
2002-05-16 18:32:19 +04:00
|
|
|
} while (NS_SUCCEEDED(rv));
|
|
|
|
mailcapFile->Close();
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2007-05-25 19:17:45 +04:00
|
|
|
nsresult nsOSHelperAppService::OSProtocolHandlerExists(const char * aProtocolScheme, PRBool * aHandlerExists)
|
2002-05-16 18:32:19 +04:00
|
|
|
{
|
2007-05-25 19:17:45 +04:00
|
|
|
LOG(("-- nsOSHelperAppService::OSProtocolHandlerExists for '%s'\n",
|
2003-01-12 07:19:02 +03:00
|
|
|
aProtocolScheme));
|
2004-11-22 22:28:24 +03:00
|
|
|
*aHandlerExists = PR_FALSE;
|
|
|
|
|
2002-05-16 18:32:19 +04:00
|
|
|
/* if applications.protocol is in prefs, then we have an external protocol handler */
|
|
|
|
nsresult rv;
|
|
|
|
nsCAutoString prefName;
|
|
|
|
prefName = NS_LITERAL_CSTRING("applications.") + nsDependentCString(aProtocolScheme);
|
|
|
|
|
2007-12-29 01:29:39 +03:00
|
|
|
nsCOMPtr<nsIPrefService> thePrefsService(do_GetService(NS_PREFSERVICE_CONTRACTID, &rv));
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
nsCOMPtr<nsIPrefBranch> prefBranch;
|
|
|
|
rv = thePrefsService->GetBranch(prefName.get(), getter_AddRefs(prefBranch));
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
nsXPIDLCString prefString;
|
|
|
|
rv = prefBranch->GetCharPref(prefName.get(), getter_Copies(prefString));
|
|
|
|
*aHandlerExists = NS_SUCCEEDED(rv) && !prefString.IsEmpty();
|
|
|
|
if (*aHandlerExists) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2004-11-22 22:28:24 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Check the OS/2 INI for the protocol */
|
|
|
|
char szAppFromINI[CCHMAXPATH];
|
|
|
|
char szParamsFromINI[MAXINIPARAMLENGTH];
|
|
|
|
rv = GetApplicationAndParametersFromINI(nsDependentCString(aProtocolScheme),
|
|
|
|
szAppFromINI, sizeof(szAppFromINI),
|
|
|
|
szParamsFromINI, sizeof(szParamsFromINI));
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
*aHandlerExists = PR_TRUE;
|
2002-05-16 18:32:19 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2004-11-22 22:28:24 +03:00
|
|
|
|
|
|
|
return NS_ERROR_FAILURE;
|
2002-05-16 18:32:19 +04:00
|
|
|
}
|
2001-01-16 03:09:58 +03:00
|
|
|
|
2004-02-24 01:49:28 +03:00
|
|
|
already_AddRefed<nsMIMEInfoOS2>
|
2004-04-16 00:56:37 +04:00
|
|
|
nsOSHelperAppService::GetFromExtension(const nsCString& aFileExt) {
|
|
|
|
// if the extension is empty, return immediately
|
|
|
|
if (aFileExt.IsEmpty())
|
2003-07-25 20:48:00 +04:00
|
|
|
return nsnull;
|
2002-05-16 18:32:19 +04:00
|
|
|
|
2004-04-16 00:56:37 +04:00
|
|
|
LOG(("Here we do an extension lookup for '%s'\n", aFileExt.get()));
|
2001-01-16 03:09:58 +03:00
|
|
|
|
2003-01-12 07:19:02 +03:00
|
|
|
nsresult rv;
|
2001-01-16 03:09:58 +03:00
|
|
|
|
2004-02-24 01:49:28 +03:00
|
|
|
nsAutoString majorType, minorType,
|
2002-05-16 18:32:19 +04:00
|
|
|
mime_types_description, mailcap_description,
|
|
|
|
handler, mozillaFlags;
|
|
|
|
|
2006-02-03 17:18:39 +03:00
|
|
|
rv = LookUpTypeAndDescription(NS_ConvertUTF8toUTF16(aFileExt),
|
2002-05-16 18:32:19 +04:00
|
|
|
majorType,
|
|
|
|
minorType,
|
|
|
|
mime_types_description);
|
|
|
|
if (NS_FAILED(rv))
|
2003-07-25 20:48:00 +04:00
|
|
|
return nsnull;
|
2002-05-16 18:32:19 +04:00
|
|
|
|
2004-02-24 01:49:28 +03:00
|
|
|
NS_LossyConvertUTF16toASCII asciiMajorType(majorType);
|
|
|
|
NS_LossyConvertUTF16toASCII asciiMinorType(minorType);
|
|
|
|
|
2003-01-12 07:19:02 +03:00
|
|
|
LOG(("Type/Description results: majorType='%s', minorType='%s', description='%s'\n",
|
2004-02-24 01:49:28 +03:00
|
|
|
asciiMajorType.get(),
|
|
|
|
asciiMinorType.get(),
|
2006-02-03 17:18:39 +03:00
|
|
|
NS_LossyConvertUTF16toASCII(mime_types_description).get()));
|
2003-01-12 07:19:02 +03:00
|
|
|
|
2002-05-16 18:32:19 +04:00
|
|
|
if (majorType.IsEmpty() && minorType.IsEmpty()) {
|
|
|
|
// we didn't get a type mapping, so we can't do anything useful
|
2003-07-25 20:48:00 +04:00
|
|
|
return nsnull;
|
2002-05-16 18:32:19 +04:00
|
|
|
}
|
|
|
|
|
2004-02-24 01:49:28 +03:00
|
|
|
nsCAutoString mimeType(asciiMajorType + NS_LITERAL_CSTRING("/") + asciiMinorType);
|
2004-06-11 19:47:57 +04:00
|
|
|
nsMIMEInfoOS2* mimeInfo = new nsMIMEInfoOS2(mimeType);
|
2004-01-28 17:48:22 +03:00
|
|
|
if (!mimeInfo)
|
2003-07-25 20:48:00 +04:00
|
|
|
return nsnull;
|
2004-01-28 17:48:22 +03:00
|
|
|
NS_ADDREF(mimeInfo);
|
2002-05-16 18:32:19 +04:00
|
|
|
|
|
|
|
mimeInfo->AppendExtension(aFileExt);
|
|
|
|
nsHashtable typeOptions; // empty hash table
|
2004-01-05 00:50:19 +03:00
|
|
|
// The mailcap lookup is two-pass to handle the case of mailcap files
|
|
|
|
// that have something like:
|
|
|
|
//
|
|
|
|
// text/*; emacs %s
|
|
|
|
// text/rtf; soffice %s
|
|
|
|
//
|
|
|
|
// in that order. We want to pick up "soffice" for text/rtf in such cases
|
2002-05-16 18:32:19 +04:00
|
|
|
rv = LookUpHandlerAndDescription(majorType, minorType, typeOptions,
|
|
|
|
handler, mailcap_description,
|
|
|
|
mozillaFlags);
|
2003-01-12 07:19:02 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
// maybe we have an entry for "majorType/*"?
|
|
|
|
rv = LookUpHandlerAndDescription(majorType, NS_LITERAL_STRING("*"),
|
|
|
|
typeOptions, handler, mailcap_description,
|
|
|
|
mozillaFlags);
|
|
|
|
}
|
|
|
|
LOG(("Handler/Description results: handler='%s', description='%s', mozillaFlags='%s'\n",
|
2006-02-03 17:18:39 +03:00
|
|
|
NS_LossyConvertUTF16toASCII(handler).get(),
|
|
|
|
NS_LossyConvertUTF16toASCII(mailcap_description).get(),
|
|
|
|
NS_LossyConvertUTF16toASCII(mozillaFlags).get()));
|
2002-05-16 18:32:19 +04:00
|
|
|
mailcap_description.Trim(" \t\"");
|
|
|
|
mozillaFlags.Trim(" \t");
|
|
|
|
if (!mime_types_description.IsEmpty()) {
|
2004-04-16 00:56:37 +04:00
|
|
|
mimeInfo->SetDescription(mime_types_description);
|
2002-05-16 18:32:19 +04:00
|
|
|
} else {
|
2004-04-16 00:56:37 +04:00
|
|
|
mimeInfo->SetDescription(mailcap_description);
|
2002-05-16 18:32:19 +04:00
|
|
|
}
|
|
|
|
if (NS_SUCCEEDED(rv) && !handler.IsEmpty()) {
|
|
|
|
nsCOMPtr<nsIFile> handlerFile;
|
|
|
|
rv = GetFileTokenForPath(handler.get(), getter_AddRefs(handlerFile));
|
2001-01-16 03:09:58 +03:00
|
|
|
|
2002-05-16 18:32:19 +04:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2004-02-24 01:49:28 +03:00
|
|
|
mimeInfo->SetDefaultApplication(handlerFile);
|
2003-01-12 04:14:35 +03:00
|
|
|
mimeInfo->SetPreferredAction(nsIMIMEInfo::useSystemDefault);
|
2004-04-16 00:56:37 +04:00
|
|
|
mimeInfo->SetDefaultDescription(handler);
|
2002-05-16 18:32:19 +04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
mimeInfo->SetPreferredAction(nsIMIMEInfo::saveToDisk);
|
|
|
|
}
|
2003-07-25 20:48:00 +04:00
|
|
|
|
|
|
|
return mimeInfo;
|
2001-01-16 03:09:58 +03:00
|
|
|
}
|
|
|
|
|
2004-02-24 01:49:28 +03:00
|
|
|
already_AddRefed<nsMIMEInfoOS2>
|
2004-04-16 00:56:37 +04:00
|
|
|
nsOSHelperAppService::GetFromType(const nsCString& aMIMEType) {
|
|
|
|
// if the extension is empty, return immediately
|
|
|
|
if (aMIMEType.IsEmpty())
|
2003-07-25 20:48:00 +04:00
|
|
|
return nsnull;
|
2002-05-16 18:32:19 +04:00
|
|
|
|
2004-04-16 00:56:37 +04:00
|
|
|
LOG(("Here we do a mimetype lookup for '%s'\n", aMIMEType.get()));
|
2003-01-12 04:14:35 +03:00
|
|
|
nsresult rv;
|
2002-05-16 18:32:19 +04:00
|
|
|
nsAutoString extensions,
|
|
|
|
mime_types_description, mailcap_description,
|
|
|
|
handler, mozillaFlags;
|
2001-01-16 03:09:58 +03:00
|
|
|
|
2002-05-16 18:32:19 +04:00
|
|
|
nsHashtable typeOptions;
|
|
|
|
|
|
|
|
// extract the major and minor types
|
2004-04-16 00:56:37 +04:00
|
|
|
NS_ConvertASCIItoUTF16 mimeType(aMIMEType);
|
2002-05-16 18:32:19 +04:00
|
|
|
nsAString::const_iterator start_iter, end_iter,
|
|
|
|
majorTypeStart, majorTypeEnd,
|
|
|
|
minorTypeStart, minorTypeEnd;
|
2001-01-16 03:09:58 +03:00
|
|
|
|
2002-05-16 18:32:19 +04:00
|
|
|
mimeType.BeginReading(start_iter);
|
|
|
|
mimeType.EndReading(end_iter);
|
2001-01-16 03:09:58 +03:00
|
|
|
|
2002-05-16 18:32:19 +04:00
|
|
|
// XXX FIXME: add typeOptions parsing in here
|
|
|
|
rv = ParseMIMEType(start_iter, majorTypeStart, majorTypeEnd,
|
|
|
|
minorTypeStart, minorTypeEnd, end_iter);
|
|
|
|
|
|
|
|
if (NS_FAILED(rv)) {
|
2003-07-25 20:48:00 +04:00
|
|
|
return nsnull;
|
2002-05-16 18:32:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsDependentSubstring majorType(majorTypeStart, majorTypeEnd);
|
|
|
|
nsDependentSubstring minorType(minorTypeStart, minorTypeEnd);
|
2004-01-05 00:50:19 +03:00
|
|
|
// The mailcap lookup is two-pass to handle the case of mailcap files
|
|
|
|
// that have something like:
|
|
|
|
//
|
|
|
|
// text/*; emacs %s
|
|
|
|
// text/rtf; soffice %s
|
|
|
|
//
|
|
|
|
// in that order. We want to pick up "soffice" for text/rtf in such cases
|
2003-01-12 07:19:02 +03:00
|
|
|
rv = LookUpHandlerAndDescription(majorType,
|
|
|
|
minorType,
|
|
|
|
typeOptions,
|
|
|
|
handler,
|
|
|
|
mailcap_description,
|
|
|
|
mozillaFlags);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
// maybe we have an entry for "majorType/*"?
|
|
|
|
rv = LookUpHandlerAndDescription(majorType,
|
|
|
|
NS_LITERAL_STRING("*"),
|
|
|
|
typeOptions,
|
|
|
|
handler,
|
|
|
|
mailcap_description,
|
|
|
|
mozillaFlags);
|
|
|
|
}
|
|
|
|
LOG(("Handler/Description results: handler='%s', description='%s', mozillaFlags='%s'\n",
|
2006-02-03 17:18:39 +03:00
|
|
|
NS_LossyConvertUTF16toASCII(handler).get(),
|
|
|
|
NS_LossyConvertUTF16toASCII(mailcap_description).get(),
|
|
|
|
NS_LossyConvertUTF16toASCII(mozillaFlags).get()));
|
2002-05-16 18:32:19 +04:00
|
|
|
|
|
|
|
if (handler.IsEmpty()) {
|
|
|
|
// we have no useful info....
|
2003-07-25 20:48:00 +04:00
|
|
|
return nsnull;
|
2002-05-16 18:32:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
mailcap_description.Trim(" \t\"");
|
|
|
|
mozillaFlags.Trim(" \t");
|
|
|
|
LookUpExtensionsAndDescription(majorType,
|
|
|
|
minorType,
|
|
|
|
extensions,
|
|
|
|
mime_types_description);
|
2003-07-25 20:48:00 +04:00
|
|
|
|
2004-06-11 19:47:57 +04:00
|
|
|
nsMIMEInfoOS2* mimeInfo = new nsMIMEInfoOS2(aMIMEType);
|
2004-01-28 17:48:22 +03:00
|
|
|
if (!mimeInfo)
|
2003-07-25 20:48:00 +04:00
|
|
|
return nsnull;
|
2004-01-28 17:48:22 +03:00
|
|
|
NS_ADDREF(mimeInfo);
|
2003-07-25 20:48:00 +04:00
|
|
|
|
2006-02-03 17:18:39 +03:00
|
|
|
mimeInfo->SetFileExtensions(NS_ConvertUTF16toUTF8(extensions));
|
2002-05-16 18:32:19 +04:00
|
|
|
if (! mime_types_description.IsEmpty()) {
|
2004-04-16 00:56:37 +04:00
|
|
|
mimeInfo->SetDescription(mime_types_description);
|
2002-05-16 18:32:19 +04:00
|
|
|
} else {
|
2004-04-16 00:56:37 +04:00
|
|
|
mimeInfo->SetDescription(mailcap_description);
|
2002-05-16 18:32:19 +04:00
|
|
|
}
|
2003-07-25 20:48:00 +04:00
|
|
|
|
2002-05-16 18:32:19 +04:00
|
|
|
nsCOMPtr<nsIFile> handlerFile;
|
|
|
|
rv = GetFileTokenForPath(handler.get(), getter_AddRefs(handlerFile));
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2004-02-24 01:49:28 +03:00
|
|
|
mimeInfo->SetDefaultApplication(handlerFile);
|
2003-01-12 04:14:35 +03:00
|
|
|
mimeInfo->SetPreferredAction(nsIMIMEInfo::useSystemDefault);
|
2004-04-16 00:56:37 +04:00
|
|
|
mimeInfo->SetDefaultDescription(handler);
|
2002-05-16 18:32:19 +04:00
|
|
|
} else {
|
|
|
|
mimeInfo->SetPreferredAction(nsIMIMEInfo::saveToDisk);
|
|
|
|
}
|
2003-07-25 20:48:00 +04:00
|
|
|
|
|
|
|
return mimeInfo;
|
2001-01-16 03:09:58 +03:00
|
|
|
}
|
2003-07-25 20:48:00 +04:00
|
|
|
|
2008-01-10 01:24:07 +03:00
|
|
|
//------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// returns a localized string from unknownContentType.properties
|
|
|
|
|
|
|
|
static nsresult
|
|
|
|
GetNLSString(const PRUnichar *aKey, nsAString& result)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIStringBundleService> bundleSvc =
|
|
|
|
do_GetService(NS_STRINGBUNDLE_CONTRACTID, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIStringBundle> bundle;
|
|
|
|
rv = bundleSvc->CreateBundle(
|
|
|
|
"chrome://mozapps/locale/downloads/unknownContentType.properties",
|
|
|
|
getter_AddRefs(bundle));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsXPIDLString string;
|
|
|
|
rv = bundle->GetStringFromName(aKey, getter_Copies(string));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
result.Assign(string);
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// returns the handle of the WPS object associated with a file extension;
|
|
|
|
// if RWS isn't being used or there's no association, returns zero;
|
|
|
|
// also constructs a description of the handler based on available info
|
|
|
|
|
|
|
|
static PRUint32
|
|
|
|
WpsGetDefaultHandler(const char *aFileExt, nsAString& aDescription)
|
|
|
|
{
|
|
|
|
aDescription.Truncate();
|
|
|
|
|
|
|
|
if (sUseRws) {
|
|
|
|
nsCOMPtr<nsIRwsService> rwsSvc(do_GetService("@mozilla.org/rwsos2;1"));
|
|
|
|
if (!rwsSvc)
|
|
|
|
sUseRws = PR_FALSE;
|
|
|
|
else {
|
|
|
|
PRUint32 handle;
|
|
|
|
// the handle may be zero if the WPS class provides the default handler
|
|
|
|
if (NS_SUCCEEDED(rwsSvc->HandlerFromExtension(aFileExt, &handle, aDescription)))
|
|
|
|
return handle;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// no RWS or RWS failed to return at least a description
|
|
|
|
if (NS_FAILED(GetNLSString(NS_LITERAL_STRING("wpsDefaultOS2").get(),
|
|
|
|
aDescription)))
|
|
|
|
aDescription.Assign(NS_LITERAL_STRING("WPS default"));
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// this constructs the system-default entry when neither mailcap nor
|
|
|
|
// mime.types provided any info
|
|
|
|
|
|
|
|
static void
|
|
|
|
WpsMimeInfoFromExtension(const char *aFileExt, nsMIMEInfoOS2 *aMI)
|
|
|
|
{
|
|
|
|
// this identifies whether the mimetype is a bogus
|
|
|
|
// "application/octet-stream" generated when no match
|
|
|
|
// could be found for this extension
|
|
|
|
PRBool exists;
|
|
|
|
aMI->ExtensionExists(nsDependentCString(aFileExt), &exists);
|
|
|
|
|
|
|
|
// get the default app's description and WPS handle (if any)
|
|
|
|
nsAutoString ustr;
|
|
|
|
PRUint32 handle = WpsGetDefaultHandler(aFileExt, ustr);
|
|
|
|
aMI->SetDefaultDescription(ustr);
|
|
|
|
aMI->SetDefaultAppHandle(handle);
|
|
|
|
|
|
|
|
// if the mimeinfo is bogus, change the mimetype & extensions list
|
|
|
|
if (!exists) {
|
|
|
|
nsCAutoString extLower;
|
|
|
|
nsCAutoString cstr;
|
|
|
|
ToLowerCase(nsDependentCString(aFileExt), extLower);
|
|
|
|
cstr.Assign(NS_LITERAL_CSTRING("application/x-") + extLower);
|
|
|
|
aMI->SetMIMEType(cstr);
|
|
|
|
aMI->SetFileExtensions(extLower);
|
|
|
|
}
|
|
|
|
|
|
|
|
// if the mimetype is valid, perhaps we can supply a description;
|
|
|
|
// if it's bogus, replace the description
|
|
|
|
if (exists)
|
|
|
|
aMI->GetDescription(ustr);
|
|
|
|
else
|
|
|
|
ustr.Truncate();
|
|
|
|
|
|
|
|
if (ustr.IsEmpty()) {
|
|
|
|
nsCAutoString extUpper;
|
|
|
|
ToUpperCase(nsDependentCString(aFileExt), extUpper);
|
|
|
|
CopyUTF8toUTF16(extUpper, ustr);
|
|
|
|
|
|
|
|
nsAutoString fileType;
|
|
|
|
if (NS_FAILED(GetNLSString(NS_LITERAL_STRING("fileType").get(), fileType)))
|
|
|
|
ustr.Assign(NS_LITERAL_STRING("%S file"));
|
|
|
|
int pos = -1;
|
|
|
|
if ((pos = fileType.Find("%S")) > -1);
|
|
|
|
fileType.Replace(pos, 2, ustr);
|
|
|
|
aMI->SetDescription(fileType);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// this is an override of nsExternalHelperAppService's method;
|
|
|
|
// after the parent's method has looked for a handler, add
|
|
|
|
// an entry for the WPS's handler if there's room and one exists;
|
|
|
|
// it will never replace entries from mailcap or mimetypes.rdf
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsOSHelperAppService::GetFromTypeAndExtension(const nsACString& aMIMEType,
|
|
|
|
const nsACString& aFileExt,
|
|
|
|
nsIMIMEInfo **_retval)
|
|
|
|
{
|
|
|
|
// let the existing code do its thing
|
|
|
|
nsresult rv = nsExternalHelperAppService::GetFromTypeAndExtension(
|
|
|
|
aMIMEType, aFileExt, _retval);
|
|
|
|
if (!(*_retval))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
// this is needed for Get/SetDefaultApplication()
|
|
|
|
nsMIMEInfoOS2 *mi = static_cast<nsMIMEInfoOS2*>(*_retval);
|
|
|
|
|
|
|
|
// do lookups using the original extension if present;
|
|
|
|
// otherwise use the extension derived from the mimetype
|
|
|
|
nsCAutoString ext;
|
|
|
|
if (!aFileExt.IsEmpty())
|
|
|
|
ext.Assign(aFileExt);
|
|
|
|
else {
|
|
|
|
mi->GetPrimaryExtension(ext);
|
|
|
|
if (ext.IsEmpty())
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIFile> defApp;
|
|
|
|
nsCOMPtr<nsIHandlerApp> prefApp;
|
|
|
|
mi->GetDefaultApplication(getter_AddRefs(defApp));
|
|
|
|
mi->GetPreferredApplicationHandler(getter_AddRefs(prefApp));
|
|
|
|
nsCOMPtr<nsILocalHandlerApp> locPrefApp = do_QueryInterface(prefApp, &rv);
|
|
|
|
|
|
|
|
// if neither mailcap nor mimetypes.rdf had an entry,
|
|
|
|
// create a default entry using the WPS handler
|
|
|
|
if (!defApp && !locPrefApp) {
|
|
|
|
WpsMimeInfoFromExtension(ext.get(), mi);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRBool gotPromoted = PR_FALSE;
|
|
|
|
|
|
|
|
// both mailcap & mimetypes.rdf have an entry; if they're
|
|
|
|
// different, exit; otherwise, clear the default entry
|
|
|
|
if (defApp && locPrefApp) {
|
|
|
|
PRBool sameFile;
|
|
|
|
nsCOMPtr<nsIFile> app;
|
|
|
|
rv = locPrefApp->GetExecutable(getter_AddRefs(app));
|
|
|
|
defApp->Equals(app, &sameFile);
|
|
|
|
if (!sameFile)
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
defApp = 0;
|
|
|
|
mi->SetDefaultApplication(0);
|
|
|
|
mi->SetDefaultDescription(EmptyString());
|
|
|
|
gotPromoted = PR_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoString description;
|
|
|
|
|
|
|
|
// mailcap has an entry but mimetypes.rdf doesn't;
|
|
|
|
// promote mailcap's entry to preferred
|
|
|
|
if (defApp && !locPrefApp) {
|
|
|
|
mi->GetDefaultDescription(description);
|
|
|
|
nsLocalHandlerApp *handlerApp(new nsLocalHandlerApp(description, defApp));
|
|
|
|
mi->SetPreferredApplicationHandler(handlerApp);
|
|
|
|
gotPromoted = PR_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if the former default app was promoted to preferred app,
|
|
|
|
// update preferred action if appropriate
|
|
|
|
if (gotPromoted) {
|
|
|
|
nsHandlerInfoAction action;
|
|
|
|
mi->GetPreferredAction(&action);
|
|
|
|
if (action == nsIMIMEInfo::useSystemDefault) {
|
|
|
|
mi->SetPreferredAction(nsIMIMEInfo::useHelperApp);
|
|
|
|
mi->SetPreferredAction(nsIMIMEInfo::useHelperApp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// use the WPS default as the system default handler
|
|
|
|
PRUint32 handle = WpsGetDefaultHandler(ext.get(), description);
|
|
|
|
mi->SetDefaultDescription(description);
|
|
|
|
mi->SetDefaultApplication(0);
|
|
|
|
mi->SetDefaultAppHandle(handle);
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------
|
2003-07-25 20:48:00 +04:00
|
|
|
|
|
|
|
already_AddRefed<nsIMIMEInfo>
|
2004-04-16 00:56:37 +04:00
|
|
|
nsOSHelperAppService::GetMIMEInfoFromOS(const nsACString& aType,
|
|
|
|
const nsACString& aFileExt,
|
2003-11-15 18:01:39 +03:00
|
|
|
PRBool *aFound) {
|
|
|
|
*aFound = PR_TRUE;
|
2004-04-16 00:56:37 +04:00
|
|
|
nsMIMEInfoOS2* retval = GetFromType(PromiseFlatCString(aType)).get();
|
2003-07-25 20:48:00 +04:00
|
|
|
PRBool hasDefault = PR_FALSE;
|
|
|
|
if (retval)
|
|
|
|
retval->GetHasDefaultHandler(&hasDefault);
|
|
|
|
if (!retval || !hasDefault) {
|
2004-04-16 00:56:37 +04:00
|
|
|
nsRefPtr<nsMIMEInfoOS2> miByExt = GetFromExtension(PromiseFlatCString(aFileExt));
|
2003-11-15 18:01:39 +03:00
|
|
|
// If we had no extension match, but a type match, use that
|
|
|
|
if (!miByExt && retval)
|
2003-07-25 20:48:00 +04:00
|
|
|
return retval;
|
2003-11-15 18:01:39 +03:00
|
|
|
// If we had an extension match but no type match, set the mimetype and use
|
|
|
|
// it
|
|
|
|
if (!retval && miByExt) {
|
2004-04-16 00:56:37 +04:00
|
|
|
if (!aType.IsEmpty())
|
2003-07-25 20:48:00 +04:00
|
|
|
miByExt->SetMIMEType(aType);
|
|
|
|
miByExt.swap(retval);
|
2003-11-15 18:01:39 +03:00
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
// If we got nothing, make a new mimeinfo
|
|
|
|
if (!retval) {
|
|
|
|
*aFound = PR_FALSE;
|
2004-06-11 19:47:57 +04:00
|
|
|
retval = new nsMIMEInfoOS2(aType);
|
2003-11-15 18:01:39 +03:00
|
|
|
if (retval) {
|
2004-01-28 17:48:22 +03:00
|
|
|
NS_ADDREF(retval);
|
2004-04-16 00:56:37 +04:00
|
|
|
if (!aFileExt.IsEmpty())
|
2003-11-15 18:01:39 +03:00
|
|
|
retval->AppendExtension(aFileExt);
|
|
|
|
}
|
|
|
|
|
2003-07-25 20:48:00 +04:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2004-02-24 01:49:28 +03:00
|
|
|
// Copy the attributes of retval onto miByExt, to return it
|
|
|
|
retval->CopyBasicDataTo(miByExt);
|
2003-07-25 20:48:00 +04:00
|
|
|
|
2004-02-24 01:49:28 +03:00
|
|
|
miByExt.swap(retval);
|
2003-07-25 20:48:00 +04:00
|
|
|
}
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
2008-02-20 07:46:36 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsOSHelperAppService::GetProtocolHandlerInfoFromOS(const nsACString &aScheme,
|
|
|
|
PRBool *found,
|
|
|
|
nsIHandlerInfo **_retval)
|
2007-08-03 01:27:28 +04:00
|
|
|
{
|
|
|
|
NS_ASSERTION(!aScheme.IsEmpty(), "No scheme was specified!");
|
|
|
|
|
|
|
|
nsresult rv = OSProtocolHandlerExists(nsPromiseFlatCString(aScheme).get(),
|
2007-08-03 23:06:33 +04:00
|
|
|
found);
|
|
|
|
if (NS_FAILED(rv))
|
2008-02-20 07:46:36 +03:00
|
|
|
return rv;
|
2007-08-03 01:27:28 +04:00
|
|
|
|
2007-08-03 23:06:33 +04:00
|
|
|
nsMIMEInfoOS2 *handlerInfo =
|
|
|
|
new nsMIMEInfoOS2(aScheme, nsMIMEInfoBase::eProtocolInfo);
|
2008-02-20 09:10:34 +03:00
|
|
|
NS_ENSURE_TRUE(handlerInfo, NS_ERROR_OUT_OF_MEMORY);
|
2008-02-20 07:46:36 +03:00
|
|
|
NS_ADDREF(*_retval = handlerInfo);
|
2007-08-03 01:27:28 +04:00
|
|
|
|
2007-08-03 23:06:33 +04:00
|
|
|
if (!*found) {
|
|
|
|
// Code that calls this requires an object regardless if the OS has
|
|
|
|
// something for us, so we return the empty object.
|
2008-02-20 07:46:36 +03:00
|
|
|
return NS_OK;
|
2007-08-03 23:06:33 +04:00
|
|
|
}
|
|
|
|
|
2007-08-03 01:27:28 +04:00
|
|
|
nsAutoString desc;
|
|
|
|
GetApplicationDescription(aScheme, desc);
|
|
|
|
handlerInfo->SetDefaultDescription(desc);
|
|
|
|
|
2008-02-20 07:46:36 +03:00
|
|
|
return NS_OK;
|
2007-08-03 01:27:28 +04:00
|
|
|
}
|
|
|
|
|
2004-11-22 22:28:24 +03:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsOSHelperAppService::GetApplicationDescription(const nsACString& aScheme, nsAString& _retval)
|
|
|
|
{
|
2007-12-29 01:29:39 +03:00
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIPrefService> thePrefsService(do_GetService(NS_PREFSERVICE_CONTRACTID, &rv));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2004-11-22 22:28:24 +03:00
|
|
|
nsCAutoString prefName = NS_LITERAL_CSTRING("applications.") + aScheme;
|
2007-12-29 01:29:39 +03:00
|
|
|
nsCOMPtr<nsIPrefBranch> prefBranch;
|
2004-11-22 22:28:24 +03:00
|
|
|
nsCAutoString applicationName;
|
|
|
|
|
2007-12-29 01:29:39 +03:00
|
|
|
rv = thePrefsService->GetBranch(prefName.get(), getter_AddRefs(prefBranch));
|
|
|
|
if (NS_FAILED(rv)) {
|
2004-11-22 22:28:24 +03:00
|
|
|
char szAppFromINI[CCHMAXPATH];
|
|
|
|
char szParamsFromINI[MAXINIPARAMLENGTH];
|
|
|
|
/* did OS2.INI contain application? */
|
|
|
|
rv = GetApplicationAndParametersFromINI(aScheme,
|
|
|
|
szAppFromINI, sizeof(szAppFromINI),
|
|
|
|
szParamsFromINI, sizeof(szParamsFromINI));
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
applicationName = szAppFromINI;
|
|
|
|
} else {
|
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
} else {
|
2007-12-29 01:29:39 +03:00
|
|
|
nsXPIDLCString prefString;
|
|
|
|
rv = prefBranch->GetCharPref(prefName.get(), getter_Copies(prefString));
|
|
|
|
if (NS_SUCCEEDED(rv) && !prefString.IsEmpty()) {
|
|
|
|
applicationName.Append(prefString);
|
|
|
|
}
|
2004-11-22 22:28:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
nsCOMPtr<nsILocalFile> application;
|
|
|
|
rv = NS_NewNativeLocalFile(nsDependentCString(applicationName.get()),
|
|
|
|
PR_FALSE,
|
|
|
|
getter_AddRefs(application));
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
char szAppPath[CCHMAXPATH];
|
|
|
|
APIRET rc = DosSearchPath(SEARCH_IGNORENETERRS | SEARCH_ENVIRONMENT,
|
|
|
|
"PATH",
|
|
|
|
applicationName.get(),
|
|
|
|
szAppPath,
|
|
|
|
sizeof(szAppPath));
|
|
|
|
/* if that worked, we can use the full pathname */
|
|
|
|
if (rc == NO_ERROR) {
|
|
|
|
_retval.Assign(NS_ConvertUTF8toUTF16(nsDependentCString(szAppPath)));
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* if the full path is given or not, use what we have */
|
|
|
|
_retval.Assign(NS_ConvertUTF8toUTF16(applicationName));
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2007-12-29 01:29:39 +03:00
|
|
|
// Check OS/2 INI for application and parameters for the protocol
|
|
|
|
// return NS_OK, if application exists for protocol in INI and is not empty
|
|
|
|
nsresult GetApplicationAndParametersFromINI(const nsACString& aProtocol,
|
|
|
|
char* app, ULONG appLength,
|
|
|
|
char* param, ULONG paramLength)
|
|
|
|
{
|
|
|
|
/* initialize app to '\0' for later check */
|
|
|
|
*app = '\0';
|
|
|
|
|
|
|
|
/* http or https */
|
|
|
|
if ((aProtocol == NS_LITERAL_CSTRING("http")) ||
|
|
|
|
(aProtocol == NS_LITERAL_CSTRING("https"))) {
|
|
|
|
PrfQueryProfileString(HINI_USER,
|
|
|
|
"WPURLDEFAULTSETTINGS",
|
|
|
|
"DefaultBrowserExe",
|
|
|
|
"",
|
|
|
|
app,
|
|
|
|
appLength);
|
|
|
|
PrfQueryProfileString(HINI_USER,
|
|
|
|
"WPURLDEFAULTSETTINGS",
|
|
|
|
"DefaultParameters",
|
|
|
|
"",
|
|
|
|
param,
|
|
|
|
paramLength);
|
|
|
|
}
|
|
|
|
/* mailto: */
|
|
|
|
else if (aProtocol == NS_LITERAL_CSTRING("mailto")) {
|
|
|
|
PrfQueryProfileString(HINI_USER,
|
|
|
|
"WPURLDEFAULTSETTINGS",
|
|
|
|
"DefaultMailExe",
|
|
|
|
"",
|
|
|
|
app,
|
|
|
|
appLength);
|
|
|
|
PrfQueryProfileString(HINI_USER,
|
|
|
|
"WPURLDEFAULTSETTINGS",
|
|
|
|
"DefaultMailParameters",
|
|
|
|
"",
|
|
|
|
param,
|
|
|
|
paramLength);
|
|
|
|
}
|
|
|
|
/* ftp */
|
|
|
|
else if (aProtocol == NS_LITERAL_CSTRING("ftp")) {
|
|
|
|
PrfQueryProfileString(HINI_USER,
|
|
|
|
"WPURLDEFAULTSETTINGS",
|
|
|
|
"DefaultFTPExe",
|
|
|
|
"",
|
|
|
|
app,
|
|
|
|
appLength);
|
|
|
|
PrfQueryProfileString(HINI_USER,
|
|
|
|
"WPURLDEFAULTSETTINGS",
|
|
|
|
"DefaultFTPParameters",
|
|
|
|
"",
|
|
|
|
param,
|
|
|
|
paramLength);
|
|
|
|
}
|
|
|
|
/* news: or snews: */
|
|
|
|
else if ((aProtocol == NS_LITERAL_CSTRING("news")) ||
|
|
|
|
(aProtocol == NS_LITERAL_CSTRING("snews"))) {
|
|
|
|
PrfQueryProfileString(HINI_USER,
|
|
|
|
"WPURLDEFAULTSETTINGS",
|
|
|
|
"DefaultNewsExe",
|
|
|
|
"",
|
|
|
|
app,
|
|
|
|
appLength);
|
|
|
|
PrfQueryProfileString(HINI_USER,
|
|
|
|
"WPURLDEFAULTSETTINGS",
|
|
|
|
"DefaultNewsParameters",
|
|
|
|
"",
|
|
|
|
param,
|
|
|
|
paramLength);
|
|
|
|
}
|
|
|
|
/* irc: */
|
|
|
|
else if (aProtocol == NS_LITERAL_CSTRING("irc")) {
|
|
|
|
PrfQueryProfileString(HINI_USER,
|
|
|
|
"WPURLDEFAULTSETTINGS",
|
|
|
|
"DefaultIRCExe",
|
|
|
|
"",
|
|
|
|
app,
|
|
|
|
appLength);
|
|
|
|
PrfQueryProfileString(HINI_USER,
|
|
|
|
"WPURLDEFAULTSETTINGS",
|
|
|
|
"DefaultIRCParameters",
|
|
|
|
"",
|
|
|
|
param,
|
|
|
|
paramLength);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
NS_WARNING("GetApplicationAndParametersFromINI(): unsupported protocol scheme");
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* application string in INI was empty */
|
|
|
|
if (app[0] == '\0')
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|