b=611953 Port desktop background settings to GSettings r=karlt

--HG--
extra : rebase_source : 0c573c2b50b6766367ecf37d9688ce192ee223e1
This commit is contained in:
Chris Coulson ext:(%20and%20jhorak%40redhat.com) 2011-05-12 20:15:16 +12:00
parent ea11341c5b
commit 3d45b28f5a

View File

@ -46,6 +46,7 @@
#include "nsStringAPI.h"
#include "nsIGConfService.h"
#include "nsIGIOService.h"
#include "nsIGSettingsService.h"
#include "nsIStringBundle.h"
#include "nsIOutputStream.h"
#include "nsIProcess.h"
@ -101,19 +102,27 @@ static const char kDesktopOptionsKey[] = DG_BACKGROUND "/picture_options";
static const char kDesktopDrawBGKey[] = DG_BACKGROUND "/draw_background";
static const char kDesktopColorKey[] = DG_BACKGROUND "/primary_color";
static const char kDesktopBGSchema[] = "org.gnome.desktop.background";
static const char kDesktopImageGSKey[] = "picture-uri";
static const char kDesktopOptionGSKey[] = "picture-options";
static const char kDesktopDrawBGGSKey[] = "draw-background";
static const char kDesktopColorGSKey[] = "primary-color";
nsresult
nsGNOMEShellService::Init()
{
nsresult rv;
// GConf or GIO _must_ be available, or we do not allow
// GConf, GSettings or GIO _must_ be available, or we do not allow
// CreateInstance to succeed.
nsCOMPtr<nsIGConfService> gconf = do_GetService(NS_GCONFSERVICE_CONTRACTID);
nsCOMPtr<nsIGIOService> giovfs =
do_GetService(NS_GIOSERVICE_CONTRACTID);
nsCOMPtr<nsIGSettingsService> gsettings =
do_GetService(NS_GSETTINGSSERVICE_CONTRACTID);
if (!gconf && !giovfs)
if (!gconf && !giovfs && !gsettings)
return NS_ERROR_NOT_AVAILABLE;
// Check G_BROKEN_FILENAMES. If it's set, then filenames in glib use
@ -412,6 +421,15 @@ nsGNOMEShellService::SetDesktopBackground(nsIDOMElement* aElement,
rv = request->GetImage(getter_AddRefs(container));
if (!container) return rv;
// Set desktop wallpaper filling style
nsCAutoString options;
if (aPosition == BACKGROUND_TILE)
options.Assign("wallpaper");
else if (aPosition == BACKGROUND_STRETCH)
options.Assign("stretched");
else
options.Assign("centered");
// Write the background file to the home directory.
nsCAutoString filePath(PR_GetEnv("HOME"));
@ -437,19 +455,38 @@ nsGNOMEShellService::SetDesktopBackground(nsIDOMElement* aElement,
// write the image to a file in the home dir
rv = WriteImage(filePath, container);
NS_ENSURE_SUCCESS(rv, rv);
// Try GSettings first. If we don't have GSettings or the right schema, fall back
// to using GConf instead. Note that if GSettings works ok, the changes get
// mirrored to GConf by the gsettings->gconf bridge in gnome-settings-daemon
nsCOMPtr<nsIGSettingsService> gsettings =
do_GetService(NS_GSETTINGSSERVICE_CONTRACTID);
if (gsettings) {
nsCOMPtr<nsIGSettingsCollection> background_settings;
gsettings->GetCollectionForSchema(
NS_LITERAL_CSTRING(kDesktopBGSchema), getter_AddRefs(background_settings));
if (background_settings) {
gchar *file_uri = g_filename_to_uri(filePath.get(), NULL, NULL);
if (!file_uri)
return NS_ERROR_FAILURE;
background_settings->SetString(NS_LITERAL_CSTRING(kDesktopOptionGSKey),
options);
background_settings->SetString(NS_LITERAL_CSTRING(kDesktopImageGSKey),
nsDependentCString(file_uri));
g_free(file_uri);
background_settings->SetBoolean(NS_LITERAL_CSTRING(kDesktopDrawBGGSKey),
PR_TRUE);
return rv;
}
}
// if the file was written successfully, set it as the system wallpaper
nsCOMPtr<nsIGConfService> gconf = do_GetService(NS_GCONFSERVICE_CONTRACTID);
if (gconf) {
nsCAutoString options;
if (aPosition == BACKGROUND_TILE)
options.Assign("wallpaper");
else if (aPosition == BACKGROUND_STRETCH)
options.Assign("stretched");
else
options.Assign("centered");
gconf->SetString(NS_LITERAL_CSTRING(kDesktopOptionsKey), options);
// Set the image to an empty string first to force a refresh
@ -471,11 +508,24 @@ nsGNOMEShellService::SetDesktopBackground(nsIDOMElement* aElement,
NS_IMETHODIMP
nsGNOMEShellService::GetDesktopBackgroundColor(PRUint32 *aColor)
{
nsCOMPtr<nsIGConfService> gconf = do_GetService(NS_GCONFSERVICE_CONTRACTID);
nsCOMPtr<nsIGSettingsService> gsettings =
do_GetService(NS_GSETTINGSSERVICE_CONTRACTID);
nsCOMPtr<nsIGSettingsCollection> background_settings;
nsCAutoString background;
if (gconf) {
gconf->GetString(NS_LITERAL_CSTRING(kDesktopColorKey), background);
if (gsettings) {
gsettings->GetCollectionForSchema(
NS_LITERAL_CSTRING(kDesktopBGSchema), getter_AddRefs(background_settings));
if (background_settings) {
background_settings->GetString(NS_LITERAL_CSTRING(kDesktopColorGSKey),
background);
}
}
if (!background_settings) {
nsCOMPtr<nsIGConfService> gconf = do_GetService(NS_GCONFSERVICE_CONTRACTID);
if (gconf)
gconf->GetString(NS_LITERAL_CSTRING(kDesktopColorKey), background);
}
if (background.IsEmpty()) {
@ -513,12 +563,25 @@ NS_IMETHODIMP
nsGNOMEShellService::SetDesktopBackgroundColor(PRUint32 aColor)
{
NS_ASSERTION(aColor <= 0xffffff, "aColor has extra bits");
nsCAutoString colorString;
ColorToCString(aColor, colorString);
nsCOMPtr<nsIGSettingsService> gsettings =
do_GetService(NS_GSETTINGSSERVICE_CONTRACTID);
if (gsettings) {
nsCOMPtr<nsIGSettingsCollection> background_settings;
gsettings->GetCollectionForSchema(
NS_LITERAL_CSTRING(kDesktopBGSchema), getter_AddRefs(background_settings));
if (background_settings) {
background_settings->SetString(NS_LITERAL_CSTRING(kDesktopColorGSKey),
colorString);
return NS_OK;
}
}
nsCOMPtr<nsIGConfService> gconf = do_GetService(NS_GCONFSERVICE_CONTRACTID);
if (gconf) {
nsCAutoString colorString;
ColorToCString(aColor, colorString);
gconf->SetString(NS_LITERAL_CSTRING(kDesktopColorKey), colorString);
}