From 0e978af3d50171815f081bb2ad04b086815c4afb Mon Sep 17 00:00:00 2001 From: Benjamin Drieu Date: Wed, 10 Nov 2021 17:21:03 +0100 Subject: [PATCH] More layout --- extension.js | 110 +++++++++++++++++++++++++++++-------------------- prefs.js | 24 +++++++---- stylesheet.css | 11 +++++ 3 files changed, 93 insertions(+), 52 deletions(-) diff --git a/extension.js b/extension.js index 880c9ab..3cd5fd0 100644 --- a/extension.js +++ b/extension.js @@ -27,8 +27,6 @@ const GETTEXT_DOMAIN = 'monito'; let _httpSession; let _status; let _ok_text; -let _warning_text; -let _critical_text; const Gettext = imports.gettext.domain(GETTEXT_DOMAIN); const _ = Gettext.gettext; @@ -40,10 +38,11 @@ const Main = imports.ui.main; const PanelMenu = imports.ui.panelMenu; 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 Convenience = Me.imports.convenience; +const Preferences = Me.imports.prefs; let settings = Convenience.getSettings(SETTINGS_SCHEMA); @@ -57,22 +56,35 @@ class Indicator extends PanelMenu.Button { this.initStatus ( ); - // Main Box -/* - let ok_box = new St.BoxLayout({ style_class: 'monito-ok-box monito-box' }); - _ok_text = new St.Label({ text: String(_status['OK']) }) - 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); + this.namesBoxes = { }; + this.warningBoxes = { }; + this.criticalBoxes = { }; + this.unknownBoxes = { }; - let critical_box = new St.BoxLayout({ style_class: 'monito-critical-box monito-box' }); - _critical_text = new St.Label({ text: String(_status['CRITICAL']) }) - critical_box.add_child(_critical_text); - box.add_child(critical_box); + for ( let _server of Preferences.getServersList() ) + { + log ( '> Server ' + _server ); + 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)); @@ -91,6 +103,14 @@ class Indicator extends PanelMenu.Button { // }); // 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._buttonMenu.actor.add_actor(this._mainLabel); @@ -125,27 +145,28 @@ class Indicator extends PanelMenu.Button { } updateStatus ( ) { - const SETTINGS_SCHEMA_ACCOUNT = "org.gnome.shell.extensions.monito@drieu.org.account"; - const Convenience = Me.imports.convenience; - let account_settings = Convenience.getSettings(SETTINGS_SCHEMA_ACCOUNT, '/org/gnome/shell/extensions/monito@drieu.org/account/0/'); + for ( let _server of Preferences.getServersList() ) + { + 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"); - let password = account_settings.get_string("password"); - let urlcgi = account_settings.get_string("urlcgi"); + if ( ! urlcgi ) + { + log ( 'Not updating monito because no URL configured' ); + return; + } - if ( ! urlcgi ) - { - log ( 'Not updating monito because no URL configured' ); - return; - } + urlcgi = urlcgi.replace ( /^(https?:\/\/)/, '$1' + username + ':' + password + '@' ); + log ( 'monito >>> ' + urlcgi ); - urlcgi = urlcgi.replace ( /^(https?:\/\/)/, '$1' + username + ':' + password + '@' ); -// log ( 'monito >>> ' + urlcgi ); - - this.load_data_async ( urlcgi, { 'jsonoutput': '' }, function(res) { Main.notify(res); } ) + this.load_data_async ( _server, urlcgi, { 'jsonoutput': '' } ); + } } - createBin(status, text, col) { + createBin ( status, text, col ) { let _widths = [ 300, 300, 200, 50, 600 ]; let _bin = new St.Bin({ style_class: 'monito-service-' + status, @@ -156,19 +177,17 @@ class Indicator extends PanelMenu.Button { return _bin; } - load_data_async(url, params, fun) { + load_data_async ( server, url, params ) { if (_httpSession === undefined) { _httpSession = new Soup.Session(); _httpSession.user_agent = Me.metadata.uuid; - } else { - // abort previous requests. - _httpSession.abort(); - } + } let message = Soup.form_request_new_from_hash('GET', url, params); _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 { // log ( 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) ); - _warning_text.set_text ( String(_status.WARNING) ); - _critical_text.set_text ( String(_status.CRITICAL) ); + this.warningBoxes[server].set_text ( String(_status.WARNING) ); + this.criticalBoxes[server].set_text ( String(_status.CRITICAL) ); } catch (e) { Main.notify(_('Zbeu!')); - _warning_text.set_text ( '…' ); - _critical_text.set_text ( '…' ); + this.warningBoxes[server].set_text ( '…' ); + this.criticalBoxes[server].set_text ( '…' ); log(e); return; } @@ -227,7 +246,7 @@ class Indicator extends PanelMenu.Button { return; } - _onPreferencesActivate() { + _onPreferencesActivate ( ) { this.menu.actor.hide(); if (typeof ExtensionUtils.openPrefs === 'function') { ExtensionUtils.openPrefs(); @@ -275,6 +294,7 @@ class Extension { enable() { this._indicator = new Indicator(); + Main.panel.addToStatusArea(this._uuid, this._indicator); } diff --git a/prefs.js b/prefs.js index 4294834..7669d8d 100644 --- a/prefs.js +++ b/prefs.js @@ -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. 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() { // 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 } ); @@ -104,7 +105,7 @@ function buildPrefsWidget() { accountsChooserContainer.pack_start(this.accountsChooser, true, true, 0); // 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.accountsChooser.connect ( 'row-activated', Lang.bind ( this, this.activateAccountRow ) ); @@ -130,6 +131,16 @@ function buildPrefsWidget() { return mainVbox; } + +function getServersList ( ) +{ + if ( ! this.settings ) + this.settings = ExtensionUtils.getSettings(SETTINGS_SCHEMA); + + return this.settings.get_string ( 'servers' ) . split ( ',' ); +} + + function getAccountSettings ( id ) { let _path = SETTINGS_SCHEMA_ACCOUNT_PATH + '/' + id + '/'; @@ -166,7 +177,6 @@ function createAccountWidgets ( isActive ) halign: Gtk.Align.FILL, visible: true, hexpand: true, - editable: isActive, can_focus: isActive, }); @@ -204,6 +214,7 @@ function createAccountWidgets ( isActive ) } ) ); } + function activateAccountRow ( ) { if ( this._accountsWidget ) @@ -264,7 +275,7 @@ function addAccountLine ( server_id ) function createAccount ( ) { log ( '> Create Account' ); - let _servers = this.settings.get_string ( 'servers' ) . split ( ',' ); + let _servers = this.getServersList ( ); let _max = Math.max ( ..._servers ); let _server_id = _max + 1; _servers.push ( _server_id ); @@ -277,12 +288,11 @@ function createAccount ( ) { } - function removeAccount ( ) { let _row = this.accountsChooser.get_selected_row(); log('Active:' + _row.server); - let _servers = this.settings.get_string ( 'servers' ) . split ( ',' ); + let _servers = this.getServersList ( ); _servers.splice ( _servers.indexOf ( _row.server ), 1 ); log ( _servers ); diff --git a/stylesheet.css b/stylesheet.css index 91ef1d5..3dee789 100644 --- a/stylesheet.css +++ b/stylesheet.css @@ -29,6 +29,10 @@ margin: 0px; */ } +.monito-serverbox { + padding-left: 1em; +} + .monito-box { color: white; border: 1px solid white; @@ -38,6 +42,13 @@ 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 { background-color: #FF3300; }