4 * Copyright (C) 2008 - 2012 The Geeqie Team
8 * This software is released under the GNU General Public License (GNU GPL).
9 * Please read the included file COPYING for more information.
10 * This software comes with no warranty of any kind, use at your own risk!
14 #include "pan-types.h"
16 #include "ui_fileops.h"
20 *-----------------------------------------------------------------------------
22 *-----------------------------------------------------------------------------
25 gboolean pan_date_compare(time_t a, time_t b, PanDateLengthType length)
30 if (length == PAN_DATE_LENGTH_EXACT) return (a == b);
32 if (!localtime_r(&a, &ta) ||
33 !localtime_r(&b, &tb)) return FALSE;
35 if (ta.tm_year != tb.tm_year) return FALSE;
36 if (length == PAN_DATE_LENGTH_YEAR) return TRUE;
38 if (ta.tm_mon != tb.tm_mon) return FALSE;
39 if (length == PAN_DATE_LENGTH_MONTH) return TRUE;
41 if (length == PAN_DATE_LENGTH_WEEK) return (ta.tm_yday / 7 == tb.tm_yday / 7);
43 if (ta.tm_mday != tb.tm_mday) return FALSE;
44 if (length == PAN_DATE_LENGTH_DAY) return TRUE;
46 return (ta.tm_hour == tb.tm_hour);
49 gint pan_date_value(time_t d, PanDateLengthType length)
53 if (!localtime_r(&d, &td)) return -1;
57 case PAN_DATE_LENGTH_DAY:
60 case PAN_DATE_LENGTH_WEEK:
63 case PAN_DATE_LENGTH_MONTH:
66 case PAN_DATE_LENGTH_YEAR:
67 return td.tm_year + 1900;
69 case PAN_DATE_LENGTH_EXACT:
77 gchar *pan_date_value_string(time_t d, PanDateLengthType length)
83 if (!localtime_r(&d, &td)) return g_strdup("");
87 case PAN_DATE_LENGTH_DAY:
88 return g_strdup_printf("%d", td.tm_mday);
90 case PAN_DATE_LENGTH_WEEK:
93 case PAN_DATE_LENGTH_MONTH:
96 case PAN_DATE_LENGTH_YEAR:
97 return g_strdup_printf("%d", td.tm_year + 1900);
99 case PAN_DATE_LENGTH_EXACT:
101 return g_strdup(text_from_time(d));
106 if (format && strftime(buf, sizeof(buf), format, &td) > 0)
108 gchar *ret = g_locale_to_utf8(buf, -1, NULL, NULL, NULL);
115 time_t pan_date_to_time(gint year, gint month, gint day)
122 lt.tm_mday = (day >= 1 && day <= 31) ? day : 1;
123 lt.tm_mon = (month >= 1 && month <= 12) ? month - 1 : 0;
124 lt.tm_year = year - 1900;
132 *-----------------------------------------------------------------------------
134 *-----------------------------------------------------------------------------
137 gboolean pan_is_link_loop(const gchar *s)
141 gboolean ret = FALSE;
143 sl = path_from_utf8(s);
145 if (lstat(sl, &st) == 0 && S_ISLNK(st.st_mode))
150 buf = g_malloc(st.st_size + 1);
151 l = readlink(sl, buf, st.st_size);
156 parse_out_relatives(buf);
159 parse_out_relatives(sl);
161 if (buf[0] == G_DIR_SEPARATOR)
163 if (strncmp(sl, buf, l) == 0 &&
164 (sl[l] == '\0' || sl[l] == G_DIR_SEPARATOR || l == 1)) ret = TRUE;
170 link_path = g_build_filename(sl, buf, NULL);
171 parse_out_relatives(link_path);
173 if (strncmp(sl, link_path, l) == 0 &&
174 (sl[l] == '\0' || sl[l] == G_DIR_SEPARATOR || l == 1)) ret = TRUE;
188 gboolean pan_is_ignored(const gchar *s, gboolean ignore_symlinks)
193 if (!lstat_utf8(s, &st)) return TRUE;
196 /* normal filesystems have directories with some size or block allocation,
197 * special filesystems (like linux /proc) set both to zero.
198 * enable this check if you enable listing the root "/" folder
200 if (st.st_size == 0 && st.st_blocks == 0) return TRUE;
203 if (S_ISLNK(st.st_mode) && (ignore_symlinks || pan_is_link_loop(s))) return TRUE;
205 n = filename_from_path(s);
206 if (n && strcmp(n, GQ_RC_DIR) == 0) return TRUE;
211 GList *pan_list_tree(FileData *dir_fd, SortType sort, gboolean ascend,
212 gboolean ignore_symlinks)
219 filelist_read(dir_fd, &flist, &dlist);
220 if (sort != SORT_NONE)
222 flist = filelist_sort(flist, sort, ascend);
223 dlist = filelist_sort(dlist, sort, ascend);
233 folders = g_list_remove(folders, fd);
235 if (!pan_is_ignored(fd->path, ignore_symlinks) &&
236 filelist_read(fd, &flist, &dlist))
238 if (sort != SORT_NONE)
240 flist = filelist_sort(flist, sort, ascend);
241 dlist = filelist_sort(dlist, sort, ascend);
244 result = g_list_concat(result, flist);
245 folders = g_list_concat(dlist, folders);
253 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */