~indicator-multiload/indicator-multiload/trunk

« back to all changes in this revision

Viewing changes to src/main.vala

  • Committer: Michael Hofmann
  • Date: 2011-05-02 06:49:06 UTC
  • Revision ID: mh21@piware.de-20110502064906-as0r6s80dtg2swdv
Initial public version.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/******************************************************************************
 
2
 * Copyright (C) 2011  Michael Hofmann <mh21@piware.de>                       *
 
3
 *                                                                            *
 
4
 * This program is free software; you can redistribute it and/or modify       *
 
5
 * it under the terms of the GNU General Public License as published by       *
 
6
 * the Free Software Foundation; either version 3 of the License, or          *
 
7
 * (at your option) any later version.                                        *
 
8
 *                                                                            *
 
9
 * This program is distributed in the hope that it will be useful,            *
 
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
 
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
 
12
 * 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, write to the Free Software Foundation, Inc.,    *
 
16
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.                *
 
17
 ******************************************************************************/
 
18
 
 
19
public bool get_settings_color(Value value, Variant variant, void *user_data)
 
20
{
 
21
    Gdk.Color color;
 
22
    if (Gdk.Color.parse(variant.get_string(), out color)) {
 
23
        value.set_boxed(&color);
 
24
        return true;
 
25
    }
 
26
    return false;
 
27
}
 
28
 
 
29
public Variant set_settings_color(Value value, VariantType expected_type, void *user_data)
 
30
{
 
31
    Gdk.Color color = *(Gdk.Color*)value.get_boxed();
 
32
    return new Variant.string(color.to_string());
 
33
}
 
34
 
 
35
public class Main : Application {
 
36
    private Gtk.Builder builder;
 
37
    private MultiLoadIndicator multi;
 
38
    private FixedGSettings.Settings datasettings;
 
39
    private FixedGSettings.Settings prefsettings;
 
40
    private unowned Gtk.Window preferences;
 
41
    private unowned Gtk.AboutDialog about;
 
42
    private unowned Gtk.Menu menu;
 
43
    private Gtk.CheckButton*[] checkbuttons; // unowned
 
44
 
 
45
    public bool autostart {
 
46
        get {
 
47
            var autostartkey = "X-GNOME-Autostart-enabled";
 
48
            var autostartfile = Path.build_filename("autostart",
 
49
                    "indicator-multiload.desktop");
 
50
            var configautostartfile = Path.build_filename(Environment.get_user_config_dir(),
 
51
                    autostartfile);
 
52
            KeyFile file = new KeyFile();
 
53
            try {
 
54
                file.load_from_file(configautostartfile, KeyFileFlags.NONE);
 
55
            } catch (Error e) {
 
56
                return false;
 
57
            }
 
58
            try {
 
59
                return file.get_boolean(KeyFileDesktop.GROUP, autostartkey);
 
60
            } catch (Error e) {
 
61
                return true;
 
62
            }
 
63
        }
 
64
 
 
65
        set {
 
66
            var autostartkey = "X-GNOME-Autostart-enabled";
 
67
            var autostartfile = Path.build_filename("autostart",
 
68
                    "indicator-multiload.desktop");
 
69
            var configautostartfile = Path.build_filename(Environment.get_user_config_dir(),
 
70
                    autostartfile);
 
71
            KeyFile file = new KeyFile();
 
72
            try {
 
73
                file.load_from_file(configautostartfile, KeyFileFlags.KEEP_COMMENTS |
 
74
                        KeyFileFlags.KEEP_TRANSLATIONS);
 
75
            } catch (Error e) {
 
76
                try {
 
77
                    file.load_from_data_dirs(autostartfile, null, KeyFileFlags.KEEP_COMMENTS |
 
78
                            KeyFileFlags.KEEP_TRANSLATIONS);
 
79
                } catch (Error e) {
 
80
                    // TODO: nicer defaults: icon, name, description
 
81
                    file.set_string(KeyFileDesktop.GROUP, KeyFileDesktop.KEY_TYPE, "Application");
 
82
                    file.set_string(KeyFileDesktop.GROUP, KeyFileDesktop.KEY_NAME, "indicator-multiload");
 
83
                    file.set_string(KeyFileDesktop.GROUP, KeyFileDesktop.KEY_EXEC, "indicator-multiload");
 
84
                }
 
85
            }
 
86
            file.set_boolean(KeyFileDesktop.GROUP, autostartkey, value);
 
87
            try {
 
88
                FileUtils.set_contents(configautostartfile, file.to_data());
 
89
            } catch (Error e) {
 
90
                stderr.printf("Could not create autostart desktop file: %s\n", e.message);
 
91
            }
 
92
        }
 
93
    }
 
94
 
 
95
    [CCode (instance_pos = -1)]
 
96
    public void on_sysmon_activate(Gtk.MenuItem source) {
 
97
        var settings = new FixedGSettings.Settings("de.mh21.indicator.multiload");
 
98
        var sysmon = settings.get_string("system-monitor");
 
99
        if (sysmon.length == 0)
 
100
            sysmon = "gnome-system-monitor.desktop";
 
101
        var info = new DesktopAppInfo(sysmon);
 
102
        var screen = this.menu.get_screen(); // TODO: maybe this needs to be the default?
 
103
        if (info != null) {
 
104
            var context = new Gdk.AppLaunchContext();
 
105
            context.set_timestamp(Gtk.get_current_event_time());
 
106
            context.set_screen(screen);
 
107
            try {
 
108
                info.launch(null, context); // TODO: launches in background
 
109
            } catch (Error e) {
 
110
                stderr.printf("Could not launch system monitor: %s\n", e.message);
 
111
            }
 
112
        } else {
 
113
            try {
 
114
                Gdk.spawn_command_line_on_screen(screen, "gnome-system-monitor");
 
115
            } catch (Error e) {
 
116
                stderr.printf("Could not launch system monitor: %s\n", e.message);
 
117
            }
 
118
        }
 
119
    }
 
120
 
 
121
    [CCode (instance_pos = -1)]
 
122
    public void on_preferences_activate(Gtk.MenuItem source) {
 
123
        this.preferences.show_all();
 
124
    }
 
125
 
 
126
    [CCode (instance_pos = -1)]
 
127
    public void on_about_activate(Gtk.MenuItem source) {
 
128
        this.about.show_all();
 
129
    }
 
130
 
 
131
    [CCode (instance_pos = -1)]
 
132
    public void on_quit_activate(Gtk.MenuItem source) {
 
133
        this.release();
 
134
    }
 
135
 
 
136
    [CCode (instance_pos = -1)]
 
137
    public void on_checkbutton_toggled(Gtk.CheckButton source) {
 
138
        uint count = 0;
 
139
        foreach (unowned Gtk.CheckButton checkbutton in this.checkbuttons)
 
140
            count += (uint)checkbutton.active;
 
141
        if (count == 1)
 
142
            foreach (unowned Gtk.CheckButton checkbutton in this.checkbuttons)
 
143
                checkbutton.sensitive = !checkbutton.active;
 
144
        else
 
145
            foreach (unowned Gtk.CheckButton checkbutton in this.checkbuttons)
 
146
                checkbutton.sensitive = true;
 
147
    }
 
148
 
 
149
    public Main(string app_id, ApplicationFlags flags) {
 
150
        Object(application_id: app_id, flags: flags);
 
151
    }
 
152
 
 
153
    public override void activate() {
 
154
        // all the work is done in startup
 
155
    }
 
156
 
 
157
    public override void startup() {
 
158
        this.builder = new Gtk.Builder();
 
159
 
 
160
        string[] datadirs = { Config.PACKAGE_DATA_DIR };
 
161
        foreach (var datadir in Environment.get_system_data_dirs())
 
162
            datadirs += Path.build_filename(datadir, Config.PACKAGE_NAME);
 
163
        bool found = false;
 
164
        foreach (var datadir in datadirs) {
 
165
            try {
 
166
                this.builder.add_from_file(Path.build_filename(datadir, "preferences.ui"));
 
167
                found = true;
 
168
                break;
 
169
            } catch (Error e) {
 
170
                stderr.printf("Could not initialize indicator gui from %s: %s\n", datadir, e.message);
 
171
            }
 
172
        }
 
173
        if (!found)
 
174
            return;
 
175
 
 
176
        this.builder.connect_signals(this);
 
177
 
 
178
        this.preferences = this.builder.get_object("preferencesdialog") as Gtk.Window;
 
179
        this.about = this.builder.get_object("aboutdialog") as Gtk.AboutDialog;
 
180
 
 
181
        this.multi = new MultiLoadIndicator();
 
182
        this.multi.add_icon_data(new CpuIconData());
 
183
        this.multi.add_icon_data(new MemIconData());
 
184
        this.multi.add_icon_data(new NetIconData());
 
185
        this.multi.add_icon_data(new SwapIconData());
 
186
        this.multi.add_icon_data(new LoadIconData());
 
187
        this.multi.add_icon_data(new DiskIconData());
 
188
 
 
189
        this.datasettings = new FixedGSettings.Settings("de.mh21.indicator.multiload");
 
190
        foreach (unowned IconData icon_data in this.multi.icon_datas) {
 
191
            var id = icon_data.id;
 
192
            var length = icon_data.length;
 
193
            for (uint j = 0, jsize = length; j < jsize; ++j)
 
194
                this.datasettings.bind_with_mapping(@"$id-color$j",
 
195
                        icon_data.trace(j), "color",
 
196
                        SettingsBindFlags.DEFAULT, get_settings_color, set_settings_color, null, () => {});
 
197
            this.datasettings.bind_with_mapping(@"$id-color$length",
 
198
                    icon_data, "color",
 
199
                    SettingsBindFlags.DEFAULT, get_settings_color, set_settings_color, null, () => {});
 
200
            this.datasettings.bind(@"view-$id",
 
201
                    icon_data, "enabled",
 
202
                    SettingsBindFlags.DEFAULT);
 
203
            this.datasettings.bind(@"$id-alpha$length",
 
204
                    icon_data, "alpha",
 
205
                    SettingsBindFlags.DEFAULT);
 
206
        }
 
207
        this.datasettings.bind("size",
 
208
                this.multi, "size",
 
209
                SettingsBindFlags.DEFAULT);
 
210
        this.datasettings.bind("speed",
 
211
                this.multi, "speed",
 
212
                SettingsBindFlags.DEFAULT);
 
213
        this.datasettings.bind("autostart",
 
214
                this, "autostart",
 
215
                SettingsBindFlags.DEFAULT);
 
216
 
 
217
        this.prefsettings = new FixedGSettings.Settings("de.mh21.indicator.multiload");
 
218
        foreach (unowned IconData icon_data in this.multi.icon_datas) {
 
219
            var id = icon_data.id;
 
220
            var length = icon_data.length;
 
221
            for (uint j = 0, jsize = length; j < jsize; ++j)
 
222
                this.prefsettings.bind_with_mapping(@"$id-color$j",
 
223
                        this.builder.get_object(@"$(id)_color$j"), "color",
 
224
                        SettingsBindFlags.DEFAULT, get_settings_color, set_settings_color, null, () => {});
 
225
            this.prefsettings.bind_with_mapping(@"$id-color$length",
 
226
                    this.builder.get_object(@"$(id)_color$length"), "color",
 
227
                    SettingsBindFlags.DEFAULT, get_settings_color, set_settings_color, null, () => {});
 
228
            this.prefsettings.bind(@"view-$id",
 
229
                    this.builder.get_object(@"view_$id"), "active",
 
230
                    SettingsBindFlags.DEFAULT);
 
231
            this.prefsettings.bind(@"$id-alpha$length",
 
232
                    this.builder.get_object(@"$(id)_color$length"), "alpha",
 
233
                    SettingsBindFlags.DEFAULT);
 
234
        }
 
235
 
 
236
        this.prefsettings.bind("size",
 
237
                this.builder.get_object("size"), "value",
 
238
                SettingsBindFlags.DEFAULT);
 
239
        this.prefsettings.bind("speed",
 
240
                this.builder.get_object("speed"), "value",
 
241
                SettingsBindFlags.DEFAULT);
 
242
        this.prefsettings.bind("autostart",
 
243
                this.builder.get_object("autostart"), "active",
 
244
                SettingsBindFlags.DEFAULT);
 
245
 
 
246
        foreach (unowned IconData icon_data in this.multi.icon_datas)
 
247
            this.checkbuttons += this.builder.get_object(@"view_$(icon_data.id)") as Gtk.CheckButton;
 
248
 
 
249
        this.menu = this.builder.get_object("menu") as Gtk.Menu;
 
250
        this.multi.menu = this.menu;
 
251
 
 
252
        this.hold();
 
253
    }
 
254
 
 
255
    ~Main() {
 
256
        if (multi != null)
 
257
            this.multi.destroy();
 
258
    }
 
259
 
 
260
    public static int main(string[] args) {
 
261
        Intl.bindtextdomain(Config.GETTEXT_PACKAGE, Config.PACKAGE_LOCALE_DIR);
 
262
        Intl.bind_textdomain_codeset(Config.GETTEXT_PACKAGE, "UTF-8");
 
263
        Intl.textdomain(Config.GETTEXT_PACKAGE);
 
264
 
 
265
        Gtk.init(ref args);
 
266
        Gtk.Window.set_default_icon_name("utilities-system-monitor");
 
267
 
 
268
        return new Main("de.mh21.indicator.multiload", ApplicationFlags.FLAGS_NONE).run(args);
 
269
    }
 
270
}