summaryrefslogtreecommitdiff
path: root/src/camera/CameraBranch.vala
blob: 052f093332627a0f0c9471f76e2ad51959e9874e (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
/* 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 Camera.Branch : Sidebar.Branch {
    internal static string? cameras_icon = Resources.ICON_CAMERAS;
    
    private Gee.HashMap<DiscoveredCamera, Camera.SidebarEntry> camera_map = new Gee.HashMap<
        DiscoveredCamera, Camera.SidebarEntry>();
    
    public Branch() {
        base (new Camera.Header(),
            Sidebar.Branch.Options.HIDE_IF_EMPTY | Sidebar.Branch.Options.AUTO_OPEN_ON_NEW_CHILD,
            camera_comparator);
        
        foreach (DiscoveredCamera camera in CameraTable.get_instance().get_cameras())
            add_camera(camera);
        
        CameraTable.get_instance().camera_added.connect(on_camera_added);
        CameraTable.get_instance().camera_removed.connect(on_camera_removed);
    }
    
    internal static void init() {
    }
    
    internal static void terminate() {
    }
    
    private static int camera_comparator(Sidebar.Entry a, Sidebar.Entry b) {
        if (a == b) 
            return 0;
        
        // Compare based on name.
        int ret = a.get_sidebar_name().collate(b.get_sidebar_name());
        if (ret == 0) {
            // Cameras had same name! Fallback to URI comparison.
            Camera.SidebarEntry? cam_a = a as Camera.SidebarEntry;
            Camera.SidebarEntry? cam_b = b as Camera.SidebarEntry;
            assert (cam_a != null && cam_b != null);
            ret = cam_a.get_uri().collate(cam_b.get_uri());
        }
        
        return ret;
    }
    
    public Camera.SidebarEntry? get_entry_for_camera(DiscoveredCamera camera) {
        return camera_map.get(camera);
    }
    
    private void on_camera_added(DiscoveredCamera camera) {
        add_camera(camera);
    }
    
    private void on_camera_removed(DiscoveredCamera camera) {
        remove_camera(camera);
    }
    
    private void add_camera(DiscoveredCamera camera) {
        assert(!camera_map.has_key(camera));
        
        Camera.SidebarEntry entry = new Camera.SidebarEntry(camera);
        camera_map.set(camera, entry);
        
        // want to show before adding page so the grouping is available to graft onto
        graft(get_root(), entry);
    }
    
    private void remove_camera(DiscoveredCamera camera) {
        assert(camera_map.has_key(camera));
        
        Camera.SidebarEntry? entry = camera_map.get(camera);
        assert(entry != null);
        
        bool removed = camera_map.unset(camera);
        assert(removed);
        
        prune(entry);
    }
}

public class Camera.Header : Sidebar.Header {
    public Header() {
        base (_("Cameras"), _("List of all discovered camera devices"));
    }
}

public class Camera.SidebarEntry : Sidebar.SimplePageEntry {
    private DiscoveredCamera camera;
    private string uri;
    
    public SidebarEntry(DiscoveredCamera camera) {
        this.camera = camera;
        this.uri = camera.uri;
    }
    
    public override string get_sidebar_name() {
        return camera.display_name ?? _("Camera");
    }
    
    public override string? get_sidebar_icon() {
        return camera.icon ?? Camera.Branch.cameras_icon;
    }
    
    protected override Page create_page() {
        return new ImportPage(camera.gcamera, uri, get_sidebar_name(), get_sidebar_icon());
    }
    
    public string get_uri() {
        return uri;
    }
}