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, stdout);
51 static void encoding_dialog(const gchar *path);
53 static gint 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 gint 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 gchar *path_to_utf8(const gchar *path)
133 GError *error = NULL;
135 if (!path) return NULL;
137 utf8 = g_filename_to_utf8(path, -1, NULL, NULL, &error);
140 log_printf("Unable to convert filename to UTF-8:\n%s\n%s\n", path, error->message);
142 encoding_dialog(path);
146 /* just let it through, but bad things may happen */
147 utf8 = g_strdup(path);
153 gchar *path_from_utf8(const gchar *utf8)
156 GError *error = NULL;
158 if (!utf8) return NULL;
160 path = g_filename_from_utf8(utf8, -1, NULL, NULL, &error);
163 log_printf("Unable to convert filename to locale from UTF-8:\n%s\n%s\n", utf8, error->message);
168 /* if invalid UTF-8, text probaby still in original form, so just copy it */
169 path = g_strdup(utf8);
175 /* first we try the HOME environment var, if that doesn't work, we try g_get_homedir(). */
176 const gchar *homedir(void)
178 static gchar *home = NULL;
181 home = path_to_utf8(getenv("HOME"));
184 home = path_to_utf8(g_get_home_dir());
189 static gchar *xdg_dir_get(const gchar *key, const gchar *fallback)
191 gchar *dir = getenv(key);
193 if (!dir || dir[0] == '\0')
195 return g_build_filename(homedir(), fallback, NULL);
198 return path_to_utf8(dir);
201 const gchar *xdg_data_home_get(void)
203 static const gchar *xdg_data_home = NULL;
205 if (xdg_data_home) return xdg_data_home;
207 xdg_data_home = xdg_dir_get("XDG_DATA_HOME", ".local/share");
209 return xdg_data_home;
212 const gchar *xdg_config_home_get(void)
214 static const gchar *xdg_config_home = NULL;
216 if (xdg_config_home) return xdg_config_home;
218 xdg_config_home = xdg_dir_get("XDG_CONFIG_HOME", ".config");
220 return xdg_config_home;
223 const gchar *xdg_cache_home_get(void)
225 static const gchar *xdg_cache_home = NULL;
227 if (xdg_cache_home) return xdg_cache_home;
229 xdg_cache_home = xdg_dir_get("XDG_CACHE_HOME", ".cache");
231 return xdg_cache_home;
234 const gchar *get_rc_dir(void)
236 static gchar *rc_dir = NULL;
238 if (rc_dir) return rc_dir;
242 rc_dir = g_build_filename(xdg_config_home_get(), GQ_APPNAME_LC, NULL);
246 rc_dir = g_build_filename(homedir(), GQ_RC_DIR, NULL);
252 const gchar *get_collections_dir(void)
254 static gchar *collections_dir = NULL;
256 if (collections_dir) return collections_dir;
260 collections_dir = g_build_filename(xdg_data_home_get(), GQ_APPNAME_LC, GQ_COLLECTIONS_DIR, NULL);
264 collections_dir = g_build_filename(get_rc_dir(), GQ_COLLECTIONS_DIR, NULL);
267 return collections_dir;
270 const gchar *get_trash_dir(void)
272 static gchar *trash_dir = NULL;
274 if (trash_dir) return trash_dir;
278 trash_dir = g_build_filename(xdg_data_home_get(), GQ_APPNAME_LC, GQ_TRASH_DIR, NULL);
282 trash_dir = g_build_filename(get_rc_dir(), GQ_TRASH_DIR, NULL);
288 gint stat_utf8(const gchar *s, struct stat *st)
293 if (!s) return FALSE;
294 sl = path_from_utf8(s);
295 ret = (stat(sl, st) == 0);
301 gint lstat_utf8(const gchar *s, struct stat *st)
306 if (!s) return FALSE;
307 sl = path_from_utf8(s);
308 ret = (lstat(sl, st) == 0);
314 /* extension must contain only ASCII characters */
315 gint stat_utf8_case_insensitive_ext(GString *base, const gchar *ext, struct stat *st)
321 gint base_len = strlen(base->str);
323 g_string_append(base, ext);
324 sl = path_from_utf8(base->str);
326 extl = strrchr(sl, '.');
330 extl++; /* the first char after . */
331 ext_len = strlen(extl);
333 for (i = 0; i < (1 << ext_len); i++)
335 for (j = 0; j < ext_len; j++)
338 extl[j] = g_ascii_toupper(extl[j]);
340 extl[j] = g_ascii_tolower(extl[j]);
342 ret = (stat(sl, st) == 0);
348 /* append the found extension to base */
349 base = g_string_truncate(base, base_len);
351 g_string_append(base, extl);
359 gint isname(const gchar *s)
363 return stat_utf8(s, &st);
366 gint isfile(const gchar *s)
370 return (stat_utf8(s, &st) && S_ISREG(st.st_mode));
373 gint isdir(const gchar *s)
377 return (stat_utf8(s, &st) && S_ISDIR(st.st_mode));
380 gint islink(const gchar *s)
384 return (lstat_utf8(s, &st) && S_ISLNK(st.st_mode));
387 gint64 filesize(const gchar *s)
391 if (!stat_utf8(s, &st)) return 0;
395 time_t filetime(const gchar *s)
399 if (!stat_utf8(s, &st)) return 0;
403 gint filetime_set(const gchar *s, time_t tval)
412 ut.actime = ut.modtime = tval;
414 sl = path_from_utf8(s);
415 ret = (utime(sl, &ut) == 0);
422 gint access_file(const gchar *s, gint mode)
427 if (!s) return FALSE;
429 sl = path_from_utf8(s);
430 ret = (access(sl, mode) == 0);
436 gint unlink_file(const gchar *s)
441 if (!s) return FALSE;
443 sl = path_from_utf8(s);
444 ret = (unlink(sl) == 0);
450 gint symlink_utf8(const gchar *source, const gchar *target)
456 if (!source || !target) return FALSE;
458 sl = path_from_utf8(source);
459 tl = path_from_utf8(target);
461 ret = (symlink(sl, tl) == 0);
469 gint mkdir_utf8(const gchar *s, gint mode)
474 if (!s) return FALSE;
476 sl = path_from_utf8(s);
477 ret = (mkdir(sl, mode) == 0);
482 gint rmdir_utf8(const gchar *s)
487 if (!s) return FALSE;
489 sl = path_from_utf8(s);
490 ret = (rmdir(sl) == 0);
496 gint copy_file_attributes(const gchar *s, const gchar *t, gint perms, gint mtime)
502 if (!s || !t) return FALSE;
504 sl = path_from_utf8(s);
505 tl = path_from_utf8(t);
507 if (stat(sl, &st) == 0)
513 /* set the dest file attributes to that of source (ignoring errors) */
515 if (perms && chown(tl, st.st_uid, st.st_gid) < 0) ret = FALSE;
516 if (perms && chmod(tl, st.st_mode) < 0) ret = FALSE;
518 tb.actime = st.st_atime;
519 tb.modtime = st.st_mtime;
520 if (mtime && utime(tl, &tb) < 0) ret = FALSE;
529 /* paths are in filesystem encoding */
530 static gint hard_linked(const gchar *a, const gchar *b)
535 if (stat(a, &sta) != 0 || stat(b, &stb) != 0) return FALSE;
537 return (sta.st_dev == stb.st_dev &&
538 sta.st_ino == stb.st_ino);
541 gint copy_file(const gchar *s, const gchar *t)
549 sl = path_from_utf8(s);
550 tl = path_from_utf8(t);
552 if (hard_linked(sl, tl))
559 fi = fopen(sl, "rb");
562 fo = fopen(tl, "wb");
573 if (!fi || !fo) return FALSE;
575 while ((b = fread(buf, sizeof(gchar), sizeof(buf), fi)) && b != 0)
577 if (fwrite(buf, sizeof(gchar), b, fo) != b)
588 copy_file_attributes(s, t, TRUE, TRUE);
593 gint move_file(const gchar *s, const gchar *t)
598 if (!s || !t) return FALSE;
600 sl = path_from_utf8(s);
601 tl = path_from_utf8(t);
602 if (rename(sl, tl) < 0)
604 /* this may have failed because moving a file across filesystems
605 was attempted, so try copy and delete instead */
610 /* err, now we can't delete the source file so return FALSE */
625 gint rename_file(const gchar *s, const gchar *t)
630 if (!s || !t) return FALSE;
632 sl = path_from_utf8(s);
633 tl = path_from_utf8(t);
634 ret = (rename(sl, tl) == 0);
641 gchar *get_current_dir(void)
646 pathl = g_get_current_dir();
647 path8 = path_to_utf8(pathl);
653 void string_list_free(GList *list)
655 g_list_foreach(list, (GFunc)g_free, NULL);
659 GList *string_list_copy(const GList *list)
661 GList *new_list = NULL;
662 GList *work = (GList *) list;
671 new_list = g_list_prepend(new_list, g_strdup(path));
674 return g_list_reverse(new_list);
677 gchar *unique_filename(const gchar *path, const gchar *ext, const gchar *divider, gint pad)
683 if (!divider) divider = "";
685 unique = g_strconcat(path, ext, NULL);
686 while (isname(unique))
691 unique = g_strdup_printf("%s%s%03d%s", path, divider, n, ext);
695 unique = g_strdup_printf("%s%s%d%s", path, divider, n, ext);
709 gchar *unique_filename_simple(const gchar *path)
715 if (!path) return NULL;
717 name = filename_from_path(path);
718 if (!name) return NULL;
720 ext = extension_from_path(name);
724 unique = unique_filename(path, NULL, "_", TRUE);
730 base = remove_extension_from_path(path);
731 unique = unique_filename(base, ext, "_", TRUE);
738 const gchar *filename_from_path(const gchar *path)
742 if (!path) return NULL;
744 base = strrchr(path, G_DIR_SEPARATOR);
745 if (base) return base + 1;
750 gchar *remove_level_from_path(const gchar *path)
754 if (!path) return NULL;
758 if (path[p] == G_DIR_SEPARATOR) n = p;
763 return g_strndup(path, (gsize) n);
766 const gchar *extension_from_path(const gchar *path)
768 if (!path) return NULL;
769 return strrchr(path, '.');
772 gint file_extension_match(const gchar *path, const gchar *ext)
777 if (!path) return FALSE;
778 if (!ext) return TRUE;
784 return (p > e && strncasecmp(path + p - e, ext, e) == 0);
787 gchar *remove_extension_from_path(const gchar *path)
791 if (!path) return NULL;
795 if (path[p] == '.') n = p;
800 return g_strndup(path, (gsize) n);
803 void parse_out_relatives(gchar *path)
811 while (path[s] != '\0')
813 if (path[s] == G_DIR_SEPARATOR && path[s+1] == '.')
815 /* /. occurence, let's see more */
818 if (path[p] == G_DIR_SEPARATOR || path[p] == '\0')
820 /* /./ or /., just skip this part */
824 else if (path[p] == '.' && (path[p+1] == G_DIR_SEPARATOR || path[p+1] == '\0'))
826 /* /../ or /.., remove previous part, ie. /a/b/../ becomes /a/ */
829 while (path[t] != G_DIR_SEPARATOR && t > 0) t--;
834 if (s != t) path[t] = path[s];
839 if (t == 0 && path[t] == G_DIR_SEPARATOR) t++;
840 if (t > 1 && path[t-1] == G_DIR_SEPARATOR) t--;
844 gint file_in_path(const gchar *name)
851 if (!name) return FALSE;
852 path = g_strdup(getenv("PATH"));
853 if (!path) return FALSE;
854 namel = path_from_utf8(name);
858 while (p < l && !ret)
862 while (path[e] != ':' && path[e] != '\0') e++;
865 f = g_build_filename(path + p, namel, NULL);
866 if (isfile(f)) ret = TRUE;
876 gboolean recursive_mkdir_if_not_exists(const gchar *path, mode_t mode)
878 if (!path) return FALSE;
882 gchar *npath = g_strdup(path);
888 if (p[0] == G_DIR_SEPARATOR || p[0] == '\0')
900 DEBUG_1("creating sub dir:%s", npath);
901 if (!mkdir_utf8(npath, mode))
903 log_printf("create dir failed: %s\n", npath);
909 if (!end) p[0] = G_DIR_SEPARATOR;
920 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */