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