summaryrefslogtreecommitdiff
path: root/src/faces/FacesBranch.vala
blob: 42fd9213372a67d9219d930c228d8edc7374c1b4 (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
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
/* Copyright 2016 Software Freedom Conservancy Inc.
 *
 * 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 Faces.Branch : Sidebar.Branch {
    private Gee.HashMap<Face, Faces.SidebarEntry> entry_map = new Gee.HashMap<Face, Faces.SidebarEntry>();
    
    public Branch() {
        base (new Faces.Grouping(),
            Sidebar.Branch.Options.HIDE_IF_EMPTY
                | Sidebar.Branch.Options.AUTO_OPEN_ON_NEW_CHILD
                | Sidebar.Branch.Options.STARTUP_EXPAND_TO_FIRST_CHILD,
            comparator);
        
        // seed the branch with existing faces
        on_faces_added_removed(Face.global.get_all(), null);
        
        // monitor collection for future events
        Face.global.contents_altered.connect(on_faces_added_removed);
        Face.global.items_altered.connect(on_faces_altered);
    }
    
    ~Branch() {
        Face.global.contents_altered.disconnect(on_faces_added_removed);
        Face.global.items_altered.disconnect(on_faces_altered);
    }
    
    public Faces.SidebarEntry? get_entry_for_face(Face face) {
        return entry_map.get(face);
    }
    
    private static int comparator(Sidebar.Entry a, Sidebar.Entry b) {
        if (a == b)
            return 0;
        
        return Face.compare_names(((Faces.SidebarEntry) a).for_face(),
            ((Faces.SidebarEntry) b).for_face());
    }
    
    private void on_faces_added_removed(Gee.Iterable<DataObject>? added, Gee.Iterable<DataObject>? removed) {
        if (added != null) {
            foreach (DataObject object in added) {
                Face face = (Face) object;
                
                Faces.SidebarEntry entry = new Faces.SidebarEntry(face);
                entry_map.set(face, entry);
                
                graft(get_root(), entry);
            }
        }
        
        if (removed != null) {
            foreach (DataObject object in removed) {
                Face face = (Face) object;
                
                Faces.SidebarEntry? entry = entry_map.get(face);
                assert(entry != null);
                
                bool is_removed = entry_map.unset(face);
                assert(is_removed);
                
                prune(entry);
            }
        }
    }
    
    private void on_faces_altered(Gee.Map<DataObject, Alteration> altered) {
        foreach (DataObject object in altered.keys) {
            if (!altered.get(object).has_detail("metadata", "name"))
                continue;
            
            Face face = (Face) object;
            Faces.SidebarEntry? entry = entry_map.get(face);
            assert(entry != null);
            
            entry.sidebar_name_changed(face.get_name());
            entry.sidebar_tooltip_changed(face.get_name());
            reorder(entry);
        }
    }
}

public class Faces.Grouping : Sidebar.Header {
    public Grouping() {
        base (_("Faces"));
    }
}

public class Faces.SidebarEntry : Sidebar.SimplePageEntry, Sidebar.RenameableEntry,
    Sidebar.DestroyableEntry {
    private static string single_face_icon = Resources.ICON_ONE_FACE;
    
    private Face face;
    
    public SidebarEntry(Face face) {
        this.face = face;
    }
    
    internal static void init() {
    }
    
    internal static void terminate() {
    }
    
    public Face for_face() {
        return face;
    }
    
    public bool is_user_renameable() {
        return true;
    }
    
    public override string get_sidebar_name() {
        return face.get_name();
    }
    
    public override string? get_sidebar_icon() {
        return single_face_icon;
    }
    
    protected override Page create_page() {
        return new FacePage(face);
    }
    
    public void rename(string new_name) {
        string? prepped = Face.prep_face_name(new_name);
        if (prepped == null)
            return;
        
        if (!Face.global.exists(prepped))
            AppWindow.get_command_manager().execute(new RenameFaceCommand(face, prepped));
        else if (prepped != face.get_name())
            AppWindow.error_message(Resources.rename_face_exists_message(prepped));
    }
    
    public void destroy_source() {
        if (Dialogs.confirm_delete_face(face))
            AppWindow.get_command_manager().execute(new DeleteFaceCommand(face));
    }
}