1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is the GNOME helper app implementation.
16 : *
17 : * The Initial Developer of the Original Code is
18 : * IBM Corporation.
19 : * Portions created by the Initial Developer are Copyright (C) 2003
20 : * the Initial Developer. All Rights Reserved.
21 : *
22 : * Contributor(s):
23 : * Brian Ryner <bryner@brianryner.com> (Original Author)
24 : *
25 : * Alternatively, the contents of this file may be used under the terms of
26 : * either the GNU General Public License Version 2 or later (the "GPL"), or
27 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 : * in which case the provisions of the GPL or the LGPL are applicable instead
29 : * of those above. If you wish to allow use of your version of this file only
30 : * under the terms of either the GPL or the LGPL, and not to allow others to
31 : * use your version of this file under the terms of the MPL, indicate your
32 : * decision by deleting the provisions above and replace them with the notice
33 : * and other provisions required by the GPL or the LGPL. If you do not delete
34 : * the provisions above, a recipient may use your version of this file under
35 : * the terms of any one of the MPL, the GPL or the LGPL.
36 : *
37 : * ***** END LICENSE BLOCK ***** */
38 :
39 : #include "nsGNOMERegistry.h"
40 : #include "prlink.h"
41 : #include "prmem.h"
42 : #include "nsString.h"
43 : #include "nsIComponentManager.h"
44 : #include "nsILocalFile.h"
45 : #include "nsMIMEInfoUnix.h"
46 : #include "nsAutoPtr.h"
47 : #include "nsIGConfService.h"
48 : #include "nsIGnomeVFSService.h"
49 : #include "nsIGIOService.h"
50 :
51 : #ifdef MOZ_WIDGET_GTK2
52 : #include <glib.h>
53 : #include <glib-object.h>
54 : #endif
55 :
56 : #ifdef MOZ_PLATFORM_MAEMO
57 : #include <libintl.h>
58 : #endif
59 :
60 : /* static */ bool
61 28 : nsGNOMERegistry::HandlerExists(const char *aProtocolScheme)
62 : {
63 56 : nsCOMPtr<nsIGIOService> giovfs = do_GetService(NS_GIOSERVICE_CONTRACTID);
64 56 : nsCOMPtr<nsIGConfService> gconf = do_GetService(NS_GCONFSERVICE_CONTRACTID);
65 28 : if (giovfs) {
66 0 : nsCOMPtr<nsIGIOMimeApp> app;
67 0 : if (NS_FAILED(giovfs->GetAppForURIScheme(nsDependentCString(aProtocolScheme),
68 : getter_AddRefs(app))))
69 0 : return false;
70 : else
71 0 : return true;
72 28 : } else if (gconf) {
73 : bool isEnabled;
74 56 : nsCAutoString handler;
75 28 : if (NS_FAILED(gconf->GetAppForProtocol(nsDependentCString(aProtocolScheme), &isEnabled, handler)))
76 0 : return false;
77 :
78 28 : return isEnabled;
79 : }
80 :
81 0 : return false;
82 : }
83 :
84 : // XXX Check HandlerExists() before calling LoadURL.
85 : //
86 : // If there is not a registered handler for the protocol, gnome_url_show()
87 : // falls back to using gnomevfs modules. See bug 389632. We don't want
88 : // this fallback to happen as we are not sure of the safety of all gnomevfs
89 : // modules and MIME-default applications. (gnomevfs should be handled in
90 : // nsGnomeVFSProtocolHandler.)
91 :
92 : /* static */ nsresult
93 0 : nsGNOMERegistry::LoadURL(nsIURI *aURL)
94 : {
95 0 : nsCOMPtr<nsIGIOService> giovfs = do_GetService(NS_GIOSERVICE_CONTRACTID);
96 0 : if (giovfs)
97 0 : return giovfs->ShowURI(aURL);
98 :
99 0 : nsCOMPtr<nsIGnomeVFSService> gnomevfs = do_GetService(NS_GNOMEVFSSERVICE_CONTRACTID);
100 0 : if (gnomevfs)
101 0 : return gnomevfs->ShowURI(aURL);
102 :
103 0 : return NS_ERROR_FAILURE;
104 : }
105 :
106 : /* static */ void
107 9 : nsGNOMERegistry::GetAppDescForScheme(const nsACString& aScheme,
108 : nsAString& aDesc)
109 : {
110 18 : nsCOMPtr<nsIGConfService> gconf = do_GetService(NS_GCONFSERVICE_CONTRACTID);
111 18 : nsCOMPtr<nsIGIOService> giovfs = do_GetService(NS_GIOSERVICE_CONTRACTID);
112 9 : if (!gconf && !giovfs)
113 : return;
114 :
115 18 : nsCAutoString name;
116 9 : if (giovfs) {
117 0 : nsCOMPtr<nsIGIOMimeApp> app;
118 0 : if (NS_FAILED(giovfs->GetAppForURIScheme(aScheme, getter_AddRefs(app))))
119 : return;
120 :
121 0 : app->GetName(name);
122 : } else {
123 : bool isEnabled;
124 9 : if (NS_FAILED(gconf->GetAppForProtocol(aScheme, &isEnabled, name)))
125 : return;
126 :
127 9 : if (!name.IsEmpty()) {
128 : // Try to only provide the executable name, as it is much simpler than with the path and arguments
129 9 : PRInt32 firstSpace = name.FindChar(' ');
130 9 : if (firstSpace != kNotFound) {
131 9 : name.Truncate(firstSpace);
132 9 : PRInt32 lastSlash = name.RFindChar('/');
133 9 : if (lastSlash != kNotFound) {
134 0 : name.Cut(0, lastSlash + 1);
135 : }
136 : }
137 : }
138 : }
139 :
140 18 : CopyUTF8toUTF16(name, aDesc);
141 : }
142 :
143 :
144 : /* static */ already_AddRefed<nsMIMEInfoBase>
145 62 : nsGNOMERegistry::GetFromExtension(const nsACString& aFileExt)
146 : {
147 124 : nsCAutoString mimeType;
148 124 : nsCOMPtr<nsIGIOService> giovfs = do_GetService(NS_GIOSERVICE_CONTRACTID);
149 :
150 62 : if (giovfs) {
151 : // Get the MIME type from the extension, then call GetFromType to
152 : // fill in the MIMEInfo.
153 0 : if (NS_FAILED(giovfs->GetMimeTypeFromExtension(aFileExt, mimeType)) ||
154 0 : mimeType.EqualsLiteral("application/octet-stream")) {
155 0 : return nsnull;
156 : }
157 : } else {
158 : /* Fallback to GnomeVFS */
159 124 : nsCOMPtr<nsIGnomeVFSService> gnomevfs = do_GetService(NS_GNOMEVFSSERVICE_CONTRACTID);
160 62 : if (!gnomevfs)
161 0 : return nsnull;
162 :
163 124 : if (NS_FAILED(gnomevfs->GetMimeTypeFromExtension(aFileExt, mimeType)) ||
164 62 : mimeType.EqualsLiteral("application/octet-stream"))
165 43 : return nsnull;
166 : }
167 :
168 19 : return GetFromType(mimeType);
169 : }
170 :
171 : /* static */ already_AddRefed<nsMIMEInfoBase>
172 60 : nsGNOMERegistry::GetFromType(const nsACString& aMIMEType)
173 : {
174 120 : nsRefPtr<nsMIMEInfoUnix> mimeInfo = new nsMIMEInfoUnix(aMIMEType);
175 60 : NS_ENSURE_TRUE(mimeInfo, nsnull);
176 :
177 120 : nsCAutoString name;
178 120 : nsCAutoString description;
179 :
180 120 : nsCOMPtr<nsIGIOService> giovfs = do_GetService(NS_GIOSERVICE_CONTRACTID);
181 60 : if (giovfs) {
182 0 : nsCOMPtr<nsIGIOMimeApp> gioHandlerApp;
183 0 : if (NS_FAILED(giovfs->GetAppForMimeType(aMIMEType, getter_AddRefs(gioHandlerApp))) ||
184 0 : !gioHandlerApp) {
185 0 : return nsnull;
186 : }
187 0 : gioHandlerApp->GetName(name);
188 0 : giovfs->GetDescriptionForMimeType(aMIMEType, description);
189 : } else {
190 : /* Fallback to GnomeVFS*/
191 120 : nsCOMPtr<nsIGnomeVFSService> gnomevfs = do_GetService(NS_GNOMEVFSSERVICE_CONTRACTID);
192 60 : if (!gnomevfs)
193 0 : return nsnull;
194 :
195 120 : nsCOMPtr<nsIGnomeVFSMimeApp> gnomeHandlerApp;
196 120 : if (NS_FAILED(gnomevfs->GetAppForMimeType(aMIMEType, getter_AddRefs(gnomeHandlerApp))) ||
197 60 : !gnomeHandlerApp) {
198 18 : return nsnull;
199 : }
200 42 : gnomeHandlerApp->GetName(name);
201 102 : gnomevfs->GetDescriptionForMimeType(aMIMEType, description);
202 : }
203 :
204 : #ifdef MOZ_PLATFORM_MAEMO
205 : // On Maemo/Hildon, GetName ends up calling gnome_vfs_mime_application_get_name,
206 : // which happens to return a non-localized message-id for the application. To
207 : // get the localized name for the application, we have to call dgettext with
208 : // the default maemo domain-name to try and translate the string into the operating
209 : // system's native language.
210 : const char kDefaultTextDomain [] = "maemo-af-desktop";
211 : nsCAutoString realName (dgettext(kDefaultTextDomain, PromiseFlatCString(name).get()));
212 : mimeInfo->SetDefaultDescription(NS_ConvertUTF8toUTF16(realName));
213 : #else
214 42 : mimeInfo->SetDefaultDescription(NS_ConvertUTF8toUTF16(name));
215 : #endif
216 42 : mimeInfo->SetPreferredAction(nsIMIMEInfo::useSystemDefault);
217 42 : mimeInfo->SetDescription(NS_ConvertUTF8toUTF16(description));
218 :
219 : nsMIMEInfoBase* retval;
220 42 : NS_ADDREF((retval = mimeInfo));
221 42 : return retval;
222 : }
|