summaryrefslogtreecommitdiff
path: root/src/baobab-pathbar.vala
blob: ad39f6c66921605eb503a018f129568dd5e59346 (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
/* Baobab - disk usage analyzer
 *
 * Copyright (C) 2020 Stefano Facchini <stefano.facchini@gmail.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

namespace Baobab {

    [GtkTemplate (ui = "/org/gnome/baobab/ui/baobab-pathbutton.ui")]
    public class PathButton : Gtk.Button {
        [GtkChild]
        new unowned Gtk.Label label;
        [GtkChild]
        unowned Gtk.Image icon;

        public PathButton (string name, Icon? gicon) {
            label.label = name;

            icon.hide ();
            if (gicon != null) {
                icon.gicon = gicon;
                icon.show ();
            }
        }
    }

    public class Pathbar : Gtk.Box {
        public signal void item_activated (Gtk.TreePath path);

        Location location_;
        public Location location {
            set {
                location_ = value;
                path = new Gtk.TreePath.first ();
            }

            get {
                return location_;
            }
        }

        public new Gtk.TreePath path {
            set {
                if (location == null || location.scanner == null) {
                    return;
                }

                clear ();

                Gtk.TreePath path_tmp = value;

                List<PathButton> buttons = null;

                while (path_tmp.get_depth () > 0) {
                    buttons.append (make_button (path_tmp));
                    path_tmp.up ();
                }

                buttons.reverse ();
                foreach (var button in buttons) {
                    add (button);
                }
            }
        }

        void clear () {
            this.foreach ((widget) => { widget.destroy (); });
        }

        PathButton make_button (Gtk.TreePath path) {
            string label;
            Icon? gicon = null;

            if (path.get_depth () == 1) {
                label = location.name;
                gicon = location.symbolic_icon;
            } else {
                Gtk.TreeIter iter;
                string name;
                string display_name;
                location.scanner.get_iter (out iter, path);
                location.scanner.get (iter,
                                      Scanner.Columns.NAME, out name,
                                      Scanner.Columns.DISPLAY_NAME, out display_name);
                label = display_name != null ? display_name : name;
            }

            var button = new PathButton (label, gicon);
            button.clicked.connect (() => {
                item_activated (path);
            });

            return button;
        }
    }
}