~ubuntu-branches/ubuntu/precise/kompozer/precise

« back to all changes in this revision

Viewing changes to mozilla/uriloader/exthandler/beos/nsOSHelperAppService.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Anthony Yarusso
  • Date: 2007-08-27 01:11:03 UTC
  • Revision ID: james.westby@ubuntu.com-20070827011103-2jgf4s6532gqu2ka
Tags: upstream-0.7.10
ImportĀ upstreamĀ versionĀ 0.7.10

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* ***** BEGIN LICENSE BLOCK ***** 
 
2
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1 
 
3
 * 
 
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/ 
 
8
 * 
 
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 
 
12
 * License. 
 
13
 * 
 
14
 * The Original Code is nsOSHelperAppService.cpp. 
 
15
 * 
 
16
 * The Initial Developer of the Original Code is 
 
17
 * Paul Ashford. 
 
18
 * Portions created by the Initial Developer are Copyright (C) 2002
 
19
 * the Initial Developer. All Rights Reserved. 
 
20
 * 
 
21
 * Contributor(s): 
 
22
 * 
 
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. 
 
34
 * 
 
35
 * ***** END LICENSE BLOCK ***** */ 
 
36
 
 
37
#include "nsOSHelperAppService.h"
 
38
#include "nsMIMEInfoBeOS.h"
 
39
#include "nsISupports.h"
 
40
#include "nsString.h"
 
41
#include "nsXPIDLString.h"
 
42
#include "nsIURL.h"
 
43
#include "nsILocalFile.h"
 
44
#include "nsIProcess.h"
 
45
#include "prenv.h"      // for PR_GetEnv()
 
46
#include <stdlib.h>             // for system()
 
47
 
 
48
#include <Message.h>
 
49
#include <Mime.h>
 
50
#include <String.h>
 
51
#include <Path.h>
 
52
#include <Entry.h>
 
53
#include <Roster.h>
 
54
 
 
55
#define LOG(args) PR_LOG(mLog, PR_LOG_DEBUG, args)
 
56
#define LOG_ENABLED() PR_LOG_TEST(mLog, PR_LOG_DEBUG)
 
57
 
 
58
nsOSHelperAppService::nsOSHelperAppService() : nsExternalHelperAppService()
 
59
{
 
60
}
 
61
 
 
62
nsOSHelperAppService::~nsOSHelperAppService()
 
63
{}
 
64
 
 
65
NS_IMETHODIMP nsOSHelperAppService::ExternalProtocolHandlerExists(const char * aProtocolScheme, PRBool * aHandlerExists)
 
66
{
 
67
        LOG(("-- nsOSHelperAppService::ExternalProtocolHandlerExists for '%s'\n",
 
68
             aProtocolScheme));
 
69
        // look up the protocol scheme in the MIME database
 
70
        *aHandlerExists = PR_FALSE;
 
71
        if (aProtocolScheme && *aProtocolScheme)
 
72
        {
 
73
                BString protoStr(aProtocolScheme);
 
74
                protoStr.Prepend("application/x-vnd.Be.URL.");
 
75
                BMimeType protocol;
 
76
                if (protocol.SetTo(protoStr.String()) == B_OK) {
 
77
                        *aHandlerExists = PR_TRUE;
 
78
                }
 
79
        }
 
80
 
 
81
        return NS_OK;
 
82
}
 
83
 
 
84
nsresult nsOSHelperAppService::LoadUriInternal(nsIURI * aURL)
 
85
{
 
86
        LOG(("-- nsOSHelperAppService::LoadUrl\n"));
 
87
        nsresult rv = NS_OK;
 
88
 
 
89
        if (aURL) {
 
90
                // Get the Protocol
 
91
                nsCAutoString scheme;
 
92
                aURL->GetScheme(scheme);
 
93
                BString protoStr(scheme.get());
 
94
                protoStr.Prepend("application/x-vnd.Be.URL.");
 
95
                // Get the Spec
 
96
                nsCAutoString spec;
 
97
                aURL->GetSpec(spec);
 
98
                // Set up the BRoster message
 
99
                BMessage args(B_ARGV_RECEIVED);
 
100
                args.AddInt32("argc",1);
 
101
                args.AddString("argv",spec.get());
 
102
 
 
103
                be_roster->Launch(protoStr.String(), &args);
 
104
        }
 
105
        return rv;
 
106
}
 
