1
/* ***** BEGIN LICENSE BLOCK *****
2
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
4
* The contents of this file are subject to the Mozilla Public License Version
5
* 1.1 (the "License"); you may not use this file except in compliance with
6
* the License. You may obtain a copy of the License at
7
* http://www.mozilla.org/MPL/
9
* Software distributed under the License is distributed on an "AS IS" basis,
10
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11
* for the specific language governing rights and limitations under the
14
* The Original Code is nsOSHelperAppService.cpp.
16
* The Initial Developer of the Original Code is
18
* Portions created by the Initial Developer are Copyright (C) 2002
19
* the Initial Developer. All Rights Reserved.
23
* Alternatively, the contents of this file may be used under the terms of
24
* either the GNU General Public License Version 2 or later (the "GPL"), or
25
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26
* in which case the provisions of the GPL or the LGPL are applicable instead
27
* of those above. If you wish to allow use of your version of this file only
28
* under the terms of either the GPL or the LGPL, and not to allow others to
29
* use your version of this file under the terms of the MPL, indicate your
30
* decision by deleting the provisions above and replace them with the notice
31
* and other provisions required by the GPL or the LGPL. If you do not delete
32
* the provisions above, a recipient may use your version of this file under
33
* the terms of any one of the MPL, the GPL or the LGPL.
35
* ***** END LICENSE BLOCK ***** */
37
#include "nsOSHelperAppService.h"
38
#include "nsMIMEInfoBeOS.h"
39
#include "nsISupports.h"
41
#include "nsXPIDLString.h"
43
#include "nsILocalFile.h"
44
#include "nsIProcess.h"
45
#include "prenv.h" // for PR_GetEnv()
46
#include <stdlib.h> // for system()
55
#define LOG(args) PR_LOG(mLog, PR_LOG_DEBUG, args)
56
#define LOG_ENABLED() PR_LOG_TEST(mLog, PR_LOG_DEBUG)
58
nsOSHelperAppService::nsOSHelperAppService() : nsExternalHelperAppService()
62
nsOSHelperAppService::~nsOSHelperAppService()
65
NS_IMETHODIMP nsOSHelperAppService::ExternalProtocolHandlerExists(const char * aProtocolScheme, PRBool * aHandlerExists)
67
LOG(("-- nsOSHelperAppService::ExternalProtocolHandlerExists for '%s'\n",
69
// look up the protocol scheme in the MIME database
70
*aHandlerExists = PR_FALSE;
71
if (aProtocolScheme && *aProtocolScheme)
73
BString protoStr(aProtocolScheme);
74
protoStr.Prepend("application/x-vnd.Be.URL.");
76
if (protocol.SetTo(protoStr.String()) == B_OK) {
77
*aHandlerExists = PR_TRUE;
84
nsresult nsOSHelperAppService::LoadUriInternal(nsIURI * aURL)
86
LOG(("-- nsOSHelperAppService::LoadUrl\n"));
92
aURL->GetScheme(scheme);
93
BString protoStr(scheme.get());
94
protoStr.Prepend("application/x-vnd.Be.URL.");
98
// Set up the BRoster message
99
BMessage args(B_ARGV_RECEIVED);
100
args.AddInt32("argc",1);
101
args.AddString("argv",spec.get());
103
be_roster->Launch(protoStr.String(), &args);
109
nsresult nsOSHelperAppService::SetMIMEInfoForType(const char *aMIMEType, nsIMIMEInfo **_retval) {
111
LOG(("-- nsOSHelperAppService::SetMIMEInfoForType: %s\n",aMIMEType));
113
nsresult rv = NS_ERROR_FAILURE;
115
nsMIMEInfoBeOS* mimeInfo = new nsMIMEInfoBeOS();
118
BMimeType mimeType(aMIMEType);
120
mimeInfo->SetMIMEType(aMIMEType);
123
LOG((" Adding extensions:\n"));
124
if (mimeType.GetFileExtensions(&data) == B_OK) {
125
while (data.FindString("extensions",index,&strData) == B_OK) {
126
// if the file extension includes the '.' then we don't want to include that when we append
127
// it to the mime info object.
128
if (strData.ByteAt(0) == '.')
129
strData.RemoveFirst(".");
130
mimeInfo->AppendExtension(strData.String());
131
LOG((" %s\n",strData.String()));
136
char desc[B_MIME_TYPE_LENGTH + 1];
137
if (mimeType.GetShortDescription(desc) == B_OK) {
138
mimeInfo->SetDescription(NS_ConvertUTF8toUCS2(desc).get());
140
if (mimeType.GetLongDescription(desc) == B_OK) {
141
mimeInfo->SetDescription(NS_ConvertUTF8toUCS2(desc).get());
143
mimeInfo->SetDescription(NS_ConvertUTF8toUCS2(aMIMEType).get());
147
LOG((" Description: %s\n",desc));
149
//set preferred app and app description
150
char appSig[B_MIME_TYPE_LENGTH + 1];
152
if (mimeType.GetPreferredApp(appSig) == B_OK) {
153
LOG((" Got preferred ap\n"));
154
BMimeType app(appSig);
158
if ((app.GetAppHint(&ref) == B_OK) &&
159
(entry.SetTo(&ref, false) == B_OK) &&
160
(entry.GetPath(&path) == B_OK)) {
162
LOG((" Got our path!\n"));
163
nsCOMPtr<nsIFile> handlerFile;
164
rv = GetFileTokenForPath(NS_ConvertUTF8toUCS2(path.Path()).get(), getter_AddRefs(handlerFile));
166
if (NS_SUCCEEDED(rv)) {
167
mimeInfo->SetDefaultApplication(handlerFile);
168
mimeInfo->SetPreferredAction(nsIMIMEInfo::useSystemDefault);
169
mimeInfo->SetDefaultDescription(NS_ConvertUTF8toUCS2(path.Leaf()).get());
170
LOG((" Preferred App: %s\n",path.Leaf()));
176
mimeInfo->SetPreferredAction(nsIMIMEInfo::saveToDisk);
177
LOG((" No Preferred App\n"));
184
rv = NS_ERROR_FAILURE;
189
nsresult nsOSHelperAppService::GetMimeInfoFromExtension(const char *aFileExt,
190
nsIMIMEInfo ** _retval) {
191
// if the extension is null, return immediately
192
if (!aFileExt || !*aFileExt)
193
return NS_ERROR_INVALID_ARG;
195
LOG(("Here we do an extension lookup for '%s'\n", aFileExt));
197
BString fileExtToUse(aFileExt);
198
if (fileExtToUse.ByteAt(0) != '.')
199
fileExtToUse.Prepend(".");
210
// Get a list of all registered MIME types
211
if (BMimeType::GetInstalledTypes(&mimeData) == B_OK) {
212
// check to see if the given MIME type is registerred
213
while (!found && mimeData.FindString("types",mimeIndex,&mimeStr) == B_OK) {
214
if ((mimeType.SetTo(mimeStr.String()) == B_OK) &&
215
(mimeType.GetFileExtensions(&extData) == B_OK)) {
217
while (!found && extData.FindString("extensions",extIndex,&extStr) == B_OK) {
218
if (extStr.ByteAt(0) != '.')
220
if (fileExtToUse.ICompare(extStr) == 0)
229
return SetMIMEInfoForType(mimeStr.String(), _retval);
233
// Extension not found
234
return NS_ERROR_FAILURE;
237
nsresult nsOSHelperAppService::GetMimeInfoFromMIMEType(const char *aMIMEType,
238
nsIMIMEInfo ** _retval) {
239
// if the mime type is null, return immediately
240
if (!aMIMEType || !*aMIMEType)
241
return NS_ERROR_INVALID_ARG;
243
LOG(("Here we do a mimetype lookup for '%s'\n", aMIMEType));
249
// Get a list of all registerred MIME types
250
if (BMimeType::GetInstalledTypes(&data) == B_OK) {
251
// check to see if the given MIME type is registerred
252
while (!found && data.FindString("types",index,&strData) == B_OK) {
253
if (strData == aMIMEType)
259
return SetMIMEInfoForType(aMIMEType, _retval);
263
return NS_ERROR_FAILURE;
266
already_AddRefed<nsIMIMEInfo>
267
nsOSHelperAppService::GetMIMEInfoFromOS(const char *aMIMEType, const char *aFileExt, PRBool* aFound)
270
nsIMIMEInfo* mi = nsnull;
271
GetMimeInfoFromMIMEType(aMIMEType, &mi);
275
GetMimeInfoFromExtension(aFileExt, &mi);
276
if (mi && aMIMEType && *aMIMEType)
277
mi->SetMIMEType(aMIMEType);
282
mi = new nsMIMEInfoBeOS();
286
if (aMIMEType && *aMIMEType)
287
mi->SetMIMEType(aMIMEType);
288
if (aFileExt && *aFileExt)
289
mi->AppendExtension(aFileExt);
294
nsresult nsOSHelperAppService::GetFileTokenForPath(const PRUnichar* platformAppPath, nsIFile ** aFile)
296
LOG(("-- nsOSHelperAppService::GetFileTokenForPath: '%s'\n", NS_ConvertUCS2toUTF8(platformAppPath).get()));
297
nsCOMPtr<nsILocalFile> localFile (do_CreateInstance(NS_LOCAL_FILE_CONTRACTID));
300
if (!localFile) return NS_ERROR_NOT_INITIALIZED;
302
// A BPath will convert relative paths automagically
304
PRBool exists = PR_FALSE;
305
if (path.SetTo(NS_ConvertUCS2toUTF8(platformAppPath).get()) == B_OK) {
306
localFile->InitWithPath(NS_ConvertUTF8toUCS2(path.Path()));
307
localFile->Exists(&exists);
313
rv = NS_ERROR_NOT_AVAILABLE;
317
NS_IF_ADDREF(*aFile);