Mercurial > geeqie
annotate src/collect-io.c @ 1239:254b09942e68
metadata write mode (direct or sidecar) made configurable for each file
format
author | nadvornik |
---|---|
date | Sun, 11 Jan 2009 12:13:30 +0000 |
parents | e2bbe90b0dcd |
children | 8b89e3ff286b |
rev | line source |
---|---|
9 | 1 /* |
196 | 2 * Geeqie |
9 | 3 * (C) 2004 John Ellis |
475 | 4 * Copyright (C) 2008 The Geeqie Team |
9 | 5 * |
6 * Author: John Ellis | |
7 * | |
8 * This software is released under the GNU General Public License (GNU GPL). | |
9 * Please read the included file COPYING for more information. | |
10 * This software comes with no warranty of any kind, use at your own risk! | |
11 */ | |
12 | |
13 | |
281 | 14 #include "main.h" |
9 | 15 #include "collect-io.h" |
16 | |
17 #include "collect.h" | |
586 | 18 #include "filedata.h" |
9 | 19 #include "layout_util.h" |
1023
650c02c0c8ff
Move quoted_value() and escquote_value() to misc.[ch].
zas_
parents:
882
diff
changeset
|
20 #include "misc.h" |
312 | 21 #include "secure_save.h" |
9 | 22 #include "thumb.h" |
23 #include "ui_fileops.h" | |
24 | |
288
d1f74154463e
Replace occurences of Geeqie / geeqie by constants defined in main.h.
zas_
parents:
283
diff
changeset
|
25 #define GQ_COLLECTION_MARKER "#" GQ_APPNAME |
9 | 26 |
283 | 27 #define GQ_COLLECTION_FAIL_MIN 300 |
28 #define GQ_COLLECTION_FAIL_PERCENT 98 | |
360
822040a51249
Increase collection_load() buffer size and do not activate
zas_
parents:
359
diff
changeset
|
29 #define GQ_COLLECTION_READ_BUFSIZE 4096 |
9 | 30 |
138 | 31 typedef struct _CollectManagerEntry CollectManagerEntry; |
9 | 32 |
33 static void collection_load_thumb_step(CollectionData *cd); | |
138 | 34 static gint collection_save_private(CollectionData *cd, const gchar *path); |
35 | |
36 static CollectManagerEntry *collect_manager_get_entry(const gchar *path); | |
37 static void collect_manager_entry_reset(CollectManagerEntry *entry); | |
235 | 38 static gint collect_manager_process_action(CollectManagerEntry *entry, gchar **path_ptr); |
9 | 39 |
40 | |
41 static gint scan_geometry(gchar *buffer, gint *x, gint *y, gint *w, gint *h) | |
42 { | |
43 gint nx, ny, nw, nh; | |
44 | |
855 | 45 if (sscanf(buffer, "%d %d %d %d", &nx, &ny, &nw, &nh) != 4) return FALSE; |
9 | 46 |
47 *x = nx; | |
48 *y = ny; | |
49 *w = nw; | |
50 *h = nh; | |
51 | |
52 return TRUE; | |
53 } | |
54 | |
358 | 55 static gint collection_load_private(CollectionData *cd, const gchar *path, CollectionLoadFlags flags) |
9 | 56 { |
360
822040a51249
Increase collection_load() buffer size and do not activate
zas_
parents:
359
diff
changeset
|
57 gchar s_buf[GQ_COLLECTION_READ_BUFSIZE]; |
9 | 58 FILE *f; |
59 gchar *pathl; | |
361
e0295af4f716
collection_load(): replace official by limit_failures (and
zas_
parents:
360
diff
changeset
|
60 gint limit_failures = TRUE; |
9 | 61 gint success = TRUE; |
365 | 62 gint has_official_header = FALSE; |
63 gint has_geometry_header = FALSE; | |
64 gint has_gqview_header = FALSE; | |
65 gint need_header = TRUE; | |
9 | 66 guint total = 0; |
67 guint fail = 0; | |
138 | 68 gboolean changed = FALSE; |
69 CollectManagerEntry *entry = NULL; | |
366
ce00494827e2
collection_load(): use booleans and always count failures (for debug).
zas_
parents:
365
diff
changeset
|
70 guint flush = !!(flags & COLLECTION_LOAD_FLUSH); |
ce00494827e2
collection_load(): use booleans and always count failures (for debug).
zas_
parents:
365
diff
changeset
|
71 guint append = !!(flags & COLLECTION_LOAD_APPEND); |
ce00494827e2
collection_load(): use booleans and always count failures (for debug).
zas_
parents:
365
diff
changeset
|
72 guint only_geometry = !!(flags & COLLECTION_LOAD_GEOMETRY); |
9 | 73 |
359
96fb24f948b7
Merge collection_load_only_geometry() into collection_load().
zas_
parents:
358
diff
changeset
|
74 if (!only_geometry) |
96fb24f948b7
Merge collection_load_only_geometry() into collection_load().
zas_
parents:
358
diff
changeset
|
75 { |
96fb24f948b7
Merge collection_load_only_geometry() into collection_load().
zas_
parents:
358
diff
changeset
|
76 collection_load_stop(cd); |
9 | 77 |
359
96fb24f948b7
Merge collection_load_only_geometry() into collection_load().
zas_
parents:
358
diff
changeset
|
78 if (flush) |
96fb24f948b7
Merge collection_load_only_geometry() into collection_load().
zas_
parents:
358
diff
changeset
|
79 collect_manager_flush(); |
96fb24f948b7
Merge collection_load_only_geometry() into collection_load().
zas_
parents:
358
diff
changeset
|
80 else |
96fb24f948b7
Merge collection_load_only_geometry() into collection_load().
zas_
parents:
358
diff
changeset
|
81 entry = collect_manager_get_entry(path); |
9 | 82 |
359
96fb24f948b7
Merge collection_load_only_geometry() into collection_load().
zas_
parents:
358
diff
changeset
|
83 if (!append) |
96fb24f948b7
Merge collection_load_only_geometry() into collection_load().
zas_
parents:
358
diff
changeset
|
84 { |
96fb24f948b7
Merge collection_load_only_geometry() into collection_load().
zas_
parents:
358
diff
changeset
|
85 collection_list_free(cd->list); |
96fb24f948b7
Merge collection_load_only_geometry() into collection_load().
zas_
parents:
358
diff
changeset
|
86 cd->list = NULL; |
96fb24f948b7
Merge collection_load_only_geometry() into collection_load().
zas_
parents:
358
diff
changeset
|
87 } |
9 | 88 } |
89 | |
90 if (!path && !cd->path) return FALSE; | |
91 | |
92 if (!path) path = cd->path; | |
442 | 93 |
506
fc9c8a3e1a8b
Handle the newline in DEBUG_N() macro instead of adding one
zas_
parents:
495
diff
changeset
|
94 DEBUG_1("collection load: append=%d flush=%d only_geometry=%d path=%s", |
360
822040a51249
Increase collection_load() buffer size and do not activate
zas_
parents:
359
diff
changeset
|
95 append, flush, only_geometry, path); |
9 | 96 |
97 /* load it */ | |
98 pathl = path_from_utf8(path); | |
99 f = fopen(pathl, "r"); | |
100 g_free(pathl); | |
101 if (!f) | |
102 { | |
673
fbebf5cf4a55
Do not use printf() directly but use new wrapper function log_printf() instead.
zas_
parents:
671
diff
changeset
|
103 log_printf("Failed to open collection file: \"%s\"\n", path); |
9 | 104 return FALSE; |
105 } | |
106 | |
107 while (fgets(s_buf, sizeof(s_buf), f)) | |
108 { | |
109 gchar *buf; | |
363
5b8617c3821c
collection_load(): accept whitespaces at start of lines.
zas_
parents:
362
diff
changeset
|
110 gchar *p = s_buf; |
5b8617c3821c
collection_load(): accept whitespaces at start of lines.
zas_
parents:
362
diff
changeset
|
111 |
5b8617c3821c
collection_load(): accept whitespaces at start of lines.
zas_
parents:
362
diff
changeset
|
112 /* Skip whitespaces and empty lines */ |
5b8617c3821c
collection_load(): accept whitespaces at start of lines.
zas_
parents:
362
diff
changeset
|
113 while (*p && g_ascii_isspace(*p)) p++; |
5b8617c3821c
collection_load(): accept whitespaces at start of lines.
zas_
parents:
362
diff
changeset
|
114 if (*p == '\n' || *p == '\r') continue; |
5b8617c3821c
collection_load(): accept whitespaces at start of lines.
zas_
parents:
362
diff
changeset
|
115 |
5b8617c3821c
collection_load(): accept whitespaces at start of lines.
zas_
parents:
362
diff
changeset
|
116 /* Parse comments */ |
5b8617c3821c
collection_load(): accept whitespaces at start of lines.
zas_
parents:
362
diff
changeset
|
117 if (*p == '#') |
9 | 118 { |
365 | 119 if (!need_header) continue; |
605
651ae2be1031
Use g_ascii_strncasecmp() instead of strncasecmp() where applicable.
zas_
parents:
586
diff
changeset
|
120 if (g_ascii_strncasecmp(p, GQ_COLLECTION_MARKER, strlen(GQ_COLLECTION_MARKER)) == 0) |
9 | 121 { |
122 /* Looks like an official collection, allow unchecked input. | |
123 * All this does is allow adding files that may not exist, | |
124 * which is needed for the collection manager to work. | |
125 * Also unofficial files abort after too many invalid entries. | |
126 */ | |
365 | 127 has_official_header = TRUE; |
361
e0295af4f716
collection_load(): replace official by limit_failures (and
zas_
parents:
360
diff
changeset
|
128 limit_failures = FALSE; |
9 | 129 } |
363
5b8617c3821c
collection_load(): accept whitespaces at start of lines.
zas_
parents:
362
diff
changeset
|
130 else if (strncmp(p, "#geometry:", 10 ) == 0 && |
442 | 131 scan_geometry(p + 10, &cd->window_x, &cd->window_y, &cd->window_w, &cd->window_h)) |
9 | 132 { |
365 | 133 has_geometry_header = TRUE; |
9 | 134 cd->window_read = TRUE; |
365 | 135 if (only_geometry) break; |
9 | 136 } |
605
651ae2be1031
Use g_ascii_strncasecmp() instead of strncasecmp() where applicable.
zas_
parents:
586
diff
changeset
|
137 else if (g_ascii_strncasecmp(p, "#GQview collection", strlen("#GQview collection")) == 0) |
360
822040a51249
Increase collection_load() buffer size and do not activate
zas_
parents:
359
diff
changeset
|
138 { |
822040a51249
Increase collection_load() buffer size and do not activate
zas_
parents:
359
diff
changeset
|
139 /* As 2008/04/15 there is no difference between our collection file format |
822040a51249
Increase collection_load() buffer size and do not activate
zas_
parents:
359
diff
changeset
|
140 * and GQview 2.1.5 collection file format so ignore failures as well. */ |
365 | 141 has_gqview_header = TRUE; |
361
e0295af4f716
collection_load(): replace official by limit_failures (and
zas_
parents:
360
diff
changeset
|
142 limit_failures = FALSE; |
360
822040a51249
Increase collection_load() buffer size and do not activate
zas_
parents:
359
diff
changeset
|
143 } |
365 | 144 need_header = (!has_official_header && !has_gqview_header) || !has_geometry_header; |
9 | 145 continue; |
146 } | |
147 | |
882
912726c2f5e3
Fix a segfault occuring when opening some collection files.
zas_
parents:
864
diff
changeset
|
148 if (only_geometry) continue; |
912726c2f5e3
Fix a segfault occuring when opening some collection files.
zas_
parents:
864
diff
changeset
|
149 |
363
5b8617c3821c
collection_load(): accept whitespaces at start of lines.
zas_
parents:
362
diff
changeset
|
150 /* Read filenames */ |
5b8617c3821c
collection_load(): accept whitespaces at start of lines.
zas_
parents:
362
diff
changeset
|
151 buf = quoted_value(p, NULL); |
9 | 152 if (buf) |
153 { | |
154 gint valid; | |
442 | 155 |
138 | 156 if (!flush) |
157 changed |= collect_manager_process_action(entry, &buf); | |
442 | 158 |
726 | 159 valid = (buf[0] == G_DIR_SEPARATOR && collection_add_check(cd, file_data_new_simple(buf), FALSE, TRUE)); |
506
fc9c8a3e1a8b
Handle the newline in DEBUG_N() macro instead of adding one
zas_
parents:
495
diff
changeset
|
160 if (!valid) DEBUG_1("collection invalid file: %s", buf); |
9 | 161 g_free(buf); |
162 | |
163 total++; | |
366
ce00494827e2
collection_load(): use booleans and always count failures (for debug).
zas_
parents:
365
diff
changeset
|
164 if (!valid) |
9 | 165 { |
166 fail++; | |
366
ce00494827e2
collection_load(): use booleans and always count failures (for debug).
zas_
parents:
365
diff
changeset
|
167 if (limit_failures && |
ce00494827e2
collection_load(): use booleans and always count failures (for debug).
zas_
parents:
365
diff
changeset
|
168 fail > GQ_COLLECTION_FAIL_MIN && |
283 | 169 fail * 100 / total > GQ_COLLECTION_FAIL_PERCENT) |
9 | 170 { |
673
fbebf5cf4a55
Do not use printf() directly but use new wrapper function log_printf() instead.
zas_
parents:
671
diff
changeset
|
171 log_printf("%d invalid filenames in unofficial collection file, closing: %s\n", fail, path); |
9 | 172 success = FALSE; |
173 break; | |
174 } | |
175 } | |
176 } | |
177 } | |
178 | |
506
fc9c8a3e1a8b
Handle the newline in DEBUG_N() macro instead of adding one
zas_
parents:
495
diff
changeset
|
179 DEBUG_1("collection files: total = %d fail = %d official=%d gqview=%d geometry=%d", |
442 | 180 total, fail, has_official_header, has_gqview_header, has_geometry_header); |
362 | 181 |
9 | 182 fclose(f); |
365 | 183 if (only_geometry) return has_geometry_header; |
359
96fb24f948b7
Merge collection_load_only_geometry() into collection_load().
zas_
parents:
358
diff
changeset
|
184 |
138 | 185 if (!flush) |
186 { | |
187 gchar *buf = NULL; | |
188 while (collect_manager_process_action(entry, &buf)) | |
189 { | |
190 collection_add_check(cd, file_data_new_simple(buf), FALSE, TRUE); | |
191 changed = TRUE; | |
192 g_free(buf); | |
492
dead6fb690c5
Fix adding a selection to a collection with no window open
zas_
parents:
475
diff
changeset
|
193 buf = NULL; |
138 | 194 } |
195 } | |
9 | 196 |
197 cd->list = collection_list_sort(cd->list, cd->sort_method); | |
442 | 198 |
138 | 199 if (!flush && changed && success) |
200 collection_save_private(cd, path); | |
442 | 201 |
138 | 202 if (!flush) |
203 collect_manager_entry_reset(entry); | |
442 | 204 |
9 | 205 if (!append) cd->changed = FALSE; |
206 | |
207 return success; | |
208 } | |
209 | |
358 | 210 gint collection_load(CollectionData *cd, const gchar *path, CollectionLoadFlags flags) |
9 | 211 { |
358 | 212 if (collection_load_private(cd, path, flags | COLLECTION_LOAD_FLUSH)) |
9 | 213 { |
214 layout_recent_add_path(cd->path); | |
215 return TRUE; | |
216 } | |
217 | |
218 return FALSE; | |
219 } | |
220 | |
221 static void collection_load_thumb_do(CollectionData *cd) | |
222 { | |
223 GdkPixbuf *pixbuf; | |
224 | |
225 if (!cd->thumb_loader || !g_list_find(cd->list, cd->thumb_info)) return; | |
226 | |
864 | 227 pixbuf = thumb_loader_get_pixbuf(cd->thumb_loader); |
9 | 228 collection_info_set_thumb(cd->thumb_info, pixbuf); |
229 g_object_unref(pixbuf); | |
230 | |
231 if (cd->info_updated_func) cd->info_updated_func(cd, cd->thumb_info, cd->info_updated_data); | |
232 } | |
233 | |
234 static void collection_load_thumb_error_cb(ThumbLoader *tl, gpointer data) | |
235 { | |
236 CollectionData *cd = data; | |
237 | |
238 collection_load_thumb_do(cd); | |
239 collection_load_thumb_step(cd); | |
240 } | |
241 | |
242 static void collection_load_thumb_done_cb(ThumbLoader *tl, gpointer data) | |
243 { | |
244 CollectionData *cd = data; | |
245 | |
246 collection_load_thumb_do(cd); | |
247 collection_load_thumb_step(cd); | |
248 } | |
249 | |
250 static void collection_load_thumb_step(CollectionData *cd) | |
251 { | |
252 GList *work; | |
253 CollectInfo *ci; | |
254 | |
255 if (!cd->list) | |
256 { | |
257 collection_load_stop(cd); | |
258 return; | |
259 } | |
260 | |
261 work = cd->list; | |
262 ci = work->data; | |
263 work = work->next; | |
264 /* find first unloaded thumb */ | |
265 while (work && ci->pixbuf) | |
266 { | |
267 ci = work->data; | |
268 work = work->next; | |
269 } | |
270 | |
271 if (!ci || ci->pixbuf) | |
272 { | |
273 /* done */ | |
274 collection_load_stop(cd); | |
275 | |
276 /* send a NULL CollectInfo to notify end */ | |
277 if (cd->info_updated_func) cd->info_updated_func(cd, NULL, cd->info_updated_data); | |
278 | |
279 return; | |
280 } | |
281 | |
282 /* setup loader and call it */ | |
283 cd->thumb_info = ci; | |
284 thumb_loader_free(cd->thumb_loader); | |
333 | 285 cd->thumb_loader = thumb_loader_new(options->thumbnails.max_width, options->thumbnails.max_height); |
9 | 286 thumb_loader_set_callbacks(cd->thumb_loader, |
287 collection_load_thumb_done_cb, | |
288 collection_load_thumb_error_cb, | |
289 NULL, | |
290 cd); | |
291 | |
292 /* start it */ | |
838 | 293 if (!thumb_loader_start(cd->thumb_loader, ci->fd)) |
9 | 294 { |
295 /* error, handle it, do next */ | |
506
fc9c8a3e1a8b
Handle the newline in DEBUG_N() macro instead of adding one
zas_
parents:
495
diff
changeset
|
296 DEBUG_1("error loading thumb for %s", ci->fd->path); |
9 | 297 collection_load_thumb_do(cd); |
298 collection_load_thumb_step(cd); | |
299 } | |
300 } | |
301 | |
302 void collection_load_thumb_idle(CollectionData *cd) | |
303 { | |
304 if (!cd->thumb_loader) collection_load_thumb_step(cd); | |
305 } | |
306 | |
358 | 307 gint collection_load_begin(CollectionData *cd, const gchar *path, CollectionLoadFlags flags) |
9 | 308 { |
358 | 309 if (!collection_load(cd, path, flags)) return FALSE; |
9 | 310 |
311 collection_load_thumb_idle(cd); | |
312 | |
313 return TRUE; | |
314 } | |
315 | |
316 void collection_load_stop(CollectionData *cd) | |
317 { | |
318 if (!cd->thumb_loader) return; | |
319 | |
320 thumb_loader_free(cd->thumb_loader); | |
321 cd->thumb_loader = NULL; | |
322 } | |
323 | |
324 static gint collection_save_private(CollectionData *cd, const gchar *path) | |
325 { | |
312 | 326 SecureSaveInfo *ssi; |
9 | 327 GList *work; |
328 gchar *pathl; | |
329 | |
330 if (!path && !cd->path) return FALSE; | |
331 | |
332 if (!path) | |
333 { | |
334 path = cd->path; | |
335 } | |
336 | |
337 | |
312 | 338 pathl = path_from_utf8(path); |
339 ssi = secure_open(pathl); | |
9 | 340 g_free(pathl); |
312 | 341 if (!ssi) |
342 { | |
694 | 343 log_printf(_("failed to open collection (write) \"%s\"\n"), path); |
9 | 344 return FALSE; |
345 } | |
346 | |
312 | 347 secure_fprintf(ssi, "%s collection\n", GQ_COLLECTION_MARKER); |
348 secure_fprintf(ssi, "#created with %s version %s\n", GQ_APPNAME, VERSION); | |
9 | 349 |
350 collection_update_geometry(cd); | |
351 if (cd->window_read) | |
352 { | |
312 | 353 secure_fprintf(ssi, "#geometry: %d %d %d %d\n", cd->window_x, cd->window_y, cd->window_w, cd->window_h); |
9 | 354 } |
355 | |
356 work = cd->list; | |
312 | 357 while (work && secsave_errno == SS_ERR_NONE) |
9 | 358 { |
359 CollectInfo *ci = work->data; | |
312 | 360 secure_fprintf(ssi, "\"%s\"\n", ci->fd->path); |
9 | 361 work = work->next; |
362 } | |
363 | |
312 | 364 secure_fprintf(ssi, "#end\n"); |
9 | 365 |
312 | 366 if (secure_close(ssi)) |
9 | 367 { |
694 | 368 log_printf(_("error saving collection file: %s\nerror: %s\n"), path, |
403 | 369 secsave_strerror(secsave_errno)); |
9 | 370 return FALSE; |
371 } | |
372 | |
373 if (!cd->path || strcmp(path, cd->path) != 0) | |
374 { | |
375 gchar *buf = cd->path; | |
376 cd->path = g_strdup(path); | |
377 path = cd->path; | |
378 g_free(buf); | |
379 | |
380 g_free(cd->name); | |
381 cd->name = g_strdup(filename_from_path(cd->path)); | |
382 | |
383 collection_path_changed(cd); | |
384 } | |
385 | |
386 cd->changed = FALSE; | |
387 | |
388 return TRUE; | |
389 } | |
390 | |
391 gint collection_save(CollectionData *cd, const gchar *path) | |
392 { | |
393 if (collection_save_private(cd, path)) | |
394 { | |
395 layout_recent_add_path(cd->path); | |
396 return TRUE; | |
397 } | |
398 | |
399 return FALSE; | |
400 } | |
401 | |
402 gint collection_load_only_geometry(CollectionData *cd, const gchar *path) | |
403 { | |
359
96fb24f948b7
Merge collection_load_only_geometry() into collection_load().
zas_
parents:
358
diff
changeset
|
404 return collection_load(cd, path, COLLECTION_LOAD_GEOMETRY); |
9 | 405 } |
406 | |
407 | |
408 /* | |
409 *------------------------------------------------------------------- | |
410 * collection manager | |
411 *------------------------------------------------------------------- | |
412 */ | |
413 | |
414 #define COLLECT_MANAGER_ACTIONS_PER_IDLE 1000 | |
415 #define COLLECT_MANAGER_FLUSH_DELAY 10000 | |
416 | |
417 struct _CollectManagerEntry | |
418 { | |
419 gchar *path; | |
138 | 420 GList *add_list; |
421 GHashTable *oldpath_hash; | |
422 GHashTable *newpath_hash; | |
423 gboolean empty; | |
9 | 424 }; |
425 | |
426 typedef enum { | |
427 COLLECTION_MANAGER_UPDATE, | |
428 COLLECTION_MANAGER_ADD, | |
429 COLLECTION_MANAGER_REMOVE | |
430 } CollectManagerType; | |
431 | |
432 typedef struct _CollectManagerAction CollectManagerAction; | |
433 struct _CollectManagerAction | |
434 { | |
435 gchar *oldpath; | |
436 gchar *newpath; | |
437 | |
438 CollectManagerType type; | |
439 | |
440 gint ref; | |
441 }; | |
442 | |
443 | |
444 static GList *collection_manager_entry_list = NULL; | |
445 static GList *collection_manager_action_list = NULL; | |
446 static GList *collection_manager_action_tail = NULL; | |
447 static gint collection_manager_timer_id = -1; | |
448 | |
449 | |
450 static CollectManagerAction *collect_manager_action_new(const gchar *oldpath, const gchar *newpath, | |
451 CollectManagerType type) | |
452 { | |
453 CollectManagerAction *action; | |
454 | |
455 action = g_new0(CollectManagerAction, 1); | |
456 action->ref = 1; | |
457 | |
458 action->oldpath = g_strdup(oldpath); | |
459 action->newpath = g_strdup(newpath); | |
460 | |
461 action->type = type; | |
462 | |
463 return action; | |
464 } | |
465 | |
466 static void collect_manager_action_ref(CollectManagerAction *action) | |
467 { | |
468 action->ref++; | |
469 } | |
470 | |
471 static void collect_manager_action_unref(CollectManagerAction *action) | |
472 { | |
473 action->ref--; | |
474 | |
475 if (action->ref > 0) return; | |
476 | |
477 g_free(action->oldpath); | |
478 g_free(action->newpath); | |
479 g_free(action); | |
480 } | |
481 | |
138 | 482 static void collect_manager_entry_free_data(CollectManagerEntry *entry) |
9 | 483 { |
484 GList *work; | |
485 | |
138 | 486 work = entry->add_list; |
9 | 487 while (work) |
488 { | |
489 CollectManagerAction *action; | |
490 | |
491 action = work->data; | |
492 work = work->next; | |
493 | |
494 collect_manager_action_unref(action); | |
495 } | |
138 | 496 g_list_free(entry->add_list); |
497 g_hash_table_destroy(entry->oldpath_hash); | |
498 g_hash_table_destroy(entry->newpath_hash); | |
499 } | |
500 | |
501 static void collect_manager_entry_init_data(CollectManagerEntry *entry) | |
502 { | |
503 entry->add_list = NULL; | |
504 entry->oldpath_hash = g_hash_table_new_full(g_str_hash, g_str_equal, NULL, (GDestroyNotify) collect_manager_action_unref); | |
505 entry->newpath_hash = g_hash_table_new(g_str_hash, g_str_equal); | |
506 entry->empty = TRUE; | |
507 | |
508 } | |
509 | |
510 static CollectManagerEntry *collect_manager_entry_new(const gchar *path) | |
511 { | |
512 CollectManagerEntry *entry; | |
513 | |
514 entry = g_new0(CollectManagerEntry, 1); | |
515 entry->path = g_strdup(path); | |
516 collect_manager_entry_init_data(entry); | |
517 | |
518 collection_manager_entry_list = g_list_append(collection_manager_entry_list, entry); | |
519 | |
520 return entry; | |
521 } | |
522 | |
523 | |
524 static void collect_manager_entry_free(CollectManagerEntry *entry) | |
525 { | |
526 collection_manager_entry_list = g_list_remove(collection_manager_entry_list, entry); | |
527 | |
528 collect_manager_entry_free_data(entry); | |
9 | 529 |
530 g_free(entry->path); | |
531 g_free(entry); | |
532 } | |
533 | |
138 | 534 static void collect_manager_entry_reset(CollectManagerEntry *entry) |
535 { | |
536 collect_manager_entry_free_data(entry); | |
537 collect_manager_entry_init_data(entry); | |
538 } | |
539 | |
540 static CollectManagerEntry *collect_manager_get_entry(const gchar *path) | |
541 { | |
542 GList *work; | |
543 | |
544 work = collection_manager_entry_list; | |
545 while (work) | |
546 { | |
547 CollectManagerEntry *entry; | |
548 | |
549 entry = work->data; | |
550 work = work->next; | |
442 | 551 if (strcmp(entry->path, path) == 0) |
138 | 552 { |
553 return entry; | |
554 } | |
555 } | |
556 return NULL; | |
557 | |
558 } | |
559 | |
560 static void collect_manager_entry_add_action(CollectManagerEntry *entry, CollectManagerAction *action) | |
561 { | |
562 | |
563 CollectManagerAction *orig_action; | |
442 | 564 |
565 entry->empty = FALSE; | |
566 | |
138 | 567 if (action->oldpath == NULL) |
568 { | |
569 /* add file */ | |
570 if (action->newpath == NULL) | |
571 { | |
572 return; | |
573 } | |
442 | 574 |
138 | 575 orig_action = g_hash_table_lookup(entry->newpath_hash, action->newpath); |
576 if (orig_action) | |
577 { | |
578 /* target already exists */ | |
673
fbebf5cf4a55
Do not use printf() directly but use new wrapper function log_printf() instead.
zas_
parents:
671
diff
changeset
|
579 log_printf("collection manager failed to add another action for target %s in collection %s\n", |
138 | 580 action->newpath, entry->path); |
581 return; | |
582 } | |
583 entry->add_list = g_list_append(entry->add_list, action); | |
584 g_hash_table_insert(entry->newpath_hash, action->newpath, action); | |
585 collect_manager_action_ref(action); | |
586 return; | |
587 } | |
588 | |
589 orig_action = g_hash_table_lookup(entry->newpath_hash, action->oldpath); | |
590 if (orig_action) | |
591 { | |
592 /* new action with the same file */ | |
593 CollectManagerAction *new_action = collect_manager_action_new(orig_action->oldpath, action->newpath, action->type); | |
442 | 594 |
138 | 595 if (new_action->oldpath) |
596 { | |
597 g_hash_table_steal(entry->oldpath_hash, orig_action->oldpath); | |
598 g_hash_table_insert(entry->oldpath_hash, new_action->oldpath, new_action); | |
599 } | |
600 else | |
601 { | |
602 GList *work = g_list_find(entry->add_list, orig_action); | |
603 work->data = new_action; | |
604 } | |
442 | 605 |
138 | 606 g_hash_table_steal(entry->newpath_hash, orig_action->newpath); |
442 | 607 if (new_action->newpath) |
138 | 608 { |
442 | 609 g_hash_table_insert(entry->newpath_hash, new_action->newpath, new_action); |
138 | 610 } |
611 collect_manager_action_unref(orig_action); | |
612 return; | |
613 } | |
614 | |
615 | |
616 orig_action = g_hash_table_lookup(entry->oldpath_hash, action->oldpath); | |
617 if (orig_action) | |
618 { | |
619 /* another action for the same source, ignore */ | |
673
fbebf5cf4a55
Do not use printf() directly but use new wrapper function log_printf() instead.
zas_
parents:
671
diff
changeset
|
620 log_printf("collection manager failed to add another action for source %s in collection %s\n", |
138 | 621 action->oldpath, entry->path); |
622 return; | |
623 } | |
442 | 624 |
138 | 625 g_hash_table_insert(entry->oldpath_hash, action->oldpath, action); |
626 if (action->newpath) | |
627 { | |
442 | 628 g_hash_table_insert(entry->newpath_hash, action->newpath, action); |
138 | 629 } |
630 collect_manager_action_ref(action); | |
631 } | |
632 | |
235 | 633 static gint collect_manager_process_action(CollectManagerEntry *entry, gchar **path_ptr) |
138 | 634 { |
635 gchar *path = *path_ptr; | |
636 CollectManagerAction *action; | |
442 | 637 |
138 | 638 if (path == NULL) |
639 { | |
640 /* get new files */ | |
641 if (entry->add_list) | |
642 { | |
643 action = entry->add_list->data; | |
644 g_assert(action->oldpath == NULL); | |
645 entry->add_list = g_list_remove(entry->add_list, action); | |
646 path = g_strdup(action->newpath); | |
647 g_hash_table_remove(entry->newpath_hash, path); | |
648 collect_manager_action_unref(action); | |
649 } | |
650 *path_ptr = path; | |
651 return (path != NULL); | |
652 } | |
442 | 653 |
138 | 654 action = g_hash_table_lookup(entry->oldpath_hash, path); |
442 | 655 |
138 | 656 if (action) |
657 { | |
658 g_free(path); | |
659 path = g_strdup(action->newpath); | |
660 *path_ptr = path; | |
661 return TRUE; | |
662 } | |
663 | |
664 return FALSE; /* no change */ | |
665 } | |
666 | |
9 | 667 static void collect_manager_refresh(void) |
668 { | |
780
44128da39e13
Drop initialization to NULL since filelist_read() will take care of it.
zas_
parents:
726
diff
changeset
|
669 GList *list; |
9 | 670 GList *work; |
783 | 671 FileData *dir_fd; |
9 | 672 |
1145
3a7af6a8cd5f
Use functions to return directories instead of constants.
zas_
parents:
1055
diff
changeset
|
673 dir_fd = file_data_new_simple(get_collections_dir()); |
783 | 674 filelist_read(dir_fd, &list, NULL); |
675 file_data_unref(dir_fd); | |
9 | 676 |
677 work = collection_manager_entry_list; | |
678 while (work && list) | |
679 { | |
680 CollectManagerEntry *entry; | |
681 GList *list_step; | |
682 | |
683 entry = work->data; | |
684 work = work->next; | |
685 | |
686 list_step = list; | |
687 while (list_step && entry) | |
688 { | |
576
9dc0513837b5
dropped path_list functions, use filelist functions everywhere
nadvornik
parents:
507
diff
changeset
|
689 FileData *fd; |
9 | 690 |
576
9dc0513837b5
dropped path_list functions, use filelist functions everywhere
nadvornik
parents:
507
diff
changeset
|
691 fd = list_step->data; |
9 | 692 list_step = list_step->next; |
693 | |
576
9dc0513837b5
dropped path_list functions, use filelist functions everywhere
nadvornik
parents:
507
diff
changeset
|
694 if (strcmp(fd->path, entry->path) == 0) |
9 | 695 { |
576
9dc0513837b5
dropped path_list functions, use filelist functions everywhere
nadvornik
parents:
507
diff
changeset
|
696 list = g_list_remove(list, fd); |
9dc0513837b5
dropped path_list functions, use filelist functions everywhere
nadvornik
parents:
507
diff
changeset
|
697 file_data_unref(fd); |
9 | 698 |
699 entry = NULL; | |
700 } | |
701 else | |
702 { | |
703 collect_manager_entry_free(entry); | |
704 } | |
705 } | |
706 } | |
707 | |
708 work = list; | |
709 while (work) | |
710 { | |
576
9dc0513837b5
dropped path_list functions, use filelist functions everywhere
nadvornik
parents:
507
diff
changeset
|
711 FileData *fd; |
9 | 712 |
576
9dc0513837b5
dropped path_list functions, use filelist functions everywhere
nadvornik
parents:
507
diff
changeset
|
713 fd = work->data; |
9 | 714 work = work->next; |
715 | |
576
9dc0513837b5
dropped path_list functions, use filelist functions everywhere
nadvornik
parents:
507
diff
changeset
|
716 collect_manager_entry_new(fd->path); |
9 | 717 } |
718 | |
576
9dc0513837b5
dropped path_list functions, use filelist functions everywhere
nadvornik
parents:
507
diff
changeset
|
719 filelist_free(list); |
9 | 720 } |
721 | |
722 static void collect_manager_process_actions(gint max) | |
723 { | |
506
fc9c8a3e1a8b
Handle the newline in DEBUG_N() macro instead of adding one
zas_
parents:
495
diff
changeset
|
724 if (collection_manager_action_list) DEBUG_1("collection manager processing actions"); |
495 | 725 |
9 | 726 while (collection_manager_action_list != NULL && max > 0) |
727 { | |
728 CollectManagerAction *action; | |
729 GList *work; | |
730 | |
731 action = collection_manager_action_list->data; | |
732 work = collection_manager_entry_list; | |
733 while (work) | |
734 { | |
735 CollectManagerEntry *entry; | |
736 | |
737 entry = work->data; | |
738 work = work->next; | |
739 | |
740 if (action->type == COLLECTION_MANAGER_UPDATE) | |
741 { | |
138 | 742 collect_manager_entry_add_action(entry, action); |
9 | 743 } |
744 else if (action->oldpath && action->newpath && | |
745 strcmp(action->newpath, entry->path) == 0) | |
746 { | |
747 /* convert action to standard add format */ | |
748 g_free(action->newpath); | |
749 if (action->type == COLLECTION_MANAGER_ADD) | |
750 { | |
751 action->newpath = action->oldpath; | |
752 action->oldpath = NULL; | |
753 } | |
754 else if (action->type == COLLECTION_MANAGER_REMOVE) | |
755 { | |
756 action->newpath = NULL; | |
757 } | |
138 | 758 collect_manager_entry_add_action(entry, action); |
9 | 759 } |
760 | |
761 max--; | |
762 } | |
763 | |
764 if (action->type != COLLECTION_MANAGER_UPDATE && | |
765 action->oldpath && action->newpath) | |
766 { | |
673
fbebf5cf4a55
Do not use printf() directly but use new wrapper function log_printf() instead.
zas_
parents:
671
diff
changeset
|
767 log_printf("collection manager failed to %s %s for collection %s\n", |
9 | 768 (action->type == COLLECTION_MANAGER_ADD) ? "add" : "remove", |
769 action->oldpath, action->newpath); | |
770 } | |
771 | |
772 if (collection_manager_action_tail == collection_manager_action_list) | |
773 { | |
774 collection_manager_action_tail = NULL; | |
775 } | |
776 collection_manager_action_list = g_list_remove(collection_manager_action_list, action); | |
777 collect_manager_action_unref(action); | |
778 } | |
779 } | |
780 | |
781 static gint collect_manager_process_entry(CollectManagerEntry *entry) | |
782 { | |
783 CollectionData *cd; | |
784 gint success; | |
785 | |
138 | 786 if (entry->empty) return FALSE; |
9 | 787 |
788 cd = collection_new(entry->path); | |
358 | 789 success = collection_load_private(cd, entry->path, COLLECTION_LOAD_NONE); |
9 | 790 |
791 collection_unref(cd); | |
792 | |
793 return TRUE; | |
794 } | |
795 | |
796 static gint collect_manager_process_entry_list(void) | |
797 { | |
798 GList *work; | |
799 | |
800 work = collection_manager_entry_list; | |
801 while (work) | |
802 { | |
803 CollectManagerEntry *entry; | |
804 | |
805 entry = work->data; | |
806 work = work->next; | |
807 if (collect_manager_process_entry(entry)) return TRUE; | |
808 } | |
809 | |
810 return FALSE; | |
811 } | |
812 | |
138 | 813 |
814 | |
9 | 815 static gint collect_manager_process_cb(gpointer data) |
816 { | |
817 if (collection_manager_action_list) collect_manager_refresh(); | |
818 collect_manager_process_actions(COLLECT_MANAGER_ACTIONS_PER_IDLE); | |
819 if (collection_manager_action_list) return TRUE; | |
820 | |
821 if (collect_manager_process_entry_list()) return TRUE; | |
822 | |
506
fc9c8a3e1a8b
Handle the newline in DEBUG_N() macro instead of adding one
zas_
parents:
495
diff
changeset
|
823 DEBUG_1("collection manager is up to date"); |
9 | 824 return FALSE; |
825 } | |
826 | |
827 static gint collect_manager_timer_cb(gpointer data) | |
828 { | |
506
fc9c8a3e1a8b
Handle the newline in DEBUG_N() macro instead of adding one
zas_
parents:
495
diff
changeset
|
829 DEBUG_1("collection manager timer expired"); |
9 | 830 |
831 g_idle_add_full(G_PRIORITY_LOW, collect_manager_process_cb, NULL, NULL); | |
832 | |
833 collection_manager_timer_id = -1; | |
834 return FALSE; | |
835 } | |
836 | |
837 static void collect_manager_timer_push(gint stop) | |
838 { | |
839 if (collection_manager_timer_id != -1) | |
840 { | |
841 if (!stop) return; | |
842 | |
843 g_source_remove(collection_manager_timer_id); | |
844 collection_manager_timer_id = -1; | |
845 } | |
846 | |
847 if (!stop) | |
848 { | |
849 collection_manager_timer_id = g_timeout_add(COLLECT_MANAGER_FLUSH_DELAY, | |
850 collect_manager_timer_cb, NULL); | |
506
fc9c8a3e1a8b
Handle the newline in DEBUG_N() macro instead of adding one
zas_
parents:
495
diff
changeset
|
851 DEBUG_1("collection manager timer started"); |
9 | 852 } |
853 } | |
854 | |
855 static void collect_manager_add_action(CollectManagerAction *action) | |
856 { | |
857 if (!action) return; | |
858 | |
859 /* we keep track of the list's tail to keep this a n(1) operation */ | |
860 | |
827 | 861 if (collection_manager_action_tail) |
862 { | |
863 collection_manager_action_tail = g_list_append(collection_manager_action_tail, action); | |
864 collection_manager_action_tail = collection_manager_action_tail->next; | |
865 } | |
866 else | |
867 { | |
868 collection_manager_action_list = g_list_append(collection_manager_action_list, action); | |
869 collection_manager_action_tail = collection_manager_action_list; | |
870 } | |
9 | 871 |
872 collect_manager_timer_push(FALSE); | |
873 } | |
874 | |
138 | 875 void collect_manager_moved(FileData *fd) |
9 | 876 { |
877 CollectManagerAction *action; | |
138 | 878 const gchar *oldpath = fd->change->source; |
879 const gchar *newpath = fd->change->dest; | |
9 | 880 |
881 action = collect_manager_action_new(oldpath, newpath, COLLECTION_MANAGER_UPDATE); | |
882 collect_manager_add_action(action); | |
883 } | |
884 | |
138 | 885 void collect_manager_add(FileData *fd, const gchar *collection) |
9 | 886 { |
887 CollectManagerAction *action; | |
888 CollectWindow *cw; | |
889 | |
138 | 890 if (!fd || !collection) return; |
9 | 891 |
892 cw = collection_window_find_by_path(collection); | |
893 if (cw) | |
894 { | |
799 | 895 if (collection_list_find_fd(cw->cd->list, fd) == NULL) |
9 | 896 { |
138 | 897 collection_add(cw->cd, fd, FALSE); |
9 | 898 } |
899 return; | |
900 } | |
901 | |
138 | 902 action = collect_manager_action_new(fd->path, collection, COLLECTION_MANAGER_ADD); |
9 | 903 collect_manager_add_action(action); |
904 } | |
905 | |
138 | 906 void collect_manager_remove(FileData *fd, const gchar *collection) |
9 | 907 { |
908 CollectManagerAction *action; | |
909 CollectWindow *cw; | |
910 | |
138 | 911 if (!fd || !collection) return; |
9 | 912 |
913 cw = collection_window_find_by_path(collection); | |
914 if (cw) | |
915 { | |
138 | 916 while (collection_remove(cw->cd, fd)); |
9 | 917 return; |
918 } | |
919 | |
138 | 920 action = collect_manager_action_new(fd->path, collection, COLLECTION_MANAGER_REMOVE); |
9 | 921 collect_manager_add_action(action); |
922 } | |
923 | |
924 void collect_manager_flush(void) | |
925 { | |
926 collect_manager_timer_push(TRUE); | |
927 | |
506
fc9c8a3e1a8b
Handle the newline in DEBUG_N() macro instead of adding one
zas_
parents:
495
diff
changeset
|
928 DEBUG_1("collection manager flushing"); |
9 | 929 while (collect_manager_process_cb(NULL)); |
930 } | |
799 | 931 |
932 void collect_manager_notify_cb(FileData *fd, NotifyType type, gpointer data) | |
933 { | |
934 | |
803 | 935 if (type != NOTIFY_TYPE_CHANGE || !fd->change) return; |
799 | 936 |
937 switch(fd->change->type) | |
938 { | |
939 case FILEDATA_CHANGE_MOVE: | |
940 collect_manager_moved(fd); | |
941 break; | |
942 case FILEDATA_CHANGE_COPY: | |
943 break; | |
944 case FILEDATA_CHANGE_RENAME: | |
945 collect_manager_moved(fd); | |
946 break; | |
947 case FILEDATA_CHANGE_DELETE: | |
948 case FILEDATA_CHANGE_UNSPECIFIED: | |
1211 | 949 case FILEDATA_CHANGE_WRITE_METADATA: |
799 | 950 break; |
951 } | |
952 | |
953 } | |
1055
1646720364cf
Adding a vim modeline to all files - patch by Klaus Ethgen
nadvornik
parents:
1023
diff
changeset
|
954 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */ |