MetadataWriter.vala 26.7 KB
Newer Older
1
/* Copyright 2016 Software Freedom Conservancy Inc.
2 3
 *
 * This software is licensed under the GNU Lesser General Public License
4
 * (version 2.1 or later).  See the COPYING file in this distribution.
5 6
 */

7 8 9 10 11 12 13
// MetadataWriter tracks LibraryPhotos for alterations to their metadata and commits those changes
// in a timely manner to their backing files.  Because only the MetadataWriter knows when the
// metadata has been properly committed, it is also responsible for updating the metadata-dirty
// flag in Photo.  Thus, MetadataWriter should *always* be running, even if the user has turned off
// the feature, so if they turn it on MetadataWriter can properly go out and update the backing
// files.

14 15 16 17
public class MetadataWriter : Object {
    public const uint COMMIT_DELAY_MSEC = 3000;
    public const uint COMMIT_SPACING_MSEC = 50;
    
18
    private const string[] INTERESTED_PHOTO_METADATA_DETAILS = { "name", "comment", "rating", "exposure-time", "gps" };
19 20 21 22 23 24 25
    
    private class CommitJob : BackgroundJob {
        public LibraryPhoto photo;
        public Gee.Set<string>? current_keywords;
        public Photo.ReimportMasterState reimport_master_state = null;
        public Photo.ReimportEditableState reimport_editable_state = null;
        public Error? err = null;
26 27
        public bool wrote_master = false;
        public bool wrote_editable = false;
28 29
        
        public CommitJob(MetadataWriter owner, LibraryPhoto photo, Gee.Set<string>? keywords) {
30
            base (owner, owner.on_update_completed, new Cancellable(), owner.on_update_cancelled);
31

32 33 34
            this.photo = photo;
            current_keywords = keywords;
        }
35

36 37 38 39 40 41 42 43 44 45
        public override void execute() {
            try {
                commit_master();
                commit_editable();
            } catch (Error err) {
                this.err = err;
            }
        }
        
        private void commit_master() throws Error {
46 47 48 49 50
            // If we have an editable, any orientation changes should be written only to it;
            // otherwise, we'll end up ruining the original, and as such, breaking the
            // ability to revert to it.
            bool skip_orientation = photo.has_editable();
            
51 52 53 54
            if (!photo.get_master_file_format().can_write_metadata())
                return;
            
            PhotoMetadata metadata = photo.get_master_metadata();
55
            if (update_metadata(metadata, skip_orientation)) {
56
                LibraryMonitor.blacklist_file(photo.get_master_file(), "MetadataWriter.commit_master");
57 58 59 60 61 62
                try {
                    photo.persist_master_metadata(metadata, out reimport_master_state);
                } finally {
                    LibraryMonitor.unblacklist_file(photo.get_master_file());
                }
            }
63 64
            
            wrote_master = true;
65 66 67 68 69 70 71 72 73
        }
        
