OfflinePage.vala 4.59 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
 */

public class OfflinePage : CheckerboardPage {
8
    public const string NAME = _("Missing Files");
9
    
10
    private class OfflineView : Thumbnail {
11 12
        public OfflineView(MediaSource source) {
            base (source);
13
            
14
            assert(source.is_offline());
15 16 17
        }
    }
    
18 19 20
    private class OfflineSearchViewFilter : DefaultSearchViewFilter {
        public override uint get_criteria() {
            return SearchFilterCriteria.TEXT | SearchFilterCriteria.FLAG | 
21
                SearchFilterCriteria.MEDIA | SearchFilterCriteria.RATING | SearchFilterCriteria.SAVEDSEARCH;
22 23 24 25
        }
    }
    
    private OfflineSearchViewFilter search_filter = new OfflineSearchViewFilter();
26
    private MediaViewTracker tracker;
27
    
28 29
    public OfflinePage() {
        base (NAME);
30
        
31 32
        init_item_context_menu("OfflineContextMenu");
        init_toolbar("OfflineToolbar");
33
        
34 35
        tracker = new MediaViewTracker(get_view());
        
36 37
        // monitor offline and initialize view with all items in it
        LibraryPhoto.global.offline_contents_altered.connect(on_offline_contents_altered);
38
        Video.global.offline_contents_altered.connect(on_offline_contents_altered);
39
        
40 41
        on_offline_contents_altered(LibraryPhoto.global.get_offline_bin_contents(), null);
        on_offline_contents_altered(Video.global.get_offline_bin_contents(), null);
42 43
    }
    
44 45 46 47 48
    ~OfflinePage() {
        LibraryPhoto.global.offline_contents_altered.disconnect(on_offline_contents_altered);
        Video.global.offline_contents_altered.disconnect(on_offline_contents_altered);
    }
    
49 50 51 52 53
    protected override void init_collect_ui_filenames(Gee.List<string> ui_filenames) {
        base.init_collect_ui_filenames(ui_filenames);
        
        ui_filenames.add("offline.ui");
    }
54 55 56 57 58

    private const GLib.ActionEntry[] entries = {
        { "RemoveFromLibrary", on_remove_from_library }
    };

Jens Georg's avatar
Jens Georg committed
59 60
    protected override void add_actions (GLib.ActionMap map) {
        base.add_actions(map);
61

Jens Georg's avatar
Jens Georg committed
62
        map.add_action_entries(entries, this);
63
    }
64

Jens Georg's avatar
Jens Georg committed
65 66
    protected override void remove_actions(GLib.ActionMap map) {
        base.remove_actions(map);
67
        foreach (var entry in entries) {
Jens Georg's avatar
Jens Georg committed
68
            map.remove_action(entry.name);
69 70 71
        }
    }

72 73 74 75
    public override Core.ViewTracker? get_view_tracker() {
        return tracker;
    }
    
76
    protected override void update_actions(int selected_count, int count) {
77
        set_action_sensitive("RemoveFromLibrary", selected_count > 0);
78

79
        base.update_actions(selected_count, count);
80 81
    }
    
82 83
    private void on_offline_contents_altered(Gee.Collection<MediaSource>? added,
        Gee.Collection<MediaSource>? removed) {
84
        if (added != null) {
85 86
            foreach (MediaSource source in added)
                get_view().add(new OfflineView(source));
87 88 89 90
        }
        
        if (removed != null) {
            Marker marker = get_view().start_marking();
91 92
            foreach (MediaSource source in removed)
                marker.mark(get_view().get_view_for_source(source));
93 94 95 96
            get_view().remove_marked(marker);
        }
    }
    
97
    private void on_remove_from_library() {
98 99 100
        Gee.Collection<MediaSource> sources =
            (Gee.Collection<MediaSource>) get_view().get_selected_sources();
        if (sources.size == 0)
101 102
            return;
        
103
        if (!remove_offline_dialog(AppWindow.get_instance(), sources.size))
104 105 106 107 108
            return;
        
        AppWindow.get_instance().set_busy_cursor();
        
        ProgressDialog progress = null;
109
        if (sources.size >= 20)
Jens Georg's avatar
Jens Georg committed
110
            progress = new ProgressDialog(AppWindow.get_instance(), _("Deleting…"));
111 112 113 114 115 116

        Gee.ArrayList<LibraryPhoto> photos = new Gee.ArrayList<LibraryPhoto>();
        Gee.ArrayList<Video> videos = new Gee.ArrayList<Video>();
        MediaSourceCollection.filter_media(sources, photos, videos);

        if (progress != null) {
117
            LibraryPhoto.global.remove_from_app(photos, false, progress.monitor);
118 119
            Video.global.remove_from_app(videos, false, progress.monitor);
        } else {
120
            LibraryPhoto.global.remove_from_app(photos, false);
121 122
            Video.global.remove_from_app(videos, false);
        }
123 124 125 126 127 128 129
        
        if (progress != null)
            progress.close();
        
        AppWindow.get_instance().set_normal_cursor();
    }
    
130 131 132
    public override SearchViewFilter get_search_view_filter() {
        return search_filter;
    }
133 134
}