Refactoring

This commit is contained in:
Benjamin Drieu 2021-11-29 16:07:29 +01:00 committed by Benjamin Drieu
parent 747778571a
commit 83c27bf84a
4 changed files with 122 additions and 36 deletions

View File

@ -75,9 +75,7 @@ class Indicator extends PanelMenu.Button {
this.initStatus ( );
this.namesBoxes = { };
this.warningBoxes = { };
this.criticalBoxes = { };
this.unknownBoxes = { };
this.boxes = { };
this.sortIcons = { };
account_settings [ server ] = Preferences.getAccountSettings ( this.server );
@ -108,15 +106,34 @@ class Indicator extends PanelMenu.Button {
this.account_settings.bind ( 'name', this.namesBoxes [ this.server ], 'text', Gio.SettingsBindFlags.GET );
this.namesBoxes [ this.server ].connect ( 'button-press-event', Lang.bind ( this, function ( ) { this.setMenu ( this.menu ); } ) );
for ( var boxName of [ 'ok', 'warning', 'critical', 'unknown' ] )
{
let _box = new St.BoxLayout( { style_class: 'monito-%s-box monito-box'.format(boxName), visible: false } );
_box.set_style ( 'background-color: %s; color: %s'.format ( this.account_settings.get_string ( boxName + '-color' ),
this.account_settings.get_string ( boxName + '-fg' ) ) );
this.account_settings.connect("changed::%s-color".format(boxName), Lang.bind ( { widget: _box }, setColor ) );
this.account_settings.connect("changed::%s-fg".format(boxName), Lang.bind ( { widget: _box }, setColor ) );
this.boxes [ boxName ] = new St.Label ( { text: '' } );
_box.add_child ( this.boxes [ boxName ] );
serverBox.add_child(_box);
}
/*
this.okBox = new St.Label({ text: String(_status['OK']), visible: false })
ok_box.add_child ( this.okBox );
serverBox.add_child(ok_box);
let warning_box = new St.BoxLayout({ style_class: 'monito-warning-box monito-box' });
warning_box.set_style ( 'background-color: %s; color: %s'.format ( this.account_settings.get_string ( 'warning-color' ), this.account_settings.get_string ( 'warning-fg' ) ) );
this.account_settings.connect("changed::warning-color", Lang.bind ( { widget: warning_box }, setColor ) );
this.account_settings.connect("changed::warning-fg", Lang.bind ( { widget: warning_box }, setColor ) );
this.warningBoxes [ this.server ] = new St.Label({ text: String(_status['WARNING']) })
warning_box.add_child ( this.warningBoxes [ this.server ] );
this.warningBox = new St.Label({ text: String(_status['WARNING']), visible: false })
warning_box.add_child ( this.warningBox );
serverBox.add_child(warning_box);
let critical_box = new St.BoxLayout({ style_class: 'monito-critical-box monito-box' });
@ -124,9 +141,10 @@ class Indicator extends PanelMenu.Button {
this.account_settings.connect("changed::critical-color", Lang.bind ( { widget: critical_box }, setColor ) );
this.account_settings.connect("changed::critical-fg", Lang.bind ( { widget: critical_box }, setColor ) );
this.criticalBoxes [ this.server ] = new St.Label({ text: String(_status['CRITICAL']) })
critical_box.add_child ( this.criticalBoxes [ this.server ] );
this.criticalBox = new St.Label({ text: String(_status['CRITICAL']), visible: false })
critical_box.add_child ( this.criticalBox );
serverBox.add_child(critical_box);
*/
box.add_child(PopupMenu.arrowIcon(St.Side.BOTTOM));
@ -300,8 +318,9 @@ class Indicator extends PanelMenu.Button {
if ( this.serverLogic.error || ! this.serverLogic.status )
{
this._box.add_child ( new St.Label ( { style_class: 'monito-network-error', text: this.serverLogic.error } ) );
this.warningBoxes[this.server].set_text ( '…' );
this.criticalBoxes[this.server].set_text ( '…' );
this.boxes['ok'].set_text ( '…' );
this.boxes['warning'].set_text ( '…' );
this.boxes['critical'].set_text ( '…' );
return;
}
@ -347,9 +366,28 @@ class Indicator extends PanelMenu.Button {
infoBox.add_child ( this.createBin ( entry.status, entry, column_definitions [ 'actions' ] ) );
}
}
this.warningBoxes[this.server].set_text ( String(_status.WARNING) );
this.criticalBoxes[this.server].set_text ( String(_status.CRITICAL) );
if ( _status.WARNING == 0 && _status.CRITICAL == 0 )
{
this.boxes['ok'].set_text ( String(_status.OK) );
this.boxes['ok'].get_parent().show ( );
this.boxes['warning'].get_parent().hide ( );
this.boxes['critical'].get_parent().hide ( );
}
else
{
this.boxes['warning'].set_text ( String(_status.WARNING) );
this.boxes['critical'].set_text ( String(_status.CRITICAL) );
this.boxes['ok'].get_parent().hide ( );
this.boxes['warning'].get_parent().show ( );
this.boxes['critical'].get_parent().show ( );
}
if ( _status.UNKNOWN != 0 )
{
this.boxes['unknown'].set_text ( String(_status.UNKNOWN) );
this.boxes['unkown'].get_parent().show ( );
}
return;
}

View File

@ -43,24 +43,31 @@ const SETTINGS_SCHEMA_ACCOUNT = "org.gnome.shell.extensions.monito.account";
const SETTINGS_SCHEMA_ACCOUNT_PATH = "/org/gnome/shell/extensions/monito/account";
const prefs = [ { type: Gtk.Entry, category: 'Settings', label: _('Name'), key: 'name' },
{ type: Gtk.ComboBoxText, category: 'Settings', label: _('Type'), key: 'type' },
{ type: Gtk.Entry, category: 'Settings', label: _('Username'), key: 'username' },
{ type: Gtk.Entry, category: 'Settings', label: _('Password'), key: 'password' },
{ type: Gtk.Entry, category: 'Settings', label: _('URL CGI'), key: 'urlcgi' },
{ type: Gtk.ColorButton, category: 'Colors', label: _('OK background color'), key: 'ok-color', align: Gtk.Align.START },
{ type: Gtk.ColorButton, category: 'Colors', label: _('Warning background color'), key: 'warning-color', align: Gtk.Align.START },
{ type: Gtk.ColorButton, category: 'Colors', label: _('Critical background color'), key: 'critical-color', align: Gtk.Align.START },
{ type: Gtk.ColorButton, category: 'Colors', label: _('Unknown background color'), key: 'unknown-color', align: Gtk.Align.START },
{ type: Gtk.ColorButton, category: 'Colors', label: _('OK color'), key: 'ok-fg', align: Gtk.Align.START },
{ type: Gtk.ColorButton, category: 'Colors', label: _('Warning color'), key: 'warning-fg', align: Gtk.Align.START },
{ type: Gtk.ColorButton, category: 'Colors', label: _('Critical color'), key: 'critical-fg', align: Gtk.Align.START },
{ type: Gtk.ColorButton, category: 'Colors', label: _('Unknown color'), key: 'unknown-fg', align: Gtk.Align.START },
{ type: Gtk.Entry, category: 'Filter', label: _('Only display hosts matching'), key: 'host-grep', align: Gtk.Align.START },
{ type: Gtk.Entry, category: 'Filter', label: _('Only display services matching'), key: 'service-grip', align: Gtk.Align.START },
{ type: Gtk.Entry, category: 'Filter', label: _('Do not display hosts matching'), key: 'host-filter-out', align: Gtk.Align.START },
{ type: Gtk.Entry, category: 'Filter', label: _('Do not display services matching'), key: 'service-filter-out', align: Gtk.Align.START },
];
const prefs = [
{ type: Gtk.Entry, category: 'Settings', label: _('Name'), key: 'name' },
{ type: Gtk.ComboBoxText, category: 'Settings', label: _('Type'), key: 'type' },
{ type: Gtk.Entry, category: 'Settings', label: _('Username'), key: 'username' },
{ type: Gtk.Entry, category: 'Settings', label: _('Password'), key: 'password' },
{ type: Gtk.Entry, category: 'Settings', label: _('URL CGI'), key: 'urlcgi' },
{ type: Gtk.ColorButton, category: 'Colors', label: _('OK background color'), key: 'ok-color', align: Gtk.Align.START },
{ type: Gtk.ColorButton, category: 'Colors', label: _('Warning background color'), key: 'warning-color', align: Gtk.Align.START },
{ type: Gtk.ColorButton, category: 'Colors', label: _('Critical background color'), key: 'critical-color', align: Gtk.Align.START },
{ type: Gtk.ColorButton, category: 'Colors', label: _('Unknown background color'), key: 'unknown-color', align: Gtk.Align.START },
{ type: Gtk.ColorButton, category: 'Colors', label: _('OK color'), key: 'ok-fg', align: Gtk.Align.START },
{ type: Gtk.ColorButton, category: 'Colors', label: _('Warning color'), key: 'warning-fg', align: Gtk.Align.START },
{ type: Gtk.ColorButton, category: 'Colors', label: _('Critical color'), key: 'critical-fg', align: Gtk.Align.START },
{ type: Gtk.ColorButton, category: 'Colors', label: _('Unknown color'), key: 'unknown-fg', align: Gtk.Align.START },
{ type: Gtk.Entry, category: 'Filters', label: _('Only display hosts matching'), key: 'host-grep' },
{ type: Gtk.Entry, category: 'Filters', label: _('Only display services matching'), key: 'service-grep' },
{ type: Gtk.Entry, category: 'Filters', label: _('Do not display hosts matching'), key: 'host-filter-out' },
{ type: Gtk.Entry, category: 'Filters', label: _('Do not display services matching'), key: 'service-filter-out' },
{ type: Gtk.Entry, category: 'Replacements', label: _('Host match regexp ...'), key: 'host-match' },
{ type: Gtk.Entry, category: 'Replacements', label: _('... to replace with'), key: 'host-replace' },
{ type: Gtk.Entry, category: 'Replacements', label: _('Service match regexp ...'), key: 'service-match' },
{ type: Gtk.Entry, category: 'Replacements', label: _('... to replace with'), key: 'service-replace' },
{ type: Gtk.Entry, category: 'Replacements', label: _('Status info match regexp ...'), key: 'status-info-match' },
{ type: Gtk.Entry, category: 'Replacements', label: _('... to replace with'), key: 'status-info-replace' },
];
// Like 'extension.js' this is used for any one-time setup like translations.
@ -213,18 +220,17 @@ function createAccountWidgets ( isActive )
this._accountsWidget = new Gtk.Notebook( { } );
this._accountsWidgetContainer.pack_start(this._accountsWidget, true, true, 0);
for ( var _tab of [ 'Settings', 'Colors', 'Filters', 'Replacements' ] )
{
this.createPrefWidgets ( _accountsWidget, _tab, isActive );
}
// Settings
this.createPrefWidgets ( _accountsWidget, 'Settings', isActive );
this.prefWidgets['name'].connect('changed', Lang.bind(this, function () {
let _row = this.accountsChooser.get_selected_row();
_row.get_child().label = this.prefWidgets['name'].text;
} ) );
// Colors
this.createPrefWidgets ( _accountsWidget, 'Colors', isActive );
// Filters
this.createPrefWidgets ( _accountsWidget, 'Filters', isActive );
this._accountsWidget.show_all();
}

View File

@ -116,5 +116,46 @@
<default>['status+', 'host_name+','service_display_name+']</default>
</key>
<key name="host-grep" type="s">
<default>''</default>
</key>
<key name="service-grep" type="s">
<default>''</default>
</key>
<key name="host-filter-out" type="s">
<default>''</default>
</key>
<key name="service-filter-out" type="s">
<default>''</default>
</key>
<key name="host-match" type="s">
<default>''</default>
</key>
<key name="host-replace" type="s">
<default>''</default>
</key>
<key name="service-match" type="s">
<default>''</default>
</key>
<key name="service-replace" type="s">
<default>''</default>
</key>
<key name="status-info-match" type="s">
<default>''</default>
</key>
<key name="status-info-replace" type="s">
<default>''</default>
</key>
</schema>
</schemalist>

View File

@ -92,6 +92,7 @@ class GenericServer {
if ( message.status_code != Soup.Status.OK )
{
log ( '>>> error: ' + message.reason_phrase );
log ( '>>> data: ' + message.data );
Main.notifyError ( 'Monito: ' + this.name,
'URL: ' + this.urlcgi + "\n" +
message.status_code + ': ' + message.reason_phrase );