summaryrefslogtreecommitdiff
path: root/src/baobab-location-list.vala
blob: 731e329fb71041982eb136e0d18c56dc2f88fd8e (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
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* Baobab - disk usage analyzer
 *
 * Copyright (C) 2012  Paolo Borelli <pborelli@gnome.org>
 *
 * 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-location-row.ui")]
    private class LocationRow : Gtk.ListBoxRow {
        [GtkChild]
        private unowned Gtk.Image image;
        [GtkChild]
        private unowned Gtk.Label name_label;
        [GtkChild]
        private unowned Gtk.Label path_label;
        [GtkChild]
        private unowned Gtk.Label available_label;
        [GtkChild]
        private unowned Gtk.Label total_size_label;
        [GtkChild]
        private unowned Gtk.LevelBar usage_bar;

        public Location? location { get; private set; }

        public LocationRow (Location l) {
            location = l;

            image.gicon = location.icon;

            var escaped = Markup.escape_text (location.name);
            name_label.label = "<b>%s</b>".printf (escaped);

            path_label.hide();
            if (location.file != null) {
                path_label.label = Markup.escape_text (location.file.get_parse_name ());
                path_label.show();
            }

            // assume for local mounts the end of the mount path is the
            // relevant information, and for remote mounts the beginning is
            // more important
            path_label.ellipsize = location.is_remote ? Pango.EllipsizeMode.END : Pango.EllipsizeMode.START;

            update_fs_usage_info ();
            location.changed.connect (() => { update_fs_usage_info (); });
        }

        void update_fs_usage_info () {
            total_size_label.hide();
            if (location.volume != null || location.mount != null || location.is_main_volume) {
                if (location.size != null) {
                    total_size_label.label = _("%s Total").printf (format_size (location.size));
                    total_size_label.show();

                    if (location.used != null) {
                        available_label.label = _("%s Available").printf (format_size (location.size - location.used));

                        usage_bar.max_value = location.size;

                        // Set critical color at 90% of the size
                        usage_bar.add_offset_value (Gtk.LEVEL_BAR_OFFSET_LOW, 0.9 * location.size);
                        usage_bar.value = location.used;
                        usage_bar.show ();
                    } else {
                        available_label.label = _("Unknown");
                    }
                } else if (location.used != null) {
                    // useful for some remote mounts where we don't know the
                    // size but do have a usage figure
                    available_label.label = _("%s Used").printf (format_size (location.used));
                } else if (location.volume != null && location.mount == null && location.volume.can_mount ()) {
                    available_label.label = _("Unmounted");
                }
            }
        }
    }

    [GtkTemplate (ui = "/org/gnome/baobab/ui/baobab-location-list.ui")]
    public class LocationList : Gtk.Box {
        [GtkChild]
        private unowned Gtk.ListBox local_list_box;
        [GtkChild]
        private unowned Gtk.ListBox remote_list_box;
        [GtkChild]
        private unowned Gtk.Box remote_box;

        public signal void location_activated (Location location);

        private const int MAX_RECENT_LOCATIONS = 5;

        private VolumeMonitor monitor;

        private List<Location> locations = null;

        construct {
            monitor = VolumeMonitor.get ();
            monitor.mount_changed.connect (mount_changed);
            monitor.mount_removed.connect (mount_removed);
            monitor.mount_added.connect (mount_added);
            monitor.volume_changed.connect (volume_changed);
            monitor.volume_removed.connect (volume_removed);
            monitor.volume_added.connect (volume_added);

            local_list_box.row_activated.connect (row_activated);

            remote_list_box.row_activated.connect (row_activated);

            populate ();

            queue_query_fs_usage ();
            Timeout.add_seconds(2, (() => {
                queue_query_fs_usage ();
                return Source.CONTINUE;
            }));
        }

        void queue_query_fs_usage () {
            foreach (var location in locations) {
                location.queue_query_fs_usage ();
            }
        }

        bool already_present (File file) {
            foreach (var l in locations) {
                if (l.file != null && l.file.equal (file)) {
                    return true;
                }
            }
            return false;
        }

        void volume_changed (Volume volume) {
            foreach (var location in locations) {
                if (location.volume == volume) {
                    location.update_volume_info ();
                }
            }

            // Collect duplicated mounts
            var mount = volume.get_mount ();
            if (mount == null) {
                return;
            }
            foreach (var location in locations) {
                // We can't just check location.mount == mount because there could be multiple GMounts with the same root...
                var same_mount = location.mount != null && location.mount.get_root ().equal (mount.get_root ());
                if (same_mount && location.volume != volume) {
                    locations.remove(location);
                }
            }

            update ();
        }

        void volume_removed (Volume volume) {
            foreach (var location in locations) {
                if (location.volume == volume) {
                    locations.remove (location);
                    break;
                }
            }

            update ();
        }

        void volume_added (Volume volume) {
            locations.append (new Location.from_volume (volume));
            volume.changed.connect (() => {
                volume_changed (volume);
            });

            update ();
        }

        void mount_changed (Mount mount) {
        }

        void mount_removed (Mount mount) {
            var volume = mount.get_volume ();
            if (volume != null) {
                volume_changed (volume);
            }

            foreach (var location in locations) {
                if (location.mount == mount && location.volume == null) {
                    locations.remove (location);
                    break;
                }
            }

            update ();
        }

        void mount_added (Mount mount) {
            var volume = mount.get_volume ();
            if (volume == null) {
                if (!already_present (mount.get_root ())) {
                    locations.append (new Location.from_mount (mount));
                }
            } else {
                volume_changed (volume);
            }

            update ();
        }

        void populate () {
            locations.append (new Location.for_home_folder ());
            locations.append (new Location.for_main_volume ());

            foreach (var volume in monitor.get_volumes ()) {
                volume_added (volume);
            }

            foreach (var mount in monitor.get_mounts ()) {
                mount_added (mount);
            }

            populate_recent ();

            update ();
        }

        void populate_recent () {
            Gtk.RecentManager recent_manager = Gtk.RecentManager.get_default ();
            List<Gtk.RecentInfo> recent_items = recent_manager.get_items ();

            unowned List<Gtk.RecentInfo> iter = recent_items;
            while (iter != null) {
                unowned List<Gtk.RecentInfo> next = iter.next;
                if (!iter.data.has_group ("baobab") || !iter.data.exists () || already_present (File.new_for_uri (iter.data.get_uri ()))) {
                    recent_items.remove_link (iter);
                }
                iter = next;
            }

            recent_items.sort ((a, b) => {
                return (int)(b.get_modified ().difference (a.get_modified ()));
            });

            unowned List<Gtk.RecentInfo> last = recent_items.nth (MAX_RECENT_LOCATIONS - 1);
            if (last != null) {
                last.next = null;
            }

            foreach (var info in recent_items) {
                locations.append (new Location.for_recent_info (info));
            }
        }

        void row_activated (Gtk.ListBoxRow row) {
            var location_widget = row as LocationRow;
            location_activated (location_widget.location);
        }

        public void update () {
            for (Gtk.Widget? child = local_list_box.get_first_child (); child != null; child = local_list_box.get_first_child ()) {
                local_list_box.remove (child);
            }

            for (Gtk.Widget? child = remote_list_box.get_first_child (); child != null; child = remote_list_box.get_first_child ()) {
                remote_list_box.remove (child);
            }

            remote_box.visible = false;

            foreach (var location in locations) {
                if (location.is_remote) {
                    remote_list_box.append (new LocationRow (location));
                    remote_box.visible = true;
                } else {
                    local_list_box.append (new LocationRow (location));
                }
            }
        }

        public void add_location (Location location) {
            if (location.file == null) {
                return;
            }

            // Add to recent files
            Gtk.RecentData data = Gtk.RecentData ();
            data.display_name = null;
            data.description = null;
            data.mime_type = "inode/directory";
            data.app_name = GLib.Environment.get_application_name ();
            data.app_exec = "%s %%u".printf (GLib.Environment.get_prgname ());
            string[] groups = new string[2];
            groups[0] = "baobab";
            groups[1] = null;
            data.groups = groups;
            Gtk.RecentManager.get_default ().add_full (location.file.get_uri (), data);

            // Reload recent locations
            clear_recent (false);
            populate_recent ();

            update ();
        }

        public void clear_recent (bool remove_from_recent_manager = true) {
            unowned List<Location> iter = locations;
            while (iter != null) {
                unowned List<Location> next = iter.next;
                if (iter.data.is_recent) {
                    try {
                        if (remove_from_recent_manager) {
                            Gtk.RecentManager.get_default ().remove_item (iter.data.file.get_uri ());
                        }
                        locations.remove_link (iter);
                    } catch (Error e) {
                        warning ("Attempting to remove an item from recent locations, but failed: %s", e.message);
                    }
                }
                iter = next;
            }

            update ();
        }
    }
}