~indicator-multiload/indicator-multiload/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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/******************************************************************************
 * Copyright (C) 2011  Michael Hofmann <mh21@piware.de>                       *
 *                                                                            *
 * 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, write to the Free Software Foundation, Inc.,    *
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.                *
 ******************************************************************************/

public bool get_settings_color(Value value, Variant variant, void *user_data)
{
    Gdk.Color color;
    if (Gdk.Color.parse(variant.get_string(), out color)) {
        value.set_boxed(&color);
        return true;
    }
    return false;
}

public Variant set_settings_color(Value value, VariantType expected_type, void *user_data)
{
    Gdk.Color color = *(Gdk.Color*)value.get_boxed();
    return new Variant.string(color.to_string());
}

public class Main : Application {
    private Gtk.Builder builder;
    private MultiLoadIndicator multi;
    private FixedGSettings.Settings datasettings;
    private FixedGSettings.Settings prefsettings;
    private Gtk.Window preferences;
    private Gtk.AboutDialog about;
    private Gtk.Menu menu;
    private Gtk.CheckButton[] checkbuttons;
    private static const string autostartkey = "X-GNOME-Autostart-enabled";
    private static const string desktopfilename = "indicator-multiload.desktop";
    private string autostartfile = Path.build_filename(Environment.get_user_config_dir(),
            "autostart", desktopfilename);
    private string applicationfile = Path.build_filename("applications",
            desktopfilename);

    public bool autostart {
        get {
            KeyFile file = new KeyFile();
            try {
                file.load_from_file(this.autostartfile, KeyFileFlags.NONE);
            } catch (Error e) {
                return false;
            }
            try {
                return file.get_boolean(KeyFileDesktop.GROUP, autostartkey);
            } catch (Error e) {
                return true;
            }
        }

        set {
            KeyFile file = new KeyFile();
            try {
                file.load_from_file(this.autostartfile, KeyFileFlags.KEEP_COMMENTS |
                        KeyFileFlags.KEEP_TRANSLATIONS);
            } catch (Error e) {
                try {
                    file.load_from_data_dirs(this.applicationfile, null, KeyFileFlags.KEEP_COMMENTS |
                            KeyFileFlags.KEEP_TRANSLATIONS);
                } catch (Error e) {
                    // TODO: nicer defaults: icon, name, description
                    file.set_string(KeyFileDesktop.GROUP, KeyFileDesktop.KEY_TYPE, "Application");
                    file.set_string(KeyFileDesktop.GROUP, KeyFileDesktop.KEY_NAME, "indicator-multiload");
                    file.set_string(KeyFileDesktop.GROUP, KeyFileDesktop.KEY_EXEC, "indicator-multiload");
                }
            }
            file.set_boolean(KeyFileDesktop.GROUP, autostartkey, value);
            try {
                FileUtils.set_contents(this.autostartfile, file.to_data());
            } catch (Error e) {
                stderr.printf("Could not create autostart desktop file: %s\n", e.message);
            }
        }
    }

    [CCode (instance_pos = -1)]
    public void on_sysmon_activate(Gtk.MenuItem source) {
        var settings = new FixedGSettings.Settings("de.mh21.indicator.multiload");
        var sysmon = settings.get_string("system-monitor");
        if (sysmon.length == 0)
            sysmon = "gnome-system-monitor.desktop";
        var info = new DesktopAppInfo(sysmon);
        var screen = this.menu.get_screen(); // TODO: maybe this needs to be the default?
        if (info != null) {
            var context = new Gdk.AppLaunchContext();
            context.set_timestamp(Gtk.get_current_event_time());
            context.set_screen(screen);
            try {
                info.launch(null, context); // TODO: launches in background
            } catch (Error e) {
                stderr.printf("Could not launch system monitor: %s\n", e.message);
            }
        } else {
            try {
                Gdk.spawn_command_line_on_screen(screen, "gnome-system-monitor");
            } catch (Error e) {
                stderr.printf("Could not launch system monitor: %s\n", e.message);
            }
        }
    }

    [CCode (instance_pos = -1)]
    public void on_preferences_activate(Gtk.MenuItem source) {
        this.preferences.show_all();
    }

    [CCode (instance_pos = -1)]
    public void on_about_activate(Gtk.MenuItem source) {
        this.about.show_all();
    }

    [CCode (instance_pos = -1)]
    public void on_quit_activate(Gtk.MenuItem source) {
        this.release();
    }

    [CCode (instance_pos = -1)]
    public void on_checkbutton_toggled(Gtk.CheckButton source) {
        uint count = 0;
        foreach (var checkbutton in this.checkbuttons)
            count += (uint)checkbutton.active;
        if (count == 1)
            foreach (var checkbutton in this.checkbuttons)
                checkbutton.sensitive = !checkbutton.active;
        else
            foreach (var checkbutton in this.checkbuttons)
                checkbutton.sensitive = true;
    }

    public Main(string app_id, ApplicationFlags flags) {
        Object(application_id: app_id, flags: flags);
    }

    public override void activate() {
        // all the work is done in startup
    }

