~ttx/shotwell/shotwell-autotagging

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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/* Copyright 2010 Yorba Foundation
 *
 * 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 TagPage : CollectionPage {
    public class Stub : PageStub {
        public Tag tag;
        
        public Stub(Tag tag) {
            this.tag = tag;
        }
        
        public override string? get_icon_name() {
            return Resources.ICON_ONE_TAG;
        }

        public override string get_name() {
            return tag.get_name();
        }
        
        public override bool is_renameable() {
            return (tag != null);
        }
        
        protected override Page construct_page() {
            return new TagPage(tag);
        }
    }
    
    private Tag tag;
    
    private TagPage(Tag tag) {
        base (tag.get_name(), "tags.ui", create_actions());
        
        this.tag = tag;
        
        Tag.global.items_altered.connect(on_tags_altered);
        tag.mirror_photos(get_view(), create_thumbnail);
        
        init_page_context_menu("/TagsContextMenu");
        
        ui.add_ui(ui.new_merge_id(), "/CollectionContextMenu/ContextTagsPlaceholder",
            "ContextRemoveTagFromPhotos", "RemoveTagFromPhotos", Gtk.UIManagerItemType.AUTO, false);
    }
    
    ~TagPage() {
        get_view().halt_mirroring();
        Tag.global.items_altered.disconnect(on_tags_altered);
    }
    
    public static TagPage.Stub create_stub(Tag tag) {
        return new Stub(tag);
    }
    
    public Tag get_tag() {
        return tag;
    }
    
    protected override void get_config_photos_sort(out bool sort_order, out int sort_by) {
        Config.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.get_instance().set_library_photos_sort(sort_order, sort_by);
    }
    
    private static Gtk.ActionEntry[] create_actions() {
        Gtk.ActionEntry[] actions = new Gtk.ActionEntry[0];
        
        Gtk.ActionEntry delete_tag = { "DeleteTag", null, TRANSLATABLE, null, null, on_delete_tag };
        // label and tooltip are assigned when the menu is displayed
        actions += delete_tag;
        
        Gtk.ActionEntry rename_tag = { "RenameTag", null, TRANSLATABLE, null, null, on_rename_tag };
        // label and tooltip are assigned when the menu is displayed
        actions += rename_tag;
        
        Gtk.ActionEntry remove_tag = { "RemoveTagFromPhotos", null, TRANSLATABLE, null, null, 
            on_remove_tag_from_photos };
        // label and tooltip are assigned when the menu is displayed
        actions += remove_tag;
        
        return actions;
    }
    
    private void on_tags_altered(Gee.Map<DataObject, Alteration> map) {
        if (map.has_key(tag))
            set_page_name(tag.get_name());
    }
    
    protected override void on_tags_menu() {
        int selected_count = get_view().get_selected_count();
        
        set_item_display("/CollectionMenuBar/TagsMenu/DeleteTag",
            Resources.delete_tag_menu(tag.get_name()),
            Resources.delete_tag_tooltip(tag.get_name(), tag.get_photos_count()),
            true);
        
        set_item_display("/CollectionMenuBar/TagsMenu/RenameTag",
            Resources.rename_tag_menu(tag.get_name()),
            Resources.rename_tag_tooltip(tag.get_name()),
            true);
        
        set_item_display("/CollectionMenuBar/TagsMenu/RemoveTagFromPhotos", 
            Resources.untag_photos_menu(tag.get_name(), selected_count),
            Resources.untag_photos_tooltip(tag.get_name(), selected_count),
            selected_count > 0);
        
        base.on_tags_menu();
    }
    
    protected override bool on_context_invoked() {
        int selected_count = get_view().get_selected_count();
        
        set_item_display("/CollectionContextMenu/ContextTagsPlaceholder/ContextRemoveTagFromPhotos",
            Resources.untag_photos_menu(tag.get_name(), selected_count),
            Resources.untag_photos_tooltip(tag.get_name(), selected_count),
            selected_count > 0);
        
        return base.on_context_invoked();
    }
    
    public override Gtk.Menu? get_page_context_menu() {
        set_item_display("/TagsContextMenu/ContextRenameTag",
            Resources.rename_tag_menu(tag.get_name()),
            Resources.rename_tag_tooltip(tag.get_name()),
            true);
        
        set_item_display("/TagsContextMenu/ContextDeleteTag",
            Resources.delete_tag_menu(tag.get_name()),
            Resources.delete_tag_tooltip(tag.get_name(), tag.get_photos_count()),
            true);
        
        return base.get_page_context_menu();
    }
    
    public override void rename(string new_name) {
        if (is_string_empty(new_name))
            return;
        
        if (!Tag.global.exists(new_name))
            get_command_manager().execute(new RenameTagCommand(tag, new_name));
        else if (new_name != tag.get_name())
            AppWindow.error_message(Resources.rename_tag_exists_message(new_name));
    }
    
    private void on_rename_tag() {
        LibraryWindow.get_app().sidebar_rename_in_place(this);
    }
    
    private void on_delete_tag() {
        int count = tag.get_photos_count();
        string msg = ngettext(
            "This will remove the tag \"%s\" from one photo.  Continue?",
            "This will remove the tag \"%s\" from %d photos.  Continue?",
            count).printf(tag.get_name(), count);
        
        if (!AppWindow.negate_affirm_question(msg, _("_Cancel"), _("_Delete"),
            Resources.DELETE_TAG_TITLE))
            return;
        
        get_command_manager().execute(new DeleteTagCommand(tag));
    }
    
    private void on_remove_tag_from_photos() {
        if (get_view().get_selected_count() > 0) {
            get_command_manager().execute(new TagUntagPhotosCommand(tag, 
                (Gee.Collection<LibraryPhoto>) get_view().get_selected_sources(), 
                get_view().get_selected_count(), false));
        }
    }
}