~malept/awn/desktop-agnostic

« back to all changes in this revision

Viewing changes to libawn/awn-desktop-item-gnome.c

  • Committer: Mark Lee
  • Date: 2008-01-15 22:49:26 UTC
  • Revision ID: bzr@lazymalevolence.com-20080115224926-9y5sojlhqh9rpuxu
Split the AwnDesktopItem source code into its respective implementations, similar to how OS-specific code for Gtk+ is arranged

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
2
 *  Copyright (C) 2007, 2008 Mark Lee <avant-wn@lazymalevolence.com>
3
3
 *
4
 
 *  For libgnome-desktop portions:
5
 
 *  Copyright (C) 1999, 2000 Red Hat Inc.
6
 
 *  Copyright (C) 2001 Sid Vicious
7
 
 *  All rights reserved.
8
 
 *
9
4
 *  This library is free software; you can redistribute it and/or
10
5
 *  modify it under the terms of the GNU Lesser General Public
11
6
 *  License as published by the Free Software Foundation; either
28
23
#include "config.h"
29
24
#endif
30
25
 
31
 
#include <string.h>
32
26
#include <gdk/gdkscreen.h>
33
27
 
34
 
#ifdef LIBAWN_USE_GNOME
35
28
#include <libgnome/gnome-desktop-item.h>
36
 
#else
37
 
#include "egg/eggdesktopfile.h"
38
 
#endif
39
29
 
40
30
/**
41
31
 * SECTION: awn-desktop-item
47
37
 */
48
38
#include "awn-desktop-item.h"
49
39
 
50
 
#ifdef LIBAWN_USE_GNOME
51
40
typedef GnomeDesktopItem _AwnDesktopItem;
52
 
#else
53
 
typedef EggDesktopFile _AwnDesktopItem;
54
 
#endif
55
41
 
56
42
/* GType function */
57
43
GType awn_desktop_item_get_type (void)
58
44
{
59
 
#ifdef LIBAWN_USE_GNOME
60
45
        return gnome_desktop_item_get_type ();
61
 
#else
62
 
        return egg_desktop_file_get_type ();
63
 
#endif
64
46
}
65
47
 
66
48
/* Wrapper functions */
79
61
AwnDesktopItem *awn_desktop_item_new (gchar *filename)
80
62
{
81
63
        AwnDesktopItem *item = NULL;
82
 
#ifdef LIBAWN_USE_GNOME
83
64
        item = gnome_desktop_item_new_from_file (filename,
84
65
                                                 GNOME_DESKTOP_ITEM_LOAD_ONLY_IF_EXISTS,
85
66
                                                 NULL);
86
 
#else
87
 
        GError *err = NULL;
88
 
        item = (AwnDesktopItem*)egg_desktop_file_new (filename, &err);
89
 
        if (err) {
90
 
                g_warning ("Could not load the desktop item at '%s': %s",
91
 
                           filename, err->message);
92
 
        }
93
 
#endif
94
67
        return item;
95
68
}
96
69
 
103
76
 */
104
77
AwnDesktopItem *awn_desktop_item_copy (const AwnDesktopItem *item)
105
78
{
106
 
#ifdef LIBAWN_USE_GNOME
107
79
        return gnome_desktop_item_copy (item);
108
 
#else
109
 
        return (AwnDesktopItem*)egg_desktop_file_copy ((EggDesktopFile*)item);
110
 
#endif
111
80
}
112
81
 
113
82
/**
120
89
 */
121
90
gchar *awn_desktop_item_get_filename (AwnDesktopItem *item)
122
91
{
123
 
#ifdef LIBAWN_USE_GNOME
124
92
        return gnome_vfs_get_local_path_from_uri (
125
93
                   gnome_desktop_item_get_location (item)
126
94
               );
127
 
#else
128
 
        return (gchar*)egg_desktop_file_get_source ((EggDesktopFile*)item);
129
 
#endif
130
95
}
131
96
 
132
97
/**
139
104
 */
