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