r4567: Get ATK headers through Zero Install.
[rox-filer/translations.git] / ROX-Filer / src / diritem.c
blob869cff3c68003b2be37fa75e8127a5913e126939
1 /*
2 * ROX-Filer, filer for the ROX desktop project
3 * Copyright (C) 2006, Thomas Leonard and others (see changelog for details).
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the Free
7 * Software Foundation; either version 2 of the License, or (at your option)
8 * any later version.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
17 * Place, Suite 330, Boston, MA 02111-1307 USA
20 /* diritem.c - get details about files */
22 /* Don't load icons larger than 400K (this is rather excessive, basically
23 * we just want to stop people crashing the filer with huge icons).
25 #define MAX_ICON_SIZE (400 * 1024)
27 #include "config.h"
29 #include <gtk/gtk.h>
30 #include <errno.h>
31 #include <stdio.h>
32 #include <string.h>
33 #include <ctype.h>
35 #include "global.h"
37 #include "diritem.h"
38 #include "support.h"
39 #include "gui_support.h"
40 #include "mount.h"
41 #include "type.h"
42 #include "usericons.h"
43 #include "options.h"
44 #include "fscache.h"
45 #include "pixmaps.h"
46 #include "xtypes.h"
48 #define RECENT_DELAY (5 * 60) /* Time in seconds to consider a file recent */
49 #define ABOUT_NOW(time) (diritem_recent_time - time < RECENT_DELAY)
50 /* If you want to make use of the RECENT flag, make sure this is set to
51 * the current time before calling diritem_restat().
53 time_t diritem_recent_time;
55 /* Static prototypes */
56 static void examine_dir(const guchar *path, DirItem *item,
57 struct stat *link_target);
59 /****************************************************************
60 * EXTERNAL INTERFACE *
61 ****************************************************************/
63 void diritem_init(void)
65 read_globicons();
68 /* Bring this item's structure uptodate.
69 * 'parent' is optional; it saves one stat() for directories.
71 void diritem_restat(const guchar *path, DirItem *item, struct stat *parent)
73 struct stat info;
75 if (item->_image)
77 g_object_unref(item->_image);
78 item->_image = NULL;
80 item->flags = 0;
81 item->mime_type = NULL;
83 if (mc_lstat(path, &info) == -1)
85 item->lstat_errno = errno;
86 item->base_type = TYPE_ERROR;
87 item->size = 0;
88 item->mode = 0;
89 item->mtime = item->ctime = item->atime = 0;
90 item->uid = (uid_t) -1;
91 item->gid = (gid_t) -1;
93 else
95 guchar *target_path;
97 item->lstat_errno = 0;
98 item->size = info.st_size;
99 item->mode = info.st_mode;
100 item->atime = info.st_atime;
101 item->ctime = info.st_ctime;
102 item->mtime = info.st_mtime;
103 item->uid = info.st_uid;
104 item->gid = info.st_gid;
105 if (ABOUT_NOW(item->mtime) || ABOUT_NOW(item->ctime))
106 item->flags |= ITEM_FLAG_RECENT;
108 if (xattr_have(path))
109 item->flags |= ITEM_FLAG_HAS_XATTR;
111 if (S_ISLNK(info.st_mode))
113 if (mc_stat(path, &info))
114 item->base_type = TYPE_ERROR;
115 else
116 item->base_type =
117 mode_to_base_type(info.st_mode);
119 item->flags |= ITEM_FLAG_SYMLINK;
121 target_path = readlink_dup(path);
123 else
125 item->base_type = mode_to_base_type(info.st_mode);
126 target_path = (guchar *) path;
129 if (item->base_type == TYPE_DIRECTORY)
131 if (mount_is_mounted(target_path, &info,
132 target_path == path ? parent : NULL))
133 item->flags |= ITEM_FLAG_MOUNT_POINT
134 | ITEM_FLAG_MOUNTED;
135 else if (g_hash_table_lookup(fstab_mounts,
136 target_path))
137 item->flags |= ITEM_FLAG_MOUNT_POINT;
140 if (path != target_path)
141 g_free(target_path);
144 if (item->base_type == TYPE_DIRECTORY)
146 /* KRJW: info.st_uid will be the uid of the dir, regardless
147 * of whether `path' is a dir or a symlink to one. Note that
148 * if path is a symlink to a dir, item->uid will be the uid
149 * of the *symlink*, but we really want the uid of the dir
150 * to which the symlink points.
152 examine_dir(path, item, &info);
154 else if (item->base_type == TYPE_FILE)
156 if (item->size == 0)
157 item->mime_type = text_plain;
158 else if (item->flags & ITEM_FLAG_SYMLINK)
160 guchar *link_path;
161 link_path = pathdup(path);
162 item->mime_type = type_from_path(link_path
163 ? link_path
164 : path);
165 g_free(link_path);
167 else
168 item->mime_type = type_from_path(path);
170 /* Note: for symlinks we need the mode of the target */
171 if (info.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))
173 /* Note that the flag is set for ALL executable
174 * files, but the mime_type must also be executable
175 * for clicking on the file to run it.
177 item->flags |= ITEM_FLAG_EXEC_FILE;
179 if (item->mime_type == NULL ||
180 item->mime_type == application_octet_stream)
182 item->mime_type = application_executable;
184 else if (item->mime_type == text_plain &&
185 !strchr(item->leafname, '.'))
187 item->mime_type = application_x_shellscript;
190 else if (item->mime_type == application_x_desktop)
192 item->flags |= ITEM_FLAG_EXEC_FILE;
195 if (!item->mime_type)
196 item->mime_type = text_plain;
198 check_globicon(path, item);
200 if (item->mime_type == application_x_desktop && item->_image == NULL)
202 item->_image = g_fscache_lookup(desktop_icon_cache, path);
205 else
206 check_globicon(path, item);
208 if (!item->mime_type)
209 item->mime_type = mime_type_from_base_type(item->base_type);
212 DirItem *diritem_new(const guchar *leafname)
214 DirItem *item;
216 item = g_new(DirItem, 1);
217 item->leafname = g_strdup(leafname);
218 item->may_delete = FALSE;
219 item->_image = NULL;
220 item->base_type = TYPE_UNKNOWN;
221 item->flags = ITEM_FLAG_NEED_RESCAN_QUEUE;
222 item->mime_type = NULL;
223 item->leafname_collate = collate_key_new(leafname);
225 return item;
228 void diritem_free(DirItem *item)
230 g_return_if_fail(item != NULL);
232 if (item->_image)
233 g_object_unref(item->_image);
234 item->_image = NULL;
235 collate_key_free(item->leafname_collate);
236 g_free(item->leafname);
237 g_free(item);
240 /* For use by di_image() only. Sets item->_image. */
241 void _diritem_get_image(DirItem *item)
243 g_return_if_fail(item->_image == NULL);
245 if (item->base_type == TYPE_ERROR)
247 item->_image = im_error;
248 g_object_ref(im_error);
250 else
251 item->_image = type_to_icon(item->mime_type);
254 /****************************************************************
255 * INTERNAL FUNCTIONS *
256 ****************************************************************/
258 /* Fill in more details of the DirItem for a directory item.
259 * - Looks for an image (but maybe still NULL on error)
260 * - Updates ITEM_FLAG_APPDIR
262 * link_target contains stat info for the link target for symlinks (or for the
263 * item itself if not a link).
265 static void examine_dir(const guchar *path, DirItem *item,
266 struct stat *link_target)
268 struct stat info;
269 static GString *tmp = NULL;
270 uid_t uid = link_target->st_uid;
272 if (!tmp)
273 tmp = g_string_new(NULL);
275 check_globicon(path, item);
277 if (item->flags & ITEM_FLAG_MOUNT_POINT)
279 item->mime_type = inode_mountpoint;
280 return; /* Try to avoid automounter problems */
283 if (link_target->st_mode & S_IWOTH)
284 return; /* Don't trust world-writable dirs */
286 /* Finding the icon:
288 * - If it contains a .DirIcon then that's the icon
289 * - If it contains an AppRun then it's an application
290 * - If it contains an AppRun but no .DirIcon then try to
291 * use AppIcon.xpm as the icon.
293 * .DirIcon and AppRun must have the same owner as the
294 * directory itself, to prevent abuse of /tmp, etc.
295 * For symlinks, we want the symlink's owner.
298 g_string_printf(tmp, "%s/.DirIcon", path);
300 if (item->_image)
301 goto no_diricon; /* Already got an icon */
303 if (mc_lstat(tmp->str, &info) != 0 || info.st_uid != uid)
304 goto no_diricon; /* Missing, or wrong owner */
306 if (S_ISLNK(info.st_mode) && mc_stat(tmp->str, &info) != 0)
307 goto no_diricon; /* Bad symlink */
309 if (info.st_size > MAX_ICON_SIZE || !S_ISREG(info.st_mode))
310 goto no_diricon; /* Too big, or non-regular file */
312 /* Try to load image; may still get NULL... */
313 item->_image = g_fscache_lookup(pixmap_cache, tmp->str);
315 no_diricon:
317 /* Try to find AppRun... */
318 g_string_truncate(tmp, tmp->len - 8);
319 g_string_append(tmp, "AppRun");
321 if (mc_lstat(tmp->str, &info) != 0 || info.st_uid != uid)
322 goto out; /* Missing, or wrong owner */
324 if (!(info.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH)))
325 goto out; /* Not executable */
327 item->flags |= ITEM_FLAG_APPDIR;
329 /* Try to load AppIcon.xpm... */
331 if (item->_image)
332 goto out; /* Already got an icon */
334 g_string_truncate(tmp, tmp->len - 3);
335 g_string_append(tmp, "Icon.xpm");
337 /* Note: since AppRun is valid we don't need to check AppIcon.xpm
338 * so carefully.
341 if (mc_stat(tmp->str, &info) != 0)
342 goto out; /* Missing, or broken symlink */
344 if (info.st_size > MAX_ICON_SIZE || !S_ISREG(info.st_mode))
345 goto out; /* Too big, or non-regular file */
347 /* Try to load image; may still get NULL... */
348 item->_image = g_fscache_lookup(pixmap_cache, tmp->str);
350 out:
352 if ((item->flags & ITEM_FLAG_APPDIR) && !item->_image)
354 /* This is an application without an icon */
355 item->_image = im_appdir;
356 g_object_ref(item->_image);