107
 
 
108
 
 
109
nsresult nsOSHelperAppService::SetMIMEInfoForType(const char *aMIMEType, nsIMIMEInfo **_retval) {
 
110
 
 
111
        LOG(("-- nsOSHelperAppService::SetMIMEInfoForType: %s\n",aMIMEType));
 
112
 
 
113
        nsresult rv = NS_ERROR_FAILURE;
 
114
 
 
115
        nsMIMEInfoBeOS* mimeInfo = new nsMIMEInfoBeOS();
 
116
        if (mimeInfo) {
 
117
                NS_ADDREF(mimeInfo);
 
118
                BMimeType mimeType(aMIMEType);
 
119
                BMessage data;
 
120
                mimeInfo->SetMIMEType(aMIMEType);
 
121
                int32 index = 0;
 
122
                BString strData;
 
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()));
 
132
                                index++;
 
133
                        }
 
134
                }
 
135
 
 
136
                char desc[B_MIME_TYPE_LENGTH + 1];
 
137
                if (mimeType.GetShortDescription(desc) == B_OK) {
 
138
                        mimeInfo->SetDescription(NS_ConvertUTF8toUCS2(desc).get());
 
139
                } else {
 
140
                        if (mimeType.GetLongDescription(desc) == B_OK) {
 
141
                                mimeInfo->SetDescription(NS_ConvertUTF8toUCS2(desc).get());
 
142
                        } else {
 
143
                                mimeInfo->SetDescription(NS_ConvertUTF8toUCS2(aMIMEType).get());
 
144
                        }
 
145
                }
 
146
                
 
147
                LOG(("    Description: %s\n",desc));
 
148
 
 
149
                //set preferred app and app description
 
150
                char appSig[B_MIME_TYPE_LENGTH + 1];
 
151
                bool doSave = true;
 
152
                if (mimeType.GetPreferredApp(appSig) == B_OK) {
 
153
                        LOG(("    Got preferred ap\n"));
 
154
                        BMimeType app(appSig);
 
155
                        entry_ref ref;
 
156
                        BEntry entry;
 
157
                        BPath path;
 
158
                        if ((app.GetAppHint(&ref) == B_OK) &&
 
159
                                (entry.SetTo(&ref, false) == B_OK) &&
 
160
                                (entry.GetPath(&path) == B_OK)) {
 
161
 
 
162
                                LOG(("    Got our path!\n"));
 
163
                                nsCOMPtr<nsIFile> handlerFile;
 
164
                                rv = GetFileTokenForPath(NS_ConvertUTF8toUCS2(path.Path()).get(), getter_AddRefs(handlerFile));
 
165
 
 
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()));
 
171
                                        doSave = false;
 
172
                                }
 
173
                        }
 
174
                }
 
175
                if (doSave) {
 
176
                        mimeInfo->SetPreferredAction(nsIMIMEInfo::saveToDisk);
 
177
                        LOG(("    No Preferred App\n"));
 
178
                }
 
179
 
 
180
                *_retval = mimeInfo;
 
181
                rv = NS_OK;
 
182
        }
 
183
        else
 
184
                rv = NS_ERROR_FAILURE;
 
185
 
 
186
        return rv;
 
187
}
 
188
 
 
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;
 
194
 
 
195
        LOG(("Here we do an extension lookup for '%s'\n", aFileExt));
 
196
 
 
197
        BString fileExtToUse(aFileExt);
 
198
        if (fileExtToUse.ByteAt(0) != '.')
 
199
                fileExtToUse.Prepend(".");
 
200
 
 
201
 
 
202
        BMessage mimeData;
 
203
        BMessage extData;
 
204
        BMimeType mimeType;
 
205
        int32 mimeIndex = 0;
 
206
        int32 extIndex = 0;
 
207
        bool found = false;
 
208
        BString mimeStr;
 
209
        BString extStr;
 
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)) {
 
216
                                extIndex = 0;
 
217
                                while (!found && extData.FindString("extensions",extIndex,&extStr) == B_OK) {
 
218
                                        if (extStr.ByteAt(0) != '.')
 
219
                                                extStr.Prepend(".");
 
220
                                        if (fileExtToUse.ICompare(extStr) == 0)
 
221
                                                found = true;
 
222
                                        else
 
223
                                                extIndex++;
 
224
                                }
 
225
                        }
 
226
                        mimeIndex++;
 
227
                }
 
228
                if (found) {
 
229
                        return SetMIMEInfoForType(mimeStr.String(), _retval);
 
230
                }
 
231
        }
 
232
 
 
233
        // Extension not found
 
234
        return NS_ERROR_FAILURE;
 
235
}
 
236
 
 
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;
 
242
 
 
243
        LOG(("Here we do a mimetype lookup for '%s'\n", aMIMEType));
 
244
 
 
245
        BMessage data;
 
246
        int32 index = 0;
 
247
        bool found = false;
 
248
        BString strData;
 
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)
 
254
                                found = true;
 
255
                        else
 
256
                                index++;
 
257
                }
 
258
                if (found) {
 
259
                        return SetMIMEInfoForType(aMIMEType, _retval);
 
260
                }
 
261
        }
 
262
 
 
263
        return NS_ERROR_FAILURE;
 
264
}
 
265
 
 
266
already_AddRefed<nsIMIMEInfo>
 
267
nsOSHelperAppService::GetMIMEInfoFromOS(const char *aMIMEType, const char *aFileExt, PRBool* aFound)
 
268
{
 
269
  *aFound = PR_TRUE;
 
270
  nsIMIMEInfo* mi = nsnull;
 
271
  GetMimeInfoFromMIMEType(aMIMEType, &mi);
 
272
  if (mi)
 
273
    return mi;
 
274
 
 
275
  GetMimeInfoFromExtension(aFileExt, &mi);
 
276
  if (mi && aMIMEType && *aMIMEType)
 
277
    mi->SetMIMEType(aMIMEType);
 
278
  if (mi)
 
279
    return mi;
 
280
 
 
281
  *aFound = PR_FALSE;
 
282
  mi = new nsMIMEInfoBeOS();
 
283
  if (!mi)
 
284
    return nsnull;
 
285
  NS_ADDREF(mi);
 
286
  if (aMIMEType && *aMIMEType)
 
287
    mi->SetMIMEType(aMIMEType);
 
288
  if (aFileExt && *aFileExt)
 
289
    mi->AppendExtension(aFileExt);
 
290
 
 
291
  return mi;
 
292
}
 
293
 
 
294
nsresult nsOSHelperAppService::GetFileTokenForPath(const PRUnichar* platformAppPath, nsIFile ** aFile)
 
295
{
 
296
        LOG(("-- nsOSHelperAppService::GetFileTokenForPath: '%s'\n", NS_ConvertUCS2toUTF8(platformAppPath).get()));
 
297
        nsCOMPtr<nsILocalFile> localFile (do_CreateInstance(NS_LOCAL_FILE_CONTRACTID));
 
298
        nsresult rv = NS_OK;
 
299
 
 
300
        if (!localFile) return NS_ERROR_NOT_INITIALIZED;
 
301
 
 
302
        // A BPath will convert relative paths automagically
 
303
        BPath path;
 
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);
 
308
        }
 
309
 
 
310
        if (exists) {
 
311
                rv = NS_OK;
 
312
        } else {
 
313
                rv = NS_ERROR_NOT_AVAILABLE;
 
314
        }
 
315
 
 
316
        *aFile = localFile;
 
317
        NS_IF_ADDREF(*aFile);
 
318
 
 
319
        return rv;
 
320
}