        private void commit_editable() throws Error {
            if (!photo.has_editable() || !photo.get_editable_file_format().can_write_metadata())
                return;
            
            PhotoMetadata? metadata = photo.get_editable_metadata();
            assert(metadata != null);
            
74
            if (update_metadata(metadata)) {
75
                LibraryMonitor.blacklist_file(photo.get_editable_file(), "MetadataWriter.commit_editable");
76 77 78 79 80 81
                try {
                    photo.persist_editable_metadata(metadata, out reimport_editable_state);
                } finally {
                    LibraryMonitor.unblacklist_file(photo.get_editable_file());
                }
            }
82 83
            
            wrote_editable = true;
84 85
        }
        
86
        private bool update_metadata(PhotoMetadata metadata, bool skip_orientation = false) {
87 88 89 90 91 92 93 94 95
            bool changed = false;
            
            // title (caption)
            string? current_title = photo.get_title();
            if (current_title != metadata.get_title()) {
                metadata.set_title(current_title);
                changed = true;
            }
            
96 97 98 99 100 101 102
            // comment
            string? current_comment = photo.get_comment();
            if (current_comment != metadata.get_comment()) {
                metadata.set_comment(current_comment);
                changed = true;
            }
            
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
            // rating
            Rating current_rating = photo.get_rating();
            if (current_rating != metadata.get_rating()) {
                metadata.set_rating(current_rating);
                changed = true;
            }
            
            // exposure date/time
            time_t current_exposure_time = photo.get_exposure_time();
            time_t metadata_exposure_time = 0;
            MetadataDateTime? metadata_exposure_date_time = metadata.get_exposure_date_time();
            if (metadata_exposure_date_time != null)
                metadata_exposure_time = metadata_exposure_date_time.get_timestamp();
            if (current_exposure_time != metadata_exposure_time) {
                metadata.set_exposure_date_time(current_exposure_time != 0
                    ? new MetadataDateTime(current_exposure_time)
                    : null);
                changed = true;
            }
122

123 124 125 126 127 128 129 130
            // gps location
            GpsCoords current_gps_coords = photo.get_gps_coords();
            GpsCoords metadata_gps_coords = metadata.get_gps_coords();
            if (!current_gps_coords.equals(ref metadata_gps_coords)) {
                metadata.set_gps_coords(current_gps_coords);
                changed = true;
            }

131
            // tags (keywords) ... replace (or clear) entirely rather than union or intersection
132 133 134 135 136 137 138 139 140
            Gee.Set<string> safe_keywords = new Gee.HashSet<string>();

            // Since the tags are stored in an image file's `keywords' field in
            // non-hierarchical format, before checking whether the tags that
            // should be associated with this image have been written, we'll need
            // to produce non-hierarchical versions of the tags to be tested.
            // get_user_visible_name() does this by returning the most deeply-nested
            // portion of a given hierarchical tag; that is, for a tag "/a/b/c",
            // it'll return "c", which is exactly the form we want here.
141 142 143 144 145
            if (current_keywords != null) {
                foreach(string tmp in current_keywords) {
                    Tag tag = Tag.for_path(tmp);
                    safe_keywords.add(tag.get_user_visible_name());
                }
146 147 148
            }

            if (!equal_sets(safe_keywords, metadata.get_keywords())) {
149 150 151
                metadata.set_keywords(current_keywords);
                changed = true;
            }
152

153
            // orientation
154 155 156 157 158 159
            if (!skip_orientation) {
                Orientation current_orientation = photo.get_orientation();
                if (current_orientation != metadata.get_orientation()) {
                    metadata.set_orientation(current_orientation);
                    changed = true;
                }
160
            }
161

162
            // add the software name/version only if updating the metadata in the file
163 164 165
            if (changed)
                metadata.set_software(Resources.APP_TITLE, Resources.APP_VERSION);
            
166 167 168 169 170 171
            return changed;
        }
    }
    
    private static MetadataWriter instance = null;
    
172
    private Workers workers = new Workers(1, false);
173
    private bool enabled = false;
174
    private HashTimedQueue<LibraryPhoto> dirty;
175
    private Gee.HashMap<LibraryPhoto, CommitJob> pending = new Gee.HashMap<LibraryPhoto, CommitJob>();
176
    private Gee.HashSet<CommitJob> pending_cancel = new Gee.HashSet<CommitJob>();
177 178 179 180 181
    private Gee.HashSet<string> interested_photo_details = new Gee.HashSet<string>();
    private LibraryPhoto? ignore_photo_alteration = null;
    private uint outstanding_total = 0;
    private uint outstanding_completed = 0;
    private bool closed = false;
182
    private int pause_count = 0;
183
    private Gee.HashSet<LibraryPhoto> importing_photos = new Gee.HashSet<LibraryPhoto>();
184 185 186 187
    
    public signal void progress(uint completed, uint total);
    
