1 : /* -*- Mode: C++; tab-width: 3; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 : *
3 : * ***** BEGIN LICENSE BLOCK *****
4 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 : *
6 : * The contents of this file are subject to the Mozilla Public License Version
7 : * 1.1 (the "License"); you may not use this file except in compliance with
8 : * the License. You may obtain a copy of the License at
9 : * http://www.mozilla.org/MPL/
10 : *
11 : * Software distributed under the License is distributed on an "AS IS" basis,
12 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 : * for the specific language governing rights and limitations under the
14 : * License.
15 : *
16 : * The Original Code is the Mozilla browser.
17 : *
18 : * The Initial Developer of the Original Code is
19 : * Mozilla Corporation.
20 : * Portions created by the Initial Developer are Copyright (C) 2007
21 : * the Initial Developer. All Rights Reserved.
22 : *
23 : * Contributor(s):
24 : * Shawn Wilsher <me@shawnwilsher.com> (Original Author)
25 : * Wolfgang Rosenauer <wr@rosenauer.org>
26 : *
27 : * Alternatively, the contents of this file may be used under the terms of
28 : * either of the GNU General Public License Version 2 or later (the "GPL"),
29 : * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
30 : * in which case the provisions of the GPL or the LGPL are applicable instead
31 : * of those above. If you wish to allow use of your version of this file only
32 : * under the terms of either the GPL or the LGPL, and not to allow others to
33 : * use your version of this file under the terms of the MPL, indicate your
34 : * decision by deleting the provisions above and replace them with the notice
35 : * and other provisions required by the GPL or the LGPL. If you do not delete
36 : * the provisions above, a recipient may use your version of this file under
37 : * the terms of any one of the MPL, the GPL or the LGPL.
38 : *
39 : * ***** END LICENSE BLOCK ***** */
40 :
41 : #if (MOZ_PLATFORM_MAEMO == 5) && defined (MOZ_ENABLE_GNOMEVFS)
42 : #include <glib.h>
43 : #include <hildon-uri.h>
44 : #include <hildon-mime.h>
45 : #include <libosso.h>
46 : #endif
47 : #ifdef MOZ_WIDGET_QT
48 : #include <QDesktopServices>
49 : #include <QUrl>
50 : #include <QString>
51 : #if (MOZ_ENABLE_CONTENTACTION)
52 : #include <contentaction/contentaction.h>
53 : #include "nsContentHandlerApp.h"
54 : #endif
55 : #endif
56 :
57 : #include "nsMIMEInfoUnix.h"
58 : #include "nsGNOMERegistry.h"
59 : #include "nsIGIOService.h"
60 : #include "nsNetCID.h"
61 : #include "nsIIOService.h"
62 : #include "nsIGnomeVFSService.h"
63 : #include "nsAutoPtr.h"
64 : #ifdef MOZ_ENABLE_DBUS
65 : #include "nsDBusHandlerApp.h"
66 : #endif
67 :
68 : nsresult
69 0 : nsMIMEInfoUnix::LoadUriInternal(nsIURI * aURI)
70 : {
71 0 : nsresult rv = nsGNOMERegistry::LoadURL(aURI);
72 :
73 : #if (MOZ_PLATFORM_MAEMO == 5) && defined (MOZ_ENABLE_GNOMEVFS)
74 : if (NS_FAILED(rv)){
75 : HildonURIAction *action = hildon_uri_get_default_action(mSchemeOrType.get(), nsnull);
76 : if (action) {
77 : nsCAutoString spec;
78 : aURI->GetAsciiSpec(spec);
79 : if (hildon_uri_open(spec.get(), action, nsnull))
80 : rv = NS_OK;
81 : hildon_uri_action_unref(action);
82 : }
83 : }
84 : #endif
85 :
86 : #ifdef MOZ_WIDGET_QT
87 : if (NS_FAILED(rv)) {
88 : nsCAutoString spec;
89 : aURI->GetAsciiSpec(spec);
90 : if (QDesktopServices::openUrl(QUrl(spec.get()))) {
91 : rv = NS_OK;
92 : }
93 : }
94 : #endif
95 :
96 0 : return rv;
97 : }
98 :
99 : NS_IMETHODIMP
100 13 : nsMIMEInfoUnix::GetHasDefaultHandler(bool *_retval)
101 : {
102 13 : *_retval = false;
103 26 : nsRefPtr<nsMIMEInfoBase> mimeInfo = nsGNOMERegistry::GetFromType(mSchemeOrType);
104 13 : if (!mimeInfo) {
105 2 : nsCAutoString ext;
106 1 : nsresult rv = GetPrimaryExtension(ext);
107 1 : if (NS_SUCCEEDED(rv)) {
108 0 : mimeInfo = nsGNOMERegistry::GetFromExtension(ext);
109 : }
110 : }
111 13 : if (mimeInfo)
112 12 : *_retval = true;
113 :
114 13 : if (*_retval)
115 12 : return NS_OK;
116 :
117 : #if (MOZ_PLATFORM_MAEMO == 5) && defined (MOZ_ENABLE_GNOMEVFS)
118 : HildonURIAction *action = hildon_uri_get_default_action(mSchemeOrType.get(), nsnull);
119 : if (action) {
120 : *_retval = true;
121 : hildon_uri_action_unref(action);
122 : return NS_OK;
123 : }
124 : #endif
125 :
126 : #if defined(MOZ_ENABLE_CONTENTACTION)
127 : ContentAction::Action action =
128 : ContentAction::Action::defaultActionForFile(QUrl(), QString(mSchemeOrType.get()));
129 : if (action.isValid()) {
130 : *_retval = true;
131 : return NS_OK;
132 : }
133 : #endif
134 :
135 : // If we didn't find a VFS handler, fallback.
136 1 : return nsMIMEInfoImpl::GetHasDefaultHandler(_retval);
137 : }
138 :
139 : nsresult
140 0 : nsMIMEInfoUnix::LaunchDefaultWithFile(nsIFile *aFile)
141 : {
142 0 : nsCAutoString nativePath;
143 0 : aFile->GetNativePath(nativePath);
144 :
145 : #if (MOZ_PLATFORM_MAEMO == 5) && defined (MOZ_ENABLE_GNOMEVFS)
146 : if(NS_SUCCEEDED(LaunchDefaultWithDBus(PromiseFlatCString(nativePath).get())))
147 : return NS_OK;
148 : #endif
149 :
150 : #if defined(MOZ_ENABLE_CONTENTACTION)
151 : QUrl uri = QUrl::fromLocalFile(QString::fromUtf8(nativePath.get()));
152 : ContentAction::Action action =
153 : ContentAction::Action::defaultActionForFile(uri, QString(mSchemeOrType.get()));
154 : if (action.isValid()) {
155 : action.trigger();
156 : return NS_OK;
157 : }
158 : return NS_ERROR_FAILURE;
159 : #endif
160 :
161 0 : nsCOMPtr<nsIGIOService> giovfs = do_GetService(NS_GIOSERVICE_CONTRACTID);
162 0 : nsCAutoString uriSpec;
163 0 : if (giovfs) {
164 : // nsGIOMimeApp->Launch wants a URI string instead of local file
165 : nsresult rv;
166 0 : nsCOMPtr<nsIIOService> ioservice = do_GetService(NS_IOSERVICE_CONTRACTID, &rv);
167 0 : NS_ENSURE_SUCCESS(rv, rv);
168 0 : nsCOMPtr<nsIURI> uri;
169 0 : rv = ioservice->NewFileURI(aFile, getter_AddRefs(uri));
170 0 : NS_ENSURE_SUCCESS(rv, rv);
171 0 : uri->GetSpec(uriSpec);
172 : }
173 :
174 0 : nsCOMPtr<nsIGnomeVFSService> gnomevfs = do_GetService(NS_GNOMEVFSSERVICE_CONTRACTID);
175 0 : if (giovfs) {
176 0 : nsCOMPtr<nsIGIOMimeApp> app;
177 0 : if (NS_SUCCEEDED(giovfs->GetAppForMimeType(mSchemeOrType, getter_AddRefs(app))) && app)
178 0 : return app->Launch(uriSpec);
179 0 : } else if (gnomevfs) {
180 : /* Fallback to GnomeVFS */
181 0 : nsCOMPtr<nsIGnomeVFSMimeApp> app;
182 0 : if (NS_SUCCEEDED(gnomevfs->GetAppForMimeType(mSchemeOrType, getter_AddRefs(app))) && app)
183 0 : return app->Launch(nativePath);
184 : }
185 :
186 : // If we haven't got an app we try to get a valid one by searching for the
187 : // extension mapped type
188 0 : nsRefPtr<nsMIMEInfoBase> mimeInfo = nsGNOMERegistry::GetFromExtension(nativePath);
189 0 : if (mimeInfo) {
190 0 : nsCAutoString type;
191 0 : mimeInfo->GetType(type);
192 0 : if (giovfs) {
193 0 : nsCOMPtr<nsIGIOMimeApp> app;
194 0 : if (NS_SUCCEEDED(giovfs->GetAppForMimeType(type, getter_AddRefs(app))) && app)
195 0 : return app->Launch(uriSpec);
196 0 : } else if (gnomevfs) {
197 0 : nsCOMPtr<nsIGnomeVFSMimeApp> app;
198 0 : if (NS_SUCCEEDED(gnomevfs->GetAppForMimeType(type, getter_AddRefs(app))) && app)
199 0 : return app->Launch(nativePath);
200 : }
201 : }
202 :
203 0 : if (!mDefaultApplication)
204 0 : return NS_ERROR_FILE_NOT_FOUND;
205 :
206 0 : return LaunchWithIProcess(mDefaultApplication, nativePath);
207 : }
208 :
209 : #if (MOZ_PLATFORM_MAEMO == 5) && defined (MOZ_ENABLE_GNOMEVFS)
210 :
211 : /* This method tries to launch the associated default handler for the given
212 : * mime/file via hildon specific APIs (in this case hildon_mime_open_file*
213 : * which are essetially wrappers to the DBUS mime_open method).
214 : */
215 :
216 : nsresult
217 : nsMIMEInfoUnix::LaunchDefaultWithDBus(const char *aFilePath)
218 : {
219 : const PRInt32 kHILDON_SUCCESS = 1;
220 : PRInt32 result = 0;
221 : DBusError err;
222 : dbus_error_init(&err);
223 :
224 : DBusConnection *connection = dbus_bus_get(DBUS_BUS_SESSION, &err);
225 : if (dbus_error_is_set(&err)) {
226 : dbus_error_free(&err);
227 : return NS_ERROR_FAILURE;
228 : }
229 :
230 : if (nsnull == connection)
231 : return NS_ERROR_FAILURE;
232 :
233 : result = hildon_mime_open_file_with_mime_type(connection,
234 : aFilePath,
235 : mSchemeOrType.get());
236 : if (result != kHILDON_SUCCESS)
237 : if (hildon_mime_open_file(connection, aFilePath) != kHILDON_SUCCESS)
238 : return NS_ERROR_FAILURE;
239 :
240 : return NS_OK;
241 : }
242 :
243 : /* static */ bool
244 : nsMIMEInfoUnix::HandlerExists(const char *aProtocolScheme)
245 : {
246 : bool isEnabled = false;
247 : HildonURIAction *action = hildon_uri_get_default_action(aProtocolScheme, nsnull);
248 : if (action) {
249 : isEnabled = true;
250 : hildon_uri_action_unref(action);
251 : }
252 : return isEnabled;
253 : }
254 :
255 : NS_IMETHODIMP
256 : nsMIMEInfoUnix::GetPossibleApplicationHandlers(nsIMutableArray ** aPossibleAppHandlers)
257 : {
258 : if (!mPossibleApplications) {
259 : mPossibleApplications = do_CreateInstance(NS_ARRAY_CONTRACTID);
260 :
261 : if (!mPossibleApplications)
262 : return NS_ERROR_OUT_OF_MEMORY;
263 :
264 : GSList *actions = hildon_uri_get_actions(mSchemeOrType.get(), nsnull);
265 : GSList *actionsPtr = actions;
266 : while (actionsPtr) {
267 : HildonURIAction *action = (HildonURIAction*)actionsPtr->data;
268 : actionsPtr = actionsPtr->next;
269 : nsDBusHandlerApp* app = new nsDBusHandlerApp();
270 : if (!app){
271 : hildon_uri_free_actions(actions);
272 : return NS_ERROR_OUT_OF_MEMORY;
273 : }
274 : nsDependentCString method(hildon_uri_action_get_method(action));
275 : nsDependentCString key(hildon_uri_action_get_service(action));
276 : nsCString service, objpath, interface;
277 : app->SetMethod(method);
278 : app->SetName(NS_ConvertUTF8toUTF16(key));
279 :
280 : if (key.FindChar('.', 0) > 0) {
281 : service.Assign(key);
282 : objpath.Assign(NS_LITERAL_CSTRING("/")+ key);
283 : objpath.ReplaceChar('.', '/');
284 : interface.Assign(key);
285 : } else {
286 : service.Assign(NS_LITERAL_CSTRING("com.nokia.")+ key);
287 : objpath.Assign(NS_LITERAL_CSTRING("/com/nokia/")+ key);
288 : interface.Assign(NS_LITERAL_CSTRING("com.nokia.")+ key);
289 : }
290 :
291 : app->SetService(service);
292 : app->SetObjectPath(objpath);
293 : app->SetDBusInterface(interface);
294 :
295 : mPossibleApplications->AppendElement(app, false);
296 : }
297 : hildon_uri_free_actions(actions);
298 : }
299 :
300 : *aPossibleAppHandlers = mPossibleApplications;
301 : NS_ADDREF(*aPossibleAppHandlers);
302 : return NS_OK;
303 : }
304 : #endif
305 :
306 : #if defined(MOZ_ENABLE_CONTENTACTION)
307 : NS_IMETHODIMP
308 : nsMIMEInfoUnix::GetPossibleApplicationHandlers(nsIMutableArray ** aPossibleAppHandlers)
309 : {
310 : if (!mPossibleApplications) {
311 : mPossibleApplications = do_CreateInstance(NS_ARRAY_CONTRACTID);
312 :
313 : if (!mPossibleApplications)
314 : return NS_ERROR_OUT_OF_MEMORY;
315 :
316 : QList<ContentAction::Action> actions =
317 : ContentAction::Action::actionsForFile(QUrl(), QString(mSchemeOrType.get()));
318 :
319 : for (int i = 0; i < actions.size(); ++i) {
320 : nsContentHandlerApp* app =
321 : new nsContentHandlerApp(nsString((PRUnichar*)actions[i].name().data()),
322 : mSchemeOrType, actions[i]);
323 : mPossibleApplications->AppendElement(app, false);
324 : }
325 : }
326 :
327 : *aPossibleAppHandlers = mPossibleApplications;
328 : NS_ADDREF(*aPossibleAppHandlers);
329 : return NS_OK;
330 : }
331 : #endif
332 :
|