summaryrefslogtreecommitdiff
path: root/src/baobab-application.vala
blob: c1ba0df7fb6b0443d9b6f0c95948e4b63d2ccd36 (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
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* Baobab - disk usage analyzer
 *
 * Copyright (C) 2012  Ryan Lortie <desrt@desrt.ca>
 * Copyright (C) 2012  Paolo Borelli <pborelli@gnome.org>
 * Copyright (C) 2012  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 {

    public class Application : Adw.Application {

        private Window window;

        static bool all_file_systems;

        const OptionEntry[] option_entries = {
            { "all-file-systems", 'a', 0, OptionArg.NONE, ref all_file_systems,
              N_("Do not skip directories on different file systems. Ignored if DIRECTORY is not specified."), null },
            { "version", 'v', 0, OptionArg.NONE, null, N_("Print version information and exit"), null },
            { null }
        };

        const GLib.ActionEntry[] action_entries = {
            { "quit", on_quit_activate }
        };

        protected override void activate () {
            ensure_window ();
            window.present ();
        }

        protected override void open (File[] files, string hint) {
            ensure_window ();

            var scan_flags = all_file_systems ? ScanFlags.NONE : ScanFlags.EXCLUDE_MOUNTS;
            window.scan_directory (files[0], scan_flags);
        }

        void ensure_window () {
            if (window == null) {
                window = new Window (this);
            }
        }

        public static new Application get_default () {
            return (Application) GLib.Application.get_default ();
        }

        public GenericSet<string> get_excluded_locations () {
            var excluded_locations = new GenericSet<string> (str_hash, str_equal);

            var prefs_settings = new Settings ("org.gnome.baobab.preferences");
            foreach (var uri in prefs_settings.get_strv ("excluded-uris")) {
                excluded_locations.add (uri);
            }

            return excluded_locations;
        }

        protected override void startup () {
            base.startup ();

            set_accels_for_action ("win.show-home-page", { "<Alt>Left" });
            set_accels_for_action ("win.show-preferences", { "<Primary>comma" });
            set_accels_for_action ("win.show-primary-menu", { "F10" });
            set_accels_for_action ("win.scan-folder", { "<Primary>o" });
            set_accels_for_action ("win.reload", { "<Primary>r" });
            set_accels_for_action ("win.help", { "F1" });
            set_accels_for_action ("app.quit", { "<Primary>q" });
        }

        protected override int handle_local_options (GLib.VariantDict options) {
            if (options.contains("version")) {
                print ("%s %s\n", Environment.get_application_name (), Config.VERSION);
                return 0;
            }

            return -1; 
        }

        public Application () {
            Object (application_id: "org.gnome.baobab", flags: ApplicationFlags.HANDLES_OPEN);

            set_resource_base_path("/org/gnome/baobab/");

            add_main_option_entries (option_entries);
            set_option_context_parameter_string ("[DIRECTORY]");

            add_action_entries (action_entries, this);
        }

        void on_quit_activate () {
            quit ();
        }
    }
}