1 /* -*- Mode: C++; tab-width: 3; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2  *
3  * This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #include "nsMIMEInfoUnix.h"
8 #include "nsGNOMERegistry.h"
9 #include "nsIGIOService.h"
10 #include "nsNetCID.h"
11 #include "nsIIOService.h"
12 #ifdef MOZ_ENABLE_DBUS
13 #  include "nsDBusHandlerApp.h"
14 #endif
15 
LoadUriInternal(nsIURI * aURI)16 nsresult nsMIMEInfoUnix::LoadUriInternal(nsIURI* aURI) {
17   return nsGNOMERegistry::LoadURL(aURI);
18 }
19 
20 NS_IMETHODIMP
GetHasDefaultHandler(bool * _retval)21 nsMIMEInfoUnix::GetHasDefaultHandler(bool* _retval) {
22   // if mDefaultApplication is set, it means the application has been set from
23   // either /etc/mailcap or ${HOME}/.mailcap, in which case we don't want to
24   // give the GNOME answer.
25   if (mDefaultApplication) return nsMIMEInfoImpl::GetHasDefaultHandler(_retval);
26 
27   *_retval = false;
28 
29   if (mClass == eProtocolInfo) {
30     *_retval = nsGNOMERegistry::HandlerExists(mSchemeOrType.get());
31   } else {
32     RefPtr<nsMIMEInfoBase> mimeInfo =
33         nsGNOMERegistry::GetFromType(mSchemeOrType);
34     if (!mimeInfo) {
35       nsAutoCString ext;
36       nsresult rv = GetPrimaryExtension(ext);
37       if (NS_SUCCEEDED(rv)) {
38         mimeInfo = nsGNOMERegistry::GetFromExtension(ext);
39       }
40     }
41     if (mimeInfo) *_retval = true;
42   }
43 
44   if (*_retval) return NS_OK;
45 
46   return NS_OK;
47 }
48 
LaunchDefaultWithFile(nsIFile * aFile)49 nsresult nsMIMEInfoUnix::LaunchDefaultWithFile(nsIFile* aFile) {
50   // if mDefaultApplication is set, it means the application has been set from
51   // either /etc/mailcap or ${HOME}/.mailcap, in which case we don't want to
52   // give the GNOME answer.
53   if (mDefaultApplication) return nsMIMEInfoImpl::LaunchDefaultWithFile(aFile);
54 
55   nsAutoCString nativePath;
56   aFile->GetNativePath(nativePath);
57 
58   nsCOMPtr<nsIGIOService> giovfs = do_GetService(NS_GIOSERVICE_CONTRACTID);
59   if (!giovfs) {
60     return NS_ERROR_FAILURE;
61   }
62 
63   // nsGIOMimeApp->Launch wants a URI string instead of local file
64   nsresult rv;
65   nsCOMPtr<nsIIOService> ioservice =
66       do_GetService(NS_IOSERVICE_CONTRACTID, &rv);
67   NS_ENSURE_SUCCESS(rv, rv);
68   nsCOMPtr<nsIURI> uri;
69   rv = ioservice->NewFileURI(aFile, getter_AddRefs(uri));
70   NS_ENSURE_SUCCESS(rv, rv);
71 
72   nsCOMPtr<nsIHandlerApp> app;
73   if (NS_FAILED(
74           giovfs->GetAppForMimeType(mSchemeOrType, getter_AddRefs(app))) ||
75       !app) {
76     return NS_ERROR_FILE_NOT_FOUND;
77   }
78 
79   return app->LaunchWithURI(uri, nullptr);
80 }
81