2002-10-09 05:25:42 +04:00
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* The Original Code is nsOSHelperAppService.cpp.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Paul Ashford.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2002
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
*
|
|
|
|
* 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 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 ***** */
|
|
|
|
|
|
|
|
#include "nsOSHelperAppService.h"
|
2004-02-24 01:49:28 +03:00
|
|
|
#include "nsMIMEInfoBeOS.h"
|
2002-10-09 05:25:42 +04:00
|
|
|
#include "nsISupports.h"
|
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsXPIDLString.h"
|
|
|
|
#include "nsIURL.h"
|
|
|
|
#include "nsILocalFile.h"
|
|
|
|
#include "nsIProcess.h"
|
|
|
|
#include "prenv.h" // for PR_GetEnv()
|
|
|
|
#include <stdlib.h> // for system()
|
|
|
|
|
|
|
|
#include <Message.h>
|
|
|
|
#include <Mime.h>
|
|
|
|
#include <String.h>
|
|
|
|
#include <Path.h>
|
|
|
|
#include <Entry.h>
|
|
|
|
#include <Roster.h>
|
|
|
|
|
2003-07-25 20:48:00 +04:00
|
|
|
#define LOG(args) PR_LOG(mLog, PR_LOG_DEBUG, args)
|
|
|
|
#define LOG_ENABLED() PR_LOG_TEST(mLog, PR_LOG_DEBUG)
|
2002-10-09 05:25:42 +04:00
|
|
|
|
|
|
|
nsOSHelperAppService::nsOSHelperAppService() : nsExternalHelperAppService()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsOSHelperAppService::~nsOSHelperAppService()
|
|
|
|
{}
|
|
|
|
|
2007-05-25 19:17:44 +04:00
|
|
|
nsresult nsOSHelperAppService::OSProtocolHandlerExists(const char * aProtocolScheme, PRBool * aHandlerExists)
|
2002-10-09 05:25:42 +04:00
|
|
|
{
|
2007-05-25 19:17:44 +04:00
|
|
|
LOG(("-- nsOSHelperAppService::OSProtocolHandlerExists for '%s'\n",
|
2002-10-09 05:25:42 +04:00
|
|
|
aProtocolScheme));
|
|
|
|
// look up the protocol scheme in the MIME database
|
|
|
|
*aHandlerExists = PR_FALSE;
|
|
|
|
if (aProtocolScheme && *aProtocolScheme)
|
|
|
|
{
|
|
|
|
BString protoStr(aProtocolScheme);
|
|
|
|
protoStr.Prepend("application/x-vnd.Be.URL.");
|
|
|
|
BMimeType protocol;
|
2005-04-12 20:05:25 +04:00
|
|
|
if (protocol.SetTo(protoStr.String()) == B_OK)
|
|
|
|
{
|
|
|
|
if (protocol.IsInstalled())
|
|
|
|
*aHandlerExists = PR_TRUE;
|
|
|
|
}
|
|
|
|
if ((!*aHandlerExists) && (!strcmp("mailto", aProtocolScheme)))
|
|
|
|
{
|
|
|
|
// mailto link, no x-vnd.Be.URL.mailto entry
|
|
|
|
if (protocol.SetTo("text/x-email") == B_OK)
|
|
|
|
{
|
|
|
|
if (protocol.IsInstalled())
|
|
|
|
*aHandlerExists = PR_TRUE;
|
|
|
|
}
|
2002-10-09 05:25:42 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-06-11 19:47:57 +04:00
|
|
|
nsresult nsOSHelperAppService::SetMIMEInfoForType(const char *aMIMEType, nsMIMEInfoBeOS**_retval) {
|
2002-10-09 05:25:42 +04:00
|
|
|
|
|
|
|
LOG(("-- nsOSHelperAppService::SetMIMEInfoForType: %s\n",aMIMEType));
|
|
|
|
|
|
|
|
nsresult rv = NS_ERROR_FAILURE;
|
|
|
|
|
2004-04-16 00:56:37 +04:00
|
|
|
nsMIMEInfoBeOS* mimeInfo = new nsMIMEInfoBeOS(aMIMEType);
|
2002-10-09 05:25:42 +04:00
|
|
|
if (mimeInfo) {
|
2004-02-24 01:49:28 +03:00
|
|
|
NS_ADDREF(mimeInfo);
|
2002-10-09 05:25:42 +04:00
|
|
|
BMimeType mimeType(aMIMEType);
|
|
|
|
BMessage data;
|
|
|
|
int32 index = 0;
|
|
|
|
BString strData;
|
|
|
|
LOG((" Adding extensions:\n"));
|
|
|
|
if (mimeType.GetFileExtensions(&data) == B_OK) {
|
|
|
|
while (data.FindString("extensions",index,&strData) == B_OK) {
|
|
|
|
// if the file extension includes the '.' then we don't want to include that when we append
|
|
|
|
// it to the mime info object.
|
|
|
|
if (strData.ByteAt(0) == '.')
|
|
|
|
strData.RemoveFirst(".");
|
2004-04-16 00:56:37 +04:00
|
|
|
mimeInfo->AppendExtension(nsDependentCString(strData.String()));
|
2002-10-09 05:25:42 +04:00
|
|
|
LOG((" %s\n",strData.String()));
|
|
|
|
index++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
char desc[B_MIME_TYPE_LENGTH + 1];
|
|
|
|
if (mimeType.GetShortDescription(desc) == B_OK) {
|
2006-02-03 17:18:39 +03:00
|
|
|
mimeInfo->SetDescription(NS_ConvertUTF8toUTF16(desc));
|
2002-10-09 05:25:42 +04:00
|
|
|
} else {
|
|
|
|
if (mimeType.GetLongDescription(desc) == B_OK) {
|
2006-02-03 17:18:39 +03:00
|
|
|
mimeInfo->SetDescription(NS_ConvertUTF8toUTF16(desc));
|
2002-10-09 05:25:42 +04:00
|
|
|
} else {
|
2006-02-03 17:18:39 +03:00
|
|
|
mimeInfo->SetDescription(NS_ConvertUTF8toUTF16(aMIMEType));
|
2002-10-09 05:25:42 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG((" Description: %s\n",desc));
|
|
|
|
|
|
|
|
//set preferred app and app description
|
|
|
|
char appSig[B_MIME_TYPE_LENGTH + 1];
|
|
|
|
bool doSave = true;
|
|
|
|
if (mimeType.GetPreferredApp(appSig) == B_OK) {
|
|
|
|
LOG((" Got preferred ap\n"));
|
|
|
|
BMimeType app(appSig);
|
|
|
|
entry_ref ref;
|
|
|
|
BEntry entry;
|
|
|
|
BPath path;
|
|
|
|
if ((app.GetAppHint(&ref) == B_OK) &&
|
|
|
|
(entry.SetTo(&ref, false) == B_OK) &&
|
|
|
|
(entry.GetPath(&path) == B_OK)) {
|
|
|
|
|
|
|
|
LOG((" Got our path!\n"));
|
|
|
|
nsCOMPtr<nsIFile> handlerFile;
|
2006-02-03 17:18:39 +03:00
|
|
|
rv = GetFileTokenForPath(NS_ConvertUTF8toUTF16(path.Path()).get(), getter_AddRefs(handlerFile));
|
2002-10-09 05:25:42 +04:00
|
|
|
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2004-02-24 01:49:28 +03:00
|
|
|
mimeInfo->SetDefaultApplication(handlerFile);
|
2003-07-25 20:48:00 +04:00
|
|
|
mimeInfo->SetPreferredAction(nsIMIMEInfo::useSystemDefault);
|
2006-02-03 17:18:39 +03:00
|
|
|
mimeInfo->SetDefaultDescription(NS_ConvertUTF8toUTF16(path.Leaf()));
|
2002-10-09 05:25:42 +04:00
|
|
|
LOG((" Preferred App: %s\n",path.Leaf()));
|
|
|
|
doSave = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (doSave) {
|
|
|
|
mimeInfo->SetPreferredAction(nsIMIMEInfo::saveToDisk);
|
|
|
|
LOG((" No Preferred App\n"));
|
|
|
|
}
|
|
|
|
|
|
|
|
*_retval = mimeInfo;
|
|
|
|
rv = NS_OK;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
rv = NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2003-07-25 20:48:00 +04:00
|
|
|
nsresult nsOSHelperAppService::GetMimeInfoFromExtension(const char *aFileExt,
|
2004-06-11 19:47:57 +04:00
|
|
|
nsMIMEInfoBeOS ** _retval) {
|
2002-10-09 05:25:42 +04:00
|
|
|
// if the extension is null, return immediately
|
2003-01-12 04:14:35 +03:00
|
|
|
if (!aFileExt || !*aFileExt)
|
2002-10-09 05:25:42 +04:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
|
|
|
|
LOG(("Here we do an extension lookup for '%s'\n", aFileExt));
|
|
|
|
|
|
|
|
BMimeType mimeType;
|
2006-02-20 21:46:35 +03:00
|
|
|
|
|
|
|
if (BMimeType::GuessMimeType(aFileExt, &mimeType) == B_OK)
|
|
|
|
return SetMIMEInfoForType(mimeType.Type(), _retval);
|
2002-10-09 05:25:42 +04:00
|
|
|
|
2003-07-26 22:58:13 +04:00
|
|
|
// Extension not found
|
|
|
|
return NS_ERROR_FAILURE;
|
2002-10-09 05:25:42 +04:00
|
|
|
}
|
|
|
|
|
2003-07-25 20:48:00 +04:00
|
|
|
nsresult nsOSHelperAppService::GetMimeInfoFromMIMEType(const char *aMIMEType,
|
2004-06-11 19:47:57 +04:00
|
|
|
nsMIMEInfoBeOS ** _retval) {
|
2002-10-09 05:25:42 +04:00
|
|
|
// if the mime type is null, return immediately
|
2003-07-25 20:48:00 +04:00
|
|
|
if (!aMIMEType || !*aMIMEType)
|
2002-10-09 05:25:42 +04:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
|
|
|
|
LOG(("Here we do a mimetype lookup for '%s'\n", aMIMEType));
|
2006-02-20 21:46:35 +03:00
|
|
|
|
|
|
|
BMimeType mimeType(aMIMEType);
|
|
|
|
if (mimeType.IsInstalled())
|
|
|
|
return SetMIMEInfoForType(aMIMEType, _retval);
|
|
|
|
|
2003-07-26 22:58:13 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
2002-10-09 05:25:42 +04:00
|
|
|
}
|
|
|
|
|
2003-07-25 20:48:00 +04:00
|
|
|
already_AddRefed<nsIMIMEInfo>
|
2004-04-16 00:56:37 +04:00
|
|
|
nsOSHelperAppService::GetMIMEInfoFromOS(const nsACString& aMIMEType, const nsACString& aFileExt, PRBool* aFound)
|
2003-07-25 20:48:00 +04:00
|
|
|
{
|
2003-11-15 18:01:39 +03:00
|
|
|
*aFound = PR_TRUE;
|
2004-06-11 19:47:57 +04:00
|
|
|
nsMIMEInfoBeOS* mi = nsnull;
|
2004-04-16 00:56:37 +04:00
|
|
|
const nsCString& flatType = PromiseFlatCString(aMIMEType);
|
|
|
|
const nsCString& flatExt = PromiseFlatCString(aFileExt);
|
|
|
|
GetMimeInfoFromMIMEType(flatType.get(), &mi);
|
2003-07-25 20:48:00 +04:00
|
|
|
if (mi)
|
|
|
|
return mi;
|
|
|
|
|
2004-04-16 00:56:37 +04:00
|
|
|
GetMimeInfoFromExtension(flatExt.get(), &mi);
|
|
|
|
if (mi && !aMIMEType.IsEmpty())
|
2003-07-25 20:48:00 +04:00
|
|
|
mi->SetMIMEType(aMIMEType);
|
2003-11-15 18:01:39 +03:00
|
|
|
if (mi)
|
|
|
|
return mi;
|
|
|
|
|
|
|
|
*aFound = PR_FALSE;
|
2004-06-11 19:47:57 +04:00
|
|
|
mi = new nsMIMEInfoBeOS(flatType);
|
2003-11-15 18:01:39 +03:00
|
|
|
if (!mi)
|
|
|
|
return nsnull;
|
2004-01-28 17:48:22 +03:00
|
|
|
NS_ADDREF(mi);
|
2004-04-16 00:56:37 +04:00
|
|
|
if (!aFileExt.IsEmpty())
|
2003-11-15 18:01:39 +03:00
|
|
|
mi->AppendExtension(aFileExt);
|
|
|
|
|
2003-07-25 20:48:00 +04:00
|
|
|
return mi;
|
|
|
|
}
|
2007-07-22 22:40:24 +04:00
|
|
|
|
2008-02-20 07:46:35 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsOSHelperAppService::GetProtocolHandlerInfoFromOS(const nsACString &aScheme,
|
|
|
|
PRBool *found,
|
|
|
|
nsIHandlerInfo **_retval)
|
2007-07-22 22:40:24 +04:00
|
|
|
{
|
|
|
|
NS_ASSERTION(!aScheme.IsEmpty(), "No scheme was specified!");
|
|
|
|
|
|
|
|
nsresult rv = OSProtocolHandlerExists(nsPromiseFlatCString(aScheme).get(),
|
2007-07-31 03:33:16 +04:00
|
|
|
found);
|
|
|
|
if (NS_FAILED(rv))
|
2008-02-20 07:46:35 +03:00
|
|
|
return rv;
|
2007-07-22 22:40:24 +04:00
|
|
|
|
2007-07-26 00:40:41 +04:00
|
|
|
nsMIMEInfoBeOS *handlerInfo =
|
|
|
|
new nsMIMEInfoBeOS(aScheme, nsMIMEInfoBase::eProtocolInfo);
|
2008-02-20 09:10:33 +03:00
|
|
|
NS_ENSURE_TRUE(handlerInfo, NS_ERROR_OUT_OF_MEMORY);
|
2008-02-20 07:46:35 +03:00
|
|
|
NS_ADDREF(*_retval = handlerInfo);
|
2007-07-22 22:40:24 +04:00
|
|
|
|
2007-07-31 03:33:16 +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:35 +03:00
|
|
|
return rv;
|
2007-07-31 03:33:16 +04:00
|
|
|
}
|
|
|
|
|
2007-07-22 22:40:24 +04:00
|
|
|
nsAutoString desc;
|
|
|
|
GetApplicationDescription(aScheme, desc);
|
|
|
|
handlerInfo->SetDefaultDescription(desc);
|
|
|
|
|
2008-02-20 07:46:35 +03:00
|
|
|
return rv;
|
2007-07-22 22:40:24 +04:00
|
|
|
}
|
|
|
|
|