    public override void startup() {
        this.builder = new Gtk.Builder();

        string[] datadirs = { Config.PACKAGE_DATA_DIR };
        foreach (var datadir in Environment.get_system_data_dirs())
            datadirs += Path.build_filename(datadir, Config.PACKAGE_NAME);
        bool found = false;
        foreach (var datadir in datadirs) {
            try {
                this.builder.add_from_file(Path.build_filename(datadir, "preferences.ui"));
                found = true;
                break;
            } catch (Error e) {
                stderr.printf("Could not initialize indicator gui from %s: %s\n", datadir, e.message);
            }
        }
        if (!found)
            return;

        this.builder.connect_signals(this);

        this.preferences = this.builder.get_object("preferencesdialog") as Gtk.Window;
        this.about = this.builder.get_object("aboutdialog") as Gtk.AboutDialog;

        this.multi = new MultiLoadIndicator();
        this.multi.add_icon_data(new CpuIconData());
        this.multi.add_icon_data(new MemIconData());
        this.multi.add_icon_data(new NetIconData());
        this.multi.add_icon_data(new SwapIconData());
        this.multi.add_icon_data(new LoadIconData());
        this.multi.add_icon_data(new DiskIconData());

        this.datasettings = new FixedGSettings.Settings("de.mh21.indicator.multiload");
        foreach (var icon_data in this.multi.icon_datas) {
            var id = icon_data.id;
            var length = icon_data.traces.length;
            for (uint j = 0, jsize = length; j < jsize; ++j)
                this.datasettings.bind_with_mapping(@"$id-color$j",
                        icon_data.traces[j], "color",
                        SettingsBindFlags.DEFAULT, get_settings_color, set_settings_color, null, () => {});
            this.datasettings.bind_with_mapping(@"$id-color$length",
                    icon_data, "color",
                    SettingsBindFlags.DEFAULT, get_settings_color, set_settings_color, null, () => {});
            this.datasettings.bind(@"view-$id",
                    icon_data, "enabled",
                    SettingsBindFlags.DEFAULT);
            this.datasettings.bind(@"$id-alpha$length",
                    icon_data, "alpha",
                    SettingsBindFlags.DEFAULT);
        }
        this.datasettings.bind("size",
                this.multi, "size",
                SettingsBindFlags.DEFAULT);
        this.datasettings.bind("speed",
                this.multi, "speed",
                SettingsBindFlags.DEFAULT);
        this.datasettings.bind("autostart",
                this, "autostart",
                SettingsBindFlags.DEFAULT);

        this.prefsettings = new FixedGSettings.Settings("de.mh21.indicator.multiload");
        foreach (var icon_data in this.multi.icon_datas) {
            var id = icon_data.id;
            var length = icon_data.traces.length;
            for (uint j = 0, jsize = length; j < jsize; ++j)
                this.prefsettings.bind_with_mapping(@"$id-color$j",
                        this.builder.get_object(@"$(id)_color$j"), "color",
                        SettingsBindFlags.DEFAULT, get_settings_color, set_settings_color, null, () => {});
            this.prefsettings.bind_with_mapping(@"$id-color$length",
                    this.builder.get_object(@"$(id)_color$length"), "color",
                    SettingsBindFlags.DEFAULT, get_settings_color, set_settings_color, null, () => {});
            this.prefsettings.bind(@"view-$id",
                    this.builder.get_object(@"view_$id"), "active",
                    SettingsBindFlags.DEFAULT);
            this.prefsettings.bind(@"$id-alpha$length",
                    this.builder.get_object(@"$(id)_color$length"), "alpha",
                    SettingsBindFlags.DEFAULT);
        }

        this.prefsettings.bind("size",
                this.builder.get_object("size"), "value",
                SettingsBindFlags.DEFAULT);
        this.prefsettings.bind("speed",
                this.builder.get_object("speed"), "value",
                SettingsBindFlags.DEFAULT);
        this.prefsettings.bind("autostart",
                this.builder.get_object("autostart"), "active",
                SettingsBindFlags.DEFAULT);

        foreach (var icon_data in this.multi.icon_datas)
            this.checkbuttons += this.builder.get_object(@"view_$(icon_data.id)") as Gtk.CheckButton;

        this.menu = this.builder.get_object("menu") as Gtk.Menu;
        this.multi.menu = this.menu;

        this.hold();

        Gdk.notify_startup_complete();
    }

    ~Main() {
        if (multi != null)
            this.multi.destroy();
    }

    public static int main(string[] args) {
        Intl.bindtextdomain(Config.GETTEXT_PACKAGE, Config.PACKAGE_LOCALE_DIR);
        Intl.bind_textdomain_codeset(Config.GETTEXT_PACKAGE, "UTF-8");
        Intl.textdomain(Config.GETTEXT_PACKAGE);

        Gtk.init(ref args);
        Gtk.Window.set_default_icon_name("utilities-system-monitor");

        return new Main("de.mh21.indicator.multiload", ApplicationFlags.FLAGS_NONE).run(args);
    }
}