Line data Source code
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 : #include "nsAutoPtr.h"
13 : #ifdef MOZ_ENABLE_DBUS
14 : #include "nsDBusHandlerApp.h"
15 : #endif
16 :
17 : nsresult
18 0 : nsMIMEInfoUnix::LoadUriInternal(nsIURI * aURI)
19 : {
20 0 : return nsGNOMERegistry::LoadURL(aURI);
21 : }
22 :
23 : NS_IMETHODIMP
24 2 : nsMIMEInfoUnix::GetHasDefaultHandler(bool *_retval)
25 : {
26 : // if mDefaultApplication is set, it means the application has been set from
27 : // either /etc/mailcap or ${HOME}/.mailcap, in which case we don't want to
28 : // give the GNOME answer.
29 2 : if (mDefaultApplication)
30 0 : return nsMIMEInfoImpl::GetHasDefaultHandler(_retval);
31 :
32 2 : *_retval = false;
33 :
34 2 : if (mClass == eProtocolInfo) {
35 0 : *_retval = nsGNOMERegistry::HandlerExists(mSchemeOrType.get());
36 : } else {
37 4 : RefPtr<nsMIMEInfoBase> mimeInfo = nsGNOMERegistry::GetFromType(mSchemeOrType);
38 2 : if (!mimeInfo) {
39 0 : nsAutoCString ext;
40 0 : nsresult rv = GetPrimaryExtension(ext);
41 0 : if (NS_SUCCEEDED(rv)) {
42 0 : mimeInfo = nsGNOMERegistry::GetFromExtension(ext);
43 : }
44 : }
45 2 : if (mimeInfo)
46 2 : *_retval = true;
47 : }
48 :
49 2 : if (*_retval)
50 2 : return NS_OK;
51 :
52 : #if defined(MOZ_ENABLE_CONTENTACTION)
53 : ContentAction::Action action =
54 : ContentAction::Action::defaultActionForFile(QUrl(), QString(mSchemeOrType.get()));
55 : if (action.isValid()) {
56 : *_retval = true;
57 : return NS_OK;
58 : }
59 : #endif
60 :
61 0 : return NS_OK;
62 : }
63 :
64 : nsresult
65 0 : nsMIMEInfoUnix::LaunchDefaultWithFile(nsIFile *aFile)
66 : {
67 : // if mDefaultApplication is set, it means the application has been set from
68 : // either /etc/mailcap or ${HOME}/.mailcap, in which case we don't want to
69 : // give the GNOME answer.
70 0 : if (mDefaultApplication)
71 0 : return nsMIMEInfoImpl::LaunchDefaultWithFile(aFile);
72 :
73 0 : nsAutoCString nativePath;
74 0 : aFile->GetNativePath(nativePath);
75 :
76 : #if defined(MOZ_ENABLE_CONTENTACTION)
77 : QUrl uri = QUrl::fromLocalFile(QString::fromUtf8(nativePath.get()));
78 : ContentAction::Action action =
79 : ContentAction::Action::defaultActionForFile(uri, QString(mSchemeOrType.get()));
80 : if (action.isValid()) {
81 : action.trigger();
82 : return NS_OK;
83 : }
84 : return NS_ERROR_FAILURE;
85 : #endif
86 :
87 0 : nsCOMPtr<nsIGIOService> giovfs = do_GetService(NS_GIOSERVICE_CONTRACTID);
88 0 : if (!giovfs) {
89 0 : return NS_ERROR_FAILURE;
90 : }
91 :
92 : // nsGIOMimeApp->Launch wants a URI string instead of local file
93 : nsresult rv;
94 0 : nsCOMPtr<nsIIOService> ioservice = do_GetService(NS_IOSERVICE_CONTRACTID, &rv);
95 0 : NS_ENSURE_SUCCESS(rv, rv);
96 0 : nsCOMPtr<nsIURI> uri;
97 0 : rv = ioservice->NewFileURI(aFile, getter_AddRefs(uri));
98 0 : NS_ENSURE_SUCCESS(rv, rv);
99 0 : nsAutoCString uriSpec;
100 0 : uri->GetSpec(uriSpec);
101 :
102 0 : nsCOMPtr<nsIGIOMimeApp> app;
103 0 : if (NS_FAILED(giovfs->GetAppForMimeType(mSchemeOrType, getter_AddRefs(app))) || !app) {
104 0 : return NS_ERROR_FILE_NOT_FOUND;
105 : }
106 :
107 0 : return app->Launch(uriSpec);
108 9 : }
109 :
110 : #if defined(MOZ_ENABLE_CONTENTACTION)
111 : NS_IMETHODIMP
112 : nsMIMEInfoUnix::GetPossibleApplicationHandlers(nsIMutableArray ** aPossibleAppHandlers)
113 : {
114 : if (!mPossibleApplications) {
115 : mPossibleApplications = do_CreateInstance(NS_ARRAY_CONTRACTID);
116 :
117 : if (!mPossibleApplications)
118 : return NS_ERROR_OUT_OF_MEMORY;
119 :
120 : QList<ContentAction::Action> actions =
121 : ContentAction::Action::actionsForFile(QUrl(), QString(mSchemeOrType.get()));
122 :
123 : for (int i = 0; i < actions.size(); ++i) {
124 : nsContentHandlerApp* app =
125 : new nsContentHandlerApp(nsString((char16_t*)actions[i].name().data()),
126 : mSchemeOrType, actions[i]);
127 : mPossibleApplications->AppendElement(app, false);
128 : }
129 : }
130 :
131 : *aPossibleAppHandlers = mPossibleApplications;
132 : NS_ADDREF(*aPossibleAppHandlers);
133 : return NS_OK;
134 : }
135 : #endif
136 :
|