2 * (SLIK) SimpLIstic sKin functions
4 * Copyright (C) 2008 - 2009 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!
22 #include <sys/param.h>
27 #include <gtk/gtk.h> /* for locale warning dialog */
30 #include "ui_fileops.h"
32 #include "ui_utildlg.h" /* for locale warning dialog */
35 *-----------------------------------------------------------------------------
36 * generic file information and manipulation routines (public)
37 *-----------------------------------------------------------------------------
42 void print_term(const gchar *text_utf8)
46 text_l = g_locale_from_utf8(text_utf8, -1, NULL, NULL, NULL);
47 fputs((text_l) ? text_l : text_utf8, stderr);
51 static void encoding_dialog(const gchar *path);
53 static gboolean encoding_dialog_idle(gpointer data)
57 encoding_dialog(path);
63 static gint encoding_dialog_delay(gpointer data)
65 g_idle_add(encoding_dialog_idle, data);
70 static void encoding_dialog(const gchar *path)
72 static gboolean warned_user = FALSE;
78 /* check that gtk is initialized (loop is level > 0) */
79 if (gtk_main_level() == 0)
81 /* gtk not initialized */
82 gtk_init_add(encoding_dialog_delay, g_strdup(path));
86 if (warned_user) return;
90 bf = getenv("G_BROKEN_FILENAMES");
92 string = g_string_new("");
93 g_string_append(string, _("One or more filenames are not encoded with the preferred locale character set.\n"));
94 g_string_append_printf(string, _("Operations on, and display of these files with %s may not succeed.\n"), PACKAGE);
95 g_string_append(string, "\n");
96 g_string_append(string, _("If your filenames are not encoded in utf-8, try setting the environment variable G_BROKEN_FILENAMES=1\n"));
98 g_string_append_printf(string, _("It appears G_BROKEN_FILENAMES is set to %s\n"), bf);
100 g_string_append(string, _("It appears G_BROKEN_FILENAMES is not set\n"));
101 g_string_append(string, "\n");
102 g_string_append_printf(string, _("The locale appears to be set to \"%s\"\n(set by the LANG environment variable)\n"), (lc) ? lc : "undefined");
103 if (lc && (strstr(lc, "UTF-8") || strstr(lc, "utf-8")))
106 name = g_convert(path, -1, "UTF-8", "ISO-8859-1", NULL, NULL, NULL);
107 string = g_string_append(string, _("\nPreferred encoding appears to be UTF-8, however the file:\n"));
108 g_string_append_printf(string, "\"%s\"\n", (name) ? name : _("[name not displayable]"));
110 if (g_utf8_validate(path, -1, NULL))
111 g_string_append_printf(string, _("\"%s\" is encoded in valid UTF-8."), (name) ? name : _("[name not displayable]"));
113 g_string_append_printf(string, _("\"%s\" is not encoded in valid UTF-8."), (name) ? name : _("[name not displayable]"));
114 g_string_append(string, "\n");
118 gd = generic_dialog_new(_("Filename encoding locale mismatch"),
119 "locale warning", NULL, TRUE, NULL, NULL);
120 generic_dialog_add_button(gd, GTK_STOCK_CLOSE, NULL, NULL, TRUE);
122 generic_dialog_add_message(gd, GTK_STOCK_DIALOG_WARNING,
123 _("Filename encoding locale mismatch"), string->str);
125 gtk_widget_show(gd->dialog);
127 g_string_free(string, TRUE);
130 #if GQ_DEBUG_PATH_UTF8
131 gchar *path_to_utf8_debug(const gchar *path, const gchar *file, gint line)
133 gchar *path_to_utf8(const gchar *path)
137 GError *error = NULL;
139 if (!path) return NULL;
141 utf8 = g_filename_to_utf8(path, -1, NULL, NULL, &error);
144 #if GQ_DEBUG_PATH_UTF8
145 log_printf("%s:%d: Unable to convert filename to UTF-8:\n%s\n%s\n", file, line, path, error->message);
147 log_printf("Unable to convert filename to UTF-8:\n%s\n%s\n", path, error->message);
150 encoding_dialog(path);
154 /* just let it through, but bad things may happen */
155 utf8 = g_strdup(path);
161 #if GQ_DEBUG_PATH_UTF8
162 gchar *path_from_utf8_debug(const gchar *utf8, const gchar *file, gint line)
164 gchar *path_from_utf8(const gchar *utf8)
168 GError *error = NULL;
170 if (!utf8) return NULL;
172 path = g_filename_from_utf8(utf8, -1, NULL, NULL, &error);
175 #if GQ_DEBUG_PATH_UTF8
176 log_printf("%s:%d: Unable to convert filename to locale from UTF-8:\n%s\n%s\n", file, line, utf8, error->message);
178 log_printf("Unable to convert filename to locale from UTF-8:\n%s\n%s\n", utf8, error->message);
184 /* if invalid UTF-8, text probaby still in original form, so just copy it */
185 path = g_strdup(utf8);
191 /* first we try the HOME environment var, if that doesn't work, we try g_get_homedir(). */
192 const gchar *homedir(void)
194 static gchar *home = NULL;
197 home = path_to_utf8(getenv("HOME"));
200 home = path_to_utf8(g_get_home_dir());
205 static gchar *xdg_dir_get(const gchar *key, const gchar *fallback)
207 gchar *dir = getenv(key);
209 if (!dir || dir[0] == '\0')
211 return g_build_filename(homedir(), fallback, NULL);
214 return path_to_utf8(dir);
217 const gchar *xdg_data_home_get(void)
219 static const gchar *xdg_data_home = NULL;
221 if (xdg_data_home) return xdg_data_home;
223 xdg_data_home = xdg_dir_get("XDG_DATA_HOME", ".local/share");
225 return xdg_data_home;
228 const gchar *xdg_config_home_get(void)
230 static const gchar *xdg_config_home = NULL;
232 if (xdg_config_home) return xdg_config_home;
234 xdg_config_home = xdg_dir_get("XDG_CONFIG_HOME", ".config");
236 return xdg_config_home;
239 const gchar *xdg_cache_home_get(void)
241 static const gchar *xdg_cache_home = NULL;
243 if (xdg_cache_home) return xdg_cache_home;
245 xdg_cache_home = xdg_dir_get("XDG_CACHE_HOME", ".cache");
247 return xdg_cache_home;
250 const gchar *get_rc_dir(void)
252 static gchar *rc_dir = NULL;
254 if (rc_dir) return rc_dir;
258 rc_dir = g_build_filename(xdg_config_home_get(), GQ_APPNAME_LC, NULL);
262 rc_dir = g_build_filename(homedir(), GQ_RC_DIR, NULL);
268 const gchar *get_collections_dir(void)
270 static gchar *collections_dir = NULL;
272 if (collections_dir) return collections_dir;
276 collections_dir = g_build_filename(xdg_data_home_get(), GQ_APPNAME_LC, GQ_COLLECTIONS_DIR, NULL);
280 collections_dir = g_build_filename(get_rc_dir(), GQ_COLLECTIONS_DIR, NULL);
283 return collections_dir;
286 const gchar *get_trash_dir(void)
288 static gchar *trash_dir = NULL;
290 if (trash_dir) return trash_dir;
294 trash_dir = g_build_filename(xdg_data_home_get(), GQ_APPNAME_LC, GQ_TRASH_DIR, NULL);
298 trash_dir = g_build_filename(get_rc_dir(), GQ_TRASH_DIR, NULL);
304 gboolean stat_utf8(const gchar *s, struct stat *st)
309 if (!s) return FALSE;
310 sl = path_from_utf8(s);
311 ret = (stat(sl, st) == 0);
317 gboolean lstat_utf8(const gchar *s, struct stat *st)
322 if (!s) return FALSE;
323 sl = path_from_utf8(s);
324 ret = (lstat(sl, st) == 0);
330 /* extension must contain only ASCII characters */
331 gboolean stat_utf8_case_insensitive_ext(GString *base, const gchar *ext, struct stat *st)
335 gboolean ret = FALSE;
337 gint base_len = strlen(base->str);
339 g_string_append(base, ext);
340 sl = path_from_utf8(base->str);
342 extl = strrchr(sl, '.');
346 extl++; /* the first char after . */
347 ext_len = strlen(extl);
349 for (i = 0; i < (1 << ext_len); i++)
351 for (j = 0; j < ext_len; j++)
354 extl[j] = g_ascii_toupper(extl[j]);
356 extl[j] = g_ascii_tolower(extl[j]);
358 ret = (stat(sl, st) == 0);
364 /* append the found extension to base */
365 base = g_string_truncate(base, base_len);
367 g_string_append(base, extl);
375 gboolean isname(const gchar *s)
379 return stat_utf8(s, &st);
382 gboolean isfile(const gchar *s)
386 return (stat_utf8(s, &st) && S_ISREG(st.st_mode));
389 gboolean isdir(const gchar *s)
393 return (stat_utf8(s, &st) && S_ISDIR(st.st_mode));
396 gboolean islink(const gchar *s)
400 return (lstat_utf8(s, &st) && S_ISLNK(st.st_mode));
403 gint64 filesize(const gchar *s)
407 if (!stat_utf8(s, &st)) return 0;
411 time_t filetime(const gchar *s)
415 if (!stat_utf8(s, &st)) return 0;
419 gboolean filetime_set(const gchar *s, time_t tval)
421 gboolean ret = FALSE;
428 ut.actime = ut.modtime = tval;
430 sl = path_from_utf8(s);
431 ret = (utime(sl, &ut) == 0);
438 gboolean is_readable_file(const gchar *s)
440 if (!s || !s[0] || !isfile(s)) return FALSE;
441 return access_file(s, R_OK);
444 gboolean access_file(const gchar *s, gint mode)
449 if (!s || !s[0]) return FALSE;
451 sl = path_from_utf8(s);
452 ret = (access(sl, mode) == 0);
458 gboolean unlink_file(const gchar *s)
463 if (!s) return FALSE;
465 sl = path_from_utf8(s);
466 ret = (unlink(sl) == 0);
472 gboolean symlink_utf8(const gchar *source, const gchar *target)
478 if (!source || !target) return FALSE;
480 sl = path_from_utf8(source);
481 tl = path_from_utf8(target);
483 ret = (symlink(sl, tl) == 0);
491 gboolean mkdir_utf8(const gchar *s, gint mode)
496 if (!s) return FALSE;
498 sl = path_from_utf8(s);
499 ret = (mkdir(sl, mode) == 0);
504 gboolean rmdir_utf8(const gchar *s)
509 if (!s) return FALSE;
511 sl = path_from_utf8(s);
512 ret = (rmdir(sl) == 0);
518 gboolean copy_file_attributes(const gchar *s, const gchar *t, gint perms, gint mtime)
522 gboolean ret = FALSE;
524 if (!s || !t) return FALSE;
526 sl = path_from_utf8(s);
527 tl = path_from_utf8(t);
529 if (stat(sl, &st) == 0)
535 /* set the dest file attributes to that of source (ignoring errors) */
537 if (perms && chown(tl, st.st_uid, st.st_gid) < 0) ret = FALSE;
538 if (perms && chmod(tl, st.st_mode) < 0) ret = FALSE;
540 tb.actime = st.st_atime;
541 tb.modtime = st.st_mtime;
542 if (mtime && utime(tl, &tb) < 0) ret = FALSE;
551 /* paths are in filesystem encoding */
552 static gboolean hard_linked(const gchar *a, const gchar *b)
557 if (stat(a, &sta) != 0 || stat(b, &stb) != 0) return FALSE;
559 return (sta.st_dev == stb.st_dev &&
560 sta.st_ino == stb.st_ino);
563 gboolean copy_file(const gchar *s, const gchar *t)
571 sl = path_from_utf8(s);
572 tl = path_from_utf8(t);
574 if (hard_linked(sl, tl))
581 fi = fopen(sl, "rb");
584 fo = fopen(tl, "wb");
595 if (!fi || !fo) return FALSE;
597 while ((b = fread(buf, sizeof(gchar), sizeof(buf), fi)) && b != 0)
599 if (fwrite(buf, sizeof(gchar), b, fo) != b)
610 copy_file_attributes(s, t, TRUE, TRUE);
615 gboolean move_file(const gchar *s, const gchar *t)
620 if (!s || !t) return FALSE;
622 sl = path_from_utf8(s);
623 tl = path_from_utf8(t);
624 if (rename(sl, tl) < 0)
626 /* this may have failed because moving a file across filesystems
627 was attempted, so try copy and delete instead */
632 /* err, now we can't delete the source file so return FALSE */
647 gboolean rename_file(const gchar *s, const gchar *t)
652 if (!s || !t) return FALSE;
654 sl = path_from_utf8(s);
655 tl = path_from_utf8(t);
656 ret = (rename(sl, tl) == 0);
663 gchar *get_current_dir(void)
668 pathl = g_get_current_dir();
669 path8 = path_to_utf8(pathl);
675 void string_list_free(GList *list)
677 g_list_foreach(list, (GFunc)g_free, NULL);
681 GList *string_list_copy(const GList *list)
683 GList *new_list = NULL;
684 GList *work = (GList *) list;
693 new_list = g_list_prepend(new_list, g_strdup(path));
696 return g_list_reverse(new_list);
699 gchar *unique_filename(const gchar *path, const gchar *ext, const gchar *divider, gboolean pad)
705 if (!divider) divider = "";
707 unique = g_strconcat(path, ext, NULL);
708 while (isname(unique))
713 unique = g_strdup_printf("%s%s%03d%s", path, divider, n, ext);
717 unique = g_strdup_printf("%s%s%d%s", path, divider, n, ext);
731 gchar *unique_filename_simple(const gchar *path)
737 if (!path) return NULL;
739 name = filename_from_path(path);
740 if (!name) return NULL;
742 ext = extension_from_path(name);
746 unique = unique_filename(path, NULL, "_", TRUE);
752 base = remove_extension_from_path(path);
753 unique = unique_filename(base, ext, "_", TRUE);
760 const gchar *filename_from_path(const gchar *path)
764 if (!path) return NULL;
766 base = strrchr(path, G_DIR_SEPARATOR);
767 if (base) return base + 1;
772 gchar *remove_level_from_path(const gchar *path)
776 if (!path) return NULL;
780 if (path[p] == G_DIR_SEPARATOR) n = p;
785 return g_strndup(path, (gsize) n);
788 const gchar *extension_from_path(const gchar *path)
790 if (!path) return NULL;
791 return strrchr(path, '.');
794 gboolean file_extension_match(const gchar *path, const gchar *ext)
799 if (!path) return FALSE;
800 if (!ext) return TRUE;
806 return (p > e && g_ascii_strncasecmp(path + p - e, ext, e) == 0);
809 gchar *remove_extension_from_path(const gchar *path)
813 if (!path) return NULL;
817 if (path[p] == '.') n = p;
822 return g_strndup(path, (gsize) n);
825 void parse_out_relatives(gchar *path)
833 while (path[s] != '\0')
835 if (path[s] == G_DIR_SEPARATOR && path[s+1] == '.')
837 /* /. occurence, let's see more */
840 if (path[p] == G_DIR_SEPARATOR || path[p] == '\0')
842 /* /./ or /., just skip this part */
846 else if (path[p] == '.' && (path[p+1] == G_DIR_SEPARATOR || path[p+1] == '\0'))
848 /* /../ or /.., remove previous part, ie. /a/b/../ becomes /a/ */
851 while (path[t] != G_DIR_SEPARATOR && t > 0) t--;
856 if (s != t) path[t] = path[s];
861 if (t == 0 && path[t] == G_DIR_SEPARATOR) t++;
862 if (t > 1 && path[t-1] == G_DIR_SEPARATOR) t--;
866 gboolean file_in_path(const gchar *name)
871 gboolean ret = FALSE;
873 if (!name) return FALSE;
874 path = g_strdup(getenv("PATH"));
875 if (!path) return FALSE;
876 namel = path_from_utf8(name);
880 while (p < l && !ret)
884 while (path[e] != ':' && path[e] != '\0') e++;
887 f = g_build_filename(path + p, namel, NULL);
888 if (isfile(f)) ret = TRUE;
898 gboolean recursive_mkdir_if_not_exists(const gchar *path, mode_t mode)
900 if (!path) return FALSE;
904 gchar *npath = g_strdup(path);
910 if (p[0] == G_DIR_SEPARATOR || p[0] == '\0')
922 DEBUG_1("creating sub dir:%s", npath);
923 if (!mkdir_utf8(npath, mode))
925 log_printf("create dir failed: %s\n", npath);
931 if (!end) p[0] = G_DIR_SEPARATOR;
942 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */