2 * (SLIK) SimpLIstic sKin functions
4 * Copyright (C) 2008 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"
33 #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 printf((text_l) ? text_l : text_utf8);
52 static void encoding_dialog(const gchar *path);
54 static gint 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 gint 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");
94 string = g_string_new("");
95 g_string_append(string, "One or more filenames are not encoded with the preferred locale character set.\n");
96 g_string_append_printf(string, "Operations on, and display of these files with %s may not succeed.\n\n", PACKAGE);
97 g_string_append(string, "If your filenames are not encoded in utf-8, try setting\n");
98 g_string_append(string, "the environment variable G_BROKEN_FILENAMES=1\n");
99 g_string_append_printf(string, "It appears G_BROKEN_FILENAMES is %s%s\n\n",
100 (bf) ? "set to " : "not set.", (bf) ? bf : "");
101 g_string_append_printf(string, "The locale appears to be set to \"%s\"\n(set by the LANG environment variable)\n", (lc) ? lc : "undefined");
102 if (lc && (strstr(lc, "UTF-8") || strstr(lc, "utf-8")))
105 name = g_convert(path, -1, "UTF-8", "ISO-8859-1", NULL, NULL, NULL);
106 string = g_string_append(string, "\nPreferred encoding appears to be UTF-8, however the file:\n");
107 g_string_append_printf(string, "\"%s\"\n%s encoded in valid UTF-8.\n",
108 (name) ? name : "[name not displayable]",
109 (g_utf8_validate(path, -1, NULL)) ? "is": "is NOT");
113 gd = generic_dialog_new("Filename encoding locale mismatch",
114 GQ_WMCLASS, "locale warning", NULL, TRUE, NULL, NULL);
115 generic_dialog_add_button(gd, GTK_STOCK_CLOSE, NULL, NULL, TRUE);
117 generic_dialog_add_message(gd, GTK_STOCK_DIALOG_WARNING,
118 "Filename encoding locale mismatch", string->str);
120 gtk_widget_show(gd->dialog);
122 g_string_free(string, TRUE);
125 gchar *path_to_utf8(const gchar *path)
128 GError *error = NULL;
130 if (!path) return NULL;
132 utf8 = g_filename_to_utf8(path, -1, NULL, NULL, &error);
135 printf("Unable to convert filename to UTF-8:\n%s\n%s\n", path, error->message);
137 encoding_dialog(path);
141 /* just let it through, but bad things may happen */
142 utf8 = g_strdup(path);
148 gchar *path_from_utf8(const gchar *utf8)
151 GError *error = NULL;
153 if (!utf8) return NULL;
155 path = g_filename_from_utf8(utf8, -1, NULL, NULL, &error);
158 printf("Unable to convert filename to locale from UTF-8:\n%s\n%s\n", utf8, error->message);
163 /* if invalid UTF-8, text probaby still in original form, so just copy it */
164 path = g_strdup(utf8);
170 /* first we try the HOME environment var, if that doesn't work, we try getpwuid(). */
171 const gchar *homedir(void)
173 static gchar *home = NULL;
177 home = path_to_utf8(getenv("HOME"));
181 struct passwd *pw = getpwuid(getuid());
182 if (pw) home = path_to_utf8(pw->pw_dir);
188 gint stat_utf8(const gchar *s, struct stat *st)
193 if (!s) return FALSE;
194 sl = path_from_utf8(s);
195 ret = (stat(sl, st) == 0);
201 gint lstat_utf8(const gchar *s, struct stat *st)
206 if (!s) return FALSE;
207 sl = path_from_utf8(s);
208 ret = (lstat(sl, st) == 0);
214 gint isname(const gchar *s)
218 return stat_utf8(s, &st);
221 gint isfile(const gchar *s)
225 return (stat_utf8(s, &st) && S_ISREG(st.st_mode));
228 gint isdir(const gchar *s)
232 return (stat_utf8(s ,&st) && S_ISDIR(st.st_mode));
235 gint islink(const gchar *s)
239 return (lstat_utf8(s ,&st) && S_ISLNK(st.st_mode));
242 gint64 filesize(const gchar *s)
246 if (!stat_utf8(s, &st)) return 0;
247 return (gint)st.st_size;
250 time_t filetime(const gchar *s)
254 if (!stat_utf8(s, &st)) return 0;
258 gint filetime_set(const gchar *s, time_t tval)
267 ut.actime = ut.modtime = tval;
269 sl = path_from_utf8(s);
270 ret = (utime(sl, &ut) == 0);
277 gint access_file(const gchar *s, int mode)
282 if (!s) return FALSE;
284 sl = path_from_utf8(s);
285 ret = (access(sl, mode) == 0);
291 gint unlink_file(const gchar *s)
296 if (!s) return FALSE;
298 sl = path_from_utf8(s);
299 ret = (unlink(sl) == 0);
305 gint symlink_utf8(const gchar *source, const gchar *target)
311 if (!source || !target) return FALSE;
313 sl = path_from_utf8(source);
314 tl = path_from_utf8(target);
316 ret = (symlink(sl, tl) == 0);
324 gint mkdir_utf8(const gchar *s, int mode)
329 if (!s) return FALSE;
331 sl = path_from_utf8(s);
332 ret = (mkdir(sl, mode) == 0);
337 gint rmdir_utf8(const gchar *s)
342 if (!s) return FALSE;
344 sl = path_from_utf8(s);
345 ret = (rmdir(sl) == 0);
351 gint copy_file_attributes(const gchar *s, const gchar *t, gint perms, gint mtime)
357 if (!s || !t) return FALSE;
359 sl = path_from_utf8(s);
360 tl = path_from_utf8(t);
362 if (stat(sl, &st) == 0)
368 /* set the dest file attributes to that of source (ignoring errors) */
370 if (perms && chown(tl, st.st_uid, st.st_gid) < 0) ret = FALSE;
371 if (perms && chmod(tl, st.st_mode) < 0) ret = FALSE;
373 tb.actime = st.st_atime;
374 tb.modtime = st.st_mtime;
375 if (mtime && utime(tl, &tb) < 0) ret = FALSE;
384 /* paths are in filesystem encoding */
385 static gint hard_linked(const gchar *a, const gchar *b)
390 if (stat(a, &sta) != 0 || stat(b, &stb) != 0) return FALSE;
392 return (sta.st_dev == stb.st_dev &&
393 sta.st_ino == stb.st_ino);
396 gint copy_file(const gchar *s, const gchar *t)
404 sl = path_from_utf8(s);
405 tl = path_from_utf8(t);
407 if (hard_linked(sl, tl))
414 fi = fopen(sl, "rb");
417 fo = fopen(tl, "wb");
428 if (!fi || !fo) return FALSE;
430 while ((b = fread(buf, sizeof(char), sizeof(buf), fi)) && b != 0)
432 if (fwrite(buf, sizeof(char), b, fo) != b)
443 copy_file_attributes(s, t, TRUE, TRUE);
448 gint move_file(const gchar *s, const gchar *t)
453 if (!s || !t) return FALSE;
455 sl = path_from_utf8(s);
456 tl = path_from_utf8(t);
457 if (rename(sl, tl) < 0)
459 /* this may have failed because moving a file across filesystems
460 was attempted, so try copy and delete instead */
465 /* err, now we can't delete the source file so return FALSE */
480 gint rename_file(const gchar *s, const gchar *t)
485 if (!s || !t) return FALSE;
487 sl = path_from_utf8(s);
488 tl = path_from_utf8(t);
489 ret = (rename(sl, tl) == 0);
496 gchar *get_current_dir(void)
501 pathl = g_get_current_dir();
502 path8 = path_to_utf8(pathl);
508 void string_list_free(GList *list)
510 g_list_foreach(list, (GFunc)g_free, NULL);
514 GList *string_list_copy(GList *list)
516 GList *new_list = NULL;
527 new_list = g_list_prepend(new_list, g_strdup(path));
530 return g_list_reverse(new_list);
533 gchar *unique_filename(const gchar *path, const gchar *ext, const gchar *divider, gint pad)
539 if (!divider) divider = "";
541 unique = g_strconcat(path, ext, NULL);
542 while (isname(unique))
547 unique = g_strdup_printf("%s%s%03d%s", path, divider, n, ext);
551 unique = g_strdup_printf("%s%s%d%s", path, divider, n, ext);
565 gchar *unique_filename_simple(const gchar *path)
571 if (!path) return NULL;
573 name = filename_from_path(path);
574 if (!name) return NULL;
576 ext = extension_from_path(name);
580 unique = unique_filename(path, NULL, "_", TRUE);
586 base = remove_extension_from_path(path);
587 unique = unique_filename(base, ext, "_", TRUE);
594 const gchar *filename_from_path(const gchar *path)
598 if (!path) return NULL;
600 base = strrchr(path, '/');
601 if (base) return base + 1;
606 gchar *remove_level_from_path(const gchar *path)
610 if (!path) return NULL;
614 if (path[p] == '/') n = p;
619 return g_strndup(path, (gsize) n);
622 gchar *concat_dir_and_file(const gchar *base, const gchar *name)
624 if (!base || !name) return NULL;
626 if (strcmp(base, "/") == 0) return g_strconcat(base, name, NULL);
628 return g_strconcat(base, "/", name, NULL);
631 const gchar *extension_from_path(const gchar *path)
633 if (!path) return NULL;
634 return strrchr(path, '.');
637 gint file_extension_match(const gchar *path, const gchar *ext)
642 if (!path) return FALSE;
643 if (!ext) return TRUE;
649 return (p > e && strncasecmp(path + p - e, ext, e) == 0);
652 gchar *remove_extension_from_path(const gchar *path)
656 if (!path) return NULL;
660 if (path[p] == '.') n = p;
665 return g_strndup(path, (gsize) n);
668 void parse_out_relatives(gchar *path)
676 while (path[s] != '\0')
678 if (path[s] == '/' && path[s+1] == '.' && (path[s+2] == '/' || path[s+2] == '\0') )
682 else if (path[s] == '/' && path[s+1] == '.' && path[s+2] == '.' && (path[s+3] == '/' || path[s+3] == '\0') )
686 while (path[t] != '/' && t > 0) t--;
690 if (s != t) path[t] = path[s];
695 if (t == 0 && path[t] == '/') t++;
696 if (t > 1 && path[t-1] == '/') t--;
700 gint file_in_path(const gchar *name)
707 if (!name) return FALSE;
708 path = g_strdup(getenv("PATH"));
709 if (!path) return FALSE;
710 namel = path_from_utf8(name);
714 while (p < l && !ret)
718 while (path[e] != ':' && path[e] != '\0') e++;
721 f = g_strconcat(path + p, "/", namel, NULL);
722 if (isfile(f)) ret = TRUE;