monito/extension.js

796 lines
28 KiB
JavaScript
Raw Normal View History

2021-11-09 15:26:18 +01:00
/* -*- mode: js; js-basic-offset: 4; indent-tabs-mode: nil -*- */
/*
Monito Gnome-Shell extension
Copyright (C) 2021 Benjamin Drieu
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 2 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.
SPDX-License-Identifier: GPL-2.0-or-later
2021-11-09 15:21:21 +01:00
*/
/* exported init */
const GETTEXT_DOMAIN = 'monito';
let _status;
2022-05-30 17:16:27 +02:00
const ExtensionUtils = imports.misc.extensionUtils; // eslint-disable-line no-undef
const Lang = imports.lang; // eslint-disable-line no-undef
const Main = imports.ui.main; // eslint-disable-line no-undef
const Mainloop = imports.mainloop; // eslint-disable-line no-undef
const Me = ExtensionUtils.getCurrentExtension(); // eslint-disable-line no-undef
const PanelMenu = imports.ui.panelMenu; // eslint-disable-line no-undef
const PopupMenu = imports.ui.popupMenu; // eslint-disable-line no-undef
2021-11-17 21:01:51 +01:00
2022-05-30 17:16:27 +02:00
const Gettext = imports.gettext.domain(GETTEXT_DOMAIN); // eslint-disable-line no-undef
2021-11-17 21:01:51 +01:00
const _ = Gettext.gettext;
2021-11-09 15:21:21 +01:00
2022-05-30 17:16:27 +02:00
const { GObject, St, Clutter, Gio, GLib, Pango } = imports.gi; // eslint-disable-line no-undef
2021-11-09 15:21:21 +01:00
2021-11-17 17:15:21 +01:00
const SETTINGS_SCHEMA = "org.gnome.shell.extensions.monito";
2021-11-09 15:21:21 +01:00
const Convenience = Me.imports.convenience;
2021-11-12 12:11:20 +01:00
const Icinga = Me.imports.servers.icinga.Icinga;
const Icinga2 = Me.imports.servers.icinga2.Icinga2;
2021-12-01 15:27:25 +01:00
const Icinga2API = Me.imports.servers.icinga2api.Icinga2API;
2021-11-17 21:01:51 +01:00
const Preferences = Me.imports.prefs;
2021-11-12 12:11:20 +01:00
2021-11-09 15:21:21 +01:00
let settings = Convenience.getSettings(SETTINGS_SCHEMA);
2021-11-17 17:15:21 +01:00
let account_settings = [ ];
2021-11-09 15:21:21 +01:00
2023-01-06 17:40:30 +01:00
//const column_definitions = {
// status: { label: _('Status'), width: 50, expand: false, },
// host_name: { label: _('Host name'), width: 300, expand: false, },
// service_display_name: { label: _('Service'), width: 300, expand: false, },
// has_been_acknowledged: { label: _('Ack'), width: 50, expand: false, align: Clutter.ActorAlign.CENTER, style: 'font-weight:bold;' },
// last_check: { label: _('Last check'), width: 200, expand: false, type: 'date' },
// attempts: { label: _('Attempts'), width: 50, expand: false, },
// status_information: { label: _('Information'), width: 600, expand: false, },
// actions: { label: 'Actions', width: 50, expand: false, special: 'actions' },
//};
2021-11-09 15:21:21 +01:00
const Indicator = GObject.registerClass(
class Indicator extends PanelMenu.Button {
_init(server) {
2021-11-09 15:21:21 +01:00
super._init(0.0, _('Monito Checker'));
this.server = server;
2021-11-09 15:21:21 +01:00
this.initStatus ( );
2021-11-10 17:21:03 +01:00
this.namesBoxes = { };
2021-11-29 16:07:29 +01:00
this.boxes = { };
this.sortIcons = { };
2022-05-30 11:27:27 +02:00
this.showAll = false;
account_settings [ server ] = Preferences.getAccountSettings ( this.server );
this.account_settings = account_settings [ server ];
let type = this.account_settings.get_string ( "type" );
if ( type == 'Icinga' )
2021-12-03 16:35:50 +01:00
this.serverLogic = new Icinga ( this.server, this );
else if ( type == 'Icinga2' )
2021-12-03 16:35:50 +01:00
this.serverLogic = new Icinga2 ( this.server, this );
2021-12-01 15:27:25 +01:00
else if ( type == 'Icinga2API' )
2021-12-03 16:35:50 +01:00
this.serverLogic = new Icinga2API ( this.server, this );
2021-11-17 17:15:21 +01:00
this.initUI ( );
}
initUI ( ) {
let box = new St.BoxLayout ( { } );
this.add_child(box);
monitoLog ( '> Server ' + this.server );
let serverBox = new St.BoxLayout ( { style_class: 'monito-serverbox' } );
box.add_child(serverBox);
let name_box = new St.BoxLayout( { style_class: 'monito-namebox' } );
2021-12-02 00:28:51 +01:00
this.namesBoxes [ this.server ] = new St.Label ( { text: this.account_settings.get_string ( 'name' ),
y_align: Clutter.ActorAlign.CENTER, } );
name_box.add_child ( this.namesBoxes [ this.server ] );
serverBox.add_child(name_box);
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 ); } ) );
2021-11-29 16:07:29 +01:00
for ( var boxName of [ 'ok', 'warning', 'critical', 'unknown' ] )
{
let _box = new St.BoxLayout( { style_class: 'monito-%s-box monito-box'.format(boxName), visible: false } );
2021-11-29 23:04:17 +01:00
_box.set_style ( this.getStyleForRow ( boxName ) );
2021-11-29 16:07:29 +01:00
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);
}
2021-11-09 15:21:21 +01:00
box.add_child(PopupMenu.arrowIcon(St.Side.BOTTOM));
this.menu_new = new PopupMenu.PopupMenu(this, Clutter.ActorAlign.START, St.Side.TOP, 0);
2021-11-09 15:21:21 +01:00
// Menu
this._buttonMenu = new PopupMenu.PopupBaseMenuItem({
reactive: false,
style_class: 'monito-menu-button-container',
});
this.menu.addMenuItem(this._buttonMenu);
2021-11-10 17:21:03 +01:00
let _path = Me.path + '/img/monito.png';
let _icon = new St.Icon({
gicon: Gio.icon_new_for_string(_path),
});
let _iconBin = new St.Bin();
_iconBin.child = _icon;
this._buttonMenu.actor.add_actor(_iconBin);
2021-12-06 11:34:55 +01:00
this._mainLabel = new St.Label ( { style_class: 'monito-title', text: _('Monito Checker'), x_expand: true } );
2021-11-09 15:21:21 +01:00
this._buttonMenu.actor.add_actor(this._mainLabel);
2022-03-22 22:19:07 +01:00
this._searchField = new St.Entry ( {
x_align: Clutter.ActorAlign.END,
y_align: Clutter.ActorAlign.CENTER,
reactive: true,
can_focus: true,
track_hover: true,
style_class: 'entry',
secondary_icon: new St.Icon ( {
style_class: 'monito-button-icon',
icon_name: 'edit-find-symbolic',
icon_size: 24,
width: 24,
height: 24,
} ),
});
this._searchField.connect('secondary-icon-clicked', Lang.bind(this, this._onSearchFieldActivate ) );
this._searchField.clutter_text.connect('activate', Lang.bind(this, this._onSearchFieldActivate ) );
2022-04-22 11:22:38 +02:00
this._searchField.clutter_text.connect('text-changed', Lang.bind(this, this._onSearchFieldActivate ) );
2022-03-22 22:19:07 +01:00
this._buttonMenu.actor.add_child (this._searchField);
2022-05-30 11:27:27 +02:00
this._showAllButton = this._createButton ( 'big', 'view-reveal-symbolic', _('Show all'), this.changeShowAll, true );
this._buttonMenu.actor.add_child (this._showAllButton );
this._prefsButton = this._createButton ( 'big', 'preferences-system-symbolic', _('Preferences'), this._onPreferencesActivate );
2021-11-09 15:21:21 +01:00
this._buttonMenu.actor.add_child (this._prefsButton);
2023-03-29 08:29:21 +02:00
this._reloadButton = this._createButton ( 'big', 'view-refresh-symbolic', _('Reload view'), this.updateStatus );
this._buttonMenu.actor.add_child (this._reloadButton );
2021-12-06 11:34:55 +01:00
if ( this.serverLogic && this.serverLogic.canRecheck )
{
2023-03-29 08:29:21 +02:00
let multiButtonBox = new St.BoxLayout( { style_class: 'monito-multibutton' } );
this._recheckButton = this._createButton ( 'big', 'mail-send-receive-symbolic', _('Recheck all'), this.recheckAll );
2023-03-29 08:29:21 +02:00
this._recheckSpecialButton = this._createPopupButton ( this.recheckSpecial );
multiButtonBox.add_child ( this._recheckButton );
multiButtonBox.add_child ( this._recheckSpecialButton );
this._buttonMenu.actor.add_child (multiButtonBox);
const sourceActor = new St.Widget();
this._menu = new PopupMenu.PopupMenu(sourceActor, 0.0, St.Side.TOP);
const menuItem1 = this._menu.addAction('Item 1', () => console.log('activated'));
2021-12-06 11:34:55 +01:00
}
2021-11-19 22:16:45 +01:00
2021-11-09 15:21:21 +01:00
let _intermediate = new PopupMenu.PopupBaseMenuItem ( {
style_class: 'monito-services',
reactive: false
});
this.menu.addMenuItem(_intermediate);
this._box = new St.BoxLayout({
style_class: 'monito-services',
vertical: true,
x_expand: true
});
_intermediate.actor.add_actor(this._box);
this.menu_orig = this.menu;
2021-11-09 15:21:21 +01:00
this.updateStatus ( );
2021-11-17 14:44:04 +01:00
this.setupTimeout ( );
}
cancelTimeout ( )
{
if (this.timeout)
Mainloop.source_remove(this.timeout);
}
2021-11-17 14:44:04 +01:00
setupTimeout ( )
{
2021-11-17 17:15:21 +01:00
monitoLog ( 'Setting up timeout of ' + settings.get_int ( "poll-delay" ) + ' secs' );
this.cancelTimeout ( );
2021-11-17 14:44:04 +01:00
this.timeout = Mainloop.timeout_add ( settings.get_int ( "poll-delay" ) * 1000, Lang.bind(this, this.updateStatus ) );
2021-11-09 15:21:21 +01:00
}
initStatus ( ) {
_status = { 'OK': 0,
'WARNING': 0,
'CRITICAL': 0,
'UNKNOWN': 0 };
}
2021-12-06 11:34:55 +01:00
recheckAll ( )
{
this.spinChildOf ( this._recheckButton );
this.serverLogic.recheckAll ( this._recheckButton );
}
2023-03-29 08:29:21 +02:00
recheckSpecial ( )
{
this._menu.open();
}
2022-05-30 11:27:27 +02:00
changeShowAll ( )
{
this.showAll = ! this.showAll;
this.refreshUI ( );
}
2021-11-17 14:44:04 +01:00
updateStatus ( )
{
2021-12-03 16:35:50 +01:00
this.spinChildOf ( this._reloadButton );
this.serverLogic.refresh ( );
2021-11-17 14:44:04 +01:00
this.setupTimeout ( );
2021-11-09 15:21:21 +01:00
}
2021-12-03 16:35:50 +01:00
spinChildOf ( widget )
{
if ( ! widget )
return;
2021-12-06 11:45:02 +01:00
let _size = widget.get_child().width;
2021-12-03 16:35:50 +01:00
let _child = new St.Icon({
style_class: 'monito-button-icon',
icon_name: 'process-working-symbolic',
icon_size: _size,
width: _size,
height: _size,
});
let _transition = new Clutter.PropertyTransition ( { property_name: 'rotation_angle_z',
repeat_count: -1,
duration: 1000 } );
_transition.set_from ( 0 );
_transition.set_to ( 360 );
2021-12-06 11:45:02 +01:00
widget.set_child ( _child );
2021-12-03 16:35:50 +01:00
_child.set_pivot_point ( .5, .5 );
_child.add_transition ( 'rotation', _transition );
}
stopChildSpin ( widget )
{
2021-12-06 11:45:02 +01:00
let _size = widget.get_child().width;
2023-01-06 17:40:30 +01:00
if ( widget.get_child() && widget.get_child().get_transition('rotation') )
widget.get_child().get_transition('rotation').stop();
2021-12-06 11:45:02 +01:00
widget.child = new St.Icon({
style_class: 'monito-button-icon',
icon_name: widget.prevIcon,
icon_size: _size,
width: _size,
height: _size,
});
2021-12-03 16:35:50 +01:00
}
2022-09-09 19:22:46 +02:00
createHeaderBin ( colName, colSize = 50 ) {
2023-01-06 17:40:30 +01:00
let col = Preferences.column_definitions [ colName ];
if ( ! col )
return new St.Bin({
style_class: 'monito-service',
width: colSize,
x_expand: false
});
let _box = new St.BoxLayout ( { vertical: false,
x_expand: true } );
_box.add_child ( new St.Label ( { text: col.label,
x_expand: true,
x_align: Clutter.ActorAlign.START } ) );
let _iconBin = new St.Bin ( { x_align: Clutter.ActorAlign.END });
_box.add_child ( _iconBin );
let _iconName = '';
let _sortOrder = Preferences.getSortOrder ( this.server );
if ( _sortOrder.indexOf ( colName + '+' ) >= 0 )
_iconName = 'view-sort-descending-symbolic';
else if ( _sortOrder.indexOf ( colName + '-' ) >= 0 )
_iconName = 'view-sort-ascending-symbolic';
this.sortIcons [ colName ] = new St.Icon ( {
style_class: 'monito-button-icon',
icon_name: _iconName,
icon_size: 16,
} );
_iconBin.child = this.sortIcons [ colName ];
let _button = new St.Button ( {
2021-12-06 11:34:55 +01:00
x_align: Clutter.ActorAlign.FILL,
2021-11-17 20:16:53 +01:00
y_align: Clutter.ActorAlign.CENTER,
2022-09-09 19:22:46 +02:00
width: colSize,
2021-11-17 20:16:53 +01:00
reactive: true,
can_focus: true,
track_hover: true,
accessible_name: col.label,
2021-11-17 20:16:53 +01:00
style_class: 'button',
} );
_button.column = colName;
_button.add_actor ( _box );
_button.connect ( 'clicked', Lang.bind ( this, this._onSortColumnClick ) );
let _bin = new St.Bin({
style_class: 'monito-service',
2022-09-09 19:22:46 +02:00
width: colSize,
x_expand: col.expand,
child: _button,
2021-11-17 20:16:53 +01:00
});
2021-11-17 20:16:53 +01:00
return _bin;
}
2022-09-09 19:22:46 +02:00
createBin ( status, text, colSize = 50, col ) {
2021-11-19 22:16:45 +01:00
let _child;
2023-01-06 17:40:30 +01:00
if ( ! col )
return new St.Bin({
track_hover: true,
width: colSize,
x_expand: false
})
if ( text === undefined )
2021-12-01 15:27:25 +01:00
text = '…';
2021-11-19 22:16:45 +01:00
if ( ! col [ 'special' ] )
{
_child = new St.Label ( { style_class: 'monito-label',
reactive: true,
can_focus: true,
track_hover: true,
2022-09-09 19:22:46 +02:00
width: colSize,
text: text.toString().replace(/\n.*/s, ''),
x_align: ( col.align ? col.align : Clutter.ActorAlign.START ),
style: ( col.style ? col.style : '' ) } );
_child.original_text = text.toString();
_child.connect('button-press-event', Lang.bind(this, this._onExpandLabel ) );
2022-05-20 16:32:36 +02:00
// _child.connect('notify::hover', Lang.bind(this, this._onEnterEvent ) );
}
else if ( col.special == 'actions' )
2021-11-19 22:16:45 +01:00
{
2021-12-06 11:34:55 +01:00
_child = new St.BoxLayout ( { x_expand: true,
vertical: false,
2022-09-09 19:22:46 +02:00
width: colSize, } );
if ( this.serverLogic.canRecheck )
{
_child.add_child ( this._createButton ( 'small', 'mail-send-receive-symbolic', text, this._onRecheckButtonClick ) );
}
_child.add_child ( this._createButton ( 'small', 'web-browser-symbolic', text, this._onOpenInBrowser ) );
2021-11-19 22:16:45 +01:00
}
2021-11-09 15:21:21 +01:00
let _bin = new St.Bin({
2021-11-17 20:16:53 +01:00
track_hover: true,
2022-09-09 19:22:46 +02:00
width: colSize,
x_expand: col.expand,
2021-11-19 22:16:45 +01:00
child: _child,
2021-11-09 15:21:21 +01:00
});
return _bin;
}
2021-11-29 23:04:17 +01:00
getStyleForRow ( boxName, row = 0 )
{
let bgColor = this.account_settings.get_string ( boxName + '-color' );
let fgColor = this.account_settings.get_string ( boxName + '-fg' );
if ( row % 2 )
{
2021-12-01 15:27:25 +01:00
bgColor = this.lightenColor(bgColor, 12);
fgColor = this.lightenColor(fgColor, 12);
2021-11-29 23:04:17 +01:00
}
else
{
2021-12-01 15:27:25 +01:00
bgColor = this.lightenColor(bgColor, -12);
fgColor = this.lightenColor(fgColor, -12);
2021-11-29 23:04:17 +01:00
}
return 'background-color: %s; color: %s' . format ( bgColor, fgColor );
}
lightenColor ( col, amt ) {
if ( col.substring(0,1) == '#' )
col = col.substring ( 1 );
col = parseInt(col, 16);
return '#%06x'.format (Math.max(Math.min((col & 0x0000FF) + amt,0x0000FF),0) |
(Math.max(Math.min((((col >> 8) & 0x00FF) + amt),0x0000FF),0) << 8) |
(Math.max(Math.min(((col >> 16) + amt),0x0000FF),0) << 16));
}
refreshUI ( ) {
2021-12-01 15:27:25 +01:00
try {
this.initStatus ( );
this._box.remove_all_children();
if ( this.serverLogic.error || ! this.serverLogic.status )
{
this._box.add_child ( new St.Label ( { style_class: 'monito-network-error', text: this.serverLogic.error } ) );
this.boxes['ok'].set_text ( '…' );
this.boxes['warning'].set_text ( '…' );
this.boxes['critical'].set_text ( '…' );
2021-12-02 00:12:33 +01:00
this.boxes['unknown'].set_text ( '…' );
2022-02-11 21:20:36 +01:00
this.stopChildSpin ( this._reloadButton );
2021-12-01 15:27:25 +01:00
return;
}
let headerBox = new St.BoxLayout({
2021-12-06 11:34:55 +01:00
track_hover: true,
2021-12-01 15:27:25 +01:00
x_expand: true
});
this._box.add_child(headerBox);
2022-09-09 19:22:46 +02:00
for ( let _col of Preferences.getColumns ( this.server ) )
headerBox.add_child ( this.createHeaderBin ( _col.name, _col.size ) );
2021-12-01 15:27:25 +01:00
let scrollBox = new St.ScrollView ( { hscrollbar_policy: St.PolicyType.NEVER,
enable_mouse_scrolling: true, } );
this._box.add_child(scrollBox);
let tableBox = new St.BoxLayout({
2021-12-06 11:34:55 +01:00
track_hover: true,
2021-12-01 15:27:25 +01:00
vertical: true,
x_expand: true,
});
scrollBox.add_actor(tableBox);
let processedStatus = this.serverLogic.getProcessedStatus ( )
for ( let entryCount of processedStatus )
_status [ entryCount.status ] ++;
let _row = 0;
for ( let entry of processedStatus )
2021-11-12 12:11:20 +01:00
{
2022-03-22 22:19:07 +01:00
if ( this._searchString &&
2022-04-22 11:22:38 +02:00
! ( entry [ 'host_name' ].toLowerCase().includes ( this._searchString ) ||
entry [ 'service_display_name' ].toLowerCase().includes ( this._searchString ) ||
entry [ 'status_information' ].toLowerCase().includes ( this._searchString ) ) )
2022-03-22 22:19:07 +01:00
continue;
2022-05-30 11:27:27 +02:00
if ( this.showAll ||
( ! _status [ 'WARNING' ] && ! _status [ 'CRITICAL' ] && ! _status [ 'UNKNOWN' ] && entry.status == 'OK' ) ||
2021-12-01 15:27:25 +01:00
( ( _status [ 'WARNING' ] || _status [ 'CRITICAL' ] || _status [ 'UNKNOWN' ] ) && entry.status != 'OK' ) )
{
let _style = this.getStyleForRow ( entry.status.toLowerCase(), _row );
let infoBox = new St.BoxLayout({
style_class: 'monito-service-line',
style: _style,
x_expand: true,
reactive: true,
can_focus: true,
track_hover: true,
2021-12-01 15:27:25 +01:00
});
tableBox.add_child(infoBox);
2022-09-09 19:22:46 +02:00
for ( let _col of Preferences.getColumns ( this.server ) )
2021-12-01 15:27:25 +01:00
{
2022-09-09 19:22:46 +02:00
entry [ 'real_' + _col.name ] = entry [ _col.name ];
if ( _col.name == 'host_name' && this.account_settings.get_string ( 'host-match' ) )
entry [ _col.name ] = entry [ _col.name ] . replace ( new RegExp ( this.account_settings.get_string ( 'host-match' ), 'i' ),
2021-12-01 15:27:25 +01:00
this.account_settings.get_string ( 'host-replace' ) );
2022-09-09 19:22:46 +02:00
else if ( _col.name == 'service_display_name' && this.account_settings.get_string ( 'service-match' ) )
entry [ _col.name ] = entry [ _col.name ] . replace ( new RegExp ( this.account_settings.get_string ( 'service-match' ), 'i' ),
2021-12-01 15:27:25 +01:00
this.account_settings.get_string ( 'service-replace' ) );
2022-09-09 19:22:46 +02:00
else if ( _col.name == 'status_information' && this.account_settings.get_string ( 'status-info-match' ) )
entry [ _col.name ] = entry [ _col.name ] . replace ( new RegExp ( this.account_settings.get_string ( 'status-info-match' ), 'i' ),
2022-05-30 17:16:27 +02:00
this.account_settings.get_string ( 'status-info-replace' ) ) . replace ( new RegExp ( "\\n.*" ), "" );
2022-09-09 19:22:46 +02:00
else if ( _col.name == 'has_been_acknowledged' )
{
2022-09-09 19:22:46 +02:00
if ( entry [ _col.name ] )
entry [ _col.name ] = '✔';
else
2022-09-09 19:22:46 +02:00
entry [ _col.name ] = '';
}
2023-01-06 17:40:30 +01:00
infoBox.add_child ( this.createBin ( entry.status, ( _col.name == 'actions' ? entry : entry [ _col.name ] ), _col.size, Preferences.column_definitions [ _col.name ] ) );
2021-12-01 15:27:25 +01:00
}
_row ++;
}
2021-11-12 12:11:20 +01:00
}
2021-11-29 16:07:29 +01:00
2021-12-01 15:27:25 +01:00
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 ( );
2021-12-02 00:12:33 +01:00
this.boxes['unknown'].get_parent().hide ( );
2021-12-01 15:27:25 +01:00
}
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) );
2021-12-02 00:12:33 +01:00
this.boxes['unknown'].get_parent().show ( );
2021-12-01 15:27:25 +01:00
}
else
{
this.boxes['unknown'].get_parent().hide ( );
}
2021-11-29 16:07:29 +01:00
}
2021-12-01 15:27:25 +01:00
catch ( e )
2021-11-29 16:07:29 +01:00
{
2021-12-01 15:27:25 +01:00
monitoLog ( 'RefreshUI error: ' + e );
monitoLog ( e.stack );
2021-11-29 16:07:29 +01:00
}
2021-12-03 16:35:50 +01:00
this.stopChildSpin ( this._reloadButton );
2022-02-11 21:20:36 +01:00
monitoLog ( 'RefreshUI done' );
2021-11-09 15:21:21 +01:00
return;
}
2022-03-22 22:19:07 +01:00
_onSearchFieldActivate ( e ) {
monitoLog ( 'Search: ' + e.text );
2022-04-22 11:22:38 +02:00
this._searchString = e.text.toLowerCase();
2022-03-22 22:19:07 +01:00
this.refreshUI ( );
return;
}
2021-11-10 17:21:03 +01:00
_onPreferencesActivate ( ) {
2021-11-09 15:21:21 +01:00
this.menu.actor.hide();
if (typeof ExtensionUtils.openPrefs === 'function') {
ExtensionUtils.openPrefs();
} else {
2022-05-30 17:16:27 +02:00
ExtensionUtils.spawn([
2021-11-09 15:21:21 +01:00
"gnome-shell-extension-prefs",
Me.uuid
]);
}
return 0;
}
_onSortColumnClick ( button ) {
let _sortOrder = Preferences.getSortOrder ( this.server );
let _indexPlus = _sortOrder.indexOf ( button.column + '+' );
let _indexMinus = _sortOrder.indexOf ( button.column + '-' );
if ( _indexPlus >= 0 )
_sortOrder [ _indexPlus ] = button.column + '-';
else if ( _indexMinus >= 0 )
_sortOrder.splice ( _indexMinus, 1 );
else
_sortOrder.unshift ( button.column + '+' );
Preferences.setSortOrder ( this.server, _sortOrder );
this.refreshUI ( );
}
2021-11-22 22:12:49 +01:00
_onRecheckButtonClick ( e )
{
2021-12-03 16:35:50 +01:00
this.spinChildOf ( e );
// monitoLog ( JSON.stringify ( e ) );
2021-12-03 16:35:50 +01:00
e.service.button = e;
2021-11-22 22:12:49 +01:00
this.serverLogic.recheck ( e.service );
}
_onExpandLabel ( e )
{
2022-05-30 17:16:27 +02:00
monitoLog ( "Monito: Click label " + e.original_text );
let temp = e.text;
e.text = e.original_text;
e.original_text = temp;
if ( e.clutter_text.line_wrap )
{
e.clutter_text.line_wrap = false;
}
else
{
e.clutter_text.line_wrap = true;
e.clutter_text.line_wrap_mode = Pango.WrapMode.WORD;
e.clutter_text.ellipsize = Pango.EllipsizeMode.NONE;
}
}
2022-05-20 16:32:36 +02:00
// _onEnterEvent ( e )
// {
// log ( "Monito: enter");
// }
_onOpenInBrowser ( e )
{
this.spinChildOf ( e );
e.service.button = e;
let loop = GLib.MainLoop.new(null, false);
try {
let _cmd = "%s %s".format(settings.get_string ( "web-browser" ), this.serverLogic.getUrlForService ( e.service ) );
let proc = Gio.Subprocess.new (
_cmd.split ( ' ' ),
Gio.SubprocessFlags.STDOUT_PIPE | Gio.SubprocessFlags.STDERR_PIPE
);
let cancellable = new Gio.Cancellable();
proc.wait_async(cancellable, (proc, result) => {
try {
proc.wait_finish(result);
2022-05-20 16:32:36 +02:00
if ( ! proc.get_successful()) {
2022-05-30 17:16:27 +02:00
monitoLog ( 'Monito: the process failed' );
}
} catch (e) {
2022-05-30 17:16:27 +02:00
monitoLog(e);
} finally {
loop.quit();
}
});
} catch ( e ) {
2022-05-30 17:16:27 +02:00
monitoLog ( 'Monito err: ' + e.message );
2022-05-20 16:32:36 +02:00
Main.notifyError ( _('Unable to execute command: %s').format ( e.message ) );
}
2022-05-20 16:32:36 +02:00
this.stopChildSpin ( e.service.button );
}
2022-05-30 11:27:27 +02:00
_createButton ( sizeName, icon, data, callback, radiobutton = false ) {
let size = 24;
if ( sizeName == 'small' )
size = 24;
else if ( sizeName == 'big' )
size = 32;
let _text = '';
2022-05-30 17:16:27 +02:00
if ( ! ( data instanceof Object ) )
_text = data;
2021-11-09 15:21:21 +01:00
let button = new St.Button({
x_align: Clutter.ActorAlign.END,
y_align: Clutter.ActorAlign.CENTER,
reactive: true,
can_focus: true,
track_hover: true,
accessible_name: _text,
style_class: 'button %s-button'.format(sizeName),
2021-12-03 16:35:50 +01:00
rotation_angle_x: 0.0,
// width: size,
// height: size
2021-11-09 15:21:21 +01:00
});
2021-12-06 11:45:02 +01:00
button.prevIcon = icon;
2021-11-09 15:21:21 +01:00
2022-05-30 17:16:27 +02:00
if ( radiobutton )
{
button.toggle_mode = true;
button.set_checked ( true );
}
2022-05-30 11:27:27 +02:00
if ( data instanceof Object )
button.service = data;
2021-11-09 15:21:21 +01:00
button.child = new St.Icon({
style_class: 'monito-button-icon',
icon_name: icon,
icon_size: size - 4,
width: size - 4,
height: size - 4
2021-11-09 15:21:21 +01:00
});
button.connect('clicked', Lang.bind(this, callback ) );
return button;
}
2023-03-29 08:29:21 +02:00
_createPopupButton ( callback ) {
let size = 48;
let sizeName = 'big';
let button = new St.Button({
x_align: Clutter.ActorAlign.END,
y_align: Clutter.ActorAlign.CENTER,
reactive: true,
can_focus: true,
track_hover: true,
style_class: 'button %s-button'.format(sizeName),
rotation_angle_x: 0.0,
width: 32,
height: size
});
button.child = PopupMenu.arrowIcon(St.Side.BOTTOM);
button.connect('clicked', Lang.bind(this, callback ) );
return button;
}
2021-11-09 15:21:21 +01:00
});
class Extension {
constructor(uuid) {
this._uuid = uuid;
2022-05-30 17:16:27 +02:00
this._connectId = settings.connect("changed::servers", Lang.bind ( this, function() {
this.disable ( );
this.enable ( );
} ) );
2021-11-09 15:21:21 +01:00
ExtensionUtils.initTranslations(GETTEXT_DOMAIN);
}
enable() {
this._indicators = { };
2021-11-10 17:21:03 +01:00
for ( let _server of Preferences.getServersList() )
{
2022-03-22 22:19:07 +01:00
let _pref = Preferences.getAccountSettings ( _server );
if ( _pref.get_boolean ( 'active' ) )
{
this._indicators [ _server ] = new Indicator(_server);
Main.panel.addToStatusArea ( '%s-%d'.format ( this._uuid, _server), this._indicators [ _server ] );
}
}
2021-11-09 15:21:21 +01:00
}
disable() {
for ( var i of Object.keys(this._indicators) )
{
this._indicators[i].cancelTimeout();
this._indicators[i].destroy();
}
2022-05-30 17:16:27 +02:00
if ( this._connectId )
{
settings.disconnect( this._connectId );
this._connectId = null;
}
2021-11-09 15:21:21 +01:00
this._indicators = { };
2021-11-09 15:21:21 +01:00
}
}
2022-05-30 17:16:27 +02:00
function init(meta) { // eslint-disable-line no-unused-vars
2021-11-09 15:21:21 +01:00
return new Extension(meta.uuid);
}
2021-11-17 17:15:21 +01:00
function monitoLog ( msg )
{
2022-05-30 17:16:27 +02:00
log ( 'Monito: ' + msg ); // eslint-disable-line no-undef
2021-11-17 17:15:21 +01:00
}
function setColor (stgs, key) {
2021-11-24 17:04:47 +01:00
// monitoLog ( '> %s color %s'.format ( key, stgs.get_string(key) ) );
// monitoLog ( '> style %s'.format ( style ) );
let style = this.widget.get_style ( );
if ( key.match ( /-fg$/ ) )
this.widget.set_style ( style + ';color: ' + stgs.get_string(key) );
else
this.widget.set_style ( style + ';background-color: ' + stgs.get_string(key) );
2021-11-17 17:15:21 +01:00
}