~elementary-pantheon/switchboard-plug-startup-applications/trunk

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
/***
    Copyright (C) 2013 Julien Spautz <spautz.julien@gmail.com>

    This program or library is free software; you can redistribute it
    and/or modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 3 of the License, or (at your option) any later version.

    This library 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
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General
    Public License along with this library; if not, write to the
    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301 USA.
***/

class Pantheon.Startup.Widgets.AppRow : Gtk.Box {

    Gtk.Button delete_button;
    Gtk.Label label;
    Gtk.Switch active_switch;
    Gtk.Image image;

    public Backend.KeyFile app { get; construct; }

    public signal void deleted ();

    public AppRow (Backend.KeyFile app) {
        Object (app: app);
        setup ();
        connect_signals ();
        on_active_changed ();
    }

    void setup () {
        orientation = Gtk.Orientation.HORIZONTAL;

        var markup = app.create_markup ();
        var icon = app.create_icon ();

        margin = 6;
        spacing = 12;

        active_switch = new Gtk.Switch ();
        active_switch.active = app.active;
        add (active_switch);

        image = new Gtk.Image.from_pixbuf (icon);
        add (image);

        label = new Gtk.Label (markup);
        label.expand = true;
        label.use_markup = true;
        label.halign = Gtk.Align.START;
        label.ellipsize = Pango.EllipsizeMode.END;
        label.sensitive = app.active;
        add (label);

        delete_button = new Gtk.Button.with_label (_("Delete"));
        delete_button.get_style_context ().add_class ("destructive-action");
        delete_button.no_show_all = true;
        delete_button.vexpand = false;
        var button_box = new Gtk.ButtonBox (Gtk.Orientation.HORIZONTAL);
        button_box.add (delete_button);
        add (button_box);

        show_all ();
    }

    void connect_signals () {
        delete_button.clicked.connect (on_delete_clicked);
        active_switch.notify["active"].connect (on_active_changed);
    }

    void on_delete_clicked () {
        app.delete_file ();
        deleted ();
    }

    void on_active_changed () {
        var active = active_switch.active;
        label.sensitive = active;
        app.active = active;
        app.write_to_file ();
    }

    public void show_delete (bool show) {
        delete_button.no_show_all = !show;
        delete_button.visible = show;
    }
}

class Pantheon.Startup.Widgets.NewAppRow : Gtk.Box {

    Dialogs.AppChooser app_chooser;
    Gtk.Button add_button;

    public signal void app_added (Backend.KeyFile app);

    public NewAppRow () {
        setup ();
        connect_signals ();
    }

    void setup () {
        orientation = Gtk.Orientation.HORIZONTAL;

        margin = 6;
        spacing = 12;

        var sw = new Gtk.Switch ();
        sw.opacity = 0.0;
        add (sw);

        add_button = new Gtk.Button.from_icon_name ("add", Gtk.IconSize.DIALOG);
        add_button.relief = Gtk.ReliefStyle.NONE;
        add (add_button);

        var label = new Gtk.Label ("<i>" + _("Add Startup App") + "</i>");
        label.expand = true;
        label.use_markup = true;
        label.halign = Gtk.Align.START;
        label.ellipsize = Pango.EllipsizeMode.END;
        add (label);

        app_chooser = new Dialogs.AppChooser (add_button);
        app_chooser.modal = true;
    }

    void connect_signals () {
        app_chooser.app_chosen.connect ((key_file) => app_added (key_file));
        add_button.clicked.connect (app_chooser.show_all);
    }
}

class Pantheon.Startup.Widgets.List : Gtk.ListBox {

    NewAppRow new_app_row;

    public string search_string { get; set; }

    public signal void selected (Backend.KeyFile app);

    public List () {
        setup_gui ();
        connect_signals ();
    }

    void setup_gui () {
        load_startup_apps ();
        add_new_app_row ();
        set_sort_func (sort_function);
    }

    void connect_signals () {
        row_selected.connect (show_delete_button_on_select);
        new_app_row.app_added.connect (add_app);
    }

    void load_startup_apps () {
        foreach (var path in get_auto_start_files ()) {
            var app = Backend.KeyFileFactory.get_or_create (path);
            add_app (app);
        }
    }

    void add_new_app_row () {
        new_app_row = new NewAppRow ();
        prepend (new_app_row);
    }

    void show_delete_button_on_select (Gtk.ListBoxRow? selected) {
        @foreach ((row) => show_delete (row, false));
        show_delete (selected, true);
    }

    void show_delete (Gtk.Widget? row, bool show) {
        var list_box_row = row as Gtk.ListBoxRow;
        if (list_box_row == null)
            return;

        var app_row = list_box_row.get_child () as AppRow;
        if (app_row == null)
            return;

        app_row.show_delete (show);
    }

    int sort_function (Gtk.ListBoxRow list_box_row_1,
                       Gtk.ListBoxRow list_box_row_2) {
        var row_1 = list_box_row_1.get_child ();
        var row_2 = list_box_row_2.get_child ();

        if (row_1 is NewAppRow)
            return +1;
        if (row_2 is NewAppRow)
            return -1;

        var name_1 = (row_1 as AppRow).app.name;
        var name_2 = (row_2 as AppRow).app.name;
        return name_1.collate (name_2);
    }

    string[] get_auto_start_files () {
        var startup_dir = Utils.get_user_startup_dir ();
        var enumerator = new Backend.DesktopFileEnumerator (startup_dir);
        return enumerator.get_desktop_files ();
    }

    public void add_app (Backend.KeyFile app) {
        var row = new AppRow (app);
        prepend (row);
        row.deleted.connect (() => remove (row.parent));
    }
}