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 */
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 /* extension must contain only ASCII characters */
332 gboolean stat_utf8_case_insensitive_ext(GString *base, const gchar *ext, struct stat *st)
336 gboolean ret = FALSE;
338 gint base_len = strlen(base->str);
340 g_string_append(base, ext);
341 sl = path_from_utf8(base->str);
343 extl = strrchr(sl, '.');
347 extl++; /* the first char after . */
348 ext_len = strlen(extl);
350 for (i = 0; i < (1 << ext_len); i++)
352 for (j = 0; j < ext_len; j++)
355 extl[j] = g_ascii_toupper(extl[j]);
357 extl[j] = g_ascii_tolower(extl[j]);
359 ret = (stat(sl, st) == 0);
365 /* append the found extension to base */
366 base = g_string_truncate(base, base_len);
368 g_string_append(base, extl);
376 gboolean isname(const gchar *s)
380 return stat_utf8(s, &st);
383 gboolean isfile(const gchar *s)
387 return (stat_utf8(s, &st) && S_ISREG(st.st_mode));
390 gboolean isdir(const gchar *s)
394 return (stat_utf8(s, &st) && S_ISDIR(st.st_mode));
397 gboolean islink(const gchar *s)
401 return (lstat_utf8(s, &st) && S_ISLNK(st.st_mode));
404 gint64 filesize(const gchar *s)
408 if (!stat_utf8(s, &st)) return 0;
412 time_t filetime(const gchar *s)
416 if (!stat_utf8(s, &st)) return 0;
420 gboolean filetime_set(const gchar *s, time_t tval)
422 gboolean ret = FALSE;
429 ut.actime = ut.modtime = tval;
431 sl = path_from_utf8(s);
432 ret = (utime(sl, &ut) == 0);
439 gboolean is_readable_file(const gchar *s)
441 if (!s || !s[0] || !isfile(s)) return FALSE;
442 return access_file(s, R_OK);
445 gboolean access_file(const gchar *s, gint mode)
450 if (!s || !s[0]) return FALSE;
452 sl = path_from_utf8(s);
453 ret = (access(sl, mode) == 0);
459 gboolean unlink_file(const gchar *s)
464 if (!s) return FALSE;
466 sl = path_from_utf8(s);
467 ret = (unlink(sl) == 0);
473 gboolean symlink_utf8(const gchar *source, const gchar *target)
479 if (!source || !target) return FALSE;
481 sl = path_from_utf8(source);
482 tl = path_from_utf8(target);
484 ret = (symlink(sl, tl) == 0);
492 gboolean mkdir_utf8(const gchar *s, gint mode)
497 if (!s) return FALSE;
499 sl = path_from_utf8(s);
500 ret = (mkdir(sl, mode) == 0);
505 gboolean rmdir_utf8(const gchar *s)
510 if (!s) return FALSE;
512 sl = path_from_utf8(s);
513 ret = (rmdir(sl) == 0);
519 gboolean copy_file_attributes(const gchar *s, const gchar *t, gint perms, gint mtime)
523 gboolean ret = FALSE;
525 if (!s || !t) return FALSE;
527 sl = path_from_utf8(s);
528 tl = path_from_utf8(t);
530 if (stat(sl, &st) == 0)
536 /* set the dest file attributes to that of source (ignoring errors) */
538 if (perms && chown(tl, st.st_uid, st.st_gid) < 0) ret = FALSE;
539 if (perms && chmod(tl, st.st_mode) < 0) ret = FALSE;
541 tb.actime = st.st_atime;
542 tb.modtime = st.st_mtime;
543 if (mtime && utime(tl, &tb) < 0) ret = FALSE;
552 /* paths are in filesystem encoding */
553 static gboolean hard_linked(const gchar *a, const gchar *b)
558 if (stat(a, &sta) != 0 || stat(b, &stb) != 0) return FALSE;
560 return (sta.st_dev == stb.st_dev &&
561 sta.st_ino == stb.st_ino);
564 gboolean copy_file(const gchar *s, const gchar *t)
572 sl = path_from_utf8(s);
573 tl = path_from_utf8(t);
575 if (hard_linked(sl, tl))
582 fi = fopen(sl, "rb");
585 fo = fopen(tl, "wb");
596 if (!fi || !fo) return FALSE;
598 while ((b = fread(buf, sizeof(gchar), sizeof(buf), fi)) && b != 0)
600 if (fwrite(buf, sizeof(gchar), b, fo) != b)
611 copy_file_attributes(s, t, TRUE, TRUE);
616 gboolean move_file(const gchar *s, const gchar *t)
621 if (!s || !t) return FALSE;
623 sl = path_from_utf8(s);
624 tl = path_from_utf8(t);
625 if (rename(sl, tl) < 0)
627 /* this may have failed because moving a file across filesystems
628 was attempted, so try copy and delete instead */
633 /* err, now we can't delete the source file so return FALSE */
648 gboolean rename_file(const gchar *s, const gchar *t)
653 if (!s || !t) return FALSE;
655 sl = path_from_utf8(s);
656 tl = path_from_utf8(t);
657 ret = (rename(sl, tl) == 0);
664 gchar *get_current_dir(void)
669 pathl = g_get_current_dir();
670 path8 = path_to_utf8(pathl);
676 void string_list_free(GList *list)
678 g_list_foreach(list, (GFunc)g_free, NULL);
682 GList *string_list_copy(const GList *list)
684 GList *new_list = NULL;
685 GList *work = (GList *) list;
694 new_list = g_list_prepend(new_list, g_strdup(path));
697 return g_list_reverse(new_list);
700 gchar *unique_filename(const gchar *path, const gchar *ext, const gchar *divider, gboolean pad)
706 if (!divider) divider = "";
708 unique = g_strconcat(path, ext, NULL);
709 while (isname(unique))
714 unique = g_strdup_printf("%s%s%03d%s", path, divider, n, ext);
718 unique = g_strdup_printf("%s%s%d%s", path, divider, n, ext);
732 gchar *unique_filename_simple(const gchar *path)
738 if (!path) return NULL;
740 name = filename_from_path(path);
741 if (!name) return NULL;
743 ext = extension_from_path(name);
747 unique = unique_filename(path, NULL, "_", TRUE);
753 base = remove_extension_from_path(path);
754 unique = unique_filename(base, ext, "_", TRUE);
761 const gchar *filename_from_path(const gchar *path)
765 if (!path) return NULL;
767 base = strrchr(path, G_DIR_SEPARATOR);
768 if (base) return base + 1;
773 gchar *remove_level_from_path(const gchar *path)
777 if (!path) return NULL;
781 if (path[p] == G_DIR_SEPARATOR) n = p;
786 return g_strndup(path, (gsize) n);
789 const gchar *extension_from_path(const gchar *path)
791 if (!path) return NULL;
792 return strrchr(path, '.');
795 gboolean file_extension_match(const gchar *path, const gchar *ext)
800 if (!path) return FALSE;
801 if (!ext) return TRUE;
807 return (p > e && g_ascii_strncasecmp(path + p - e, ext, e) == 0);
810 gchar *remove_extension_from_path(const gchar *path)
814 if (!path) return NULL;
818 if (path[p] == '.') n = p;
823 return g_strndup(path, (gsize) n);
826 void parse_out_relatives(gchar *path)
834 while (path[s] != '\0')
836 if (path[s] == G_DIR_SEPARATOR && path[s+1] == '.')
838 /* /. occurence, let's see more */
841 if (path[p] == G_DIR_SEPARATOR || path[p] == '\0')
843 /* /./ or /., just skip this part */
847 else if (path[p] == '.' && (path[p+1] == G_DIR_SEPARATOR || path[p+1] == '\0'))
849 /* /../ or /.., remove previous part, ie. /a/b/../ becomes /a/ */
852 while (path[t] != G_DIR_SEPARATOR && t > 0) t--;
857 if (s != t) path[t] = path[s];
862 if (t == 0 && path[t] == G_DIR_SEPARATOR) t++;
863 if (t > 1 && path[t-1] == G_DIR_SEPARATOR) t--;
867 gboolean file_in_path(const gchar *name)
872 gboolean ret = FALSE;
874 if (!name) return FALSE;
875 path = g_strdup(getenv("PATH"));
876 if (!path) return FALSE;
877 namel = path_from_utf8(name);
881 while (p < l && !ret)
885 while (path[e] != ':' && path[e] != '\0') e++;
888 f = g_build_filename(path + p, namel, NULL);
889 if (isfile(f)) ret = TRUE;
899 gboolean recursive_mkdir_if_not_exists(const gchar *path, mode_t mode)
901 if (!path) return FALSE;
905 gchar *npath = g_strdup(path);
911 if (p[0] == G_DIR_SEPARATOR || p[0] == '\0')
923 DEBUG_1("creating sub dir:%s", npath);
924 if (!mkdir_utf8(npath, mode))
926 log_printf("create dir failed: %s\n", npath);
932 if (!end) p[0] = G_DIR_SEPARATOR;
941 /* does filename utf8 to filesystem encoding first */
942 gboolean md5_get_digest_from_file_utf8(const gchar *path, guchar digest[16])
947 pathl = path_from_utf8(path);
948 success = md5_get_digest_from_file(pathl, digest);
955 gchar *md5_text_from_file_utf8(const gchar *path, const gchar *error_text)
959 if (!md5_get_digest_from_file_utf8(path, digest)) return g_strdup(error_text);
961 return md5_digest_to_text(digest);
965 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */