2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
|
|
|
*
|
|
|
|
* The Original Code is the GNOME helper app implementation.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* IBM Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 2003
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Brian Ryner <bryner@brianryner.com> (Original Author)
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
|
|
|
|
|
|
|
#include "nsGNOMERegistry.h"
|
|
|
|
#include "prlink.h"
|
|
|
|
#include "prmem.h"
|
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsIComponentManager.h"
|
|
|
|
#include "nsILocalFile.h"
|
2007-07-25 22:52:27 -07:00
|
|
|
#include "nsMIMEInfoUnix.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsAutoPtr.h"
|
2008-01-29 20:36:11 -08:00
|
|
|
#include "nsIGConfService.h"
|
|
|
|
#include "nsIGnomeVFSService.h"
|
2009-08-25 11:58:11 -07:00
|
|
|
#include "nsIGIOService.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2008-04-19 06:27:21 -07:00
|
|
|
#ifdef MOZ_WIDGET_GTK2
|
2007-03-22 10:30:00 -07:00
|
|
|
#include <glib.h>
|
|
|
|
#include <glib-object.h>
|
2009-11-13 11:47:27 -08:00
|
|
|
#endif
|
2009-03-30 11:10:26 -07:00
|
|
|
|
2010-02-09 11:02:11 -08:00
|
|
|
#ifdef MOZ_PLATFORM_MAEMO
|
2009-03-30 11:10:26 -07:00
|
|
|
#include <libintl.h>
|
|
|
|
#endif
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/* static */ PRBool
|
|
|
|
nsGNOMERegistry::HandlerExists(const char *aProtocolScheme)
|
|
|
|
{
|
2008-01-29 20:36:11 -08:00
|
|
|
nsCOMPtr<nsIGConfService> gconf = do_GetService(NS_GCONFSERVICE_CONTRACTID);
|
|
|
|
if (!gconf)
|
|
|
|
return PR_FALSE;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2008-01-29 20:36:11 -08:00
|
|
|
PRBool isEnabled;
|
|
|
|
nsCAutoString handler;
|
|
|
|
if (NS_FAILED(gconf->GetAppForProtocol(nsDependentCString(aProtocolScheme), &isEnabled, handler)))
|
|
|
|
return PR_FALSE;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2008-01-29 20:36:11 -08:00
|
|
|
return isEnabled;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2007-07-27 11:31:13 -07:00
|
|
|
// XXX Check HandlerExists() before calling LoadURL.
|
|
|
|
//
|
|
|
|
// If there is not a registered handler for the protocol, gnome_url_show()
|
|
|
|
// falls back to using gnomevfs modules. See bug 389632. We don't want
|
|
|
|
// this fallback to happen as we are not sure of the safety of all gnomevfs
|
|
|
|
// modules and MIME-default applications. (gnomevfs should be handled in
|
|
|
|
// nsGnomeVFSProtocolHandler.)
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
/* static */ nsresult
|
|
|
|
nsGNOMERegistry::LoadURL(nsIURI *aURL)
|
|
|
|
{
|
2009-08-25 11:58:11 -07:00
|
|
|
nsCOMPtr<nsIGIOService> giovfs = do_GetService(NS_GIOSERVICE_CONTRACTID);
|
|
|
|
nsCOMPtr<nsIGnomeVFSService> gnomevfs = do_GetService(NS_GNOMEVFSSERVICE_CONTRACTID);
|
|
|
|
if (giovfs) {
|
|
|
|
return giovfs->ShowURI(aURL);
|
|
|
|
} else if (gnomevfs) {
|
|
|
|
/* Fallback to GnomeVFS */
|
|
|
|
return gnomevfs->ShowURI(aURL);
|
|
|
|
}
|
|
|
|
return NS_ERROR_FAILURE;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ void
|
|
|
|
nsGNOMERegistry::GetAppDescForScheme(const nsACString& aScheme,
|
|
|
|
nsAString& aDesc)
|
|
|
|
{
|
2008-01-29 20:36:11 -08:00
|
|
|
nsCOMPtr<nsIGConfService> gconf = do_GetService(NS_GCONFSERVICE_CONTRACTID);
|
|
|
|
if (!gconf)
|
|
|
|
return;
|
|
|
|
|
|
|
|
PRBool isEnabled;
|
|
|
|
nsCAutoString app;
|
|
|
|
if (NS_FAILED(gconf->GetAppForProtocol(aScheme, &isEnabled, app)))
|
|
|
|
return;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2008-01-29 20:36:11 -08:00
|
|
|
if (!app.IsEmpty()) {
|
2007-12-03 14:18:03 -08:00
|
|
|
// Try to only provide the executable name, as it is much simpler than with the path and arguments
|
2008-01-29 20:36:11 -08:00
|
|
|
PRInt32 firstSpace = app.FindChar(' ');
|
|
|
|
if (firstSpace != kNotFound) {
|
|
|
|
app.Truncate(firstSpace);
|
|
|
|
PRInt32 lastSlash = app.RFindChar('/');
|
|
|
|
if (lastSlash != kNotFound) {
|
|
|
|
app.Cut(0, lastSlash + 1);
|
2007-12-03 14:18:03 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
CopyUTF8toUTF16(app, aDesc);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* static */ already_AddRefed<nsMIMEInfoBase>
|
2008-01-29 20:36:11 -08:00
|
|
|
nsGNOMERegistry::GetFromExtension(const nsACString& aFileExt)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2008-01-29 20:36:11 -08:00
|
|
|
nsCAutoString mimeType;
|
2009-08-25 11:58:11 -07:00
|
|
|
nsCOMPtr<nsIGnomeVFSService> gnomevfs = do_GetService(NS_GNOMEVFSSERVICE_CONTRACTID);
|
|
|
|
nsCOMPtr<nsIGIOService> giovfs = do_GetService(NS_GIOSERVICE_CONTRACTID);
|
|
|
|
|
|
|
|
if (!gnomevfs && !giovfs)
|
2007-03-22 10:30:00 -07:00
|
|
|
return nsnull;
|
|
|
|
|
2009-08-25 11:58:11 -07:00
|
|
|
if (giovfs) {
|
|
|
|
// Get the MIME type from the extension, then call GetFromType to
|
|
|
|
// fill in the MIMEInfo.
|
|
|
|
if (NS_FAILED(giovfs->GetMimeTypeFromExtension(aFileExt, mimeType)) ||
|
|
|
|
mimeType.EqualsLiteral("application/octet-stream"))
|
|
|
|
return nsnull;
|
|
|
|
} else if (gnomevfs) {
|
|
|
|
/* Fallback to GnomeVFS */
|
|
|
|
if (NS_FAILED(gnomevfs->GetMimeTypeFromExtension(aFileExt, mimeType)) ||
|
|
|
|
mimeType.EqualsLiteral("application/octet-stream"))
|
|
|
|
return nsnull;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
return GetFromType(mimeType);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ already_AddRefed<nsMIMEInfoBase>
|
2008-01-29 20:36:11 -08:00
|
|
|
nsGNOMERegistry::GetFromType(const nsACString& aMIMEType)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-08-25 11:58:11 -07:00
|
|
|
nsCOMPtr<nsIGIOService> giovfs = do_GetService(NS_GIOSERVICE_CONTRACTID);
|
|
|
|
nsCOMPtr<nsIGnomeVFSService> gnomevfs = do_GetService(NS_GNOMEVFSSERVICE_CONTRACTID);
|
|
|
|
nsCOMPtr<nsIGIOMimeApp> gioHandlerApp;
|
|
|
|
nsCOMPtr<nsIGnomeVFSMimeApp> gnomeHandlerApp;
|
|
|
|
|
|
|
|
if (!giovfs && !gnomevfs)
|
2007-03-22 10:30:00 -07:00
|
|
|
return nsnull;
|
|
|
|
|
2009-08-25 11:58:11 -07:00
|
|
|
if (giovfs) {
|
|
|
|
if (NS_FAILED(giovfs->GetAppForMimeType(aMIMEType, getter_AddRefs(gioHandlerApp))) ||
|
|
|
|
!gioHandlerApp)
|
|
|
|
return nsnull;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
/* Fallback to GnomeVFS*/
|
|
|
|
if (NS_FAILED(gnomevfs->GetAppForMimeType(aMIMEType, getter_AddRefs(gnomeHandlerApp))) ||
|
|
|
|
!gnomeHandlerApp)
|
|
|
|
return nsnull;
|
|
|
|
|
|
|
|
}
|
2007-07-25 22:52:27 -07:00
|
|
|
nsRefPtr<nsMIMEInfoUnix> mimeInfo = new nsMIMEInfoUnix(aMIMEType);
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ENSURE_TRUE(mimeInfo, nsnull);
|
|
|
|
|
2008-01-29 20:36:11 -08:00
|
|
|
nsCAutoString description;
|
2009-08-25 11:58:11 -07:00
|
|
|
if (giovfs)
|
|
|
|
giovfs->GetDescriptionForMimeType(aMIMEType, description);
|
|
|
|
else
|
|
|
|
gnomevfs->GetDescriptionForMimeType(aMIMEType, description);
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
mimeInfo->SetDescription(NS_ConvertUTF8toUTF16(description));
|
|
|
|
|
2008-02-26 16:58:15 -08:00
|
|
|
nsCAutoString name;
|
2009-08-25 11:58:11 -07:00
|
|
|
if (giovfs)
|
|
|
|
gioHandlerApp->GetName(name);
|
|
|
|
else
|
|
|
|
gnomeHandlerApp->GetName(name);
|
|
|
|
|
2010-02-09 11:02:11 -08:00
|
|
|
#ifdef MOZ_PLATFORM_MAEMO
|
2009-03-30 11:10:26 -07:00
|
|
|
// On Maemo/Hildon, GetName ends up calling gnome_vfs_mime_application_get_name,
|
|
|
|
// which happens to return a non-localized message-id for the application. To
|
|
|
|
// get the localized name for the application, we have to call dgettext with
|
|
|
|
// the default maemo domain-name to try and translate the string into the operating
|
|
|
|
// system's native language.
|
|
|
|
const char kDefaultTextDomain [] = "maemo-af-desktop";
|
|
|
|
nsCAutoString realName (dgettext(kDefaultTextDomain, PromiseFlatCString(name).get()));
|
|
|
|
mimeInfo->SetDefaultDescription(NS_ConvertUTF8toUTF16(realName));
|
|
|
|
#else
|
2008-02-26 16:58:15 -08:00
|
|
|
mimeInfo->SetDefaultDescription(NS_ConvertUTF8toUTF16(name));
|
2009-03-30 11:10:26 -07:00
|
|
|
#endif
|
2008-02-26 16:58:15 -08:00
|
|
|
mimeInfo->SetPreferredAction(nsIMIMEInfo::useSystemDefault);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsMIMEInfoBase* retval;
|
|
|
|
NS_ADDREF((retval = mimeInfo));
|
|
|
|
return retval;
|
|
|
|
}
|