140
105
gchar *awn_desktop_item_get_item_type (AwnDesktopItem *item)
141
106
{
142
 
#ifdef LIBAWN_USE_GNOME
143
107
        return gnome_desktop_item_get_string (item, GNOME_DESKTOP_ITEM_TYPE);
144
 
#else
145
 
        return awn_desktop_item_get_string (item, EGG_DESKTOP_FILE_KEY_TYPE);
146
 
#endif
147
108
}
148
109
 
149
110
/**
156
117
 */
157
118
void awn_desktop_item_set_item_type (AwnDesktopItem *item, gchar *item_type)
158
119
{
159
 
#ifdef LIBAWN_USE_GNOME
160
120
        gnome_desktop_item_set_string (item, GNOME_DESKTOP_ITEM_TYPE, item_type);
161
 
#else
162
 
        awn_desktop_item_set_string (item, EGG_DESKTOP_FILE_KEY_TYPE, item_type);
163
 
#endif
164
121
}
165
122
 
166
123
/**
173
130
 */
174
131
gchar *awn_desktop_item_get_icon (AwnDesktopItem *item, GtkIconTheme *icon_theme)
175
132
{
176
 
#ifdef LIBAWN_USE_GNOME
177
133
        return gnome_desktop_item_get_icon (item, icon_theme);
178
 
#else
179
 
        /* adapted from libgnome-desktop/gnome-desktop-item.c, r4904
180
 
         * gnome_desktop_item_get_icon(), gnome_desktop_item_find_icon()
181
 
         */
182
 
        gchar *full = NULL;
183
 
        gchar *icon = (gchar*)egg_desktop_file_get_icon ((EggDesktopFile*)item);
184
 
        if (!icon || strcmp (icon, "") == 0) {
185
 
                return NULL;
186
 
        } else if (g_path_is_absolute (icon)) {
187
 
                if (g_file_test (icon, G_FILE_TEST_EXISTS)) {
188
 
                        return g_strdup (icon);
189
 
                } else {
190
 
                        return NULL;
191
 
                }
192
 
        } else {
193
 
                char *icon_no_extension;
194
 
                char *p;
195
 
 
196
 
                if (icon_theme == NULL)
197
 
                        icon_theme = gtk_icon_theme_get_default ();
198
 
 
199
 
                icon_no_extension = g_strdup (icon);
200
 
                p = strrchr (icon_no_extension, '.');
201
 
                if (p &&
202
 
                    (strcmp (p, ".png") == 0 ||
203
 
                     strcmp (p, ".xpm") == 0 ||
204
 
                     strcmp (p, ".svg") == 0)) {
205
 
                    *p = 0;
206
 
                }
207
 
 
208
 
                GtkIconInfo *info;
209
 
 
210
 
                info = gtk_icon_theme_lookup_icon (icon_theme,
211
 
                                                   icon_no_extension,
212
 
                                                   48,
213
 
                                                   0);
214
 
 
215
 
                full = NULL;
216
 
                if (info) {
217
 
                        full = g_strdup (gtk_icon_info_get_filename (info));
218
 
                        gtk_icon_info_free (info);
219
 
                }
220
 
 
221
 
                g_free (icon_no_extension);
222
 
        }
223
 
        return full;
224
 
#endif
225
134
}
226
135
 
227
136
/**
234
143
 */
235
144
void awn_desktop_item_set_icon (AwnDesktopItem *item, gchar *icon)
236
145
{
237
 
#ifdef LIBAWN_USE_GNOME
238
146
        gnome_desktop_item_set_string (item, GNOME_DESKTOP_ITEM_ICON, icon);
239
 
#else
240
 
        awn_desktop_item_set_string (item, EGG_DESKTOP_FILE_KEY_ICON, icon);
241
 
#endif
242
147
}
243
148
 
244
149
/**
250
155
 */
