7 * This software is released under the GNU General Public License (GNU GPL).
8 * Please read the included file COPYING for more information.
9 * This software comes with no warranty of any kind, use at your own risk!
13 #include "pan-types.h"
15 #include "ui_fileops.h"
19 *-----------------------------------------------------------------------------
21 *-----------------------------------------------------------------------------
24 gint pan_date_compare(time_t a, time_t b, PanDateLengthType length)
29 if (length == PAN_DATE_LENGTH_EXACT) return (a == b);
31 if (!localtime_r(&a, &ta) ||
32 !localtime_r(&b, &tb)) return FALSE;
34 if (ta.tm_year != tb.tm_year) return FALSE;
35 if (length == PAN_DATE_LENGTH_YEAR) return TRUE;
37 if (ta.tm_mon != tb.tm_mon) return FALSE;
38 if (length == PAN_DATE_LENGTH_MONTH) return TRUE;
40 if (length == PAN_DATE_LENGTH_WEEK) return (ta.tm_yday / 7 == tb.tm_yday / 7);
42 if (ta.tm_mday != tb.tm_mday) return FALSE;
43 if (length == PAN_DATE_LENGTH_DAY) return TRUE;
45 return (ta.tm_hour == tb.tm_hour);
48 gint pan_date_value(time_t d, PanDateLengthType length)
52 if (!localtime_r(&d, &td)) return -1;
56 case PAN_DATE_LENGTH_DAY:
59 case PAN_DATE_LENGTH_WEEK:
62 case PAN_DATE_LENGTH_MONTH:
65 case PAN_DATE_LENGTH_YEAR:
66 return td.tm_year + 1900;
68 case PAN_DATE_LENGTH_EXACT:
76 gchar *pan_date_value_string(time_t d, PanDateLengthType length)
82 if (!localtime_r(&d, &td)) return g_strdup("");
86 case PAN_DATE_LENGTH_DAY:
87 return g_strdup_printf("%d", td.tm_mday);
89 case PAN_DATE_LENGTH_WEEK:
92 case PAN_DATE_LENGTH_MONTH:
95 case PAN_DATE_LENGTH_YEAR:
96 return g_strdup_printf("%d", td.tm_year + 1900);
98 case PAN_DATE_LENGTH_EXACT:
100 return g_strdup(text_from_time(d));
105 if (format && strftime(buf, sizeof(buf), format, &td) > 0)
107 gchar *ret = g_locale_to_utf8(buf, -1, NULL, NULL, NULL);
114 time_t pan_date_to_time(gint year, gint month, gint day)
121 lt.tm_mday = (day >= 1 && day <= 31) ? day : 1;
122 lt.tm_mon = (month >= 1 && month <= 12) ? month - 1 : 0;
123 lt.tm_year = year - 1900;
131 *-----------------------------------------------------------------------------
133 *-----------------------------------------------------------------------------
136 gint pan_is_link_loop(const gchar *s)
142 sl = path_from_utf8(s);
144 if (lstat(sl, &st) == 0 && S_ISLNK(st.st_mode))
149 buf = g_malloc(st.st_size + 1);
150 l = readlink(sl, buf, st.st_size);
155 parse_out_relatives(buf);
158 parse_out_relatives(sl);
162 if (strncmp(sl, buf, l) == 0 &&
163 (sl[l] == '\0' || sl[l] == '/' || l == 1)) ret = TRUE;
169 link_path = concat_dir_and_file(sl, buf);
170 parse_out_relatives(link_path);
172 if (strncmp(sl, link_path, l) == 0 &&
173 (sl[l] == '\0' || sl[l] == '/' || l == 1)) ret = TRUE;
187 gint pan_is_ignored(const gchar *s, gint ignore_symlinks)
192 if (!lstat_utf8(s, &st)) return TRUE;
195 /* normal filesystems have directories with some size or block allocation,
196 * special filesystems (like linux /proc) set both to zero.
197 * enable this check if you enable listing the root "/" folder
199 if (st.st_size == 0 && st.st_blocks == 0) return TRUE;
202 if (S_ISLNK(st.st_mode) && (ignore_symlinks || pan_is_link_loop(s))) return TRUE;
204 n = filename_from_path(s);
205 if (n && strcmp(n, GQVIEW_RC_DIR) == 0) return TRUE;
210 GList *pan_list_tree(const gchar *path, SortType sort, gint ascend,
211 gint ignore_symlinks)
218 filelist_read(path, &flist, &dlist);
219 if (sort != SORT_NONE)
221 flist = filelist_sort(flist, sort, ascend);
222 dlist = filelist_sort(dlist, sort, ascend);
232 folders = g_list_remove(folders, fd);
234 if (!pan_is_ignored(fd->path, ignore_symlinks) &&
235 filelist_read(fd->path, &flist, &dlist))
237 if (sort != SORT_NONE)
239 flist = filelist_sort(flist, sort, ascend);
240 dlist = filelist_sort(dlist, sort, ascend);
243 result = g_list_concat(result, flist);
244 folders = g_list_concat(dlist, folders);