Mercurial > geeqie.yaz
annotate src/filedata.c @ 800:a25b228978a0
various notification improvements
author | nadvornik |
---|---|
date | Sat, 07 Jun 2008 15:08:33 +0000 |
parents | 99ea3d973ad3 |
children | 9b676bb76a2e |
rev | line source |
---|---|
586 | 1 /* |
2 * Geeqie | |
3 * (C) 2006 John Ellis | |
4 * Copyright (C) 2008 The Geeqie Team | |
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 "rcfile.h" | |
20 #include "secure_save.h" | |
21 #include "thumb_standard.h" | |
22 #include "ui_fileops.h" | |
23 | |
24 | |
785 | 25 static GHashTable *file_data_pool = NULL; |
26 | |
586 | 27 static gint sidecar_file_priority(const gchar *path); |
28 | |
29 | |
30 /* | |
31 *----------------------------------------------------------------------------- | |
32 * text conversion utils | |
33 *----------------------------------------------------------------------------- | |
34 */ | |
35 | |
36 gchar *text_from_size(gint64 size) | |
37 { | |
38 gchar *a, *b; | |
39 gchar *s, *d; | |
40 gint l, n, i; | |
41 | |
42 /* what I would like to use is printf("%'d", size) | |
43 * BUT: not supported on every libc :( | |
44 */ | |
45 if (size > G_MAXUINT) | |
46 { | |
47 /* the %lld conversion is not valid in all libcs, so use a simple work-around */ | |
48 a = g_strdup_printf("%d%09d", (guint)(size / 1000000000), (guint)(size % 1000000000)); | |
49 } | |
50 else | |
51 { | |
52 a = g_strdup_printf("%d", (guint)size); | |
53 } | |
54 l = strlen(a); | |
55 n = (l - 1)/ 3; | |
56 if (n < 1) return a; | |
57 | |
58 b = g_new(gchar, l + n + 1); | |
59 | |
60 s = a; | |
61 d = b; | |
62 i = l - n * 3; | |
63 while (*s != '\0') | |
64 { | |
65 if (i < 1) | |
66 { | |
67 i = 3; | |
68 *d = ','; | |
69 d++; | |
70 } | |
71 | |
72 *d = *s; | |
73 s++; | |
74 d++; | |
75 i--; | |
76 } | |
77 *d = '\0'; | |
78 | |
79 g_free(a); | |
80 return b; | |
81 } | |
82 | |
83 gchar *text_from_size_abrev(gint64 size) | |
84 { | |
85 if (size < (gint64)1024) | |
86 { | |
87 return g_strdup_printf(_("%d bytes"), (gint)size); | |
88 } | |
89 if (size < (gint64)1048576) | |
90 { | |
91 return g_strdup_printf(_("%.1f K"), (double)size / 1024.0); | |
92 } | |
93 if (size < (gint64)1073741824) | |
94 { | |
95 return g_strdup_printf(_("%.1f MB"), (double)size / 1048576.0); | |
96 } | |
97 | |
98 /* to avoid overflowing the double, do division in two steps */ | |
99 size /= 1048576; | |
100 return g_strdup_printf(_("%.1f GB"), (double)size / 1024.0); | |
101 } | |
102 | |
103 /* note: returned string is valid until next call to text_from_time() */ | |
104 const gchar *text_from_time(time_t t) | |
105 { | |
106 static gchar *ret = NULL; | |
107 gchar buf[128]; | |
108 gint buflen; | |
109 struct tm *btime; | |
110 GError *error = NULL; | |
111 | |
112 btime = localtime(&t); | |
113 | |
114 /* the %x warning about 2 digit years is not an error */ | |
115 buflen = strftime(buf, sizeof(buf), "%x %H:%M", btime); | |
116 if (buflen < 1) return ""; | |
117 | |
118 g_free(ret); | |
119 ret = g_locale_to_utf8(buf, buflen, NULL, NULL, &error); | |
120 if (error) | |
121 { | |
673
fbebf5cf4a55
Do not use printf() directly but use new wrapper function log_printf() instead.
zas_
parents:
671
diff
changeset
|
122 log_printf("Error converting locale strftime to UTF-8: %s\n", error->message); |
586 | 123 g_error_free(error); |
124 return ""; | |
125 } | |
126 | |
127 return ret; | |
128 } | |
129 | |
130 /* | |
131 *----------------------------------------------------------------------------- | |
132 * file info struct | |
133 *----------------------------------------------------------------------------- | |
134 */ | |
135 | |
136 FileData *file_data_merge_sidecar_files(FileData *target, FileData *source); | |
137 static void file_data_check_sidecars(FileData *fd); | |
138 FileData *file_data_disconnect_sidecar_file(FileData *target, FileData *sfd); | |
139 | |
140 | |
763
81f9e8dbb4bf
improved infrastructure for tracing changes, optimized vflist_populate_view
nadvornik
parents:
753
diff
changeset
|
141 void file_data_increment_version(FileData *fd) |
81f9e8dbb4bf
improved infrastructure for tracing changes, optimized vflist_populate_view
nadvornik
parents:
753
diff
changeset
|
142 { |
81f9e8dbb4bf
improved infrastructure for tracing changes, optimized vflist_populate_view
nadvornik
parents:
753
diff
changeset
|
143 fd->version++; |
81f9e8dbb4bf
improved infrastructure for tracing changes, optimized vflist_populate_view
nadvornik
parents:
753
diff
changeset
|
144 if (fd->parent) fd->parent->version++; |
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 |
785 | 147 static void file_data_set_collate_keys(FileData *fd) |
148 { | |
149 gchar *caseless_name; | |
150 | |
151 g_assert(g_utf8_validate(fd->name, -1, NULL)); | |
152 | |
153 caseless_name = g_utf8_casefold(fd->name, -1); | |
154 | |
155 g_free(fd->collate_key_name); | |
156 g_free(fd->collate_key_name_nocase); | |
157 | |
158 #if GLIB_CHECK_VERSION(2, 8, 0) | |
159 fd->collate_key_name = g_utf8_collate_key_for_filename(fd->name, -1); | |
160 fd->collate_key_name_nocase = g_utf8_collate_key_for_filename(caseless_name, -1); | |
161 #else | |
162 fd->collate_key_name = g_utf8_collate_key(fd->name, -1); | |
163 fd->collate_key_name_nocase = g_utf8_collate_key(caseless_name, -1); | |
164 #endif | |
165 g_free(caseless_name); | |
166 } | |
763
81f9e8dbb4bf
improved infrastructure for tracing changes, optimized vflist_populate_view
nadvornik
parents:
753
diff
changeset
|
167 |
586 | 168 static void file_data_set_path(FileData *fd, const gchar *path) |
169 { | |
790 | 170 g_assert(path /* && *path*/); /* view_dir_tree uses FileData with zero length path */ |
785 | 171 g_assert(file_data_pool); |
172 | |
173 g_free(fd->path); | |
174 | |
175 if (fd->original_path) | |
176 { | |
177 g_hash_table_remove(file_data_pool, fd->original_path); | |
178 g_free(fd->original_path); | |
179 } | |
180 fd->original_path = g_strdup(path); | |
181 g_hash_table_insert(file_data_pool, fd->original_path, fd); | |
586 | 182 |
725 | 183 if (strcmp(path, G_DIR_SEPARATOR_S) == 0) |
586 | 184 { |
185 fd->path = g_strdup(path); | |
186 fd->name = fd->path; | |
187 fd->extension = fd->name + 1; | |
785 | 188 file_data_set_collate_keys(fd); |
586 | 189 return; |
190 } | |
191 | |
192 fd->path = g_strdup(path); | |
193 fd->name = filename_from_path(fd->path); | |
194 | |
195 if (strcmp(fd->name, "..") == 0) | |
196 { | |
197 gchar *dir = remove_level_from_path(path); | |
198 g_free(fd->path); | |
199 fd->path = remove_level_from_path(dir); | |
200 g_free(dir); | |
201 fd->name = ".."; | |
202 fd->extension = fd->name + 2; | |
785 | 203 file_data_set_collate_keys(fd); |
586 | 204 return; |
205 } | |
206 else if (strcmp(fd->name, ".") == 0) | |
207 { | |
208 g_free(fd->path); | |
209 fd->path = remove_level_from_path(path); | |
210 fd->name = "."; | |
211 fd->extension = fd->name + 1; | |
785 | 212 file_data_set_collate_keys(fd); |
586 | 213 return; |
214 } | |
215 | |
216 fd->extension = extension_from_path(fd->path); | |
217 if (fd->extension == NULL) | |
218 fd->extension = fd->name + strlen(fd->name); | |
785 | 219 |
220 file_data_set_collate_keys(fd); | |
586 | 221 } |
222 | |
223 static void file_data_check_changed_files(FileData *fd, struct stat *st) | |
224 { | |
225 GList *work; | |
226 if (fd->size != st->st_size || | |
227 fd->date != st->st_mtime) | |
228 { | |
229 fd->size = st->st_size; | |
230 fd->date = st->st_mtime; | |
231 if (fd->pixbuf) g_object_unref(fd->pixbuf); | |
232 fd->pixbuf = NULL; | |
763
81f9e8dbb4bf
improved infrastructure for tracing changes, optimized vflist_populate_view
nadvornik
parents:
753
diff
changeset
|
233 file_data_increment_version(fd); |
800 | 234 file_data_send_notification(fd, NOTIFY_TYPE_REREAD); |
586 | 235 } |
236 | |
237 work = fd->sidecar_files; | |
238 while (work) | |
239 { | |
240 FileData *sfd = work->data; | |
241 struct stat st; | |
242 | |
243 if (!stat_utf8(sfd->path, &st)) | |
244 { | |
245 file_data_disconnect_sidecar_file(fd, sfd); | |
246 } | |
247 | |
248 file_data_check_changed_files(sfd, &st); | |
249 work = work->next; | |
250 } | |
251 } | |
252 | |
253 static FileData *file_data_new(const gchar *path_utf8, struct stat *st, gboolean check_sidecars) | |
254 { | |
255 FileData *fd; | |
256 | |
257 DEBUG_2("file_data_new: '%s' %d", path_utf8, check_sidecars); | |
258 | |
259 if (!file_data_pool) | |
260 file_data_pool = g_hash_table_new(g_str_hash, g_str_equal); | |
261 | |
262 fd = g_hash_table_lookup(file_data_pool, path_utf8); | |
263 if (fd) | |
264 { | |
265 file_data_check_changed_files(fd, st); | |
266 DEBUG_2("file_data_pool hit: '%s'", fd->path); | |
267 return file_data_ref(fd); | |
268 } | |
269 | |
270 fd = g_new0(FileData, 1); | |
785 | 271 |
272 fd->path = NULL; | |
273 fd->name = NULL; | |
274 fd->collate_key_name = NULL; | |
275 fd->collate_key_name_nocase = NULL; | |
276 fd->original_path = NULL; | |
586 | 277 |
278 fd->size = st->st_size; | |
279 fd->date = st->st_mtime; | |
280 fd->pixbuf = NULL; | |
281 fd->sidecar_files = NULL; | |
282 fd->ref = 1; | |
283 fd->magick = 0x12345678; | |
284 | |
785 | 285 file_data_set_path(fd, path_utf8); /* set path, name, collate_key_*, original_path */ |
586 | 286 |
287 if (check_sidecars && sidecar_file_priority(fd->extension)) | |
288 file_data_check_sidecars(fd); | |
785 | 289 |
586 | 290 return fd; |
291 } | |
292 | |
293 static void file_data_check_sidecars(FileData *fd) | |
294 { | |
295 int base_len = fd->extension - fd->path; | |
296 GString *fname = g_string_new_len(fd->path, base_len); | |
297 FileData *parent_fd = NULL; | |
298 GList *work = sidecar_ext_get_list(); | |
299 while (work) | |
300 { | |
301 /* check for possible sidecar files; | |
302 the sidecar files created here are referenced only via fd->sidecar_files or fd->parent, | |
303 they have fd->ref set to 0 and file_data unref must chack and free them all together | |
304 (using fd->ref would cause loops and leaks) | |
305 */ | |
306 | |
307 FileData *new_fd; | |
308 | |
309 gchar *ext = work->data; | |
310 work = work->next; | |
311 | |
312 if (strcmp(ext, fd->extension) == 0) | |
313 { | |
314 new_fd = fd; /* processing the original file */ | |
315 } | |
316 else | |
317 { | |
318 struct stat nst; | |
319 g_string_truncate(fname, base_len); | |
320 g_string_append(fname, ext); | |
321 | |
322 if (!stat_utf8(fname->str, &nst)) | |
323 continue; | |
324 | |
325 new_fd = file_data_new(fname->str, &nst, FALSE); | |
326 new_fd->ref--; /* do not use ref here */ | |
327 } | |
328 | |
329 if (!parent_fd) | |
330 parent_fd = new_fd; /* parent is the one with the highest prio, found first */ | |
331 else | |
332 file_data_merge_sidecar_files(parent_fd, new_fd); | |
333 } | |
334 g_string_free(fname, TRUE); | |
335 } | |
336 | |
337 | |
338 static FileData *file_data_new_local(const gchar *path, struct stat *st, gboolean check_sidecars) | |
339 { | |
340 gchar *path_utf8 = path_to_utf8(path); | |
341 FileData *ret = file_data_new(path_utf8, st, check_sidecars); | |
342 g_free(path_utf8); | |
343 return ret; | |
344 } | |
345 | |
346 FileData *file_data_new_simple(const gchar *path_utf8) | |
347 { | |
348 struct stat st; | |
349 | |
350 if (!stat_utf8(path_utf8, &st)) | |
351 { | |
352 st.st_size = 0; | |
353 st.st_mtime = 0; | |
354 } | |
355 | |
356 return file_data_new(path_utf8, &st, TRUE); | |
357 } | |
358 | |
359 FileData *file_data_add_sidecar_file(FileData *target, FileData *sfd) | |
360 { | |
361 sfd->parent = target; | |
362 if(!g_list_find(target->sidecar_files, sfd)) | |
363 target->sidecar_files = g_list_prepend(target->sidecar_files, sfd); | |
364 return target; | |
365 } | |
366 | |
367 | |
368 FileData *file_data_merge_sidecar_files(FileData *target, FileData *source) | |
369 { | |
370 GList *work; | |
371 file_data_add_sidecar_file(target, source); | |
372 | |
373 work = source->sidecar_files; | |
374 while (work) | |
375 { | |
376 FileData *sfd = work->data; | |
377 file_data_add_sidecar_file(target, sfd); | |
378 work = work->next; | |
379 } | |
380 | |
381 g_list_free(source->sidecar_files); | |
382 source->sidecar_files = NULL; | |
383 | |
384 target->sidecar_files = filelist_sort(target->sidecar_files, SORT_NAME, TRUE); | |
385 return target; | |
386 } | |
387 | |
388 | |
389 | |
390 FileData *file_data_ref(FileData *fd) | |
391 { | |
392 if (fd == NULL) return NULL; | |
393 | |
394 // return g_memdup(fd, sizeof(FileData)); | |
395 g_assert(fd->magick == 0x12345678); | |
396 fd->ref++; | |
397 return fd; | |
398 } | |
399 | |
400 static void file_data_free(FileData *fd) | |
401 { | |
402 g_assert(fd->magick == 0x12345678); | |
403 g_assert(fd->ref == 0); | |
404 | |
405 g_hash_table_remove(file_data_pool, fd->original_path); | |
406 | |
407 g_free(fd->path); | |
408 g_free(fd->original_path); | |
785 | 409 g_free(fd->collate_key_name); |
410 g_free(fd->collate_key_name_nocase); | |
586 | 411 if (fd->pixbuf) g_object_unref(fd->pixbuf); |
412 | |
413 | |
414 g_assert(fd->sidecar_files == NULL); /* sidecar files must be freed before calling this */ | |
415 | |
416 file_data_change_info_free(NULL, fd); | |
417 g_free(fd); | |
418 } | |
419 | |
420 void file_data_unref(FileData *fd) | |
421 { | |
422 if (fd == NULL) return; | |
423 g_assert(fd->magick == 0x12345678); | |
424 | |
425 fd->ref--; | |
426 DEBUG_2("file_data_unref (%d): '%s'", fd->ref, fd->path); | |
427 | |
428 if (fd->ref == 0) | |
429 { | |
430 FileData *parent = fd->parent ? fd->parent : fd; | |
431 | |
432 GList *work; | |
433 | |
434 if (parent->ref > 0) | |
435 return; | |
436 | |
437 work = parent->sidecar_files; | |
438 while (work) | |
439 { | |
440 FileData *sfd = work->data; | |
441 if (sfd->ref > 0) | |
442 return; | |
443 work = work->next; | |
444 } | |
445 | |
446 /* none of parent/children is referenced, we can free everything */ | |
447 | |
448 DEBUG_2("file_data_unref: deleting '%s', parent '%s'", fd->path, parent->path); | |
449 | |
450 work = parent->sidecar_files; | |
451 while (work) | |
452 { | |
453 FileData *sfd = work->data; | |
454 file_data_free(sfd); | |
455 work = work->next; | |
456 } | |
457 | |
458 g_list_free(parent->sidecar_files); | |
459 parent->sidecar_files = NULL; | |
460 | |
461 file_data_free(parent); | |
462 | |
463 } | |
464 } | |
465 | |
466 FileData *file_data_disconnect_sidecar_file(FileData *target, FileData *sfd) | |
467 { | |
468 sfd->parent = target; | |
469 g_assert(g_list_find(target->sidecar_files, sfd)); | |
470 | |
471 target->sidecar_files = g_list_remove(target->sidecar_files, sfd); | |
472 sfd->parent = NULL; | |
473 | |
474 if (sfd->ref == 0) { | |
475 file_data_free(sfd); | |
476 return NULL; | |
477 } | |
478 | |
479 return sfd; | |
480 } | |
481 | |
482 /* compare name without extension */ | |
483 gint file_data_compare_name_without_ext(FileData *fd1, FileData *fd2) | |
484 { | |
485 size_t len1 = fd1->extension - fd1->name; | |
486 size_t len2 = fd2->extension - fd2->name; | |
487 | |
488 if (len1 < len2) return -1; | |
489 if (len1 > len2) return 1; | |
490 | |
785 | 491 return strncmp(fd1->name, fd2->name, len1); /* FIXME: utf8 */ |
586 | 492 } |
493 | |
494 gboolean file_data_add_change_info(FileData *fd, FileDataChangeType type, const gchar *src, const gchar *dest) | |
495 { | |
496 | |
497 FileDataChangeInfo *fdci; | |
498 | |
499 if (fd->change) return FALSE; | |
500 | |
501 fdci = g_new0(FileDataChangeInfo, 1); | |
502 | |
503 fdci->type = type; | |
504 | |
505 if (src) | |
506 fdci->source = g_strdup(src); | |
507 else | |
508 fdci->source = g_strdup(fd->path); | |
509 | |
510 if (dest) | |
511 fdci->dest = g_strdup(dest); | |
512 | |
513 fd->change = fdci; | |
514 return TRUE; | |
515 } | |
516 | |
517 void file_data_change_info_free(FileDataChangeInfo *fdci, FileData *fd) | |
518 { | |
519 if (!fdci && fd) | |
520 fdci = fd->change; | |
521 | |
522 if (!fdci) | |
523 return; | |
524 | |
525 g_free(fdci->source); | |
526 g_free(fdci->dest); | |
527 | |
528 g_free(fdci); | |
529 | |
530 if (fd) | |
531 fd->change = NULL; | |
532 } | |
533 | |
534 | |
535 | |
536 | |
537 /* | |
538 *----------------------------------------------------------------------------- | |
539 * sidecar file info struct | |
540 *----------------------------------------------------------------------------- | |
541 */ | |
542 | |
543 | |
544 | |
545 static gint sidecar_file_priority(const gchar *path) | |
546 { | |
547 const char *extension = extension_from_path(path); | |
548 int i = 1; | |
549 GList *work; | |
550 if (extension == NULL) | |
551 return 0; | |
552 | |
553 work = sidecar_ext_get_list(); | |
554 | |
555 while (work) { | |
556 gchar *ext = work->data; | |
557 work = work->next; | |
558 if (strcmp(extension, ext) == 0) return i; | |
559 i++; | |
560 } | |
561 return 0; | |
562 } | |
563 | |
564 | |
565 /* | |
566 *----------------------------------------------------------------------------- | |
567 * load file list | |
568 *----------------------------------------------------------------------------- | |
569 */ | |
570 | |
571 static SortType filelist_sort_method = SORT_NONE; | |
572 static gint filelist_sort_ascend = TRUE; | |
573 | |
574 | |
575 gint filelist_sort_compare_filedata(FileData *fa, FileData *fb) | |
576 { | |
577 if (!filelist_sort_ascend) | |
578 { | |
579 FileData *tmp = fa; | |
580 fa = fb; | |
581 fb = tmp; | |
582 } | |
583 | |
584 switch (filelist_sort_method) | |
585 { | |
785 | 586 case SORT_NAME: |
587 break; | |
586 | 588 case SORT_SIZE: |
589 if (fa->size < fb->size) return -1; | |
590 if (fa->size > fb->size) return 1; | |
785 | 591 /* fall back to name */ |
586 | 592 break; |
593 case SORT_TIME: | |
594 if (fa->date < fb->date) return -1; | |
595 if (fa->date > fb->date) return 1; | |
785 | 596 /* fall back to name */ |
586 | 597 break; |
598 #ifdef HAVE_STRVERSCMP | |
599 case SORT_NUMBER: | |
600 return strverscmp(fa->name, fb->name); | |
601 break; | |
602 #endif | |
603 default: | |
604 break; | |
605 } | |
785 | 606 |
607 if (options->file_sort.case_sensitive) | |
608 return strcmp(fa->collate_key_name, fb->collate_key_name); | |
609 else | |
610 return strcmp(fa->collate_key_name_nocase, fb->collate_key_name_nocase); | |
586 | 611 } |
612 | |
613 gint filelist_sort_compare_filedata_full(FileData *fa, FileData *fb, SortType method, gint ascend) | |
614 { | |
615 filelist_sort_method = method; | |
616 filelist_sort_ascend = ascend; | |
617 return filelist_sort_compare_filedata(fa, fb); | |
618 } | |
619 | |
620 static gint filelist_sort_file_cb(void *a, void *b) | |
621 { | |
622 return filelist_sort_compare_filedata(a, b); | |
623 } | |
624 | |
625 GList *filelist_sort_full(GList *list, SortType method, gint ascend, GCompareFunc cb) | |
626 { | |
627 filelist_sort_method = method; | |
628 filelist_sort_ascend = ascend; | |
629 return g_list_sort(list, cb); | |
630 } | |
631 | |
632 GList *filelist_insert_sort_full(GList *list, void *data, SortType method, gint ascend, GCompareFunc cb) | |
633 { | |
634 filelist_sort_method = method; | |
635 filelist_sort_ascend = ascend; | |
636 return g_list_insert_sorted(list, data, cb); | |
637 } | |
638 | |
639 GList *filelist_sort(GList *list, SortType method, gint ascend) | |
640 { | |
641 return filelist_sort_full(list, method, ascend, (GCompareFunc) filelist_sort_file_cb); | |
642 } | |
643 | |
644 GList *filelist_insert_sort(GList *list, FileData *fd, SortType method, gint ascend) | |
645 { | |
646 return filelist_insert_sort_full(list, fd, method, ascend, (GCompareFunc) filelist_sort_file_cb); | |
647 } | |
648 | |
649 | |
650 static GList *filelist_filter_out_sidecars(GList *flist) | |
651 { | |
652 GList *work = flist; | |
653 GList *flist_filtered = NULL; | |
654 | |
655 while (work) | |
656 { | |
657 FileData *fd = work->data; | |
658 work = work->next; | |
659 if (fd->parent) /* remove fd's that are children */ | |
660 file_data_unref(fd); | |
661 else | |
662 flist_filtered = g_list_prepend(flist_filtered, fd); | |
663 } | |
664 g_list_free(flist); | |
665 return flist_filtered; | |
666 } | |
667 | |
783 | 668 static gint filelist_read_real(FileData *dir_fd, GList **files, GList **dirs, gint follow_symlinks) |
586 | 669 { |
670 DIR *dp; | |
671 struct dirent *dir; | |
672 gchar *pathl; | |
779 | 673 GList *dlist = NULL; |
674 GList *flist = NULL; | |
675 int (*stat_func)(const char *path, struct stat *buf); | |
586 | 676 |
779 | 677 g_assert(files || dirs); |
678 | |
679 if (files) *files = NULL; | |
680 if (dirs) *dirs = NULL; | |
586 | 681 |
783 | 682 pathl = path_from_utf8(dir_fd->path); |
779 | 683 if (!pathl) return FALSE; |
684 | |
685 dp = opendir(pathl); | |
686 if (dp == NULL) | |
586 | 687 { |
688 g_free(pathl); | |
689 return FALSE; | |
690 } | |
691 | |
779 | 692 if (follow_symlinks) |
693 stat_func = stat; | |
694 else | |
695 stat_func = lstat; | |
696 | |
586 | 697 while ((dir = readdir(dp)) != NULL) |
698 { | |
779 | 699 struct stat ent_sbuf; |
700 const gchar *name = dir->d_name; | |
701 gchar *filepath; | |
702 | |
703 if (!options->file_filter.show_hidden_files && ishidden(name)) | |
704 continue; | |
705 | |
706 filepath = g_build_filename(pathl, name, NULL); | |
707 if (stat_func(filepath, &ent_sbuf) >= 0) | |
586 | 708 { |
779 | 709 if (S_ISDIR(ent_sbuf.st_mode)) |
586 | 710 { |
779 | 711 /* we ignore the .thumbnails dir for cleanliness */ |
712 if (dirs && | |
713 !(name[0] == '.' && (name[1] == '\0' || (name[1] == '.' && name[2] == '\0'))) && | |
714 strcmp(name, GQ_CACHE_LOCAL_THUMB) != 0 && | |
715 strcmp(name, GQ_CACHE_LOCAL_METADATA) != 0 && | |
716 strcmp(name, THUMB_FOLDER_LOCAL) != 0) | |
586 | 717 { |
779 | 718 dlist = g_list_prepend(dlist, file_data_new_local(filepath, &ent_sbuf, FALSE)); |
586 | 719 } |
720 } | |
779 | 721 else |
722 { | |
723 if (files && filter_name_exists(name)) | |
724 { | |
725 flist = g_list_prepend(flist, file_data_new_local(filepath, &ent_sbuf, TRUE)); | |
726 } | |
727 } | |
586 | 728 } |
779 | 729 g_free(filepath); |
586 | 730 } |
731 | |
732 closedir(dp); | |
779 | 733 |
586 | 734 g_free(pathl); |
735 | |
736 if (dirs) *dirs = dlist; | |
779 | 737 if (files) *files = filelist_filter_out_sidecars(flist); |
586 | 738 |
739 return TRUE; | |
740 } | |
741 | |
783 | 742 gint filelist_read(FileData *dir_fd, GList **files, GList **dirs) |
586 | 743 { |
783 | 744 return filelist_read_real(dir_fd, files, dirs, TRUE); |
586 | 745 } |
746 | |
783 | 747 gint filelist_read_lstat(FileData *dir_fd, GList **files, GList **dirs) |
586 | 748 { |
783 | 749 return filelist_read_real(dir_fd, files, dirs, FALSE); |
586 | 750 } |
751 | |
752 void filelist_free(GList *list) | |
753 { | |
754 GList *work; | |
755 | |
756 work = list; | |
757 while (work) | |
758 { | |
759 file_data_unref((FileData *)work->data); | |
760 work = work->next; | |
761 } | |
762 | |
763 g_list_free(list); | |
764 } | |
765 | |
766 | |
767 GList *filelist_copy(GList *list) | |
768 { | |
769 GList *new_list = NULL; | |
770 GList *work; | |
771 | |
772 work = list; | |
773 while (work) | |
774 { | |
775 FileData *fd; | |
776 | |
777 fd = work->data; | |
778 work = work->next; | |
779 | |
780 new_list = g_list_prepend(new_list, file_data_ref(fd)); | |
781 } | |
782 | |
783 return g_list_reverse(new_list); | |
784 } | |
785 | |
786 GList *filelist_from_path_list(GList *list) | |
787 { | |
788 GList *new_list = NULL; | |
789 GList *work; | |
790 | |
791 work = list; | |
792 while (work) | |
793 { | |
794 gchar *path; | |
795 | |
796 path = work->data; | |
797 work = work->next; | |
798 | |
799 new_list = g_list_prepend(new_list, file_data_new_simple(path)); | |
800 } | |
801 | |
802 return g_list_reverse(new_list); | |
803 } | |
804 | |
805 GList *filelist_to_path_list(GList *list) | |
806 { | |
807 GList *new_list = NULL; | |
808 GList *work; | |
809 | |
810 work = list; | |
811 while (work) | |
812 { | |
813 FileData *fd; | |
814 | |
815 fd = work->data; | |
816 work = work->next; | |
817 | |
818 new_list = g_list_prepend(new_list, g_strdup(fd->path)); | |
819 } | |
820 | |
821 return g_list_reverse(new_list); | |
822 } | |
823 | |
824 GList *filelist_filter(GList *list, gint is_dir_list) | |
825 { | |
826 GList *work; | |
827 | |
828 if (!is_dir_list && options->file_filter.disable && options->file_filter.show_hidden_files) return list; | |
829 | |
830 work = list; | |
831 while (work) | |
832 { | |
833 FileData *fd = (FileData *)(work->data); | |
834 const gchar *name = fd->name; | |
835 | |
836 if ((!options->file_filter.show_hidden_files && ishidden(name)) || | |
837 (!is_dir_list && !filter_name_exists(name)) || | |
838 (is_dir_list && name[0] == '.' && (strcmp(name, GQ_CACHE_LOCAL_THUMB) == 0 || | |
839 strcmp(name, GQ_CACHE_LOCAL_METADATA) == 0)) ) | |
840 { | |
841 GList *link = work; | |
842 work = work->next; | |
843 list = g_list_remove_link(list, link); | |
844 file_data_unref(fd); | |
845 g_list_free(link); | |
846 } | |
847 else | |
848 { | |
849 work = work->next; | |
850 } | |
851 } | |
852 | |
853 return list; | |
854 } | |
855 | |
856 /* | |
857 *----------------------------------------------------------------------------- | |
858 * filelist recursive | |
859 *----------------------------------------------------------------------------- | |
860 */ | |
861 | |
862 static gint filelist_sort_path_cb(gconstpointer a, gconstpointer b) | |
863 { | |
864 return CASE_SORT(((FileData *)a)->path, ((FileData *)b)->path); | |
865 } | |
866 | |
867 GList *filelist_sort_path(GList *list) | |
868 { | |
869 return g_list_sort(list, filelist_sort_path_cb); | |
870 } | |
871 | |
872 static void filelist_recursive_append(GList **list, GList *dirs) | |
873 { | |
874 GList *work; | |
875 | |
876 work = dirs; | |
877 while (work) | |
878 { | |
879 FileData *fd = (FileData *)(work->data); | |
780
44128da39e13
Drop initialization to NULL since filelist_read() will take care of it.
zas_
parents:
779
diff
changeset
|
880 GList *f; |
44128da39e13
Drop initialization to NULL since filelist_read() will take care of it.
zas_
parents:
779
diff
changeset
|
881 GList *d; |
586 | 882 |
783 | 883 if (filelist_read(fd, &f, &d)) |
586 | 884 { |
885 f = filelist_filter(f, FALSE); | |
886 f = filelist_sort_path(f); | |
887 *list = g_list_concat(*list, f); | |
888 | |
889 d = filelist_filter(d, TRUE); | |
890 d = filelist_sort_path(d); | |
891 filelist_recursive_append(list, d); | |
892 filelist_free(d); | |
893 } | |
894 | |
895 work = work->next; | |
896 } | |
897 } | |
898 | |
783 | 899 GList *filelist_recursive(FileData *dir_fd) |
586 | 900 { |
780
44128da39e13
Drop initialization to NULL since filelist_read() will take care of it.
zas_
parents:
779
diff
changeset
|
901 GList *list; |
44128da39e13
Drop initialization to NULL since filelist_read() will take care of it.
zas_
parents:
779
diff
changeset
|
902 GList *d; |
586 | 903 |
783 | 904 if (!filelist_read(dir_fd, &list, &d)) return NULL; |
586 | 905 list = filelist_filter(list, FALSE); |
906 list = filelist_sort_path(list); | |
907 | |
908 d = filelist_filter(d, TRUE); | |
909 d = filelist_sort_path(d); | |
910 filelist_recursive_append(&list, d); | |
911 filelist_free(d); | |
912 | |
913 return list; | |
914 } | |
590 | 915 |
916 | |
800 | 917 /* |
918 * marks and orientation | |
919 */ | |
920 | |
921 | |
922 gboolean file_data_get_mark(FileData *fd, gint n) | |
923 { | |
924 return !!(fd->marks & (1 << n)); | |
925 } | |
926 | |
927 void file_data_set_mark(FileData *fd, gint n, gboolean value) | |
928 { | |
929 if (!value == !(fd->marks & (1 << n))) return; | |
930 | |
931 fd->marks = fd->marks ^ (1 << n); | |
932 file_data_increment_version(fd); | |
933 file_data_send_notification(fd, NOTIFY_TYPE_INTERNAL); | |
934 | |
935 } | |
936 | |
937 gint file_data_get_user_orientation(FileData *fd) | |
938 { | |
939 return fd->user_orientation; | |
940 } | |
941 | |
942 void file_data_set_user_orientation(FileData *fd, gint value) | |
943 { | |
944 if (fd->user_orientation == value) return; | |
945 | |
946 fd->user_orientation = value; | |
947 file_data_increment_version(fd); | |
948 file_data_send_notification(fd, NOTIFY_TYPE_INTERNAL); | |
949 } | |
950 | |
951 | |
590 | 952 |
953 /* | |
954 * file_data - operates on the given fd | |
955 * file_data_sc - operates on the given fd + sidecars - all fds linked via fd->sidecar_files or fd->parent | |
956 */ | |
957 | |
958 | |
959 /* return list of sidecar file extensions in a string */ | |
596 | 960 gchar *file_data_sc_list_to_string(FileData *fd) |
961 { | |
962 GList *work; | |
963 GString *result = g_string_new(""); | |
964 | |
965 work = fd->sidecar_files; | |
966 while (work) | |
967 { | |
968 FileData *sfd = work->data; | |
969 result = g_string_append(result, "+ "); | |
970 result = g_string_append(result, sfd->extension); | |
971 work = work->next; | |
972 if (work) result = g_string_append_c(result, ' '); | |
973 } | |
974 | |
975 return g_string_free(result, FALSE); | |
976 } | |
590 | 977 |
978 | |
979 /* disables / enables grouping for particular file, sends UPDATE notification */ | |
980 void file_data_disable_grouping(FileData *fd); // now file_data_disconnect_sidecar_file, broken | |
981 void file_data_disable_grouping(FileData *fd); | |
982 | |
983 /* runs stat on a file and sends UPDATE notification if it has been changed */ | |
984 void file_data_sc_update(FileData *fd); | |
985 | |
986 | |
987 | |
988 | |
989 /* | |
990 * add FileDataChangeInfo (see typedefs.h) for the given operation | |
991 * uses file_data_add_change_info | |
992 * | |
993 * fails if the fd->change already exists - change operations can't run in parallel | |
994 * fd->change_info works as a lock | |
995 * | |
996 * dest can be NULL - in this case the current name is used for now, it will | |
997 * be changed later | |
998 */ | |
999 | |
1000 /* | |
1001 FileDataChangeInfo types: | |
1002 COPY | |
1003 MOVE - patch is changed, name may be changed too | |
1004 RENAME - path remains unchanged, name is changed | |
1005 extension should remain (FIXME should we allow editing extension? it will make problems wth grouping) | |
1006 sidecar names are changed too, extensions are not changed | |
1007 DELETE | |
1008 UPDATE - file size, date or grouping has been changed | |
1009 */ | |
1010 | |
1011 gboolean file_data_add_ci(FileData *fd, FileDataChangeType type, const gchar *src, const gchar *dest) | |
1012 { | |
1013 | |
1014 FileDataChangeInfo *fdci; | |
1015 | |
1016 if (fd->change) return FALSE; | |
1017 | |
1018 fdci = g_new0(FileDataChangeInfo, 1); | |
1019 | |
1020 fdci->type = type; | |
1021 | |
1022 if (src) | |
1023 fdci->source = g_strdup(src); | |
1024 else | |
1025 fdci->source = g_strdup(fd->path); | |
1026 | |
1027 if (dest) | |
1028 fdci->dest = g_strdup(dest); | |
1029 | |
1030 fd->change = fdci; | |
1031 | |
1032 return TRUE; | |
1033 } | |
1034 | |
1035 void file_data_free_ci(FileData *fd) | |
1036 { | |
1037 FileDataChangeInfo *fdci = fd->change; | |
1038 | |
1039 if (!fdci) | |
1040 return; | |
1041 | |
1042 g_free(fdci->source); | |
1043 g_free(fdci->dest); | |
1044 | |
1045 g_free(fdci); | |
1046 | |
1047 fd->change = NULL; | |
1048 } | |
1049 | |
1050 | |
1051 static gboolean file_data_sc_add_ci(FileData *fd, FileDataChangeType type) | |
1052 { | |
1053 GList *work; | |
1054 if (fd->parent) fd = fd->parent; | |
1055 | |
1056 if (fd->change) return FALSE; | |
1057 work = fd->sidecar_files; | |
1058 while (work) | |
1059 { | |
1060 FileData *sfd = work->data; | |
1061 if (sfd->change) return FALSE; | |
1062 work = work->next; | |
1063 } | |
1064 | |
1065 file_data_add_ci(fd, type, NULL, NULL); | |
1066 | |
1067 work = fd->sidecar_files; | |
1068 while (work) | |
1069 { | |
1070 FileData *sfd = work->data; | |
1071 file_data_add_ci(sfd, type, NULL, NULL); | |
1072 work = work->next; | |
1073 } | |
1074 | |
1075 return TRUE; | |
1076 } | |
1077 | |
1078 static gboolean file_data_sc_check_ci(FileData *fd, FileDataChangeType type) | |
1079 { | |
1080 GList *work; | |
1081 if (fd->parent) fd = fd->parent; | |
1082 | |
1083 if (!fd->change) return FALSE; | |
1084 if (fd->change->type != type) return FALSE; | |
1085 work = fd->sidecar_files; | |
1086 while (work) | |
1087 { | |
1088 FileData *sfd = work->data; | |
1089 if (!sfd->change) return FALSE; | |
1090 if (sfd->change->type != type) return FALSE; | |
1091 work = work->next; | |
1092 } | |
1093 return TRUE; | |
1094 } | |
1095 | |
1096 | |
751 | 1097 gboolean file_data_sc_add_ci_copy(FileData *fd, const gchar *dest_path) |
590 | 1098 { |
1099 if (!file_data_sc_add_ci(fd, FILEDATA_CHANGE_COPY)) return FALSE; | |
1100 file_data_sc_update_ci_copy(fd, dest_path); | |
1101 return TRUE; | |
1102 } | |
1103 | |
751 | 1104 gboolean file_data_sc_add_ci_move(FileData *fd, const gchar *dest_path) |
590 | 1105 { |
1106 if (!file_data_sc_add_ci(fd, FILEDATA_CHANGE_MOVE)) return FALSE; | |
1107 file_data_sc_update_ci_move(fd, dest_path); | |
1108 return TRUE; | |
1109 } | |
1110 | |
751 | 1111 gboolean file_data_sc_add_ci_rename(FileData *fd, const gchar *dest_path) |
590 | 1112 { |
1113 if (!file_data_sc_add_ci(fd, FILEDATA_CHANGE_RENAME)) return FALSE; | |
1114 file_data_sc_update_ci_rename(fd, dest_path); | |
1115 return TRUE; | |
1116 } | |
1117 | |
1118 gboolean file_data_sc_add_ci_delete(FileData *fd) | |
1119 { | |
1120 return file_data_sc_add_ci(fd, FILEDATA_CHANGE_DELETE); | |
1121 } | |
1122 | |
753 | 1123 gboolean file_data_sc_add_ci_unspecified(FileData *fd, const gchar *dest_path) |
590 | 1124 { |
753 | 1125 if (!file_data_sc_add_ci(fd, FILEDATA_CHANGE_UNSPECIFIED)) return FALSE; |
1126 file_data_sc_update_ci_unspecified(fd, dest_path); | |
1127 return TRUE; | |
590 | 1128 } |
1129 | |
1130 void file_data_sc_free_ci(FileData *fd) | |
1131 { | |
1132 GList *work; | |
1133 if (fd->parent) fd = fd->parent; | |
1134 | |
1135 file_data_free_ci(fd); | |
1136 | |
1137 work = fd->sidecar_files; | |
1138 while (work) | |
1139 { | |
1140 FileData *sfd = work->data; | |
1141 file_data_free_ci(sfd); | |
1142 work = work->next; | |
1143 } | |
1144 } | |
1145 | |
751 | 1146 gboolean file_data_sc_add_ci_delete_list(GList *fd_list) |
1147 { | |
1148 GList *work; | |
1149 gboolean ret = TRUE; | |
1150 work = fd_list; | |
1151 while (work) | |
1152 { | |
1153 FileData *fd = work->data; | |
1154 if (!file_data_sc_add_ci_delete(fd)) ret = FALSE; | |
1155 work = work->next; | |
1156 } | |
1157 return ret; | |
1158 } | |
1159 | |
1160 gboolean file_data_sc_add_ci_copy_list(GList *fd_list, const gchar *dest) | |
1161 { | |
1162 GList *work; | |
1163 gboolean ret = TRUE; | |
1164 work = fd_list; | |
1165 while (work) | |
1166 { | |
1167 FileData *fd = work->data; | |
1168 if (!file_data_sc_add_ci_copy(fd, dest)) ret = FALSE; | |
1169 work = work->next; | |
1170 } | |
1171 return ret; | |
1172 } | |
1173 | |
1174 gboolean file_data_sc_add_ci_move_list(GList *fd_list, const gchar *dest) | |
1175 { | |
1176 GList *work; | |
1177 gboolean ret = TRUE; | |
1178 work = fd_list; | |
1179 while (work) | |
1180 { | |
1181 FileData *fd = work->data; | |
1182 if (!file_data_sc_add_ci_move(fd, dest)) ret = FALSE; | |
1183 work = work->next; | |
1184 } | |
1185 return ret; | |
1186 } | |
1187 | |
1188 gboolean file_data_sc_add_ci_rename_list(GList *fd_list, const gchar *dest) | |
1189 { | |
1190 GList *work; | |
1191 gboolean ret = TRUE; | |
1192 work = fd_list; | |
1193 while (work) | |
1194 { | |
1195 FileData *fd = work->data; | |
1196 if (!file_data_sc_add_ci_rename(fd, dest)) ret = FALSE; | |
1197 work = work->next; | |
1198 } | |
1199 return ret; | |
1200 } | |
1201 | |
753 | 1202 gboolean file_data_sc_add_ci_unspecified_list(GList *fd_list, const gchar *dest) |
1203 { | |
1204 GList *work; | |
1205 gboolean ret = TRUE; | |
1206 work = fd_list; | |
1207 while (work) | |
1208 { | |
1209 FileData *fd = work->data; | |
1210 if (!file_data_sc_add_ci_unspecified(fd, dest)) ret = FALSE; | |
1211 work = work->next; | |
1212 } | |
1213 return ret; | |
1214 } | |
1215 | |
751 | 1216 void file_data_sc_free_ci_list(GList *fd_list) |
1217 { | |
1218 GList *work; | |
1219 work = fd_list; | |
1220 while (work) | |
1221 { | |
1222 FileData *fd = work->data; | |
1223 file_data_sc_free_ci(fd); | |
1224 work = work->next; | |
1225 } | |
1226 } | |
590 | 1227 |
1228 /* | |
1229 * update existing fd->change, it will be used from dialog callbacks for interactive editing | |
1230 * fails if fd->change does not exist or the change type does not match | |
1231 */ | |
1232 | |
751 | 1233 static void file_data_update_ci_dest(FileData *fd, const gchar *dest_path) |
590 | 1234 { |
1235 g_free(fd->change->dest); | |
1236 fd->change->dest = g_strdup(dest_path); | |
1237 } | |
1238 | |
751 | 1239 static void file_data_update_ci_dest_preserve_ext(FileData *fd, const gchar *dest_path) |
590 | 1240 { |
1241 const char *extension = extension_from_path(fd->change->source); | |
751 | 1242 gchar *base = remove_extension_from_path(dest_path); |
590 | 1243 g_free(fd->change->dest); |
751 | 1244 fd->change->dest = g_strdup_printf("%s%s", base, extension); |
1245 g_free(base); | |
590 | 1246 } |
1247 | |
751 | 1248 static void file_data_sc_update_ci(FileData *fd, const gchar *dest_path) |
590 | 1249 { |
1250 GList *work; | |
751 | 1251 gchar *dest_path_full = NULL; |
590 | 1252 if (fd->parent) fd = fd->parent; |
1253 | |
751 | 1254 if (!dest_path) dest_path = fd->path; |
1255 | |
1256 if (!strchr(dest_path, G_DIR_SEPARATOR)) /* we got only filename, not a full path */ | |
1257 { | |
1258 gchar *dir = remove_level_from_path(fd->path); | |
1259 dest_path_full = g_build_filename(dir, dest_path, NULL); | |
1260 g_free(dir); | |
1261 dest_path = dest_path_full; | |
1262 } | |
1263 | |
1264 if (isdir(dest_path)) | |
1265 { | |
1266 dest_path_full = g_build_filename(dest_path, fd->name, NULL); | |
1267 dest_path = dest_path_full; | |
1268 } | |
1269 | |
1270 | |
590 | 1271 file_data_update_ci_dest(fd, dest_path); |
1272 work = fd->sidecar_files; | |
1273 while (work) | |
1274 { | |
1275 FileData *sfd = work->data; | |
1276 file_data_update_ci_dest_preserve_ext(sfd, dest_path); | |
1277 work = work->next; | |
1278 } | |
751 | 1279 g_free(dest_path_full); |
590 | 1280 } |
1281 | |
751 | 1282 gint file_data_sc_update_ci_copy(FileData *fd, const gchar *dest_path) |
590 | 1283 { |
1284 if (!file_data_sc_check_ci(fd, FILEDATA_CHANGE_COPY)) return FALSE; | |
1285 file_data_sc_update_ci(fd, dest_path); | |
1286 return TRUE; | |
1287 } | |
1288 | |
751 | 1289 gint file_data_sc_update_ci_move(FileData *fd, const gchar *dest_path) |
590 | 1290 { |
1291 if (!file_data_sc_check_ci(fd, FILEDATA_CHANGE_MOVE)) return FALSE; | |
1292 file_data_sc_update_ci(fd, dest_path); | |
1293 return TRUE; | |
1294 } | |
1295 | |
751 | 1296 gint file_data_sc_update_ci_rename(FileData *fd, const gchar *dest_path) |
590 | 1297 { |
1298 if (!file_data_sc_check_ci(fd, FILEDATA_CHANGE_RENAME)) return FALSE; | |
1299 file_data_sc_update_ci(fd, dest_path); | |
1300 return TRUE; | |
1301 } | |
1302 | |
753 | 1303 gint file_data_sc_update_ci_unspecified(FileData *fd, const gchar *dest_path) |
1304 { | |
1305 if (!file_data_sc_check_ci(fd, FILEDATA_CHANGE_UNSPECIFIED)) return FALSE; | |
1306 file_data_sc_update_ci(fd, dest_path); | |
1307 return TRUE; | |
1308 } | |
1309 | |
590 | 1310 |
751 | 1311 gboolean file_data_sc_update_ci_move_list(GList *fd_list, const gchar *dest) |
1312 { | |
1313 GList *work; | |
1314 gboolean ret = TRUE; | |
1315 work = fd_list; | |
1316 while (work) | |
1317 { | |
1318 FileData *fd = work->data; | |
1319 if (!file_data_sc_update_ci_move(fd, dest)) ret = FALSE; | |
1320 work = work->next; | |
1321 } | |
1322 return ret; | |
1323 } | |
1324 | |
1325 gboolean file_data_sc_update_ci_copy_list(GList *fd_list, const gchar *dest) | |
1326 { | |
1327 GList *work; | |
1328 gboolean ret = TRUE; | |
1329 work = fd_list; | |
1330 while (work) | |
1331 { | |
1332 FileData *fd = work->data; | |
1333 if (!file_data_sc_update_ci_copy(fd, dest)) ret = FALSE; | |
1334 work = work->next; | |
1335 } | |
1336 return ret; | |
1337 } | |
1338 | |
753 | 1339 gboolean file_data_sc_update_ci_unspecified_list(GList *fd_list, const gchar *dest) |
1340 { | |
1341 GList *work; | |
1342 gboolean ret = TRUE; | |
1343 work = fd_list; | |
1344 while (work) | |
1345 { | |
1346 FileData *fd = work->data; | |
1347 if (!file_data_sc_update_ci_unspecified(fd, dest)) ret = FALSE; | |
1348 work = work->next; | |
1349 } | |
1350 return ret; | |
1351 } | |
1352 | |
590 | 1353 |
1354 /* | |
1355 * check dest paths - dest image exists, etc. | |
1356 * returns FIXME | |
1357 * it should detect all possible problems with the planned operation | |
1358 */ | |
1359 | |
1360 gint file_data_sc_check_ci_dest(FileData *fd) | |
1361 { | |
1362 } | |
1363 | |
1364 | |
1365 | |
1366 | |
1367 /* | |
1368 * perform the change described by FileFataChangeInfo | |
1369 * it is used for internal operations, | |
1370 * this function actually operates with files on the filesystem | |
1371 * it should implement safe delete | |
1372 */ | |
1373 | |
1374 static gboolean file_data_perform_move(FileData *fd) | |
1375 { | |
1376 g_assert(!strcmp(fd->change->source, fd->path)); | |
1377 return move_file(fd->change->source, fd->change->dest); | |
1378 } | |
1379 | |
1380 static gboolean file_data_perform_copy(FileData *fd) | |
1381 { | |
1382 g_assert(!strcmp(fd->change->source, fd->path)); | |
1383 return copy_file(fd->change->source, fd->change->dest); | |
1384 } | |
1385 | |
1386 static gboolean file_data_perform_delete(FileData *fd) | |
1387 { | |
1388 return unlink_file(fd->path); | |
1389 } | |
1390 | |
1391 static gboolean file_data_perform_ci(FileData *fd) | |
1392 { | |
1393 FileDataChangeType type = fd->change->type; | |
1394 switch (type) | |
1395 { | |
1396 case FILEDATA_CHANGE_MOVE: | |
1397 return file_data_perform_move(fd); | |
1398 case FILEDATA_CHANGE_COPY: | |
1399 return file_data_perform_copy(fd); | |
1400 case FILEDATA_CHANGE_RENAME: | |
1401 return file_data_perform_move(fd); /* the same as move */ | |
1402 case FILEDATA_CHANGE_DELETE: | |
1403 return file_data_perform_delete(fd); | |
753 | 1404 case FILEDATA_CHANGE_UNSPECIFIED: |
596 | 1405 /* nothing to do here */ |
590 | 1406 break; |
1407 } | |
1408 return TRUE; | |
1409 } | |
1410 | |
1411 | |
1412 | |
1413 gboolean file_data_sc_perform_ci(FileData *fd) | |
1414 { | |
1415 GList *work; | |
1416 gboolean ret = TRUE; | |
1417 FileDataChangeType type = fd->change->type; | |
1418 if (!file_data_sc_check_ci(fd, type)) return FALSE; | |
1419 | |
1420 work = fd->sidecar_files; | |
1421 while (work) | |
1422 { | |
1423 FileData *sfd = work->data; | |
1424 if (!file_data_perform_ci(sfd)) ret = FALSE; | |
1425 work = work->next; | |
1426 } | |
1427 if (!file_data_perform_ci(fd)) ret = FALSE; | |
1428 return ret; | |
1429 } | |
1430 | |
1431 /* | |
1432 * updates FileData structure according to FileDataChangeInfo | |
1433 */ | |
1434 | |
1435 static void file_data_apply_ci(FileData *fd) | |
1436 { | |
1437 FileDataChangeType type = fd->change->type; | |
1438 /* FIXME delete ?*/ | |
773 | 1439 if (type == FILEDATA_CHANGE_MOVE || type == FILEDATA_CHANGE_RENAME) |
590 | 1440 { |
1441 file_data_set_path(fd, fd->change->dest); | |
1442 } | |
763
81f9e8dbb4bf
improved infrastructure for tracing changes, optimized vflist_populate_view
nadvornik
parents:
753
diff
changeset
|
1443 file_data_increment_version(fd); |
800 | 1444 file_data_send_notification(fd, NOTIFY_TYPE_CHANGE); |
590 | 1445 } |
1446 | |
596 | 1447 gint file_data_sc_apply_ci(FileData *fd) |
590 | 1448 { |
1449 GList *work; | |
1450 FileDataChangeType type = fd->change->type; | |
1451 if (!file_data_sc_check_ci(fd, type)) return FALSE; | |
1452 | |
1453 work = fd->sidecar_files; | |
1454 while (work) | |
1455 { | |
1456 FileData *sfd = work->data; | |
1457 file_data_apply_ci(sfd); | |
1458 work = work->next; | |
1459 } | |
1460 file_data_apply_ci(fd); | |
1461 return TRUE; | |
1462 } | |
1463 | |
1464 | |
1465 /* | |
1466 * notify other modules about the change described by FileFataChangeInfo | |
1467 */ | |
1468 | |
1469 /* might use file_maint_ functions for now, later it should be changed to a system of callbacks | |
1470 FIXME do we need the ignore_list? It looks like a workaround for ineffective | |
1471 implementation in view_file_list.c */ | |
1472 | |
784
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
1473 |
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
1474 |
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
1475 |
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
1476 typedef struct _NotifyData NotifyData; |
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
1477 |
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
1478 struct _NotifyData { |
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
1479 FileDataNotifyFunc func; |
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
1480 gpointer data; |
791 | 1481 NotifyPriority priority; |
784
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
1482 }; |
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
1483 |
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
1484 static GList *notify_func_list = NULL; |
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
1485 |
791 | 1486 static gint file_data_notify_sort(gconstpointer a, gconstpointer b) |
1487 { | |
1488 NotifyData *nda = (NotifyData *)a; | |
1489 NotifyData *ndb = (NotifyData *)b; | |
1490 if (nda->priority < ndb->priority) return -1; | |
1491 if (nda->priority > ndb->priority) return 1; | |
1492 return 0; | |
1493 } | |
1494 | |
1495 gint 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
|
1496 { |
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
1497 NotifyData *nd = g_new(NotifyData, 1); |
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
1498 nd->func = func; |
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
1499 nd->data = data; |
791 | 1500 nd->priority = priority; |
1501 notify_func_list = g_list_insert_sorted(notify_func_list, nd, file_data_notify_sort); | |
1502 DEBUG_1("Notify func registered: %p", nd); | |
784
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
1503 return TRUE; |
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
1504 } |
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
1505 |
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
1506 gint file_data_unregister_notify_func(FileDataNotifyFunc func, gpointer data) |
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
1507 { |
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
1508 GList *work = notify_func_list; |
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
1509 |
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
1510 while(work) |
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
1511 { |
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
1512 NotifyData *nd = (NotifyData *)work->data; |
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
1513 if (nd->func == func && nd->data == data) |
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
1514 { |
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
1515 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
|
1516 g_free(nd); |
791 | 1517 DEBUG_1("Notify func unregistered: %p", nd); |
784
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
1518 return TRUE; |
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
1519 } |
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
1520 work = work->next; |
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
1521 } |
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
1522 return FALSE; |
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
1523 } |
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
1524 |
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
1525 |
792 | 1526 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
|
1527 { |
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
1528 GList *work = notify_func_list; |
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
1529 while(work) |
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
1530 { |
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
1531 NotifyData *nd = (NotifyData *)work->data; |
791 | 1532 DEBUG_1("Notify func calling: %p %s", nd, fd->path); |
792 | 1533 nd->func(fd, type, nd->data); |
784
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
1534 work = work->next; |
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
1535 } |
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
1536 } |
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
1537 |
791 | 1538 static GHashTable *file_data_monitor_pool = NULL; |
1539 static gint realtime_monitor_id = -1; | |
1540 | |
1541 static void realtime_monitor_check_cb(gpointer key, gpointer value, gpointer data) | |
1542 { | |
1543 FileData *fd = key; | |
1544 struct stat st; | |
1545 | |
800 | 1546 if (stat_utf8(fd->path, &st)) |
1547 file_data_check_changed_files(fd, &st); | |
1548 else | |
1549 file_data_send_notification(fd, NOTIFY_TYPE_REREAD); | |
791 | 1550 |
1551 DEBUG_1("monitor %s", fd->path); | |
1552 } | |
1553 | |
1554 static gboolean realtime_monitor_cb(gpointer data) | |
1555 { | |
1556 g_hash_table_foreach(file_data_monitor_pool, realtime_monitor_check_cb, NULL); | |
1557 return TRUE; | |
1558 } | |
1559 | |
1560 gint file_data_register_real_time_monitor(FileData *fd) | |
1561 { | |
1562 gint count = 0; | |
1563 | |
1564 file_data_ref(fd); | |
1565 | |
1566 if (!file_data_monitor_pool) | |
1567 file_data_monitor_pool = g_hash_table_new(g_direct_hash, g_direct_equal); | |
1568 | |
1569 count = GPOINTER_TO_INT(g_hash_table_lookup(file_data_monitor_pool, fd)); | |
1570 | |
1571 DEBUG_1("Register realtime %d %s", count, fd->path); | |
1572 | |
1573 count++; | |
1574 g_hash_table_insert(file_data_monitor_pool, fd, GINT_TO_POINTER(count)); | |
1575 | |
1576 if (realtime_monitor_id == -1) | |
1577 { | |
1578 realtime_monitor_id = g_timeout_add(5000, realtime_monitor_cb, NULL); | |
1579 } | |
1580 return TRUE; | |
1581 } | |
1582 | |
1583 gint file_data_unregister_real_time_monitor(FileData *fd) | |
1584 { | |
1585 gint count; | |
1586 g_assert(file_data_monitor_pool); | |
1587 | |
1588 count = GPOINTER_TO_INT(g_hash_table_lookup(file_data_monitor_pool, fd)); | |
1589 | |
1590 DEBUG_1("Unregister realtime %d %s", count, fd->path); | |
1591 | |
1592 g_assert(count > 0); | |
1593 | |
1594 count--; | |
1595 | |
1596 if (count == 0) | |
1597 g_hash_table_remove(file_data_monitor_pool, fd); | |
1598 else | |
1599 g_hash_table_insert(file_data_monitor_pool, fd, GINT_TO_POINTER(count)); | |
1600 | |
1601 file_data_unref(fd); | |
1602 | |
1603 if (g_hash_table_size(file_data_monitor_pool) == 0) | |
1604 { | |
1605 g_source_remove(realtime_monitor_id); | |
1606 realtime_monitor_id = -1; | |
1607 return FALSE; | |
1608 } | |
1609 return TRUE; | |
1610 } | |
784
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
1611 |
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
1612 |
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
1613 |
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
1614 |
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
1615 |
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
1616 |
16b3a5c8aedc
new notification system (used only in vflist for now)
nadvornik
parents:
783
diff
changeset
|
1617 |