1
/******************************************************************************
2
* Copyright (C) 2011 Michael Hofmann <mh21@piware.de> *
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. *
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. *
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
******************************************************************************/
19
public bool get_settings_color(Value value, Variant variant, void *user_data)
22
if (Gdk.Color.parse(variant.get_string(), out color)) {
23
value.set_boxed(&color);
29
public Variant set_settings_color(Value value, VariantType expected_type, void *user_data)
31
Gdk.Color color = *(Gdk.Color*)value.get_boxed();
32
return new Variant.string(color.to_string());
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
45
public bool autostart {
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(),
52
KeyFile file = new KeyFile();
54
file.load_from_file(configautostartfile, KeyFileFlags.NONE);
59
return file.get_boolean(KeyFileDesktop.GROUP, autostartkey);
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(),
71
KeyFile file = new KeyFile();
73
file.load_from_file(configautostartfile, KeyFileFlags.KEEP_COMMENTS |
74
KeyFileFlags.KEEP_TRANSLATIONS);
77
file.load_from_data_dirs(autostartfile, null, KeyFileFlags.KEEP_COMMENTS |
78
KeyFileFlags.KEEP_TRANSLATIONS);
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");
86
file.set_boolean(KeyFileDesktop.GROUP, autostartkey, value);
88
FileUtils.set_contents(configautostartfile, file.to_data());
90
stderr.printf("Could not create autostart desktop file: %s\n", e.message);
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?
104
var context = new Gdk.AppLaunchContext();
105
context.set_timestamp(Gtk.get_current_event_time());
106
context.set_screen(screen);
108
info.launch(null, context); // TODO: launches in background
110
stderr.printf("Could not launch system monitor: %s\n", e.message);
114
Gdk.spawn_command_line_on_screen(screen, "gnome-system-monitor");
116
stderr.printf("Could not launch system monitor: %s\n", e.message);
121
[CCode (instance_pos = -1)]
122
public void on_preferences_activate(Gtk.MenuItem source) {
123
this.preferences.show_all();
126
[CCode (instance_pos = -1)]
127
public void on_about_activate(Gtk.MenuItem source) {
128
this.about.show_all();
131
[CCode (instance_pos = -1)]
132
public void on_quit_activate(Gtk.MenuItem source) {
136
[CCode (instance_pos = -1)]
137
public void on_checkbutton_toggled(Gtk.CheckButton source) {
139
foreach (unowned Gtk.CheckButton checkbutton in this.checkbuttons)
140
count += (uint)checkbutton.active;
142
foreach (unowned Gtk.CheckButton checkbutton in this.checkbuttons)
143
checkbutton.sensitive = !checkbutton.active;
145
foreach (unowned Gtk.CheckButton checkbutton in this.checkbuttons)
146
checkbutton.sensitive = true;
149
public Main(string app_id, ApplicationFlags flags) {
150
Object(application_id: app_id, flags: flags);
153
public override void activate() {
154
// all the work is done in startup
157
public override void startup() {
158
this.builder = new Gtk.Builder();
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);
164
foreach (var datadir in datadirs) {
166
this.builder.add_from_file(Path.build_filename(datadir, "preferences.ui"));
170
stderr.printf("Could not initialize indicator gui from %s: %s\n", datadir, e.message);
176
this.builder.connect_signals(this);
178
this.preferences = this.builder.get_object("preferencesdialog") as Gtk.Window;
179
this.about = this.builder.get_object("aboutdialog") as Gtk.AboutDialog;
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());
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",
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",
205
SettingsBindFlags.DEFAULT);
207
this.datasettings.bind("size",
209
SettingsBindFlags.DEFAULT);
210
this.datasettings.bind("speed",
212
SettingsBindFlags.DEFAULT);
213
this.datasettings.bind("autostart",
215
SettingsBindFlags.DEFAULT);
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);
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);
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;
249
this.menu = this.builder.get_object("menu") as Gtk.Menu;
250
this.multi.menu = this.menu;
257
this.multi.destroy();
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);
266
Gtk.Window.set_default_icon_name("utilities-system-monitor");
268
return new Main("de.mh21.indicator.multiload", ApplicationFlags.FLAGS_NONE).run(args);