Update preferences to handle colors

This commit is contained in:
Benjamin Drieu 2021-11-17 14:44:04 +01:00
parent 1f95af7137
commit 57e67ef312
6 changed files with 94 additions and 20 deletions

View File

@ -37,6 +37,7 @@ const Me = ExtensionUtils.getCurrentExtension();
const Main = imports.ui.main; const Main = imports.ui.main;
const PanelMenu = imports.ui.panelMenu; const PanelMenu = imports.ui.panelMenu;
const PopupMenu = imports.ui.popupMenu; const PopupMenu = imports.ui.popupMenu;
const Mainloop = imports.mainloop;
const { GObject, St, Clutter, Gio } = imports.gi; const { GObject, St, Clutter, Gio } = imports.gi;
@ -91,7 +92,6 @@ class Indicator extends PanelMenu.Button {
box.add_child(PopupMenu.arrowIcon(St.Side.BOTTOM)); box.add_child(PopupMenu.arrowIcon(St.Side.BOTTOM));
// Menu // Menu
this._buttonMenu = new PopupMenu.PopupBaseMenuItem({ this._buttonMenu = new PopupMenu.PopupBaseMenuItem({
reactive: false, reactive: false,
@ -99,7 +99,6 @@ class Indicator extends PanelMenu.Button {
}); });
this.menu.addMenuItem(this._buttonMenu); this.menu.addMenuItem(this._buttonMenu);
// let item = new PopupMenu.PopupMenuItem(_('Reload')); // let item = new PopupMenu.PopupMenuItem(_('Reload'));
// item.connect('activate', () => { // item.connect('activate', () => {
// this.updateStatus ( ); // this.updateStatus ( );
@ -117,7 +116,6 @@ class Indicator extends PanelMenu.Button {
this._mainLabel = new St.Label({ style_class: 'monito-title', text: 'Monito Checker', x_expand: true }); this._mainLabel = new St.Label({ style_class: 'monito-title', text: 'Monito Checker', x_expand: true });
this._buttonMenu.actor.add_actor(this._mainLabel); this._buttonMenu.actor.add_actor(this._mainLabel);
this._prefsButton = this._createButton ( 'preferences-system-symbolic', 'Preferences', this._onPreferencesActivate ); this._prefsButton = this._createButton ( 'preferences-system-symbolic', 'Preferences', this._onPreferencesActivate );
this._buttonMenu.actor.add_child (this._prefsButton); this._buttonMenu.actor.add_child (this._prefsButton);
@ -138,6 +136,16 @@ class Indicator extends PanelMenu.Button {
_intermediate.actor.add_actor(this._box); _intermediate.actor.add_actor(this._box);
this.updateStatus ( ); this.updateStatus ( );
this.setupTimeout ( );
}
setupTimeout ( )
{
log ( 'Setting up timeout of ' + settings.get_int ( "poll-delay" ) + ' secs' );
if (this.timeout) {
Mainloop.source_remove(this.timeout);
}
this.timeout = Mainloop.timeout_add ( settings.get_int ( "poll-delay" ) * 1000, Lang.bind(this, this.updateStatus ) );
} }
initStatus ( ) { initStatus ( ) {
@ -147,7 +155,8 @@ class Indicator extends PanelMenu.Button {
'UNKNOWN': 0 }; 'UNKNOWN': 0 };
} }
updateStatus ( ) { updateStatus ( )
{
for ( let _server of Preferences.getServersList() ) for ( let _server of Preferences.getServersList() )
{ {
let _account_settings = Preferences.getAccountSettings ( _server ); let _account_settings = Preferences.getAccountSettings ( _server );
@ -176,6 +185,8 @@ class Indicator extends PanelMenu.Button {
} }
} }
} }
this.setupTimeout ( );
} }
createBin ( status, text, col ) { createBin ( status, text, col ) {

View File

@ -23,12 +23,14 @@
const Gio = imports.gi.Gio; const Gio = imports.gi.Gio;
const GLib = imports.gi.GLib; const GLib = imports.gi.GLib;
const Gtk = imports.gi.Gtk; const Gtk = imports.gi.Gtk;
const Gdk = imports.gi.Gdk;
// It's common practice to keep GNOME API and JS imports in separate blocks // It's common practice to keep GNOME API and JS imports in separate blocks
const Lang = imports.lang; const Lang = imports.lang;
const ExtensionUtils = imports.misc.extensionUtils; const ExtensionUtils = imports.misc.extensionUtils;
const Me = ExtensionUtils.getCurrentExtension(); const Me = ExtensionUtils.getCurrentExtension();
const Convenience = Me.imports.convenience; const Convenience = Me.imports.convenience;
const Mainloop = imports.mainloop;
const Gettext = imports.gettext.domain('monito'); const Gettext = imports.gettext.domain('monito');
const _ = Gettext.gettext; const _ = Gettext.gettext;
@ -46,6 +48,10 @@ const prefs = [ { type: Gtk.Entry, label: _('Name'), key: 'name' },
{ type: Gtk.Entry, label: _('Username'), key: 'username' }, { type: Gtk.Entry, label: _('Username'), key: 'username' },
{ type: Gtk.Entry, label: _('Password'), key: 'password' }, { type: Gtk.Entry, label: _('Password'), key: 'password' },
{ type: Gtk.Entry, label: _('URL CGI'), key: 'urlcgi' }, { type: Gtk.Entry, label: _('URL CGI'), key: 'urlcgi' },
{ type: Gtk.ColorButton, label: _('OK color'), key: 'ok-color', align: Gtk.Align.START },
{ type: Gtk.ColorButton, label: _('Warning color'), key: 'warning-color', align: Gtk.Align.START },
{ type: Gtk.ColorButton, label: _('Critical color'), key: 'critical-color', align: Gtk.Align.START },
{ type: Gtk.ColorButton, label: _('Unknown color'), key: 'unknown-color', align: Gtk.Align.START },
]; ];
@ -61,7 +67,7 @@ function init() {
function buildPrefsWidget() { function buildPrefsWidget() {
// Copy the same GSettings code from `extension.js` // Copy the same GSettings code from `extension.js`
// this.settings = ExtensionUtils.getSettings(SETTINGS_SCHEMA); this.settings = ExtensionUtils.getSettings(SETTINGS_SCHEMA);
let mainVbox = new Gtk.Box( { orientation: Gtk.Orientation.VERTICAL } ); let mainVbox = new Gtk.Box( { orientation: Gtk.Orientation.VERTICAL } );
@ -84,6 +90,25 @@ function buildPrefsWidget() {
}); });
prefsWidget.attach(title, 0, 0, 2, 1); prefsWidget.attach(title, 0, 0, 2, 1);
let _label = new Gtk.Label({
label: 'Poll delay in seconds',
visible: true,
halign: Gtk.Align.START,
});
prefsWidget.attach ( _label, 0, 1, 1, 1 );
let _entry = new Gtk.SpinButton ({
numeric: true,
halign: Gtk.Align.FILL,
visible: true,
hexpand: true,
can_focus: true,
});
_entry.set_range ( 1, 1000 );
_entry.set_increments ( 1, 5 );
prefsWidget.attach ( _entry, 1, 1, 1, 1 );
this.settings.bind ( 'poll-delay', _entry, 'value', Gio.SettingsBindFlags.DEFAULT );
// Misc Settings (TBD) // Misc Settings (TBD)
mainWidget.append_page ( prefsWidget, mainWidget.append_page ( prefsWidget,
new Gtk.Label ( { label: 'General', } ) ); new Gtk.Label ( { label: 'General', } ) );
@ -173,12 +198,12 @@ function createAccountWidgets ( isActive )
}); });
this._accountsWidget.attach ( _label, 0, y, 1, 1 ); this._accountsWidget.attach ( _label, 0, y, 1, 1 );
this.prefWidgets[prefEntry.key] = new prefEntry.type({ this.prefWidgets[prefEntry.key] = new prefEntry.type ( {
halign: Gtk.Align.FILL, halign: ( prefEntry.align ? prefEntry.align : Gtk.Align.FILL ),
visible: true, visible: true,
hexpand: true, hexpand: true,
can_focus: isActive, can_focus: isActive,
}); } );
let boundValue = 'text'; let boundValue = 'text';
if ( prefEntry.key == 'password' ) if ( prefEntry.key == 'password' )
@ -239,6 +264,14 @@ function activateAccountRow ( ) {
Gio.SettingsBindFlags.DEFAULT Gio.SettingsBindFlags.DEFAULT
); );
} }
else if ( prefEntry.type == Gtk.ColorButton )
{
let _color = new Gdk.RGBA ( );
_color.parse (_account_settings.get_string(prefEntry.key) );
this.prefWidgets[prefEntry.key].set_use_alpha ( false );
this.prefWidgets[prefEntry.key].set_rgba ( _color );
this.prefWidgets[prefEntry.key].connect('color-set', Lang.bind ( { key: prefEntry.key }, setColor ) );
}
else if ( prefEntry.type == Gtk.ComboBoxText ) else if ( prefEntry.type == Gtk.ComboBoxText )
{ {
this.prefWidgets[prefEntry.key].set_active(_account_settings.get_enum('type')); this.prefWidgets[prefEntry.key].set_active(_account_settings.get_enum('type'));
@ -302,3 +335,14 @@ function removeAccount ( ) {
_row.destroy(); _row.destroy();
this.settings.set_string ( 'servers', _servers . join ( ',' ) ); this.settings.set_string ( 'servers', _servers . join ( ',' ) );
} }
function setColor ( color )
{
log ( 'Color ' + + ': ' + color );
let _output = '#%02x%02x%02x'.format(
255 * color.get_rgba().red,
255 * color.get_rgba().green,
255 * color.get_rgba().blue );
_account_settings.set_string('' + this.key, _output);
}

View File

@ -43,6 +43,10 @@
<default>"Short"</default> <default>"Short"</default>
</key> </key>
<key name="poll-delay" type="i">
<default>300</default>
</key>
</schema> </schema>
<!-- Account list --> <!-- Account list -->
@ -72,9 +76,22 @@
<default>''</default> <default>''</default>
</key> </key>
<key name="timeoutinterval" type="i"> <key name="ok-color" type="s">
<default>1</default> <default>'#00cc33'</default>
</key> </key>
<key name="warning-color" type="s">
<default>'#ffa500'</default>
</key>
<key name="critical-color" type="s">
<default>'#ff3300'</default>
</key>
<key name="unknown-color" type="s">
<default>'#e496f5'</default>
</key>
</schema> </schema>
</schemalist> </schemalist>

View File

@ -33,7 +33,7 @@ let _httpSession;
class Icinga { class Icinga {
constructor ( _server ) { constructor ( _server ) {
log ( '>>> New Icinga' + _server ); log ( '>>> New Icinga #' + _server );
this.server = _server this.server = _server
} }
@ -49,9 +49,9 @@ class Icinga {
log ( 'monito name >>> ' + name ); log ( 'monito name >>> ' + name );
log ( 'monito type >>> ' + type ); log ( 'monito type >>> ' + type );
log ( 'monito username >>> ' + username ); log ( 'monito username >>> ' + username );
log ( 'monito urlcgi >>> ' + urlcgi );
urlcgi = urlcgi.replace ( /^(https?:\/\/)/, '$1' + username + ':' + password + '@' ); urlcgi = urlcgi.replace ( /^(https?:\/\/)/, '$1' + username + ':' + password + '@' );
log ( 'monito urlcgi >>> ' + urlcgi );
if (_httpSession === undefined) { if (_httpSession === undefined) {
_httpSession = new Soup.Session(); _httpSession = new Soup.Session();
@ -65,7 +65,7 @@ class Icinga {
_httpSession.queue_message(message, Lang.bind _httpSession.queue_message(message, Lang.bind
(this, function(_httpSession, message) (this, function(_httpSession, message)
{ {
log ( '>>> ' + message.response_body.data ); // log ( '>>> ' + message.response_body.data );
let json = JSON.parse(message.response_body.data); let json = JSON.parse(message.response_body.data);
this.status = json.status; this.status = json.status;

View File

@ -33,7 +33,7 @@ let _httpSession;
class Icinga2 { class Icinga2 {
constructor ( _server ) { constructor ( _server ) {
log ( '>>> New Icinga2' + _server ); log ( '>>> New Icinga2 #' + _server );
this.server = _server this.server = _server
} }
@ -49,9 +49,9 @@ class Icinga2 {
log ( 'monito name >>> ' + name ); log ( 'monito name >>> ' + name );
log ( 'monito type >>> ' + type ); log ( 'monito type >>> ' + type );
log ( 'monito username >>> ' + username ); log ( 'monito username >>> ' + username );
log ( 'monito urlcgi >>> ' + urlcgi );
// urlcgi = urlcgi.replace ( /^(https?:\/\/)/, '$1' + username + ':' + password + '@' ); // urlcgi = urlcgi.replace ( /^(https?:\/\/)/, '$1' + username + ':' + password + '@' );
log ( 'monito urlcgi >>> ' + urlcgi );
if (_httpSession === undefined) { if (_httpSession === undefined) {
_httpSession = new Soup.Session(); _httpSession = new Soup.Session();
@ -81,15 +81,17 @@ class Icinga2 {
for ( var entry of json ) for ( var entry of json )
{ {
// if ( entry.status != 0 )
// log ( JSON.stringify(entry, null, 2) );
this.status.service_status.push ( { this.status.service_status.push ( {
status: _statuses [ entry.service_state ], status: _statuses [ entry.service_state ],
host_name: entry.host_name, host_name: entry.host_name,
service_display_name: entry.service_display_name, service_display_name: entry.service_display_name,
attempts: entry.service_attempt, attempts: entry.service_attempt,
status_information: entry.service_output, status_information: entry.service_output,
last_check: new Date(parseInt(entry.service_last_state_change)).toString(), last_check: new Date ( parseInt(entry.service_last_state_change) * 1000 ) . toString(),
} ); } );
log ( 'date ' + new Date(parseInt(entry.service_last_state_change)) );
} }
extension.refreshUI ( this ); extension.refreshUI ( this );

View File

@ -50,7 +50,7 @@
} }
.monito-critical-box, .monito-service-CRITICAL, .monito-service-line-CRITICAL { .monito-critical-box, .monito-service-CRITICAL, .monito-service-line-CRITICAL {
background-color: #FF3300; background-color: #ff3300;
} }
.monito-warning-box, .monito-service-WARNING, .monito-service-line-WARNING { .monito-warning-box, .monito-service-WARNING, .monito-service-line-WARNING {
@ -58,11 +58,11 @@
} }
.monito-ok-box, .monito-service-OK, .monito-service-line-OK { .monito-ok-box, .monito-service-OK, .monito-service-line-OK {
background-color: #00CC33; background-color: #00cc33;
} }
.monito-unknown-box, .monito-service-UNKNOWN, .monito-service-line-UNKNOWN { .monito-unknown-box, .monito-service-UNKNOWN, .monito-service-line-UNKNOWN {
background-color: purple; background-color: #e496f5;
} }
.monito-service-line { .monito-service-line {