MediaViewTracker.vala 3.31 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/* Copyright 2011 Yorba Foundation
 *
 * This software is licensed under the GNU Lesser General Public License
 * (version 2.1 or later).  See the COPYING file in this distribution. 
 */

public class MediaViewTracker : Core.ViewTracker {
    public MediaAccumulator all = new MediaAccumulator();
    public MediaAccumulator visible = new MediaAccumulator();
    public MediaAccumulator selected = new MediaAccumulator();
    
    public MediaViewTracker(ViewCollection collection) {
        base (collection);
        
        start(all, visible, selected);
    }
}

public class MediaAccumulator : Object, Core.TrackerAccumulator {
20 21 22 23 24
    public int total = 0;
    public int photos = 0;
    public int videos = 0;
    public int raw = 0;
    public int flagged = 0;
25 26 27 28 29 30 31 32
    
    public bool include(DataObject object) {
        DataSource source = ((DataView) object).get_source();
        
        total++;
        
        Photo? photo = source as Photo;
        if (photo != null) {
33
            if (photo.get_master_file_format() == PhotoFileFormat.RAW) {
34
                raw++;
35 36 37 38 39 40
            }
            
            if (photo.get_master_file_format() != PhotoFileFormat.RAW || 
                photo.is_raw_developer_available(RawDeveloper.CAMERA)) {
                photos++;
            }
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
        } else if (source is VideoSource) {
            videos++;
        }
        
        Flaggable? flaggable = source as Flaggable;
        if (flaggable != null && flaggable.is_flagged())
            flagged++;
        
        // because of total, always fire "updated"
        return true;
    }
    
    public bool uninclude(DataObject object) {
        DataSource source = ((DataView) object).get_source();
        
        assert(total > 0);
        total--;
        
        Photo? photo = source as Photo;
        if (photo != null) {
            if (photo.get_master_file_format() == PhotoFileFormat.RAW) {
                assert(raw > 0);
                raw--;
            }
65 66 67 68 69 70
            
            if (photo.get_master_file_format() != PhotoFileFormat.RAW || 
                photo.is_raw_developer_available(RawDeveloper.CAMERA)) {
                assert(photos > 0);
                photos--;
            }
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
        } else if (source is Video) {
            assert(videos > 0);
            videos--;
        }
        
        Flaggable? flaggable = source as Flaggable;
        if (flaggable != null && flaggable.is_flagged()) {
            assert(flagged > 0);
            flagged--;
        }
        
        // because of total, always fire "updated"
        return true;
    }
    
    public bool altered(DataObject object, Alteration alteration) {
        // the only alteration that can happen to MediaSources this accumulator is concerned with is
        // flagging; typeness and raw-ness don't change at runtime
        if (!alteration.has_detail("metadata", "flagged"))
            return false;
        
        Flaggable? flaggable = ((DataView) object).get_source() as Flaggable;
        if (flaggable == null)
            return false;
        
        if (flaggable.is_flagged()) {
            flagged++;
        } else {
            assert(flagged > 0);
            flagged--;
        }
        
        return true;
    }
    
    public string to_string() {
        return "%d photos/%d videos/%d raw/%d flagged".printf(photos, videos, raw, flagged);
    }
}