Mercurial > geeqie.yaz
annotate src/filedata.c @ 1706:60f138e3ff81
save one unnecessary call to stat_utf8
author | nadvornik |
---|---|
date | Wed, 26 Aug 2009 19:16:02 +0000 |
parents | f80ee95314dd |
children | 8213f95b65c9 |
rev | line source |
---|---|
586 | 1 /* |
2 * Geeqie | |
3 * (C) 2006 John Ellis | |
1284 | 4 * Copyright (C) 2008 - 2009 The Geeqie Team |
586 | 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 | |
14 #include "main.h" | |
15 #include "filedata.h" | |
16 | |
17 #include "filefilter.h" | |
18 #include "cache.h" | |
19 #include "thumb_standard.h" | |
20 #include "ui_fileops.h" | |
1205
3ff2aa99108b
use the workflow in utilops.c for metadata approving and writting
nadvornik
parents:
1055
diff
changeset
|
21 #include "metadata.h" |
1212 | 22 #include "trash.h" |
1439 | 23 #include "histogram.h" |
586 | 24 |
25 | |
785 | 26 static GHashTable *file_data_pool = NULL; |
899
5d9c0b4e6d5f
fixed the case when a renamed file is detected by directory scanning
nadvornik
parents:
896
diff
changeset
|
27 static GHashTable *file_data_planned_change_hash = NULL; |
1518 | 28 static GHashTable *file_data_basename_hash = NULL; |
785 | 29 |
586 | 30 static gint sidecar_file_priority(const gchar *path); |
1701 | 31 static FileData *file_data_new_local(const gchar *path, struct stat *st, gboolean check_sidecars, gboolean stat_sidecars); |
586 | 32 |
33 | |
34 /* | |
35 *----------------------------------------------------------------------------- | |
36 * text conversion utils | |
37 *----------------------------------------------------------------------------- | |
38 */ | |
39 | |
40 gchar *text_from_size(gint64 size) | |
41 { | |
42 gchar *a, *b; | |
43 gchar *s, *d; | |
44 gint l, n, i; | |
45 | |
46 /* what I would like to use is printf("%'d", size) | |
47 * BUT: not supported on every libc :( | |
48 */ | |
49 if (size > G_MAXUINT) | |
50 { | |
51 /* the %lld conversion is not valid in all libcs, so use a simple work-around */ | |
52 a = g_strdup_printf("%d%09d", (guint)(size / 1000000000), (guint)(size % 1000000000)); | |
53 } | |
54 else | |
55 { | |
56 a = g_strdup_printf("%d", (guint)size); | |
57 } | |
58 l = strlen(a); | |
59 n = (l - 1)/ 3; | |
60 if (n < 1) return a; | |
61 | |
62 b = g_new(gchar, l + n + 1); | |
63 | |
64 s = a; | |
65 d = b; | |
66 i = l - n * 3; | |
67 while (*s != '\0') | |
68 { | |
69 if (i < 1) | |
70 { | |
71 i = 3; | |
72 *d = ','; | |
73 d++; | |
74 } | |
75 | |
76 *d = *s; | |
77 s++; | |
78 d++; | |
79 i--; | |
80 } | |
81 *d = '\0'; | |
82 | |
83 g_free(a); | |
84 return b; | |
85 } | |
86 | |
87 gchar *text_from_size_abrev(gint64 size) | |
88 { | |
89 if (size < (gint64)1024) | |
90 { | |
91 return g_strdup_printf(_("%d bytes"), (gint)size); | |
92 } | |
93 if (size < (gint64)1048576) | |
94 { | |
1000
4fe8f9656107
For the sake of consistency, use glib basic types everywhere.
zas_
parents:
998
diff
changeset
|
95 return g_strdup_printf(_("%.1f K"), (gdouble)size / 1024.0); |
586 | 96 } |
97 if (size < (gint64)1073741824) | |
98 { | |
1000
4fe8f9656107
For the sake of consistency, use glib basic types everywhere.
zas_
parents:
998
diff
changeset
|
99 return g_strdup_printf(_("%.1f MB"), (gdouble)size / 1048576.0); |
586 | 100 } |
101 | |
1000
4fe8f9656107
For the sake of consistency, use glib basic types everywhere.
zas_
parents:
998
diff
changeset
|
102 /* to avoid overflowing the gdouble, do division in two steps */ |
586 | 103 size /= 1048576; |
1000
4fe8f9656107
For the sake of consistency, use glib basic types everywhere.
zas_
parents:
998
diff
changeset
|
104 return g_strdup_printf(_("%.1f GB"), (gdouble)size / 1024.0); |
586 | 105 } |
106 | |
107 /* note: returned string is valid until next call to text_from_time() */ | |
108 const gchar *text_from_time(time_t t) | |
109 { | |
110 static gchar *ret = NULL; | |
111 gchar buf[128]; | |
112 gint buflen; | |
113 struct tm *btime; | |
114 GError *error = NULL; | |
115 | |
116 btime = localtime(&t); | |
117 | |
118 /* the %x warning about 2 digit years is not an error */ | |
119 buflen = strftime(buf, sizeof(buf), "%x %H:%M", btime); | |
120 if (buflen < 1) return ""; | |
121 | |
122 g_free(ret); | |
123 ret = g_locale_to_utf8(buf, buflen, NULL, NULL, &error); | |
124 if (error) | |
125 { | |
673
fbebf5cf4a55
Do not use printf() directly but use new wrapper function log_printf() instead.
zas_
parents:
671
diff
changeset
|
126 log_printf("Error converting locale strftime to UTF-8: %s\n", error->message); |
586 | 127 g_error_free(error); |
128 return ""; | |
129 } | |
130 | |
131 return ret; | |
132 } | |
133 | |
134 /* | |
135 *----------------------------------------------------------------------------- | |
136 * file info struct | |
137 *----------------------------------------------------------------------------- | |
138 */ | |
139 | |
140 FileData *file_data_merge_sidecar_files(FileData *target, FileData *source); | |
1518 | 141 static void file_data_check_sidecars(FileData *fd, gboolean stat_sidecars); |
586 | 142 FileData *file_data_disconnect_sidecar_file(FileData *target, FileData *sfd); |
143 | |
144 | |
763
81f9e8dbb4bf
improved infrastructure for tracing changes, optimized vflist_populate_view
nadvornik
parents:
753
diff
changeset
|
145 void file_data_increment_version(FileData *fd) |
81f9e8dbb4bf
improved infrastructure for tracing changes, optimized vflist_populate_view
nadvornik
parents:
753
diff
changeset
|
146 { |
81f9e8dbb4bf
improved infrastructure for tracing changes, optimized vflist_populate_view
nadvornik
parents:
753
diff
changeset
|
147 fd->version++; |
1227 | 148 fd->valid_marks = 0; |
149 if (fd->parent) | |
150 { | |
151 fd->parent->version++; | |
152 fd->parent->valid_marks = 0; | |
153 } | |
763
81f9e8dbb4bf
improved infrastructure for tracing changes, optimized vflist_populate_view
nadvornik
parents:
753
diff
changeset
|
154 } |
81f9e8dbb4bf
improved infrastructure for tracing changes, optimized vflist_populate_view
nadvornik
parents:
753
diff
changeset
|
155 |
1701 | 156 static gint file_data_sort_by_ext(gconstpointer a, gconstpointer b) |
157 { | |
158 const FileData *fda = a; | |
159 const FileData *fdb = b; | |
160 | |
161 return strcmp(fdb->extension, fda->extension); | |
162 } | |
163 | |
1518 | 164 static void file_data_basename_hash_insert(FileData *fd) |
165 { | |
166 GList *list; | |
167 const gchar *ext = extension_from_path(fd->path); | |
168 gchar *basename = ext ? g_strndup(fd->path, ext - fd->path) : g_strdup(fd->path); | |
169 if (!file_data_basename_hash) | |
170 file_data_basename_hash = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL); | |
171 | |
172 list = g_hash_table_lookup(file_data_basename_hash, basename); | |
173 | |
174 if (!g_list_find(list, fd)) | |
175 { | |
1701 | 176 list = g_list_insert_sorted(list, fd, file_data_sort_by_ext); |
1518 | 177 g_hash_table_insert(file_data_basename_hash, basename, list); |
178 } | |
179 else | |
180 { | |
181 g_free(basename); | |
182 } | |
183 } | |
184 | |
185 static void file_data_basename_hash_remove(FileData *fd) | |
186 { | |
187 GList *list; | |
188 const gchar *ext = extension_from_path(fd->path); | |
189 gchar *basename = ext ? g_strndup(fd->path, ext - fd->path) : g_strdup(fd->path); | |
190 if (!file_data_basename_hash) | |
191 file_data_basename_hash = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL); | |
192 | |
193 list = g_hash_table_lookup(file_data_basename_hash, basename); | |
194 | |
195 list = g_list_remove(list, fd); | |
196 | |
197 if (list) | |
198 { | |
199 g_hash_table_insert(file_data_basename_hash, basename, list); | |
200 } | |
201 else | |
202 { | |
203 g_hash_table_remove(file_data_basename_hash, basename); | |
204 g_free(basename); | |
205 } | |
206 } | |
207 | |
785 | 208 static void file_data_set_collate_keys(FileData *fd) |
209 { | |
210 gchar *caseless_name; | |
1007
4303ee1e88ec
Removed converting fd->name to utf8 from file_data_set_collate_keys(), because fd->name is utf8.
bruclik
parents:
1002
diff
changeset
|
211 |
4303ee1e88ec
Removed converting fd->name to utf8 from file_data_set_collate_keys(), because fd->name is utf8.
bruclik
parents:
1002
diff
changeset
|
212 caseless_name = g_utf8_casefold(fd->name, -1); |
785 | 213 |
214 g_free(fd->collate_key_name); | |
215 g_free(fd->collate_key_name_nocase); | |
216 | |
217 #if GLIB_CHECK_VERSION(2, 8, 0) | |
1007
4303ee1e88ec
Removed converting fd->name to utf8 from file_data_set_collate_keys(), because fd->name is utf8.
bruclik
parents:
1002
diff
changeset
|
218 fd->collate_key_name = g_utf8_collate_key_for_filename(fd->name, -1); |
785 | 219 fd->collate_key_name_nocase = g_utf8_collate_key_for_filename(caseless_name, -1); |
220 #else | |
1007
4303ee1e88ec
Removed converting fd->name to utf8 from file_data_set_collate_keys(), because fd->name is utf8.
bruclik
parents:
1002
diff
changeset
|
221 fd->collate_key_name = g_utf8_collate_key(fd->name, -1); |
785 | 222 fd->collate_key_name_nocase = g_utf8_collate_key(caseless_name, -1); |
223 #endif | |
224 g_free(caseless_name); | |
225 } | |
763
81f9e8dbb4bf
improved infrastructure for tracing changes, optimized vflist_populate_view
nadvornik
parents:
753
diff
changeset
|
226 |
586 | 227 static void file_data_set_path(FileData *fd, const gchar *path) |
228 { | |
790 | 229 g_assert(path /* && *path*/); /* view_dir_tree uses FileData with zero length path */ |
785 | 230 g_assert(file_data_pool); |
231 | |
1518 | 232 if (fd->path) file_data_basename_hash_remove(fd); |
233 | |
785 | 234 g_free(fd->path); |
235 | |
236 if (fd->original_path) | |
237 { | |
238 g_hash_table_remove(file_data_pool, fd->original_path); | |
239 g_free(fd->original_path); | |
240 } | |
915 | 241 |
242 g_assert(!g_hash_table_lookup(file_data_pool, path)); | |
243 | |
785 | 244 fd->original_path = g_strdup(path); |
245 g_hash_table_insert(file_data_pool, fd->original_path, fd); | |
586 | 246 |
725 | 247 if (strcmp(path, G_DIR_SEPARATOR_S) == 0) |
586 | 248 { |
249 fd->path = g_strdup(path); | |
250 fd->name = fd->path; | |
251 fd->extension = fd->name + 1; | |
785 | 252 file_data_set_collate_keys(fd); |
586 | 253 return; |
254 } | |
255 | |
256 fd->path = g_strdup(path); | |
257 fd->name = filename_from_path(fd->path); | |
258 | |
259 if (strcmp(fd->name, "..") == 0) | |
260 { | |
261 gchar *dir = remove_level_from_path(path); | |
262 g_free(fd->path); | |
263 fd->path = remove_level_from_path(dir); | |
264 g_free(dir); | |
265 fd->name = ".."; | |
266 fd->extension = fd->name + 2; | |
785 | 267 file_data_set_collate_keys(fd); |
586 | 268 return; |
269 } | |
270 else if (strcmp(fd->name, ".") == 0) | |
271 { | |
272 g_free(fd->path); | |
273 fd->path = remove_level_from_path(path); | |
274 fd->name = "."; | |
275 fd->extension = fd->name + 1; | |
785 | 276 file_data_set_collate_keys(fd); |
586 | 277 return; |
278 } | |
279 | |
280 fd->extension = extension_from_path(fd->path); | |
281 if (fd->extension == NULL) | |
1422 | 282 { |
586 | 283 fd->extension = fd->name + strlen(fd->name); |
1422 | 284 } |
785 | 285 |
1518 | 286 file_data_basename_hash_insert(fd); /* we can ignore the special cases above - they don't have extensions */ |
287 | |
785 | 288 file_data_set_collate_keys(fd); |
586 | 289 } |
290 | |
801 | 291 static gboolean file_data_check_changed_files_recursive(FileData *fd, struct stat *st) |
586 | 292 { |
801 | 293 gboolean ret = FALSE; |
586 | 294 GList *work; |
806 | 295 |
586 | 296 if (fd->size != st->st_size || |
297 fd->date != st->st_mtime) | |
298 { | |
299 fd->size = st->st_size; | |
300 fd->date = st->st_mtime; | |
945
fd84847c8231
speed-up of directory notification on deleting large number of files
nadvornik
parents:
942
diff
changeset
|
301 fd->mode = st->st_mode; |
845 | 302 if (fd->thumb_pixbuf) g_object_unref(fd->thumb_pixbuf); |
303 fd->thumb_pixbuf = NULL; | |
763
81f9e8dbb4bf
improved infrastructure for tracing changes, optimized vflist_populate_view
nadvornik
parents:
753
diff
changeset
|
304 file_data_increment_version(fd); |
1432 | 305 file_data_send_notification(fd, NOTIFY_REREAD); |
801 | 306 ret = TRUE; |
586 | 307 } |
308 | |
309 work = fd->sidecar_files; | |
310 while (work) | |
311 { | |
312 FileData *sfd = work->data; | |
313 struct stat st; | |
801 | 314 work = work->next; |
586 | 315 |
316 if (!stat_utf8(sfd->path, &st)) | |
317 { | |
801 | 318 fd->size = 0; |
319 fd->date = 0; | |
586 | 320 file_data_disconnect_sidecar_file(fd, sfd); |
801 | 321 ret = TRUE; |
322 continue; | |
586 | 323 } |
324 | |
801 | 325 ret |= file_data_check_changed_files_recursive(sfd, &st); |
586 | 326 } |
801 | 327 return ret; |
328 } | |
329 | |
330 | |
888 | 331 gboolean file_data_check_changed_files(FileData *fd) |
801 | 332 { |
333 gboolean ret = FALSE; | |
334 struct stat st; | |
806 | 335 |
801 | 336 if (fd->parent) fd = fd->parent; |
337 | |
338 if (!stat_utf8(fd->path, &st)) | |
339 { | |
806 | 340 GList *work; |
341 FileData *sfd = NULL; | |
342 | |
801 | 343 /* parent is missing, we have to rebuild whole group */ |
344 ret = TRUE; | |
345 fd->size = 0; | |
346 fd->date = 0; | |
806 | 347 |
348 work = fd->sidecar_files; | |
801 | 349 while (work) |
350 { | |
351 sfd = work->data; | |
352 work = work->next; | |
353 | |
354 file_data_disconnect_sidecar_file(fd, sfd); | |
355 } | |
1518 | 356 if (sfd) file_data_check_sidecars(sfd, FALSE); /* this will group the sidecars back together */ |
1432 | 357 file_data_send_notification(fd, NOTIFY_REREAD); |
801 | 358 } |
806 | 359 else |
360 { | |
801 | 361 ret |= file_data_check_changed_files_recursive(fd, &st); |
806 | 362 } |
363 | |
801 | 364 return ret; |
586 | 365 } |
366 | |
1518 | 367 static FileData *file_data_new(const gchar *path_utf8, struct stat *st, gboolean check_sidecars, gboolean stat_sidecars) |
586 | 368 { |
369 FileData *fd; | |
370 | |
1518 | 371 DEBUG_2("file_data_new: '%s' %d %d", path_utf8, check_sidecars, stat_sidecars); |
586 | 372 |
373 if (!file_data_pool) | |
374 file_data_pool = g_hash_table_new(g_str_hash, g_str_equal); | |
375 | |
918 | 376 fd = g_hash_table_lookup(file_data_pool, path_utf8); |
377 if (fd) | |
899
5d9c0b4e6d5f
fixed the case when a renamed file is detected by directory scanning
nadvornik
parents:
896
diff
changeset
|
378 { |
5d9c0b4e6d5f
fixed the case when a renamed file is detected by directory scanning
nadvornik
parents:
896
diff
changeset
|
379 file_data_ref(fd); |
5d9c0b4e6d5f
fixed the case when a renamed file is detected by directory scanning
nadvornik
parents:
896
diff
changeset
|
380 } |
5d9c0b4e6d5f
fixed the case when a renamed file is detected by directory scanning
nadvornik
parents:
896
diff
changeset
|
381 |
5d9c0b4e6d5f
fixed the case when a renamed file is detected by directory scanning
nadvornik
parents:
896
diff
changeset
|
382 if (!fd && file_data_planned_change_hash) |
5d9c0b4e6d5f
fixed the case when a renamed file is detected by directory scanning
nadvornik
parents:
896
diff
changeset
|
383 { |
918 | 384 fd = g_hash_table_lookup(file_data_planned_change_hash, path_utf8); |
385 if (fd) | |
899
5d9c0b4e6d5f
fixed the case when a renamed file is detected by directory scanning
nadvornik
parents:
896
diff
changeset
|
386 { |
5d9c0b4e6d5f
fixed the case when a renamed file is detected by directory scanning
nadvornik
parents:
896
diff
changeset
|
387 DEBUG_1("planned change: using %s -> %s", path_utf8, fd->path); |
5d9c0b4e6d5f
fixed the case when a renamed file is detected by directory scanning
nadvornik
parents:
896
diff
changeset
|
388 file_data_ref(fd); |
5d9c0b4e6d5f
fixed the case when a renamed file is detected by directory scanning
nadvornik
parents:
896
diff
changeset
|
389 file_data_apply_ci(fd); |
5d9c0b4e6d5f
fixed the case when a renamed file is detected by directory scanning
nadvornik
parents:
896
diff
changeset
|
390 } |
5d9c0b4e6d5f
fixed the case when a renamed file is detected by directory scanning
nadvornik
parents:
896
diff
changeset
|
391 } |
5d9c0b4e6d5f
fixed the case when a renamed file is detected by directory scanning
nadvornik
parents:
896
diff
changeset
|
392 |
586 | 393 if (fd) |
394 { | |
801 | 395 gboolean changed; |
892
2022112583d0
increase reference count before sending notification in file_data_new
nadvornik
parents:
888
diff
changeset
|
396 |
801 | 397 if (fd->parent) |
398 changed = file_data_check_changed_files(fd); | |
399 else | |
400 changed = file_data_check_changed_files_recursive(fd, st); | |
401 if (changed && check_sidecars && sidecar_file_priority(fd->extension)) | |
1518 | 402 file_data_check_sidecars(fd, stat_sidecars); |
801 | 403 DEBUG_2("file_data_pool hit: '%s' %s", fd->path, changed ? "(changed)" : ""); |
806 | 404 |
892
2022112583d0
increase reference count before sending notification in file_data_new
nadvornik
parents:
888
diff
changeset
|
405 return fd; |
586 | 406 } |
407 | |
408 fd = g_new0(FileData, 1); | |
785 | 409 |
586 | 410 fd->size = st->st_size; |
411 fd->date = st->st_mtime; | |
945
fd84847c8231
speed-up of directory notification on deleting large number of files
nadvornik
parents:
942
diff
changeset
|
412 fd->mode = st->st_mode; |
586 | 413 fd->ref = 1; |
414 fd->magick = 0x12345678; | |
415 | |
785 | 416 file_data_set_path(fd, path_utf8); /* set path, name, collate_key_*, original_path */ |
586 | 417 |
849
db68d673448f
added possibility to disable grouping of selected files
nadvornik
parents:
846
diff
changeset
|
418 if (check_sidecars) |
1518 | 419 file_data_check_sidecars(fd, stat_sidecars); |
785 | 420 |
586 | 421 return fd; |
422 } | |
423 | |
1701 | 424 /* extension must contain only ASCII characters */ |
425 static GList *check_case_insensitive_ext(gchar *path) | |
426 { | |
427 gchar *sl; | |
428 gchar *extl; | |
429 gint ext_len; | |
430 GList *list = NULL; | |
431 | |
432 sl = path_from_utf8(path); | |
433 | |
434 extl = strrchr(sl, '.'); | |
435 if (extl) | |
436 { | |
437 gint i, j; | |
438 extl++; /* the first char after . */ | |
439 ext_len = strlen(extl); | |
440 | |
441 for (i = 0; i < (1 << ext_len); i++) | |
442 { | |
443 struct stat st; | |
444 for (j = 0; j < ext_len; j++) | |
445 { | |
446 if (i & (1 << (ext_len - 1 - j))) | |
447 extl[j] = g_ascii_tolower(extl[j]); | |
448 else | |
449 extl[j] = g_ascii_toupper(extl[j]); | |
450 } | |
451 if (stat(sl, &st) == 0) | |
452 { | |
453 list = g_list_prepend(list, file_data_new_local(sl, &st, FALSE, FALSE)); | |
454 } | |
455 } | |
456 } | |
457 g_free(sl); | |
458 | |
459 return list; | |
460 } | |
461 | |
1518 | 462 static void file_data_check_sidecars(FileData *fd, gboolean stat_sidecars) |
586 | 463 { |
1000
4fe8f9656107
For the sake of consistency, use glib basic types everywhere.
zas_
parents:
998
diff
changeset
|
464 gint base_len; |
849
db68d673448f
added possibility to disable grouping of selected files
nadvornik
parents:
846
diff
changeset
|
465 GString *fname; |
586 | 466 FileData *parent_fd = NULL; |
849
db68d673448f
added possibility to disable grouping of selected files
nadvornik
parents:
846
diff
changeset
|
467 GList *work; |
1701 | 468 const GList *basename_list = NULL; |
469 GList *group_list = NULL; | |
849
db68d673448f
added possibility to disable grouping of selected files
nadvornik
parents:
846
diff
changeset
|
470 if (fd->disable_grouping || !sidecar_file_priority(fd->extension)) |
db68d673448f
added possibility to disable grouping of selected files
nadvornik
parents:
846
diff
changeset
|
471 return; |
db68d673448f
added possibility to disable grouping of selected files
nadvornik
parents:
846
diff
changeset
|
472 |
db68d673448f
added possibility to disable grouping of selected files
nadvornik
parents:
846
diff
changeset
|
473 base_len = fd->extension - fd->path; |
db68d673448f
added possibility to disable grouping of selected files
nadvornik
parents:
846
diff
changeset
|
474 fname = g_string_new_len(fd->path, base_len); |
1518 | 475 |
476 if (!stat_sidecars) | |
477 { | |
478 basename_list = g_hash_table_lookup(file_data_basename_hash, fname->str); | |
479 } | |
480 | |
1701 | 481 |
482 /* check for possible sidecar files; | |
483 the sidecar files created here are referenced only via fd->sidecar_files or fd->parent, | |
484 they have fd->ref set to 0 and file_data unref must chack and free them all together | |
485 (using fd->ref would cause loops and leaks) | |
486 */ | |
487 | |
488 /* find all possible sidecar files and order them according to sidecar_ext_get_list, | |
489 for case-only differences put lowercase first, | |
490 put the result to group_list | |
491 */ | |
849
db68d673448f
added possibility to disable grouping of selected files
nadvornik
parents:
846
diff
changeset
|
492 work = sidecar_ext_get_list(); |
586 | 493 while (work) |
494 { | |
806 | 495 gchar *ext = work->data; |
586 | 496 work = work->next; |
497 | |
1701 | 498 if (stat_sidecars) |
586 | 499 { |
1701 | 500 GList *new_list; |
501 g_string_truncate(fname, base_len); | |
502 g_string_append(fname, ext); | |
503 new_list = check_case_insensitive_ext(fname->str); | |
504 group_list = g_list_concat(group_list, new_list); | |
586 | 505 } |
506 else | |
507 { | |
1701 | 508 const GList *work2 = basename_list; |
509 | |
510 while (work2) | |
1518 | 511 { |
512 struct stat nst; | |
1701 | 513 FileData *sfd = work2->data; |
1518 | 514 |
1701 | 515 if (g_ascii_strcasecmp(ext, sfd->extension) == 0 && |
1706 | 516 (sfd == fd || stat_utf8(sfd->path, &nst))) |
517 /* basename list can contain deleted files, fd was recently stat'd by caller */ | |
1518 | 518 { |
1701 | 519 group_list = g_list_append(group_list, file_data_ref(sfd)); |
1518 | 520 } |
1701 | 521 work2 = work2->next; |
1518 | 522 } |
586 | 523 } |
1701 | 524 } |
525 g_string_free(fname, TRUE); | |
526 | |
527 /* process the group list - the first one is the parent file, others are sidecars */ | |
528 work = group_list; | |
529 while (work) | |
530 { | |
531 FileData *new_fd = work->data; | |
532 work = work->next; | |
533 | |
534 if (new_fd->disable_grouping) | |
535 { | |
536 file_data_unref(new_fd); | |
537 continue; | |
538 } | |
539 | |
540 new_fd->ref--; /* do not use ref here */ | |
586 | 541 |
542 if (!parent_fd) | |
543 parent_fd = new_fd; /* parent is the one with the highest prio, found first */ | |
544 else | |
545 file_data_merge_sidecar_files(parent_fd, new_fd); | |
546 } | |
1701 | 547 g_list_free(group_list); |
586 | 548 } |
549 | |
550 | |
1518 | 551 static FileData *file_data_new_local(const gchar *path, struct stat *st, gboolean check_sidecars, gboolean stat_sidecars) |
586 | 552 { |
553 gchar *path_utf8 = path_to_utf8(path); | |
1518 | 554 FileData *ret = file_data_new(path_utf8, st, check_sidecars, stat_sidecars); |
806 | 555 |
586 | 556 g_free(path_utf8); |
557 return ret; | |
558 } | |
559 | |
560 FileData *file_data_new_simple(const gchar *path_utf8) | |
561 { | |
562 struct stat st; | |
563 | |
564 if (!stat_utf8(path_utf8, &st)) | |
565 { | |
566 st.st_size = 0; | |
567 st.st_mtime = 0; | |
568 } | |
569 | |
1518 | 570 return file_data_new(path_utf8, &st, TRUE, TRUE); |
586 | 571 } |
572 | |
573 FileData *file_data_add_sidecar_file(FileData *target, FileData *sfd) | |
574 { | |
575 sfd->parent = target; | |
855 | 576 if (!g_list_find(target->sidecar_files, sfd)) |
586 | 577 target->sidecar_files = g_list_prepend(target->sidecar_files, sfd); |
801 | 578 file_data_increment_version(sfd); /* increments both sfd and target */ |
586 | 579 return target; |
580 } | |
581 | |
582 | |
583 FileData *file_data_merge_sidecar_files(FileData *target, FileData *source) | |
584 { | |
585 GList *work; | |
806 | 586 |
586 | 587 file_data_add_sidecar_file(target, source); |
588 | |
589 work = source->sidecar_files; | |
590 while (work) | |
591 { | |
592 FileData *sfd = work->data; | |
593 file_data_add_sidecar_file(target, sfd); | |
594 work = work->next; | |
595 } | |
596 | |
597 g_list_free(source->sidecar_files); | |
598 source->sidecar_files = NULL; | |
599 | |
600 target->sidecar_files = filelist_sort(target->sidecar_files, SORT_NAME, TRUE); | |
806 | 601 |
586 | 602 return target; |
603 } | |
604 | |
874
fa39a4d786ad
Increase debugging info in file_data_ref() and file_data_unref().
zas_
parents:
855
diff
changeset
|
605 #ifdef DEBUG_FILEDATA |
fa39a4d786ad
Increase debugging info in file_data_ref() and file_data_unref().
zas_
parents:
855
diff
changeset
|
606 FileData *file_data_ref_debug(const gchar *file, gint line, FileData *fd) |
fa39a4d786ad
Increase debugging info in file_data_ref() and file_data_unref().
zas_
parents:
855
diff
changeset
|
607 #else |
586 | 608 FileData *file_data_ref(FileData *fd) |
874
fa39a4d786ad
Increase debugging info in file_data_ref() and file_data_unref().
zas_
parents:
855
diff
changeset
|
609 #endif |
586 | 610 { |
611 if (fd == NULL) return NULL; | |
874
fa39a4d786ad
Increase debugging info in file_data_ref() and file_data_unref().
zas_
parents:
855
diff
changeset
|
612 #ifdef DEBUG_FILEDATA |
fa39a4d786ad
Increase debugging info in file_data_ref() and file_data_unref().
zas_
parents:
855
diff
changeset
|
613 if (fd->magick != 0x12345678) |
fa39a4d786ad
Increase debugging info in file_data_ref() and file_data_unref().
zas_
parents:
855
diff
changeset
|
614 DEBUG_0("fd magick mismatch at %s:%d", file, line); |
fa39a4d786ad
Increase debugging info in file_data_ref() and file_data_unref().
zas_
parents:
855
diff
changeset
|
615 #endif |
586 | 616 g_assert(fd->magick == 0x12345678); |
617 fd->ref++; | |
874
fa39a4d786ad
Increase debugging info in file_data_ref() and file_data_unref().
zas_
parents:
855
diff
changeset
|
618 |
fa39a4d786ad
Increase debugging info in file_data_ref() and file_data_unref().
zas_
parents:
855
diff
changeset
|
619 #ifdef DEBUG_FILEDATA |
fa39a4d786ad
Increase debugging info in file_data_ref() and file_data_unref().
zas_
parents:
855
diff
changeset
|
620 DEBUG_2("file_data_ref (%d): '%s' @ %s:%d", fd->ref, fd->path, file, line); |
fa39a4d786ad
Increase debugging info in file_data_ref() and file_data_unref().
zas_
parents:
855
diff
changeset
|
621 #else |
fa39a4d786ad
Increase debugging info in file_data_ref() and file_data_unref().
zas_
parents:
855
diff
changeset
|
622 DEBUG_2("file_data_ref (%d): '%s'", fd->ref, fd->path); |
fa39a4d786ad
Increase debugging info in file_data_ref() and file_data_unref().
zas_
parents:
855
diff
changeset
|
623 #endif |
586 | 624 return fd; |
625 } | |
626 | |
627 static void file_data_free(FileData *fd) | |
628 { | |
629 g_assert(fd->magick == 0x12345678); | |
630 g_assert(fd->ref == 0); | |
631 | |
1518 | 632 if (fd->path) file_data_basename_hash_remove(fd); |
586 | 633 g_hash_table_remove(file_data_pool, fd->original_path); |
634 | |
635 g_free(fd->path); | |
636 g_free(fd->original_path); | |
785 | 637 g_free(fd->collate_key_name); |
638 g_free(fd->collate_key_name_nocase); | |
845 | 639 if (fd->thumb_pixbuf) g_object_unref(fd->thumb_pixbuf); |
1439 | 640 histmap_free(fd->histmap); |
1294 | 641 |
586 | 642 g_assert(fd->sidecar_files == NULL); /* sidecar files must be freed before calling this */ |
643 | |
644 file_data_change_info_free(NULL, fd); | |
645 g_free(fd); | |
646 } | |
647 | |
874
fa39a4d786ad
Increase debugging info in file_data_ref() and file_data_unref().
zas_
parents:
855
diff
changeset
|
648 #ifdef DEBUG_FILEDATA |
fa39a4d786ad
Increase debugging info in file_data_ref() and file_data_unref().
zas_
parents:
855
diff
changeset
|
649 void file_data_unref_debug(const gchar *file, gint line, FileData *fd) |
fa39a4d786ad
Increase debugging info in file_data_ref() and file_data_unref().
zas_
parents:
855
diff
changeset
|
650 #else |
586 | 651 void file_data_unref(FileData *fd) |
874
fa39a4d786ad
Increase debugging info in file_data_ref() and file_data_unref().
zas_
parents:
855
diff
changeset
|
652 #endif |
586 | 653 { |
654 if (fd == NULL) return; | |
874
fa39a4d786ad
Increase debugging info in file_data_ref() and file_data_unref().
zas_
parents:
855
diff
changeset
|
655 #ifdef DEBUG_FILEDATA |
fa39a4d786ad
Increase debugging info in file_data_ref() and file_data_unref().
zas_
parents:
855
diff
changeset
|
656 if (fd->magick != 0x12345678) |
fa39a4d786ad
Increase debugging info in file_data_ref() and file_data_unref().
zas_
parents:
855
diff
changeset
|
657 DEBUG_0("fd magick mismatch @ %s:%d", file, line); |
fa39a4d786ad
Increase debugging info in file_data_ref() and file_data_unref().
zas_
parents:
855
diff
changeset
|
658 #endif |
586 | 659 g_assert(fd->magick == 0x12345678); |
874
fa39a4d786ad
Increase debugging info in file_data_ref() and file_data_unref().
zas_
parents:
855
diff
changeset
|
660 |
586 | 661 fd->ref--; |
874
fa39a4d786ad
Increase debugging info in file_data_ref() and file_data_unref().
zas_
parents:
855
diff
changeset
|
662 #ifdef DEBUG_FILEDATA |
fa39a4d786ad
Increase debugging info in file_data_ref() and file_data_unref().
zas_
parents:
855
diff
changeset
|
663 DEBUG_2("file_data_unref (%d): '%s' @ %s:%d", fd->ref, fd->path, file, line); |
fa39a4d786ad
Increase debugging info in file_data_ref() and file_data_unref().
zas_
parents:
855
diff
changeset
|
664 #else |
586 | 665 DEBUG_2("file_data_unref (%d): '%s'", fd->ref, fd->path); |
874
fa39a4d786ad
Increase debugging info in file_data_ref() and file_data_unref().
zas_
parents:
855
diff
changeset
|
666 #endif |
586 | 667 if (fd->ref == 0) |
668 { | |
806 | 669 GList *work; |
586 | 670 FileData *parent = fd->parent ? fd->parent : fd; |
806 | 671 |
1422 | 672 if (parent->ref > 0) return; |
586 | 673 |
674 work = parent->sidecar_files; | |
675 while (work) | |
676 { | |
677 FileData *sfd = work->data; | |
1422 | 678 if (sfd->ref > 0) return; |
586 | 679 work = work->next; |
680 } | |
681 | |
682 /* none of parent/children is referenced, we can free everything */ | |
683 | |
874
fa39a4d786ad
Increase debugging info in file_data_ref() and file_data_unref().
zas_
parents:
855
diff
changeset
|
684 DEBUG_2("file_data_unref: deleting '%s', parent '%s'", fd->path, fd->parent ? parent->path : "-"); |
586 | 685 |
686 work = parent->sidecar_files; | |
687 while (work) | |
688 { | |
689 FileData *sfd = work->data; | |
690 file_data_free(sfd); | |
691 work = work->next; | |
692 } | |
693 | |
694 g_list_free(parent->sidecar_files); | |
695 parent->sidecar_files = NULL; | |
696 | |
697 file_data_free(parent); | |
698 } | |
699 } | |
700 | |
701 FileData *file_data_disconnect_sidecar_file(FileData *target, FileData *sfd) | |
702 { | |
703 sfd->parent = target; | |
704 g_assert(g_list_find(target->sidecar_files, sfd)); | |
801 | 705 |
706 file_data_increment_version(sfd); /* increments both sfd and target */ | |
586 | 707 |
708 target->sidecar_files = g_list_remove(target->sidecar_files, sfd); | |
709 sfd->parent = NULL; | |
710 | |
806 | 711 if (sfd->ref == 0) |
712 { | |
586 | 713 file_data_free(sfd); |
714 return NULL; | |
806 | 715 } |
586 | 716 |
717 return sfd; | |
718 } | |
719 | |
849
db68d673448f
added possibility to disable grouping of selected files
nadvornik
parents:
846
diff
changeset
|
720 /* disables / enables grouping for particular file, sends UPDATE notification */ |
db68d673448f
added possibility to disable grouping of selected files
nadvornik
parents:
846
diff
changeset
|
721 void file_data_disable_grouping(FileData *fd, gboolean disable) |
db68d673448f
added possibility to disable grouping of selected files
nadvornik
parents:
846
diff
changeset
|
722 { |
db68d673448f
added possibility to disable grouping of selected files
nadvornik
parents:
846
diff
changeset
|
723 if (!fd->disable_grouping == !disable) return; |
1599 | 724 |
849
db68d673448f
added possibility to disable grouping of selected files
nadvornik
parents:
846
diff
changeset
|
725 fd->disable_grouping = !!disable; |
db68d673448f
added possibility to disable grouping of selected files
nadvornik
parents:
846
diff
changeset
|
726 |
db68d673448f
added possibility to disable grouping of selected files
nadvornik
parents:
846
diff
changeset
|
727 if (disable) |
db68d673448f
added possibility to disable grouping of selected files
nadvornik
parents:
846
diff
changeset
|
728 { |
db68d673448f
added possibility to disable grouping of selected files
nadvornik
parents:
846
diff
changeset
|
729 if (fd->parent) |
db68d673448f
added possibility to disable grouping of selected files
nadvornik
parents:
846
diff
changeset
|
730 { |
db68d673448f
added possibility to disable grouping of selected files
nadvornik
parents:
846
diff
changeset
|
731 FileData *parent = file_data_ref(fd->parent); |
db68d673448f
added possibility to disable grouping of selected files
nadvornik
parents:
846
diff
changeset
|
732 file_data_disconnect_sidecar_file(parent, fd); |
1432 | 733 file_data_send_notification(parent, NOTIFY_GROUPING); |
849
db68d673448f
added possibility to disable grouping of selected files
nadvornik
parents:
846
diff
changeset
|
734 file_data_unref(parent); |
db68d673448f
added possibility to disable grouping of selected files
nadvornik
parents:
846
diff
changeset
|
735 } |
db68d673448f
added possibility to disable grouping of selected files
nadvornik
parents:
846
diff
changeset
|
736 else if (fd->sidecar_files) |
db68d673448f
added possibility to disable grouping of selected files
nadvornik
parents:
846
diff
changeset
|
737 { |
db68d673448f
added possibility to disable grouping of selected files
nadvornik
parents:
846
diff
changeset
|
738 GList *sidecar_files = filelist_copy(fd->sidecar_files); |
db68d673448f
added possibility to disable grouping of selected files
nadvornik
parents:
846
diff
changeset
|
739 GList *work = sidecar_files; |
db68d673448f
added possibility to disable grouping of selected files
nadvornik
parents:
846
diff
changeset
|
740 while (work) |
db68d673448f
added possibility to disable grouping of selected files
nadvornik
parents:
846
diff
changeset
|
741 { |
db68d673448f
added possibility to disable grouping of selected files
nadvornik
parents:
846
diff
changeset
|
742 FileData *sfd = work->data; |
db68d673448f
added possibility to disable grouping of selected files
nadvornik
parents:
846
diff
changeset
|
743 work = work->next; |
db68d673448f
added possibility to disable grouping of selected files
nadvornik
parents:
846
diff
changeset
|
744 file_data_disconnect_sidecar_file(fd, sfd); |
1432 | 745 file_data_send_notification(sfd, NOTIFY_GROUPING); |
849
db68d673448f
added possibility to disable grouping of selected files
nadvornik
parents:
846
diff
changeset
|
746 } |
1518 | 747 file_data_check_sidecars((FileData *)sidecar_files->data, FALSE); /* this will group the sidecars back together */ |
849
db68d673448f
added possibility to disable grouping of selected files
nadvornik
parents:
846
diff
changeset
|
748 filelist_free(sidecar_files); |
db68d673448f
added possibility to disable grouping of selected files
nadvornik
parents:
846
diff
changeset
|
749 } |
1599 | 750 else |
751 { | |
752 file_data_increment_version(fd); /* the functions called in the cases above increments the version too */ | |
753 } | |
849
db68d673448f
added possibility to disable grouping of selected files
nadvornik
parents:
846
diff
changeset
|
754 } |
db68d673448f
added possibility to disable grouping of selected files
nadvornik
parents:
846
diff
changeset
|
755 else |
db68d673448f
added possibility to disable grouping of selected files
nadvornik
parents:
846
diff
changeset
|
756 { |
1599 | 757 file_data_increment_version(fd); |
1518 | 758 file_data_check_sidecars(fd, FALSE); |
1599 | 759 } |
760 file_data_send_notification(fd, NOTIFY_GROUPING); | |
761 } | |
762 | |
763 void file_data_disable_grouping_list(GList *fd_list, gboolean disable) | |
764 { | |
765 GList *work; | |
766 | |
767 work = fd_list; | |
768 while (work) | |
769 { | |
770 FileData *fd = work->data; | |
771 | |
772 file_data_disable_grouping(fd, disable); | |
773 work = work->next; | |
849
db68d673448f
added possibility to disable grouping of selected files
nadvornik
parents:
846
diff
changeset
|
774 } |
db68d673448f
added possibility to disable grouping of selected files
nadvornik
parents:
846
diff
changeset
|
775 } |
db68d673448f
added possibility to disable grouping of selected files
nadvornik
parents:
846
diff
changeset
|
776 |
1599 | 777 |
586 | 778 /* compare name without extension */ |
779 gint file_data_compare_name_without_ext(FileData *fd1, FileData *fd2) | |
780 { | |
781 size_t len1 = fd1->extension - fd1->name; | |
782 size_t len2 = fd2->extension - fd2->name; | |
783 | |
784 if (len1 < len2) return -1; | |
785 if (len1 > len2) return 1; | |
786 | |
785 | 787 return strncmp(fd1->name, fd2->name, len1); /* FIXME: utf8 */ |
586 | 788 } |
789 | |
790 void file_data_change_info_free(FileDataChangeInfo *fdci, FileData *fd) | |
791 { | |
1422 | 792 if (!fdci && fd) fdci = fd->change; |
586 | 793 |
1422 | 794 if (!fdci) return; |
586 | 795 |
796 g_free(fdci->source); | |
797 g_free(fdci->dest); | |
798 | |
799 g_free(fdci); | |
800 | |
1422 | 801 if (fd) fd->change = NULL; |
586 | 802 } |
803 | |
1224 | 804 static gboolean file_data_can_write_directly(FileData *fd) |
805 { | |
1239
254b09942e68
metadata write mode (direct or sidecar) made configurable for each file
nadvornik
parents:
1229
diff
changeset
|
806 return filter_name_is_writable(fd->extension); |
1224 | 807 } |
586 | 808 |
1224 | 809 static gboolean file_data_can_write_sidecar(FileData *fd) |
810 { | |
1239
254b09942e68
metadata write mode (direct or sidecar) made configurable for each file
nadvornik
parents:
1229
diff
changeset
|
811 return filter_name_allow_sidecar(fd->extension) && !filter_name_is_writable(fd->extension); |
1224 | 812 } |
813 | |
814 gchar *file_data_get_sidecar_path(FileData *fd, gboolean existing_only) | |
815 { | |
816 gchar *sidecar_path = NULL; | |
817 GList *work; | |
1422 | 818 |
1224 | 819 if (!file_data_can_write_sidecar(fd)) return NULL; |
820 | |
821 work = fd->parent ? fd->parent->sidecar_files : fd->sidecar_files; | |
822 while (work) | |
823 { | |
824 FileData *sfd = work->data; | |
825 work = work->next; | |
1307 | 826 if (g_ascii_strcasecmp(sfd->extension, ".xmp") == 0) |
1224 | 827 { |
828 sidecar_path = g_strdup(sfd->path); | |
829 break; | |
830 } | |
831 } | |
832 | |
833 if (!existing_only && !sidecar_path) | |
834 { | |
835 gchar *base = remove_extension_from_path(fd->path); | |
836 sidecar_path = g_strconcat(base, ".xmp", NULL); | |
837 g_free(base); | |
838 } | |
839 | |
840 return sidecar_path; | |
841 } | |
586 | 842 |
843 | |
844 /* | |
845 *----------------------------------------------------------------------------- | |
846 * sidecar file info struct | |
847 *----------------------------------------------------------------------------- | |
848 */ | |
849 | |
850 | |
851 | |
852 static gint sidecar_file_priority(const gchar *path) | |
853 { | |
1000
4fe8f9656107
For the sake of consistency, use glib basic types everywhere.
zas_
parents:
998
diff
changeset
|
854 const gchar *extension = extension_from_path(path); |
4fe8f9656107
For the sake of consistency, use glib basic types everywhere.
zas_
parents:
998
diff
changeset
|
855 gint i = 1; |
586 | 856 GList *work; |
806 | 857 |
586 | 858 if (extension == NULL) |
859 return 0; | |
860 | |
861 work = sidecar_ext_get_list(); | |
862 | |
863 while (work) { | |
864 gchar *ext = work->data; | |
806 | 865 |
586 | 866 work = work->next; |
1307 | 867 if (g_ascii_strcasecmp(extension, ext) == 0) return i; |
586 | 868 i++; |
869 } | |
870 return 0; | |
871 } | |
872 | |
873 | |
874 /* | |
875 *----------------------------------------------------------------------------- | |
876 * load file list | |
877 *----------------------------------------------------------------------------- | |
878 */ | |
879 | |
880 static SortType filelist_sort_method = SORT_NONE; | |
1422 | 881 static gboolean filelist_sort_ascend = TRUE; |
586 | 882 |
883 | |
884 gint filelist_sort_compare_filedata(FileData *fa, FileData *fb) | |
885 { | |
886 if (!filelist_sort_ascend) | |
887 { | |
888 FileData *tmp = fa; | |
889 fa = fb; | |
890 fb = tmp; | |
891 } | |
892 | |
893 switch (filelist_sort_method) | |
894 { | |
785 | 895 case SORT_NAME: |
896 break; | |
586 | 897 case SORT_SIZE: |
898 if (fa->size < fb->size) return -1; | |
899 if (fa->size > fb->size) return 1; | |
785 | 900 /* fall back to name */ |
586 | 901 break; |
902 case SORT_TIME: | |
903 if (fa->date < fb->date) return -1; | |
904 if (fa->date > fb->date) return 1; | |
785 | 905 /* fall back to name */ |
586 | 906 break; |
907 #ifdef HAVE_STRVERSCMP | |
908 case SORT_NUMBER: | |
909 return strverscmp(fa->name, fb->name); | |
910 break; | |
911 #endif | |
912 default: | |
913 break; | |
914 } | |
785 | 915 |
916 if (options->file_sort.case_sensitive) | |
917 return strcmp(fa->collate_key_name, fb->collate_key_name); | |
918 else | |
919 return strcmp(fa->collate_key_name_nocase, fb->collate_key_name_nocase); | |
586 | 920 } |
921 | |
1422 | 922 gint filelist_sort_compare_filedata_full(FileData *fa, FileData *fb, SortType method, gboolean ascend) |
586 | 923 { |
924 filelist_sort_method = method; | |
925 filelist_sort_ascend = ascend; | |
926 return filelist_sort_compare_filedata(fa, fb); | |
927 } | |
928 | |
1002 | 929 static gint filelist_sort_file_cb(gpointer a, gpointer b) |
586 | 930 { |
931 return filelist_sort_compare_filedata(a, b); | |
932 } | |
933 | |
1422 | 934 GList *filelist_sort_full(GList *list, SortType method, gboolean ascend, GCompareFunc cb) |
586 | 935 { |
936 filelist_sort_method = method; | |
937 filelist_sort_ascend = ascend; | |
938 return g_list_sort(list, cb); | |
939 } | |
940 | |
1422 | 941 GList *filelist_insert_sort_full(GList *list, gpointer data, SortType method, gboolean ascend, GCompareFunc cb) |
586 | 942 { |
943 filelist_sort_method = method; | |
944 filelist_sort_ascend = ascend; | |
945 return g_list_insert_sorted(list, data, cb); | |
946 } | |
947 | |
1422 | 948 GList *filelist_sort(GList *list, SortType method, gboolean ascend) |
586 | 949 { |
950 return filelist_sort_full(list, method, ascend, (GCompareFunc) filelist_sort_file_cb); | |
951 } | |
952 | |
1422 | 953 GList *filelist_insert_sort(GList *list, FileData *fd, SortType method, gboolean ascend) |
586 | 954 { |
955 return filelist_insert_sort_full(list, fd, method, ascend, (GCompareFunc) filelist_sort_file_cb); | |
956 } | |
957 | |
958 | |
959 static GList *filelist_filter_out_sidecars(GList *flist) | |
960 { | |
961 GList *work = flist; | |
962 GList *flist_filtered = NULL; | |
963 | |
964 while (work) | |
965 { | |
966 FileData *fd = work->data; | |
806 | 967 |
586 | 968 work = work->next; |
969 if (fd->parent) /* remove fd's that are children */ | |
970 file_data_unref(fd); | |
971 else | |
972 flist_filtered = g_list_prepend(flist_filtered, fd); | |
973 } | |
974 g_list_free(flist); | |
806 | 975 |
586 | 976 return flist_filtered; |
977 } | |
978 | |
1423
6933974f0885
Make ishidden() static to filedata.c and rename it is_hidden_file().
zas_
parents:
1422
diff
changeset
|
979 static gboolean is_hidden_file(const gchar *name) |
6933974f0885
Make ishidden() static to filedata.c and rename it is_hidden_file().
zas_
parents:
1422
diff
changeset
|
980 { |
6933974f0885
Make ishidden() static to filedata.c and rename it is_hidden_file().
zas_
parents:
1422
diff
changeset
|
981 if (name[0] != '.') return FALSE; |
6933974f0885
Make ishidden() static to filedata.c and rename it is_hidden_file().
zas_
parents:
1422
diff
changeset
|
982 if (name[1] == '\0' || (name[1] == '.' && name[2] == '\0')) return FALSE; |
6933974f0885
Make ishidden() static to filedata.c and rename it is_hidden_file().
zas_
parents:
1422
diff
changeset
|
983 return TRUE; |
6933974f0885
Make ishidden() static to filedata.c and rename it is_hidden_file().
zas_
parents:
1422
diff
changeset
|
984 } |
6933974f0885
Make ishidden() static to filedata.c and rename it is_hidden_file().
zas_
parents:
1422
diff
changeset
|
985 |
1422 | 986 static gboolean filelist_read_real(FileData *dir_fd, GList **files, GList **dirs, gboolean follow_symlinks) |
586 | 987 { |
988 DIR *dp; | |
989 struct dirent *dir; | |
990 gchar *pathl; | |
779 | 991 GList *dlist = NULL; |
992 GList *flist = NULL; | |
1000
4fe8f9656107
For the sake of consistency, use glib basic types everywhere.
zas_
parents:
998
diff
changeset
|
993 gint (*stat_func)(const gchar *path, struct stat *buf); |
586 | 994 |
779 | 995 g_assert(files || dirs); |
996 | |
997 if (files) *files = NULL; | |
998 if (dirs) *dirs = NULL; | |
586 | 999 |
783 | 1000 pathl = path_from_utf8(dir_fd->path); |
779 | 1001 if (!pathl) return FALSE; |
1002 | |
1003 dp = opendir(pathl); | |
1004 if (dp == NULL) | |
586 | 1005 { |
1006 g_free(pathl); | |
1007 return FALSE; | |
1008 } | |
1009 | |
779 | 1010 if (follow_symlinks) |
1011 stat_func = stat; | |
1012 else | |
1013 stat_func = lstat; | |
1014 | |
586 | 1015 while ((dir = readdir(dp)) != NULL) |
1016 { | |
779 | 1017 struct stat ent_sbuf; |
1018 const gchar *name = dir->d_name; | |
1019 gchar *filepath; | |
1020 | |
1423
6933974f0885
Make ishidden() static to filedata.c and rename it is_hidden_file().
zas_
parents:
1422
diff
changeset
|
1021 if (!options->file_filter.show_hidden_files && is_hidden_file(name)) |
779 | 1022 continue; |
1023 | |
1024 filepath = g_build_filename(pathl, name, NULL); | |
1025 if (stat_func(filepath, &ent_sbuf) >= 0) | |
586 | 1026 { |
779 | 1027 if (S_ISDIR(ent_sbuf.st_mode)) |
586 | 1028 { |
779 | 1029 /* we ignore the .thumbnails dir for cleanliness */ |
1030 if (dirs && | |
1031 !(name[0] == '.' && (name[1] == '\0' || (name[1] == '.' && name[2] == '\0'))) && | |
1032 strcmp(name, GQ_CACHE_LOCAL_THUMB) != 0 && | |
1033 strcmp(name, GQ_CACHE_LOCAL_METADATA) != 0 && | |
1034 strcmp(name, THUMB_FOLDER_LOCAL) != 0) | |
586 | 1035 { |
1518 | 1036 dlist = g_list_prepend(dlist, file_data_new_local(filepath, &ent_sbuf, FALSE, FALSE)); |
586 | 1037 } |
1038 } | |
779 | 1039 else |
1040 { | |
1041 if (files && filter_name_exists(name)) | |
1042 { | |
1518 | 1043 flist = g_list_prepend(flist, file_data_new_local(filepath, &ent_sbuf, TRUE, FALSE)); |
779 | 1044 } |
1045 } | |
586 | 1046 } |
779 | 1047 g_free(filepath); |
586 | 1048 } |
1049 | |
1050 closedir(dp); | |
779 | 1051 |
586 | 1052 g_free(pathl); |
1053 | |
1054 if (dirs) *dirs = dlist; | |
779 | 1055 if (files) *files = filelist_filter_out_sidecars(flist); |
586 | 1056 |
1057 return TRUE; | |
1058 } | |
1059 | |
1422 | 1060 gboolean filelist_read(FileData *dir_fd, GList **files, GList **dirs) |
586 | 1061 { |
783 | 1062 return filelist_read_real(dir_fd, files, dirs, TRUE); |
586 | 1063 } |
1064 | |
1422 | 1065 gboolean filelist_read_lstat(FileData *dir_fd, GList **files, GList **dirs) |
586 | 1066 { |
783 | 1067 return filelist_read_real(dir_fd, files, dirs, FALSE); |
586 | 1068 } |
1069 | |
1070 void filelist_free(GList *list) | |
1071 { | |
1072 GList *work; | |
1073 | |
1074 work = list; | |
1075 while (work) | |
1076 { | |
1077 file_data_unref((FileData *)work->data); | |
1078 work = work->next; | |
1079 } | |
1080 | |
1081 g_list_free(list); | |
1082 } | |
1083 | |
1084 | |
1085 GList *filelist_copy(GList *list) | |
1086 { | |
1087 GList *new_list = NULL; | |
1088 GList *work; | |
1089 | |
1090 work = list; | |
1091 while (work) | |
1092 { | |
1093 FileData *fd; | |
1094 | |
1095 fd = work->data; | |
1096 work = work->next; | |
1097 | |
1098 new_list = g_list_prepend(new_list, file_data_ref(fd)); | |
1099 } | |
1100 | |
1101 return g_list_reverse(new_list); | |
1102 } | |
1103 | |
1104 GList *filelist_from_path_list(GList *list) | |
1105 { | |
1106 GList *new_list = NULL; | |
1107 GList *work; | |
1108 | |
1109 work = list; | |
1110 while (work) | |
1111 { | |
1112 gchar *path; | |
1113 | |
1114 path = work->data; | |
1115 work = work->next; | |
1116 | |
1117 new_list = g_list_prepend(new_list, file_data_new_simple(path)); | |
1118 } | |
1119 | |
1120 return g_list_reverse(new_list); | |
1121 } | |
1122 | |
1123 GList *filelist_to_path_list(GList *list) | |
1124 { | |
1125 GList *new_list = NULL; | |
1126 GList *work; | |
1127 | |
1128 work = list; | |
1129 while (work) | |
1130 { | |
1131 FileData *fd; | |
1132 | |
1133 fd = work->data; | |
1134 work = work->next; | |
1135 | |
1136 new_list = g_list_prepend(new_list, g_strdup(fd->path)); | |
1137 } | |
1138 | |
1139 return g_list_reverse(new_list); | |
1140 } | |
1141 | |
1422 | 1142 GList *filelist_filter(GList *list, gboolean is_dir_list) |
586 | 1143 { |
1144 GList *work; | |
1145 | |
1146 if (!is_dir_list && options->file_filter.disable && options->file_filter.show_hidden_files) return list; | |
1147 | |
1148 work = list; | |
1149 while (work) | |
1150 { | |
1151 FileData *fd = (FileData *)(work->data); | |
1152 const gchar *name = fd->name; | |
1153 | |
1423
6933974f0885
Make ishidden() static to filedata.c and rename it is_hidden_file().
zas_
parents:
1422
diff
changeset
|
1154 if ((!options->file_filter.show_hidden_files && is_hidden_file(name)) || |
586 | 1155 (!is_dir_list && !filter_name_exists(name)) || |
1156 (is_dir_list && name[0] == '.' && (strcmp(name, GQ_CACHE_LOCAL_THUMB) == 0 || | |
1157 strcmp(name, GQ_CACHE_LOCAL_METADATA) == 0)) ) | |
1158 { | |
1159 GList *link = work; | |
806 | 1160 |
586 | 1161 list = g_list_remove_link(list, link); |
1162 file_data_unref(fd); | |
1163 g_list_free(link); | |
1164 } | |
806 | 1165 |
1166 work = work->next; | |
586 | 1167 } |
1168 | |
1169 return list; | |
1170 } | |
1171 | |
1172 /* | |
1173 *----------------------------------------------------------------------------- | |
1174 * filelist recursive | |
1175 *----------------------------------------------------------------------------- | |
1176 */ | |
1177 | |
1178 static gint filelist_sort_path_cb(gconstpointer a, gconstpointer b) | |
1179 { | |
1180 return CASE_SORT(((FileData *)a)->path, ((FileData *)b)->path); | |
1181 } | |
1182 | |
1183 GList *filelist_sort_path(GList *list) | |
1184 { | |
1185 return g_list_sort(list, filelist_sort_path_cb); | |
1186 } | |
1187 | |
1188 static void filelist_recursive_append(GList **list, GList *dirs) | |
1189 { | |
1190 GList *work; | |
1191 | |
1192 work = dirs; | |
1193 while (work) | |
1194 { | |
1195 FileData *fd = (FileData *)(work->data); | |
780
44128da39e13
Drop initialization to NULL since filelist_read() will take care of it.
zas_
parents:
779
diff
changeset
|
1196 GList *f; |
44128da39e13
Drop initialization to NULL since filelist_read() will take care of it.
zas_
parents:
779
diff
changeset
|
1197 GList *d; |
586 | 1198 |
783 | 1199 if (filelist_read(fd, &f, &d)) |
586 | 1200 { |
1201 f = filelist_filter(f, FALSE); | |
1202 f = filelist_sort_path(f); | |
1203 *list = g_list_concat(*list, f); | |
1204 | |
1205 d = filelist_filter(d, TRUE); | |
1206 d = filelist_sort_path(d); | |
1207 filelist_recursive_append(list, d); | |
1208 filelist_free(d); | |
1209 } | |
1210 | |
1211 work = work->next; | |
1212 } | |
1213 } | |
1214 | |
783 | 1215 GList *filelist_recursive(FileData *dir_fd) |
586 | 1216 { |
780
44128da39e13
Drop initialization to NULL since filelist_read() will take care of it.
zas_
parents:
779
diff
changeset
|
1217 GList *list; |
44128da39e13
Drop initialization to NULL since filelist_read() will take care of it.
zas_
parents:
779
diff
changeset
|
1218 GList *d; |
586 | 1219 |
783 | 1220 if (!filelist_read(dir_fd, &list, &d)) return NULL; |
586 | 1221 list = filelist_filter(list, FALSE); |
1222 list = filelist_sort_path(list); | |
1223 | |
1224 d = filelist_filter(d, TRUE); | |
1225 d = filelist_sort_path(d); | |
1226 filelist_recursive_append(&list, d); | |
1227 filelist_free(d); | |
1228 | |
1229 return list; | |
1230 } | |
590 | 1231 |
1232 | |
800 | 1233 /* |
1234 * marks and orientation | |
1235 */ | |
995 | 1236 |
1222 | 1237 static FileDataGetMarkFunc file_data_get_mark_func[FILEDATA_MARKS_SIZE]; |
1238 static FileDataSetMarkFunc file_data_set_mark_func[FILEDATA_MARKS_SIZE]; | |
1239 static gpointer file_data_mark_func_data[FILEDATA_MARKS_SIZE]; | |
1425 | 1240 static GDestroyNotify file_data_destroy_mark_func[FILEDATA_MARKS_SIZE]; |
995 | 1241 |
800 | 1242 gboolean file_data_get_mark(FileData *fd, gint n) |
1243 { | |
1227 | 1244 gboolean valid = (fd->valid_marks & (1 << n)); |
1422 | 1245 |
1227 | 1246 if (file_data_get_mark_func[n] && !valid) |
1222 | 1247 { |
1227 | 1248 guint old = fd->marks; |
1222 | 1249 gboolean value = (file_data_get_mark_func[n])(fd, n, file_data_mark_func_data[n]); |
1422 | 1250 |
1227 | 1251 if (!value != !(fd->marks & (1 << n))) |
1252 { | |
1253 fd->marks = fd->marks ^ (1 << n); | |
1254 } | |
1422 | 1255 |
1227 | 1256 fd->valid_marks |= (1 << n); |
1257 if (old && !fd->marks) /* keep files with non-zero marks in memory */ | |
1258 { | |
1259 file_data_unref(fd); | |
1260 } | |
1261 else if (!old && fd->marks) | |
1262 { | |
1263 file_data_ref(fd); | |
1264 } | |
1222 | 1265 } |
1266 | |
800 | 1267 return !!(fd->marks & (1 << n)); |
1268 } | |
1269 | |
966 | 1270 guint file_data_get_marks(FileData *fd) |
1271 { | |
1222 | 1272 gint i; |
1273 for (i = 0; i < FILEDATA_MARKS_SIZE; i++) file_data_get_mark(fd, i); | |
966 | 1274 return fd->marks; |
1275 } | |
1276 | |
800 | 1277 void file_data_set_mark(FileData *fd, gint n, gboolean value) |
1278 { | |
1227 | 1279 guint old; |
1280 if (!value == !file_data_get_mark(fd, n)) return; | |
1281 | |
1222 | 1282 if (file_data_set_mark_func[n]) |
1283 { | |
1284 (file_data_set_mark_func[n])(fd, n, value, file_data_mark_func_data[n]); | |
1285 } | |
1227 | 1286 |
1287 old = fd->marks; | |
1222 | 1288 |
800 | 1289 fd->marks = fd->marks ^ (1 << n); |
965 | 1290 |
1291 if (old && !fd->marks) /* keep files with non-zero marks in memory */ | |
1292 { | |
1293 file_data_unref(fd); | |
1294 } | |
1295 else if (!old && fd->marks) | |
1296 { | |
1297 file_data_ref(fd); | |
1298 } | |
1299 | |
800 | 1300 file_data_increment_version(fd); |
1432 | 1301 file_data_send_notification(fd, NOTIFY_MARKS); |
800 | 1302 } |
1303 | |
964 | 1304 gboolean file_data_filter_marks(FileData *fd, guint filter) |
1305 { | |
1222 | 1306 gint i; |
1307 for (i = 0; i < FILEDATA_MARKS_SIZE; i++) if (filter & (1 << i)) file_data_get_mark(fd, i); | |
964 | 1308 return ((fd->marks & filter) == filter); |
1309 } | |
1310 | |
1311 GList *file_data_filter_marks_list(GList *list, guint filter) | |
1312 { | |
1313 GList *work; | |
1314 | |
1315 work = list; | |
1316 while (work) | |
1317 { | |
1318 FileData *fd = work->data; | |
1319 GList *link = work; | |
1320 work = work->next; | |
1321 | |
1322 if (!file_data_filter_marks(fd, filter)) | |
1323 { | |
1324 list = g_list_remove_link(list, link); | |
1325 file_data_unref(fd); | |
1326 g_list_free(link); | |
1327 } | |
1328 } | |
1329 | |
1330 return list; | |
1331 } | |
1332 | |
1222 | 1333 static void file_data_notify_mark_func(gpointer key, gpointer value, gpointer user_data) |
1334 { | |
1335 FileData *fd = value; | |
1336 file_data_increment_version(fd); | |
1432 | 1337 file_data_send_notification(fd, NOTIFY_MARKS); |
1222 | 1338 } |
1339 | |
1425 | 1340 gboolean file_data_register_mark_func(gint n, FileDataGetMarkFunc get_mark_func, FileDataSetMarkFunc set_mark_func, gpointer data, GDestroyNotify notify) |
1222 | 1341 { |
1342 if (n < 0 || n >= FILEDATA_MARKS_SIZE) return FALSE; | |
1425 | 1343 |
1344 if (file_data_destroy_mark_func[n]) (file_data_destroy_mark_func[n])(file_data_mark_func_data[n]); | |
1222 | 1345 |
1346 file_data_get_mark_func[n] = get_mark_func; | |
1347 file_data_set_mark_func[n] = set_mark_func; | |
1348 file_data_mark_func_data[n] = data; | |
1425 | 1349 file_data_destroy_mark_func[n] = notify; |
1350 | |
1222 | 1351 if (get_mark_func) |
1425 | 1352 { |
1353 /* this effectively changes all known files */ | |
1354 g_hash_table_foreach(file_data_pool, file_data_notify_mark_func, NULL); | |
1355 } | |
1356 | |
1222 | 1357 return TRUE; |
1358 } | |
1359 | |
1360 void file_data_get_registered_mark_func(gint n, FileDataGetMarkFunc *get_mark_func, FileDataSetMarkFunc *set_mark_func, gpointer *data) | |
1361 { | |
1362 if (get_mark_func) *get_mark_func = file_data_get_mark_func[n]; | |
1363 if (set_mark_func) *set_mark_func = file_data_set_mark_func[n]; | |
1364 if (data) *data = file_data_mark_func_data[n]; | |
1365 } | |
1366 | |
800 | 1367 gint file_data_get_user_orientation(FileData *fd) |
1368 { | |
1369 return fd->user_orientation; | |
1370 } | |
1371 | |
1372 void file_data_set_user_orientation(FileData *fd, gint value) | |
1373 { | |
1374 if (fd->user_orientation == value) return; | |
1375 | |
1376 fd->user_orientation = value; | |
1377 file_data_increment_version(fd); | |
1432 | 1378 file_data_send_notification(fd, NOTIFY_ORIENTATION); |
800 | 1379 } |
1380 | |
1381 | |
590 | 1382 /* |
1383 * file_data - operates on the given fd | |
1384 * file_data_sc - operates on the given fd + sidecars - all fds linked via fd->sidecar_files or fd->parent | |
1385 */ | |
1386 | |
1387 | |
1388 /* return list of sidecar file extensions in a string */ | |
596 | 1389 gchar *file_data_sc_list_to_string(FileData *fd) |
1390 { | |
1391 GList *work; | |
1392 GString *result = g_string_new(""); | |
1393 | |
1394 work = fd->sidecar_files; | |
1395 while (work) | |
1396 { | |
1397 FileData *sfd = work->data; | |
806 | 1398 |
596 | 1399 result = g_string_append(result, "+ "); |
1400 result = g_string_append(result, sfd->extension); | |
1401 work = work->next; | |
1402 if (work) result = g_string_append_c(result, ' '); | |
1403 } | |
1404 | |
1405 return g_string_free(result, FALSE); | |
1406 } | |
590 | 1407 |
1408 | |
995 | 1409 |
1410 /* | |
1411 * add FileDataChangeInfo (see typedefs.h) for the given operation | |
590 | 1412 * uses file_data_add_change_info |
1413 * | |
1414 * fails if the fd->change already exists - change operations can't run in parallel | |
1415 * fd->change_info works as a lock | |
1416 * | |
1417 * dest can be NULL - in this case the current name is used for now, it will | |
995 | 1418 * be changed later |
590 | 1419 */ |
1420 | |
1421 /* | |
1422 FileDataChangeInfo types: | |
1423 COPY | |
950 | 1424 MOVE - path is changed, name may be changed too |
590 | 1425 RENAME - path remains unchanged, name is changed |
1426 extension should remain (FIXME should we allow editing extension? it will make problems wth grouping) | |
1427 sidecar names are changed too, extensions are not changed | |
1428 DELETE | |
995 | 1429 UPDATE - file size, date or grouping has been changed |
590 | 1430 */ |
1431 | |
1432 gboolean file_data_add_ci(FileData *fd, FileDataChangeType type, const gchar *src, const gchar *dest) | |
1433 { | |
1434 FileDataChangeInfo *fdci; | |
1435 | |
1436 if (fd->change) return FALSE; | |
1437 | |
1438 fdci = g_new0(FileDataChangeInfo, 1); | |
1439 | |
1440 fdci->type = type; | |
1441 | |
1442 if (src) | |
1443 fdci->source = g_strdup(src); | |
1444 else | |
1445 fdci->source = g_strdup(fd->path); | |
1446 | |
1447 if (dest) | |
1448 fdci->dest = g_strdup(dest); | |
1449 | |
1450 fd->change = fdci; | |
1451 | |
1452 return TRUE; | |
1453 } | |
1454 | |
912
9108a7158c02
remove items from file_data_planned_change_hash when the operation is
nadvornik
parents:
910
diff
changeset
|
1455 static void file_data_planned_change_remove(FileData *fd) |
9108a7158c02
remove items from file_data_planned_change_hash when the operation is
nadvornik
parents:
910
diff
changeset
|
1456 { |
995 | 1457 if (file_data_planned_change_hash && |
912
9108a7158c02
remove items from file_data_planned_change_hash when the operation is
nadvornik
parents:
910
diff
changeset
|
1458 (fd->change->type == FILEDATA_CHANGE_MOVE || fd->change->type == FILEDATA_CHANGE_RENAME)) |
9108a7158c02
remove items from file_data_planned_change_hash when the operation is
nadvornik
parents:
910
diff
changeset
|
1459 { |
9108a7158c02
remove items from file_data_planned_change_hash when the operation is
nadvornik
parents:
910
diff
changeset
|
1460 if (g_hash_table_lookup(file_data_planned_change_hash, fd->change->dest) == fd) |
9108a7158c02
remove items from file_data_planned_change_hash when the operation is
nadvornik
parents:
910
diff
changeset
|
1461 { |
9108a7158c02
remove items from file_data_planned_change_hash when the operation is
nadvornik
parents:
910
diff
changeset
|
1462 DEBUG_1("planned change: removing %s -> %s", fd->change->dest, fd->path); |
9108a7158c02
remove items from file_data_planned_change_hash when the operation is
nadvornik
parents:
910
diff
changeset
|
1463 g_hash_table_remove(file_data_planned_change_hash, fd->change->dest); |
9108a7158c02
remove items from file_data_planned_change_hash when the operation is
nadvornik
parents:
910
diff
changeset
|
1464 file_data_unref(fd); |
9108a7158c02
remove items from file_data_planned_change_hash when the operation is
nadvornik
parents:
910
diff
changeset
|
1465 if (g_hash_table_size(file_data_planned_change_hash) == 0) |
9108a7158c02
remove items from file_data_planned_change_hash when the operation is
nadvornik
parents:
910
diff
changeset
|
1466 { |
9108a7158c02
remove items from file_data_planned_change_hash when the operation is
nadvornik
parents:
910
diff
changeset
|
1467 g_hash_table_destroy(file_data_planned_change_hash); |
9108a7158c02
remove items from file_data_planned_change_hash when the operation is
nadvornik
parents:
910
diff
changeset
|
1468 file_data_planned_change_hash = NULL; |
9108a7158c02
remove items from file_data_planned_change_hash when the operation is
nadvornik
parents:
910
diff
changeset
|
1469 DEBUG_1("planned change: empty"); |
9108a7158c02
remove items from file_data_planned_change_hash when the operation is
nadvornik
parents:
910
diff
changeset
|
1470 } |
9108a7158c02
remove items from file_data_planned_change_hash when the operation is
nadvornik
parents:
910
diff
changeset
|
1471 } |
9108a7158c02
remove items from file_data_planned_change_hash when the operation is
nadvornik
parents:
910
diff
changeset
|
1472 } |
9108a7158c02
remove items from file_data_planned_change_hash when the operation is
nadvornik
parents:
910
diff
changeset
|
1473 } |
995 | 1474 |
912
9108a7158c02
remove items from file_data_planned_change_hash when the operation is
nadvornik
parents:
910
diff
changeset
|
1475 |
590 | 1476 void file_data_free_ci(FileData *fd) |
1477 { | |
1478 FileDataChangeInfo *fdci = fd->change; | |
1479 | |
1422 | 1480 if (!fdci) return; |
590 | 1481 |
912
9108a7158c02
remove items from file_data_planned_change_hash when the operation is
nadvornik
parents:
910
diff
changeset
|
1482 file_data_planned_change_remove(fd); |
9108a7158c02
remove items from file_data_planned_change_hash when the operation is
nadvornik
parents:
910
diff
changeset
|
1483 |
590 | 1484 g_free(fdci->source); |
1485 g_free(fdci->dest); | |
1486 | |
1487 g_free(fdci); | |
1488 | |
1489 fd->change = NULL; | |
1490 } | |
1491 | |
995 | 1492 |
590 | 1493 static gboolean file_data_sc_add_ci(FileData *fd, FileDataChangeType type) |
1494 { | |
1495 GList *work; | |
806 | 1496 |
590 | 1497 if (fd->parent) fd = fd->parent; |
1498 | |
1499 if (fd->change) return FALSE; | |
806 | 1500 |
590 | 1501 work = fd->sidecar_files; |
1502 while (work) | |
1503 { | |
1504 FileData *sfd = work->data; | |
806 | 1505 |
590 | 1506 if (sfd->change) return FALSE; |
1507 work = work->next; | |
1508 } | |
1509 | |
1510 file_data_add_ci(fd, type, NULL, NULL); | |
1511 | |
1512 work = fd->sidecar_files; | |
1513 while (work) | |
1514 { | |
1515 FileData *sfd = work->data; | |
806 | 1516 |
590 | 1517 file_data_add_ci(sfd, type, NULL, NULL); |
1518 work = work->next; | |
1519 } | |
1520 | |
995 | 1521 return TRUE; |
590 | 1522 } |
1523 | |
1524 static gboolean file_data_sc_check_ci(FileData *fd, FileDataChangeType type) | |
1525 { | |
1526 GList *work; | |
806 | 1527 |
590 | 1528 if (fd->parent) fd = fd->parent; |
1529 | |
806 | 1530 if (!fd->change || fd->change->type != type) return FALSE; |
1531 | |
590 | 1532 work = fd->sidecar_files; |
1533 while (work) | |
1534 { | |
1535 FileData *sfd = work->data; | |
806 | 1536 |
1537 if (!sfd->change || sfd->change->type != type) return FALSE; | |
590 | 1538 work = work->next; |
1539 } | |
806 | 1540 |
590 | 1541 return TRUE; |
1542 } | |
1543 | |
1544 | |
751 | 1545 gboolean file_data_sc_add_ci_copy(FileData *fd, const gchar *dest_path) |
590 | 1546 { |
1547 if (!file_data_sc_add_ci(fd, FILEDATA_CHANGE_COPY)) return FALSE; | |
1548 file_data_sc_update_ci_copy(fd, dest_path); | |
1549 return TRUE; | |
1550 } | |
1551 | |
751 | 1552 gboolean file_data_sc_add_ci_move(FileData *fd, const gchar *dest_path) |
590 | 1553 { |
1554 if (!file_data_sc_add_ci(fd, FILEDATA_CHANGE_MOVE)) return FALSE; | |
1555 file_data_sc_update_ci_move(fd, dest_path); | |
1556 return TRUE; | |
1557 } | |
1558 | |
751 | 1559 gboolean file_data_sc_add_ci_rename(FileData *fd, const gchar *dest_path) |
590 | 1560 { |
1561 if (!file_data_sc_add_ci(fd, FILEDATA_CHANGE_RENAME)) return FALSE; | |
1562 file_data_sc_update_ci_rename(fd, dest_path); | |
1563 return TRUE; | |
1564 } | |
1565 | |
1566 gboolean file_data_sc_add_ci_delete(FileData *fd) | |
1567 { | |
1568 return file_data_sc_add_ci(fd, FILEDATA_CHANGE_DELETE); | |
1569 } | |
1570 | |
753 | 1571 gboolean file_data_sc_add_ci_unspecified(FileData *fd, const gchar *dest_path) |
590 | 1572 { |
753 | 1573 if (!file_data_sc_add_ci(fd, FILEDATA_CHANGE_UNSPECIFIED)) return FALSE; |
1574 file_data_sc_update_ci_unspecified(fd, dest_path); | |
1575 return TRUE; | |
590 | 1576 } |
1577 | |
1205
3ff2aa99108b
use the workflow in utilops.c for metadata approving and writting
nadvornik
parents:
1055
diff
changeset
|
1578 gboolean file_data_add_ci_write_metadata(FileData *fd) |
3ff2aa99108b
use the workflow in utilops.c for metadata approving and writting
nadvornik
parents:
1055
diff
changeset
|
1579 { |
3ff2aa99108b
use the workflow in utilops.c for metadata approving and writting
nadvornik
parents:
1055
diff
changeset
|
1580 return file_data_add_ci(fd, FILEDATA_CHANGE_WRITE_METADATA, NULL, NULL); |
3ff2aa99108b
use the workflow in utilops.c for metadata approving and writting
nadvornik
parents:
1055
diff
changeset
|
1581 } |
3ff2aa99108b
use the workflow in utilops.c for metadata approving and writting
nadvornik
parents:
1055
diff
changeset
|
1582 |
590 | 1583 void file_data_sc_free_ci(FileData *fd) |
1584 { | |
1585 GList *work; | |
806 | 1586 |
590 | 1587 if (fd->parent) fd = fd->parent; |
1588 | |
1589 file_data_free_ci(fd); | |
1590 | |
1591 work = fd->sidecar_files; | |
1592 while (work) | |
1593 { | |
1594 FileData *sfd = work->data; | |
806 | 1595 |
590 | 1596 file_data_free_ci(sfd); |
1597 work = work->next; | |
1598 } | |
1599 } | |
1600 | |
751 | 1601 gboolean file_data_sc_add_ci_delete_list(GList *fd_list) |
1602 { | |
1603 GList *work; | |
1604 gboolean ret = TRUE; | |
806 | 1605 |
751 | 1606 work = fd_list; |
1607 while (work) | |
1608 { | |
1609 FileData *fd = work->data; | |
806 | 1610 |
751 | 1611 if (!file_data_sc_add_ci_delete(fd)) ret = FALSE; |
1612 work = work->next; | |
1613 } | |
806 | 1614 |
751 | 1615 return ret; |
1616 } | |
1617 | |
913 | 1618 static void file_data_sc_revert_ci_list(GList *fd_list) |
751 | 1619 { |
1620 GList *work; | |
806 | 1621 |
751 | 1622 work = fd_list; |
1623 while (work) | |
1624 { | |
1625 FileData *fd = work->data; | |
806 | 1626 |
913 | 1627 file_data_sc_free_ci(fd); |
1628 work = work->prev; | |
1629 } | |
1630 } | |
1631 | |
923 | 1632 static gboolean file_data_sc_add_ci_list_call_func(GList *fd_list, const gchar *dest, gboolean (*func)(FileData *, const gchar *)) |
913 | 1633 { |
1634 GList *work; | |
1635 | |
1636 work = fd_list; | |
1637 while (work) | |
1638 { | |
1639 FileData *fd = work->data; | |
1640 | |
995 | 1641 if (!func(fd, dest)) |
913 | 1642 { |
1643 file_data_sc_revert_ci_list(work->prev); | |
1644 return FALSE; | |
1645 } | |
751 | 1646 work = work->next; |
1647 } | |
806 | 1648 |
913 | 1649 return TRUE; |
751 | 1650 } |
1651 | |
923 | 1652 gboolean file_data_sc_add_ci_copy_list(GList *fd_list, const gchar *dest) |
1653 { | |
1654 return file_data_sc_add_ci_list_call_func(fd_list, dest, file_data_sc_add_ci_copy); | |
1655 } | |
1656 | |
751 | 1657 gboolean file_data_sc_add_ci_move_list(GList *fd_list, const gchar *dest) |
1658 { | |
923 | 1659 return file_data_sc_add_ci_list_call_func(fd_list, dest, file_data_sc_add_ci_move); |
751 | 1660 } |
1661 | |
1662 gboolean file_data_sc_add_ci_rename_list(GList *fd_list, const gchar *dest) | |
1663 { | |
923 | 1664 return file_data_sc_add_ci_list_call_func(fd_list, dest, file_data_sc_add_ci_rename); |
751 | 1665 } |
1666 | |
753 | 1667 gboolean file_data_sc_add_ci_unspecified_list(GList *fd_list, const gchar *dest) |
1668 { | |
923 | 1669 return file_data_sc_add_ci_list_call_func(fd_list, dest, file_data_sc_add_ci_unspecified); |
753 | 1670 } |
1671 | |
1205
3ff2aa99108b
use the workflow in utilops.c for metadata approving and writting
nadvornik
parents:
1055
diff
changeset
|
1672 gboolean file_data_add_ci_write_metadata_list(GList *fd_list) |
3ff2aa99108b
use the workflow in utilops.c for metadata approving and writting
nadvornik
parents:
1055
diff
changeset
|
1673 { |
3ff2aa99108b
use the workflow in utilops.c for metadata approving and writting
nadvornik
parents:
1055
diff
changeset
|
1674 GList *work; |
3ff2aa99108b
use the workflow in utilops.c for metadata approving and writting
nadvornik
parents:
1055
diff
changeset
|
1675 gboolean ret = TRUE; |
3ff2aa99108b
use the workflow in utilops.c for metadata approving and writting
nadvornik
parents:
1055
diff
changeset
|
1676 |
3ff2aa99108b
use the workflow in utilops.c for metadata approving and writting
nadvornik
parents:
1055
diff
changeset
|
1677 work = fd_list; |
3ff2aa99108b
use the workflow in utilops.c for metadata approving and writting
nadvornik
parents:
1055
diff
changeset
|
1678 while (work) |
3ff2aa99108b
use the workflow in utilops.c for metadata approving and writting
nadvornik
parents:
1055
diff
changeset
|
1679 { |
3ff2aa99108b
use the workflow in utilops.c for metadata approving and writting
nadvornik
parents:
1055
diff
changeset
|
1680 FileData *fd = work->data; |
3ff2aa99108b
use the workflow in utilops.c for metadata approving and writting
nadvornik
parents:
1055
diff
changeset
|
1681 |
3ff2aa99108b
use the workflow in utilops.c for metadata approving and writting
nadvornik
parents:
1055
diff
changeset
|
1682 if (!file_data_add_ci_write_metadata(fd)) ret = FALSE; |
3ff2aa99108b
use the workflow in utilops.c for metadata approving and writting
nadvornik
parents:
1055
diff
changeset
|
1683 work = work->next; |
3ff2aa99108b
use the workflow in utilops.c for metadata approving and writting
nadvornik
parents:
1055
diff
changeset
|
1684 } |
3ff2aa99108b
use the workflow in utilops.c for metadata approving and writting
nadvornik
parents:
1055
diff
changeset
|
1685 |
3ff2aa99108b
use the workflow in utilops.c for metadata approving and writting
nadvornik
parents:
1055
diff
changeset
|
1686 return ret; |
3ff2aa99108b
use the workflow in utilops.c for metadata approving and writting
nadvornik
parents:
1055
diff
changeset
|
1687 } |
3ff2aa99108b
use the workflow in utilops.c for metadata approving and writting
nadvornik
parents:
1055
diff
changeset
|
1688 |
3ff2aa99108b
use the workflow in utilops.c for metadata approving and writting
nadvornik
parents:
1055
diff
changeset
|
1689 void file_data_free_ci_list(GList *fd_list) |
3ff2aa99108b
use the workflow in utilops.c for metadata approving and writting
nadvornik
parents:
1055
diff
changeset
|
1690 { |
3ff2aa99108b
use the workflow in utilops.c for metadata approving and writting
nadvornik
parents:
1055
diff
changeset
|
1691 GList *work; |
3ff2aa99108b
use the workflow in utilops.c for metadata approving and writting
nadvornik
parents:
1055
diff
changeset
|
1692 |
3ff2aa99108b
use the workflow in utilops.c for metadata approving and writting
nadvornik
parents:
1055
diff
changeset
|
1693 work = fd_list; |
3ff2aa99108b
use the workflow in utilops.c for metadata approving and writting
nadvornik
parents:
1055
diff
changeset
|
1694 while (work) |
3ff2aa99108b
use the workflow in utilops.c for metadata approving and writting
nadvornik
parents:
1055
diff
changeset
|
1695 { |
3ff2aa99108b
use the workflow in utilops.c for metadata approving and writting
nadvornik
parents:
1055
diff
changeset
|
1696 FileData *fd = work->data; |
3ff2aa99108b
use the workflow in utilops.c for metadata approving and writting
nadvornik
parents:
1055
diff
changeset
|
1697 |
3ff2aa99108b
use the workflow in utilops.c for metadata approving and writting
nadvornik
parents:
1055
diff
changeset
|
1698 file_data_free_ci(fd); |
3ff2aa99108b
use the workflow in utilops.c for metadata approving and writting
nadvornik
parents:
1055
diff
changeset
|
1699 work = work->next; |
3ff2aa99108b
use the workflow in utilops.c for metadata approving and writting
nadvornik
parents:
1055
diff
changeset
|
1700 } |
3ff2aa99108b
use the workflow in utilops.c for metadata approving and writting
nadvornik
parents:
1055
diff
changeset
|
1701 } |
3ff2aa99108b
use the workflow in utilops.c for metadata approving and writting
nadvornik
parents:
1055
diff
changeset
|
1702 |
751 | 1703 void file_data_sc_free_ci_list(GList *fd_list) |
1704 { | |
1705 GList *work; | |
806 | 1706 |
751 | 1707 work = fd_list; |
1708 while (work) | |
1709 { | |
1710 FileData *fd = work->data; | |
806 | 1711 |
751 | 1712 file_data_sc_free_ci(fd); |
1713 work = work->next; | |
1714 } | |
1715 } | |
590 | 1716 |
995 | 1717 /* |
590 | 1718 * update existing fd->change, it will be used from dialog callbacks for interactive editing |
1719 * fails if fd->change does not exist or the change type does not match | |
1720 */ | |
1721 | |
899
5d9c0b4e6d5f
fixed the case when a renamed file is detected by directory scanning
nadvornik
parents:
896
diff
changeset
|
1722 static void file_data_update_planned_change_hash(FileData *fd, const gchar *old_path, gchar *new_path) |
5d9c0b4e6d5f
fixed the case when a renamed file is detected by directory scanning
nadvornik
parents:
896
diff
changeset
|
1723 { |
5d9c0b4e6d5f
fixed the case when a renamed file is detected by directory scanning
nadvornik
parents:
896
diff
changeset
|
1724 FileDataChangeType type = fd->change->type; |
5d9c0b4e6d5f
fixed the case when a renamed file is detected by directory scanning
nadvornik
parents:
896
diff
changeset
|
1725 |
5d9c0b4e6d5f
fixed the case when a renamed file is detected by directory scanning
nadvornik
parents:
896
diff
changeset
|
1726 if (type == FILEDATA_CHANGE_MOVE || type == FILEDATA_CHANGE_RENAME) |
5d9c0b4e6d5f
fixed the case when a renamed file is detected by directory scanning
nadvornik
parents:
896
diff
changeset
|
1727 { |
5d9c0b4e6d5f
fixed the case when a renamed file is detected by directory scanning
nadvornik
parents:
896
diff
changeset
|
1728 FileData *ofd; |
5d9c0b4e6d5f
fixed the case when a renamed file is detected by directory scanning
nadvornik
parents:
896
diff
changeset
|
1729 |
5d9c0b4e6d5f
fixed the case when a renamed file is detected by directory scanning
nadvornik
parents:
896
diff
changeset
|
1730 if (!file_data_planned_change_hash) |
5d9c0b4e6d5f
fixed the case when a renamed file is detected by directory scanning
nadvornik
parents:
896
diff
changeset
|
1731 file_data_planned_change_hash = g_hash_table_new(g_str_hash, g_str_equal); |
5d9c0b4e6d5f
fixed the case when a renamed file is detected by directory scanning
nadvornik
parents:
896
diff
changeset
|
1732 |
5d9c0b4e6d5f
fixed the case when a renamed file is detected by directory scanning
nadvornik
parents:
896
diff
changeset
|
1733 if (old_path && g_hash_table_lookup(file_data_planned_change_hash, old_path) == fd) |
5d9c0b4e6d5f
fixed the case when a renamed file is detected by directory scanning
nadvornik
parents:
896
diff
changeset
|
1734 { |
5d9c0b4e6d5f
fixed the case when a renamed file is detected by directory scanning
nadvornik
parents:
896
diff
changeset
|
1735 DEBUG_1("planned change: removing %s -> %s", old_path, fd->path); |
5d9c0b4e6d5f
fixed the case when a renamed file is detected by directory scanning
nadvornik
parents:
896
diff
changeset
|
1736 g_hash_table_remove(file_data_planned_change_hash, old_path); |
5d9c0b4e6d5f
fixed the case when a renamed file is detected by directory scanning
nadvornik
parents:
896
diff
changeset
|
1737 file_data_unref(fd); |
5d9c0b4e6d5f
fixed the case when a renamed file is detected by directory scanning
nadvornik
parents:
896
diff
changeset
|
1738 } |
5d9c0b4e6d5f
fixed the case when a renamed file is detected by directory scanning
nadvornik
parents:
896
diff
changeset
|
1739 |
918 | 1740 ofd = g_hash_table_lookup(file_data_planned_change_hash, new_path); |
1741 if (ofd != fd) | |
899
5d9c0b4e6d5f
fixed the case when a renamed file is detected by directory scanning
nadvornik
parents:
896
diff
changeset
|
1742 { |
5d9c0b4e6d5f
fixed the case when a renamed file is detected by directory scanning
nadvornik
parents:
896
diff
changeset
|
1743 if (ofd) |
5d9c0b4e6d5f
fixed the case when a renamed file is detected by directory scanning
nadvornik
parents:
896
diff
changeset
|
1744 { |
5d9c0b4e6d5f
fixed the case when a renamed file is detected by directory scanning
nadvornik
parents:
896
diff
changeset
|
1745 DEBUG_1("planned change: replacing %s -> %s", new_path, ofd->path); |
5d9c0b4e6d5f
fixed the case when a renamed file is detected by directory scanning
nadvornik
parents:
896
diff
changeset
|
1746 g_hash_table_remove(file_data_planned_change_hash, new_path); |
5d9c0b4e6d5f
fixed the case when a renamed file is detected by directory scanning
nadvornik
parents:
896
diff
changeset
|
1747 file_data_unref(ofd); |
5d9c0b4e6d5f
fixed the case when a renamed file is detected by directory scanning
nadvornik
parents:
896
diff
changeset
|
1748 } |
5d9c0b4e6d5f
fixed the case when a renamed file is detected by directory scanning
nadvornik
parents:
896
diff
changeset
|
1749 |
5d9c0b4e6d5f
fixed the case when a renamed file is detected by directory scanning
nadvornik
parents:
896
diff
changeset
|
1750 DEBUG_1("planned change: inserting %s -> %s", new_path, fd->path); |
5d9c0b4e6d5f
fixed the case when a renamed file is detected by directory scanning
nadvornik
parents:
896
diff
changeset
|
1751 file_data_ref(fd); |
5d9c0b4e6d5f
fixed the case when a renamed file is detected by directory scanning
nadvornik
parents:
896
diff
changeset
|
1752 g_hash_table_insert(file_data_planned_change_hash, new_path, fd); |
5d9c0b4e6d5f
fixed the case when a renamed file is detected by directory scanning
nadvornik
parents:
896
diff
changeset
|
1753 } |
5d9c0b4e6d5f
fixed the case when a renamed file is detected by directory scanning
nadvornik
parents:
896
diff
changeset
|
1754 } |
5d9c0b4e6d5f
fixed the case when a renamed file is detected by directory scanning
nadvornik
parents:
896
diff
changeset
|
1755 } |
5d9c0b4e6d5f
fixed the case when a renamed file is detected by directory scanning
nadvornik
parents:
896
diff
changeset
|
1756 |
751 | 1757 static void file_data_update_ci_dest(FileData *fd, const gchar *dest_path) |
590 | 1758 { |
899
5d9c0b4e6d5f
fixed the case when a renamed file is detected by directory scanning
nadvornik
parents:
896
diff
changeset
|
1759 gchar *old_path = fd->change->dest; |
918 | 1760 |
590 | 1761 fd->change->dest = g_strdup(dest_path); |
899
5d9c0b4e6d5f
fixed the case when a renamed file is detected by directory scanning
nadvornik
parents:
896
diff
changeset
|
1762 file_data_update_planned_change_hash(fd, old_path, fd->change->dest); |
5d9c0b4e6d5f
fixed the case when a renamed file is detected by directory scanning
nadvornik
parents:
896
diff
changeset
|
1763 g_free(old_path); |
590 | 1764 } |
1765 | |
751 | 1766 static void file_data_update_ci_dest_preserve_ext(FileData *fd, const gchar *dest_path) |
590 | 1767 { |
1000
4fe8f9656107
For the sake of consistency, use glib basic types everywhere.
zas_
parents:
998
diff
changeset
|
1768 const gchar *extension = extension_from_path(fd->change->source); |
751 | 1769 gchar *base = remove_extension_from_path(dest_path); |
899
5d9c0b4e6d5f
fixed the case when a renamed file is detected by directory scanning
nadvornik
parents:
896
diff
changeset
|
1770 gchar *old_path = fd->change->dest; |
806 | 1771 |
920
408879d2a660
Use g_strconcat() instead of g_strdup_printf("%s%s", ...).
zas_
parents:
918
diff
changeset
|
1772 fd->change->dest = g_strconcat(base, extension, NULL); |
899
5d9c0b4e6d5f
fixed the case when a renamed file is detected by directory scanning
nadvornik
parents:
896
diff
changeset
|
1773 file_data_update_planned_change_hash(fd, old_path, fd->change->dest); |
806 | 1774 |
899
5d9c0b4e6d5f
fixed the case when a renamed file is detected by directory scanning
nadvornik
parents:
896
diff
changeset
|
1775 g_free(old_path); |
751 | 1776 g_free(base); |
590 | 1777 } |
1778 | |
751 | 1779 static void file_data_sc_update_ci(FileData *fd, const gchar *dest_path) |
590 | 1780 { |
1781 GList *work; | |
751 | 1782 gchar *dest_path_full = NULL; |
806 | 1783 |
590 | 1784 if (fd->parent) fd = fd->parent; |
1785 | |
995 | 1786 if (!dest_path) |
934 | 1787 { |
1788 dest_path = fd->path; | |
1789 } | |
1790 else if (!strchr(dest_path, G_DIR_SEPARATOR)) /* we got only filename, not a full path */ | |
751 | 1791 { |
1792 gchar *dir = remove_level_from_path(fd->path); | |
806 | 1793 |
751 | 1794 dest_path_full = g_build_filename(dir, dest_path, NULL); |
1795 g_free(dir); | |
1796 dest_path = dest_path_full; | |
1797 } | |
934 | 1798 else if (fd->change->type != FILEDATA_CHANGE_RENAME && isdir(dest_path)) /* rename should not move files between directories */ |
751 | 1799 { |
1800 dest_path_full = g_build_filename(dest_path, fd->name, NULL); | |
1801 dest_path = dest_path_full; | |
1802 } | |
806 | 1803 |
1804 file_data_update_ci_dest(fd, dest_path); | |
751 | 1805 |
590 | 1806 work = fd->sidecar_files; |
1807 while (work) | |
1808 { | |
1809 FileData *sfd = work->data; | |
806 | 1810 |
590 | 1811 file_data_update_ci_dest_preserve_ext(sfd, dest_path); |
1812 work = work->next; | |
1813 } | |
806 | 1814 |
751 | 1815 g_free(dest_path_full); |
590 | 1816 } |
1817 | |
1422 | 1818 static gboolean file_data_sc_check_update_ci(FileData *fd, const gchar *dest_path, FileDataChangeType type) |
590 | 1819 { |
950 | 1820 if (!file_data_sc_check_ci(fd, type)) return FALSE; |
590 | 1821 file_data_sc_update_ci(fd, dest_path); |
1822 return TRUE; | |
1823 } | |
1824 | |
1422 | 1825 gboolean file_data_sc_update_ci_copy(FileData *fd, const gchar *dest_path) |
950 | 1826 { |
1827 return file_data_sc_check_update_ci(fd, dest_path, FILEDATA_CHANGE_COPY); | |
1828 } | |
1829 | |
1422 | 1830 gboolean file_data_sc_update_ci_move(FileData *fd, const gchar *dest_path) |
950 | 1831 { |
1832 return file_data_sc_check_update_ci(fd, dest_path, FILEDATA_CHANGE_MOVE); | |
1833 } | |
1834 | |
1422 | 1835 gboolean file_data_sc_update_ci_rename(FileData *fd, const gchar *dest_path) |
590 | 1836 { |
950 | 1837 return file_data_sc_check_update_ci(fd, dest_path, FILEDATA_CHANGE_RENAME); |
590 | 1838 } |
1839 | |
1422 | 1840 gboolean file_data_sc_update_ci_unspecified(FileData *fd, const gchar *dest_path) |
753 | 1841 { |
950 | 1842 return file_data_sc_check_update_ci(fd, dest_path, FILEDATA_CHANGE_UNSPECIFIED); |
753 | 1843 } |
1844 | |
950 | 1845 static gboolean file_data_sc_update_ci_list_call_func(GList *fd_list, |
1846 const gchar *dest, | |
1847 gboolean (*func)(FileData *, const gchar *)) | |
751 | 1848 { |
1849 GList *work; | |
1850 gboolean ret = TRUE; | |
806 | 1851 |
751 | 1852 work = fd_list; |
1853 while (work) | |
1854 { | |
1855 FileData *fd = work->data; | |
806 | 1856 |
950 | 1857 if (!func(fd, dest)) ret = FALSE; |
751 | 1858 work = work->next; |
1859 } | |
806 | 1860 |
751 | 1861 return ret; |
1862 } | |
1863 | |
950 | 1864 gboolean file_data_sc_update_ci_move_list(GList *fd_list, const gchar *dest) |
1865 { | |
1866 return file_data_sc_update_ci_list_call_func(fd_list, dest, file_data_sc_update_ci_move); | |
1867 } | |
1868 | |
751 | 1869 gboolean file_data_sc_update_ci_copy_list(GList *fd_list, const gchar *dest) |
1870 { | |
950 | 1871 return file_data_sc_update_ci_list_call_func(fd_list, dest, file_data_sc_update_ci_copy); |
751 | 1872 } |
1873 | |
753 | 1874 gboolean file_data_sc_update_ci_unspecified_list(GList *fd_list, const gchar *dest) |
1875 { | |
950 | 1876 return file_data_sc_update_ci_list_call_func(fd_list, dest, file_data_sc_update_ci_unspecified); |
753 | 1877 } |
1878 | |
590 | 1879 |
1880 /* | |
929 | 1881 * verify source and dest paths - dest image exists, etc. |
590 | 1882 * it should detect all possible problems with the planned operation |
1883 */ | |
914
9427c91951e8
basic infrastructure for early error and dangerous operations checking
nadvornik
parents:
913
diff
changeset
|
1884 |
928 | 1885 gint file_data_verify_ci(FileData *fd) |
914
9427c91951e8
basic infrastructure for early error and dangerous operations checking
nadvornik
parents:
913
diff
changeset
|
1886 { |
9427c91951e8
basic infrastructure for early error and dangerous operations checking
nadvornik
parents:
913
diff
changeset
|
1887 gint ret = CHANGE_OK; |
929 | 1888 gchar *dir; |
914
9427c91951e8
basic infrastructure for early error and dangerous operations checking
nadvornik
parents:
913
diff
changeset
|
1889 |
942 | 1890 if (!fd->change) |
1891 { | |
1892 DEBUG_1("Change checked: no change info: %s", fd->path); | |
995 | 1893 return ret; |
942 | 1894 } |
929 | 1895 |
1224 | 1896 if (!isname(fd->path)) |
929 | 1897 { |
1898 /* this probably should not happen */ | |
1899 ret |= CHANGE_NO_SRC; | |
1900 DEBUG_1("Change checked: file does not exist: %s", fd->path); | |
995 | 1901 return ret; |
929 | 1902 } |
1903 | |
1904 dir = remove_level_from_path(fd->path); | |
914
9427c91951e8
basic infrastructure for early error and dangerous operations checking
nadvornik
parents:
913
diff
changeset
|
1905 |
995 | 1906 if (fd->change->type != FILEDATA_CHANGE_DELETE && |
1662
7a4034d32503
warn if another operation is performed on a file with unsaved metadata
nadvornik
parents:
1649
diff
changeset
|
1907 fd->change->type != FILEDATA_CHANGE_MOVE && /* the unsaved metadata should survive move and rename operations */ |
7a4034d32503
warn if another operation is performed on a file with unsaved metadata
nadvornik
parents:
1649
diff
changeset
|
1908 fd->change->type != FILEDATA_CHANGE_RENAME && |
7a4034d32503
warn if another operation is performed on a file with unsaved metadata
nadvornik
parents:
1649
diff
changeset
|
1909 fd->change->type != FILEDATA_CHANGE_WRITE_METADATA && |
7a4034d32503
warn if another operation is performed on a file with unsaved metadata
nadvornik
parents:
1649
diff
changeset
|
1910 fd->modified_xmp) |
7a4034d32503
warn if another operation is performed on a file with unsaved metadata
nadvornik
parents:
1649
diff
changeset
|
1911 { |
7a4034d32503
warn if another operation is performed on a file with unsaved metadata
nadvornik
parents:
1649
diff
changeset
|
1912 ret |= CHANGE_WARN_UNSAVED_META; |
7a4034d32503
warn if another operation is performed on a file with unsaved metadata
nadvornik
parents:
1649
diff
changeset
|
1913 DEBUG_1("Change checked: unsaved metadata: %s", fd->path); |
7a4034d32503
warn if another operation is performed on a file with unsaved metadata
nadvornik
parents:
1649
diff
changeset
|
1914 } |
7a4034d32503
warn if another operation is performed on a file with unsaved metadata
nadvornik
parents:
1649
diff
changeset
|
1915 |
7a4034d32503
warn if another operation is performed on a file with unsaved metadata
nadvornik
parents:
1649
diff
changeset
|
1916 if (fd->change->type != FILEDATA_CHANGE_DELETE && |
1211 | 1917 fd->change->type != FILEDATA_CHANGE_WRITE_METADATA && |
929 | 1918 !access_file(fd->path, R_OK)) |
1919 { | |
1920 ret |= CHANGE_NO_READ_PERM; | |
1921 DEBUG_1("Change checked: no read permission: %s", fd->path); | |
1922 } | |
1923 else if ((fd->change->type == FILEDATA_CHANGE_DELETE || fd->change->type == FILEDATA_CHANGE_MOVE) && | |
995 | 1924 !access_file(dir, W_OK)) |
914
9427c91951e8
basic infrastructure for early error and dangerous operations checking
nadvornik
parents:
913
diff
changeset
|
1925 { |
929 | 1926 ret |= CHANGE_NO_WRITE_PERM_DIR; |
1927 DEBUG_1("Change checked: source dir is readonly: %s", fd->path); | |
1928 } | |
995 | 1929 else if (fd->change->type != FILEDATA_CHANGE_COPY && |
1930 fd->change->type != FILEDATA_CHANGE_UNSPECIFIED && | |
1211 | 1931 fd->change->type != FILEDATA_CHANGE_WRITE_METADATA && |
995 | 1932 !access_file(fd->path, W_OK)) |
929 | 1933 { |
1934 ret |= CHANGE_WARN_NO_WRITE_PERM; | |
1935 DEBUG_1("Change checked: no write permission: %s", fd->path); | |
1936 } | |
1211 | 1937 /* WRITE_METADATA is special because it can be configured to silently write to ~/.geeqie/... |
1938 - that means that there are no hard errors and warnings can be disabled | |
1224 | 1939 - the destination is determined during the check |
1211 | 1940 */ |
1224 | 1941 else if (fd->change->type == FILEDATA_CHANGE_WRITE_METADATA) |
1211 | 1942 { |
1224 | 1943 /* determine destination file */ |
1944 gboolean have_dest = FALSE; | |
1945 gchar *dest_dir = NULL; | |
1946 | |
1947 if (options->metadata.save_in_image_file) | |
1211 | 1948 { |
1224 | 1949 if (file_data_can_write_directly(fd)) |
1950 { | |
1951 /* we can write the file directly */ | |
1952 if (access_file(fd->path, W_OK)) | |
1953 { | |
1954 have_dest = TRUE; | |
1955 } | |
1956 else | |
1957 { | |
1958 if (options->metadata.warn_on_write_problems) | |
1959 { | |
1960 ret |= CHANGE_WARN_NO_WRITE_PERM; | |
1961 DEBUG_1("Change checked: file is not writable: %s", fd->path); | |
1962 } | |
1963 } | |
1964 } | |
1965 else if (file_data_can_write_sidecar(fd)) | |
1966 { | |
1967 /* we can write sidecar */ | |
1968 gchar *sidecar = file_data_get_sidecar_path(fd, FALSE); | |
1969 if (access_file(sidecar, W_OK) || (!isname(sidecar) && access_file(dir, W_OK))) | |
1970 { | |
1971 file_data_update_ci_dest(fd, sidecar); | |
1972 have_dest = TRUE; | |
1973 } | |
1974 else | |
1975 { | |
1976 if (options->metadata.warn_on_write_problems) | |
1977 { | |
1978 ret |= CHANGE_WARN_NO_WRITE_PERM; | |
1979 DEBUG_1("Change checked: file is not writable: %s", sidecar); | |
1980 } | |
1981 } | |
1982 g_free(sidecar); | |
1983 } | |
1211 | 1984 } |
1985 | |
1224 | 1986 if (!have_dest) |
1211 | 1987 { |
1224 | 1988 /* write private metadata file under ~/.geeqie */ |
1989 | |
1990 /* If an existing metadata file exists, we will try writing to | |
1991 * it's location regardless of the user's preference. | |
1992 */ | |
1667 | 1993 gchar *metadata_path = NULL; |
1994 #ifdef HAVE_EXIV2 | |
1995 /* but ignore XMP if we are not able to write it */ | |
1996 metadata_path = cache_find_location(CACHE_TYPE_XMP_METADATA, fd->path); | |
1997 #endif | |
1224 | 1998 if (!metadata_path) metadata_path = cache_find_location(CACHE_TYPE_METADATA, fd->path); |
1999 | |
2000 if (metadata_path && !access_file(metadata_path, W_OK)) | |
2001 { | |
2002 g_free(metadata_path); | |
2003 metadata_path = NULL; | |
2004 } | |
2005 | |
2006 if (!metadata_path) | |
2007 { | |
2008 mode_t mode = 0755; | |
2009 | |
2010 dest_dir = cache_get_location(CACHE_TYPE_METADATA, fd->path, FALSE, &mode); | |
2011 if (recursive_mkdir_if_not_exists(dest_dir, mode)) | |
2012 { | |
2013 gchar *filename = g_strconcat(fd->name, options->metadata.save_legacy_format ? GQ_CACHE_EXT_METADATA : GQ_CACHE_EXT_XMP_METADATA, NULL); | |
2014 | |
2015 metadata_path = g_build_filename(dest_dir, filename, NULL); | |
2016 g_free(filename); | |
2017 } | |
2018 } | |
2019 if (access_file(metadata_path, W_OK) || (!isname(metadata_path) && access_file(dest_dir, W_OK))) | |
2020 { | |
2021 file_data_update_ci_dest(fd, metadata_path); | |
2022 have_dest = TRUE; | |
2023 } | |
2024 else | |
2025 { | |
2026 ret |= CHANGE_NO_WRITE_PERM_DEST; | |
2027 DEBUG_1("Change checked: file is not writable: %s", metadata_path); | |
2028 } | |
2029 g_free(metadata_path); | |
1211 | 2030 } |
1224 | 2031 g_free(dest_dir); |
1211 | 2032 } |
2033 | |
1224 | 2034 if (fd->change->dest && fd->change->type != FILEDATA_CHANGE_WRITE_METADATA) |
914
9427c91951e8
basic infrastructure for early error and dangerous operations checking
nadvornik
parents:
913
diff
changeset
|
2035 { |
953 | 2036 gboolean same; |
951 | 2037 gchar *dest_dir; |
955 | 2038 |
953 | 2039 same = (strcmp(fd->path, fd->change->dest) == 0); |
2040 | |
955 | 2041 if (!same) |
933 | 2042 { |
955 | 2043 const gchar *dest_ext = extension_from_path(fd->change->dest); |
2044 if (!dest_ext) dest_ext = ""; | |
933 | 2045 |
1307 | 2046 if (g_ascii_strcasecmp(fd->extension, dest_ext) != 0) |
955 | 2047 { |
2048 ret |= CHANGE_WARN_CHANGED_EXT; | |
2049 DEBUG_1("Change checked: source and destination have different extensions: %s -> %s", fd->path, fd->change->dest); | |
2050 } | |
2051 } | |
2052 else | |
933 | 2053 { |
955 | 2054 if (fd->change->type != FILEDATA_CHANGE_UNSPECIFIED) /* FIXME this is now needed for running editors */ |
2055 { | |
2056 ret |= CHANGE_WARN_SAME; | |
2057 DEBUG_1("Change checked: source and destination are the same: %s -> %s", fd->path, fd->change->dest); | |
2058 } | |
2059 } | |
933 | 2060 |
951 | 2061 dest_dir = remove_level_from_path(fd->change->dest); |
2062 | |
995 | 2063 if (!isdir(dest_dir)) |
929 | 2064 { |
2065 ret |= CHANGE_NO_DEST_DIR; | |
2066 DEBUG_1("Change checked: destination dir does not exist: %s -> %s", fd->path, fd->change->dest); | |
2067 } | |
2068 else if (!access_file(dest_dir, W_OK)) | |
2069 { | |
2070 ret |= CHANGE_NO_WRITE_PERM_DEST_DIR; | |
2071 DEBUG_1("Change checked: destination dir is readonly: %s -> %s", fd->path, fd->change->dest); | |
2072 } | |
955 | 2073 else if (!same) |
929 | 2074 { |
955 | 2075 if (isfile(fd->change->dest)) |
952 | 2076 { |
955 | 2077 if (!access_file(fd->change->dest, W_OK)) |
2078 { | |
2079 ret |= CHANGE_NO_WRITE_PERM_DEST; | |
2080 DEBUG_1("Change checked: destination file exists and is readonly: %s -> %s", fd->path, fd->change->dest); | |
2081 } | |
2082 else | |
2083 { | |
2084 ret |= CHANGE_WARN_DEST_EXISTS; | |
2085 DEBUG_1("Change checked: destination exists: %s -> %s", fd->path, fd->change->dest); | |
2086 } | |
952 | 2087 } |
955 | 2088 else if (isdir(fd->change->dest)) |
952 | 2089 { |
955 | 2090 ret |= CHANGE_DEST_EXISTS; |
952 | 2091 DEBUG_1("Change checked: destination exists: %s -> %s", fd->path, fd->change->dest); |
2092 } | |
929 | 2093 } |
951 | 2094 |
2095 g_free(dest_dir); | |
914
9427c91951e8
basic infrastructure for early error and dangerous operations checking
nadvornik
parents:
913
diff
changeset
|
2096 } |
9427c91951e8
basic infrastructure for early error and dangerous operations checking
nadvornik
parents:
913
diff
changeset
|
2097 |
9427c91951e8
basic infrastructure for early error and dangerous operations checking
nadvornik
parents:
913
diff
changeset
|
2098 fd->change->error = ret; |
9427c91951e8
basic infrastructure for early error and dangerous operations checking
nadvornik
parents:
913
diff
changeset
|
2099 if (ret == 0) DEBUG_1("Change checked: OK: %s", fd->path); |
9427c91951e8
basic infrastructure for early error and dangerous operations checking
nadvornik
parents:
913
diff
changeset
|
2100 |
929 | 2101 g_free(dir); |
914
9427c91951e8
basic infrastructure for early error and dangerous operations checking
nadvornik
parents:
913
diff
changeset
|
2102 return ret; |
9427c91951e8
basic infrastructure for early error and dangerous operations checking
nadvornik
parents:
913
diff
changeset
|
2103 } |
9427c91951e8
basic infrastructure for early error and dangerous operations checking
nadvornik
parents:
913
diff
changeset
|
2104 |
995 | 2105 |
928 | 2106 gint file_data_sc_verify_ci(FileData *fd) |
590 | 2107 { |
914
9427c91951e8
basic infrastructure for early error and dangerous operations checking
nadvornik
parents:
913
diff
changeset
|
2108 GList *work; |
928 | 2109 gint ret; |
914
9427c91951e8
basic infrastructure for early error and dangerous operations checking
nadvornik
parents:
913
diff
changeset
|
2110 |
928 | 2111 ret = file_data_verify_ci(fd); |
914
9427c91951e8
basic infrastructure for early error and dangerous operations checking
nadvornik
parents:
913
diff
changeset
|
2112 |
9427c91951e8
basic infrastructure for early error and dangerous operations checking
nadvornik
parents:
913
diff
changeset
|
2113 work = fd->sidecar_files; |
9427c91951e8
basic infrastructure for early error and dangerous operations checking
nadvornik
parents:
913
diff
changeset
|
2114 while (work) |
9427c91951e8
basic infrastructure for early error and dangerous operations checking
nadvornik
parents:
913
diff
changeset
|
2115 { |
9427c91951e8
basic infrastructure for early error and dangerous operations checking
nadvornik
parents:
913
diff
changeset
|
2116 FileData *sfd = work->data; |
9427c91951e8
basic infrastructure for early error and dangerous operations checking
nadvornik
parents:
913
diff
changeset
|
2117 |
928 | 2118 ret |= file_data_verify_ci(sfd); |
914
9427c91951e8
basic infrastructure for early error and dangerous operations checking
nadvornik
parents:
913
diff
changeset
|
2119 work = work->next; |
9427c91951e8
basic infrastructure for early error and dangerous operations checking
nadvornik
parents:
913
diff
changeset
|
2120 } |
9427c91951e8
basic infrastructure for early error and dangerous operations checking
nadvornik
parents:
913
diff
changeset
|
2121 |
9427c91951e8
basic infrastructure for early error and dangerous operations checking
nadvornik
parents:
913
diff
changeset
|
2122 return ret; |
590 | 2123 } |
2124 | |
928 | 2125 gchar *file_data_get_error_string(gint error) |
2126 { | |
2127 GString *result = g_string_new(""); | |
590 | 2128 |
929 | 2129 if (error & CHANGE_NO_SRC) |
2130 { | |
2131 if (result->len > 0) g_string_append(result, ", "); | |
2132 g_string_append(result, _("file or directory does not exist")); | |
2133 } | |
2134 | |
2135 if (error & CHANGE_DEST_EXISTS) | |
2136 { | |
2137 if (result->len > 0) g_string_append(result, ", "); | |
2138 g_string_append(result, _("destination already exists")); | |
2139 } | |
2140 | |
2141 if (error & CHANGE_NO_WRITE_PERM_DEST) | |
2142 { | |
2143 if (result->len > 0) g_string_append(result, ", "); | |
2144 g_string_append(result, _("destination can't be overwritten")); | |
2145 } | |
2146 | |
2147 if (error & CHANGE_NO_WRITE_PERM_DEST_DIR) | |
2148 { | |
2149 if (result->len > 0) g_string_append(result, ", "); | |
2150 g_string_append(result, _("destination directory is not writable")); | |
2151 } | |
2152 | |
2153 if (error & CHANGE_NO_DEST_DIR) | |
2154 { | |
2155 if (result->len > 0) g_string_append(result, ", "); | |
2156 g_string_append(result, _("destination directory does not exist")); | |
2157 } | |
2158 | |
2159 if (error & CHANGE_NO_WRITE_PERM_DIR) | |
2160 { | |
2161 if (result->len > 0) g_string_append(result, ", "); | |
2162 g_string_append(result, _("source directory is not writable")); | |
2163 } | |
2164 | |
2165 if (error & CHANGE_NO_READ_PERM) | |
928 | 2166 { |
2167 if (result->len > 0) g_string_append(result, ", "); | |
2168 g_string_append(result, _("no read permission")); | |
2169 } | |
2170 | |
929 | 2171 if (error & CHANGE_WARN_NO_WRITE_PERM) |
928 | 2172 { |
2173 if (result->len > 0) g_string_append(result, ", "); | |
929 | 2174 g_string_append(result, _("file is readonly")); |
2175 } | |
2176 | |
2177 if (error & CHANGE_WARN_DEST_EXISTS) | |
2178 { | |
2179 if (result->len > 0) g_string_append(result, ", "); | |
2180 g_string_append(result, _("destination already exists and will be overwritten")); | |
2181 } | |
933 | 2182 |
929 | 2183 if (error & CHANGE_WARN_SAME) |
2184 { | |
2185 if (result->len > 0) g_string_append(result, ", "); | |
948 | 2186 g_string_append(result, _("source and destination are the same")); |
928 | 2187 } |
2188 | |
933 | 2189 if (error & CHANGE_WARN_CHANGED_EXT) |
2190 { | |
2191 if (result->len > 0) g_string_append(result, ", "); | |
2192 g_string_append(result, _("source and destination have different extension")); | |
2193 } | |
2194 | |
1662
7a4034d32503
warn if another operation is performed on a file with unsaved metadata
nadvornik
parents:
1649
diff
changeset
|
2195 if (error & CHANGE_WARN_UNSAVED_META) |
7a4034d32503
warn if another operation is performed on a file with unsaved metadata
nadvornik
parents:
1649
diff
changeset
|
2196 { |
7a4034d32503
warn if another operation is performed on a file with unsaved metadata
nadvornik
parents:
1649
diff
changeset
|
2197 if (result->len > 0) g_string_append(result, ", "); |
7a4034d32503
warn if another operation is performed on a file with unsaved metadata
nadvornik
parents:
1649
diff
changeset
|
2198 g_string_append(result, _("there are unsaved metadata changes for the file")); |
7a4034d32503
warn if another operation is performed on a file with unsaved metadata
nadvornik
parents:
1649
diff
changeset
|
2199 } |
7a4034d32503
warn if another operation is performed on a file with unsaved metadata
nadvornik
parents:
1649
diff
changeset
|
2200 |
928 | 2201 return g_string_free(result, FALSE); |
2202 } | |
2203 | |
1205
3ff2aa99108b
use the workflow in utilops.c for metadata approving and writting
nadvornik
parents:
1055
diff
changeset
|
2204 gint file_data_verify_ci_list(GList *list, gchar **desc, gboolean with_sidecars) |
928 | 2205 { |
956 | 2206 GList *work; |
928 | 2207 gint all_errors = 0; |
995 | 2208 gint common_errors = ~0; |
928 | 2209 gint num; |
2210 gint *errors; | |
2211 gint i; | |
2212 | |
2213 if (!list) return 0; | |
2214 | |
2215 num = g_list_length(list); | |
2216 errors = g_new(int, num); | |
956 | 2217 work = list; |
928 | 2218 i = 0; |
2219 while (work) | |
2220 { | |
2221 FileData *fd; | |
2222 gint error; | |
2223 | |
2224 fd = work->data; | |
2225 work = work->next; | |
2226 | |
1205
3ff2aa99108b
use the workflow in utilops.c for metadata approving and writting
nadvornik
parents:
1055
diff
changeset
|
2227 error = with_sidecars ? file_data_sc_verify_ci(fd) : file_data_verify_ci(fd); |
928 | 2228 all_errors |= error; |
2229 common_errors &= error; | |
2230 | |
2231 errors[i] = error; | |
2232 | |
2233 i++; | |
2234 } | |
2235 | |
2236 if (desc && all_errors) | |
2237 { | |
956 | 2238 GList *work; |
928 | 2239 GString *result = g_string_new(""); |
2240 | |
2241 if (common_errors) | |
2242 { | |
2243 gchar *str = file_data_get_error_string(common_errors); | |
2244 g_string_append(result, str); | |
2245 g_string_append(result, "\n"); | |
2246 g_free(str); | |
2247 } | |
2248 | |
956 | 2249 work = list; |
928 | 2250 i = 0; |
2251 while (work) | |
2252 { | |
2253 FileData *fd; | |
2254 gint error; | |
2255 | |
2256 fd = work->data; | |
2257 work = work->next; | |
2258 | |
2259 error = errors[i] & ~common_errors; | |
2260 | |
2261 if (error) | |
2262 { | |
2263 gchar *str = file_data_get_error_string(error); | |
2264 g_string_append_printf(result, "%s: %s\n", fd->name, str); | |
2265 g_free(str); | |
2266 } | |
2267 i++; | |
2268 } | |
2269 *desc = g_string_free(result, FALSE); | |
2270 } | |
2271 | |
2272 g_free(errors); | |
2273 return all_errors; | |
2274 } | |
590 | 2275 |
2276 | |
2277 /* | |
2278 * perform the change described by FileFataChangeInfo | |
995 | 2279 * it is used for internal operations, |
590 | 2280 * this function actually operates with files on the filesystem |
2281 * it should implement safe delete | |
2282 */ | |
995 | 2283 |
590 | 2284 static gboolean file_data_perform_move(FileData *fd) |
2285 { | |
2286 g_assert(!strcmp(fd->change->source, fd->path)); | |
2287 return move_file(fd->change->source, fd->change->dest); | |
2288 } | |
2289 | |
2290 static gboolean file_data_perform_copy(FileData *fd) | |
2291 { | |
2292 g_assert(!strcmp(fd->change->source, fd->path)); | |
2293 return copy_file(fd->change->source, fd->change->dest); | |
2294 } | |
2295 | |
2296 static gboolean file_data_perform_delete(FileData *fd) | |
2297 { | |
896
cf21dc928122
implemented directory rename and delete operations
nadvornik
parents:
892
diff
changeset
|
2298 if (isdir(fd->path) && !islink(fd->path)) |
cf21dc928122
implemented directory rename and delete operations
nadvornik
parents:
892
diff
changeset
|
2299 return rmdir_utf8(fd->path); |
cf21dc928122
implemented directory rename and delete operations
nadvornik
parents:
892
diff
changeset
|
2300 else |
1212 | 2301 if (options->file_ops.safe_delete_enable) |
2302 return file_util_safe_unlink(fd->path); | |
2303 else | |
2304 return unlink_file(fd->path); | |
590 | 2305 } |
2306 | |
1205
3ff2aa99108b
use the workflow in utilops.c for metadata approving and writting
nadvornik
parents:
1055
diff
changeset
|
2307 gboolean file_data_perform_ci(FileData *fd) |
590 | 2308 { |
2309 FileDataChangeType type = fd->change->type; | |
1422 | 2310 |
590 | 2311 switch (type) |
2312 { | |
2313 case FILEDATA_CHANGE_MOVE: | |
2314 return file_data_perform_move(fd); | |
2315 case FILEDATA_CHANGE_COPY: | |
2316 return file_data_perform_copy(fd); | |
2317 case FILEDATA_CHANGE_RENAME: | |
2318 return file_data_perform_move(fd); /* the same as move */ | |
2319 case FILEDATA_CHANGE_DELETE: | |
2320 return file_data_perform_delete(fd); | |
1205
3ff2aa99108b
use the workflow in utilops.c for metadata approving and writting
nadvornik
parents:
1055
diff
changeset
|
2321 case FILEDATA_CHANGE_WRITE_METADATA: |
3ff2aa99108b
use the workflow in utilops.c for metadata approving and writting
nadvornik
parents:
1055
diff
changeset
|
2322 return metadata_write_perform(fd); |
753 | 2323 case FILEDATA_CHANGE_UNSPECIFIED: |
596 | 2324 /* nothing to do here */ |
590 | 2325 break; |
2326 } | |
2327 return TRUE; | |
2328 } | |
2329 | |
2330 | |
2331 | |
2332 gboolean file_data_sc_perform_ci(FileData *fd) | |
2333 { | |
2334 GList *work; | |
2335 gboolean ret = TRUE; | |
2336 FileDataChangeType type = fd->change->type; | |
806 | 2337 |
590 | 2338 if (!file_data_sc_check_ci(fd, type)) return FALSE; |
2339 | |
2340 work = fd->sidecar_files; | |
2341 while (work) | |
2342 { | |
2343 FileData *sfd = work->data; | |
806 | 2344 |
590 | 2345 if (!file_data_perform_ci(sfd)) ret = FALSE; |
2346 work = work->next; | |
2347 } | |
806 | 2348 |
590 | 2349 if (!file_data_perform_ci(fd)) ret = FALSE; |
806 | 2350 |
590 | 2351 return ret; |
2352 } | |
2353 | |
2354 /* | |
2355 * updates FileData structure according to FileDataChangeInfo | |
2356 */ | |
912
9108a7158c02
remove items from file_data_planned_change_hash when the operation is
nadvornik
parents:
910
diff
changeset
|
2357 |
1422 | 2358 gboolean file_data_apply_ci(FileData *fd) |
590 | 2359 { |
2360 FileDataChangeType type = fd->change->type; | |
1205
3ff2aa99108b
use the workflow in utilops.c for metadata approving and writting
nadvornik
parents:
1055
diff
changeset
|
2361 |
590 | 2362 /* FIXME delete ?*/ |
773 | 2363 if (type == FILEDATA_CHANGE_MOVE || type == FILEDATA_CHANGE_RENAME) |
590 | 2364 { |
912
9108a7158c02
remove items from file_data_planned_change_hash when the operation is
nadvornik
parents:
910
diff
changeset
|
2365 DEBUG_1("planned change: applying %s -> %s", fd->change->dest, fd->path); |
9108a7158c02
remove items from file_data_planned_change_hash when the operation is
nadvornik
parents:
910
diff
changeset
|
2366 file_data_planned_change_remove(fd); |
915 | 2367 |
2368 if (g_hash_table_lookup(file_data_pool, fd->change->dest)) | |
2369 { | |
2370 /* this change overwrites another file which is already known to other modules | |
2371 renaming fd would create duplicate FileData structure | |
2372 the best thing we can do is nothing | |
2373 FIXME: maybe we could copy stuff like marks | |
2374 */ | |
2375 DEBUG_1("can't rename fd, target exists %s -> %s", fd->change->dest, fd->path); | |
2376 } | |
2377 else | |
2378 { | |
2379 file_data_set_path(fd, fd->change->dest); | |
2380 } | |
590 | 2381 } |
763
81f9e8dbb4bf
improved infrastructure for tracing changes, optimized vflist_populate_view
nadvornik
parents:
753
diff
changeset
|
2382 file_data_increment_version(fd); |
1432 | 2383 file_data_send_notification(fd, NOTIFY_CHANGE); |
1205
3ff2aa99108b
use the workflow in utilops.c for metadata approving and writting
nadvornik
parents:
1055
diff
changeset
|
2384 |
3ff2aa99108b
use the workflow in utilops.c for metadata approving and writting
nadvornik
parents:
1055
diff
changeset
|
2385 return TRUE; |
590 | 2386 } |
2387 | |
1422 | 2388 gboolean file_data_sc_apply_ci(FileData *fd) |
590 | 2389 { |
2390 GList *work; | |
2391 FileDataChangeType type = fd->change->type; | |
806 | 2392 |
590 | 2393 if (!file_data_sc_check_ci(fd, type)) return FALSE; |
2394 | |
2395 work = fd->sidecar_files; | |
2396 while (work) | |
2397 { | |
2398 FileData *sfd = work->data; | |
806 | 2399 |
590 | 2400 file_data_apply_ci(sfd); |
2401 work = work->next; | |
2402 } | |
806 | 2403 |
590 | 2404 file_data_apply_ci(fd); |
806 | 2405 |
590 | 2406 return TRUE; |
2407 } | |
2408 | |
1619 | 2409 static gboolean file_data_list_contains_whole_group(GList *list, FileData *fd) |
2410 { | |
2411 GList *work; | |
2412 if (fd->parent) fd = fd->parent; | |
2413 if (!g_list_find(list, fd)) return FALSE; | |
2414 | |
2415 work = fd->sidecar_files; | |
2416 while (work) | |
2417 { | |
2418 if (!g_list_find(list, work->data)) return FALSE; | |
2419 work = work->next; | |
2420 } | |
2421 return TRUE; | |
2422 } | |
2423 | |
2424 #if 0 | |
2425 static gboolean file_data_list_dump(GList *list) | |
2426 { | |
2427 GList *work, *work2; | |
2428 | |
2429 work = list; | |
2430 while (work) | |
2431 { | |
2432 FileData *fd = work->data; | |
2433 printf("%s\n", fd->name); | |
2434 work2 = fd->sidecar_files; | |
2435 while (work2) | |
2436 { | |
2437 FileData *fd = work2->data; | |
2438 printf(" %s\n", fd->name); | |
2439 work2 = work2->next; | |
2440 } | |
2441 work = work->next; | |
2442 } | |
2443 return TRUE; | |
2444 } | |
2445 #endif | |
2446 | |
2447 GList *file_data_process_groups(GList *list) | |
2448 { | |
2449 GList *out = NULL; | |
2450 GList *work = list; | |
2451 | |
2452 /* change partial groups to independent files */ | |
2453 while (work) | |
2454 { | |
2455 FileData *fd = work->data; | |
2456 work = work->next; | |
2457 | |
2458 if (!file_data_list_contains_whole_group(list, fd)) | |
2459 file_data_disable_grouping(fd, TRUE); | |
2460 } | |
2461 | |
2462 /* remove sidecars from the list, | |
2463 they can be still acessed via main_fd->sidecar_files */ | |
2464 work = list; | |
2465 while (work) | |
2466 { | |
2467 FileData *fd = work->data; | |
2468 work = work->next; | |
2469 | |
2470 if (!fd->parent) | |
2471 { | |
2472 out = g_list_prepend(out, fd); | |
2473 } | |
2474 else | |
2475 { | |
2476 file_data_unref(fd); | |
2477 } | |
2478 } | |
2479 | |
2480 g_list_free(list); | |
2481 out = g_list_reverse(out); | |
2482 | |
2483 return out; | |
2484 } | |
2485 | |
2486 | |
2487 | |
2488 | |
2489 | |
590 | 2490 /* |
1619 | 2491 * notify other modules about the change described by FileDataChangeInfo |
590 | 2492 */ |
995 | 2493 |
590 | 2494 /* might use file_maint_ functions for now, later it should be changed to a system of callbacks |
2495 FIXME do we need the ignore_list? It looks like a workaround for ineffective | |
2496 implementation in view_file_list.c */ | |
2497 | |
784
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
2498 |
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
2499 |
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
2500 |
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
2501 typedef struct _NotifyData NotifyData; |
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
2502 |
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
2503 struct _NotifyData { |
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
2504 FileDataNotifyFunc func; |
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
2505 gpointer data; |
791 | 2506 NotifyPriority priority; |
1422 | 2507 }; |
784
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
2508 |
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
2509 static GList *notify_func_list = NULL; |
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
2510 |
791 | 2511 static gint file_data_notify_sort(gconstpointer a, gconstpointer b) |
2512 { | |
2513 NotifyData *nda = (NotifyData *)a; | |
2514 NotifyData *ndb = (NotifyData *)b; | |
806 | 2515 |
791 | 2516 if (nda->priority < ndb->priority) return -1; |
2517 if (nda->priority > ndb->priority) return 1; | |
2518 return 0; | |
2519 } | |
2520 | |
1422 | 2521 gboolean file_data_register_notify_func(FileDataNotifyFunc func, gpointer data, NotifyPriority priority) |
784
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
2522 { |
806 | 2523 NotifyData *nd; |
2524 | |
2525 nd = g_new(NotifyData, 1); | |
784
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
2526 nd->func = func; |
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
2527 nd->data = data; |
791 | 2528 nd->priority = priority; |
806 | 2529 |
791 | 2530 notify_func_list = g_list_insert_sorted(notify_func_list, nd, file_data_notify_sort); |
1498 | 2531 DEBUG_2("Notify func registered: %p", nd); |
806 | 2532 |
784
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
2533 return TRUE; |
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
2534 } |
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
2535 |
1422 | 2536 gboolean file_data_unregister_notify_func(FileDataNotifyFunc func, gpointer data) |
784
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
2537 { |
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
2538 GList *work = notify_func_list; |
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
2539 |
806 | 2540 while (work) |
784
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
2541 { |
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
2542 NotifyData *nd = (NotifyData *)work->data; |
806 | 2543 |
784
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
2544 if (nd->func == func && nd->data == data) |
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
2545 { |
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
2546 notify_func_list = g_list_delete_link(notify_func_list, work); |
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
2547 g_free(nd); |
1498 | 2548 DEBUG_2("Notify func unregistered: %p", nd); |
784
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
2549 return TRUE; |
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
2550 } |
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
2551 work = work->next; |
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
2552 } |
806 | 2553 |
784
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
2554 return FALSE; |
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
2555 } |
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
2556 |
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
2557 |
792 | 2558 void file_data_send_notification(FileData *fd, NotifyType type) |
784
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
2559 { |
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
2560 GList *work = notify_func_list; |
806 | 2561 |
2562 while (work) | |
784
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
2563 { |
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
2564 NotifyData *nd = (NotifyData *)work->data; |
806 | 2565 |
792 | 2566 nd->func(fd, type, nd->data); |
784
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
2567 work = work->next; |
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
2568 } |
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
2569 } |
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
2570 |
791 | 2571 static GHashTable *file_data_monitor_pool = NULL; |
1523 | 2572 static guint realtime_monitor_id = 0; /* event source id */ |
791 | 2573 |
2574 static void realtime_monitor_check_cb(gpointer key, gpointer value, gpointer data) | |
2575 { | |
2576 FileData *fd = key; | |
2577 | |
801 | 2578 file_data_check_changed_files(fd); |
791 | 2579 |
2580 DEBUG_1("monitor %s", fd->path); | |
2581 } | |
2582 | |
2583 static gboolean realtime_monitor_cb(gpointer data) | |
2584 { | |
903
c93823609f15
periodic testing of changed files can be now disabled
nadvornik
parents:
899
diff
changeset
|
2585 if (!options->update_on_time_change) return TRUE; |
791 | 2586 g_hash_table_foreach(file_data_monitor_pool, realtime_monitor_check_cb, NULL); |
2587 return TRUE; | |
2588 } | |
2589 | |
1422 | 2590 gboolean file_data_register_real_time_monitor(FileData *fd) |
791 | 2591 { |
950 | 2592 gint count; |
791 | 2593 |
2594 file_data_ref(fd); | |
2595 | |
2596 if (!file_data_monitor_pool) | |
2597 file_data_monitor_pool = g_hash_table_new(g_direct_hash, g_direct_equal); | |
2598 | |
2599 count = GPOINTER_TO_INT(g_hash_table_lookup(file_data_monitor_pool, fd)); | |
2600 | |
2601 DEBUG_1("Register realtime %d %s", count, fd->path); | |
2602 | |
2603 count++; | |
2604 g_hash_table_insert(file_data_monitor_pool, fd, GINT_TO_POINTER(count)); | |
2605 | |
1523 | 2606 if (!realtime_monitor_id) |
791 | 2607 { |
2608 realtime_monitor_id = g_timeout_add(5000, realtime_monitor_cb, NULL); | |
2609 } | |
806 | 2610 |
791 | 2611 return TRUE; |
2612 } | |
2613 | |
1422 | 2614 gboolean file_data_unregister_real_time_monitor(FileData *fd) |
791 | 2615 { |
2616 gint count; | |
806 | 2617 |
791 | 2618 g_assert(file_data_monitor_pool); |
2619 | |
2620 count = GPOINTER_TO_INT(g_hash_table_lookup(file_data_monitor_pool, fd)); | |
2621 | |
2622 DEBUG_1("Unregister realtime %d %s", count, fd->path); | |
2623 | |
2624 g_assert(count > 0); | |
2625 | |
2626 count--; | |
2627 | |
2628 if (count == 0) | |
2629 g_hash_table_remove(file_data_monitor_pool, fd); | |
2630 else | |
2631 g_hash_table_insert(file_data_monitor_pool, fd, GINT_TO_POINTER(count)); | |
2632 | |
2633 file_data_unref(fd); | |
2634 | |
2635 if (g_hash_table_size(file_data_monitor_pool) == 0) | |
2636 { | |
2637 g_source_remove(realtime_monitor_id); | |
1523 | 2638 realtime_monitor_id = 0; |
791 | 2639 return FALSE; |
2640 } | |
806 | 2641 |
791 | 2642 return TRUE; |
2643 } | |
1055
1646720364cf
Adding a vim modeline to all files - patch by Klaus Ethgen
nadvornik
parents:
1023
diff
changeset
|
2644 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */ |