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