~mefrio-g/+junk/plank-plug

« back to all changes in this revision

Viewing changes to plank-plug.vala

  • Committer: Mario Guerriero
  • Date: 2011-12-18 13:05:03 UTC
  • Revision ID: mefrio.g@gmail.com-20111218130503-fsgdkj4nznmr93jk
added cmake and gettext support

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/***
2
 
  BEGIN LICENSE
3
 
        
4
 
  Copyright (C) 2011 Mario Guerriero <mefrio.g@gmail.com>       
5
 
  This program is free software: you can redistribute it and/or modify it       
6
 
  under the terms of the GNU Lesser General Public License version 3, as published      
7
 
  by the Free Software Foundation.
8
 
        
9
 
  This program is distributed in the hope that it will be useful, but   
10
 
  WITHOUT ANY WARRANTY; without even the implied warranties of  
11
 
  MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR    
12
 
  PURPOSE.  See the GNU General Public License for more details.
13
 
        
14
 
  You should have received a copy of the GNU General Public License along       
15
 
  with this program.  If not, see <http://www.gnu.org/licenses/>        
16
 
  
17
 
  END LICENSE   
18
 
***/
19
 
 
20
 
using Gtk;
21
 
 
22
 
public class PlankPlug : Pantheon.Switchboard.Plug {
23
 
 
24
 
    public PlankPlug () {
25
 
                
26
 
        var plug = new Plank ();
27
 
        this.add(plug);
28
 
    
29
 
    }
30
 
}
31
 
 
32
 
 
33
 
public class Settings {
34
 
    public int iconsize { get; set; default = 48; }
35
 
    public int hidemode { get; set; default = 1; }
36
 
    public int monitor { get; set; default = 0; }
37
 
}
38
 
 
39
 
enum HideType {
40
 
    NEVER,
41
 
    INTELLIGENT,
42
 
    ALWAYS
43
 
}
44
 
 
45
 
public class Plank : VBox {
46
 
    string plank_config_file;
47
 
    string config_dir;
48
 
    GLib.KeyFile plank_settings;
49
 
    Settings settings = new Settings ();
50
 
    string key_group = "PlankDockPreferences";
51
 
 
52
 
    ComboBoxText hidemode;
53
 
    ComboBoxText monitor;
54
 
    HScale iconsize;
55
 
    Label pix_label;
56
 
 
57
 
    public Plank () {
58
 
        config_dir = Environment.get_user_config_dir ();
59
 
        plank_config_file = config_dir + "/plank/dock1/settings";
60
 
 
61
 
        load ();
62
 
                
63
 
        var label = new Label ("<b>Icon size: </b>");
64
 
        label.use_markup = true;
65
 
        pix_label = new Label ("px");
66
 
        set_pixel_label ();
67
 
        iconsize = new HScale.with_range (24.0, 128.0, 1.0);
68
 
        iconsize.set_draw_value (false);
69
 
        iconsize.set_value (settings.iconsize);
70
 
        iconsize.value_changed.connect (apply);
71
 
        var hbox = new HBox (false, 4);
72
 
                hbox.pack_start (label, false, false, 25);
73
 
                hbox.pack_end (pix_label, false, false, 25);
74
 
        pack_start (hbox, false, false, 10);
75
 
        hbox = new HBox (false, 4);
76
 
                hbox.pack_start (iconsize, true, true, 45);
77
 
        pack_start (hbox, false, true, 0);
78
 
        
79
 
        label = new Label ("<b>Hide mode: </b>");
80
 
        label.use_markup = true;
81
 
        hidemode = new ComboBoxText ();
82
 
        hidemode.append_text ("Never");
83
 
        hidemode.append_text ("Intelligent");
84
 
        hidemode.append_text ("Always");
85
 
        hidemode.set_active (settings.hidemode);
86
 
        hidemode.changed.connect (apply);
87
 
        hbox = new HBox (false, 4);
88
 
                hbox.pack_start (label, false, false, 25);
89
 
        pack_start (hbox, false, false, 10);
90
 
        hbox = new HBox (false, 4);
91
 
                hbox.pack_start (hidemode, false, false, 45);
92
 
        pack_start (hbox, false, true, 0);
93
 
        
94
 
        label = new Label ("<b>Monitor: </b>");
95
 
        label.use_markup = true;
96
 
        monitor = new ComboBoxText ();
97
 
                monitor.append_text ("0");
98
 
                monitor.append_text ("1");
99
 
                monitor.append_text ("2");
100
 
                monitor.set_active (settings.monitor);
101
 
                monitor.changed.connect (apply);
102
 
        hbox = new HBox (false, 4);
103
 
                hbox.pack_start (label, false, false, 25);
104
 
        pack_start (hbox, false, false, 10);
105
 
        hbox = new HBox (false, 4);
106
 
                hbox.pack_start (monitor, false, false, 45);
107
 
        pack_start (hbox, false, true, 0);
108
 
 
109
 
                stdout.printf ("\n\n\n\n");
110
 
        show_all ();
111
 
    }
112
 
 
113
 
    void apply () {
114
 
        settings.iconsize = (int) iconsize.get_value ();
115
 
        settings.hidemode = hidemode.get_active ();
116
 
        settings.monitor = monitor.get_active ();
117
 
 
118
 
        plank_settings.set_integer (key_group, "IconSize", settings.iconsize);
119
 
        plank_settings.set_integer (key_group, "HideMode", settings.hidemode);
120
 
        plank_settings.set_integer (key_group, "Monitor", settings.monitor);
121
 
                
122
 
                set_pixel_label ();
123
 
                
124
 
        try {
125
 
            write_plank_settings ();
126
 
        } catch (GLib.Error error) {
127
 
            GLib.critical ("Failed to save \"%s\": %s", plank_config_file, error.message);
128
 
        }
129
 
 
130
 
    }
131
 
 
132
 
    void write_plank_settings () throws Error {
133
 
        string config_path = Environment.get_user_config_dir () + "/plank/dock1";
134
 
        if (DirUtils.create_with_parents (config_path, 0700) != 0)
135
 
            throw new GLib.FileError.FAILED ("Config folder couldn't be created.");
136
 
        File f = File.new_for_path(plank_config_file);
137
 
        if (f.query_exists(null)) {
138
 
            f.delete(null);
139
 
        }
140
 
        var f_stream = f.create(FileCreateFlags.REPLACE_DESTINATION, null);
141
 
        var d_stream = new DataOutputStream(f_stream);
142
 
        d_stream.put_string(plank_settings.to_data());
143
 
        
144
 
        set_pixel_label ();
145
 
    }
146
 
 
147
 
    void load () {
148
 
        /* Load plank settings */
149
 
        plank_settings = new GLib.KeyFile ();
150
 
        try {
151
 
            plank_settings.load_from_file (plank_config_file, 0);
152
 
        } catch (GLib.Error error) {
153
 
            GLib.debug ("Failed to load \"%s\": %s", plank_config_file, error.message);
154
 
        }
155
 
 
156
 
        try {
157
 
            if (plank_settings.has_key (key_group, "IconSize"))
158
 
                settings.iconsize = plank_settings.get_integer (key_group, "IconSize");
159
 
            if (plank_settings.has_key (key_group, "HideMode"))
160
 
                settings.hidemode = plank_settings.get_integer (key_group, "HideMode");
161
 
            if (plank_settings.has_key (key_group, "Monitor"))
162
 
                settings.monitor = plank_settings.get_integer (key_group, "Monitor");
163
 
        } catch (GLib.KeyFileError error) {
164
 
            GLib.debug ("Failed to load some preferences from \"%s\": %s", plank_config_file, error.message);
165
 
        }
166
 
        
167
 
        set_pixel_label ();
168
 
    }
169
 
    
170
 
    void set_pixel_label () {
171
 
                pix_label.set_text (settings.iconsize.to_string () + " px");
172
 
        }
173
 
 
174
 
}
175
 
 
176
 
public static int main (string[] args) {
177
 
 
178
 
    Gtk.init (ref args);
179
 
    var plug = new PlankPlug ();
180
 
    plug.register ("Plank");
181
 
    plug.show_all ();
182
 
    Gtk.main ();
183
 
    return 0;
184
 
}