More layout

This commit is contained in:
Benjamin Drieu 2021-11-10 17:21:03 +01:00
parent 4e27aa28cf
commit 0e978af3d5
3 changed files with 93 additions and 52 deletions

View File

@ -27,8 +27,6 @@ const GETTEXT_DOMAIN = 'monito';
let _httpSession; let _httpSession;
let _status; let _status;
let _ok_text; let _ok_text;
let _warning_text;
let _critical_text;
const Gettext = imports.gettext.domain(GETTEXT_DOMAIN); const Gettext = imports.gettext.domain(GETTEXT_DOMAIN);
const _ = Gettext.gettext; const _ = Gettext.gettext;
@ -40,10 +38,11 @@ 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 { GObject, St, Soup, Clutter } = imports.gi; const { GObject, St, Soup, Clutter, Gio } = imports.gi;
const SETTINGS_SCHEMA = "org.gnome.shell.extensions.monito@drieu.org"; const SETTINGS_SCHEMA = "org.gnome.shell.extensions.monito@drieu.org";
const Convenience = Me.imports.convenience; const Convenience = Me.imports.convenience;
const Preferences = Me.imports.prefs;
let settings = Convenience.getSettings(SETTINGS_SCHEMA); let settings = Convenience.getSettings(SETTINGS_SCHEMA);
@ -57,22 +56,35 @@ class Indicator extends PanelMenu.Button {
this.initStatus ( ); this.initStatus ( );
// Main Box this.namesBoxes = { };
/* this.warningBoxes = { };
let ok_box = new St.BoxLayout({ style_class: 'monito-ok-box monito-box' }); this.criticalBoxes = { };
_ok_text = new St.Label({ text: String(_status['OK']) }) this.unknownBoxes = { };
ok_box.add_child(_ok_text);
box.add_child(ok_box);
*/
let warning_box = new St.BoxLayout({ style_class: 'monito-warning-box monito-box' });
_warning_text = new St.Label({ text: String(_status['WARNING']) })
warning_box.add_child(_warning_text);
box.add_child(warning_box);
let critical_box = new St.BoxLayout({ style_class: 'monito-critical-box monito-box' }); for ( let _server of Preferences.getServersList() )
_critical_text = new St.Label({ text: String(_status['CRITICAL']) }) {
critical_box.add_child(_critical_text); log ( '> Server ' + _server );
box.add_child(critical_box); let _account_settings = Preferences.getAccountSettings ( _server );
let serverBox = new St.BoxLayout ( { style_class: 'monito-serverbox' } );
box.add_child(serverBox);
let name_box = new St.BoxLayout( { style_class: 'monito-namebox' } );
this.namesBoxes [ _server ] = new St.Label ( { text: _account_settings.get_string ( 'name' ) } );
name_box.add_child ( this.namesBoxes [ _server ] );
serverBox.add_child(name_box);
let warning_box = new St.BoxLayout({ style_class: 'monito-warning-box monito-box' });
this.warningBoxes [ _server ] = new St.Label({ text: String(_status['WARNING']) })
warning_box.add_child ( this.warningBoxes [ _server ] );
serverBox.add_child(warning_box);
let critical_box = new St.BoxLayout({ style_class: 'monito-critical-box monito-box' });
this.criticalBoxes [ _server ] = new St.Label({ text: String(_status['CRITICAL']) })
critical_box.add_child ( this.criticalBoxes [ _server ] );
serverBox.add_child(critical_box);
}
box.add_child(PopupMenu.arrowIcon(St.Side.BOTTOM)); box.add_child(PopupMenu.arrowIcon(St.Side.BOTTOM));
@ -91,6 +103,14 @@ class Indicator extends PanelMenu.Button {
// }); // });
// this.menu.addMenuItem(item); // this.menu.addMenuItem(item);
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);
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);
@ -125,27 +145,28 @@ class Indicator extends PanelMenu.Button {
} }
updateStatus ( ) { updateStatus ( ) {
const SETTINGS_SCHEMA_ACCOUNT = "org.gnome.shell.extensions.monito@drieu.org.account"; for ( let _server of Preferences.getServersList() )
const Convenience = Me.imports.convenience; {
let account_settings = Convenience.getSettings(SETTINGS_SCHEMA_ACCOUNT, '/org/gnome/shell/extensions/monito@drieu.org/account/0/'); let account_settings = Preferences.getAccountSettings ( _server );
let username = account_settings.get_string("username");
let password = account_settings.get_string("password");
let urlcgi = account_settings.get_string("urlcgi");
let username = account_settings.get_string("username"); if ( ! urlcgi )
let password = account_settings.get_string("password"); {
let urlcgi = account_settings.get_string("urlcgi"); log ( 'Not updating monito because no URL configured' );
return;
}
if ( ! urlcgi ) urlcgi = urlcgi.replace ( /^(https?:\/\/)/, '$1' + username + ':' + password + '@' );
{ log ( 'monito >>> ' + urlcgi );
log ( 'Not updating monito because no URL configured' );
return;
}
urlcgi = urlcgi.replace ( /^(https?:\/\/)/, '$1' + username + ':' + password + '@' ); this.load_data_async ( _server, urlcgi, { 'jsonoutput': '' } );
// log ( 'monito >>> ' + urlcgi ); }
this.load_data_async ( urlcgi, { 'jsonoutput': '' }, function(res) { Main.notify(res); } )
} }
createBin(status, text, col) { createBin ( status, text, col ) {
let _widths = [ 300, 300, 200, 50, 600 ]; let _widths = [ 300, 300, 200, 50, 600 ];
let _bin = new St.Bin({ let _bin = new St.Bin({
style_class: 'monito-service-' + status, style_class: 'monito-service-' + status,
@ -156,19 +177,17 @@ class Indicator extends PanelMenu.Button {
return _bin; return _bin;
} }
load_data_async(url, params, fun) { load_data_async ( server, url, params ) {
if (_httpSession === undefined) { if (_httpSession === undefined) {
_httpSession = new Soup.Session(); _httpSession = new Soup.Session();
_httpSession.user_agent = Me.metadata.uuid; _httpSession.user_agent = Me.metadata.uuid;
} else { }
// abort previous requests.
_httpSession.abort();
}
let message = Soup.form_request_new_from_hash('GET', url, params); let message = Soup.form_request_new_from_hash('GET', url, params);
_httpSession.queue_message(message, Lang.bind(this, function(_httpSession, message) { _httpSession.queue_message(message, Lang.bind(this, function(_httpSession, message) {
//Main.notify(message.response_body.data); // Main.notify(message.response_body.data);
log ( '>>> ' + message.response_body.data );
try { try {
// 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);
@ -214,12 +233,12 @@ class Indicator extends PanelMenu.Button {
} }
// _ok_text.set_text ( String(_status.OK) ); // _ok_text.set_text ( String(_status.OK) );
_warning_text.set_text ( String(_status.WARNING) ); this.warningBoxes[server].set_text ( String(_status.WARNING) );
_critical_text.set_text ( String(_status.CRITICAL) ); this.criticalBoxes[server].set_text ( String(_status.CRITICAL) );
} catch (e) { } catch (e) {
Main.notify(_('Zbeu!')); Main.notify(_('Zbeu!'));
_warning_text.set_text ( '…' ); this.warningBoxes[server].set_text ( '…' );
_critical_text.set_text ( '…' ); this.criticalBoxes[server].set_text ( '…' );
log(e); log(e);
return; return;
} }
@ -227,7 +246,7 @@ class Indicator extends PanelMenu.Button {
return; return;
} }
_onPreferencesActivate() { _onPreferencesActivate ( ) {
this.menu.actor.hide(); this.menu.actor.hide();
if (typeof ExtensionUtils.openPrefs === 'function') { if (typeof ExtensionUtils.openPrefs === 'function') {
ExtensionUtils.openPrefs(); ExtensionUtils.openPrefs();
@ -275,6 +294,7 @@ class Extension {
enable() { enable() {
this._indicator = new Indicator(); this._indicator = new Indicator();
Main.panel.addToStatusArea(this._uuid, this._indicator); Main.panel.addToStatusArea(this._uuid, this._indicator);
} }

View File

@ -51,7 +51,8 @@ const prefs = [ { type: Gtk.Entry, label: _('Name'), key: 'name' },
// Like 'extension.js' this is used for any one-time setup like translations. // Like 'extension.js' this is used for any one-time setup like translations.
function init() { function init() {
// log('initializing ${Me.metadata.name} Preferences'); log('initializing ${Me.metadata.name} Preferences');
this.settings = ExtensionUtils.getSettings(SETTINGS_SCHEMA);
} }
@ -60,7 +61,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 } );
@ -104,7 +105,7 @@ function buildPrefsWidget() {
accountsChooserContainer.pack_start(this.accountsChooser, true, true, 0); accountsChooserContainer.pack_start(this.accountsChooser, true, true, 0);
// Account list // Account list
for ( var server_id of this.settings.get_string ( 'servers' ) . split ( ',' ) ) for ( var server_id of this.getServersList ( ) )
this.addAccountLine ( server_id ); this.addAccountLine ( server_id );
this.accountsChooser.connect ( 'row-activated', Lang.bind ( this, this.activateAccountRow ) ); this.accountsChooser.connect ( 'row-activated', Lang.bind ( this, this.activateAccountRow ) );
@ -130,6 +131,16 @@ function buildPrefsWidget() {
return mainVbox; return mainVbox;
} }
function getServersList ( )
{
if ( ! this.settings )
this.settings = ExtensionUtils.getSettings(SETTINGS_SCHEMA);
return this.settings.get_string ( 'servers' ) . split ( ',' );
}
function getAccountSettings ( id ) function getAccountSettings ( id )
{ {
let _path = SETTINGS_SCHEMA_ACCOUNT_PATH + '/' + id + '/'; let _path = SETTINGS_SCHEMA_ACCOUNT_PATH + '/' + id + '/';
@ -166,7 +177,6 @@ function createAccountWidgets ( isActive )
halign: Gtk.Align.FILL, halign: Gtk.Align.FILL,
visible: true, visible: true,
hexpand: true, hexpand: true,
editable: isActive,
can_focus: isActive, can_focus: isActive,
}); });
@ -204,6 +214,7 @@ function createAccountWidgets ( isActive )
} ) ); } ) );
} }
function activateAccountRow ( ) { function activateAccountRow ( ) {
if ( this._accountsWidget ) if ( this._accountsWidget )
@ -264,7 +275,7 @@ function addAccountLine ( server_id )
function createAccount ( ) { function createAccount ( ) {
log ( '> Create Account' ); log ( '> Create Account' );
let _servers = this.settings.get_string ( 'servers' ) . split ( ',' ); let _servers = this.getServersList ( );
let _max = Math.max ( ..._servers ); let _max = Math.max ( ..._servers );
let _server_id = _max + 1; let _server_id = _max + 1;
_servers.push ( _server_id ); _servers.push ( _server_id );
@ -277,12 +288,11 @@ function createAccount ( ) {
} }
function removeAccount ( ) { function removeAccount ( ) {
let _row = this.accountsChooser.get_selected_row(); let _row = this.accountsChooser.get_selected_row();
log('Active:' + _row.server); log('Active:' + _row.server);
let _servers = this.settings.get_string ( 'servers' ) . split ( ',' ); let _servers = this.getServersList ( );
_servers.splice ( _servers.indexOf ( _row.server ), 1 ); _servers.splice ( _servers.indexOf ( _row.server ), 1 );
log ( _servers ); log ( _servers );

View File

@ -29,6 +29,10 @@
margin: 0px; */ margin: 0px; */
} }
.monito-serverbox {
padding-left: 1em;
}
.monito-box { .monito-box {
color: white; color: white;
border: 1px solid white; border: 1px solid white;
@ -38,6 +42,13 @@
line-height: 20px; line-height: 20px;
} }
.monito-namebox {
line-height: 20px;
vertical-align: baseline;
height: 100%;
margin-right: .5em;
}
.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;
} }