michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 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: #include "mozilla/ArrayUtils.h" michael@0: michael@0: #include "nsCOMPtr.h" michael@0: #include "nsGNOMEShellService.h" michael@0: #include "nsShellService.h" michael@0: #include "nsIServiceManager.h" michael@0: #include "nsIFile.h" michael@0: #include "nsIProperties.h" michael@0: #include "nsDirectoryServiceDefs.h" michael@0: #include "nsIPrefService.h" michael@0: #include "prenv.h" michael@0: #include "nsStringAPI.h" michael@0: #include "nsIGConfService.h" michael@0: #include "nsIGIOService.h" michael@0: #include "nsIGSettingsService.h" michael@0: #include "nsIStringBundle.h" michael@0: #include "nsIOutputStream.h" michael@0: #include "nsIProcess.h" michael@0: #include "nsNetUtil.h" michael@0: #include "nsIDOMHTMLImageElement.h" michael@0: #include "nsIImageLoadingContent.h" michael@0: #include "imgIRequest.h" michael@0: #include "imgIContainer.h" michael@0: #include "prprf.h" michael@0: #if defined(MOZ_WIDGET_GTK) michael@0: #include "nsIImageToPixbuf.h" michael@0: #endif michael@0: #include "nsXULAppAPI.h" michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: michael@0: using namespace mozilla; michael@0: michael@0: struct ProtocolAssociation michael@0: { michael@0: const char *name; michael@0: bool essential; michael@0: }; michael@0: michael@0: struct MimeTypeAssociation michael@0: { michael@0: const char *mimeType; michael@0: const char *extensions; michael@0: }; michael@0: michael@0: static const ProtocolAssociation appProtocols[] = { michael@0: { "http", true }, michael@0: { "https", true }, michael@0: { "ftp", false }, michael@0: { "chrome", false } michael@0: }; michael@0: michael@0: static const MimeTypeAssociation appTypes[] = { michael@0: { "text/html", "htm html shtml" }, michael@0: { "application/xhtml+xml", "xhtml xht" } michael@0: }; michael@0: michael@0: // GConf registry key constants michael@0: #define DG_BACKGROUND "/desktop/gnome/background" michael@0: michael@0: static const char kDesktopImageKey[] = DG_BACKGROUND "/picture_filename"; michael@0: static const char kDesktopOptionsKey[] = DG_BACKGROUND "/picture_options"; michael@0: static const char kDesktopDrawBGKey[] = DG_BACKGROUND "/draw_background"; michael@0: static const char kDesktopColorKey[] = DG_BACKGROUND "/primary_color"; michael@0: michael@0: static const char kDesktopBGSchema[] = "org.gnome.desktop.background"; michael@0: static const char kDesktopImageGSKey[] = "picture-uri"; michael@0: static const char kDesktopOptionGSKey[] = "picture-options"; michael@0: static const char kDesktopDrawBGGSKey[] = "draw-background"; michael@0: static const char kDesktopColorGSKey[] = "primary-color"; michael@0: michael@0: nsresult michael@0: nsGNOMEShellService::Init() michael@0: { michael@0: nsresult rv; michael@0: michael@0: // GConf, GSettings or GIO _must_ be available, or we do not allow michael@0: // CreateInstance to succeed. michael@0: michael@0: nsCOMPtr gconf = do_GetService(NS_GCONFSERVICE_CONTRACTID); michael@0: nsCOMPtr giovfs = michael@0: do_GetService(NS_GIOSERVICE_CONTRACTID); michael@0: nsCOMPtr gsettings = michael@0: do_GetService(NS_GSETTINGSSERVICE_CONTRACTID); michael@0: michael@0: if (!gconf && !giovfs && !gsettings) michael@0: return NS_ERROR_NOT_AVAILABLE; michael@0: michael@0: // Check G_BROKEN_FILENAMES. If it's set, then filenames in glib use michael@0: // the locale encoding. If it's not set, they use UTF-8. michael@0: mUseLocaleFilenames = PR_GetEnv("G_BROKEN_FILENAMES") != nullptr; michael@0: michael@0: if (GetAppPathFromLauncher()) michael@0: return NS_OK; michael@0: michael@0: nsCOMPtr dirSvc michael@0: (do_GetService("@mozilla.org/file/directory_service;1")); michael@0: NS_ENSURE_TRUE(dirSvc, NS_ERROR_NOT_AVAILABLE); michael@0: michael@0: nsCOMPtr appPath; michael@0: rv = dirSvc->Get(XRE_EXECUTABLE_FILE, NS_GET_IID(nsIFile), michael@0: getter_AddRefs(appPath)); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: return appPath->GetNativePath(mAppPath); michael@0: } michael@0: michael@0: NS_IMPL_ISUPPORTS(nsGNOMEShellService, nsIShellService) michael@0: michael@0: bool michael@0: nsGNOMEShellService::GetAppPathFromLauncher() michael@0: { michael@0: gchar *tmp; michael@0: michael@0: const char *launcher = PR_GetEnv("MOZ_APP_LAUNCHER"); michael@0: if (!launcher) michael@0: return false; michael@0: michael@0: if (g_path_is_absolute(launcher)) { michael@0: mAppPath = launcher; michael@0: tmp = g_path_get_basename(launcher); michael@0: gchar *fullpath = g_find_program_in_path(tmp); michael@0: if (fullpath && mAppPath.Equals(fullpath)) michael@0: mAppIsInPath = true; michael@0: g_free(fullpath); michael@0: } else { michael@0: tmp = g_find_program_in_path(launcher); michael@0: if (!tmp) michael@0: return false; michael@0: mAppPath = tmp; michael@0: mAppIsInPath = true; michael@0: } michael@0: michael@0: g_free(tmp); michael@0: return true; michael@0: } michael@0: michael@0: bool michael@0: nsGNOMEShellService::KeyMatchesAppName(const char *aKeyValue) const michael@0: { michael@0: michael@0: gchar *commandPath; michael@0: if (mUseLocaleFilenames) { michael@0: gchar *nativePath = g_filename_from_utf8(aKeyValue, -1, michael@0: nullptr, nullptr, nullptr); michael@0: if (!nativePath) { michael@0: NS_ERROR("Error converting path to filesystem encoding"); michael@0: return false; michael@0: } michael@0: michael@0: commandPath = g_find_program_in_path(nativePath); michael@0: g_free(nativePath); michael@0: } else { michael@0: commandPath = g_find_program_in_path(aKeyValue); michael@0: } michael@0: michael@0: if (!commandPath) michael@0: return false; michael@0: michael@0: bool matches = mAppPath.Equals(commandPath); michael@0: g_free(commandPath); michael@0: return matches; michael@0: } michael@0: michael@0: bool michael@0: nsGNOMEShellService::CheckHandlerMatchesAppName(const nsACString &handler) const michael@0: { michael@0: gint argc; michael@0: gchar **argv; michael@0: nsAutoCString command(handler); michael@0: michael@0: // The string will be something of the form: [/path/to/]browser "%s" michael@0: // We want to remove all of the parameters and get just the binary name. michael@0: michael@0: if (g_shell_parse_argv(command.get(), &argc, &argv, nullptr) && argc > 0) { michael@0: command.Assign(argv[0]); michael@0: g_strfreev(argv); michael@0: } michael@0: michael@0: if (!KeyMatchesAppName(command.get())) michael@0: return false; // the handler is set to another app michael@0: michael@0: return true; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsGNOMEShellService::IsDefaultBrowser(bool aStartupCheck, michael@0: bool aForAllTypes, michael@0: bool* aIsDefaultBrowser) michael@0: { michael@0: *aIsDefaultBrowser = false; michael@0: if (aStartupCheck) michael@0: mCheckedThisSession = true; michael@0: michael@0: nsCOMPtr gconf = do_GetService(NS_GCONFSERVICE_CONTRACTID); michael@0: nsCOMPtr giovfs = do_GetService(NS_GIOSERVICE_CONTRACTID); michael@0: michael@0: bool enabled; michael@0: nsAutoCString handler; michael@0: nsCOMPtr gioApp; michael@0: michael@0: for (unsigned int i = 0; i < ArrayLength(appProtocols); ++i) { michael@0: if (!appProtocols[i].essential) michael@0: continue; michael@0: michael@0: if (gconf) { michael@0: handler.Truncate(); michael@0: gconf->GetAppForProtocol(nsDependentCString(appProtocols[i].name), michael@0: &enabled, handler); michael@0: michael@0: if (!CheckHandlerMatchesAppName(handler) || !enabled) michael@0: return NS_OK; // the handler is disabled or set to another app michael@0: } michael@0: michael@0: if (giovfs) { michael@0: handler.Truncate(); michael@0: giovfs->GetAppForURIScheme(nsDependentCString(appProtocols[i].name), michael@0: getter_AddRefs(gioApp)); michael@0: if (!gioApp) michael@0: return NS_OK; michael@0: michael@0: gioApp->GetCommand(handler); michael@0: michael@0: if (!CheckHandlerMatchesAppName(handler)) michael@0: return NS_OK; // the handler is set to another app michael@0: } michael@0: } michael@0: michael@0: *aIsDefaultBrowser = true; michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsGNOMEShellService::SetDefaultBrowser(bool aClaimAllTypes, michael@0: bool aForAllUsers) michael@0: { michael@0: #ifdef DEBUG michael@0: if (aForAllUsers) michael@0: NS_WARNING("Setting the default browser for all users is not yet supported"); michael@0: #endif michael@0: michael@0: nsCOMPtr gconf = do_GetService(NS_GCONFSERVICE_CONTRACTID); michael@0: nsCOMPtr giovfs = do_GetService(NS_GIOSERVICE_CONTRACTID); michael@0: if (gconf) { michael@0: nsAutoCString appKeyValue; michael@0: if (mAppIsInPath) { michael@0: // mAppPath is in the users path, so use only the basename as the launcher michael@0: gchar *tmp = g_path_get_basename(mAppPath.get()); michael@0: appKeyValue = tmp; michael@0: g_free(tmp); michael@0: } else { michael@0: appKeyValue = mAppPath; michael@0: } michael@0: michael@0: appKeyValue.AppendLiteral(" %s"); michael@0: michael@0: for (unsigned int i = 0; i < ArrayLength(appProtocols); ++i) { michael@0: if (appProtocols[i].essential || aClaimAllTypes) { michael@0: gconf->SetAppForProtocol(nsDependentCString(appProtocols[i].name), michael@0: appKeyValue); michael@0: } michael@0: } michael@0: } michael@0: michael@0: if (giovfs) { michael@0: nsresult rv; michael@0: nsCOMPtr bundleService = michael@0: do_GetService(NS_STRINGBUNDLE_CONTRACTID, &rv); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: nsCOMPtr brandBundle; michael@0: rv = bundleService->CreateBundle(BRAND_PROPERTIES, getter_AddRefs(brandBundle)); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: nsString brandShortName; michael@0: brandBundle->GetStringFromName(MOZ_UTF16("brandShortName"), michael@0: getter_Copies(brandShortName)); michael@0: michael@0: // use brandShortName as the application id. michael@0: NS_ConvertUTF16toUTF8 id(brandShortName); michael@0: nsCOMPtr appInfo; michael@0: rv = giovfs->CreateAppFromCommand(mAppPath, michael@0: id, michael@0: getter_AddRefs(appInfo)); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: // set handler for the protocols michael@0: for (unsigned int i = 0; i < ArrayLength(appProtocols); ++i) { michael@0: if (appProtocols[i].essential || aClaimAllTypes) { michael@0: appInfo->SetAsDefaultForURIScheme(nsDependentCString(appProtocols[i].name)); michael@0: } michael@0: } michael@0: michael@0: // set handler for .html and xhtml files and MIME types: michael@0: if (aClaimAllTypes) { michael@0: // Add mime types for html, xhtml extension and set app to just created appinfo. michael@0: for (unsigned int i = 0; i < ArrayLength(appTypes); ++i) { michael@0: appInfo->SetAsDefaultForMimeType(nsDependentCString(appTypes[i].mimeType)); michael@0: appInfo->SetAsDefaultForFileExtensions(nsDependentCString(appTypes[i].extensions)); michael@0: } michael@0: } michael@0: } michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsGNOMEShellService::GetShouldCheckDefaultBrowser(bool* aResult) michael@0: { michael@0: // If we've already checked, the browser has been started and this is a michael@0: // new window open, and we don't want to check again. michael@0: if (mCheckedThisSession) { michael@0: *aResult = false; michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsCOMPtr prefs; michael@0: nsCOMPtr pserve(do_GetService(NS_PREFSERVICE_CONTRACTID)); michael@0: if (pserve) michael@0: pserve->GetBranch("", getter_AddRefs(prefs)); michael@0: michael@0: if (prefs) michael@0: prefs->GetBoolPref(PREF_CHECKDEFAULTBROWSER, aResult); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsGNOMEShellService::SetShouldCheckDefaultBrowser(bool aShouldCheck) michael@0: { michael@0: nsCOMPtr prefs; michael@0: nsCOMPtr pserve(do_GetService(NS_PREFSERVICE_CONTRACTID)); michael@0: if (pserve) michael@0: pserve->GetBranch("", getter_AddRefs(prefs)); michael@0: michael@0: if (prefs) michael@0: prefs->SetBoolPref(PREF_CHECKDEFAULTBROWSER, aShouldCheck); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsGNOMEShellService::GetCanSetDesktopBackground(bool* aResult) michael@0: { michael@0: // setting desktop background is currently only supported michael@0: // for Gnome or desktops using the same GSettings and GConf keys michael@0: const char* gnomeSession = getenv("GNOME_DESKTOP_SESSION_ID"); michael@0: if (gnomeSession) { michael@0: *aResult = true; michael@0: } else { michael@0: *aResult = false; michael@0: } michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: static nsresult michael@0: WriteImage(const nsCString& aPath, imgIContainer* aImage) michael@0: { michael@0: #if !defined(MOZ_WIDGET_GTK) michael@0: return NS_ERROR_NOT_AVAILABLE; michael@0: #else michael@0: nsCOMPtr imgToPixbuf = michael@0: do_GetService("@mozilla.org/widget/image-to-gdk-pixbuf;1"); michael@0: if (!imgToPixbuf) michael@0: return NS_ERROR_NOT_AVAILABLE; michael@0: michael@0: GdkPixbuf* pixbuf = imgToPixbuf->ConvertImageToPixbuf(aImage); michael@0: if (!pixbuf) michael@0: return NS_ERROR_NOT_AVAILABLE; michael@0: michael@0: gboolean res = gdk_pixbuf_save(pixbuf, aPath.get(), "png", nullptr, nullptr); michael@0: michael@0: g_object_unref(pixbuf); michael@0: return res ? NS_OK : NS_ERROR_FAILURE; michael@0: #endif michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsGNOMEShellService::SetDesktopBackground(nsIDOMElement* aElement, michael@0: int32_t aPosition) michael@0: { michael@0: nsresult rv; michael@0: nsCOMPtr imageContent = do_QueryInterface(aElement, &rv); michael@0: if (!imageContent) return rv; michael@0: michael@0: // get the image container michael@0: nsCOMPtr request; michael@0: rv = imageContent->GetRequest(nsIImageLoadingContent::CURRENT_REQUEST, michael@0: getter_AddRefs(request)); michael@0: if (!request) return rv; michael@0: nsCOMPtr container; michael@0: rv = request->GetImage(getter_AddRefs(container)); michael@0: if (!container) return rv; michael@0: michael@0: // Set desktop wallpaper filling style michael@0: nsAutoCString options; michael@0: if (aPosition == BACKGROUND_TILE) michael@0: options.Assign("wallpaper"); michael@0: else if (aPosition == BACKGROUND_STRETCH) michael@0: options.Assign("stretched"); michael@0: else if (aPosition == BACKGROUND_FILL) michael@0: options.Assign("zoom"); michael@0: else if (aPosition == BACKGROUND_FIT) michael@0: options.Assign("scaled"); michael@0: else michael@0: options.Assign("centered"); michael@0: michael@0: // Write the background file to the home directory. michael@0: nsAutoCString filePath(PR_GetEnv("HOME")); michael@0: michael@0: // get the product brand name from localized strings michael@0: nsString brandName; michael@0: nsCID bundleCID = NS_STRINGBUNDLESERVICE_CID; michael@0: nsCOMPtr bundleService(do_GetService(bundleCID)); michael@0: if (bundleService) { michael@0: nsCOMPtr brandBundle; michael@0: rv = bundleService->CreateBundle(BRAND_PROPERTIES, michael@0: getter_AddRefs(brandBundle)); michael@0: if (NS_SUCCEEDED(rv) && brandBundle) { michael@0: rv = brandBundle->GetStringFromName(MOZ_UTF16("brandShortName"), michael@0: getter_Copies(brandName)); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: } michael@0: } michael@0: michael@0: // build the file name michael@0: filePath.Append('/'); michael@0: filePath.Append(NS_ConvertUTF16toUTF8(brandName)); michael@0: filePath.Append("_wallpaper.png"); michael@0: michael@0: // write the image to a file in the home dir michael@0: rv = WriteImage(filePath, container); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: // Try GSettings first. If we don't have GSettings or the right schema, fall back michael@0: // to using GConf instead. Note that if GSettings works ok, the changes get michael@0: // mirrored to GConf by the gsettings->gconf bridge in gnome-settings-daemon michael@0: nsCOMPtr gsettings = michael@0: do_GetService(NS_GSETTINGSSERVICE_CONTRACTID); michael@0: if (gsettings) { michael@0: nsCOMPtr background_settings; michael@0: gsettings->GetCollectionForSchema( michael@0: NS_LITERAL_CSTRING(kDesktopBGSchema), getter_AddRefs(background_settings)); michael@0: if (background_settings) { michael@0: gchar *file_uri = g_filename_to_uri(filePath.get(), nullptr, nullptr); michael@0: if (!file_uri) michael@0: return NS_ERROR_FAILURE; michael@0: michael@0: background_settings->SetString(NS_LITERAL_CSTRING(kDesktopOptionGSKey), michael@0: options); michael@0: michael@0: background_settings->SetString(NS_LITERAL_CSTRING(kDesktopImageGSKey), michael@0: nsDependentCString(file_uri)); michael@0: g_free(file_uri); michael@0: background_settings->SetBoolean(NS_LITERAL_CSTRING(kDesktopDrawBGGSKey), michael@0: true); michael@0: return rv; michael@0: } michael@0: } michael@0: michael@0: // if the file was written successfully, set it as the system wallpaper michael@0: nsCOMPtr gconf = do_GetService(NS_GCONFSERVICE_CONTRACTID); michael@0: michael@0: if (gconf) { michael@0: gconf->SetString(NS_LITERAL_CSTRING(kDesktopOptionsKey), options); michael@0: michael@0: // Set the image to an empty string first to force a refresh michael@0: // (since we could be writing a new image on top of an existing michael@0: // Firefox_wallpaper.png and nautilus doesn't monitor the file for changes) michael@0: gconf->SetString(NS_LITERAL_CSTRING(kDesktopImageKey), michael@0: EmptyCString()); michael@0: michael@0: gconf->SetString(NS_LITERAL_CSTRING(kDesktopImageKey), filePath); michael@0: gconf->SetBool(NS_LITERAL_CSTRING(kDesktopDrawBGKey), true); michael@0: } michael@0: michael@0: return rv; michael@0: } michael@0: michael@0: #define COLOR_16_TO_8_BIT(_c) ((_c) >> 8) michael@0: #define COLOR_8_TO_16_BIT(_c) ((_c) << 8 | (_c)) michael@0: michael@0: NS_IMETHODIMP michael@0: nsGNOMEShellService::GetDesktopBackgroundColor(uint32_t *aColor) michael@0: { michael@0: nsCOMPtr gsettings = michael@0: do_GetService(NS_GSETTINGSSERVICE_CONTRACTID); michael@0: nsCOMPtr background_settings; michael@0: nsAutoCString background; michael@0: michael@0: if (gsettings) { michael@0: gsettings->GetCollectionForSchema( michael@0: NS_LITERAL_CSTRING(kDesktopBGSchema), getter_AddRefs(background_settings)); michael@0: if (background_settings) { michael@0: background_settings->GetString(NS_LITERAL_CSTRING(kDesktopColorGSKey), michael@0: background); michael@0: } michael@0: } michael@0: michael@0: if (!background_settings) { michael@0: nsCOMPtr gconf = do_GetService(NS_GCONFSERVICE_CONTRACTID); michael@0: if (gconf) michael@0: gconf->GetString(NS_LITERAL_CSTRING(kDesktopColorKey), background); michael@0: } michael@0: michael@0: if (background.IsEmpty()) { michael@0: *aColor = 0; michael@0: return NS_OK; michael@0: } michael@0: michael@0: GdkColor color; michael@0: gboolean success = gdk_color_parse(background.get(), &color); michael@0: michael@0: NS_ENSURE_TRUE(success, NS_ERROR_FAILURE); michael@0: michael@0: *aColor = COLOR_16_TO_8_BIT(color.red) << 16 | michael@0: COLOR_16_TO_8_BIT(color.green) << 8 | michael@0: COLOR_16_TO_8_BIT(color.blue); michael@0: return NS_OK; michael@0: } michael@0: michael@0: static void michael@0: ColorToCString(uint32_t aColor, nsCString& aResult) michael@0: { michael@0: // The #rrrrggggbbbb format is used to match gdk_color_to_string() michael@0: char *buf = aResult.BeginWriting(13); michael@0: if (!buf) michael@0: return; michael@0: michael@0: uint16_t red = COLOR_8_TO_16_BIT((aColor >> 16) & 0xff); michael@0: uint16_t green = COLOR_8_TO_16_BIT((aColor >> 8) & 0xff); michael@0: uint16_t blue = COLOR_8_TO_16_BIT(aColor & 0xff); michael@0: michael@0: PR_snprintf(buf, 14, "#%04x%04x%04x", red, green, blue); michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsGNOMEShellService::SetDesktopBackgroundColor(uint32_t aColor) michael@0: { michael@0: NS_ASSERTION(aColor <= 0xffffff, "aColor has extra bits"); michael@0: nsAutoCString colorString; michael@0: ColorToCString(aColor, colorString); michael@0: michael@0: nsCOMPtr gsettings = michael@0: do_GetService(NS_GSETTINGSSERVICE_CONTRACTID); michael@0: if (gsettings) { michael@0: nsCOMPtr background_settings; michael@0: gsettings->GetCollectionForSchema( michael@0: NS_LITERAL_CSTRING(kDesktopBGSchema), getter_AddRefs(background_settings)); michael@0: if (background_settings) { michael@0: background_settings->SetString(NS_LITERAL_CSTRING(kDesktopColorGSKey), michael@0: colorString); michael@0: return NS_OK; michael@0: } michael@0: } michael@0: michael@0: nsCOMPtr gconf = do_GetService(NS_GCONFSERVICE_CONTRACTID); michael@0: michael@0: if (gconf) { michael@0: gconf->SetString(NS_LITERAL_CSTRING(kDesktopColorKey), colorString); michael@0: } michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsGNOMEShellService::OpenApplication(int32_t aApplication) michael@0: { michael@0: nsAutoCString scheme; michael@0: if (aApplication == APPLICATION_MAIL) michael@0: scheme.Assign("mailto"); michael@0: else if (aApplication == APPLICATION_NEWS) michael@0: scheme.Assign("news"); michael@0: else michael@0: return NS_ERROR_NOT_AVAILABLE; michael@0: michael@0: nsCOMPtr giovfs = do_GetService(NS_GIOSERVICE_CONTRACTID); michael@0: if (giovfs) { michael@0: nsCOMPtr gioApp; michael@0: giovfs->GetAppForURIScheme(scheme, getter_AddRefs(gioApp)); michael@0: if (gioApp) michael@0: return gioApp->Launch(EmptyCString()); michael@0: } michael@0: michael@0: nsCOMPtr gconf = do_GetService(NS_GCONFSERVICE_CONTRACTID); michael@0: if (!gconf) michael@0: return NS_ERROR_FAILURE; michael@0: michael@0: bool enabled; michael@0: nsAutoCString appCommand; michael@0: gconf->GetAppForProtocol(scheme, &enabled, appCommand); michael@0: michael@0: if (!enabled) michael@0: return NS_ERROR_FAILURE; michael@0: michael@0: // XXX we don't currently handle launching a terminal window. michael@0: // If the handler requires a terminal, bail. michael@0: bool requiresTerminal; michael@0: gconf->HandlerRequiresTerminal(scheme, &requiresTerminal); michael@0: if (requiresTerminal) michael@0: return NS_ERROR_FAILURE; michael@0: michael@0: // Perform shell argument expansion michael@0: int argc; michael@0: char **argv; michael@0: if (!g_shell_parse_argv(appCommand.get(), &argc, &argv, nullptr)) michael@0: return NS_ERROR_FAILURE; michael@0: michael@0: char **newArgv = new char*[argc + 1]; michael@0: int newArgc = 0; michael@0: michael@0: // Run through the list of arguments. Copy all of them to the new michael@0: // argv except for %s, which we skip. michael@0: for (int i = 0; i < argc; ++i) { michael@0: if (strcmp(argv[i], "%s") != 0) michael@0: newArgv[newArgc++] = argv[i]; michael@0: } michael@0: michael@0: newArgv[newArgc] = nullptr; michael@0: michael@0: gboolean err = g_spawn_async(nullptr, newArgv, nullptr, G_SPAWN_SEARCH_PATH, michael@0: nullptr, nullptr, nullptr, nullptr); michael@0: michael@0: g_strfreev(argv); michael@0: delete[] newArgv; michael@0: michael@0: return err ? NS_OK : NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsGNOMEShellService::OpenApplicationWithURI(nsIFile* aApplication, const nsACString& aURI) michael@0: { michael@0: nsresult rv; michael@0: nsCOMPtr process = michael@0: do_CreateInstance("@mozilla.org/process/util;1", &rv); michael@0: if (NS_FAILED(rv)) michael@0: return rv; michael@0: michael@0: rv = process->Init(aApplication); michael@0: if (NS_FAILED(rv)) michael@0: return rv; michael@0: michael@0: const nsCString spec(aURI); michael@0: const char* specStr = spec.get(); michael@0: return process->Run(false, &specStr, 1); michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsGNOMEShellService::GetDefaultFeedReader(nsIFile** _retval) michael@0: { michael@0: return NS_ERROR_NOT_IMPLEMENTED; michael@0: }