Defuzzied one entry
[midnight-commander.git] / gnome / gdesktop-init.c
blob8ba0161d8e6d01bd383d7ddf857ec6973bd220e8
1 /* Customizable desktop links for the Midnight Commander
3 * Copyright (C) 1998-1999 The Free Software Foundation
5 * Authors: Miguel de Icaza <miguel@nuclecu.unam.mx>
6 */
8 #include <config.h>
9 #include <unistd.h>
10 #include <libgnome/libgnome.h>
11 #include "x.h"
12 #include "main.h"
13 #include "util.h"
14 #include "gdesktop.h"
15 #include "gdesktop-init.h"
16 #include "gprint.h"
17 #include "gmount.h"
18 #include "../vfs/vfs.h"
20 static void
21 desktop_load_init_from (const char *file)
23 char *key;
24 char *file_and_section;
25 char *title, *type;
27 void *iterator_handle;
28 char *config_path = g_strconcat ("=", file, "=", NULL);
30 iterator_handle = gnome_config_init_iterator_sections (config_path);
32 iterator_handle = gnome_config_iterator_next (
33 iterator_handle, &key, NULL);
35 do {
36 /* Now access the values in the section */
37 file_and_section = g_strconcat (config_path, "/", key, "/", NULL);
39 gnome_config_push_prefix (file_and_section);
40 title = gnome_config_get_translated_string ("title=None");
41 type = gnome_config_get_string ("type=url");
44 * handle the different link types
46 if (strcmp (type, "url") == 0){
47 int used;
48 char *icon, *url;
49 char *icon2 = NULL;
51 url = gnome_config_get_string ("url");
52 icon = gnome_config_get_string_with_default ("icon=", &used);
53 if (icon){
54 icon2 = gnome_pixmap_file (icon);
55 g_free (icon);
57 if (!icon2)
58 icon2 = g_concat_dir_and_file (ICONDIR, "gnome-http-url.png");
59 if (url && *url){
60 char *filename = g_concat_dir_and_file (desktop_directory, key);
62 desktop_create_url (filename, title, url, icon2, FALSE);
63 g_free (filename);
66 if (url)
67 g_free (url);
68 g_free (icon2);
70 g_free (title);
71 g_free (file_and_section);
72 gnome_config_pop_prefix ();
74 /* Get next section name */
75 iterator_handle = gnome_config_iterator_next (
76 iterator_handle, &key, NULL);
78 } while (iterator_handle);
80 g_free (config_path);
83 static void
84 desktop_init_at (const char *dir)
86 DIR *d;
87 struct dirent *dent;
88 struct stat s;
89 const int links_extlen = sizeof (".links")-1;
91 d = opendir (dir);
92 if (!d)
93 return;
95 while ((dent = readdir (d)) != NULL){
96 int len = strlen (dent->d_name);
97 char *fname;
99 fname = g_concat_dir_and_file (dir, dent->d_name);
100 if (stat (fname, &s) < 0) {
101 g_free (fname);
102 continue;
104 if (S_ISDIR (s.st_mode)) {
105 g_free (fname);
106 continue;
108 if (is_exe (s.st_mode)) {
109 /* let's try running it */
110 char *desktop_quoted;
111 char *command;
113 desktop_quoted = name_quote (desktop_directory, 0);
114 command = g_strconcat (fname, " --desktop-dir=", desktop_quoted, NULL);
115 g_free (desktop_quoted);
117 my_system (EXECUTE_WAIT | EXECUTE_AS_SHELL, shell, command);
118 g_free (command);
119 g_free (fname);
120 continue;
123 if (len < links_extlen){
124 g_free (fname);
125 continue;
128 if (strcmp (dent->d_name + len - links_extlen, ".links")){
129 g_free (fname);
130 continue;
133 desktop_load_init_from (fname);
134 g_free (fname);
136 closedir (d);
139 void
140 gdesktop_links_init (void)
142 char *link_name;
143 char *icon;
144 char *dir;
146 /* Create the link to the user's home directory so that he will have an icon */
147 link_name = g_concat_dir_and_file (desktop_directory, _("Home directory"));
148 mc_symlink (gnome_user_home_dir, link_name);
149 g_free (link_name);
151 /* Create the link to the user's trash directory */
152 link_name = g_concat_dir_and_file (desktop_directory, "Trash.gmc");
153 icon = gnome_pixmap_file ("mc/gnome-trashcan.png");
154 mc_mkdir (link_name, S_IRUSR | S_IWUSR | S_IXUSR );
155 if (icon){
156 gnome_metadata_set (link_name, "icon-filename", strlen (icon) + 1, icon);
157 g_free (icon);
159 gnome_metadata_set (link_name, "icon-caption", strlen (_("Trash")) + 1, _("Trash"));
160 g_free (link_name);
162 /* Create custom links */
164 desktop_init_at (DESKTOP_INIT_DIR);
166 dir = gnome_libdir_file ("desktop-links");
167 if (dir) {
168 desktop_init_at (dir);
169 g_free (dir);