summaryrefslogtreecommitdiffstats
path: root/toolkit/system/gnome/nsGConfService.cpp
blob: ede71e5883a94d9d36e78347f5c8ec401581c77d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 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/. */

#include "mozilla/ArrayUtils.h"
#include "nsGConfService.h"
#include "nsString.h"
#include "nsCOMPtr.h"
#include "nsComponentManagerUtils.h"
#include "nsISupportsPrimitives.h"
#include "nsIMutableArray.h"
#include "prlink.h"

#include <gconf/gconf-client.h>

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

static PRLibrary *gconfLib = nullptr;

typedef void (*nsGConfFunc)();
struct nsGConfDynamicFunction {
  const char *functionName;
  nsGConfFunc *function;
};

nsGConfService::~nsGConfService()
{
  if (mClient)
    g_object_unref(mClient);

  // 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
}

nsresult
nsGConfService::Init()
{
#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;
  }

  for (uint32_t i = 0; i < ArrayLength(kGConfSymbols); i++) {
    *kGConfSymbols[i].function =
      PR_FindFunctionSymbol(gconfLib, kGConfSymbols[i].functionName);
    if (!*kGConfSymbols[i].function) {
      return NS_ERROR_FAILURE;
    }
  }

  mClient = gconf_client_get_default();
  return mClient ? NS_OK : NS_ERROR_FAILURE;
}

NS_IMPL_ISUPPORTS(nsGConfService, nsIGConfService)

NS_IMETHODIMP
nsGConfService::GetBool(const nsACString &aKey, bool *aResult)
{
  GError* error = nullptr;
  *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)
{
  GError* error = nullptr;
  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
nsGConfService::GetInt(const nsACString &aKey, int32_t* aResult)
{
  GError* error = nullptr;
  *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)
{
  GError* error = nullptr;
  *aResult = gconf_client_get_float(mClient, PromiseFlatCString(aKey).get(),
                                    &error);

  if (error) {
    g_error_free(error);
    return NS_ERROR_FAILURE;
  }

  return NS_OK;
}

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;
    
  GError* error = nullptr;
  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) {
    nsCOMPtr<nsISupportsString> obj(do_CreateInstance(NS_SUPPORTS_STRING_CONTRACTID));
    if (!obj) {
      g_slist_free(list);
      return NS_ERROR_OUT_OF_MEMORY;
    }
    obj->SetData(NS_ConvertUTF8toUTF16((const char*)l->data));
    items->AppendElement(obj, false);
    g_free(l->data);
  }
  
  g_slist_free(list);
  items.forget(aResult);
  return NS_OK;
}

NS_IMETHODIMP
nsGConfService::SetBool(const nsACString &aKey, bool aValue)
{
  bool res = gconf_client_set_bool(mClient, PromiseFlatCString(aKey).get(),
                                     aValue, nullptr);

  return res ? NS_OK : NS_ERROR_FAILURE;
}

NS_IMETHODIMP
nsGConfService::SetString(const nsACString &aKey, const nsACString &aValue)
{
  bool res = gconf_client_set_string(mClient, PromiseFlatCString(aKey).get(),
                                       PromiseFlatCString(aValue).get(),
                                       nullptr);

  return res ? NS_OK : NS_ERROR_FAILURE;
}

NS_IMETHODIMP
nsGConfService::SetInt(const nsACString &aKey, int32_t aValue)
{
  bool res = gconf_client_set_int(mClient, PromiseFlatCString(aKey).get(),
                                    aValue, nullptr);

  return res ? NS_OK : NS_ERROR_FAILURE;
}

NS_IMETHODIMP
nsGConfService::SetFloat(const nsACString &aKey, float aValue)
{
  bool res = gconf_client_set_float(mClient, PromiseFlatCString(aKey).get(),
                                      aValue, nullptr);

  return res ? NS_OK : NS_ERROR_FAILURE;
}

NS_IMETHODIMP
nsGConfService::GetAppForProtocol(const nsACString &aScheme, bool *aEnabled,
                                  nsACString &aHandler)
{
  nsAutoCString key("/desktop/gnome/url-handlers/");
  key.Append(aScheme);
  key.AppendLiteral("/command");

  GError *err = nullptr;
  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 {
    *aEnabled = false;
  }

  aHandler.Assign(command);
  g_free(command);

  if (err) {
    g_error_free(err);
    return NS_ERROR_FAILURE;
  }

  return NS_OK;
}

NS_IMETHODIMP
nsGConfService::HandlerRequiresTerminal(const nsACString &aScheme,
                                        bool *aResult)
{
  nsAutoCString key("/desktop/gnome/url-handlers/");
  key.Append(aScheme);
  key.AppendLiteral("/requires_terminal");

  GError *err = nullptr;
  *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)
{
  nsAutoCString key("/desktop/gnome/url-handlers/");
  key.Append(aScheme);
  key.AppendLiteral("/command");

  bool res = gconf_client_set_string(mClient, key.get(),
                                       PromiseFlatCString(aCommand).get(),
                                       nullptr);
  if (res) {
    key.Replace(key.Length() - 7, 7, NS_LITERAL_CSTRING("enabled"));
    res = gconf_client_set_bool(mClient, key.get(), true, nullptr);
    if (res) {
      key.Replace(key.Length() - 7, 7, NS_LITERAL_CSTRING("needs_terminal"));
      res = gconf_client_set_bool(mClient, key.get(), false, nullptr);
      if (res) {
        key.Replace(key.Length() - 14, 14, NS_LITERAL_CSTRING("command-id"));
        res = gconf_client_unset(mClient, key.get(), nullptr);
      }
    }
  }

  return res ? NS_OK : NS_ERROR_FAILURE;
}