251
156
gchar *awn_desktop_item_get_name (AwnDesktopItem *item)
252
157
{
253
 
#ifdef LIBAWN_USE_GNOME
254
158
        return gnome_desktop_item_get_localestring (item,
255
159
                                                    GNOME_DESKTOP_ITEM_NAME);
256
 
#else
257
 
        return (gchar*)egg_desktop_file_get_name ((EggDesktopFile*)item);
258
 
#endif
259
160
}
260
161
 
261
162
/**
267
168
 */
268
169
void awn_desktop_item_set_name (AwnDesktopItem *item, gchar *name)
269
170
{
270
 
#ifdef LIBAWN_USE_GNOME
271
171
        gnome_desktop_item_set_string (item, GNOME_DESKTOP_ITEM_NAME, name);
272
 
#else
273
 
        awn_desktop_item_set_string (item, EGG_DESKTOP_FILE_KEY_NAME, name);
274
 
#endif
275
172
}
276
173
 
277
174
/**
283
180
 */
284
181
gchar *awn_desktop_item_get_exec (AwnDesktopItem *item)
285
182
{
286
 
#ifdef LIBAWN_USE_GNOME
287
183
        return gnome_desktop_item_get_string (item, GNOME_DESKTOP_ITEM_EXEC);
288
 
#else
289
 
        return awn_desktop_item_get_string (item, EGG_DESKTOP_FILE_KEY_EXEC);
290
 
#endif
291
184
}
292
185
 
293
186
/**
299
192
 */
300
193
void awn_desktop_item_set_exec (AwnDesktopItem *item, gchar *exec)
301
194
{
302
 
#ifdef LIBAWN_USE_GNOME
303
195
        gnome_desktop_item_set_string (item, GNOME_DESKTOP_ITEM_EXEC, exec);
304
 
#else
305
 
        awn_desktop_item_set_string (item, EGG_DESKTOP_FILE_KEY_EXEC, exec);
306
 
#endif
307
196
}
308
197
 
309
198
/**
316
205
 */
317
206
gchar *awn_desktop_item_get_string (AwnDesktopItem *item, gchar *key)
318
207
{
319
 
#ifdef LIBAWN_USE_GNOME
320
208
        return gnome_desktop_item_get_string (item, key);
321
 
#else
322
 
        gchar *value;
323
 
        GError *err = NULL;
324
 
        value = g_key_file_get_string (egg_desktop_file_get_key_file ((EggDesktopFile*)item), EGG_DESKTOP_FILE_GROUP, key, &err);
325
 
        if (err) {
326
 
                g_warning("Could not get the value of '%s' from '%s': %s",
327
 
                          key, awn_desktop_item_get_filename (item), err->message);
328
 
        }
329
 
        return value;
330
 
#endif
331
209
}
332
210
 
333
211
/**
340
218
 */
341
219
void awn_desktop_item_set_string (AwnDesktopItem *item, gchar *key, gchar *value)
342
220
{
343
 
#ifdef LIBAWN_USE_GNOME
344
221
        gnome_desktop_item_set_string (item, key, value);
345
 
#else
346
 
        g_key_file_set_string (egg_desktop_file_get_key_file ((EggDesktopFile*)item),
347
 
                               EGG_DESKTOP_FILE_GROUP, key, value);
348
 
#endif
349
222
}
350
223
 
351
224
/**
358
231
 */
359
232
gchar *awn_desktop_item_get_localestring (AwnDesktopItem *item, gchar *key)
360
233
{
361
 
#ifdef LIBAWN_USE_GNOME
362
234
        return gnome_desktop_item_get_localestring (item, key);
363
 
#else
364
 
        gchar *value;
365
 
        GError *err = NULL;
366
 
        const gchar* const *langs = g_get_language_names ();
367
 
        guint i = 0;
368
 
        guint langs_len = g_strv_length ((gchar**)langs);
369
 
        do {
370
 
                value = g_key_file_get_locale_string (egg_desktop_file_get_key_file ((EggDesktopFile*)item),
371
 
                                                      EGG_DESKTOP_FILE_GROUP, key, langs[i], &err);
372
 
                i++;
373
 
        } while (err && i < langs_len);
374
 
        if (err) {
375
 
                g_warning("Could not get the value of '%s' from '%s': %s",
376
 
                          key, awn_desktop_item_get_filename (item), err->message);
377
 
        }
378
 
        return value;
379
 
#endif
380
235
}
381
236
 
