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