~ubuntu-branches/debian/sid/gnome-main-menu/sid

« back to all changes in this revision

Viewing changes to libslab/gnome-utils.c

  • Committer: Bazaar Package Importer
  • Author(s): Emilio Pozuelo Monfort
  • Date: 2009-11-08 21:22:49 UTC
  • mto: (5.1.3 squeeze)
  • mto: This revision was merged to the branch mainline in revision 8.
  • Revision ID: james.westby@ubuntu.com-20091108212249-9cql8cptajeua2eh
Tags: upstream-0.9.13
ImportĀ upstreamĀ versionĀ 0.9.13

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#include "gnome-utils.h"
2
 
 
3
 
#include <string.h>
4
 
 
5
 
static void section_header_style_set (GtkWidget *, GtkStyle *, gpointer);
6
 
 
7
 
gboolean
8
 
load_image_by_id (GtkImage * image, GtkIconSize size, const gchar * image_id)
9
 
{
10
 
        GdkPixbuf *pixbuf;
11
 
        gint width;
12
 
        gint height;
13
 
 
14
 
        GtkIconTheme *icon_theme;
15
 
 
16
 
        gchar *id;
17
 
 
18
 
        gboolean icon_exists;
19
 
 
20
 
        if (!image_id)
21
 
                return FALSE;
22
 
 
23
 
        id = g_strdup (image_id);
24
 
 
25
 
        gtk_icon_size_lookup (size, &width, &height);
26
 
 
27
 
        if (g_path_is_absolute (id))
28
 
        {
29
 
                pixbuf = gdk_pixbuf_new_from_file_at_size (id, width, height, NULL);
30
 
 
31
 
                icon_exists = (pixbuf != NULL);
32
 
 
33
 
                if (icon_exists)
34
 
                {
35
 
                        gtk_image_set_from_pixbuf (image, pixbuf);
36
 
 
37
 
                        g_object_unref (pixbuf);
38
 
                }
39
 
                else
40
 
                        gtk_image_set_from_stock (image, "gtk-missing-image", size);
41
 
        }
42
 
        else
43
 
        {
44
 
                if (            /* file extensions are not copesetic with loading by "name" */
45
 
                        g_str_has_suffix (id, ".png") ||
46
 
                        g_str_has_suffix (id, ".svg") ||
47
 
                        g_str_has_suffix (id, ".xpm")
48
 
                   )
49
 
 
50
 
                        id[strlen (id) - 4] = '\0';
51
 
 
52
 
                if (gtk_widget_has_screen (GTK_WIDGET (image)))
53
 
                        icon_theme =
54
 
                                gtk_icon_theme_get_for_screen (gtk_widget_get_screen (GTK_WIDGET
55
 
                                        (image)));
56
 
                else
57
 
                        icon_theme = gtk_icon_theme_get_default ();
58
 
 
59
 
                icon_exists = gtk_icon_theme_has_icon (icon_theme, id);
60
 
 
61
 
                if (icon_exists)
62
 
                        gtk_image_set_from_icon_name (image, id, size);
63
 
                else
64
 
                        gtk_image_set_from_stock (image, "gtk-missing-image", size);
65
 
 
66
 
        }
67
 
 
68
 
        g_free (id);
69
 
 
70
 
        return icon_exists;
71
 
}
72
 
 
73
 
GnomeDesktopItem *
74
 
load_desktop_item_by_unknown_id (const gchar * id)
75
 
{
76
 
        GnomeDesktopItem *item;
77
 
        GError *error = NULL;
78
 
 
79
 
        item = gnome_desktop_item_new_from_uri (id, 0, &error);
80
 
 
81
 
        if (!error)
82
 
                return item;
83
 
        else
84
 
        {
85
 
                g_error_free (error);
86
 
                error = NULL;
87
 
        }
88
 
 
89
 
        item = gnome_desktop_item_new_from_file (id, 0, &error);
90
 
 
91
 
        if (!error)
92
 
                return item;
93
 
        else
94
 
        {
95
 
                g_error_free (error);
96
 
                error = NULL;
97
 
        }
98
 
 
99
 
        item = gnome_desktop_item_new_from_basename (id, 0, &error);
100
 
 
101
 
        if (!error)
102
 
                return item;
103
 
        else
104
 
        {
105
 
                g_error_free (error);
106
 
                error = NULL;
107
 
        }
108
 
 
109
 
        return NULL;
110
 
}
111
 
 
112
 
gpointer
113
 
get_gconf_value (const gchar * key)
114
 
{
115
 
        GConfClient *client;
116
 
        GConfValue *value;
117
 
        GError *error = NULL;
118
 
 
119
 
        gpointer retval = NULL;
120
 
 
121
 
        GList *list;
122
 
        GSList *slist;
123
 
 
124
 
        GConfValue *value_i;
125
 
        GSList *node;
126
 
 
127
 
        client = gconf_client_get_default ();
128
 
        value = gconf_client_get (client, key, &error);
129
 
 
130
 
        if (error || ! value)
131
 
        {
132
 
                handle_g_error (&error, "%s: error getting %s", G_STRFUNC, key);
133
 
 
134
 
                goto exit;
135
 
        }
136
 
 
137
 
        switch (value->type)
138
 
        {
139
 
        case GCONF_VALUE_STRING:
140
 
                retval = (gpointer) g_strdup (gconf_value_get_string (value));
141
 
                break;
142
 
 
143
 
        case GCONF_VALUE_INT:
144
 
                retval = GINT_TO_POINTER (gconf_value_get_int (value));
145
 
                break;
146
 
 
147
 
        case GCONF_VALUE_BOOL:
148
 
                retval = GINT_TO_POINTER (gconf_value_get_bool (value));
149
 
                break;
150
 
 
151
 
        case GCONF_VALUE_LIST:
152
 
                list = NULL;
153
 
                slist = gconf_value_get_list (value);
154
 
 
155
 
                for (node = slist; node; node = node->next)
156
 
                {
157
 
                        value_i = (GConfValue *) node->data;
158
 
 
159
 
                        if (value_i->type == GCONF_VALUE_STRING)
160
 
                                list = g_list_append (list,
161
 
                                        g_strdup (gconf_value_get_string (value_i)));
162
 
                        else if (value_i->type == GCONF_VALUE_INT)
163
 
                                list = g_list_append (list,
164
 
                                        GINT_TO_POINTER (gconf_value_get_int (value_i)));
165
 
                        else
166
 
                                g_assert_not_reached ();
167
 
                }
168
 
 
169
 
                retval = (gpointer) list;
170
 
 
171
 
                break;
172
 
 
173
 
        default:
174
 
                g_assert_not_reached ();
175
 
                break;
176
 
        }
177
 
 
178
 
        exit:
179
 
 
180
 
        g_object_unref (client);
181
 
        if(value)
182
 
                gconf_value_free (value);
183
 
 
184
 
        return retval;
185
 
}
186
 
 
187
 
void
188
 
set_gconf_value (const gchar * key, gconstpointer data)
189
 
{
190
 
        GConfClient *client;
191
 
        GConfValue *value;
192
 
 
193
 
        GConfValueType type;
194
 
        GConfValueType list_type;
195
 
 
196
 
        GSList *slist = NULL;
197
 
 
198
 
        GError *error = NULL;
199
 
 
200
 
        GConfValue *value_i;
201
 
        GList *node;
202
 
 
203
 
        client = gconf_client_get_default ();
204
 
        value = gconf_client_get (client, key, &error);
205
 
 
206
 
        if (error)
207
 
        {
208
 
                handle_g_error (&error, "%s: error getting %s", G_STRFUNC, key);
209
 
 
210
 
                goto exit;
211
 
        }
212
 
 
213
 
        type = value->type;
214
 
        list_type =
215
 
                (type ==
216
 
                GCONF_VALUE_LIST ? gconf_value_get_list_type (value) : GCONF_VALUE_INVALID);
217
 
 
218
 
        gconf_value_free (value);
219
 
        value = gconf_value_new (type);
220
 
 
221
 
        if (type == GCONF_VALUE_LIST)
222
 
                gconf_value_set_list_type (value, list_type);
223
 
 
224
 
        switch (type)
225
 
        {
226
 
        case GCONF_VALUE_STRING:
227
 
                gconf_value_set_string (value, g_strdup ((gchar *) data));
228
 
                break;
229
 
 
230
 
        case GCONF_VALUE_INT:
231
 
                gconf_value_set_int (value, GPOINTER_TO_INT (data));
232
 
                break;
233
 
 
234
 
        case GCONF_VALUE_BOOL:
235
 
                gconf_value_set_bool (value, GPOINTER_TO_INT (data));
236
 
                break;
237
 
 
238
 
        case GCONF_VALUE_LIST:
239
 
                for (node = (GList *) data; node; node = node->next)
240
 
                {
241
 
                        value_i = gconf_value_new (list_type);
242
 
 
243
 
                        if (list_type == GCONF_VALUE_STRING)
244
 
                                gconf_value_set_string (value_i, (const gchar *) node->data);
245
 
                        else if (list_type == GCONF_VALUE_INT)
246
 
                                gconf_value_set_int (value_i, GPOINTER_TO_INT (node->data));
247
 
                        else
248
 
                                g_assert_not_reached ();
249
 
 
250
 
                        slist = g_slist_append (slist, value_i);
251
 
                }
252
 
 
253
 
                gconf_value_set_list_nocopy (value, slist);
254
 
 
255
 
                break;
256
 
 
257
 
        default:
258
 
                g_assert_not_reached ();
259
 
                break;
260
 
        }
261
 
 
262
 
        gconf_client_set (client, key, value, &error);
263
 
 
264
 
        if (error)
265
 
                handle_g_error (&error, "%s: error setting %s", G_STRFUNC, key);
266
 
 
267
 
      exit:
268
 
 
269
 
        gconf_value_free (value);
270
 
        g_object_unref (client);
271
 
}
272
 
 
273
 
