michael@0: /* -*- Mode: C++; tab-width: 3; indent-tabs-mode: nil; c-basic-offset: 2 -*- michael@0: * michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifdef MOZ_WIDGET_QT michael@0: #include michael@0: #include michael@0: #include michael@0: #if (MOZ_ENABLE_CONTENTACTION) michael@0: #include michael@0: #include "nsContentHandlerApp.h" michael@0: #endif michael@0: #endif michael@0: michael@0: #include "nsMIMEInfoUnix.h" michael@0: #include "nsGNOMERegistry.h" michael@0: #include "nsIGIOService.h" michael@0: #include "nsNetCID.h" michael@0: #include "nsIIOService.h" michael@0: #include "nsIGnomeVFSService.h" michael@0: #include "nsAutoPtr.h" michael@0: #ifdef MOZ_ENABLE_DBUS michael@0: #include "nsDBusHandlerApp.h" michael@0: #endif michael@0: michael@0: nsresult michael@0: nsMIMEInfoUnix::LoadUriInternal(nsIURI * aURI) michael@0: { michael@0: nsresult rv = nsGNOMERegistry::LoadURL(aURI); michael@0: michael@0: #ifdef MOZ_WIDGET_QT michael@0: if (NS_FAILED(rv)) { michael@0: nsAutoCString spec; michael@0: aURI->GetAsciiSpec(spec); michael@0: if (QDesktopServices::openUrl(QUrl(spec.get()))) { michael@0: rv = NS_OK; michael@0: } michael@0: } michael@0: #endif michael@0: michael@0: return rv; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsMIMEInfoUnix::GetHasDefaultHandler(bool *_retval) michael@0: { michael@0: // if mDefaultApplication is set, it means the application has been set from michael@0: // either /etc/mailcap or ${HOME}/.mailcap, in which case we don't want to michael@0: // give the GNOME answer. michael@0: if (mDefaultApplication) michael@0: return nsMIMEInfoImpl::GetHasDefaultHandler(_retval); michael@0: michael@0: *_retval = false; michael@0: michael@0: if (mClass == eProtocolInfo) { michael@0: *_retval = nsGNOMERegistry::HandlerExists(mSchemeOrType.get()); michael@0: } else { michael@0: nsRefPtr mimeInfo = nsGNOMERegistry::GetFromType(mSchemeOrType); michael@0: if (!mimeInfo) { michael@0: nsAutoCString ext; michael@0: nsresult rv = GetPrimaryExtension(ext); michael@0: if (NS_SUCCEEDED(rv)) { michael@0: mimeInfo = nsGNOMERegistry::GetFromExtension(ext); michael@0: } michael@0: } michael@0: if (mimeInfo) michael@0: *_retval = true; michael@0: } michael@0: michael@0: if (*_retval) michael@0: return NS_OK; michael@0: michael@0: #if defined(MOZ_ENABLE_CONTENTACTION) michael@0: ContentAction::Action action = michael@0: ContentAction::Action::defaultActionForFile(QUrl(), QString(mSchemeOrType.get())); michael@0: if (action.isValid()) { michael@0: *_retval = true; michael@0: return NS_OK; michael@0: } michael@0: #endif michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: nsMIMEInfoUnix::LaunchDefaultWithFile(nsIFile *aFile) michael@0: { michael@0: // if mDefaultApplication is set, it means the application has been set from michael@0: // either /etc/mailcap or ${HOME}/.mailcap, in which case we don't want to michael@0: // give the GNOME answer. michael@0: if (mDefaultApplication) michael@0: return nsMIMEInfoImpl::LaunchDefaultWithFile(aFile); michael@0: michael@0: nsAutoCString nativePath; michael@0: aFile->GetNativePath(nativePath); michael@0: michael@0: #if defined(MOZ_ENABLE_CONTENTACTION) michael@0: QUrl uri = QUrl::fromLocalFile(QString::fromUtf8(nativePath.get())); michael@0: ContentAction::Action action = michael@0: ContentAction::Action::defaultActionForFile(uri, QString(mSchemeOrType.get())); michael@0: if (action.isValid()) { michael@0: action.trigger(); michael@0: return NS_OK; michael@0: } michael@0: return NS_ERROR_FAILURE; michael@0: #endif michael@0: michael@0: nsCOMPtr giovfs = do_GetService(NS_GIOSERVICE_CONTRACTID); michael@0: nsAutoCString uriSpec; michael@0: if (giovfs) { michael@0: // nsGIOMimeApp->Launch wants a URI string instead of local file michael@0: nsresult rv; michael@0: nsCOMPtr ioservice = do_GetService(NS_IOSERVICE_CONTRACTID, &rv); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: nsCOMPtr uri; michael@0: rv = ioservice->NewFileURI(aFile, getter_AddRefs(uri)); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: uri->GetSpec(uriSpec); michael@0: } michael@0: michael@0: nsCOMPtr gnomevfs = do_GetService(NS_GNOMEVFSSERVICE_CONTRACTID); michael@0: if (giovfs) { michael@0: nsCOMPtr app; michael@0: if (NS_SUCCEEDED(giovfs->GetAppForMimeType(mSchemeOrType, getter_AddRefs(app))) && app) michael@0: return app->Launch(uriSpec); michael@0: } else if (gnomevfs) { michael@0: /* Fallback to GnomeVFS */ michael@0: nsCOMPtr app; michael@0: if (NS_SUCCEEDED(gnomevfs->GetAppForMimeType(mSchemeOrType, getter_AddRefs(app))) && app) michael@0: return app->Launch(nativePath); michael@0: } michael@0: michael@0: // If we haven't got an app we try to get a valid one by searching for the michael@0: // extension mapped type michael@0: nsRefPtr mimeInfo = nsGNOMERegistry::GetFromExtension(nativePath); michael@0: if (mimeInfo) { michael@0: nsAutoCString type; michael@0: mimeInfo->GetType(type); michael@0: if (giovfs) { michael@0: nsCOMPtr app; michael@0: if (NS_SUCCEEDED(giovfs->GetAppForMimeType(type, getter_AddRefs(app))) && app) michael@0: return app->Launch(uriSpec); michael@0: } else if (gnomevfs) { michael@0: nsCOMPtr app; michael@0: if (NS_SUCCEEDED(gnomevfs->GetAppForMimeType(type, getter_AddRefs(app))) && app) michael@0: return app->Launch(nativePath); michael@0: } michael@0: } michael@0: michael@0: if (!mDefaultApplication) michael@0: return NS_ERROR_FILE_NOT_FOUND; michael@0: michael@0: return LaunchWithIProcess(mDefaultApplication, nativePath); michael@0: } michael@0: michael@0: #if defined(MOZ_ENABLE_CONTENTACTION) michael@0: NS_IMETHODIMP michael@0: nsMIMEInfoUnix::GetPossibleApplicationHandlers(nsIMutableArray ** aPossibleAppHandlers) michael@0: { michael@0: if (!mPossibleApplications) { michael@0: mPossibleApplications = do_CreateInstance(NS_ARRAY_CONTRACTID); michael@0: michael@0: if (!mPossibleApplications) michael@0: return NS_ERROR_OUT_OF_MEMORY; michael@0: michael@0: QList actions = michael@0: ContentAction::Action::actionsForFile(QUrl(), QString(mSchemeOrType.get())); michael@0: michael@0: for (int i = 0; i < actions.size(); ++i) { michael@0: nsContentHandlerApp* app = michael@0: new nsContentHandlerApp(nsString((char16_t*)actions[i].name().data()), michael@0: mSchemeOrType, actions[i]); michael@0: mPossibleApplications->AppendElement(app, false); michael@0: } michael@0: } michael@0: michael@0: *aPossibleAppHandlers = mPossibleApplications; michael@0: NS_ADDREF(*aPossibleAppHandlers); michael@0: return NS_OK; michael@0: } michael@0: #endif michael@0: