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