1/*
2* Copyright (c) 2010-2013 Yorba Foundation
3*
4* This program is free software; you can redistribute it and/or
5* modify it under the terms of the GNU Lesser General Public
6* License as published by the Free Software Foundation; either
7* version 2.1 of the License, or (at your option) any later version.
8*
9* This program is distributed in the hope that it will be useful,
10* but WITHOUT ANY WARRANTY; without even the implied warranty of
11* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12* General Public License for more details.
13*
14* You should have received a copy of the GNU General Public
15* License along with this program; if not, write to the
16* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17* Boston, MA 02110-1301 USA
18*/
19
20public class LastImportPage : CollectionPage {
21    public const string NAME = _ ("Last Import");
22
23    private class LastImportViewManager : CollectionViewManager {
24        private ImportID import_id;
25
26        public LastImportViewManager (LastImportPage owner, ImportID import_id) {
27            base (owner);
28
29            this.import_id = import_id;
30        }
31
32        public override bool include_in_view (DataSource source) {
33            return ((MediaSource) source).get_import_id ().id == import_id.id;
34        }
35    }
36
37    private ImportID last_import_id = ImportID ();
38    private Alteration last_import_alteration = new Alteration ("metadata", "import-id");
39
40    public LastImportPage () {
41        base (NAME);
42
43        // be notified when the import rolls change
44        foreach (MediaSourceCollection col in MediaCollectionRegistry.get_instance ().get_all ()) {
45            col.import_roll_altered.connect (on_import_rolls_altered);
46        }
47
48        // set up view manager for the last import roll
49        on_import_rolls_altered ();
50    }
51
52    ~LastImportPage () {
53        foreach (MediaSourceCollection col in MediaCollectionRegistry.get_instance ().get_all ()) {
54            col.import_roll_altered.disconnect (on_import_rolls_altered);
55        }
56    }
57
58    private void on_import_rolls_altered () {
59        // see if there's a new last ImportID, or no last import at all
60        ImportID? current_last_import_id =
61            MediaCollectionRegistry.get_instance ().get_last_import_id ();
62
63        if (current_last_import_id == null) {
64            get_view ().halt_all_monitoring ();
65            get_view ().clear ();
66
67            return;
68        }
69
70        if (current_last_import_id.id == last_import_id.id)
71            return;
72
73        last_import_id = current_last_import_id;
74
75        get_view ().halt_all_monitoring ();
76        get_view ().clear ();
77
78        foreach (MediaSourceCollection col in MediaCollectionRegistry.get_instance ().get_all ()) {
79            get_view ().monitor_source_collection (col, new LastImportViewManager (this,
80                                                  last_import_id), last_import_alteration);
81        }
82    }
83}
84