guint
274
 
connect_gconf_notify (const gchar * key, GConfClientNotifyFunc cb, gpointer user_data)
275
 
{
276
 
        GConfClient *client;
277
 
        guint conn_id;
278
 
 
279
 
        GError *error = NULL;
280
 
 
281
 
        client = gconf_client_get_default ();
282
 
        conn_id = gconf_client_notify_add (client, key, cb, user_data, NULL, &error);
283
 
 
284
 
        if (error)
285
 
                handle_g_error (&error, "%s: error adding notify for (%s)", G_STRFUNC, key);
286
 
 
287
 
        g_object_unref (client);
288
 
 
289
 
        return conn_id;
290
 
}
291
 
 
292
 
void
293
 
handle_g_error (GError ** error, const gchar * msg_format, ...)
294
 
{
295
 
        gchar *msg;
296
 
        va_list args;
297
 
 
298
 
        va_start (args, msg_format);
299
 
        msg = g_strdup_vprintf (msg_format, args);
300
 
        va_end (args);
301
 
 
302
 
        if (*error)
303
 
        {
304
 
                g_log (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING,
305
 
                        "\nGError raised: [%s]\nuser_message: [%s]\n", (*error)->message, msg);
306
 
 
307
 
                g_error_free (*error);
308
 
 
309
 
                *error = NULL;
310
 
        }
311
 
        else
312
 
                g_log (G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, "\nerror raised: [%s]\n", msg);
313
 
 
314
 
        g_free (msg);
315
 
}
316
 
 
317
 
GtkWidget *
318
 
get_main_menu_section_header (const gchar * markup)
319
 
{
320
 
        GtkWidget *label;
321
 
        gchar *text;
322
 
 
323
 
        text = g_strdup_printf ("<span size=\"large\">%s</span>", markup);
324
 
 
325
 
        label = gtk_label_new (text);
326
 
        gtk_label_set_use_markup (GTK_LABEL (label), TRUE);
327
 
        gtk_widget_set_name (label, "gnome-main-menu-section-header");
328
 
 
329
 
        g_signal_connect (G_OBJECT (label), "style-set", G_CALLBACK (section_header_style_set),
330
 
                NULL);
331
 
 
332
 
        g_free (text);
333
 
 
334
 
        return label;
335
 
}
336
 
 
337
 
static void
338
 
section_header_style_set (GtkWidget * widget, GtkStyle * prev_style, gpointer user_data)
339
 
{
340
 
        if (prev_style
341
 
                && widget->style->fg[GTK_STATE_SELECTED].green ==
342
 
                prev_style->fg[GTK_STATE_SELECTED].green)
343
 
                return;
344
 
 
345
 
        gtk_widget_modify_fg (widget, GTK_STATE_NORMAL, &widget->style->bg[GTK_STATE_SELECTED]);
346
 
}