~kroq-gar78/ubuntu/precise/gnome-control-center/fix-885947

« back to all changes in this revision

Viewing changes to libslab/slab-gnome-util.c

  • Committer: Bazaar Package Importer
  • Author(s): Rodrigo Moya
  • Date: 2011-05-17 10:47:27 UTC
  • mfrom: (0.1.11 experimental) (1.1.45 upstream)
  • Revision ID: james.westby@ubuntu.com-20110517104727-lqel6m8vhfw5jby1
Tags: 1:3.0.1.1-1ubuntu1
* Rebase on Debian, remaining Ubuntu changes:
* debian/control:
  - Build-Depend on hardening-wrapper, dpkg-dev and dh-autoreconf
  - Add dependency on ubuntu-system-service
  - Remove dependency on gnome-icon-theme-symbolic
  - Move dependency on apg, gnome-icon-theme-symbolic and accountsservice to
    be a Recommends: until we get them in main
* debian/rules:
  - Use autoreconf
  - Add binary-post-install rule for gnome-control-center-data
  - Run dh-autoreconf
* debian/gnome-control-center.dirs:
* debian/gnome-control-center.links:
  - Add a link to the control center shell for indicators
* debian/patches/00_disable-nm.patch:
  - Temporary patch to disable building with NetworkManager until we get
    the new one in the archive
* debian/patches/01_git_remove_gettext_calls.patch:
  - Remove calls to AM_GNU_GETTEXT, IT_PROG_INTLTOOL should be enough
* debian/patches/01_git_kill_warning.patch:
  - Kill warning
* debian/patches/50_ubuntu_systemwide_prefs.patch:
  - Ubuntu specific proxy preferences
* debian/patches/51_ubuntu_system_keyboard.patch:
  - Implement the global keyboard spec at https://wiki.ubuntu.com/DefaultKeyboardSettings

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * This file is part of libslab.
3
 
 *
4
 
 * Copyright (c) 2006 Novell, Inc.
5
 
 *
6
 
 * Libslab is free software; you can redistribute it and/or modify it under the
7
 
 * terms of the GNU Lesser General Public License as published by the Free
8
 
 * Software Foundation; either version 2 of the License, or (at your option)
9
 
 * any later version.
10
 
 *
11
 
 * Libslab is distributed in the hope that it will be useful, but WITHOUT ANY
12
 
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13
 
 * FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for
14
 
 * more details.
15
 
 *
16
 
 * You should have received a copy of the GNU Lesser General Public License
17
 
 * along with libslab; if not, write to the Free Software Foundation, Inc., 51
18
 
 * Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19
 
 */
20
 
 
21
 
#include "slab-gnome-util.h"
22
 
#include "libslab-utils.h"
23
 
 
24
 
#include <gconf/gconf-client.h>
25
 
#include <gio/gio.h>
26
 
#include <string.h>
27
 
 
28
 
gboolean
29
 
get_slab_gconf_bool (const gchar * key)
30
 
{
31
 
        GConfClient *gconf_client;
32
 
        GError *error;
33
 
 
34
 
        gboolean value;
35
 
 
36
 
        gconf_client = gconf_client_get_default ();
37
 
        error = NULL;
38
 
 
39
 
        value = gconf_client_get_bool (gconf_client, key, &error);
40
 
 
41
 
        g_object_unref (gconf_client);
42
 
 
43
 
        if (error)
44
 
        {
45
 
                g_warning ("error accessing %s [%s]\n", key, error->message);
46
 
                g_error_free (error);
47
 
        }
48
 
 
49
 
        return value;
50
 
}
51
 
 
52
 
gint
53
 
get_slab_gconf_int (const gchar * key)
54
 
{
55
 
        GConfClient *gconf_client;
56
 
        GError *error;
57
 
 
58
 
        gint value;
59
 
 
60
 
        gconf_client = gconf_client_get_default ();
61
 
        error = NULL;
62
 
 
63
 
        value = gconf_client_get_int (gconf_client, key, &error);
64
 
 
65
 
        g_object_unref (gconf_client);
66
 
        if (error)
67
 
        {
68
 
                g_warning ("error accessing %s [%s]\n", key, error->message);
69
 
                g_error_free (error);
70
 
        }
71
 
 
72
 
        return value;
73
 
}
74
 
 
75
 
gchar *
76
 
get_slab_gconf_string (const gchar * key)
77
 
{
78
 
        GConfClient *gconf_client;
79
 
        GError *error;
80
 
 
81
 
        gchar *value;
82
 
 
83
 
        gconf_client = gconf_client_get_default ();
84
 
        error = NULL;
85
 
 
86
 
        value = gconf_client_get_string (gconf_client, key, &error);
87
 
 
88
 
        g_object_unref (gconf_client);
89
 
        if (error)
90
 
        {
91
 
                g_warning ("error accessing %s [%s]\n", key, error->message);
92
 
                g_error_free (error);
93
 
        }
94
 
 
95
 
        return value;
96
 
}
97
 
 
98
 
void
99
 
free_list_of_strings (GList * string_list)
100
 
{
101
 
        g_assert (string_list != NULL);
102
 
        g_list_foreach (string_list, (GFunc) g_free, NULL);
103
 
        g_list_free (string_list);
104
 
}
105
 
 
106
 
void
107
 
free_slab_gconf_slist_of_strings (GSList * string_list)
108
 
{
109
 
        g_assert (string_list != NULL);
110
 
        g_slist_foreach (string_list, (GFunc) g_free, NULL);
111
 
        g_slist_free (string_list);
112
 
}
113
 
 
114
 
GSList *
115
 
get_slab_gconf_slist (const gchar * key)
116
 
{
117
 
        GConfClient *gconf_client;
118
 
        GError *error;
119
 
 
120
 
        GSList *value;
121
 
 
122
 
        gconf_client = gconf_client_get_default ();
123
 
        error = NULL;
124
 
 
125
 
        value = gconf_client_get_list (gconf_client, key, GCONF_VALUE_STRING, &error);
126
 
 
127
 
        g_object_unref (gconf_client);
128
 
        if (error)
129
 
        {
130
 
                g_warning ("error accessing %s [%s]\n", key, error->message);
131
 
 
132
 
                g_error_free (error);
133
 
        }
134
 
 
135
 
        return value;
136
 
}
137
 
 
138
 
GnomeDesktopItem *
139
 
load_desktop_item_from_gconf_key (const gchar * key)
140
 
{
141
 
        GnomeDesktopItem *item;
142
 
        gchar *id = get_slab_gconf_string (key);
143
 
 
144
 
        if (!id)
145
 
                return NULL;
146
 
 
147
 
        item = load_desktop_item_from_unknown (id);
148
 
        g_free (id);
149
 
        return item;
150
 
}
151
 
 
152
 
GnomeDesktopItem *
153
 
load_desktop_item_from_unknown (const gchar *id)
154
 
{
155
 
        GnomeDesktopItem *item;
156
 
        gchar            *basename;
157
 
 
158
 
        GError *error = NULL;
159
 
 
160
 
 
161
 
        item = gnome_desktop_item_new_from_uri (id, 0, &error);
162
 
 
163
 
        if (! error)
164
 
                return item;
165
 
        else {
166
 
                g_error_free (error);
167
 
                error = NULL;
168
 
        }
169
 
 
170
 
        item = gnome_desktop_item_new_from_file (id, 0, &error);
171
 
 
172
 
        if (! error)
173
 
                return item;
174
 
        else {
175
 
                g_error_free (error);
176
 
                error = NULL;
177
 
        }
178
 
 
179
 
        item = gnome_desktop_item_new_from_basename (id, 0, &error);
180
 
 
181
 
        if (! error)
182
 
                return item;
183
 
        else {
184
 
                g_error_free (error);
185
 
                error = NULL;
186
 
        }
187
 
 
188
 
        basename = g_strrstr (id, "/");
189
 
 
190
 
        if (basename) {
191
 
                basename++;
192
 
 
193
 
                item = gnome_desktop_item_new_from_basename (basename, 0, &error);
194
 
 
195
 
                if (! error)
196
 
                        return item;
197
 
                else {
198
 
                        g_error_free (error);
199
 
                        error = NULL;
200
 
                }
201
 
        }
202
 
 
203
 
        return NULL;
204
 
}
205
 
 
206
 
gchar *
207
 
get_package_name_from_desktop_item (GnomeDesktopItem * desktop_item)
208
 
{
209
 
        gchar *argv[6];
210
 
        gchar *package_name;
211
 
        gint retval;
212
 
        GError *error;
213
 
 
214
 
        argv[0] = "rpm";
215
 
        argv[1] = "-qf";
216
 
        argv[2] = "--qf";
217
 
        argv[3] = "%{NAME}";
218
 
        argv[4] = g_filename_from_uri (gnome_desktop_item_get_location (desktop_item), NULL, NULL);
219
 
        argv[5] = NULL;
220
 
 
221
 
        error = NULL;
222
 
 
223
 
        if (!g_spawn_sync (NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL, &package_name, NULL,
224
 
                        &retval, &error))
225
 
        {
226
 
                g_warning ("error: [%s]\n", error->message);
227
 
                g_error_free (error);
228
 
                retval = -1;
229
 
        }
230
 
 
231
 
        g_free (argv[4]);
232
 
 
233
 
        if (!retval)
234
 
                return package_name;
235
 
        else
236
 
                return NULL;
237
 
}
238
 
 
239
 
gboolean
240
 
open_desktop_item_exec (GnomeDesktopItem * desktop_item)
241
 
{
242
 
        GError *error = NULL;
243
 
 
244
 
        if (!desktop_item)
245
 
                return FALSE;
246
 
 
247
 
        gnome_desktop_item_launch (desktop_item, NULL, GNOME_DESKTOP_ITEM_LAUNCH_ONLY_ONE, &error);
248
 
 
249
 
        if (error)
250
 
        {
251
 
                g_warning ("error launching %s [%s]\n",
252
 
                        gnome_desktop_item_get_location (desktop_item), error->message);
253
 
 
254
 
                g_error_free (error);
255
 
                return FALSE;
256
 
        }
257
 
 
258
 
        return TRUE;
259
 
}
260
 
 
261
 
gboolean
262
 
open_desktop_item_help (GnomeDesktopItem * desktop_item)
263
 
{
264
 
        const gchar *doc_path;
265
 
        gchar *help_uri;
266
 
 
267
 
        GError *error;
268
 
 
269
 
        if (!desktop_item)
270
 
                return FALSE;
271
 
 
272
 
        doc_path = gnome_desktop_item_get_string (desktop_item, "DocPath");
273
 
 
274
 
        if (doc_path)
275
 
        {
276
 
                help_uri = g_strdup_printf ("ghelp:%s", doc_path);
277
 
 
278
 
                error = NULL;
279
 
                if (!gtk_show_uri (libslab_get_current_screen (), help_uri, gtk_get_current_event_time (), &error))
280
 
                {
281
 
                        g_warning ("error opening %s [%s]\n", help_uri, error->message);
282
 
 
283
 
                        g_free (help_uri);
284
 
                        g_error_free (error);
285
 
                        return FALSE;
286
 
                }
287
 
 
288
 
                g_free (help_uri);
289
 
        }
290
 
        else
291
 
                return FALSE;
292
 
 
293
 
        return TRUE;
294
 
}
295
 
 
296
 
gboolean
297
 
desktop_item_is_in_main_menu (GnomeDesktopItem * desktop_item)
298
 
{
299
 
        return desktop_uri_is_in_main_menu (gnome_desktop_item_get_location (desktop_item));
300
 
}
301
 
 
302
 
gboolean
303
 
desktop_uri_is_in_main_menu (const gchar * uri)
304
 
{
305
 
        GSList *app_list;
306
 
 
307
 
        GSList *node;
308
 
        gint offset;
309
 
        gint uri_len;
310
 
        gboolean found = FALSE;
311
 
 
312
 
        app_list = get_slab_gconf_slist (SLAB_USER_SPECIFIED_APPS_KEY);
313
 
 
314
 
        if (!app_list)
315
 
                return FALSE;
316
 
 
317
 
        uri_len = strlen (uri);
318
 
 
319
 
        for (node = app_list; node; node = node->next)
320
 
        {
321
 
                offset = uri_len - strlen ((gchar *) node->data);
322
 
 
323
 
                if (offset < 0)
324
 
                        offset = 0;
325
 
 
326
 
                if (!strcmp (&uri[offset], (gchar *) node->data))
327
 
                {
328
 
                        found = TRUE;
329
 
                        break;
330
 
                }
331
 
        }
332
 
 
333
 
        free_slab_gconf_slist_of_strings (app_list);
334
 
        return found;
335
 
}
336
 
 
337
 
gint
338
 
desktop_item_location_compare (gconstpointer a_obj, gconstpointer b_obj)
339
 
