summaryrefslogtreecommitdiff
path: root/src/searches/SavedSearchPage.vala
blob: 1693ca39a1366bd9db8a57fe0081f01484e59ba7 (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
/* Copyright 2016 Software Freedom Conservancy Inc.
 *
 * This software is licensed under the GNU LGPL (version 2.1 or later).
 * See the COPYING file in this distribution.
 */

// Source monitoring for saved searches.
private class SavedSearchManager : CollectionViewManager {
    SavedSearch search;
    public SavedSearchManager(SavedSearchPage owner, SavedSearch search) {
        base (owner);
        this.search = search;
    }
    
    public override bool include_in_view(DataSource source) {
        return search.predicate((MediaSource) source);
    }
}

// Page for displaying saved searches.
public class SavedSearchPage : CollectionPage {

    // The search logic and parameters are contained in the SavedSearch.
    private SavedSearch search;
    
    public SavedSearchPage(SavedSearch search) {
        base (search.get_name());
        this.search = search;
        
        
        foreach (MediaSourceCollection sources in MediaCollectionRegistry.get_instance().get_all())
            get_view().monitor_source_collection(sources, new SavedSearchManager(this, search), null);
        
        init_page_context_menu("/SearchContextMenu");
    }
    
    protected override void get_config_photos_sort(out bool sort_order, out int sort_by) {
        Config.Facade.get_instance().get_library_photos_sort(out sort_order, out sort_by);
    }
    
    protected override void set_config_photos_sort(bool sort_order, int sort_by) {
        Config.Facade.get_instance().set_library_photos_sort(sort_order, sort_by);
    }
    
    protected override void init_collect_ui_filenames(Gee.List<string> ui_filenames) {
        base.init_collect_ui_filenames(ui_filenames);
        ui_filenames.add("savedsearch.ui");
    }
    
    protected override Gtk.ActionEntry[] init_collect_action_entries() {
        Gtk.ActionEntry[] actions = base.init_collect_action_entries();
        
        Gtk.ActionEntry rename_search = { "RenameSearch", null, TRANSLATABLE, null, null, on_rename_search };
        actions += rename_search;
        
        Gtk.ActionEntry edit_search = { "EditSearch", null, TRANSLATABLE, null, null, on_edit_search };
        actions += edit_search;
        
        Gtk.ActionEntry delete_search = { "DeleteSearch", null, TRANSLATABLE, null, null, on_delete_search };
        actions += delete_search;
        
        return actions;
    }
    
    private void on_delete_search() {
        if (Dialogs.confirm_delete_saved_search(search))
            AppWindow.get_command_manager().execute(new DeleteSavedSearchCommand(search));
    }
    
    private void on_rename_search() {
        LibraryWindow.get_app().rename_search_in_sidebar(search);
    }
    
    private void on_edit_search() {
        SavedSearchDialog ssd = new SavedSearchDialog.edit_existing(search);
        ssd.show();
    }
    
    protected override void update_actions(int selected_count, int count) {
        set_action_details("RenameSearch",
            Resources.RENAME_SEARCH_MENU,
            null, true);
        set_action_details("EditSearch",
            Resources.EDIT_SEARCH_MENU,
            null, true);
        set_action_details("DeleteSearch",
            Resources.DELETE_SEARCH_MENU,
            null, true);
        base.update_actions(selected_count, count);
    }
}