    private MetadataWriter() {
188
        dirty = new HashTimedQueue<LibraryPhoto>(COMMIT_DELAY_MSEC, on_photo_dequeued);
189 190
        dirty.set_dequeue_spacing_msec(COMMIT_SPACING_MSEC);
        
191 192 193 194 195
        // start with the writer paused, waiting for the LibraryMonitor initial discovery to
        // complete (note that if the LibraryMonitor is ever disabled, the MetadataWriter will not
        // start on its own)
        pause();
        
196 197 198
        // convert all interested metadata Alteration details into lookup hash
        foreach (string detail in INTERESTED_PHOTO_METADATA_DETAILS)
            interested_photo_details.add(detail);
199

Lucas Beeler's avatar
Lucas Beeler committed
200 201 202
        // sync up with the configuration system
        enabled = Config.Facade.get_instance().get_commit_metadata_to_masters();
        Config.Facade.get_instance().commit_metadata_to_masters_changed.connect(on_config_changed);
203
        
204
        // add all current photos to look for ones that are dirty and need updating
205
        force_rescan();
206
        
207 208
        LibraryPhoto.global.media_import_starting.connect(on_importing_photos);
        LibraryPhoto.global.media_import_completed.connect(on_photos_imported);
209 210
        LibraryPhoto.global.contents_altered.connect(on_photos_added_removed);
        LibraryPhoto.global.items_altered.connect(on_photos_altered);
211 212
        LibraryPhoto.global.frozen.connect(on_collection_frozen);
        LibraryPhoto.global.thawed.connect(on_collection_thawed);
213
        LibraryPhoto.global.items_destroyed.connect(on_photos_destroyed);
214 215
        
        Tag.global.items_altered.connect(on_tags_altered);
216 217
        Tag.global.container_contents_altered.connect(on_tag_contents_altered);
        Tag.global.backlink_to_container_removed.connect(on_tag_backlink_removed);
218 219
        Tag.global.frozen.connect(on_collection_frozen);
        Tag.global.thawed.connect(on_collection_thawed);
220 221
        
        Application.get_instance().exiting.connect(on_application_exiting);
222
        
223 224
        LibraryMonitorPool.get_instance().monitor_installed.connect(on_monitor_installed);
        LibraryMonitorPool.get_instance().monitor_destroyed.connect(on_monitor_destroyed);
225 226 227
    }
    
    ~MetadataWriter() {
Lucas Beeler's avatar
Lucas Beeler committed
228
        Config.Facade.get_instance().commit_metadata_to_masters_changed.disconnect(on_config_changed);
229
        
230 231
        LibraryPhoto.global.media_import_starting.disconnect(on_importing_photos);
        LibraryPhoto.global.media_import_completed.disconnect(on_photos_imported);
232 233
        LibraryPhoto.global.contents_altered.disconnect(on_photos_added_removed);
        LibraryPhoto.global.items_altered.disconnect(on_photos_altered);
234 235
        LibraryPhoto.global.frozen.disconnect(on_collection_frozen);
        LibraryPhoto.global.thawed.disconnect(on_collection_thawed);
236
        LibraryPhoto.global.items_destroyed.disconnect(on_photos_destroyed);
237 238
        
        Tag.global.items_altered.disconnect(on_tags_altered);
239 240
        Tag.global.container_contents_altered.disconnect(on_tag_contents_altered);
        Tag.global.backlink_to_container_removed.disconnect(on_tag_backlink_removed);
241 242
        Tag.global.frozen.disconnect(on_collection_frozen);
        Tag.global.thawed.disconnect(on_collection_thawed);
243 244
        
        Application.get_instance().exiting.disconnect(on_application_exiting);
245
        
246 247
        LibraryMonitorPool.get_instance().monitor_installed.disconnect(on_monitor_installed);
        LibraryMonitorPool.get_instance().monitor_destroyed.disconnect(on_monitor_destroyed);
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
    }
    
    public static void init() {
        instance = new MetadataWriter();
    }
    
    public static void terminate() {
        if (instance != null)
            instance.close();
        
        instance = null;
    }
    
    public static MetadataWriter get_instance() {
        return instance;
    }
    
265 266
    // This will examine all photos for dirty metadata and schedule commits if enabled.
    public void force_rescan() {
267
        schedule_if_dirty((Gee.Collection<LibraryPhoto>) LibraryPhoto.global.get_all(), "force rescan");
268 269
    }
    
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
    public void pause() {
        if (pause_count++ != 0)
            return;
        
        dirty.pause();
        
        progress(0, 0);
    }
    
    public void unpause() {
        if (pause_count == 0 || --pause_count != 0)
            return;
        
        dirty.unpause();
    }
    
286 287 288 289
    public void close() {
        if (closed)
            return;
        
290
        cancel_all(true);
291
        
292 293 294
        closed = true;
    }
    
Lucas Beeler's avatar
Lucas Beeler committed
295 296
    private void on_config_changed() {
        bool value = Config.Facade.get_instance().get_commit_metadata_to_masters();
297
        
298 299
        if (enabled == value)
            return;
300
        
301 302 303 304 305
        enabled = value;
        if (enabled)
            force_rescan();
        else
            cancel_all(false);
306 307 308 309 310 311
    }
    
    private void on_application_exiting() {
        close();
    }
    
312 313 314 315 316 317 318 319
    private void on_monitor_installed(LibraryMonitor monitor) {
        monitor.discovery_completed.connect(on_discovery_completed);
    }

    private void on_monitor_destroyed(LibraryMonitor monitor) {
        monitor.discovery_completed.disconnect(on_discovery_completed);
    }
    
320 321 322 323 324 325 326 327 328 329 330 331
    private void on_discovery_completed() {
        unpause();
    }
    
    private void on_collection_frozen() {
        pause();
    }
    
    private void on_collection_thawed() {
        unpause();
    }
    
332
    private void on_importing_photos(Gee.Collection<MediaSource> media_sources) {
333
        importing_photos.add_all((Gee.Collection<LibraryPhoto>) media_sources);
334 335 336
    }
    
    private void on_photos_imported(Gee.Collection<MediaSource> media_sources) {
337
        importing_photos.remove_all((Gee.Collection<LibraryPhoto>) media_sources);
338 339
    }
    
340 341
    private void on_photos_added_removed(Gee.Iterable<DataObject>? added,
        Gee.Iterable<DataObject>? removed) {
342 343
        // no reason to go through this exercise if auto-commit is disabled
        if (added != null && enabled)
344
            schedule_if_dirty((Gee.Iterable<LibraryPhoto>) added, "added to LibraryPhoto.global");
345
        
346
        // want to cancel jobs no matter what, however
347
        if (removed != null) {
348
            bool cancelled = false;
349
            foreach (DataObject object in removed)
350 351 352 353
                cancelled = cancel_job((LibraryPhoto) object) || cancelled;
            
            if (cancelled)
                progress(outstanding_completed, outstanding_total);
354 355 356 357 358 359 360 361 362 363 364 365 366
        }
    }
    
    private void on_photos_altered(Gee.Map<DataObject, Alteration> items) {
        Gee.HashSet<LibraryPhoto> photos = null;
        foreach (DataObject object in items.keys) {
            LibraryPhoto photo = (LibraryPhoto) object;
            
            // ignore this signal on this photo (means it's coming up from completing the metadata
            // update)
            if (photo == ignore_photo_alteration)
                continue;
            
367 368 369 370 371 372 373 374 375 376 377 378 379 380
            Alteration alteration = items.get(object);
            
            // if an image:orientation detail, write that out
            if (alteration.has_detail("image", "orientation")) {
                if (photos == null)
                    photos = new Gee.HashSet<LibraryPhoto>();
                
                photos.add(photo);
                
                continue;
            }
            
            // get all "metadata" details for this alteration
            Gee.Collection<string>? details = alteration.get_details("metadata");
381 382 383 384 385 386 387 388 389
            if (details == null)
                continue;
            
            // only enqueue an update if an alteration of metadata actually written out occurs
            foreach (string detail in details) {
                if (interested_photo_details.contains(detail)) {
                    if (photos == null)
                        photos = new Gee.HashSet<LibraryPhoto>();
                    
390
                    photos.add(photo);
391 392 393 394 395 396 397
                    
                    break;
                }
            }
        }
        
        if (photos != null)
398 399 400 401 402 403 404 405 406
            photos_are_dirty(photos, "alteration", false);
    }
    
    private void on_photos_destroyed(Gee.Collection<DataSource> destroyed) {
        foreach (DataSource source in destroyed) {
            LibraryPhoto photo = (LibraryPhoto) source;
            cancel_job(photo);
            importing_photos.remove(photo);
        }
407 408
    }
    
409
    private void on_tags_altered(Gee.Map<DataObject, Alteration> map) {
410
        Gee.HashSet<LibraryPhoto>? photos = null;
411 412 413 414 415
        foreach (DataObject object in map.keys) {
            if (!map.get(object).has_detail("metadata", "name"))
                continue;
            
            if (photos == null)
416
                photos = new Gee.HashSet<LibraryPhoto>();
417 418 419 420 421 422 423 424 425
            
            foreach (MediaSource media in ((Tag) object).get_sources()) {
                LibraryPhoto? photo = media as LibraryPhoto;
                if (photo != null)
                    photos.add(photo);
            }
        }
        
        if (photos != null)
426
            photos_are_dirty(photos, "tag renamed", false);
427 428
    }
    
429
    private void on_tag_contents_altered(ContainerSource container, Gee.Collection<DataSource>? added,
430
        bool relinking, Gee.Collection<DataSource>? removed, bool unlinking) {
431 432
        Tag tag = (Tag) container;
        
433
        if (added != null && !relinking) {
434 435
            Gee.ArrayList<LibraryPhoto> added_photos = new Gee.ArrayList<LibraryPhoto>();
            foreach (DataSource source in added) {
436
                LibraryPhoto? photo =  source as LibraryPhoto;
437
                if (photo != null && !importing_photos.contains(photo))
438 439 440
                    added_photos.add(photo);
            }
            
441
            photos_are_dirty(added_photos, "added to %s".printf(tag.to_string()), false);
442
        }
443
        
444 445
        if (removed != null && !unlinking) {
            Gee.ArrayList<LibraryPhoto> removed_photos = new Gee.ArrayList<LibraryPhoto>();
446
            foreach (DataSource source in removed) {
447 448 449
                LibraryPhoto? photo = source as LibraryPhoto;
                if (photo != null)
                    removed_photos.add(photo);
450 451
            }
            
452
            photos_are_dirty(removed_photos, "removed from %s".printf(tag.to_string()), false);
453 454 455 456
        }
    }
    
    private void on_tag_backlink_removed(ContainerSource container, Gee.Collection<DataSource> sources) {
457 458 459 460 461 462 463
        Gee.ArrayList<LibraryPhoto> photos = new Gee.ArrayList<LibraryPhoto>();
        foreach (DataSource source in sources) {
            LibraryPhoto? photo = source as LibraryPhoto;
            if (photo != null)
                photos.add(photo);
        }
        
464
        photos_are_dirty(photos, "backlink removed from %s".printf(container.to_string()), false);
465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505
    }
    
    private void count_enqueued_work(int count, bool report) {
        outstanding_total += count;
        
#if TRACE_METADATA_WRITER
        debug("[%u/%u] %d metadata jobs enqueued", outstanding_completed, outstanding_total, count);
#endif
        
        if (report)
            progress(outstanding_completed, outstanding_total);
    }
    
    private void count_cancelled_work(int count, bool report) {
        outstanding_total = (outstanding_total >= count) ? outstanding_total - count : 0;
        if (outstanding_completed >= outstanding_total) {
            outstanding_completed = 0;
            outstanding_total = 0;
        }
        
#if TRACE_METADATA_WRITER
        debug("[%u/%u] %d metadata jobs cancelled", outstanding_completed, outstanding_total, count);
#endif
        
        if (report)
            progress(outstanding_completed, outstanding_total);
    }
    
    private void count_completed_work(int count, bool report) {
        outstanding_completed += count;
        if (outstanding_completed >= outstanding_total) {
            outstanding_completed = 0;
            outstanding_total = 0;
        }
        
#if TRACE_METADATA_WRITER
        debug("[%u/%u] %d metadata jobs completed", outstanding_completed, outstanding_total, count);
#endif
        
        if (report)
            progress(outstanding_completed, outstanding_total);
506 507
    }
    
508
    private void schedule_if_dirty(Gee.Iterable<MediaSource> media_sources, string reason) {
509
        Gee.ArrayList<LibraryPhoto> photos = null;
510 511
        foreach (MediaSource media in media_sources) {
            LibraryPhoto? photo = media as LibraryPhoto;
512 513
            if (photo == null)
                continue;
514
            
515 516 517 518
            // if in the importing stage, do not schedule for commit
            if (importing_photos.contains(photo))
                continue;
            
519
            if (photo.is_master_metadata_dirty()) {
520 521
                if (photos == null)
                    photos = new Gee.ArrayList<LibraryPhoto>();
522
                
523
                photos.add(photo);
524 525 526
            }
        }
        
527
        if (photos != null)
528
            photos_are_dirty(photos, reason, true);
529 530
    }
    
531 532
    // No photos are dirty.  The human body is a thing of beauty and grace.
    private void photos_are_dirty(Gee.Collection<LibraryPhoto> photos, string reason, bool already_marked) {
533 534 535 536 537
        if (photos.size == 0)
            return;
        
        // cancel all outstanding and pending jobs
        foreach (LibraryPhoto photo in photos)
538
            cancel_job(photo);
539
        
540
        // mark all the photos as dirty
541 542
        if (!already_marked) {
            try {
543 544 545 546 547 548 549 550 551 552 553
                LibraryPhoto.global.transaction_controller.begin();
                
                foreach (LibraryPhoto photo in photos)
                    photo.set_master_metadata_dirty(true);
                
                LibraryPhoto.global.transaction_controller.commit();
            } catch (Error err) {
                if (err is DatabaseError)
                    AppWindow.database_error((DatabaseError) err);
                else
                    error("Unable to mark metadata as dirty: %s", err.message);
554
            }
555 556
        }
        
557 558
        // ok to drop this on the floor, now that they're marked dirty (will attempt to write them
        // out the next time MetadataWriter runs)
559
        if (closed || !enabled)
560 561 562
            return;
        
#if TRACE_METADATA_WRITER
563
        debug("[%s] adding %d photos to dirty list", reason, photos.size);
564
#endif
565 566
        
        foreach (LibraryPhoto photo in photos) {
567 568 569
            bool enqueued = dirty.enqueue(photo);
            assert(enqueued);
        }
570
        
571
        count_enqueued_work(photos.size, true);
572 573 574 575 576 577 578 579 580 581 582
    }
    
    private void cancel_all(bool wait) {
        dirty.clear();
        
        foreach (CommitJob job in pending.values)
            job.cancel();
        
        if (wait)
            workers.wait_for_empty_queue();
        
583
        count_cancelled_work(int.MAX, true);
584 585
    }
    
586 587 588
    private bool cancel_job(LibraryPhoto photo) {
        bool cancelled = false;
        
589
        if (pending.has_key(photo)) {
590 591 592 593
            CommitJob j = (CommitJob) pending.get(photo);
            pending_cancel.add(j);
            j.cancel();
            pending.unset(photo);
594
            cancelled = true;
595
        }
596
        
597 598 599 600 601 602 603 604
        if (dirty.contains(photo)) {
            bool removed = dirty.remove_first(photo);
            assert(removed);
            
            assert(!dirty.contains(photo));
            
            count_cancelled_work(1, false);
            cancelled = true;
605 606
        }
        
607
        return cancelled;
608 609 610
    }
    
