summaryrefslogtreecommitdiff
path: root/src/gui/settingsWindow.vala
blob: 0e7af206f91e2c7fb9102fbbebda889558a4ea71 (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
/* 
Copyright (c) 2011 by Simon Schneegans

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 3 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, see <http://www.gnu.org/licenses/>. 
*/

namespace GnomePie {

/////////////////////////////////////////////////////////////////////////    
/// The settings menu of Gnome-Pie, with options for theme switching and
/// some general options.
/////////////////////////////////////////////////////////////////////////

public class SettingsWindow : GLib.Object {
    
    /////////////////////////////////////////////////////////////////////
    /// Some widgets.
    /////////////////////////////////////////////////////////////////////
    
    private Gtk.Dialog? window = null;
    private ThemeList? theme_list = null;
    private Gtk.ToggleButton? indicator = null;
    private Gtk.ToggleButton? autostart = null;
    private Gtk.ToggleButton? captions = null;
    
    /////////////////////////////////////////////////////////////////////
    /// C'tor creates, the dialog.
    /////////////////////////////////////////////////////////////////////
    
    public SettingsWindow() {
        try {
        
            Gtk.Builder builder = new Gtk.Builder();

            builder.add_from_file (Paths.ui_files + "/settings.ui");

            this.window = builder.get_object("window") as Gtk.Dialog;
            
            this.theme_list = new ThemeList();
            this.theme_list.on_select_new.connect(() => {
                this.captions.active = Config.global.show_captions;
                if (Config.global.theme.has_slice_captions) {
                    this.captions.sensitive = true;
                } else {
                    this.captions.sensitive = false;
                }
            });
            
            var scroll_area = builder.get_object("theme-scrolledwindow") as Gtk.ScrolledWindow;
                scroll_area.add(this.theme_list);
                
            (builder.get_object("close-button") as Gtk.Button).clicked.connect(on_close_button_clicked);
            
            this.autostart = (builder.get_object("autostart-checkbox") as Gtk.ToggleButton);
            this.autostart.toggled.connect(on_autostart_toggled);
            
            this.indicator = (builder.get_object("indicator-checkbox") as Gtk.ToggleButton);
            this.indicator.toggled.connect(on_indicator_toggled);
            
            this.captions = (builder.get_object("captions-checkbox") as Gtk.ToggleButton);
            this.captions.toggled.connect(on_captions_toggled);
            
            var scale_slider = (builder.get_object("scale-hscale") as Gtk.HScale);
                scale_slider.set_range(0.5, 2.0);
                scale_slider.set_increments(0.05, 0.25);
                scale_slider.set_value(Config.global.global_scale);
                
                bool changing = false;
                bool changed_again = false;

                scale_slider.value_changed.connect(() => {
                    if (!changing) {
                        changing = true;
                        Timeout.add(300, () => {
                            if (changed_again) {
                                changed_again = false;
                                return true;
                            }

                            Config.global.global_scale = scale_slider.get_value();
                            Config.global.load_themes(Config.global.theme.name);
                            changing = false;
                            return false;
                        });
                    } else {
                        changed_again = true;
                    }
                });
                
            this.window.delete_event.connect(this.window.hide_on_delete);
                
        } catch (GLib.Error e) {
            error("Could not load UI: %s\n", e.message);
        }
    }
    
    /////////////////////////////////////////////////////////////////////
    /// Sets the parent window, in order to make this window stay in
    /// front.
    /////////////////////////////////////////////////////////////////////
    
    public void set_parent(Gtk.Window parent) {
        this.window.set_transient_for(parent);
    }
    
    /////////////////////////////////////////////////////////////////////
    /// Displays the window on the screen.
    /////////////////////////////////////////////////////////////////////
    
    public void show() {
        this.indicator.active = Config.global.show_indicator;
        this.autostart.active = Config.global.auto_start;     
        this.captions.active = Config.global.show_captions;
        
        if (Config.global.theme.has_slice_captions) {
            this.captions.sensitive = true;
        } else {
            this.captions.sensitive = false;
        }
        
        this.window.show_all(); 
    }
    
    /////////////////////////////////////////////////////////////////////
    /// Called when the close button is clicked.
    /////////////////////////////////////////////////////////////////////
    
    private void on_close_button_clicked() {
        this.window.hide();
        
        Logger.stats("SETTINGS " + Config.global.theme.name + 
                     (this.indicator.active ? " INDICATOR" : "") +
                     (this.autostart.active ? " AUTOSTART" : "") +
                     (this.captions.active ? " CAPTIONS" : "") +
                     " %f".printf(Config.global.global_scale));
    }
    
    /////////////////////////////////////////////////////////////////////
    /// Creates or deletes the autostart file. This code is inspired
    /// by project synapse as well.
    /////////////////////////////////////////////////////////////////////
    
    private void on_autostart_toggled(Gtk.ToggleButton check_box) {
        bool active = check_box.active;
        if (!active && FileUtils.test(Paths.autostart, FileTest.EXISTS)) {
            // delete the autostart file
            FileUtils.remove (Paths.autostart);
        }
        else if (active && !FileUtils.test(Paths.autostart, FileTest.EXISTS)) {
            string autostart_entry = 
                "#!/usr/bin/env xdg-open\n" + 
                "[Desktop Entry]\n" +
                "Name=Gnome-Pie\n" +
                "Exec=gnome-pie\n" +
                "Encoding=UTF-8\n" +
                "Type=Application\n" +
                "X-GNOME-Autostart-enabled=true\n" +
                "Icon=gnome-pie\n";

            // create the autostart file
            string autostart_dir = GLib.Path.get_dirname(Paths.autostart);
            if (!FileUtils.test(autostart_dir, FileTest.EXISTS | FileTest.IS_DIR)) {
                DirUtils.create_with_parents(autostart_dir, 0755);
            }
            
            try {
                FileUtils.set_contents(Paths.autostart, autostart_entry);
                FileUtils.chmod(Paths.autostart, 0755);
            } catch (Error e) {
                var d = new Gtk.MessageDialog (this.window, 0, Gtk.MessageType.ERROR, Gtk.ButtonsType.CLOSE,
                                           "%s", e.message);
                d.run ();
                d.destroy ();
            }
        }
    }
    
    /////////////////////////////////////////////////////////////////////
    /// Shows or hides the indicator.
    /////////////////////////////////////////////////////////////////////
    
    private void on_indicator_toggled(Gtk.ToggleButton check_box) {
        var check = check_box as Gtk.CheckButton;
        Config.global.show_indicator = check.active;
    }
    
    /////////////////////////////////////////////////////////////////////
    /// Shows or hides the captions of Slices.
    /////////////////////////////////////////////////////////////////////
    
    private void on_captions_toggled(Gtk.ToggleButton check_box) {
        var check = check_box as Gtk.CheckButton;
        Config.global.show_captions = check.active;
    }
}

}