2 * (SLIK) SimpLIstic sKin functions
4 * Copyright (C) 2008 - 2010 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 */
36 *-----------------------------------------------------------------------------
37 * generic file information and manipulation routines (public)
38 *-----------------------------------------------------------------------------
43 void print_term(const gchar *text_utf8)
47 text_l = g_locale_from_utf8(text_utf8, -1, NULL, NULL, NULL);
48 fputs((text_l) ? text_l : text_utf8, stderr);
52 static void encoding_dialog(const gchar *path);
54 static gboolean encoding_dialog_idle(gpointer data)
58 encoding_dialog(path);
64 static gint encoding_dialog_delay(gpointer data)
66 g_idle_add(encoding_dialog_idle, data);
71 static void encoding_dialog(const gchar *path)
73 static gboolean warned_user = FALSE;
79 /* check that gtk is initialized (loop is level > 0) */
80 if (gtk_main_level() == 0)
82 /* gtk not initialized */
83 gtk_init_add(encoding_dialog_delay, g_strdup(path));
87 if (warned_user) return;
91 bf = getenv("G_BROKEN_FILENAMES");
93 string = g_string_new("");
94 g_string_append(string, _("One or more filenames are not encoded with the preferred locale character set.\n"));
95 g_string_append_printf(string, _("Operations on, and display of these files with %s may not succeed.\n"), PACKAGE);
96 g_string_append(string, "\n");
97 g_string_append(string, _("If your filenames are not encoded in utf-8, try setting the environment variable G_BROKEN_FILENAMES=1\n"));
99 g_string_append_printf(string, _("It appears G_BROKEN_FILENAMES is set to %s\n"), bf);
101 g_string_append(string, _("It appears G_BROKEN_FILENAMES is not set\n"));
102 g_string_append(string, "\n");
103 g_string_append_printf(string, _("The locale appears to be set to \"%s\"\n(set by the LANG environment variable)\n"), (lc) ? lc : "undefined");
104 if (lc && (strstr(lc, "UTF-8") || strstr(lc, "utf-8")))
107 name = g_convert(path, -1, "UTF-8", "ISO-8859-1", NULL, NULL, NULL);
108 string = g_string_append(string, _("\nPreferred encoding appears to be UTF-8, however the file:\n"));
109 g_string_append_printf(string, "\"%s\"\n", (name) ? name : _("[name not displayable]"));
111 if (g_utf8_validate(path, -1, NULL))
112 g_string_append_printf(string, _("\"%s\" is encoded in valid UTF-8."), (name) ? name : _("[name not displayable]"));
114 g_string_append_printf(string, _("\"%s\" is not encoded in valid UTF-8."), (name) ? name : _("[name not displayable]"));
115 g_string_append(string, "\n");
119 gd = generic_dialog_new(_("Filename encoding locale mismatch"),
120 "locale warning", NULL, TRUE, NULL, NULL);
121 generic_dialog_add_button(gd, GTK_STOCK_CLOSE, NULL, NULL, TRUE);
123 generic_dialog_add_message(gd, GTK_STOCK_DIALOG_WARNING,
124 _("Filename encoding locale mismatch"), string->str);
126 gtk_widget_show(gd->dialog);
128 g_string_free(string, TRUE);
131 #if GQ_DEBUG_PATH_UTF8
132 gchar *path_to_utf8_debug(const gchar *path, const gchar *file, gint line)
134 gchar *path_to_utf8(const gchar *path)
138 GError *error = NULL;
140 if (!path) return NULL;
142 utf8 = g_filename_to_utf8(path, -1, NULL, NULL, &error);
145 #if GQ_DEBUG_PATH_UTF8
146 log_printf("%s:%d: Unable to convert filename to UTF-8:\n%s\n%s\n", file, line, path, error->message);
148 log_printf("Unable to convert filename to UTF-8:\n%s\n%s\n", path, error->message);
151 encoding_dialog(path);
155 /* just let it through, but bad things may happen */
156 utf8 = g_strdup(path);
162 #if GQ_DEBUG_PATH_UTF8
163 gchar *path_from_utf8_debug(const gchar *utf8, const gchar *file, gint line)
165 gchar *path_from_utf8(const gchar *utf8)
169 GError *error = NULL;
171 if (!utf8) return NULL;
173 path = g_filename_from_utf8(utf8, -1, NULL, NULL, &error);
176 #if GQ_DEBUG_PATH_UTF8
177 log_printf("%s:%d: Unable to convert filename to locale from UTF-8:\n%s\n%s\n", file, line, utf8, error->message);
179 log_printf("Unable to convert filename to locale from UTF-8:\n%s\n%s\n", utf8, error->message);
185 /* if invalid UTF-8, text probaby still in original form, so just copy it */
186 path = g_strdup(utf8);
192 /* first we try the HOME environment var, if that doesn't work, we try g_get_homedir(). */
193 const gchar *homedir(void)
195 static gchar *home = NULL;
198 home = path_to_utf8(getenv("HOME"));
201 home = path_to_utf8(g_get_home_dir());
206 static gchar *xdg_dir_get(const gchar *key, const gchar *fallback)
208 gchar *dir = getenv(key);
210 if (!dir || dir[0] == '\0')
212 return g_build_filename(homedir(), fallback, NULL);
215 return path_to_utf8(dir);
218 const gchar *xdg_data_home_get(void)
220 static const gchar *xdg_data_home = NULL;
222 if (xdg_data_home) return xdg_data_home;
224 xdg_data_home = xdg_dir_get("XDG_DATA_HOME", ".local/share");
226 return xdg_data_home;
229 const gchar *xdg_config_home_get(void)
231 static const gchar *xdg_config_home = NULL;
233 if (xdg_config_home) return xdg_config_home;
235 xdg_config_home = xdg_dir_get("XDG_CONFIG_HOME", ".config");
237 return xdg_config_home;
240 const gchar *xdg_cache_home_get(void)
242 static const gchar *xdg_cache_home = NULL;
244 if (xdg_cache_home) return xdg_cache_home;
246 xdg_cache_home = xdg_dir_get("XDG_CACHE_HOME", ".cache");
248 return xdg_cache_home;
251 const gchar *get_rc_dir(void)
253 static gchar *rc_dir = NULL;
255 if (rc_dir) return rc_dir;
259 rc_dir = g_build_filename(xdg_config_home_get(), GQ_APPNAME_LC, NULL);
263 rc_dir = g_build_filename(homedir(), GQ_RC_DIR, NULL);
269 const gchar *get_collections_dir(void)
271 static gchar *collections_dir = NULL;
273 if (collections_dir) return collections_dir;
277 collections_dir = g_build_filename(xdg_data_home_get(), GQ_APPNAME_LC, GQ_COLLECTIONS_DIR, NULL);
281 collections_dir = g_build_filename(get_rc_dir(), GQ_COLLECTIONS_DIR, NULL);
284 return collections_dir;
287 const gchar *get_trash_dir(void)
289 static gchar *trash_dir = NULL;
291 if (trash_dir) return trash_dir;
295 trash_dir = g_build_filename(xdg_data_home_get(), GQ_APPNAME_LC, GQ_TRASH_DIR, NULL);
299 trash_dir = g_build_filename(get_rc_dir(), GQ_TRASH_DIR, NULL);
305 gboolean stat_utf8(const gchar *s, struct stat *st)
310 if (!s) return FALSE;
311 sl = path_from_utf8(s);
312 ret = (stat(sl, st) == 0);
318 gboolean lstat_utf8(const gchar *s, struct stat *st)
323 if (!s) return FALSE;
324 sl = path_from_utf8(s);
325 ret = (lstat(sl, st) == 0);
331 gboolean isname(const gchar *s)
335 return stat_utf8(s, &st);
338 gboolean isfile(const gchar *s)
342 return (stat_utf8(s, &st) && S_ISREG(st.st_mode));
345 gboolean isdir(const gchar *s)
349 return (stat_utf8(s, &st) && S_ISDIR(st.st_mode));
352 gboolean islink(const gchar *s)
356 return (lstat_utf8(s, &st) && S_ISLNK(st.st_mode));
359 gint64 filesize(const gchar *s)
363 if (!stat_utf8(s, &st)) return 0;
367 time_t filetime(const gchar *s)
371 if (!stat_utf8(s, &st)) return 0;
375 gboolean filetime_set(const gchar *s, time_t tval)
377 gboolean ret = FALSE;
384 ut.actime = ut.modtime = tval;
386 sl = path_from_utf8(s);
387 ret = (utime(sl, &ut) == 0);
394 gboolean is_readable_file(const gchar *s)
396 if (!s || !s[0] || !isfile(s)) return FALSE;
397 return access_file(s, R_OK);
400 gboolean access_file(const gchar *s, gint mode)
405 if (!s || !s[0]) return FALSE;
407 sl = path_from_utf8(s);
408 ret = (access(sl, mode) == 0);
414 gboolean unlink_file(const gchar *s)
419 if (!s) return FALSE;
421 sl = path_from_utf8(s);
422 ret = (unlink(sl) == 0);
428 gboolean symlink_utf8(const gchar *source, const gchar *target)
434 if (!source || !target) return FALSE;
436 sl = path_from_utf8(source);
437 tl = path_from_utf8(target);
439 ret = (symlink(sl, tl) == 0);
447 gboolean mkdir_utf8(const gchar *s, gint mode)
452 if (!s) return FALSE;
454 sl = path_from_utf8(s);
455 ret = (mkdir(sl, mode) == 0);
460 gboolean rmdir_utf8(const gchar *s)
465 if (!s) return FALSE;
467 sl = path_from_utf8(s);
468 ret = (rmdir(sl) == 0);
474 gboolean copy_file_attributes(const gchar *s, const gchar *t, gint perms, gint mtime)
478 gboolean ret = FALSE;
480 if (!s || !t) return FALSE;
482 sl = path_from_utf8(s);
483 tl = path_from_utf8(t);
485 if (stat(sl, &st) == 0)
491 /* set the dest file attributes to that of source (ignoring errors) */
493 if (perms && chown(tl, st.st_uid, st.st_gid) < 0) ret = FALSE;
494 if (perms && chmod(tl, st.st_mode) < 0) ret = FALSE;
496 tb.actime = st.st_atime;
497 tb.modtime = st.st_mtime;
498 if (mtime && utime(tl, &tb) < 0) ret = FALSE;
507 /* paths are in filesystem encoding */
508 static gboolean hard_linked(const gchar *a, const gchar *b)
513 if (stat(a, &sta) != 0 || stat(b, &stb) != 0) return FALSE;
515 return (sta.st_dev == stb.st_dev &&
516 sta.st_ino == stb.st_ino);
519 gboolean copy_file(const gchar *s, const gchar *t)
525 gchar *randname = NULL;
531 sl = path_from_utf8(s);
532 tl = path_from_utf8(t);
534 if (hard_linked(sl, tl))
540 fi = fopen(sl, "rb");
543 /* First we write to a temporary file, then we rename it on success,
544 and attributes from original file are copied */
545 randname = g_strconcat(tl, ".tmp_XXXXXX", NULL);
546 if (!randname) goto end;
548 fd = g_mkstemp(randname);
549 if (fd == -1) goto end;
551 fo = fdopen(fd, "wb");
557 while ((b = fread(buf, sizeof(gchar), sizeof(buf), fi)) && b != 0)
559 if (fwrite(buf, sizeof(gchar), b, fo) != b)
566 fclose(fi); fi = NULL;
567 fclose(fo); fo = NULL;
569 if (rename(randname, tl) < 0) {
574 ret = copy_file_attributes(s, t, TRUE, TRUE);
581 if (randname) g_free(randname);
585 gboolean move_file(const gchar *s, const gchar *t)
590 if (!s || !t) return FALSE;
592 sl = path_from_utf8(s);
593 tl = path_from_utf8(t);
594 if (rename(sl, tl) < 0)
596 /* this may have failed because moving a file across filesystems
597 was attempted, so try copy and delete instead */
602 /* err, now we can't delete the source file so return FALSE */
617 gboolean rename_file(const gchar *s, const gchar *t)
622 if (!s || !t) return FALSE;
624 sl = path_from_utf8(s);
625 tl = path_from_utf8(t);
626 ret = (rename(sl, tl) == 0);
633 gchar *get_current_dir(void)
638 pathl = g_get_current_dir();
639 path8 = path_to_utf8(pathl);
645 void string_list_free(GList *list)
647 g_list_foreach(list, (GFunc)g_free, NULL);
651 GList *string_list_copy(const GList *list)
653 GList *new_list = NULL;
654 GList *work = (GList *) list;
663 new_list = g_list_prepend(new_list, g_strdup(path));
666 return g_list_reverse(new_list);
669 gchar *unique_filename(const gchar *path, const gchar *ext, const gchar *divider, gboolean pad)
675 if (!divider) divider = "";
677 unique = g_strconcat(path, ext, NULL);
678 while (isname(unique))
683 unique = g_strdup_printf("%s%s%03d%s", path, divider, n, ext);
687 unique = g_strdup_printf("%s%s%d%s", path, divider, n, ext);
701 gchar *unique_filename_simple(const gchar *path)
707 if (!path) return NULL;
709 name = filename_from_path(path);
710 if (!name) return NULL;
712 ext = extension_from_path(name);
716 unique = unique_filename(path, NULL, "_", TRUE);
722 base = remove_extension_from_path(path);
723 unique = unique_filename(base, ext, "_", TRUE);
730 const gchar *filename_from_path(const gchar *path)
734 if (!path) return NULL;
736 base = strrchr(path, G_DIR_SEPARATOR);
737 if (base) return base + 1;
742 gchar *remove_level_from_path(const gchar *path)
746 if (!path) return NULL;
750 if (path[p] == G_DIR_SEPARATOR) n = p;
755 return g_strndup(path, (gsize) n);
758 const gchar *extension_from_path(const gchar *path)
760 if (!path) return NULL;
761 return strrchr(path, '.');
764 gboolean file_extension_match(const gchar *path, const gchar *ext)
769 if (!path) return FALSE;
770 if (!ext) return TRUE;
776 return (p > e && g_ascii_strncasecmp(path + p - e, ext, e) == 0);
779 gchar *remove_extension_from_path(const gchar *path)
783 if (!path) return NULL;
787 if (path[p] == '.') n = p;
792 return g_strndup(path, (gsize) n);
795 void parse_out_relatives(gchar *path)
803 while (path[s] != '\0')
805 if (path[s] == G_DIR_SEPARATOR && path[s+1] == '.')
807 /* /. occurence, let's see more */
810 if (path[p] == G_DIR_SEPARATOR || path[p] == '\0')
812 /* /./ or /., just skip this part */
816 else if (path[p] == '.' && (path[p+1] == G_DIR_SEPARATOR || path[p+1] == '\0'))
818 /* /../ or /.., remove previous part, ie. /a/b/../ becomes /a/ */
821 while (path[t] != G_DIR_SEPARATOR && t > 0) t--;
826 if (s != t) path[t] = path[s];
831 if (t == 0 && path[t] == G_DIR_SEPARATOR) t++;
832 if (t > 1 && path[t-1] == G_DIR_SEPARATOR) t--;
836 gboolean file_in_path(const gchar *name)
841 gboolean ret = FALSE;
843 if (!name) return FALSE;
844 path = g_strdup(getenv("PATH"));
845 if (!path) return FALSE;
846 namel = path_from_utf8(name);
850 while (p < l && !ret)
854 while (path[e] != ':' && path[e] != '\0') e++;
857 f = g_build_filename(path + p, namel, NULL);
858 if (isfile(f)) ret = TRUE;
868 gboolean recursive_mkdir_if_not_exists(const gchar *path, mode_t mode)
870 if (!path) return FALSE;
874 gchar *npath = g_strdup(path);
880 if (p[0] == G_DIR_SEPARATOR || p[0] == '\0')
892 DEBUG_1("creating sub dir:%s", npath);
893 if (!mkdir_utf8(npath, mode))
895 log_printf("create dir failed: %s\n", npath);
901 if (!end) p[0] = G_DIR_SEPARATOR;
910 /* does filename utf8 to filesystem encoding first */
911 gboolean md5_get_digest_from_file_utf8(const gchar *path, guchar digest[16])
916 pathl = path_from_utf8(path);
917 success = md5_get_digest_from_file(pathl, digest);
924 gchar *md5_text_from_file_utf8(const gchar *path, const gchar *error_text)
928 if (!md5_get_digest_from_file_utf8(path, digest)) return g_strdup(error_text);
930 return md5_digest_to_text(digest);
934 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */