Fri, 16 Jan 2015 18:13:44 +0100
Integrate suggestion from review to improve consistency with existing code.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #include "nsGNOMERegistry.h" |
michael@0 | 7 | #include "nsString.h" |
michael@0 | 8 | #include "nsIComponentManager.h" |
michael@0 | 9 | #include "nsIFile.h" |
michael@0 | 10 | #include "nsMIMEInfoUnix.h" |
michael@0 | 11 | #include "nsAutoPtr.h" |
michael@0 | 12 | #include "nsIGConfService.h" |
michael@0 | 13 | #include "nsIGnomeVFSService.h" |
michael@0 | 14 | #include "nsIGIOService.h" |
michael@0 | 15 | |
michael@0 | 16 | #ifdef MOZ_WIDGET_GTK |
michael@0 | 17 | #include <glib.h> |
michael@0 | 18 | #include <glib-object.h> |
michael@0 | 19 | #endif |
michael@0 | 20 | |
michael@0 | 21 | /* static */ bool |
michael@0 | 22 | nsGNOMERegistry::HandlerExists(const char *aProtocolScheme) |
michael@0 | 23 | { |
michael@0 | 24 | nsCOMPtr<nsIGIOService> giovfs = do_GetService(NS_GIOSERVICE_CONTRACTID); |
michael@0 | 25 | nsCOMPtr<nsIGConfService> gconf = do_GetService(NS_GCONFSERVICE_CONTRACTID); |
michael@0 | 26 | if (giovfs) { |
michael@0 | 27 | nsCOMPtr<nsIGIOMimeApp> app; |
michael@0 | 28 | if (NS_FAILED(giovfs->GetAppForURIScheme(nsDependentCString(aProtocolScheme), |
michael@0 | 29 | getter_AddRefs(app)))) |
michael@0 | 30 | return false; |
michael@0 | 31 | else |
michael@0 | 32 | return true; |
michael@0 | 33 | } else if (gconf) { |
michael@0 | 34 | bool isEnabled; |
michael@0 | 35 | nsAutoCString handler; |
michael@0 | 36 | if (NS_FAILED(gconf->GetAppForProtocol(nsDependentCString(aProtocolScheme), &isEnabled, handler))) |
michael@0 | 37 | return false; |
michael@0 | 38 | |
michael@0 | 39 | return isEnabled; |
michael@0 | 40 | } |
michael@0 | 41 | |
michael@0 | 42 | return false; |
michael@0 | 43 | } |
michael@0 | 44 | |
michael@0 | 45 | // XXX Check HandlerExists() before calling LoadURL. |
michael@0 | 46 | // |
michael@0 | 47 | // If there is not a registered handler for the protocol, gnome_url_show() |
michael@0 | 48 | // falls back to using gnomevfs modules. See bug 389632. We don't want |
michael@0 | 49 | // this fallback to happen as we are not sure of the safety of all gnomevfs |
michael@0 | 50 | // modules and MIME-default applications. (gnomevfs should be handled in |
michael@0 | 51 | // nsGnomeVFSProtocolHandler.) |
michael@0 | 52 | |
michael@0 | 53 | /* static */ nsresult |
michael@0 | 54 | nsGNOMERegistry::LoadURL(nsIURI *aURL) |
michael@0 | 55 | { |
michael@0 | 56 | nsCOMPtr<nsIGIOService> giovfs = do_GetService(NS_GIOSERVICE_CONTRACTID); |
michael@0 | 57 | if (giovfs) |
michael@0 | 58 | return giovfs->ShowURI(aURL); |
michael@0 | 59 | |
michael@0 | 60 | nsCOMPtr<nsIGnomeVFSService> gnomevfs = do_GetService(NS_GNOMEVFSSERVICE_CONTRACTID); |
michael@0 | 61 | if (gnomevfs) |
michael@0 | 62 | return gnomevfs->ShowURI(aURL); |
michael@0 | 63 | |
michael@0 | 64 | return NS_ERROR_FAILURE; |
michael@0 | 65 | } |
michael@0 | 66 | |
michael@0 | 67 | /* static */ void |
michael@0 | 68 | nsGNOMERegistry::GetAppDescForScheme(const nsACString& aScheme, |
michael@0 | 69 | nsAString& aDesc) |
michael@0 | 70 | { |
michael@0 | 71 | nsCOMPtr<nsIGConfService> gconf = do_GetService(NS_GCONFSERVICE_CONTRACTID); |
michael@0 | 72 | nsCOMPtr<nsIGIOService> giovfs = do_GetService(NS_GIOSERVICE_CONTRACTID); |
michael@0 | 73 | if (!gconf && !giovfs) |
michael@0 | 74 | return; |
michael@0 | 75 | |
michael@0 | 76 | nsAutoCString name; |
michael@0 | 77 | if (giovfs) { |
michael@0 | 78 | nsCOMPtr<nsIGIOMimeApp> app; |
michael@0 | 79 | if (NS_FAILED(giovfs->GetAppForURIScheme(aScheme, getter_AddRefs(app)))) |
michael@0 | 80 | return; |
michael@0 | 81 | |
michael@0 | 82 | app->GetName(name); |
michael@0 | 83 | } else { |
michael@0 | 84 | bool isEnabled; |
michael@0 | 85 | if (NS_FAILED(gconf->GetAppForProtocol(aScheme, &isEnabled, name))) |
michael@0 | 86 | return; |
michael@0 | 87 | |
michael@0 | 88 | if (!name.IsEmpty()) { |
michael@0 | 89 | // Try to only provide the executable name, as it is much simpler than with the path and arguments |
michael@0 | 90 | int32_t firstSpace = name.FindChar(' '); |
michael@0 | 91 | if (firstSpace != kNotFound) { |
michael@0 | 92 | name.Truncate(firstSpace); |
michael@0 | 93 | int32_t lastSlash = name.RFindChar('/'); |
michael@0 | 94 | if (lastSlash != kNotFound) { |
michael@0 | 95 | name.Cut(0, lastSlash + 1); |
michael@0 | 96 | } |
michael@0 | 97 | } |
michael@0 | 98 | } |
michael@0 | 99 | } |
michael@0 | 100 | |
michael@0 | 101 | CopyUTF8toUTF16(name, aDesc); |
michael@0 | 102 | } |
michael@0 | 103 | |
michael@0 | 104 | |
michael@0 | 105 | /* static */ already_AddRefed<nsMIMEInfoBase> |
michael@0 | 106 | nsGNOMERegistry::GetFromExtension(const nsACString& aFileExt) |
michael@0 | 107 | { |
michael@0 | 108 | nsAutoCString mimeType; |
michael@0 | 109 | nsCOMPtr<nsIGIOService> giovfs = do_GetService(NS_GIOSERVICE_CONTRACTID); |
michael@0 | 110 | |
michael@0 | 111 | if (giovfs) { |
michael@0 | 112 | // Get the MIME type from the extension, then call GetFromType to |
michael@0 | 113 | // fill in the MIMEInfo. |
michael@0 | 114 | if (NS_FAILED(giovfs->GetMimeTypeFromExtension(aFileExt, mimeType)) || |
michael@0 | 115 | mimeType.EqualsLiteral("application/octet-stream")) { |
michael@0 | 116 | return nullptr; |
michael@0 | 117 | } |
michael@0 | 118 | } else { |
michael@0 | 119 | /* Fallback to GnomeVFS */ |
michael@0 | 120 | nsCOMPtr<nsIGnomeVFSService> gnomevfs = do_GetService(NS_GNOMEVFSSERVICE_CONTRACTID); |
michael@0 | 121 | if (!gnomevfs) |
michael@0 | 122 | return nullptr; |
michael@0 | 123 | |
michael@0 | 124 | if (NS_FAILED(gnomevfs->GetMimeTypeFromExtension(aFileExt, mimeType)) || |
michael@0 | 125 | mimeType.EqualsLiteral("application/octet-stream")) |
michael@0 | 126 | return nullptr; |
michael@0 | 127 | } |
michael@0 | 128 | |
michael@0 | 129 | nsRefPtr<nsMIMEInfoBase> mi = GetFromType(mimeType); |
michael@0 | 130 | if (mi) { |
michael@0 | 131 | mi->AppendExtension(aFileExt); |
michael@0 | 132 | } |
michael@0 | 133 | |
michael@0 | 134 | return mi.forget(); |
michael@0 | 135 | } |
michael@0 | 136 | |
michael@0 | 137 | /* static */ already_AddRefed<nsMIMEInfoBase> |
michael@0 | 138 | nsGNOMERegistry::GetFromType(const nsACString& aMIMEType) |
michael@0 | 139 | { |
michael@0 | 140 | nsRefPtr<nsMIMEInfoUnix> mimeInfo = new nsMIMEInfoUnix(aMIMEType); |
michael@0 | 141 | NS_ENSURE_TRUE(mimeInfo, nullptr); |
michael@0 | 142 | |
michael@0 | 143 | nsAutoCString name; |
michael@0 | 144 | nsAutoCString description; |
michael@0 | 145 | |
michael@0 | 146 | nsCOMPtr<nsIGIOService> giovfs = do_GetService(NS_GIOSERVICE_CONTRACTID); |
michael@0 | 147 | if (giovfs) { |
michael@0 | 148 | nsCOMPtr<nsIGIOMimeApp> gioHandlerApp; |
michael@0 | 149 | if (NS_FAILED(giovfs->GetAppForMimeType(aMIMEType, getter_AddRefs(gioHandlerApp))) || |
michael@0 | 150 | !gioHandlerApp) { |
michael@0 | 151 | return nullptr; |
michael@0 | 152 | } |
michael@0 | 153 | gioHandlerApp->GetName(name); |
michael@0 | 154 | giovfs->GetDescriptionForMimeType(aMIMEType, description); |
michael@0 | 155 | } else { |
michael@0 | 156 | /* Fallback to GnomeVFS*/ |
michael@0 | 157 | nsCOMPtr<nsIGnomeVFSService> gnomevfs = do_GetService(NS_GNOMEVFSSERVICE_CONTRACTID); |
michael@0 | 158 | if (!gnomevfs) |
michael@0 | 159 | return nullptr; |
michael@0 | 160 | |
michael@0 | 161 | nsCOMPtr<nsIGnomeVFSMimeApp> gnomeHandlerApp; |
michael@0 | 162 | if (NS_FAILED(gnomevfs->GetAppForMimeType(aMIMEType, getter_AddRefs(gnomeHandlerApp))) || |
michael@0 | 163 | !gnomeHandlerApp) { |
michael@0 | 164 | return nullptr; |
michael@0 | 165 | } |
michael@0 | 166 | gnomeHandlerApp->GetName(name); |
michael@0 | 167 | gnomevfs->GetDescriptionForMimeType(aMIMEType, description); |
michael@0 | 168 | } |
michael@0 | 169 | |
michael@0 | 170 | mimeInfo->SetDefaultDescription(NS_ConvertUTF8toUTF16(name)); |
michael@0 | 171 | mimeInfo->SetPreferredAction(nsIMIMEInfo::useSystemDefault); |
michael@0 | 172 | mimeInfo->SetDescription(NS_ConvertUTF8toUTF16(description)); |
michael@0 | 173 | |
michael@0 | 174 | return mimeInfo.forget(); |
michael@0 | 175 | } |