382
237
/**
391
246
 */
392
247
void awn_desktop_item_set_localestring (AwnDesktopItem *item, gchar *key, gchar *locale, gchar *value)
393
248
{
394
 
#ifdef LIBAWN_USE_GNOME
395
249
        if (locale == NULL) {
396
250
                gnome_desktop_item_set_localestring (item, key, value);
397
251
        } else {
398
252
                gnome_desktop_item_set_localestring_lang (item, key, locale, value);
399
253
        }
400
 
#else
401
 
        if (locale == NULL) {
402
 
                /* use the first locale from g_get_language_names () */
403
 
                locale = (gchar*)(g_get_language_names ()[0]);
404
 
        }
405
 
        g_key_file_set_locale_string (egg_desktop_file_get_key_file ((EggDesktopFile*)item),
406
 
                                      EGG_DESKTOP_FILE_GROUP, key, locale, value);
407
 
#endif
408
254
}
409
255
 
410
256
/**
417
263
 */
418
264
gboolean awn_desktop_item_exists (AwnDesktopItem *item)
419
265
{
420
 
#ifdef LIBAWN_USE_GNOME
421
266
        return gnome_desktop_item_exists (item);
422
 
#else
423
 
        return egg_desktop_file_can_launch ((EggDesktopFile*)item, NULL);
424
 
#endif
425
267
}
426
268
 
427
269
/**
437
279
 */
438
280
gint awn_desktop_item_launch (AwnDesktopItem *item, GSList *documents, GError **err)
439
281
{
440
 
#ifdef LIBAWN_USE_GNOME
441
282
        return gnome_desktop_item_launch_on_screen (item,
442
283
                                                    NULL,
443
284
                                                    0,
444
285
                                                    gdk_screen_get_default(),
445
286
                                                    -1,
446
287
                                                    err);
447
 
#else
448
 
        GPid pid;
449
 
        egg_desktop_file_launch ((EggDesktopFile*)item, documents, err,
450
 
                                 EGG_DESKTOP_FILE_LAUNCH_RETURN_PID, &pid,
451
 
                                 NULL);
452
 
        return pid;
453
 
#endif
454
288
}
455
289
 
456
290
/**
466
300
 */
467
301
void awn_desktop_item_save (AwnDesktopItem *item, gchar *new_filename, GError **err)
468
302
{
469
 
#ifdef LIBAWN_USE_GNOME
470
303
        gnome_desktop_item_save (item, new_filename, TRUE, err);
471
 
#else
472
 
        gchar *data;
473
 
        gchar *filename;
474
 
        gsize data_len;
475
 
        data = g_key_file_to_data (egg_desktop_file_get_key_file ((EggDesktopFile*)item), &data_len, err);
476
 
        if (err) {
477
 
                g_free (data);
478
 
                return;
479
 
        } else {
480
 
                if (new_filename) {
481
 
                        filename = new_filename;
482
 
                } else {
483
 
                        filename = awn_desktop_item_get_filename (item);
484
 
                }
485
 
                /* requires glib 2.8 */
486
 
                g_file_set_contents (filename, data, data_len, err);
487
 
                g_free (data);
488
 
        }
489
 
#endif
490
304
}
491
305
 
492
306
/**
497
311
 */
498
312
void awn_desktop_item_free (AwnDesktopItem *item)
499
313
{
500
 
#ifdef LIBAWN_USE_GNOME
501
314
        gnome_desktop_item_unref (item);
502
 
#else
503
 
        egg_desktop_file_free ((EggDesktopFile*)item);
504
 
#endif
505
315
}
506
316
/*  vim: set noet ts=8 sts=8 sw=8 : */