Mercurial > geeqie
annotate src/ui_fileops.c @ 1670:2239dc484435
added command for editing ufraw id file, changed menu location
author | nadvornik |
---|---|
date | Sat, 27 Jun 2009 22:40:25 +0000 |
parents | 51d70f62338c |
children | 192d4752fd06 |
rev | line source |
---|---|
9 | 1 /* |
2 * (SLIK) SimpLIstic sKin functions | |
69
31759d770628
Fri Oct 13 10:27:22 2006 John Ellis <johne@verizon.net>
gqview
parents:
40
diff
changeset
|
3 * (C) 2006 John Ellis |
1284 | 4 * Copyright (C) 2008 - 2009 The Geeqie Team |
9 | 5 * |
6 * Author: John Ellis | |
7 * | |
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! | |
11 */ | |
12 | |
13 #ifdef HAVE_CONFIG_H | |
14 # include "config.h" | |
15 #endif | |
16 | |
17 #include <pwd.h> | |
18 #include <stdio.h> | |
19 #include <stdlib.h> | |
20 #include <string.h> | |
21 #include <unistd.h> | |
22 #include <sys/param.h> | |
23 #include <dirent.h> | |
24 #include <utime.h> | |
25 | |
26 #include <glib.h> | |
27 #include <gtk/gtk.h> /* for locale warning dialog */ | |
28 | |
281 | 29 #include "main.h" |
9 | 30 #include "ui_fileops.h" |
31 | |
32 #include "ui_utildlg.h" /* for locale warning dialog */ | |
1642
51d70f62338c
Fix up compilation using make -jN: sometimes it fails due to order of headers inclusion.
zas_
parents:
1555
diff
changeset
|
33 #include "md5-util.h" |
9 | 34 |
35 /* | |
36 *----------------------------------------------------------------------------- | |
37 * generic file information and manipulation routines (public) | |
38 *----------------------------------------------------------------------------- | |
442 | 39 */ |
9 | 40 |
41 | |
42 | |
43 void print_term(const gchar *text_utf8) | |
44 { | |
45 gchar *text_l; | |
46 | |
47 text_l = g_locale_from_utf8(text_utf8, -1, NULL, NULL, NULL); | |
1555 | 48 fputs((text_l) ? text_l : text_utf8, stderr); |
9 | 49 g_free(text_l); |
50 } | |
51 | |
52 static void encoding_dialog(const gchar *path); | |
53 | |
1448 | 54 static gboolean encoding_dialog_idle(gpointer data) |
9 | 55 { |
56 gchar *path = data; | |
57 | |
58 encoding_dialog(path); | |
59 g_free(path); | |
60 | |
61 return FALSE; | |
62 } | |
63 | |
64 static gint encoding_dialog_delay(gpointer data) | |
65 { | |
66 g_idle_add(encoding_dialog_idle, data); | |
67 | |
68 return 0; | |
69 } | |
70 | |
71 static void encoding_dialog(const gchar *path) | |
72 { | |
1437 | 73 static gboolean warned_user = FALSE; |
9 | 74 GenericDialog *gd; |
75 GString *string; | |
76 const gchar *lc; | |
77 const gchar *bf; | |
78 | |
79 /* check that gtk is initialized (loop is level > 0) */ | |
80 if (gtk_main_level() == 0) | |
81 { | |
82 /* gtk not initialized */ | |
83 gtk_init_add(encoding_dialog_delay, g_strdup(path)); | |
84 return; | |
85 } | |
86 | |
87 if (warned_user) return; | |
88 warned_user = TRUE; | |
89 | |
90 lc = getenv("LANG"); | |
91 bf = getenv("G_BROKEN_FILENAMES"); | |
92 | |
93 string = g_string_new(""); | |
989
50e1f5e54c1a
Fix untranslated messages. French translation and POTFILES.in were updated.
zas_
parents:
983
diff
changeset
|
94 g_string_append(string, _("One or more filenames are not encoded with the preferred locale character set.\n")); |
50e1f5e54c1a
Fix untranslated messages. French translation and POTFILES.in were updated.
zas_
parents:
983
diff
changeset
|
95 g_string_append_printf(string, _("Operations on, and display of these files with %s may not succeed.\n"), PACKAGE); |
50e1f5e54c1a
Fix untranslated messages. French translation and POTFILES.in were updated.
zas_
parents:
983
diff
changeset
|
96 g_string_append(string, "\n"); |
50e1f5e54c1a
Fix untranslated messages. French translation and POTFILES.in were updated.
zas_
parents:
983
diff
changeset
|
97 g_string_append(string, _("If your filenames are not encoded in utf-8, try setting the environment variable G_BROKEN_FILENAMES=1\n")); |
50e1f5e54c1a
Fix untranslated messages. French translation and POTFILES.in were updated.
zas_
parents:
983
diff
changeset
|
98 if (bf) |
50e1f5e54c1a
Fix untranslated messages. French translation and POTFILES.in were updated.
zas_
parents:
983
diff
changeset
|
99 g_string_append_printf(string, _("It appears G_BROKEN_FILENAMES is set to %s\n"), bf); |
50e1f5e54c1a
Fix untranslated messages. French translation and POTFILES.in were updated.
zas_
parents:
983
diff
changeset
|
100 else |
50e1f5e54c1a
Fix untranslated messages. French translation and POTFILES.in were updated.
zas_
parents:
983
diff
changeset
|
101 g_string_append(string, _("It appears G_BROKEN_FILENAMES is not set\n")); |
50e1f5e54c1a
Fix untranslated messages. French translation and POTFILES.in were updated.
zas_
parents:
983
diff
changeset
|
102 g_string_append(string, "\n"); |
50e1f5e54c1a
Fix untranslated messages. French translation and POTFILES.in were updated.
zas_
parents:
983
diff
changeset
|
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"); |
9 | 104 if (lc && (strstr(lc, "UTF-8") || strstr(lc, "utf-8"))) |
105 { | |
106 gchar *name; | |
107 name = g_convert(path, -1, "UTF-8", "ISO-8859-1", NULL, NULL, NULL); | |
989
50e1f5e54c1a
Fix untranslated messages. French translation and POTFILES.in were updated.
zas_
parents:
983
diff
changeset
|
108 string = g_string_append(string, _("\nPreferred encoding appears to be UTF-8, however the file:\n")); |
50e1f5e54c1a
Fix untranslated messages. French translation and POTFILES.in were updated.
zas_
parents:
983
diff
changeset
|
109 g_string_append_printf(string, "\"%s\"\n", (name) ? name : _("[name not displayable]")); |
50e1f5e54c1a
Fix untranslated messages. French translation and POTFILES.in were updated.
zas_
parents:
983
diff
changeset
|
110 |
50e1f5e54c1a
Fix untranslated messages. French translation and POTFILES.in were updated.
zas_
parents:
983
diff
changeset
|
111 if (g_utf8_validate(path, -1, NULL)) |
50e1f5e54c1a
Fix untranslated messages. French translation and POTFILES.in were updated.
zas_
parents:
983
diff
changeset
|
112 g_string_append_printf(string, _("\"%s\" is encoded in valid UTF-8."), (name) ? name : _("[name not displayable]")); |
50e1f5e54c1a
Fix untranslated messages. French translation and POTFILES.in were updated.
zas_
parents:
983
diff
changeset
|
113 else |
50e1f5e54c1a
Fix untranslated messages. French translation and POTFILES.in were updated.
zas_
parents:
983
diff
changeset
|
114 g_string_append_printf(string, _("\"%s\" is not encoded in valid UTF-8."), (name) ? name : _("[name not displayable]")); |
50e1f5e54c1a
Fix untranslated messages. French translation and POTFILES.in were updated.
zas_
parents:
983
diff
changeset
|
115 g_string_append(string, "\n"); |
9 | 116 g_free(name); |
117 } | |
118 | |
989
50e1f5e54c1a
Fix untranslated messages. French translation and POTFILES.in were updated.
zas_
parents:
983
diff
changeset
|
119 gd = generic_dialog_new(_("Filename encoding locale mismatch"), |
1174
0bea79d87065
Drop useless wmclass stuff. Gtk will take care of it and as said in the documentation using gtk_window_set_wmclass() is sort of pointless.
zas_
parents:
1164
diff
changeset
|
120 "locale warning", NULL, TRUE, NULL, NULL); |
9 | 121 generic_dialog_add_button(gd, GTK_STOCK_CLOSE, NULL, NULL, TRUE); |
122 | |
123 generic_dialog_add_message(gd, GTK_STOCK_DIALOG_WARNING, | |
989
50e1f5e54c1a
Fix untranslated messages. French translation and POTFILES.in were updated.
zas_
parents:
983
diff
changeset
|
124 _("Filename encoding locale mismatch"), string->str); |
9 | 125 |
126 gtk_widget_show(gd->dialog); | |
127 | |
128 g_string_free(string, TRUE); | |
129 } | |
130 | |
1386
4da6d326919c
Add debug versions of path_to_utf8() and path_from_utf8() which allows to report the caller file and line, this is enabled with --enable-debug-flags (and developer mode too).
zas_
parents:
1360
diff
changeset
|
131 #if GQ_DEBUG_PATH_UTF8 |
4da6d326919c
Add debug versions of path_to_utf8() and path_from_utf8() which allows to report the caller file and line, this is enabled with --enable-debug-flags (and developer mode too).
zas_
parents:
1360
diff
changeset
|
132 gchar *path_to_utf8_debug(const gchar *path, const gchar *file, gint line) |
4da6d326919c
Add debug versions of path_to_utf8() and path_from_utf8() which allows to report the caller file and line, this is enabled with --enable-debug-flags (and developer mode too).
zas_
parents:
1360
diff
changeset
|
133 #else |
9 | 134 gchar *path_to_utf8(const gchar *path) |
1386
4da6d326919c
Add debug versions of path_to_utf8() and path_from_utf8() which allows to report the caller file and line, this is enabled with --enable-debug-flags (and developer mode too).
zas_
parents:
1360
diff
changeset
|
135 #endif |
9 | 136 { |
137 gchar *utf8; | |
138 GError *error = NULL; | |
139 | |
140 if (!path) return NULL; | |
141 | |
142 utf8 = g_filename_to_utf8(path, -1, NULL, NULL, &error); | |
143 if (error) | |
144 { | |
1386
4da6d326919c
Add debug versions of path_to_utf8() and path_from_utf8() which allows to report the caller file and line, this is enabled with --enable-debug-flags (and developer mode too).
zas_
parents:
1360
diff
changeset
|
145 #if GQ_DEBUG_PATH_UTF8 |
4da6d326919c
Add debug versions of path_to_utf8() and path_from_utf8() which allows to report the caller file and line, this is enabled with --enable-debug-flags (and developer mode too).
zas_
parents:
1360
diff
changeset
|
146 log_printf("%s:%d: Unable to convert filename to UTF-8:\n%s\n%s\n", file, line, path, error->message); |
4da6d326919c
Add debug versions of path_to_utf8() and path_from_utf8() which allows to report the caller file and line, this is enabled with --enable-debug-flags (and developer mode too).
zas_
parents:
1360
diff
changeset
|
147 #else |
673
fbebf5cf4a55
Do not use printf() directly but use new wrapper function log_printf() instead.
zas_
parents:
671
diff
changeset
|
148 log_printf("Unable to convert filename to UTF-8:\n%s\n%s\n", path, error->message); |
1386
4da6d326919c
Add debug versions of path_to_utf8() and path_from_utf8() which allows to report the caller file and line, this is enabled with --enable-debug-flags (and developer mode too).
zas_
parents:
1360
diff
changeset
|
149 #endif |
9 | 150 g_error_free(error); |
151 encoding_dialog(path); | |
152 } | |
153 if (!utf8) | |
154 { | |
155 /* just let it through, but bad things may happen */ | |
156 utf8 = g_strdup(path); | |
157 } | |
158 | |
159 return utf8; | |
160 } | |
161 | |
1386
4da6d326919c
Add debug versions of path_to_utf8() and path_from_utf8() which allows to report the caller file and line, this is enabled with --enable-debug-flags (and developer mode too).
zas_
parents:
1360
diff
changeset
|
162 #if GQ_DEBUG_PATH_UTF8 |
4da6d326919c
Add debug versions of path_to_utf8() and path_from_utf8() which allows to report the caller file and line, this is enabled with --enable-debug-flags (and developer mode too).
zas_
parents:
1360
diff
changeset
|
163 gchar *path_from_utf8_debug(const gchar *utf8, const gchar *file, gint line) |
4da6d326919c
Add debug versions of path_to_utf8() and path_from_utf8() which allows to report the caller file and line, this is enabled with --enable-debug-flags (and developer mode too).
zas_
parents:
1360
diff
changeset
|
164 #else |
9 | 165 gchar *path_from_utf8(const gchar *utf8) |
1386
4da6d326919c
Add debug versions of path_to_utf8() and path_from_utf8() which allows to report the caller file and line, this is enabled with --enable-debug-flags (and developer mode too).
zas_
parents:
1360
diff
changeset
|
166 #endif |
9 | 167 { |
168 gchar *path; | |
169 GError *error = NULL; | |
170 | |
171 if (!utf8) return NULL; | |
172 | |
173 path = g_filename_from_utf8(utf8, -1, NULL, NULL, &error); | |
174 if (error) | |
175 { | |
1386
4da6d326919c
Add debug versions of path_to_utf8() and path_from_utf8() which allows to report the caller file and line, this is enabled with --enable-debug-flags (and developer mode too).
zas_
parents:
1360
diff
changeset
|
176 #if GQ_DEBUG_PATH_UTF8 |
4da6d326919c
Add debug versions of path_to_utf8() and path_from_utf8() which allows to report the caller file and line, this is enabled with --enable-debug-flags (and developer mode too).
zas_
parents:
1360
diff
changeset
|
177 log_printf("%s:%d: Unable to convert filename to locale from UTF-8:\n%s\n%s\n", file, line, utf8, error->message); |
4da6d326919c
Add debug versions of path_to_utf8() and path_from_utf8() which allows to report the caller file and line, this is enabled with --enable-debug-flags (and developer mode too).
zas_
parents:
1360
diff
changeset
|
178 #else |
673
fbebf5cf4a55
Do not use printf() directly but use new wrapper function log_printf() instead.
zas_
parents:
671
diff
changeset
|
179 log_printf("Unable to convert filename to locale from UTF-8:\n%s\n%s\n", utf8, error->message); |
1386
4da6d326919c
Add debug versions of path_to_utf8() and path_from_utf8() which allows to report the caller file and line, this is enabled with --enable-debug-flags (and developer mode too).
zas_
parents:
1360
diff
changeset
|
180 #endif |
9 | 181 g_error_free(error); |
182 } | |
183 if (!path) | |
184 { | |
185 /* if invalid UTF-8, text probaby still in original form, so just copy it */ | |
186 path = g_strdup(utf8); | |
187 } | |
188 | |
189 return path; | |
190 } | |
191 | |
998
d2701a5864c5
Fixed segfault when is run inside directory with non valid uft-8 image
bruclik
parents:
990
diff
changeset
|
192 /* first we try the HOME environment var, if that doesn't work, we try g_get_homedir(). */ |
9 | 193 const gchar *homedir(void) |
194 { | |
195 static gchar *home = NULL; | |
196 | |
197 if (!home) | |
198 home = path_to_utf8(getenv("HOME")); | |
998
d2701a5864c5
Fixed segfault when is run inside directory with non valid uft-8 image
bruclik
parents:
990
diff
changeset
|
199 |
9 | 200 if (!home) |
998
d2701a5864c5
Fixed segfault when is run inside directory with non valid uft-8 image
bruclik
parents:
990
diff
changeset
|
201 home = path_to_utf8(g_get_home_dir()); |
9 | 202 |
203 return home; | |
204 } | |
205 | |
1149
c4fcf8001574
Implement preliminary support for XDG Base Directory Specification.
zas_
parents:
1148
diff
changeset
|
206 static gchar *xdg_dir_get(const gchar *key, const gchar *fallback) |
c4fcf8001574
Implement preliminary support for XDG Base Directory Specification.
zas_
parents:
1148
diff
changeset
|
207 { |
c4fcf8001574
Implement preliminary support for XDG Base Directory Specification.
zas_
parents:
1148
diff
changeset
|
208 gchar *dir = getenv(key); |
c4fcf8001574
Implement preliminary support for XDG Base Directory Specification.
zas_
parents:
1148
diff
changeset
|
209 |
c4fcf8001574
Implement preliminary support for XDG Base Directory Specification.
zas_
parents:
1148
diff
changeset
|
210 if (!dir || dir[0] == '\0') |
c4fcf8001574
Implement preliminary support for XDG Base Directory Specification.
zas_
parents:
1148
diff
changeset
|
211 { |
c4fcf8001574
Implement preliminary support for XDG Base Directory Specification.
zas_
parents:
1148
diff
changeset
|
212 return g_build_filename(homedir(), fallback, NULL); |
c4fcf8001574
Implement preliminary support for XDG Base Directory Specification.
zas_
parents:
1148
diff
changeset
|
213 } |
c4fcf8001574
Implement preliminary support for XDG Base Directory Specification.
zas_
parents:
1148
diff
changeset
|
214 |
c4fcf8001574
Implement preliminary support for XDG Base Directory Specification.
zas_
parents:
1148
diff
changeset
|
215 return path_to_utf8(dir); |
c4fcf8001574
Implement preliminary support for XDG Base Directory Specification.
zas_
parents:
1148
diff
changeset
|
216 } |
c4fcf8001574
Implement preliminary support for XDG Base Directory Specification.
zas_
parents:
1148
diff
changeset
|
217 |
c4fcf8001574
Implement preliminary support for XDG Base Directory Specification.
zas_
parents:
1148
diff
changeset
|
218 const gchar *xdg_data_home_get(void) |
c4fcf8001574
Implement preliminary support for XDG Base Directory Specification.
zas_
parents:
1148
diff
changeset
|
219 { |
c4fcf8001574
Implement preliminary support for XDG Base Directory Specification.
zas_
parents:
1148
diff
changeset
|
220 static const gchar *xdg_data_home = NULL; |
c4fcf8001574
Implement preliminary support for XDG Base Directory Specification.
zas_
parents:
1148
diff
changeset
|
221 |
c4fcf8001574
Implement preliminary support for XDG Base Directory Specification.
zas_
parents:
1148
diff
changeset
|
222 if (xdg_data_home) return xdg_data_home; |
c4fcf8001574
Implement preliminary support for XDG Base Directory Specification.
zas_
parents:
1148
diff
changeset
|
223 |
c4fcf8001574
Implement preliminary support for XDG Base Directory Specification.
zas_
parents:
1148
diff
changeset
|
224 xdg_data_home = xdg_dir_get("XDG_DATA_HOME", ".local/share"); |
c4fcf8001574
Implement preliminary support for XDG Base Directory Specification.
zas_
parents:
1148
diff
changeset
|
225 |
c4fcf8001574
Implement preliminary support for XDG Base Directory Specification.
zas_
parents:
1148
diff
changeset
|
226 return xdg_data_home; |
c4fcf8001574
Implement preliminary support for XDG Base Directory Specification.
zas_
parents:
1148
diff
changeset
|
227 } |
c4fcf8001574
Implement preliminary support for XDG Base Directory Specification.
zas_
parents:
1148
diff
changeset
|
228 |
c4fcf8001574
Implement preliminary support for XDG Base Directory Specification.
zas_
parents:
1148
diff
changeset
|
229 const gchar *xdg_config_home_get(void) |
c4fcf8001574
Implement preliminary support for XDG Base Directory Specification.
zas_
parents:
1148
diff
changeset
|
230 { |
c4fcf8001574
Implement preliminary support for XDG Base Directory Specification.
zas_
parents:
1148
diff
changeset
|
231 static const gchar *xdg_config_home = NULL; |
c4fcf8001574
Implement preliminary support for XDG Base Directory Specification.
zas_
parents:
1148
diff
changeset
|
232 |
c4fcf8001574
Implement preliminary support for XDG Base Directory Specification.
zas_
parents:
1148
diff
changeset
|
233 if (xdg_config_home) return xdg_config_home; |
c4fcf8001574
Implement preliminary support for XDG Base Directory Specification.
zas_
parents:
1148
diff
changeset
|
234 |
c4fcf8001574
Implement preliminary support for XDG Base Directory Specification.
zas_
parents:
1148
diff
changeset
|
235 xdg_config_home = xdg_dir_get("XDG_CONFIG_HOME", ".config"); |
c4fcf8001574
Implement preliminary support for XDG Base Directory Specification.
zas_
parents:
1148
diff
changeset
|
236 |
c4fcf8001574
Implement preliminary support for XDG Base Directory Specification.
zas_
parents:
1148
diff
changeset
|
237 return xdg_config_home; |
c4fcf8001574
Implement preliminary support for XDG Base Directory Specification.
zas_
parents:
1148
diff
changeset
|
238 } |
c4fcf8001574
Implement preliminary support for XDG Base Directory Specification.
zas_
parents:
1148
diff
changeset
|
239 |
c4fcf8001574
Implement preliminary support for XDG Base Directory Specification.
zas_
parents:
1148
diff
changeset
|
240 const gchar *xdg_cache_home_get(void) |
c4fcf8001574
Implement preliminary support for XDG Base Directory Specification.
zas_
parents:
1148
diff
changeset
|
241 { |
c4fcf8001574
Implement preliminary support for XDG Base Directory Specification.
zas_
parents:
1148
diff
changeset
|
242 static const gchar *xdg_cache_home = NULL; |
c4fcf8001574
Implement preliminary support for XDG Base Directory Specification.
zas_
parents:
1148
diff
changeset
|
243 |
c4fcf8001574
Implement preliminary support for XDG Base Directory Specification.
zas_
parents:
1148
diff
changeset
|
244 if (xdg_cache_home) return xdg_cache_home; |
c4fcf8001574
Implement preliminary support for XDG Base Directory Specification.
zas_
parents:
1148
diff
changeset
|
245 |
c4fcf8001574
Implement preliminary support for XDG Base Directory Specification.
zas_
parents:
1148
diff
changeset
|
246 xdg_cache_home = xdg_dir_get("XDG_CACHE_HOME", ".cache"); |
c4fcf8001574
Implement preliminary support for XDG Base Directory Specification.
zas_
parents:
1148
diff
changeset
|
247 |
c4fcf8001574
Implement preliminary support for XDG Base Directory Specification.
zas_
parents:
1148
diff
changeset
|
248 return xdg_cache_home; |
c4fcf8001574
Implement preliminary support for XDG Base Directory Specification.
zas_
parents:
1148
diff
changeset
|
249 } |
c4fcf8001574
Implement preliminary support for XDG Base Directory Specification.
zas_
parents:
1148
diff
changeset
|
250 |
1145
3a7af6a8cd5f
Use functions to return directories instead of constants.
zas_
parents:
1055
diff
changeset
|
251 const gchar *get_rc_dir(void) |
3a7af6a8cd5f
Use functions to return directories instead of constants.
zas_
parents:
1055
diff
changeset
|
252 { |
3a7af6a8cd5f
Use functions to return directories instead of constants.
zas_
parents:
1055
diff
changeset
|
253 static gchar *rc_dir = NULL; |
3a7af6a8cd5f
Use functions to return directories instead of constants.
zas_
parents:
1055
diff
changeset
|
254 |
3a7af6a8cd5f
Use functions to return directories instead of constants.
zas_
parents:
1055
diff
changeset
|
255 if (rc_dir) return rc_dir; |
3a7af6a8cd5f
Use functions to return directories instead of constants.
zas_
parents:
1055
diff
changeset
|
256 |
1149
c4fcf8001574
Implement preliminary support for XDG Base Directory Specification.
zas_
parents:
1148
diff
changeset
|
257 if (USE_XDG) |
c4fcf8001574
Implement preliminary support for XDG Base Directory Specification.
zas_
parents:
1148
diff
changeset
|
258 { |
c4fcf8001574
Implement preliminary support for XDG Base Directory Specification.
zas_
parents:
1148
diff
changeset
|
259 rc_dir = g_build_filename(xdg_config_home_get(), GQ_APPNAME_LC, NULL); |
c4fcf8001574
Implement preliminary support for XDG Base Directory Specification.
zas_
parents:
1148
diff
changeset
|
260 } |
c4fcf8001574
Implement preliminary support for XDG Base Directory Specification.
zas_
parents:
1148
diff
changeset
|
261 else |
c4fcf8001574
Implement preliminary support for XDG Base Directory Specification.
zas_
parents:
1148
diff
changeset
|
262 { |
c4fcf8001574
Implement preliminary support for XDG Base Directory Specification.
zas_
parents:
1148
diff
changeset
|
263 rc_dir = g_build_filename(homedir(), GQ_RC_DIR, NULL); |
c4fcf8001574
Implement preliminary support for XDG Base Directory Specification.
zas_
parents:
1148
diff
changeset
|
264 } |
c4fcf8001574
Implement preliminary support for XDG Base Directory Specification.
zas_
parents:
1148
diff
changeset
|
265 |
1145
3a7af6a8cd5f
Use functions to return directories instead of constants.
zas_
parents:
1055
diff
changeset
|
266 return rc_dir; |
3a7af6a8cd5f
Use functions to return directories instead of constants.
zas_
parents:
1055
diff
changeset
|
267 } |
3a7af6a8cd5f
Use functions to return directories instead of constants.
zas_
parents:
1055
diff
changeset
|
268 |
3a7af6a8cd5f
Use functions to return directories instead of constants.
zas_
parents:
1055
diff
changeset
|
269 const gchar *get_collections_dir(void) |
3a7af6a8cd5f
Use functions to return directories instead of constants.
zas_
parents:
1055
diff
changeset
|
270 { |
3a7af6a8cd5f
Use functions to return directories instead of constants.
zas_
parents:
1055
diff
changeset
|
271 static gchar *collections_dir = NULL; |
3a7af6a8cd5f
Use functions to return directories instead of constants.
zas_
parents:
1055
diff
changeset
|
272 |
3a7af6a8cd5f
Use functions to return directories instead of constants.
zas_
parents:
1055
diff
changeset
|
273 if (collections_dir) return collections_dir; |
3a7af6a8cd5f
Use functions to return directories instead of constants.
zas_
parents:
1055
diff
changeset
|
274 |
1149
c4fcf8001574
Implement preliminary support for XDG Base Directory Specification.
zas_
parents:
1148
diff
changeset
|
275 if (USE_XDG) |
c4fcf8001574
Implement preliminary support for XDG Base Directory Specification.
zas_
parents:
1148
diff
changeset
|
276 { |
c4fcf8001574
Implement preliminary support for XDG Base Directory Specification.
zas_
parents:
1148
diff
changeset
|
277 collections_dir = g_build_filename(xdg_data_home_get(), GQ_APPNAME_LC, GQ_COLLECTIONS_DIR, NULL); |
c4fcf8001574
Implement preliminary support for XDG Base Directory Specification.
zas_
parents:
1148
diff
changeset
|
278 } |
c4fcf8001574
Implement preliminary support for XDG Base Directory Specification.
zas_
parents:
1148
diff
changeset
|
279 else |
c4fcf8001574
Implement preliminary support for XDG Base Directory Specification.
zas_
parents:
1148
diff
changeset
|
280 { |
c4fcf8001574
Implement preliminary support for XDG Base Directory Specification.
zas_
parents:
1148
diff
changeset
|
281 collections_dir = g_build_filename(get_rc_dir(), GQ_COLLECTIONS_DIR, NULL); |
c4fcf8001574
Implement preliminary support for XDG Base Directory Specification.
zas_
parents:
1148
diff
changeset
|
282 } |
c4fcf8001574
Implement preliminary support for XDG Base Directory Specification.
zas_
parents:
1148
diff
changeset
|
283 |
1145
3a7af6a8cd5f
Use functions to return directories instead of constants.
zas_
parents:
1055
diff
changeset
|
284 return collections_dir; |
3a7af6a8cd5f
Use functions to return directories instead of constants.
zas_
parents:
1055
diff
changeset
|
285 } |
3a7af6a8cd5f
Use functions to return directories instead of constants.
zas_
parents:
1055
diff
changeset
|
286 |
3a7af6a8cd5f
Use functions to return directories instead of constants.
zas_
parents:
1055
diff
changeset
|
287 const gchar *get_trash_dir(void) |
3a7af6a8cd5f
Use functions to return directories instead of constants.
zas_
parents:
1055
diff
changeset
|
288 { |
3a7af6a8cd5f
Use functions to return directories instead of constants.
zas_
parents:
1055
diff
changeset
|
289 static gchar *trash_dir = NULL; |
3a7af6a8cd5f
Use functions to return directories instead of constants.
zas_
parents:
1055
diff
changeset
|
290 |
3a7af6a8cd5f
Use functions to return directories instead of constants.
zas_
parents:
1055
diff
changeset
|
291 if (trash_dir) return trash_dir; |
1149
c4fcf8001574
Implement preliminary support for XDG Base Directory Specification.
zas_
parents:
1148
diff
changeset
|
292 |
c4fcf8001574
Implement preliminary support for XDG Base Directory Specification.
zas_
parents:
1148
diff
changeset
|
293 if (USE_XDG) |
c4fcf8001574
Implement preliminary support for XDG Base Directory Specification.
zas_
parents:
1148
diff
changeset
|
294 { |
c4fcf8001574
Implement preliminary support for XDG Base Directory Specification.
zas_
parents:
1148
diff
changeset
|
295 trash_dir = g_build_filename(xdg_data_home_get(), GQ_APPNAME_LC, GQ_TRASH_DIR, NULL); |
c4fcf8001574
Implement preliminary support for XDG Base Directory Specification.
zas_
parents:
1148
diff
changeset
|
296 } |
c4fcf8001574
Implement preliminary support for XDG Base Directory Specification.
zas_
parents:
1148
diff
changeset
|
297 else |
c4fcf8001574
Implement preliminary support for XDG Base Directory Specification.
zas_
parents:
1148
diff
changeset
|
298 { |
c4fcf8001574
Implement preliminary support for XDG Base Directory Specification.
zas_
parents:
1148
diff
changeset
|
299 trash_dir = g_build_filename(get_rc_dir(), GQ_TRASH_DIR, NULL); |
c4fcf8001574
Implement preliminary support for XDG Base Directory Specification.
zas_
parents:
1148
diff
changeset
|
300 } |
1145
3a7af6a8cd5f
Use functions to return directories instead of constants.
zas_
parents:
1055
diff
changeset
|
301 |
3a7af6a8cd5f
Use functions to return directories instead of constants.
zas_
parents:
1055
diff
changeset
|
302 return trash_dir; |
3a7af6a8cd5f
Use functions to return directories instead of constants.
zas_
parents:
1055
diff
changeset
|
303 } |
3a7af6a8cd5f
Use functions to return directories instead of constants.
zas_
parents:
1055
diff
changeset
|
304 |
1448 | 305 gboolean stat_utf8(const gchar *s, struct stat *st) |
9 | 306 { |
307 gchar *sl; | |
1448 | 308 gboolean ret; |
9 | 309 |
310 if (!s) return FALSE; | |
311 sl = path_from_utf8(s); | |
312 ret = (stat(sl, st) == 0); | |
313 g_free(sl); | |
314 | |
315 return ret; | |
316 } | |
317 | |
1448 | 318 gboolean lstat_utf8(const gchar *s, struct stat *st) |
40
dcc04a6a58bf
Sat Apr 16 12:29:42 2005 John Ellis <johne@verizon.net>
gqview
parents:
9
diff
changeset
|
319 { |
dcc04a6a58bf
Sat Apr 16 12:29:42 2005 John Ellis <johne@verizon.net>
gqview
parents:
9
diff
changeset
|
320 gchar *sl; |
1448 | 321 gboolean ret; |
40
dcc04a6a58bf
Sat Apr 16 12:29:42 2005 John Ellis <johne@verizon.net>
gqview
parents:
9
diff
changeset
|
322 |
dcc04a6a58bf
Sat Apr 16 12:29:42 2005 John Ellis <johne@verizon.net>
gqview
parents:
9
diff
changeset
|
323 if (!s) return FALSE; |
dcc04a6a58bf
Sat Apr 16 12:29:42 2005 John Ellis <johne@verizon.net>
gqview
parents:
9
diff
changeset
|
324 sl = path_from_utf8(s); |
dcc04a6a58bf
Sat Apr 16 12:29:42 2005 John Ellis <johne@verizon.net>
gqview
parents:
9
diff
changeset
|
325 ret = (lstat(sl, st) == 0); |
dcc04a6a58bf
Sat Apr 16 12:29:42 2005 John Ellis <johne@verizon.net>
gqview
parents:
9
diff
changeset
|
326 g_free(sl); |
dcc04a6a58bf
Sat Apr 16 12:29:42 2005 John Ellis <johne@verizon.net>
gqview
parents:
9
diff
changeset
|
327 |
dcc04a6a58bf
Sat Apr 16 12:29:42 2005 John Ellis <johne@verizon.net>
gqview
parents:
9
diff
changeset
|
328 return ret; |
dcc04a6a58bf
Sat Apr 16 12:29:42 2005 John Ellis <johne@verizon.net>
gqview
parents:
9
diff
changeset
|
329 } |
dcc04a6a58bf
Sat Apr 16 12:29:42 2005 John Ellis <johne@verizon.net>
gqview
parents:
9
diff
changeset
|
330 |
1229
878718372aca
sidecar files grouping was made case-insensitive
nadvornik
parents:
1198
diff
changeset
|
331 /* extension must contain only ASCII characters */ |
1448 | 332 gboolean stat_utf8_case_insensitive_ext(GString *base, const gchar *ext, struct stat *st) |
1229
878718372aca
sidecar files grouping was made case-insensitive
nadvornik
parents:
1198
diff
changeset
|
333 { |
878718372aca
sidecar files grouping was made case-insensitive
nadvornik
parents:
1198
diff
changeset
|
334 gchar *sl; |
878718372aca
sidecar files grouping was made case-insensitive
nadvornik
parents:
1198
diff
changeset
|
335 gchar *extl; |
1448 | 336 gboolean ret = FALSE; |
1229
878718372aca
sidecar files grouping was made case-insensitive
nadvornik
parents:
1198
diff
changeset
|
337 gint ext_len; |
878718372aca
sidecar files grouping was made case-insensitive
nadvornik
parents:
1198
diff
changeset
|
338 gint base_len = strlen(base->str); |
878718372aca
sidecar files grouping was made case-insensitive
nadvornik
parents:
1198
diff
changeset
|
339 |
878718372aca
sidecar files grouping was made case-insensitive
nadvornik
parents:
1198
diff
changeset
|
340 g_string_append(base, ext); |
878718372aca
sidecar files grouping was made case-insensitive
nadvornik
parents:
1198
diff
changeset
|
341 sl = path_from_utf8(base->str); |
878718372aca
sidecar files grouping was made case-insensitive
nadvornik
parents:
1198
diff
changeset
|
342 |
878718372aca
sidecar files grouping was made case-insensitive
nadvornik
parents:
1198
diff
changeset
|
343 extl = strrchr(sl, '.'); |
878718372aca
sidecar files grouping was made case-insensitive
nadvornik
parents:
1198
diff
changeset
|
344 if (extl) |
878718372aca
sidecar files grouping was made case-insensitive
nadvornik
parents:
1198
diff
changeset
|
345 { |
878718372aca
sidecar files grouping was made case-insensitive
nadvornik
parents:
1198
diff
changeset
|
346 gint i, j; |
878718372aca
sidecar files grouping was made case-insensitive
nadvornik
parents:
1198
diff
changeset
|
347 extl++; /* the first char after . */ |
878718372aca
sidecar files grouping was made case-insensitive
nadvornik
parents:
1198
diff
changeset
|
348 ext_len = strlen(extl); |
878718372aca
sidecar files grouping was made case-insensitive
nadvornik
parents:
1198
diff
changeset
|
349 |
878718372aca
sidecar files grouping was made case-insensitive
nadvornik
parents:
1198
diff
changeset
|
350 for (i = 0; i < (1 << ext_len); i++) |
878718372aca
sidecar files grouping was made case-insensitive
nadvornik
parents:
1198
diff
changeset
|
351 { |
878718372aca
sidecar files grouping was made case-insensitive
nadvornik
parents:
1198
diff
changeset
|
352 for (j = 0; j < ext_len; j++) |
878718372aca
sidecar files grouping was made case-insensitive
nadvornik
parents:
1198
diff
changeset
|
353 { |
878718372aca
sidecar files grouping was made case-insensitive
nadvornik
parents:
1198
diff
changeset
|
354 if (i & (1 << j)) |
878718372aca
sidecar files grouping was made case-insensitive
nadvornik
parents:
1198
diff
changeset
|
355 extl[j] = g_ascii_toupper(extl[j]); |
878718372aca
sidecar files grouping was made case-insensitive
nadvornik
parents:
1198
diff
changeset
|
356 else |
878718372aca
sidecar files grouping was made case-insensitive
nadvornik
parents:
1198
diff
changeset
|
357 extl[j] = g_ascii_tolower(extl[j]); |
878718372aca
sidecar files grouping was made case-insensitive
nadvornik
parents:
1198
diff
changeset
|
358 } |
878718372aca
sidecar files grouping was made case-insensitive
nadvornik
parents:
1198
diff
changeset
|
359 ret = (stat(sl, st) == 0); |
878718372aca
sidecar files grouping was made case-insensitive
nadvornik
parents:
1198
diff
changeset
|
360 if (ret) break; |
878718372aca
sidecar files grouping was made case-insensitive
nadvornik
parents:
1198
diff
changeset
|
361 } |
878718372aca
sidecar files grouping was made case-insensitive
nadvornik
parents:
1198
diff
changeset
|
362 |
878718372aca
sidecar files grouping was made case-insensitive
nadvornik
parents:
1198
diff
changeset
|
363 if (ret) |
878718372aca
sidecar files grouping was made case-insensitive
nadvornik
parents:
1198
diff
changeset
|
364 { |
878718372aca
sidecar files grouping was made case-insensitive
nadvornik
parents:
1198
diff
changeset
|
365 /* append the found extension to base */ |
878718372aca
sidecar files grouping was made case-insensitive
nadvornik
parents:
1198
diff
changeset
|
366 base = g_string_truncate(base, base_len); |
878718372aca
sidecar files grouping was made case-insensitive
nadvornik
parents:
1198
diff
changeset
|
367 extl--; |
878718372aca
sidecar files grouping was made case-insensitive
nadvornik
parents:
1198
diff
changeset
|
368 g_string_append(base, extl); |
878718372aca
sidecar files grouping was made case-insensitive
nadvornik
parents:
1198
diff
changeset
|
369 } |
878718372aca
sidecar files grouping was made case-insensitive
nadvornik
parents:
1198
diff
changeset
|
370 } |
878718372aca
sidecar files grouping was made case-insensitive
nadvornik
parents:
1198
diff
changeset
|
371 g_free(sl); |
878718372aca
sidecar files grouping was made case-insensitive
nadvornik
parents:
1198
diff
changeset
|
372 |
878718372aca
sidecar files grouping was made case-insensitive
nadvornik
parents:
1198
diff
changeset
|
373 return ret; |
878718372aca
sidecar files grouping was made case-insensitive
nadvornik
parents:
1198
diff
changeset
|
374 } |
878718372aca
sidecar files grouping was made case-insensitive
nadvornik
parents:
1198
diff
changeset
|
375 |
1448 | 376 gboolean isname(const gchar *s) |
9 | 377 { |
378 struct stat st; | |
379 | |
380 return stat_utf8(s, &st); | |
381 } | |
382 | |
1448 | 383 gboolean isfile(const gchar *s) |
9 | 384 { |
385 struct stat st; | |
386 | |
387 return (stat_utf8(s, &st) && S_ISREG(st.st_mode)); | |
388 } | |
389 | |
1448 | 390 gboolean isdir(const gchar *s) |
9 | 391 { |
392 struct stat st; | |
442 | 393 |
1198 | 394 return (stat_utf8(s, &st) && S_ISDIR(st.st_mode)); |
9 | 395 } |
396 | |
1448 | 397 gboolean islink(const gchar *s) |
40
dcc04a6a58bf
Sat Apr 16 12:29:42 2005 John Ellis <johne@verizon.net>
gqview
parents:
9
diff
changeset
|
398 { |
dcc04a6a58bf
Sat Apr 16 12:29:42 2005 John Ellis <johne@verizon.net>
gqview
parents:
9
diff
changeset
|
399 struct stat st; |
442 | 400 |
1198 | 401 return (lstat_utf8(s, &st) && S_ISLNK(st.st_mode)); |
40
dcc04a6a58bf
Sat Apr 16 12:29:42 2005 John Ellis <johne@verizon.net>
gqview
parents:
9
diff
changeset
|
402 } |
dcc04a6a58bf
Sat Apr 16 12:29:42 2005 John Ellis <johne@verizon.net>
gqview
parents:
9
diff
changeset
|
403 |
9 | 404 gint64 filesize(const gchar *s) |
405 { | |
406 struct stat st; | |
442 | 407 |
9 | 408 if (!stat_utf8(s, &st)) return 0; |
983 | 409 return st.st_size; |
9 | 410 } |
411 | |
412 time_t filetime(const gchar *s) | |
413 { | |
442 | 414 struct stat st; |
9 | 415 |
416 if (!stat_utf8(s, &st)) return 0; | |
417 return st.st_mtime; | |
418 } | |
419 | |
1448 | 420 gboolean filetime_set(const gchar *s, time_t tval) |
9 | 421 { |
1437 | 422 gboolean ret = FALSE; |
9 | 423 |
424 if (tval > 0) | |
425 { | |
426 struct utimbuf ut; | |
427 gchar *sl; | |
428 | |
429 ut.actime = ut.modtime = tval; | |
430 | |
431 sl = path_from_utf8(s); | |
432 ret = (utime(sl, &ut) == 0); | |
433 g_free(sl); | |
434 } | |
435 | |
436 return ret; | |
437 } | |
438 | |
1360
721ffb823d6e
Introduce is_readable_file() which test if file exists, is regular and readable.
zas_
parents:
1356
diff
changeset
|
439 gboolean is_readable_file(const gchar *s) |
721ffb823d6e
Introduce is_readable_file() which test if file exists, is regular and readable.
zas_
parents:
1356
diff
changeset
|
440 { |
721ffb823d6e
Introduce is_readable_file() which test if file exists, is regular and readable.
zas_
parents:
1356
diff
changeset
|
441 if (!s || !s[0] || !isfile(s)) return FALSE; |
721ffb823d6e
Introduce is_readable_file() which test if file exists, is regular and readable.
zas_
parents:
1356
diff
changeset
|
442 return access_file(s, R_OK); |
721ffb823d6e
Introduce is_readable_file() which test if file exists, is regular and readable.
zas_
parents:
1356
diff
changeset
|
443 } |
721ffb823d6e
Introduce is_readable_file() which test if file exists, is regular and readable.
zas_
parents:
1356
diff
changeset
|
444 |
1448 | 445 gboolean access_file(const gchar *s, gint mode) |
9 | 446 { |
447 gchar *sl; | |
448 gint ret; | |
449 | |
1356
681e79dd0820
Slightly modify access_file() and use it to test profile files existence and read access.
zas_
parents:
1307
diff
changeset
|
450 if (!s || !s[0]) return FALSE; |
9 | 451 |
452 sl = path_from_utf8(s); | |
453 ret = (access(sl, mode) == 0); | |
454 g_free(sl); | |
455 | |
456 return ret; | |
457 } | |
458 | |
1448 | 459 gboolean unlink_file(const gchar *s) |
9 | 460 { |
461 gchar *sl; | |
1448 | 462 gboolean ret; |
9 | 463 |
464 if (!s) return FALSE; | |
465 | |
466 sl = path_from_utf8(s); | |
467 ret = (unlink(sl) == 0); | |
468 g_free(sl); | |
469 | |
470 return ret; | |
471 } | |
472 | |
1448 | 473 gboolean symlink_utf8(const gchar *source, const gchar *target) |
9 | 474 { |
475 gchar *sl; | |
476 gchar *tl; | |
1448 | 477 gboolean ret; |
9 | 478 |
479 if (!source || !target) return FALSE; | |
480 | |
481 sl = path_from_utf8(source); | |
482 tl = path_from_utf8(target); | |
483 | |
484 ret = (symlink(sl, tl) == 0); | |
485 | |
486 g_free(sl); | |
487 g_free(tl); | |
488 | |
489 return ret; | |
490 } | |
491 | |
1448 | 492 gboolean mkdir_utf8(const gchar *s, gint mode) |
9 | 493 { |
494 gchar *sl; | |
1448 | 495 gboolean ret; |
9 | 496 |
497 if (!s) return FALSE; | |
498 | |
499 sl = path_from_utf8(s); | |
500 ret = (mkdir(sl, mode) == 0); | |
501 g_free(sl); | |
502 return ret; | |
503 } | |
504 | |
1448 | 505 gboolean rmdir_utf8(const gchar *s) |
9 | 506 { |
507 gchar *sl; | |
1448 | 508 gboolean ret; |
9 | 509 |
510 if (!s) return FALSE; | |
511 | |
512 sl = path_from_utf8(s); | |
513 ret = (rmdir(sl) == 0); | |
514 g_free(sl); | |
515 | |
516 return ret; | |
517 } | |
518 | |
1448 | 519 gboolean copy_file_attributes(const gchar *s, const gchar *t, gint perms, gint mtime) |
9 | 520 { |
521 struct stat st; | |
522 gchar *sl, *tl; | |
1437 | 523 gboolean ret = FALSE; |
9 | 524 |
525 if (!s || !t) return FALSE; | |
526 | |
527 sl = path_from_utf8(s); | |
528 tl = path_from_utf8(t); | |
529 | |
530 if (stat(sl, &st) == 0) | |
531 { | |
532 struct utimbuf tb; | |
533 | |
534 ret = TRUE; | |
535 | |
536 /* set the dest file attributes to that of source (ignoring errors) */ | |
537 | |
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; | |
540 | |
541 tb.actime = st.st_atime; | |
542 tb.modtime = st.st_mtime; | |
543 if (mtime && utime(tl, &tb) < 0) ret = FALSE; | |
544 } | |
545 | |
546 g_free(sl); | |
547 g_free(tl); | |
548 | |
549 return ret; | |
550 } | |
551 | |
552 /* paths are in filesystem encoding */ | |
1448 | 553 static gboolean hard_linked(const gchar *a, const gchar *b) |
9 | 554 { |
555 struct stat sta; | |
556 struct stat stb; | |
557 | |
558 if (stat(a, &sta) != 0 || stat(b, &stb) != 0) return FALSE; | |
559 | |
560 return (sta.st_dev == stb.st_dev && | |
561 sta.st_ino == stb.st_ino); | |
562 } | |
563 | |
1448 | 564 gboolean copy_file(const gchar *s, const gchar *t) |
9 | 565 { |
566 FILE *fi = NULL; | |
567 FILE *fo = NULL; | |
568 gchar *sl, *tl; | |
569 gchar buf[4096]; | |
704
839525c4e85f
Use size_t instead of gint, it silents a signed vs unsigned warning.
zas_
parents:
703
diff
changeset
|
570 size_t b; |
9 | 571 |
572 sl = path_from_utf8(s); | |
573 tl = path_from_utf8(t); | |
574 | |
575 if (hard_linked(sl, tl)) | |
576 { | |
577 g_free(sl); | |
578 g_free(tl); | |
579 return TRUE; | |
580 } | |
581 | |
582 fi = fopen(sl, "rb"); | |
583 if (fi) | |
584 { | |
585 fo = fopen(tl, "wb"); | |
586 if (!fo) | |
587 { | |
588 fclose(fi); | |
589 fi = NULL; | |
590 } | |
591 } | |
592 | |
593 g_free(sl); | |
594 g_free(tl); | |
595 | |
596 if (!fi || !fo) return FALSE; | |
597 | |
1000
4fe8f9656107
For the sake of consistency, use glib basic types everywhere.
zas_
parents:
998
diff
changeset
|
598 while ((b = fread(buf, sizeof(gchar), sizeof(buf), fi)) && b != 0) |
9 | 599 { |
1000
4fe8f9656107
For the sake of consistency, use glib basic types everywhere.
zas_
parents:
998
diff
changeset
|
600 if (fwrite(buf, sizeof(gchar), b, fo) != b) |
9 | 601 { |
602 fclose(fi); | |
603 fclose(fo); | |
604 return FALSE; | |
605 } | |
606 } | |
607 | |
608 fclose(fi); | |
609 fclose(fo); | |
610 | |
611 copy_file_attributes(s, t, TRUE, TRUE); | |
612 | |
613 return TRUE; | |
614 } | |
615 | |
1448 | 616 gboolean move_file(const gchar *s, const gchar *t) |
9 | 617 { |
618 gchar *sl, *tl; | |
1437 | 619 gboolean ret = TRUE; |
9 | 620 |
621 if (!s || !t) return FALSE; | |
622 | |
623 sl = path_from_utf8(s); | |
624 tl = path_from_utf8(t); | |
625 if (rename(sl, tl) < 0) | |
626 { | |
627 /* this may have failed because moving a file across filesystems | |
628 was attempted, so try copy and delete instead */ | |
629 if (copy_file(s, t)) | |
630 { | |
631 if (unlink(sl) < 0) | |
632 { | |
633 /* err, now we can't delete the source file so return FALSE */ | |
634 ret = FALSE; | |
635 } | |
636 } | |
637 else | |
638 { | |
639 ret = FALSE; | |
640 } | |
641 } | |
642 g_free(sl); | |
643 g_free(tl); | |
644 | |
645 return ret; | |
646 } | |
647 | |
1448 | 648 gboolean rename_file(const gchar *s, const gchar *t) |
9 | 649 { |
650 gchar *sl, *tl; | |
1448 | 651 gboolean ret; |
9 | 652 |
653 if (!s || !t) return FALSE; | |
654 | |
655 sl = path_from_utf8(s); | |
656 tl = path_from_utf8(t); | |
657 ret = (rename(sl, tl) == 0); | |
658 g_free(sl); | |
659 g_free(tl); | |
660 | |
661 return ret; | |
662 } | |
663 | |
664 gchar *get_current_dir(void) | |
665 { | |
666 gchar *pathl; | |
667 gchar *path8; | |
668 | |
669 pathl = g_get_current_dir(); | |
670 path8 = path_to_utf8(pathl); | |
671 g_free(pathl); | |
672 | |
673 return path8; | |
674 } | |
675 | |
576
9dc0513837b5
dropped path_list functions, use filelist functions everywhere
nadvornik
parents:
545
diff
changeset
|
676 void string_list_free(GList *list) |
9 | 677 { |
678 g_list_foreach(list, (GFunc)g_free, NULL); | |
679 g_list_free(list); | |
680 } | |
681 | |
1238
947e603a52c6
simplified metadata interface, dropped metadata_read,
nadvornik
parents:
1229
diff
changeset
|
682 GList *string_list_copy(const GList *list) |
9 | 683 { |
684 GList *new_list = NULL; | |
1249 | 685 GList *work = (GList *) list; |
9 | 686 |
687 while (work) | |
688 { | |
689 gchar *path; | |
442 | 690 |
9 | 691 path = work->data; |
692 work = work->next; | |
442 | 693 |
9 | 694 new_list = g_list_prepend(new_list, g_strdup(path)); |
695 } | |
442 | 696 |
9 | 697 return g_list_reverse(new_list); |
698 } | |
699 | |
1448 | 700 gchar *unique_filename(const gchar *path, const gchar *ext, const gchar *divider, gboolean pad) |
9 | 701 { |
702 gchar *unique; | |
703 gint n = 1; | |
704 | |
705 if (!ext) ext = ""; | |
706 if (!divider) divider = ""; | |
707 | |
708 unique = g_strconcat(path, ext, NULL); | |
709 while (isname(unique)) | |
710 { | |
711 g_free(unique); | |
712 if (pad) | |
713 { | |
714 unique = g_strdup_printf("%s%s%03d%s", path, divider, n, ext); | |
715 } | |
716 else | |
717 { | |
718 unique = g_strdup_printf("%s%s%d%s", path, divider, n, ext); | |
719 } | |
720 n++; | |
721 if (n > 999) | |
722 { | |
723 /* well, we tried */ | |
724 g_free(unique); | |
725 return NULL; | |
726 } | |
727 } | |
728 | |
729 return unique; | |
730 } | |
731 | |
732 gchar *unique_filename_simple(const gchar *path) | |
733 { | |
734 gchar *unique; | |
735 const gchar *name; | |
736 const gchar *ext; | |
737 | |
738 if (!path) return NULL; | |
739 | |
740 name = filename_from_path(path); | |
741 if (!name) return NULL; | |
742 | |
743 ext = extension_from_path(name); | |
744 | |
745 if (!ext) | |
746 { | |
747 unique = unique_filename(path, NULL, "_", TRUE); | |
748 } | |
749 else | |
750 { | |
751 gchar *base; | |
752 | |
753 base = remove_extension_from_path(path); | |
754 unique = unique_filename(base, ext, "_", TRUE); | |
755 g_free(base); | |
756 } | |
757 | |
758 return unique; | |
759 } | |
760 | |
761 const gchar *filename_from_path(const gchar *path) | |
762 { | |
763 const gchar *base; | |
764 | |
765 if (!path) return NULL; | |
766 | |
701 | 767 base = strrchr(path, G_DIR_SEPARATOR); |
9 | 768 if (base) return base + 1; |
769 | |
770 return path; | |
771 } | |
772 | |
773 gchar *remove_level_from_path(const gchar *path) | |
774 { | |
543 | 775 gint p = 0, n = -1; |
9 | 776 |
777 if (!path) return NULL; | |
778 | |
543 | 779 while (path[p]) |
780 { | |
701 | 781 if (path[p] == G_DIR_SEPARATOR) n = p; |
543 | 782 p++; |
783 } | |
784 if (n <= 0) n++; | |
785 | |
786 return g_strndup(path, (gsize) n); | |
9 | 787 } |
788 | |
789 const gchar *extension_from_path(const gchar *path) | |
790 { | |
791 if (!path) return NULL; | |
792 return strrchr(path, '.'); | |
793 } | |
794 | |
1448 | 795 gboolean file_extension_match(const gchar *path, const gchar *ext) |
9 | 796 { |
797 gint p; | |
798 gint e; | |
799 | |
800 if (!path) return FALSE; | |
801 if (!ext) return TRUE; | |
802 | |
803 p = strlen(path); | |
804 e = strlen(ext); | |
805 | |
605
651ae2be1031
Use g_ascii_strncasecmp() instead of strncasecmp() where applicable.
zas_
parents:
576
diff
changeset
|
806 /* FIXME: utf8 */ |
1307 | 807 return (p > e && g_ascii_strncasecmp(path + p - e, ext, e) == 0); |
9 | 808 } |
809 | |
810 gchar *remove_extension_from_path(const gchar *path) | |
811 { | |
544 | 812 gint p = 0, n = -1; |
9 | 813 |
814 if (!path) return NULL; | |
815 | |
544 | 816 while (path[p]) |
817 { | |
818 if (path[p] == '.') n = p; | |
819 p++; | |
820 } | |
821 if (n < 0) n = p; | |
822 | |
823 return g_strndup(path, (gsize) n); | |
9 | 824 } |
825 | |
826 void parse_out_relatives(gchar *path) | |
827 { | |
828 gint s, t; | |
829 | |
830 if (!path) return; | |
831 | |
832 s = t = 0; | |
833 | |
834 while (path[s] != '\0') | |
835 { | |
919 | 836 if (path[s] == G_DIR_SEPARATOR && path[s+1] == '.') |
9 | 837 { |
919 | 838 /* /. occurence, let's see more */ |
839 gint p = s + 2; | |
840 | |
841 if (path[p] == G_DIR_SEPARATOR || path[p] == '\0') | |
842 { | |
843 /* /./ or /., just skip this part */ | |
844 s = p; | |
845 continue; | |
846 } | |
847 else if (path[p] == '.' && (path[p+1] == G_DIR_SEPARATOR || path[p+1] == '\0')) | |
848 { | |
849 /* /../ or /.., remove previous part, ie. /a/b/../ becomes /a/ */ | |
850 s = p + 1; | |
851 if (t > 0) t--; | |
852 while (path[t] != G_DIR_SEPARATOR && t > 0) t--; | |
853 continue; | |
854 } | |
9 | 855 } |
919 | 856 |
857 if (s != t) path[t] = path[s]; | |
858 t++; | |
859 s++; | |
9 | 860 } |
919 | 861 |
701 | 862 if (t == 0 && path[t] == G_DIR_SEPARATOR) t++; |
863 if (t > 1 && path[t-1] == G_DIR_SEPARATOR) t--; | |
9 | 864 path[t] = '\0'; |
865 } | |
866 | |
1448 | 867 gboolean file_in_path(const gchar *name) |
9 | 868 { |
869 gchar *path; | |
870 gchar *namel; | |
871 gint p, l; | |
1437 | 872 gboolean ret = FALSE; |
9 | 873 |
874 if (!name) return FALSE; | |
875 path = g_strdup(getenv("PATH")); | |
876 if (!path) return FALSE; | |
877 namel = path_from_utf8(name); | |
878 | |
879 p = 0; | |
880 l = strlen(path); | |
881 while (p < l && !ret) | |
882 { | |
883 gchar *f; | |
884 gint e = p; | |
885 while (path[e] != ':' && path[e] != '\0') e++; | |
886 path[e] = '\0'; | |
887 e++; | |
703 | 888 f = g_build_filename(path + p, namel, NULL); |
9 | 889 if (isfile(f)) ret = TRUE; |
890 g_free(f); | |
891 p = e; | |
892 } | |
893 g_free(namel); | |
894 g_free(path); | |
895 | |
896 return ret; | |
897 } | |
1148
95860439070b
Replace cache_ensure_dir_exists() by new recursive_mkdir_if_not_exists().
zas_
parents:
1145
diff
changeset
|
898 |
1164 | 899 gboolean recursive_mkdir_if_not_exists(const gchar *path, mode_t mode) |
1148
95860439070b
Replace cache_ensure_dir_exists() by new recursive_mkdir_if_not_exists().
zas_
parents:
1145
diff
changeset
|
900 { |
95860439070b
Replace cache_ensure_dir_exists() by new recursive_mkdir_if_not_exists().
zas_
parents:
1145
diff
changeset
|
901 if (!path) return FALSE; |
95860439070b
Replace cache_ensure_dir_exists() by new recursive_mkdir_if_not_exists().
zas_
parents:
1145
diff
changeset
|
902 |
95860439070b
Replace cache_ensure_dir_exists() by new recursive_mkdir_if_not_exists().
zas_
parents:
1145
diff
changeset
|
903 if (!isdir(path)) |
95860439070b
Replace cache_ensure_dir_exists() by new recursive_mkdir_if_not_exists().
zas_
parents:
1145
diff
changeset
|
904 { |
95860439070b
Replace cache_ensure_dir_exists() by new recursive_mkdir_if_not_exists().
zas_
parents:
1145
diff
changeset
|
905 gchar *npath = g_strdup(path); |
95860439070b
Replace cache_ensure_dir_exists() by new recursive_mkdir_if_not_exists().
zas_
parents:
1145
diff
changeset
|
906 gchar *p = npath; |
95860439070b
Replace cache_ensure_dir_exists() by new recursive_mkdir_if_not_exists().
zas_
parents:
1145
diff
changeset
|
907 |
95860439070b
Replace cache_ensure_dir_exists() by new recursive_mkdir_if_not_exists().
zas_
parents:
1145
diff
changeset
|
908 while (p[0] != '\0') |
95860439070b
Replace cache_ensure_dir_exists() by new recursive_mkdir_if_not_exists().
zas_
parents:
1145
diff
changeset
|
909 { |
95860439070b
Replace cache_ensure_dir_exists() by new recursive_mkdir_if_not_exists().
zas_
parents:
1145
diff
changeset
|
910 p++; |
95860439070b
Replace cache_ensure_dir_exists() by new recursive_mkdir_if_not_exists().
zas_
parents:
1145
diff
changeset
|
911 if (p[0] == G_DIR_SEPARATOR || p[0] == '\0') |
95860439070b
Replace cache_ensure_dir_exists() by new recursive_mkdir_if_not_exists().
zas_
parents:
1145
diff
changeset
|
912 { |
1437 | 913 gboolean end = TRUE; |
1148
95860439070b
Replace cache_ensure_dir_exists() by new recursive_mkdir_if_not_exists().
zas_
parents:
1145
diff
changeset
|
914 |
95860439070b
Replace cache_ensure_dir_exists() by new recursive_mkdir_if_not_exists().
zas_
parents:
1145
diff
changeset
|
915 if (p[0] != '\0') |
95860439070b
Replace cache_ensure_dir_exists() by new recursive_mkdir_if_not_exists().
zas_
parents:
1145
diff
changeset
|
916 { |
95860439070b
Replace cache_ensure_dir_exists() by new recursive_mkdir_if_not_exists().
zas_
parents:
1145
diff
changeset
|
917 p[0] = '\0'; |
95860439070b
Replace cache_ensure_dir_exists() by new recursive_mkdir_if_not_exists().
zas_
parents:
1145
diff
changeset
|
918 end = FALSE; |
95860439070b
Replace cache_ensure_dir_exists() by new recursive_mkdir_if_not_exists().
zas_
parents:
1145
diff
changeset
|
919 } |
95860439070b
Replace cache_ensure_dir_exists() by new recursive_mkdir_if_not_exists().
zas_
parents:
1145
diff
changeset
|
920 |
95860439070b
Replace cache_ensure_dir_exists() by new recursive_mkdir_if_not_exists().
zas_
parents:
1145
diff
changeset
|
921 if (!isdir(npath)) |
95860439070b
Replace cache_ensure_dir_exists() by new recursive_mkdir_if_not_exists().
zas_
parents:
1145
diff
changeset
|
922 { |
95860439070b
Replace cache_ensure_dir_exists() by new recursive_mkdir_if_not_exists().
zas_
parents:
1145
diff
changeset
|
923 DEBUG_1("creating sub dir:%s", npath); |
95860439070b
Replace cache_ensure_dir_exists() by new recursive_mkdir_if_not_exists().
zas_
parents:
1145
diff
changeset
|
924 if (!mkdir_utf8(npath, mode)) |
95860439070b
Replace cache_ensure_dir_exists() by new recursive_mkdir_if_not_exists().
zas_
parents:
1145
diff
changeset
|
925 { |
95860439070b
Replace cache_ensure_dir_exists() by new recursive_mkdir_if_not_exists().
zas_
parents:
1145
diff
changeset
|
926 log_printf("create dir failed: %s\n", npath); |
95860439070b
Replace cache_ensure_dir_exists() by new recursive_mkdir_if_not_exists().
zas_
parents:
1145
diff
changeset
|
927 g_free(npath); |
95860439070b
Replace cache_ensure_dir_exists() by new recursive_mkdir_if_not_exists().
zas_
parents:
1145
diff
changeset
|
928 return FALSE; |
95860439070b
Replace cache_ensure_dir_exists() by new recursive_mkdir_if_not_exists().
zas_
parents:
1145
diff
changeset
|
929 } |
95860439070b
Replace cache_ensure_dir_exists() by new recursive_mkdir_if_not_exists().
zas_
parents:
1145
diff
changeset
|
930 } |
95860439070b
Replace cache_ensure_dir_exists() by new recursive_mkdir_if_not_exists().
zas_
parents:
1145
diff
changeset
|
931 |
95860439070b
Replace cache_ensure_dir_exists() by new recursive_mkdir_if_not_exists().
zas_
parents:
1145
diff
changeset
|
932 if (!end) p[0] = G_DIR_SEPARATOR; |
95860439070b
Replace cache_ensure_dir_exists() by new recursive_mkdir_if_not_exists().
zas_
parents:
1145
diff
changeset
|
933 } |
95860439070b
Replace cache_ensure_dir_exists() by new recursive_mkdir_if_not_exists().
zas_
parents:
1145
diff
changeset
|
934 } |
95860439070b
Replace cache_ensure_dir_exists() by new recursive_mkdir_if_not_exists().
zas_
parents:
1145
diff
changeset
|
935 g_free(npath); |
95860439070b
Replace cache_ensure_dir_exists() by new recursive_mkdir_if_not_exists().
zas_
parents:
1145
diff
changeset
|
936 } |
95860439070b
Replace cache_ensure_dir_exists() by new recursive_mkdir_if_not_exists().
zas_
parents:
1145
diff
changeset
|
937 |
95860439070b
Replace cache_ensure_dir_exists() by new recursive_mkdir_if_not_exists().
zas_
parents:
1145
diff
changeset
|
938 return TRUE; |
95860439070b
Replace cache_ensure_dir_exists() by new recursive_mkdir_if_not_exists().
zas_
parents:
1145
diff
changeset
|
939 } |
95860439070b
Replace cache_ensure_dir_exists() by new recursive_mkdir_if_not_exists().
zas_
parents:
1145
diff
changeset
|
940 |
1642
51d70f62338c
Fix up compilation using make -jN: sometimes it fails due to order of headers inclusion.
zas_
parents:
1555
diff
changeset
|
941 /* does filename utf8 to filesystem encoding first */ |
51d70f62338c
Fix up compilation using make -jN: sometimes it fails due to order of headers inclusion.
zas_
parents:
1555
diff
changeset
|
942 gboolean md5_get_digest_from_file_utf8(const gchar *path, guchar digest[16]) |
51d70f62338c
Fix up compilation using make -jN: sometimes it fails due to order of headers inclusion.
zas_
parents:
1555
diff
changeset
|
943 { |
51d70f62338c
Fix up compilation using make -jN: sometimes it fails due to order of headers inclusion.
zas_
parents:
1555
diff
changeset
|
944 gboolean success; |
51d70f62338c
Fix up compilation using make -jN: sometimes it fails due to order of headers inclusion.
zas_
parents:
1555
diff
changeset
|
945 gchar *pathl; |
51d70f62338c
Fix up compilation using make -jN: sometimes it fails due to order of headers inclusion.
zas_
parents:
1555
diff
changeset
|
946 |
51d70f62338c
Fix up compilation using make -jN: sometimes it fails due to order of headers inclusion.
zas_
parents:
1555
diff
changeset
|
947 pathl = path_from_utf8(path); |
51d70f62338c
Fix up compilation using make -jN: sometimes it fails due to order of headers inclusion.
zas_
parents:
1555
diff
changeset
|
948 success = md5_get_digest_from_file(pathl, digest); |
51d70f62338c
Fix up compilation using make -jN: sometimes it fails due to order of headers inclusion.
zas_
parents:
1555
diff
changeset
|
949 g_free(pathl); |
51d70f62338c
Fix up compilation using make -jN: sometimes it fails due to order of headers inclusion.
zas_
parents:
1555
diff
changeset
|
950 |
51d70f62338c
Fix up compilation using make -jN: sometimes it fails due to order of headers inclusion.
zas_
parents:
1555
diff
changeset
|
951 return success; |
51d70f62338c
Fix up compilation using make -jN: sometimes it fails due to order of headers inclusion.
zas_
parents:
1555
diff
changeset
|
952 } |
51d70f62338c
Fix up compilation using make -jN: sometimes it fails due to order of headers inclusion.
zas_
parents:
1555
diff
changeset
|
953 |
51d70f62338c
Fix up compilation using make -jN: sometimes it fails due to order of headers inclusion.
zas_
parents:
1555
diff
changeset
|
954 |
51d70f62338c
Fix up compilation using make -jN: sometimes it fails due to order of headers inclusion.
zas_
parents:
1555
diff
changeset
|
955 gchar *md5_text_from_file_utf8(const gchar *path, const gchar *error_text) |
51d70f62338c
Fix up compilation using make -jN: sometimes it fails due to order of headers inclusion.
zas_
parents:
1555
diff
changeset
|
956 { |
51d70f62338c
Fix up compilation using make -jN: sometimes it fails due to order of headers inclusion.
zas_
parents:
1555
diff
changeset
|
957 guchar digest[16]; |
51d70f62338c
Fix up compilation using make -jN: sometimes it fails due to order of headers inclusion.
zas_
parents:
1555
diff
changeset
|
958 |
51d70f62338c
Fix up compilation using make -jN: sometimes it fails due to order of headers inclusion.
zas_
parents:
1555
diff
changeset
|
959 if (!md5_get_digest_from_file_utf8(path, digest)) return g_strdup(error_text); |
51d70f62338c
Fix up compilation using make -jN: sometimes it fails due to order of headers inclusion.
zas_
parents:
1555
diff
changeset
|
960 |
51d70f62338c
Fix up compilation using make -jN: sometimes it fails due to order of headers inclusion.
zas_
parents:
1555
diff
changeset
|
961 return md5_digest_to_text(digest); |
51d70f62338c
Fix up compilation using make -jN: sometimes it fails due to order of headers inclusion.
zas_
parents:
1555
diff
changeset
|
962 } |
1148
95860439070b
Replace cache_ensure_dir_exists() by new recursive_mkdir_if_not_exists().
zas_
parents:
1145
diff
changeset
|
963 |
95860439070b
Replace cache_ensure_dir_exists() by new recursive_mkdir_if_not_exists().
zas_
parents:
1145
diff
changeset
|
964 |
1055
1646720364cf
Adding a vim modeline to all files - patch by Klaus Ethgen
nadvornik
parents:
1000
diff
changeset
|
965 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */ |