summaryrefslogtreecommitdiff
path: root/src/MediaViewTracker.vala
blob: 27fcf4a001bbfab4d353295875b91efcbf9384ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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
111
112
113
114
/* Copyright 2016 Software Freedom Conservancy Inc.
 *
 * 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 {
    public int total = 0;
    public int photos = 0;
    public int videos = 0;
    public int raw = 0;
    public int flagged = 0;
    
    public bool include(DataObject object) {
        DataSource source = ((DataView) object).get_source();
        
        total++;
        
        Photo? photo = source as Photo;
        if (photo != null) {
            if (photo.get_master_file_format() == PhotoFileFormat.RAW) {
                raw++;
            }
            
            if (photo.get_master_file_format() != PhotoFileFormat.RAW || 
                photo.is_raw_developer_available(RawDeveloper.CAMERA)) {
                photos++;
            }
        } 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();
        
        if (total < 1) {
            warning("Tried to remove DataObject %s from empty %s (%s)".printf(object.to_string(),
                get_type().name(), to_string()));
            return false;
        }
        total--;
        
        Photo? photo = source as Photo;
        if (photo != null) {
            if (photo.get_master_file_format() == PhotoFileFormat.RAW) {
                assert(raw > 0);
                raw--;
            }
            
            if (photo.get_master_file_format() != PhotoFileFormat.RAW || 
                photo.is_raw_developer_available(RawDeveloper.CAMERA)) {
                assert(photos > 0);
                photos--;
            }
        } 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);
    }
}