2000-06-18 23:54:37 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 3; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* The Original Code is the Mozilla browser.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is Netscape
|
|
|
|
* Communications, Inc. Portions created by Netscape are
|
|
|
|
* Copyright (C) 1999, Mozilla. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Scott MacGregor <mscott@netscape.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "nsOSHelperAppService.h"
|
|
|
|
#include "nsISupports.h"
|
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsXPIDLString.h"
|
|
|
|
#include "nsIURL.h"
|
|
|
|
#include "nsILocalFile.h"
|
2000-06-23 08:43:13 +04:00
|
|
|
#include "nsILocalFileMac.h"
|
2000-09-07 01:12:53 +04:00
|
|
|
#include "nsMimeTypes.h"
|
2001-04-17 00:16:41 +04:00
|
|
|
#include "nsIStringBundle.h"
|
|
|
|
#include "nsIPromptService.h"
|
|
|
|
#include "nsMemory.h"
|
2000-09-07 01:12:53 +04:00
|
|
|
|
|
|
|
#include "nsIInternetConfigService.h"
|
2000-06-19 02:48:43 +04:00
|
|
|
|
2003-04-22 08:16:49 +04:00
|
|
|
#include <LaunchServices.h>
|
|
|
|
|
2001-04-17 00:16:41 +04:00
|
|
|
// chrome URL's
|
|
|
|
#define HELPERAPPLAUNCHER_BUNDLE_URL "chrome://global/locale/helperAppLauncher.properties"
|
|
|
|
#define BRAND_BUNDLE_URL "chrome://global/locale/brand.properties"
|
|
|
|
|
2002-02-12 06:32:14 +03:00
|
|
|
#define NS_PROMPTSERVICE_CONTRACTID "@mozilla.org/embedcomp/prompt-service;1"
|
2001-04-17 00:16:41 +04:00
|
|
|
|
2000-06-18 23:54:37 +04:00
|
|
|
nsOSHelperAppService::nsOSHelperAppService() : nsExternalHelperAppService()
|
2000-06-23 08:43:13 +04:00
|
|
|
{
|
|
|
|
}
|
2000-06-18 23:54:37 +04:00
|
|
|
|
|
|
|
nsOSHelperAppService::~nsOSHelperAppService()
|
|
|
|
{}
|
|
|
|
|
2000-07-07 05:48:17 +04:00
|
|
|
NS_IMETHODIMP nsOSHelperAppService::LaunchAppWithTempFile(nsIMIMEInfo * aMIMEInfo, nsIFile * aTempFile)
|
2000-06-18 23:54:37 +04:00
|
|
|
{
|
2000-07-07 05:48:17 +04:00
|
|
|
nsresult rv = NS_OK;
|
|
|
|
if (aMIMEInfo)
|
|
|
|
{
|
2003-04-22 08:16:49 +04:00
|
|
|
nsCOMPtr<nsIFile> application;
|
2003-01-12 04:14:35 +03:00
|
|
|
|
2003-01-12 05:22:23 +03:00
|
|
|
nsMIMEInfoHandleAction action = nsIMIMEInfo::useSystemDefault;
|
2003-01-12 04:14:35 +03:00
|
|
|
aMIMEInfo->GetPreferredAction(&action);
|
|
|
|
|
|
|
|
if (action==nsIMIMEInfo::useHelperApp)
|
|
|
|
aMIMEInfo->GetPreferredApplicationHandler(getter_AddRefs(application));
|
|
|
|
else
|
|
|
|
aMIMEInfo->GetDefaultApplicationHandler(getter_AddRefs(application));
|
|
|
|
|
|
|
|
if (!application)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
2003-04-22 08:16:49 +04:00
|
|
|
nsCOMPtr<nsILocalFileMac> app = do_QueryInterface(application, &rv);
|
2003-01-12 04:14:35 +03:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2003-04-22 08:16:49 +04:00
|
|
|
nsCOMPtr<nsILocalFile> docToLoad = do_QueryInterface(aTempFile, &rv);
|
2003-01-12 04:14:35 +03:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
rv = app->LaunchWithDoc(docToLoad, PR_FALSE);
|
2000-06-18 23:54:37 +04:00
|
|
|
}
|
2003-04-22 08:16:49 +04:00
|
|
|
#ifdef XP_MACOSX
|
|
|
|
else
|
|
|
|
{ // We didn't get an application to handle the file from aMIMEInfo, ask LaunchServices directly
|
|
|
|
nsCOMPtr <nsILocalFileMac> tempFile = do_QueryInterface(aTempFile, &rv);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
FSRef tempFileRef;
|
|
|
|
tempFile->GetFSRef(&tempFileRef);
|
|
|
|
|
|
|
|
FSRef appFSRef;
|
|
|
|
if (::LSGetApplicationForItem(&tempFileRef, kLSRolesAll, &appFSRef, nsnull) == noErr)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsILocalFileMac> app(do_CreateInstance("@mozilla.org/file/local;1"));
|
|
|
|
if (!app) return NS_ERROR_FAILURE;
|
|
|
|
app->InitWithFSRef(&appFSRef);
|
|
|
|
|
|
|
|
nsCOMPtr <nsILocalFile> docToLoad = do_QueryInterface(aTempFile, &rv);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
rv = app->LaunchWithDoc(docToLoad, PR_FALSE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2000-07-07 05:48:17 +04:00
|
|
|
return rv;
|
2000-06-18 23:54:37 +04:00
|
|
|
}
|
2000-06-19 02:48:43 +04:00
|
|
|
|
|
|
|
NS_IMETHODIMP nsOSHelperAppService::ExternalProtocolHandlerExists(const char * aProtocolScheme, PRBool * aHandlerExists)
|
|
|
|
{
|
2001-04-17 00:16:41 +04:00
|
|
|
// look up the protocol scheme in Internet Config....if we find a match then we have a handler for it...
|
2000-06-19 02:48:43 +04:00
|
|
|
*aHandlerExists = PR_FALSE;
|
2001-04-17 00:16:41 +04:00
|
|
|
// ask the internet config service to look it up for us...
|
|
|
|
nsresult rv = NS_ERROR_FAILURE;
|
|
|
|
nsCOMPtr<nsIInternetConfigService> icService (do_GetService(NS_INTERNETCONFIGSERVICE_CONTRACTID));
|
|
|
|
if (icService)
|
|
|
|
{
|
|
|
|
rv = icService->HasProtocolHandler(aProtocolScheme, aHandlerExists);
|
|
|
|
if (rv == NS_ERROR_NOT_AVAILABLE)
|
|
|
|
{
|
|
|
|
// current app is registered to handle the protocol, put up an alert
|
|
|
|
nsCOMPtr<nsIStringBundleService> stringBundleService = do_GetService(NS_STRINGBUNDLE_CONTRACTID);
|
|
|
|
if (stringBundleService)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIStringBundle> appLauncherBundle;
|
2001-04-28 02:35:13 +04:00
|
|
|
rv = stringBundleService->CreateBundle(HELPERAPPLAUNCHER_BUNDLE_URL, getter_AddRefs(appLauncherBundle));
|
2001-04-17 00:16:41 +04:00
|
|
|
if (rv == NS_OK)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIStringBundle> brandBundle;
|
2001-04-28 02:35:13 +04:00
|
|
|
rv = stringBundleService->CreateBundle(BRAND_BUNDLE_URL, getter_AddRefs(brandBundle));
|
2001-04-17 00:16:41 +04:00
|
|
|
if (rv == NS_OK)
|
|
|
|
{
|
|
|
|
nsXPIDLString brandName;
|
|
|
|
rv = brandBundle->GetStringFromName(NS_LITERAL_STRING("brandShortName").get(), getter_Copies(brandName));
|
|
|
|
if (rv == NS_OK)
|
|
|
|
{
|
|
|
|
nsXPIDLString errorStr;
|
|
|
|
NS_ConvertASCIItoUCS2 proto(aProtocolScheme);
|
|
|
|
const PRUnichar *formatStrings[] = { brandName.get(), proto.get() };
|
|
|
|
rv = appLauncherBundle->FormatStringFromName(NS_LITERAL_STRING("protocolNotHandled").get(),
|
|
|
|
formatStrings,
|
|
|
|
2,
|
|
|
|
getter_Copies(errorStr));
|
|
|
|
if (rv == NS_OK)
|
|
|
|
{
|
2002-02-12 06:32:14 +03:00
|
|
|
nsCOMPtr<nsIPromptService> prompt (do_GetService(NS_PROMPTSERVICE_CONTRACTID));
|
2001-04-17 00:16:41 +04:00
|
|
|
if (prompt)
|
|
|
|
prompt->Alert(nsnull, NS_LITERAL_STRING("Alert").get(), errorStr.get());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rv;
|
2000-06-19 02:48:43 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsOSHelperAppService::LoadUrl(nsIURI * aURL)
|
|
|
|
{
|
2002-03-06 10:48:55 +03:00
|
|
|
nsCAutoString url;
|
2001-04-17 00:16:41 +04:00
|
|
|
nsresult rv = NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
if (aURL)
|
|
|
|
{
|
2002-03-06 10:48:55 +03:00
|
|
|
aURL->GetSpec(url);
|
|
|
|
if (!url.IsEmpty())
|
2001-04-17 00:16:41 +04:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIInternetConfigService> icService (do_GetService(NS_INTERNETCONFIGSERVICE_CONTRACTID));
|
|
|
|
if (icService)
|
2002-03-06 10:48:55 +03:00
|
|
|
rv = icService->LaunchURL(url.get());
|
2001-04-17 00:16:41 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return rv;
|
2000-06-19 02:48:43 +04:00
|
|
|
}
|
2000-06-22 08:41:36 +04:00
|
|
|
|
|
|
|
nsresult nsOSHelperAppService::GetFileTokenForPath(const PRUnichar * platformAppPath, nsIFile ** aFile)
|
|
|
|
{
|
2000-09-14 03:57:52 +04:00
|
|
|
nsCOMPtr<nsILocalFile> localFile (do_CreateInstance(NS_LOCAL_FILE_CONTRACTID));
|
2003-01-12 04:14:35 +03:00
|
|
|
if (!localFile)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
localFile->InitWithPath(nsDependentString(platformAppPath));
|
|
|
|
*aFile = localFile;
|
|
|
|
NS_IF_ADDREF(*aFile);
|
2000-06-23 08:43:13 +04:00
|
|
|
|
2003-01-12 04:14:35 +03:00
|
|
|
return NS_OK;
|
2000-06-22 08:41:36 +04:00
|
|
|
}
|
2000-09-15 10:06:58 +04:00
|
|
|
///////////////////////////
|
2003-01-12 04:14:35 +03:00
|
|
|
// method overrides --> use internet config information for mime type lookup.
|
2000-09-15 10:06:58 +04:00
|
|
|
///////////////////////////
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsOSHelperAppService::GetFromExtension(const char * aFileExt, nsIMIMEInfo ** aMIMEInfo)
|
|
|
|
{
|
2003-01-12 04:14:35 +03:00
|
|
|
// first, ask our base class....
|
2000-09-15 10:06:58 +04:00
|
|
|
nsresult rv = nsExternalHelperAppService::GetFromExtension(aFileExt, aMIMEInfo);
|
2001-04-13 04:44:55 +04:00
|
|
|
if (NS_SUCCEEDED(rv) && *aMIMEInfo)
|
|
|
|
{
|
|
|
|
UpdateCreatorInfo(*aMIMEInfo);
|
|
|
|
}
|
2003-01-12 04:14:35 +03:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult nsOSHelperAppService::GetMIMEInfoForExtensionFromOS(const char * aFileExt, nsIMIMEInfo ** aMIMEInfo)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
2000-09-15 10:06:58 +04:00
|
|
|
|
2003-01-12 04:14:35 +03:00
|
|
|
// ask the internet config service to look it up for us...
|
2000-09-15 10:06:58 +04:00
|
|
|
nsCOMPtr<nsIInternetConfigService> icService (do_GetService(NS_INTERNETCONFIGSERVICE_CONTRACTID));
|
|
|
|
if (icService)
|
|
|
|
{
|
|
|
|
rv = icService->GetMIMEInfoFromExtension(aFileExt, aMIMEInfo);
|
|
|
|
// if we got an entry, don't waste time hitting IC for this information next time, store it in our
|
|
|
|
// hash table....
|
2002-02-06 06:35:31 +03:00
|
|
|
// XXX Once cache can be invalidated, add the mime info to it. See bug 121644
|
|
|
|
// if (NS_SUCCEEDED(rv) && *aMIMEInfo)
|
|
|
|
// AddMimeInfoToCache(*aMIMEInfo);
|
2000-09-15 10:06:58 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!*aMIMEInfo) rv = NS_ERROR_FAILURE;
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsOSHelperAppService::GetFromMIMEType(const char * aMIMEType, nsIMIMEInfo ** aMIMEInfo)
|
|
|
|
{
|
2003-01-12 04:14:35 +03:00
|
|
|
// first, ask our base class....
|
2000-09-15 10:06:58 +04:00
|
|
|
nsresult rv = nsExternalHelperAppService::GetFromMIMEType(aMIMEType, aMIMEInfo);
|
2001-04-13 04:44:55 +04:00
|
|
|
if (NS_SUCCEEDED(rv) && *aMIMEInfo)
|
|
|
|
{
|
|
|
|
UpdateCreatorInfo(*aMIMEInfo);
|
|
|
|
}
|
2003-01-12 04:14:35 +03:00
|
|
|
return rv;
|
|
|
|
}
|
2000-09-15 10:06:58 +04:00
|
|
|
|
2003-01-12 04:14:35 +03:00
|
|
|
nsresult nsOSHelperAppService::GetMIMEInfoForMimeTypeFromOS(const char * aMIMEType, nsIMIMEInfo ** aMIMEInfo)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
// ask the internet config service to look it up for us...
|
2000-09-15 10:06:58 +04:00
|
|
|
nsCOMPtr<nsIInternetConfigService> icService (do_GetService(NS_INTERNETCONFIGSERVICE_CONTRACTID));
|
|
|
|
if (icService)
|
|
|
|
{
|
|
|
|
rv = icService->FillInMIMEInfo(aMIMEType, nsnull, aMIMEInfo);
|
|
|
|
// if we got an entry, don't waste time hitting IC for this information next time, store it in our
|
|
|
|
// hash table....
|
2002-02-06 06:35:31 +03:00
|
|
|
// XXX Once cache can be invalidated, add the mime info to it. See bug 121644
|
|
|
|
// if (NS_SUCCEEDED(rv) && *aMIMEInfo)
|
|
|
|
// AddMimeInfoToCache(*aMIMEInfo);
|
2000-09-15 10:06:58 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!*aMIMEInfo) rv = NS_ERROR_FAILURE;
|
|
|
|
return rv;
|
|
|
|
}
|
2001-04-13 04:44:55 +04:00
|
|
|
|
|
|
|
// we never want to use a hard coded value for the creator and file type for the mac. always look these values up
|
|
|
|
// from internet config.
|
|
|
|
void nsOSHelperAppService::UpdateCreatorInfo(nsIMIMEInfo * aMIMEInfo)
|
|
|
|
{
|
|
|
|
PRUint32 macCreatorType;
|
|
|
|
PRUint32 macFileType;
|
|
|
|
aMIMEInfo->GetMacType(&macFileType);
|
|
|
|
aMIMEInfo->GetMacCreator(&macCreatorType);
|
|
|
|
|
|
|
|
if (macFileType == 0 || macCreatorType == 0)
|
|
|
|
{
|
|
|
|
// okay these values haven't been initialized yet so fetch a mime object from internet config.
|
|
|
|
nsXPIDLCString mimeType;
|
|
|
|
aMIMEInfo->GetMIMEType(getter_Copies(mimeType));
|
|
|
|
nsCOMPtr<nsIInternetConfigService> icService (do_GetService(NS_INTERNETCONFIGSERVICE_CONTRACTID));
|
|
|
|
if (icService)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIMIMEInfo> osMimeObject;
|
|
|
|
icService->FillInMIMEInfo(mimeType, nsnull, getter_AddRefs(osMimeObject));
|
|
|
|
if (osMimeObject)
|
|
|
|
{
|
|
|
|
osMimeObject->GetMacType(&macFileType);
|
|
|
|
osMimeObject->GetMacCreator(&macCreatorType);
|
|
|
|
aMIMEInfo->SetMacCreator(macCreatorType);
|
|
|
|
aMIMEInfo->SetMacType(macFileType);
|
|
|
|
} // if we got an os object
|
|
|
|
} // if we got the ic service
|
|
|
|
} // if the creator or file type hasn't been initialized yet
|
|
|
|
}
|