1
/* vim: set sw=8: -*- mode: c; style: linux -*- */
4
* Copyright (C) 2000, 2001 Ximian, Inc.
5
* Copyright (C) 1998 Red Hat Software, Inc.
7
* Written by Bradford Hovinen <hovinen@ximian.com>,
8
* Jonathan Blandford <jrb@redhat.com>
10
* This program is free software; you can redistribute it and/or modify
11
* it under the terms of the GNU General Public License as published by
12
* the Free Software Foundation; either version 2, or (at your option)
15
* This program is distributed in the hope that it will be useful,
16
* but WITHOUT ANY WARRANTY; without even the implied warranty of
17
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
* GNU General Public License for more details.
20
* You should have received a copy of the GNU General Public License
21
* along with this program; if not, write to the Free Software
22
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
32
#include <sys/types.h>
36
#include "capplet-dir.h"
37
#include "capplet-dir-view.h"
39
static void capplet_activate (Capplet *capplet);
40
static void capplet_dir_activate (CappletDir *capplet_dir,
41
CappletDirView *launcher);
43
static void capplet_shutdown (Capplet *capplet);
44
static void capplet_dir_shutdown (CappletDir *capplet_dir);
46
static GSList *read_entries (CappletDir *dir);
48
#ifdef USE_ROOT_MANAGER
49
static void start_capplet_through_root_manager (GnomeDesktopItem *gde);
52
CappletDirView *(*get_view_cb) (CappletDir *dir, CappletDirView *launcher);
54
/* nice global table for capplet lookup */
55
GHashTable *capplet_hash = NULL;
57
/********************************************************************
59
* Stolen from nautilus to keep control center and nautilus in sync
62
eel_str_has_suffix (const char *haystack, const char *needle)
69
if (haystack == NULL) {
70
return needle[0] == '\0';
73
/* Eat one character at a time. */
74
h = haystack + strlen(haystack);
75
n = needle + strlen(needle);
83
} while (*--h == *--n);
87
eel_str_strip_trailing_str (const char *source, const char *remove_this)
93
if (remove_this == NULL) {
94
return g_strdup (source);
96
end = source + strlen (source);
97
if (strcmp (end - strlen (remove_this), remove_this) != 0) {
98
return g_strdup (source);
101
return g_strndup (source, strlen (source) - strlen(remove_this));
106
nautilus_remove_icon_file_name_suffix (const char *icon_name)
110
static const char *icon_file_name_suffixes[] = { ".svg", ".svgz", ".png", ".jpg", ".xpm" };
112
for (i = 0; i < G_N_ELEMENTS (icon_file_name_suffixes); i++) {
113
suffix = icon_file_name_suffixes[i];
114
if (eel_str_has_suffix (icon_name, suffix)) {
115
return eel_str_strip_trailing_str (icon_name, suffix);
118
return g_strdup (icon_name);
120
/********************************************************************/
123
find_icon (GnomeDesktopItem *dentry)
127
GtkIconTheme *icon_theme = gtk_icon_theme_get_default ();
129
icon = gnome_desktop_item_get_string (dentry, GNOME_DESKTOP_ITEM_ICON);
131
if (icon == NULL || icon[0] == 0)
132
icon = "gnome-settings";
133
else if (g_path_is_absolute (icon))
134
res = gdk_pixbuf_new_from_file (icon, NULL);
136
char *no_suffix = nautilus_remove_icon_file_name_suffix (icon);
137
res = gtk_icon_theme_load_icon (icon_theme, no_suffix, 48, 0, NULL);
140
char *path = g_build_filename (GNOMECC_ICONS_DIR, icon, NULL);
141
res = gdk_pixbuf_new_from_file (path, NULL);
146
res = gtk_icon_theme_load_icon (icon_theme, "gnome-unknown", 48, 0, NULL);
148
res = gtk_icon_theme_load_icon (icon_theme, "gtk-missing-image", 48, 0, NULL);
153
capplet_new (CappletDir *dir, gchar *desktop_path)
156
CappletDirEntry *entry;
157
GnomeDesktopItem *dentry;
159
const gchar *execstr;
161
g_return_val_if_fail (desktop_path != NULL, NULL);
163
entry = g_hash_table_lookup (capplet_hash, desktop_path);
168
dentry = gnome_desktop_item_new_from_uri (desktop_path,
169
GNOME_DESKTOP_ITEM_TYPE_NULL,
174
execstr = gnome_desktop_item_get_string (dentry,
175
GNOME_DESKTOP_ITEM_EXEC);
176
/* Perhaps use poptParseArgvString here */
177
vec = g_strsplit (execstr, " ", 0);
178
if (!(execstr && execstr[0]) || !(vec && (path = g_find_program_in_path (vec[0]))))
181
gnome_desktop_item_unref (dentry);
186
capplet = g_new0 (Capplet, 1);
187
capplet->launching = FALSE;
189
entry = CAPPLET_DIR_ENTRY (capplet);
191
entry->type = TYPE_CAPPLET;
192
entry->entry = dentry;
194
entry->label = g_strdup (gnome_desktop_item_get_localestring (dentry,
195
GNOME_DESKTOP_ITEM_NAME));
196
entry->icon = find_icon (dentry);
197
entry->uri = gnome_vfs_uri_new (desktop_path);
201
g_hash_table_insert (capplet_hash, g_strdup (desktop_path), entry);
207
capplet_dir_new (CappletDir *dir, gchar *dir_path)
209
CappletDir *capplet_dir;
210
CappletDirEntry *entry;
211
GnomeVFSURI *desktop_uri;
212
GnomeVFSURI *dir_uri;
213
char *desktop_uri_string;
215
g_return_val_if_fail (dir_path != NULL, NULL);
217
dir_uri = gnome_vfs_uri_new (dir_path);
219
entry = g_hash_table_lookup (capplet_hash, dir_path);
224
desktop_uri = gnome_vfs_uri_append_file_name (dir_uri, ".directory");
225
desktop_uri_string = gnome_vfs_uri_to_string (desktop_uri, GNOME_VFS_URI_HIDE_NONE);
227
capplet_dir = g_new0 (CappletDir, 1);
228
entry = CAPPLET_DIR_ENTRY (capplet_dir);
230
entry->type = TYPE_CAPPLET_DIR;
231
entry->entry = gnome_desktop_item_new_from_uri (desktop_uri_string,
232
GNOME_DESKTOP_ITEM_TYPE_NULL,
235
entry->uri = dir_uri;
237
gnome_vfs_uri_unref (desktop_uri);
238
g_free (desktop_uri_string);
241
entry->label = g_strdup (gnome_desktop_item_get_localestring (
243
GNOME_DESKTOP_ITEM_NAME));
244
entry->icon = find_icon (entry->entry);
246
/* If the .directory file could not be found or read, abort */
247
g_free (capplet_dir);
253
g_hash_table_insert (capplet_hash, g_strdup (dir_path), entry);
255
capplet_dir_load (CAPPLET_DIR (entry));
261
capplet_lookup (const char *path)
263
return g_hash_table_lookup (capplet_hash, path);
267
capplet_dir_entry_destroy (CappletDirEntry *entry)
269
if (entry->type == TYPE_CAPPLET) {
270
capplet_shutdown (CAPPLET (entry));
272
capplet_dir_shutdown (CAPPLET_DIR (entry));
275
g_free (entry->label);
276
g_object_unref (entry->icon);
277
gnome_vfs_uri_unref (entry->uri);
278
g_strfreev (entry->exec);
279
gnome_desktop_item_unref (entry->entry);
284
capplet_dir_entry_activate (CappletDirEntry *entry,
285
CappletDirView *launcher)
287
g_return_if_fail (entry != NULL);
289
if (entry->type == TYPE_CAPPLET)
290
capplet_activate (CAPPLET (entry));
291
else if (entry->type == TYPE_CAPPLET_DIR)
292
capplet_dir_activate (CAPPLET_DIR (entry), launcher);
294
g_assert_not_reached ();
298
capplet_dir_entry_shutdown (CappletDirEntry *entry)
300
if (entry->type == TYPE_CAPPLET)
301
capplet_shutdown (CAPPLET (entry));
302
else if (entry->type == TYPE_CAPPLET_DIR)
303
capplet_dir_shutdown (CAPPLET_DIR (entry));
305
g_assert_not_reached ();
309
capplet_reset_cb (Capplet *capplet)
311
capplet->launching = FALSE;
316
capplet_activate (Capplet *capplet)
318
CappletDirEntry *entry;
320
entry = CAPPLET_DIR_ENTRY (capplet);
322
if (capplet->launching) {
325
capplet->launching = TRUE;
326
gtk_timeout_add (1000, (GtkFunction) capplet_reset_cb, capplet);
327
gnome_desktop_item_launch (entry->entry, NULL, 0, NULL);
332
capplet_dir_load (CappletDir *capplet_dir)
334
if (capplet_dir->entries) return;
335
capplet_dir->entries = read_entries (capplet_dir);
339
capplet_dir_activate (CappletDir *capplet_dir, CappletDirView *launcher)
341
capplet_dir_load (capplet_dir);
342
capplet_dir->view = get_view_cb (capplet_dir, launcher);
344
capplet_dir_view_load_dir (capplet_dir->view, capplet_dir);
345
capplet_dir_view_show (capplet_dir->view);
349
capplet_shutdown (Capplet *capplet)
351
/* Can't do much here ... :-( */
355
cde_destroy (CappletDirEntry *e, gpointer null)
357
capplet_dir_entry_destroy (e);
361
capplet_dir_shutdown (CappletDir *capplet_dir)
363
if (capplet_dir->view)
364
g_object_unref (G_OBJECT (capplet_dir->view));
366
g_slist_foreach (capplet_dir->entries, (GFunc) cde_destroy, NULL);
368
g_slist_free (capplet_dir->entries);
372
node_compare (gconstpointer a, gconstpointer b)
374
return strcmp (CAPPLET_DIR_ENTRY (a)->label,
375
CAPPLET_DIR_ENTRY (b)->label);
378
/* Adapted from the original control center... */
381
read_entries (CappletDir *dir)
384
CappletDirEntry *entry;
385
gchar *fullpath, *test;
386
GnomeVFSURI *fulluri;
387
GnomeVFSDirectoryHandle *parent_dir;
388
GnomeVFSResult result;
390
GnomeVFSFileInfo *child = gnome_vfs_file_info_new ();
392
result = gnome_vfs_directory_open_from_uri (&parent_dir, CAPPLET_DIR_ENTRY (dir)->uri,
393
GNOME_VFS_FILE_INFO_DEFAULT);
395
if (result != GNOME_VFS_OK) {
396
gnome_vfs_file_info_unref (child);
400
while ( gnome_vfs_directory_read_next (parent_dir, child) == GNOME_VFS_OK ) {
401
if (child->name[0] == '.')
404
fulluri = gnome_vfs_uri_append_path (CAPPLET_DIR_ENTRY (dir)->uri, child->name);
405
fullpath = gnome_vfs_uri_to_string (fulluri, GNOME_VFS_URI_HIDE_NONE);
409
if (child->type == GNOME_VFS_FILE_TYPE_DIRECTORY) {
410
entry = capplet_dir_new (dir, fullpath);
412
test = strrchr(child->name, '.');
414
/* if it's a .desktop file, it's interesting for sure! */
415
if (test && !strcmp (".desktop", test))
416
entry = capplet_new (dir, fullpath);
420
list = g_slist_prepend (list, entry);
425
gnome_vfs_file_info_unref (child);
426
gnome_vfs_directory_close (parent_dir);
428
list = g_slist_sort (list, node_compare);
430
/* remove FALSE for parent entry */
431
return FALSE && CAPPLET_DIR_ENTRY (dir)->dir
432
? g_slist_prepend (list, CAPPLET_DIR_ENTRY (dir)->dir)
436
#ifdef USE_ROOT_MANAGER
438
start_capplet_through_root_manager (GnomeDesktopItem *gde)
440
static FILE *output = NULL;
451
if (pid == (pid_t) -1) {
452
g_error ("%s", g_strerror (errno));
453
} else if (pid == 0) {
457
dup2 (pipe_fd[0], STDIN_FILENO);
459
for (i = 3; i < FOPEN_MAX; i++) close(i);
461
arg[0] = gnome_is_program_in_path ("root-manager-helper");
465
output = fdopen(pipe_fd[1], "a");
468
capplet_dir_views_set_authenticated (TRUE);
472
oldexec = gde->exec[1];
473
gde->exec[1] = g_concat_dir_and_file (GNOME_SBINDIR, oldexec);
475
cmdline = g_strjoinv (" ", gde->exec + 1);
477
g_free (gde->exec[1]);
478
gde->exec[1] = oldexec;
480
fprintf (output, "%s\n", cmdline);
487
capplet_dir_init (CappletDirView *(*cb) (CappletDir *, CappletDirView *))
490
capplet_hash = g_hash_table_new (g_str_hash, g_str_equal);
494
get_root_capplet_dir (void)
496
static CappletDir *root_dir = NULL;
498
if (root_dir == NULL) {
499
CappletDirEntry *entry;
501
entry = capplet_dir_new (NULL, "preferences:///");
504
root_dir = CAPPLET_DIR (entry);
506
g_warning ("Could not find directory of control panels [%s]", "preferences:///");