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
7public class LastImportPage : CollectionPage {
8    public const string NAME = _("Last Import");
9
10    private class LastImportViewManager : CollectionViewManager {
11        private ImportID import_id;
12
13        public LastImportViewManager(LastImportPage owner, ImportID import_id) {
14            base (owner);
15
16            this.import_id = import_id;
17        }
18
19        public override bool include_in_view(DataSource source) {
20            return ((MediaSource) source).get_import_id().id == import_id.id;
21        }
22    }
23
24    private ImportID last_import_id = ImportID();
25    private Alteration last_import_alteration = new Alteration("metadata", "import-id");
26
27    public LastImportPage() {
28        base (NAME);
29
30        // be notified when the import rolls change
31        foreach (MediaSourceCollection col in MediaCollectionRegistry.get_instance().get_all()) {
32            col.import_roll_altered.connect(on_import_rolls_altered);
33        }
34
35        // set up view manager for the last import roll
36        on_import_rolls_altered();
37    }
38
39    public LastImportPage.for_id(ImportID id) {
40        base(NAME);
41
42        this.last_import_id = id;
43
44        get_view().halt_all_monitoring();
45        get_view().clear();
46
47        foreach (MediaSourceCollection col in MediaCollectionRegistry.get_instance().get_all()) {
48            get_view().monitor_source_collection(col, new LastImportViewManager(this,
49                last_import_id), last_import_alteration);
50        }
51     }
52
53    ~LastImportPage() {
54        foreach (MediaSourceCollection col in MediaCollectionRegistry.get_instance().get_all()) {
55            col.import_roll_altered.disconnect(on_import_rolls_altered);
56        }
57    }
58
59    private void on_import_rolls_altered() {
60        // see if there's a new last ImportID, or no last import at all
61        ImportID? current_last_import_id =
62            MediaCollectionRegistry.get_instance().get_last_import_id();
63
64        if (current_last_import_id == null) {
65            get_view().halt_all_monitoring();
66            get_view().clear();
67
68            return;
69        }
70
71        if (current_last_import_id.id == last_import_id.id)
72            return;
73
74        last_import_id = current_last_import_id;
75
76        get_view().halt_all_monitoring();
77        get_view().clear();
78
79        foreach (MediaSourceCollection col in MediaCollectionRegistry.get_instance().get_all()) {
80            get_view().monitor_source_collection(col, new LastImportViewManager(this,
81                last_import_id), last_import_alteration);
82        }
83    }
84
85    protected override void get_config_photos_sort(out bool sort_order, out int sort_by) {
86        Config.Facade.get_instance().get_library_photos_sort(out sort_order, out sort_by);
87    }
88
89    protected override void set_config_photos_sort(bool sort_order, int sort_by) {
90        Config.Facade.get_instance().set_library_photos_sort(sort_order, sort_by);
91    }
92}
93
94