r4034: Use application_x_shellscript for scripts.
[rox-filer/translations.git] / ROX-Filer / src / diritem.c
blob6aac619c927882b6cec2de5ee6388763bf268f9a
1 /*
2 * $Id$
4 * Copyright (C) 2005, the ROX-Filer team.
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation; either version 2 of the License, or (at your option)
9 * any later version.
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * more details.
16 * You should have received a copy of the GNU General Public License along with
17 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
18 * Place, Suite 330, Boston, MA 02111-1307 USA
21 /* diritem.c - get details about files */
23 /* Don't load icons larger than 400K (this is rather excessive, basically
24 * we just want to stop people crashing the filer with huge icons).
26 #define MAX_ICON_SIZE (400 * 1024)
28 #include "config.h"
30 #include <gtk/gtk.h>
31 #include <errno.h>
32 #include <stdio.h>
33 #include <string.h>
34 #include <ctype.h>
36 #include "global.h"
38 #include "diritem.h"
39 #include "support.h"
40 #include "gui_support.h"
41 #include "mount.h"
42 #include "type.h"
43 #include "usericons.h"
44 #include "options.h"
45 #include "fscache.h"
46 #include "pixmaps.h"
47 #include "xtypes.h"
49 #define RECENT_DELAY (5 * 60) /* Time in seconds to consider a file recent */
50 #define ABOUT_NOW(time) (diritem_recent_time - time < RECENT_DELAY)
51 /* If you want to make use of the RECENT flag, make sure this is set to
52 * the current time before calling diritem_restat().
54 time_t diritem_recent_time;
56 /* Static prototypes */
57 static void examine_dir(const guchar *path, DirItem *item,
58 struct stat *link_target);
60 /****************************************************************
61 * EXTERNAL INTERFACE *
62 ****************************************************************/
64 void diritem_init(void)
66 read_globicons();
69 /* Bring this item's structure uptodate.
70 * 'parent' is optional; it saves one stat() for directories.
72 void diritem_restat(const guchar *path, DirItem *item, struct stat *parent)
74 struct stat info;
76 if (item->_image)
78 g_object_unref(item->_image);
79 item->_image = NULL;
81 item->flags = 0;
82 item->mime_type = NULL;
84 if (mc_lstat(path, &info) == -1)
86 item->lstat_errno = errno;
87 item->base_type = TYPE_ERROR;
88 item->size = 0;
89 item->mode = 0;
90 item->mtime = item->ctime = item->atime = 0;
91 item->uid = (uid_t) -1;
92 item->gid = (gid_t) -1;
94 else
96 guchar *target_path;
98 item->lstat_errno = 0;
99 item->size = info.st_size;
100 item->mode = info.st_mode;
101 item->atime = info.st_atime;
102 item->ctime = info.st_ctime;
103 item->mtime = info.st_mtime;
104 item->uid = info.st_uid;
105 item->gid = info.st_gid;
106 if (ABOUT_NOW(item->mtime) || ABOUT_NOW(item->ctime))
107 item->flags |= ITEM_FLAG_RECENT;
109 if (xtype_have_attr(path))
110 item->flags |= ITEM_FLAG_HAS_XATTR;
112 if (S_ISLNK(info.st_mode))
114 if (mc_stat(path, &info))
115 item->base_type = TYPE_ERROR;
116 else
117 item->base_type =
118 mode_to_base_type(info.st_mode);
120 item->flags |= ITEM_FLAG_SYMLINK;
122 target_path = readlink_dup(path);
124 else
126 item->base_type = mode_to_base_type(info.st_mode);
127 target_path = (guchar *) path;
130 if (item->base_type == TYPE_DIRECTORY)
132 if (mount_is_mounted(target_path, &info,
133 target_path == path ? parent : NULL))
134 item->flags |= ITEM_FLAG_MOUNT_POINT
135 | ITEM_FLAG_MOUNTED;
136 else if (g_hash_table_lookup(fstab_mounts,
137 target_path))
138 item->flags |= ITEM_FLAG_MOUNT_POINT;
141 if (path != target_path)
142 g_free(target_path);
145 if (item->base_type == TYPE_DIRECTORY)
147 /* KRJW: info.st_uid will be the uid of the dir, regardless
148 * of whether `path' is a dir or a symlink to one. Note that
149 * if path is a symlink to a dir, item->uid will be the uid
150 * of the *symlink*, but we really want the uid of the dir
151 * to which the symlink points.
153 examine_dir(path, item, &info);
155 else if (item->base_type == TYPE_FILE)
157 if (item->size == 0)
158 item->mime_type = text_plain;
159 else if (item->flags & ITEM_FLAG_SYMLINK)
161 guchar *link_path;
162 link_path = pathdup(path);
163 item->mime_type = type_from_path(link_path
164 ? link_path
165 : path);
166 g_free(link_path);
168 else
169 item->mime_type = type_from_path(path);
171 /* Note: for symlinks we need the mode of the target */
172 if (info.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))
174 /* Note that the flag is set for ALL executable
175 * files, but the mime_type must also be executable
176 * for clicking on the file to run it.
178 item->flags |= ITEM_FLAG_EXEC_FILE;
180 if (item->mime_type == NULL ||
181 item->mime_type == application_octet_stream)
183 item->mime_type = application_executable;
185 else if (item->mime_type == text_plain &&
186 !strchr(item->leafname, '.'))
188 item->mime_type = application_x_shellscript;
192 if (!item->mime_type)
193 item->mime_type = text_plain;
195 check_globicon(path, item);
197 else
198 check_globicon(path, item);
200 if (!item->mime_type)
201 item->mime_type = mime_type_from_base_type(item->base_type);
204 DirItem *diritem_new(const guchar *leafname)
206 DirItem *item;
208 item = g_new(DirItem, 1);
209 item->leafname = g_strdup(leafname);
210 item->may_delete = FALSE;
211 item->_image = NULL;
212 item->base_type = TYPE_UNKNOWN;
213 item->flags = ITEM_FLAG_NEED_RESCAN_QUEUE;
214 item->mime_type = NULL;
215 item->leafname_collate = collate_key_new(leafname);
217 return item;
220 void diritem_free(DirItem *item)
222 g_return_if_fail(item != NULL);
224 if (item->_image)
225 g_object_unref(item->_image);
226 item->_image = NULL;
227 collate_key_free(item->leafname_collate);
228 g_free(item->leafname);
229 g_free(item);
232 /* For use by di_image() only. Sets item->_image. */
233 void _diritem_get_image(DirItem *item)
235 g_return_if_fail(item->_image == NULL);
237 if (item->base_type == TYPE_ERROR)
239 item->_image = im_error;
240 g_object_ref(im_error);
242 else
243 item->_image = type_to_icon(item->mime_type);
246 /****************************************************************
247 * INTERNAL FUNCTIONS *
248 ****************************************************************/
250 /* Fill in more details of the DirItem for a directory item.
251 * - Looks for an image (but maybe still NULL on error)
252 * - Updates ITEM_FLAG_APPDIR
254 * link_target contains stat info for the link target for symlinks (or for the
255 * item itself if not a link).
257 static void examine_dir(const guchar *path, DirItem *item,
258 struct stat *link_target)
260 struct stat info;
261 static GString *tmp = NULL;
262 uid_t uid = link_target->st_uid;
264 if (!tmp)
265 tmp = g_string_new(NULL);
267 check_globicon(path, item);
269 if (item->flags & ITEM_FLAG_MOUNT_POINT)
271 item->mime_type = inode_mountpoint;
272 return; /* Try to avoid automounter problems */
275 if (link_target->st_mode & S_IWOTH)
276 return; /* Don't trust world-writable dirs */
278 /* Finding the icon:
280 * - If it contains a .DirIcon then that's the icon
281 * - If it contains an AppRun then it's an application
282 * - If it contains an AppRun but no .DirIcon then try to
283 * use AppIcon.xpm as the icon.
285 * .DirIcon and AppRun must have the same owner as the
286 * directory itself, to prevent abuse of /tmp, etc.
287 * For symlinks, we want the symlink's owner.
290 g_string_printf(tmp, "%s/.DirIcon", path);
292 if (item->_image)
293 goto no_diricon; /* Already got an icon */
295 if (mc_lstat(tmp->str, &info) != 0 || info.st_uid != uid)
296 goto no_diricon; /* Missing, or wrong owner */
298 if (S_ISLNK(info.st_mode) && mc_stat(tmp->str, &info) != 0)
299 goto no_diricon; /* Bad symlink */
301 if (info.st_size > MAX_ICON_SIZE || !S_ISREG(info.st_mode))
302 goto no_diricon; /* Too big, or non-regular file */
304 /* Try to load image; may still get NULL... */
305 item->_image = g_fscache_lookup(pixmap_cache, tmp->str);
307 no_diricon:
309 /* Try to find AppRun... */
310 g_string_truncate(tmp, tmp->len - 8);
311 g_string_append(tmp, "AppRun");
313 if (mc_lstat(tmp->str, &info) != 0 || info.st_uid != uid)
314 goto out; /* Missing, or wrong owner */
316 if (!(info.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH)))
317 goto out; /* Not executable */
319 item->flags |= ITEM_FLAG_APPDIR;
321 /* Try to load AppIcon.xpm... */
323 if (item->_image)
324 goto out; /* Already got an icon */
326 g_string_truncate(tmp, tmp->len - 3);
327 g_string_append(tmp, "Icon.xpm");
329 /* Note: since AppRun is valid we don't need to check AppIcon.xpm
330 * so carefully.
333 if (mc_stat(tmp->str, &info) != 0)
334 goto out; /* Missing, or broken symlink */
336 if (info.st_size > MAX_ICON_SIZE || !S_ISREG(info.st_mode))
337 goto out; /* Too big, or non-regular file */
339 /* Try to load image; may still get NULL... */
340 item->_image = g_fscache_lookup(pixmap_cache, tmp->str);
342 out:
344 if ((item->flags & ITEM_FLAG_APPDIR) && !item->_image)
346 /* This is an application without an icon */
347 item->_image = im_appdir;
348 g_object_ref(item->_image);