2
* This file is part of libslab.
4
* Copyright (c) 2006 Novell, Inc.
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)
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
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
21
#include "slab-gnome-util.h"
22
#include "libslab-utils.h"
24
#include <gconf/gconf-client.h>
29
get_slab_gconf_bool (const gchar * key)
31
GConfClient *gconf_client;
36
gconf_client = gconf_client_get_default ();
39
value = gconf_client_get_bool (gconf_client, key, &error);
41
g_object_unref (gconf_client);
45
g_warning ("error accessing %s [%s]\n", key, error->message);
53
get_slab_gconf_int (const gchar * key)
55
GConfClient *gconf_client;
60
gconf_client = gconf_client_get_default ();
63
value = gconf_client_get_int (gconf_client, key, &error);
65
g_object_unref (gconf_client);
68
g_warning ("error accessing %s [%s]\n", key, error->message);
76
get_slab_gconf_string (const gchar * key)
78
GConfClient *gconf_client;
83
gconf_client = gconf_client_get_default ();
86
value = gconf_client_get_string (gconf_client, key, &error);
88
g_object_unref (gconf_client);
91
g_warning ("error accessing %s [%s]\n", key, error->message);
99
free_list_of_strings (GList * string_list)
101
g_assert (string_list != NULL);
102
g_list_foreach (string_list, (GFunc) g_free, NULL);
103
g_list_free (string_list);
107
free_slab_gconf_slist_of_strings (GSList * string_list)
109
g_assert (string_list != NULL);
110
g_slist_foreach (string_list, (GFunc) g_free, NULL);
111
g_slist_free (string_list);
115
get_slab_gconf_slist (const gchar * key)
117
GConfClient *gconf_client;
122
gconf_client = gconf_client_get_default ();
125
value = gconf_client_get_list (gconf_client, key, GCONF_VALUE_STRING, &error);
127
g_object_unref (gconf_client);
130
g_warning ("error accessing %s [%s]\n", key, error->message);
132
g_error_free (error);
139
load_desktop_item_from_gconf_key (const gchar * key)
141
GnomeDesktopItem *item;
142
gchar *id = get_slab_gconf_string (key);
147
item = load_desktop_item_from_unknown (id);
153
load_desktop_item_from_unknown (const gchar *id)
155
GnomeDesktopItem *item;
158
GError *error = NULL;
161
item = gnome_desktop_item_new_from_uri (id, 0, &error);
166
g_error_free (error);
170
item = gnome_desktop_item_new_from_file (id, 0, &error);
175
g_error_free (error);
179
item = gnome_desktop_item_new_from_basename (id, 0, &error);
184
g_error_free (error);
188
basename = g_strrstr (id, "/");
193
item = gnome_desktop_item_new_from_basename (basename, 0, &error);
198
g_error_free (error);
207
get_package_name_from_desktop_item (GnomeDesktopItem * desktop_item)
218
argv[4] = g_filename_from_uri (gnome_desktop_item_get_location (desktop_item), NULL, NULL);
223
if (!g_spawn_sync (NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL, &package_name, NULL,
226
g_warning ("error: [%s]\n", error->message);
227
g_error_free (error);
240
open_desktop_item_exec (GnomeDesktopItem * desktop_item)
242
GError *error = NULL;
247
gnome_desktop_item_launch (desktop_item, NULL, GNOME_DESKTOP_ITEM_LAUNCH_ONLY_ONE, &error);
251
g_warning ("error launching %s [%s]\n",
252
gnome_desktop_item_get_location (desktop_item), error->message);
254
g_error_free (error);
262
open_desktop_item_help (GnomeDesktopItem * desktop_item)
264
const gchar *doc_path;
272
doc_path = gnome_desktop_item_get_string (desktop_item, "DocPath");
276
help_uri = g_strdup_printf ("ghelp:%s", doc_path);
279
if (!gtk_show_uri (libslab_get_current_screen (), help_uri, gtk_get_current_event_time (), &error))
281
g_warning ("error opening %s [%s]\n", help_uri, error->message);
284
g_error_free (error);
297
desktop_item_is_in_main_menu (GnomeDesktopItem * desktop_item)
299
return desktop_uri_is_in_main_menu (gnome_desktop_item_get_location (desktop_item));
303
desktop_uri_is_in_main_menu (const gchar * uri)
310
gboolean found = FALSE;
312
app_list = get_slab_gconf_slist (SLAB_USER_SPECIFIED_APPS_KEY);
317
uri_len = strlen (uri);
319
for (node = app_list; node; node = node->next)
321
offset = uri_len - strlen ((gchar *) node->data);
326
if (!strcmp (&uri[offset], (gchar *) node->data))
333
free_slab_gconf_slist_of_strings (app_list);
338
desktop_item_location_compare (gconstpointer a_obj, gconstpointer b_obj)
345
a = (const gchar *) a_obj;
346
b = (const gchar *) b_obj;
348
offset = strlen (a) - strlen (b);
351
return strcmp (&a[offset], b);
353
return strcmp (a, &b[-offset]);
355
return strcmp (a, b);
359
slab_load_image (GtkImage * image, GtkIconSize size, const gchar * image_id)
370
id = g_strdup (image_id);
372
gtk_icon_size_lookup (size, &width, &height);
374
if (g_path_is_absolute (id))
375
pixbuf = gdk_pixbuf_new_from_file_at_size (id, width, height, NULL);
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")
384
id[strlen (id) - 4] = '\0';
386
pixbuf = gtk_icon_theme_load_icon (gtk_icon_theme_get_default (), id, width, 0,
392
gtk_image_set_from_pixbuf (image, pixbuf);
394
g_object_unref (pixbuf);
401
{ /* This will make it show the "broken image" icon */
402
gtk_image_set_from_file (image, id);
411
string_replace_once (const gchar * str_template, const gchar * key, const gchar * value)
416
pivot = strstr (str_template, key) - str_template;
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)]);
422
return g_string_free (str_built, FALSE);
426
spawn_process (const gchar *command)
429
GError *error = NULL;
431
if (!command || strlen (command) < 1)
434
argv = g_strsplit (command, " ", -1);
436
g_spawn_async (NULL, argv, NULL, G_SPAWN_SEARCH_PATH, NULL, NULL, NULL, &error);
440
g_warning ("error spawning [%s]: [%s]\n", command, error->message);
442
g_error_free (error);
449
copy_file (const gchar * src_uri, const gchar * dst_uri)
453
GError *error = NULL;
456
src = g_file_new_for_uri (src_uri);
457
dst = g_file_new_for_uri (dst_uri);
459
res = g_file_copy (src, dst,
461
NULL, NULL, NULL, &error);
465
g_warning ("error copying [%s] to [%s]: %s.", src_uri, dst_uri, error->message);
466
g_error_free (error);
469
g_object_unref (src);
470
g_object_unref (dst);