{
340
 
        const gchar *a;
341
 
        const gchar *b;
342
 
 
343
 
        gint offset;
344
 
 
345
 
        a = (const gchar *) a_obj;
346
 
        b = (const gchar *) b_obj;
347
 
 
348
 
        offset = strlen (a) - strlen (b);
349
 
 
350
 
        if (offset > 0)
351
 
                return strcmp (&a[offset], b);
352
 
        else if (offset < 0)
353
 
                return strcmp (a, &b[-offset]);
354
 
        else
355
 
                return strcmp (a, b);
356
 
}
357
 
 
358
 
gboolean
359
 
slab_load_image (GtkImage * image, GtkIconSize size, const gchar * image_id)
360
 
{
361
 
        GdkPixbuf *pixbuf;
362
 
        gint width;
363
 
        gint height;
364
 
 
365
 
        gchar *id;
366
 
 
367
 
        if (!image_id)
368
 
                return FALSE;
369
 
 
370
 
        id = g_strdup (image_id);
371
 
 
372
 
        gtk_icon_size_lookup (size, &width, &height);
373
 
 
374
 
        if (g_path_is_absolute (id))
375
 
                pixbuf = gdk_pixbuf_new_from_file_at_size (id, width, height, NULL);
376
 
        else
377
 
        {
378
 
                if (    /* file extensions are not copesetic with loading by "name" */
379
 
                        g_str_has_suffix (id, ".png") ||
380
 
                        g_str_has_suffix (id, ".svg") ||
381
 
                        g_str_has_suffix (id, ".xpm")
382
 
                   )
383
 
 
384
 
                        id[strlen (id) - 4] = '\0';
385
 
 
386
 
                pixbuf = gtk_icon_theme_load_icon (gtk_icon_theme_get_default (), id, width, 0,
387
 
                        NULL);
388
 
        }
389
 
 
390
 
        if (pixbuf)
391
 
        {
392
 
                gtk_image_set_from_pixbuf (image, pixbuf);
393
 
 
394
 
                g_object_unref (pixbuf);
395
 
 
396
 
                g_free (id);
397
 
 
398
 
                return TRUE;
399
 
        }
400
 
        else
401
 
        {                       /* This will make it show the "broken image" icon */
402
 
                gtk_image_set_from_file (image, id);
403
 
 
404
 
                g_free (id);
405
 
 
406
 
                return FALSE;
407
 
        }
408
 
}
409
 
 
410
 
gchar *
411
 
string_replace_once (const gchar * str_template, const gchar * key, const gchar * value)
412
 
{
413
 
        GString *str_built;
414
 
        gint pivot;
415
 
 
416
 
        pivot = strstr (str_template, key) - str_template;
417
 
 
418
 
        str_built = g_string_new_len (str_template, pivot);
419
 
        g_string_append (str_built, value);
420
 
        g_string_append (str_built, &str_template[pivot + strlen (key)]);
421
 
 
422
 
        return g_string_free (str_built, FALSE);
423
 
}
424
 
 
425
 
void
426
 
spawn_process (const gchar *command)
427
 
{
428
 
        gchar **argv;
429
 
        GError *error = NULL;
430
 
 
431
 
        if (!command || strlen (command) < 1)
432
 
                return;
433
 
 
434
 
        argv = g_strsplit (command, " ", -1);
435
 
 
436
 
        g_spawn_async (NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL, NULL, &error);
437
 
 
438
 
        if (error)
439
 
        {
440
 
                g_warning ("error spawning [%s]: [%s]\n", command, error->message);
441
 
 
442
 
                g_error_free (error);
443
 
        }
444
 
 
445
 
        g_strfreev (argv);
446
 
}
447
 
 
448
 
void
449
 
copy_file (const gchar * src_uri, const gchar * dst_uri)
450
 
{
451
 
        GFile *src;
452
 
        GFile *dst;
453
 
        GError *error = NULL;
454
 
        gboolean res;
455
 
 
456
 
        src = g_file_new_for_uri (src_uri);
457
 
        dst = g_file_new_for_uri (dst_uri);
458
 
 
459
 
        res = g_file_copy (src, dst,
460
 
                           G_FILE_COPY_NONE,
461
 
                           NULL, NULL, NULL, &error);
462
 
 
463
 
        if (!res)
464
 
        {
465
 
                g_warning ("error copying [%s] to [%s]: %s.", src_uri, dst_uri, error->message);
466
 
                g_error_free (error);
467
 
        }
468
 
 
469
 
        g_object_unref (src);
470
 
        g_object_unref (dst);
471
 
}