    private void on_photo_dequeued(LibraryPhoto photo) {
611 612 613
        if (!enabled) {
            count_cancelled_work(1, true);
            
614
            return;
615
        }
616 617
        
        Gee.Set<string>? keywords = null;
618
        Gee.Collection<Tag>? tags = Tag.global.fetch_for_source(photo);
619 620 621 622 623 624
        if (tags != null) {
            keywords = new Gee.HashSet<string>();
            foreach (Tag tag in tags)
                keywords.add(tag.get_name());
        }
        
625 626 627 628
        // check if there is already a job for that photo. if yes, cancel it.
        if (pending.has_key(photo))
            cancel_job(photo);

629 630 631 632 633 634 635 636 637 638 639 640 641
        CommitJob job = new CommitJob(this, photo, keywords);
        pending.set(photo, job);
        
#if TRACE_METADATA_WRITER
        debug("%s dequeued for metadata commit, %d pending", photo.to_string(), pending.size);
#endif
        
        workers.enqueue(job);
    }
    
    private void on_update_completed(BackgroundJob j) {
        CommitJob job = (CommitJob) j;
        
642 643 644 645 646 647 648 649 650 651 652 653 654 655 656
        if (job.err != null) {
            warning("Unable to write metadata to %s: %s", job.photo.to_string(), job.err.message);
        } else {
            if (job.wrote_master)
                message("Completed writing metadata to %s", job.photo.get_master_file().get_path());
            else
                message("Unable to write metadata to %s", job.photo.get_master_file().get_path());
            
            if (job.photo.get_editable_file() != null) {
                if (job.wrote_editable)
                    message("Completed writing metadata to %s", job.photo.get_editable_file().get_path());
                else
                    message("Unable to write metadata to %s", job.photo.get_editable_file().get_path());
            }
        }
657 658 659 660
        
        bool removed = pending.unset(job.photo);
        assert(removed);
        
661 662 663
        // since there's potentially multiple state-change operations here, use the transaction
        // controller
        LibraryPhoto.global.transaction_controller.begin();
664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694
        
        if (job.reimport_master_state != null || job.reimport_editable_state != null) {
            // finish_update_*_metadata are going to issue an "altered" signal, and we want to 
            // ignore it
            assert(ignore_photo_alteration == null);
            ignore_photo_alteration = job.photo;
            try {
                if (job.reimport_master_state != null)
                    job.photo.finish_update_master_metadata(job.reimport_master_state);
                
                if (job.reimport_editable_state != null)
                    job.photo.finish_update_editable_metadata(job.reimport_editable_state);
            } catch (DatabaseError err) {
                AppWindow.database_error(err);
            } finally {
                // this assertion guards against reentrancy
                assert(ignore_photo_alteration == job.photo);
                ignore_photo_alteration = null;
            }
        } else {
#if TRACE_METADATA_WRITER
            debug("[%u/%u] No metadata changes for %s", outstanding_completed, outstanding_total,
                job.photo.to_string());
#endif
        }
        
        try {
            job.photo.set_master_metadata_dirty(false);
        } catch (DatabaseError err) {
            AppWindow.database_error(err);
        }
695 696 697 698 699 700 701
        
        LibraryPhoto.global.transaction_controller.commit();
        
        count_completed_work(1, true);
    }
    
    private void on_update_cancelled(BackgroundJob j) {
702
        bool removed = pending_cancel.remove((CommitJob) j);
703 704 705
        assert(removed);
        
        count_cancelled_work(1, true);
706 707 708
    }
}