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