|
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 #ifdef MOZ_WIDGET_QT |
|
8 #include <QDesktopServices> |
|
9 #include <QUrl> |
|
10 #include <QString> |
|
11 #if (MOZ_ENABLE_CONTENTACTION) |
|
12 #include <contentaction/contentaction.h> |
|
13 #include "nsContentHandlerApp.h" |
|
14 #endif |
|
15 #endif |
|
16 |
|
17 #include "nsMIMEInfoUnix.h" |
|
18 #include "nsGNOMERegistry.h" |
|
19 #include "nsIGIOService.h" |
|
20 #include "nsNetCID.h" |
|
21 #include "nsIIOService.h" |
|
22 #include "nsIGnomeVFSService.h" |
|
23 #include "nsAutoPtr.h" |
|
24 #ifdef MOZ_ENABLE_DBUS |
|
25 #include "nsDBusHandlerApp.h" |
|
26 #endif |
|
27 |
|
28 nsresult |
|
29 nsMIMEInfoUnix::LoadUriInternal(nsIURI * aURI) |
|
30 { |
|
31 nsresult rv = nsGNOMERegistry::LoadURL(aURI); |
|
32 |
|
33 #ifdef MOZ_WIDGET_QT |
|
34 if (NS_FAILED(rv)) { |
|
35 nsAutoCString spec; |
|
36 aURI->GetAsciiSpec(spec); |
|
37 if (QDesktopServices::openUrl(QUrl(spec.get()))) { |
|
38 rv = NS_OK; |
|
39 } |
|
40 } |
|
41 #endif |
|
42 |
|
43 return rv; |
|
44 } |
|
45 |
|
46 NS_IMETHODIMP |
|
47 nsMIMEInfoUnix::GetHasDefaultHandler(bool *_retval) |
|
48 { |
|
49 // if mDefaultApplication is set, it means the application has been set from |
|
50 // either /etc/mailcap or ${HOME}/.mailcap, in which case we don't want to |
|
51 // give the GNOME answer. |
|
52 if (mDefaultApplication) |
|
53 return nsMIMEInfoImpl::GetHasDefaultHandler(_retval); |
|
54 |
|
55 *_retval = false; |
|
56 |
|
57 if (mClass == eProtocolInfo) { |
|
58 *_retval = nsGNOMERegistry::HandlerExists(mSchemeOrType.get()); |
|
59 } else { |
|
60 nsRefPtr<nsMIMEInfoBase> mimeInfo = nsGNOMERegistry::GetFromType(mSchemeOrType); |
|
61 if (!mimeInfo) { |
|
62 nsAutoCString ext; |
|
63 nsresult rv = GetPrimaryExtension(ext); |
|
64 if (NS_SUCCEEDED(rv)) { |
|
65 mimeInfo = nsGNOMERegistry::GetFromExtension(ext); |
|
66 } |
|
67 } |
|
68 if (mimeInfo) |
|
69 *_retval = true; |
|
70 } |
|
71 |
|
72 if (*_retval) |
|
73 return NS_OK; |
|
74 |
|
75 #if defined(MOZ_ENABLE_CONTENTACTION) |
|
76 ContentAction::Action action = |
|
77 ContentAction::Action::defaultActionForFile(QUrl(), QString(mSchemeOrType.get())); |
|
78 if (action.isValid()) { |
|
79 *_retval = true; |
|
80 return NS_OK; |
|
81 } |
|
82 #endif |
|
83 |
|
84 return NS_OK; |
|
85 } |
|
86 |
|
87 nsresult |
|
88 nsMIMEInfoUnix::LaunchDefaultWithFile(nsIFile *aFile) |
|
89 { |
|
90 // if mDefaultApplication is set, it means the application has been set from |
|
91 // either /etc/mailcap or ${HOME}/.mailcap, in which case we don't want to |
|
92 // give the GNOME answer. |
|
93 if (mDefaultApplication) |
|
94 return nsMIMEInfoImpl::LaunchDefaultWithFile(aFile); |
|
95 |
|
96 nsAutoCString nativePath; |
|
97 aFile->GetNativePath(nativePath); |
|
98 |
|
99 #if defined(MOZ_ENABLE_CONTENTACTION) |
|
100 QUrl uri = QUrl::fromLocalFile(QString::fromUtf8(nativePath.get())); |
|
101 ContentAction::Action action = |
|
102 ContentAction::Action::defaultActionForFile(uri, QString(mSchemeOrType.get())); |
|
103 if (action.isValid()) { |
|
104 action.trigger(); |
|
105 return NS_OK; |
|
106 } |
|
107 return NS_ERROR_FAILURE; |
|
108 #endif |
|
109 |
|
110 nsCOMPtr<nsIGIOService> giovfs = do_GetService(NS_GIOSERVICE_CONTRACTID); |
|
111 nsAutoCString uriSpec; |
|
112 if (giovfs) { |
|
113 // nsGIOMimeApp->Launch wants a URI string instead of local file |
|
114 nsresult rv; |
|
115 nsCOMPtr<nsIIOService> ioservice = do_GetService(NS_IOSERVICE_CONTRACTID, &rv); |
|
116 NS_ENSURE_SUCCESS(rv, rv); |
|
117 nsCOMPtr<nsIURI> uri; |
|
118 rv = ioservice->NewFileURI(aFile, getter_AddRefs(uri)); |
|
119 NS_ENSURE_SUCCESS(rv, rv); |
|
120 uri->GetSpec(uriSpec); |
|
121 } |
|
122 |
|
123 nsCOMPtr<nsIGnomeVFSService> gnomevfs = do_GetService(NS_GNOMEVFSSERVICE_CONTRACTID); |
|
124 if (giovfs) { |
|
125 nsCOMPtr<nsIGIOMimeApp> app; |
|
126 if (NS_SUCCEEDED(giovfs->GetAppForMimeType(mSchemeOrType, getter_AddRefs(app))) && app) |
|
127 return app->Launch(uriSpec); |
|
128 } else if (gnomevfs) { |
|
129 /* Fallback to GnomeVFS */ |
|
130 nsCOMPtr<nsIGnomeVFSMimeApp> app; |
|
131 if (NS_SUCCEEDED(gnomevfs->GetAppForMimeType(mSchemeOrType, getter_AddRefs(app))) && app) |
|
132 return app->Launch(nativePath); |
|
133 } |
|
134 |
|
135 // If we haven't got an app we try to get a valid one by searching for the |
|
136 // extension mapped type |
|
137 nsRefPtr<nsMIMEInfoBase> mimeInfo = nsGNOMERegistry::GetFromExtension(nativePath); |
|
138 if (mimeInfo) { |
|
139 nsAutoCString type; |
|
140 mimeInfo->GetType(type); |
|
141 if (giovfs) { |
|
142 nsCOMPtr<nsIGIOMimeApp> app; |
|
143 if (NS_SUCCEEDED(giovfs->GetAppForMimeType(type, getter_AddRefs(app))) && app) |
|
144 return app->Launch(uriSpec); |
|
145 } else if (gnomevfs) { |
|
146 nsCOMPtr<nsIGnomeVFSMimeApp> app; |
|
147 if (NS_SUCCEEDED(gnomevfs->GetAppForMimeType(type, getter_AddRefs(app))) && app) |
|
148 return app->Launch(nativePath); |
|
149 } |
|
150 } |
|
151 |
|
152 if (!mDefaultApplication) |
|
153 return NS_ERROR_FILE_NOT_FOUND; |
|
154 |
|
155 return LaunchWithIProcess(mDefaultApplication, nativePath); |
|
156 } |
|
157 |
|
158 #if defined(MOZ_ENABLE_CONTENTACTION) |
|
159 NS_IMETHODIMP |
|
160 nsMIMEInfoUnix::GetPossibleApplicationHandlers(nsIMutableArray ** aPossibleAppHandlers) |
|
161 { |
|
162 if (!mPossibleApplications) { |
|
163 mPossibleApplications = do_CreateInstance(NS_ARRAY_CONTRACTID); |
|
164 |
|
165 if (!mPossibleApplications) |
|
166 return NS_ERROR_OUT_OF_MEMORY; |
|
167 |
|
168 QList<ContentAction::Action> actions = |
|
169 ContentAction::Action::actionsForFile(QUrl(), QString(mSchemeOrType.get())); |
|
170 |
|
171 for (int i = 0; i < actions.size(); ++i) { |
|
172 nsContentHandlerApp* app = |
|
173 new nsContentHandlerApp(nsString((char16_t*)actions[i].name().data()), |
|
174 mSchemeOrType, actions[i]); |
|
175 mPossibleApplications->AppendElement(app, false); |
|
176 } |
|
177 } |
|
178 |
|
179 *aPossibleAppHandlers = mPossibleApplications; |
|
180 NS_ADDREF(*aPossibleAppHandlers); |
|
181 return NS_OK; |
|
182 } |
|
183 #endif |
|
184 |