2006-07-27 14:42:23 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 11:12:37 +00:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2006-07-27 14:42:23 +00:00
|
|
|
|
2013-12-09 02:52:54 +00:00
|
|
|
#include "mozilla/ArrayUtils.h"
|
2006-07-27 14:42:23 +00:00
|
|
|
#include "nsGConfService.h"
|
2006-07-27 14:42:26 +00:00
|
|
|
#include "nsStringAPI.h"
|
2008-01-29 18:38:15 +00:00
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsComponentManagerUtils.h"
|
|
|
|
#include "nsISupportsPrimitives.h"
|
|
|
|
#include "nsIMutableArray.h"
|
2012-01-04 22:19:46 +00:00
|
|
|
#include "prlink.h"
|
2006-07-27 14:42:23 +00:00
|
|
|
|
|
|
|
#include <gconf/gconf-client.h>
|
|
|
|
|
2012-01-04 22:19:46 +00:00
|
|
|
using namespace mozilla;
|
|
|
|
|
|
|
|
#define GCONF_FUNCTIONS \
|
|
|
|
FUNC(gconf_client_get_default, GConfClient*, (void)) \
|
|
|
|
FUNC(gconf_client_get_bool, gboolean, (GConfClient*, const gchar*, GError**)) \
|
|
|
|
FUNC(gconf_client_get_string, gchar*, (GConfClient*, const gchar*, GError**)) \
|
|
|
|
FUNC(gconf_client_get_int, gint, (GConfClient*, const gchar*, GError**)) \
|
|
|
|
FUNC(gconf_client_get_float, gdouble, (GConfClient*, const gchar*, GError**)) \
|
|
|
|
FUNC(gconf_client_get_list, GSList*, (GConfClient*, const gchar*, GConfValueType, GError**)) \
|
|
|
|
FUNC(gconf_client_set_bool, gboolean, (GConfClient*, const gchar*, gboolean, GError**)) \
|
|
|
|
FUNC(gconf_client_set_string, gboolean, (GConfClient*, const gchar*, const gchar*, GError**)) \
|
|
|
|
FUNC(gconf_client_set_int, gboolean, (GConfClient*, const gchar*, gint, GError**)) \
|
|
|
|
FUNC(gconf_client_set_float, gboolean, (GConfClient*, const gchar*, gdouble, GError**)) \
|
|
|
|
FUNC(gconf_client_unset, gboolean, (GConfClient*, const gchar*, GError**))
|
|
|
|
|
|
|
|
#define FUNC(name, type, params) \
|
|
|
|
typedef type (*_##name##_fn) params; \
|
|
|
|
static _##name##_fn _##name;
|
|
|
|
|
|
|
|
GCONF_FUNCTIONS
|
|
|
|
|
|
|
|
#undef FUNC
|
|
|
|
|
|
|
|
#define gconf_client_get_default _gconf_client_get_default
|
|
|
|
#define gconf_client_get_bool _gconf_client_get_bool
|
|
|
|
#define gconf_client_get_string _gconf_client_get_string
|
|
|
|
#define gconf_client_get_int _gconf_client_get_int
|
|
|
|
#define gconf_client_get_float _gconf_client_get_float
|
|
|
|
#define gconf_client_get_list _gconf_client_get_list
|
|
|
|
#define gconf_client_set_bool _gconf_client_set_bool
|
|
|
|
#define gconf_client_set_string _gconf_client_set_string
|
|
|
|
#define gconf_client_set_int _gconf_client_set_int
|
|
|
|
#define gconf_client_set_float _gconf_client_set_float
|
|
|
|
#define gconf_client_unset _gconf_client_unset
|
|
|
|
|
2012-07-30 14:20:58 +00:00
|
|
|
static PRLibrary *gconfLib = nullptr;
|
2012-01-04 22:19:46 +00:00
|
|
|
|
|
|
|
typedef void (*nsGConfFunc)();
|
|
|
|
struct nsGConfDynamicFunction {
|
|
|
|
const char *functionName;
|
|
|
|
nsGConfFunc *function;
|
|
|
|
};
|
|
|
|
|
2006-07-27 14:42:23 +00:00
|
|
|
nsGConfService::~nsGConfService()
|
|
|
|
{
|
|
|
|
if (mClient)
|
|
|
|
g_object_unref(mClient);
|
2012-01-04 22:19:46 +00:00
|
|
|
|
|
|
|
// We don't unload gconf here because liborbit uses atexit(). In addition to
|
|
|
|
// this, it's not a good idea to unload any gobject based library, as it
|
|
|
|
// leaves types registered in glib's type system
|
2006-07-27 14:42:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsGConfService::Init()
|
|
|
|
{
|
2012-01-04 22:19:46 +00:00
|
|
|
#define FUNC(name, type, params) { #name, (nsGConfFunc *)&_##name },
|
|
|
|
static const nsGConfDynamicFunction kGConfSymbols[] = {
|
|
|
|
GCONF_FUNCTIONS
|
|
|
|
};
|
|
|
|
#undef FUNC
|
|
|
|
|
|
|
|
if (!gconfLib) {
|
|
|
|
gconfLib = PR_LoadLibrary("libgconf-2.so.4");
|
|
|
|
if (!gconfLib)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
for (uint32_t i = 0; i < ArrayLength(kGConfSymbols); i++) {
|
2012-01-04 22:19:46 +00:00
|
|
|
*kGConfSymbols[i].function =
|
|
|
|
PR_FindFunctionSymbol(gconfLib, kGConfSymbols[i].functionName);
|
|
|
|
if (!*kGConfSymbols[i].function) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-07-27 14:42:23 +00:00
|
|
|
mClient = gconf_client_get_default();
|
|
|
|
return mClient ? NS_OK : NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS1(nsGConfService, nsIGConfService)
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-29 06:19:26 +00:00
|
|
|
nsGConfService::GetBool(const nsACString &aKey, bool *aResult)
|
2006-07-27 14:42:23 +00:00
|
|
|
{
|
2012-07-30 14:20:58 +00:00
|
|
|
GError* error = nullptr;
|
2006-07-27 14:42:23 +00:00
|
|
|
*aResult = gconf_client_get_bool(mClient, PromiseFlatCString(aKey).get(),
|
|
|
|
&error);
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
g_error_free(error);
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsGConfService::GetString(const nsACString &aKey, nsACString &aResult)
|
|
|
|
{
|
2012-07-30 14:20:58 +00:00
|
|
|
GError* error = nullptr;
|
2006-07-27 14:42:23 +00:00
|
|
|
gchar *result = gconf_client_get_string(mClient,
|
|
|
|
PromiseFlatCString(aKey).get(),
|
|
|
|
&error);
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
g_error_free(error);
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We do a string copy here so that the caller doesn't need to worry about
|
|
|
|
// freeing the string with g_free().
|
|
|
|
|
|
|
|
aResult.Assign(result);
|
|
|
|
g_free(result);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 15:56:38 +00:00
|
|
|
nsGConfService::GetInt(const nsACString &aKey, int32_t* aResult)
|
2006-07-27 14:42:23 +00:00
|
|
|
{
|
2012-07-30 14:20:58 +00:00
|
|
|
GError* error = nullptr;
|
2006-07-27 14:42:23 +00:00
|
|
|
*aResult = gconf_client_get_int(mClient, PromiseFlatCString(aKey).get(),
|
|
|
|
&error);
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
g_error_free(error);
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsGConfService::GetFloat(const nsACString &aKey, float* aResult)
|
|
|
|
{
|
2012-07-30 14:20:58 +00:00
|
|
|
GError* error = nullptr;
|
2006-07-27 14:42:23 +00:00
|
|
|
*aResult = gconf_client_get_float(mClient, PromiseFlatCString(aKey).get(),
|
|
|
|
&error);
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
g_error_free(error);
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2008-01-29 18:38:15 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsGConfService::GetStringList(const nsACString &aKey, nsIArray** aResult)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIMutableArray> items(do_CreateInstance(NS_ARRAY_CONTRACTID));
|
|
|
|
if (!items)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
2012-07-30 14:20:58 +00:00
|
|
|
GError* error = nullptr;
|
2008-01-29 18:38:15 +00:00
|
|
|
GSList* list = gconf_client_get_list(mClient, PromiseFlatCString(aKey).get(),
|
|
|
|
GCONF_VALUE_STRING, &error);
|
|
|
|
if (error) {
|
|
|
|
g_error_free(error);
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (GSList* l = list; l; l = l->next) {
|
2008-04-22 19:46:07 +00:00
|
|
|
nsCOMPtr<nsISupportsString> obj(do_CreateInstance(NS_SUPPORTS_STRING_CONTRACTID));
|
2008-01-29 18:38:15 +00:00
|
|
|
if (!obj) {
|
|
|
|
g_slist_free(list);
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
obj->SetData(NS_ConvertUTF8toUTF16((const char*)l->data));
|
2011-10-17 14:59:28 +00:00
|
|
|
items->AppendElement(obj, false);
|
2008-01-29 18:38:15 +00:00
|
|
|
g_free(l->data);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_slist_free(list);
|
|
|
|
NS_ADDREF(*aResult = items);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2006-07-27 14:42:23 +00:00
|
|
|
NS_IMETHODIMP
|
2011-09-29 06:19:26 +00:00
|
|
|
nsGConfService::SetBool(const nsACString &aKey, bool aValue)
|
2006-07-27 14:42:23 +00:00
|
|
|
{
|
2011-09-29 06:19:26 +00:00
|
|
|
bool res = gconf_client_set_bool(mClient, PromiseFlatCString(aKey).get(),
|
2012-07-30 14:20:58 +00:00
|
|
|
aValue, nullptr);
|
2006-07-27 14:42:23 +00:00
|
|
|
|
|
|
|
return res ? NS_OK : NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsGConfService::SetString(const nsACString &aKey, const nsACString &aValue)
|
|
|
|
{
|
2011-09-29 06:19:26 +00:00
|
|
|
bool res = gconf_client_set_string(mClient, PromiseFlatCString(aKey).get(),
|
2006-07-27 14:42:23 +00:00
|
|
|
PromiseFlatCString(aValue).get(),
|
2012-07-30 14:20:58 +00:00
|
|
|
nullptr);
|
2006-07-27 14:42:23 +00:00
|
|
|
|
|
|
|
return res ? NS_OK : NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 15:56:38 +00:00
|
|
|
nsGConfService::SetInt(const nsACString &aKey, int32_t aValue)
|
2006-07-27 14:42:23 +00:00
|
|
|
{
|
2011-09-29 06:19:26 +00:00
|
|
|
bool res = gconf_client_set_int(mClient, PromiseFlatCString(aKey).get(),
|
2012-07-30 14:20:58 +00:00
|
|
|
aValue, nullptr);
|
2006-07-27 14:42:23 +00:00
|
|
|
|
|
|
|
return res ? NS_OK : NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsGConfService::SetFloat(const nsACString &aKey, float aValue)
|
|
|
|
{
|
2011-09-29 06:19:26 +00:00
|
|
|
bool res = gconf_client_set_float(mClient, PromiseFlatCString(aKey).get(),
|
2012-07-30 14:20:58 +00:00
|
|
|
aValue, nullptr);
|
2006-07-27 14:42:23 +00:00
|
|
|
|
|
|
|
return res ? NS_OK : NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-29 06:19:26 +00:00
|
|
|
nsGConfService::GetAppForProtocol(const nsACString &aScheme, bool *aEnabled,
|
2006-07-27 14:42:23 +00:00
|
|
|
nsACString &aHandler)
|
|
|
|
{
|
2012-09-02 02:35:17 +00:00
|
|
|
nsAutoCString key("/desktop/gnome/url-handlers/");
|
2006-07-27 14:42:26 +00:00
|
|
|
key.Append(aScheme);
|
|
|
|
key.Append("/command");
|
2006-07-27 14:42:23 +00:00
|
|
|
|
2012-07-30 14:20:58 +00:00
|
|
|
GError *err = nullptr;
|
2006-07-27 14:42:23 +00:00
|
|
|
gchar *command = gconf_client_get_string(mClient, key.get(), &err);
|
|
|
|
if (!err && command) {
|
|
|
|
key.Replace(key.Length() - 7, 7, NS_LITERAL_CSTRING("enabled"));
|
|
|
|
*aEnabled = gconf_client_get_bool(mClient, key.get(), &err);
|
|
|
|
} else {
|
2011-10-17 14:59:28 +00:00
|
|
|
*aEnabled = false;
|
2006-07-27 14:42:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
aHandler.Assign(command);
|
2011-05-11 13:12:26 +00:00
|
|
|
g_free(command);
|
2006-07-27 14:42:23 +00:00
|
|
|
|
|
|
|
if (err) {
|
|
|
|
g_error_free(err);
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsGConfService::HandlerRequiresTerminal(const nsACString &aScheme,
|
2011-09-29 06:19:26 +00:00
|
|
|
bool *aResult)
|
2006-07-27 14:42:23 +00:00
|
|
|
{
|
2012-09-02 02:35:17 +00:00
|
|
|
nsAutoCString key("/desktop/gnome/url-handlers/");
|
2006-07-27 14:42:26 +00:00
|
|
|
key.Append(aScheme);
|
|
|
|
key.Append("/requires_terminal");
|
2006-07-27 14:42:23 +00:00
|
|
|
|
2012-07-30 14:20:58 +00:00
|
|
|
GError *err = nullptr;
|
2006-07-27 14:42:23 +00:00
|
|
|
*aResult = gconf_client_get_bool(mClient, key.get(), &err);
|
|
|
|
if (err) {
|
|
|
|
g_error_free(err);
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsGConfService::SetAppForProtocol(const nsACString &aScheme,
|
|
|
|
const nsACString &aCommand)
|
|
|
|
{
|
2012-09-02 02:35:17 +00:00
|
|
|
nsAutoCString key("/desktop/gnome/url-handlers/");
|
2006-07-27 14:42:26 +00:00
|
|
|
key.Append(aScheme);
|
|
|
|
key.Append("/command");
|
2006-07-27 14:42:23 +00:00
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool res = gconf_client_set_string(mClient, key.get(),
|
2006-07-27 14:42:23 +00:00
|
|
|
PromiseFlatCString(aCommand).get(),
|
2012-07-30 14:20:58 +00:00
|
|
|
nullptr);
|
2006-07-27 14:42:23 +00:00
|
|
|
if (res) {
|
|
|
|
key.Replace(key.Length() - 7, 7, NS_LITERAL_CSTRING("enabled"));
|
2012-07-30 14:20:58 +00:00
|
|
|
res = gconf_client_set_bool(mClient, key.get(), true, nullptr);
|
2006-07-27 14:42:23 +00:00
|
|
|
if (res) {
|
|
|
|
key.Replace(key.Length() - 7, 7, NS_LITERAL_CSTRING("needs_terminal"));
|
2012-07-30 14:20:58 +00:00
|
|
|
res = gconf_client_set_bool(mClient, key.get(), false, nullptr);
|
2006-07-27 14:42:23 +00:00
|
|
|
if (res) {
|
|
|
|
key.Replace(key.Length() - 14, 14, NS_LITERAL_CSTRING("command-id"));
|
2012-07-30 14:20:58 +00:00
|
|
|
res = gconf_client_unset(mClient, key.get(), nullptr);
|
2006-07-27 14:42:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return res ? NS_OK : NS_ERROR_FAILURE;
|
|
|
|
}
|