monito/prefs.js

655 lines
24 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
const Gio = imports.gi.Gio;
const GLib = imports.gi.GLib;
const Gtk = imports.gi.Gtk;
2021-11-17 14:44:04 +01:00
const Gdk = imports.gi.Gdk;
const GObject = imports.gi.GObject;
2021-11-09 15:21:21 +01:00
// It's common practice to keep GNOME API and JS imports in separate blocks
const Lang = imports.lang;
const ExtensionUtils = imports.misc.extensionUtils;
const Me = ExtensionUtils.getCurrentExtension();
const Convenience = Me.imports.convenience;
2021-11-17 14:44:04 +01:00
const Mainloop = imports.mainloop;
2021-11-09 15:21:21 +01:00
const Gettext = imports.gettext.domain('monito');
const _ = Gettext.gettext;
const N_ = function (e) {
return e;
};
2021-11-17 17:15:21 +01:00
const SETTINGS_SCHEMA = "org.gnome.shell.extensions.monito";
const SETTINGS_SCHEMA_ACCOUNT = "org.gnome.shell.extensions.monito.account";
const SETTINGS_SCHEMA_ACCOUNT_PATH = "/org/gnome/shell/extensions/monito/account";
2021-11-09 15:21:21 +01:00
2022-09-09 19:22:46 +02: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 },
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-29 16:07:29 +01:00
const prefs = [
2022-03-22 22:19:07 +01:00
{ type: Gtk.Switch, category: 'Settings', label: _('Active (restart for effect)'), key: 'active', align: Gtk.Align.START },
2021-11-29 16:07:29 +01:00
{ 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: _('Web URL'), key: 'url' },
{ type: Gtk.Entry, category: 'Settings', label: _('CGI / API URL'), key: 'urlcgi' },
2022-03-22 22:19:07 +01:00
{ type: Gtk.Switch, category: 'Settings', label: _('Check SSL certificate'), key: 'strict-ssl', align: Gtk.Align.START },
2021-11-29 16:07:29 +01:00
{ 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: _('Pending background color'), key: 'pending-color', align: Gtk.Align.START },
2021-11-29 16:07:29 +01:00
{ 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.ColorButton, category: 'Colors', label: _('Pending color'), key: 'pending-fg', align: Gtk.Align.START },
{ type: Gtk.Switch, category: 'Filters', label: _('Do <b>not</b> display acknowledged services'), key: 'acknowledged-filter-out', align: Gtk.Align.START },
2021-11-29 16:07:29 +01:00
{ 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: _('Only display status info matching'), key: 'status-info-grep' },
2021-11-29 23:04:17 +01:00
{ type: Gtk.Entry, category: 'Filters', label: _('Do <b>not</b> display hosts matching'), key: 'host-filter-out' },
{ type: Gtk.Entry, category: 'Filters', label: _('Do <b>not</b> display services matching'), key: 'service-filter-out' },
{ type: Gtk.Entry, category: 'Filters', label: _('Do <b>not</b> display status info matching'), key: 'status-info-filter-out' },
2021-11-29 23:04:17 +01:00
{ type: Gtk.Entry, category: 'Replacements', label: _('Host regexp ...'), key: 'host-match' },
2021-11-29 16:07:29 +01:00
{ type: Gtk.Entry, category: 'Replacements', label: _('... to replace with'), key: 'host-replace' },
2021-11-29 23:04:17 +01:00
{ type: Gtk.Entry, category: 'Replacements', label: _('Service regexp ...'), key: 'service-match' },
2021-11-29 16:07:29 +01:00
{ type: Gtk.Entry, category: 'Replacements', label: _('... to replace with'), key: 'service-replace' },
2021-11-29 23:04:17 +01:00
{ type: Gtk.Entry, category: 'Replacements', label: _('Status info regexp ...'), key: 'status-info-match' },
2021-11-29 16:07:29 +01:00
{ type: Gtk.Entry, category: 'Replacements', label: _('... to replace with'), key: 'status-info-replace' },
];
2021-11-09 15:21:21 +01:00
2021-11-10 15:52:40 +01:00
2021-11-09 15:21:21 +01:00
// Like 'extension.js' this is used for any one-time setup like translations.
function init() {
2021-11-17 17:15:21 +01:00
monitoLog('initializing ${Me.metadata.name} Preferences');
2021-11-10 17:21:03 +01:00
this.settings = ExtensionUtils.getSettings(SETTINGS_SCHEMA);
2022-05-30 11:27:11 +02:00
this.gtkVersion = Gtk.get_major_version();
2021-11-09 15:21:21 +01:00
}
// This function is called when the preferences window is first created to build
// and return a Gtk widget. As an example we'll create and return a GtkLabel.
function buildPrefsWidget() {
// Copy the same GSettings code from `extension.js`
2021-11-17 14:44:04 +01:00
this.settings = ExtensionUtils.getSettings(SETTINGS_SCHEMA);
2021-11-09 15:21:21 +01:00
2022-05-30 17:16:27 +02:00
this._columnsStores = { };
2021-11-09 15:21:21 +01:00
let mainVbox = new Gtk.Box( { orientation: Gtk.Orientation.VERTICAL } );
let mainWidget = new Gtk.Notebook( { } );
let prefsWidget = new Gtk.Grid({
column_spacing: 12,
row_spacing: 12,
column_homogeneous: false,
});
2022-05-30 11:27:11 +02:00
if ( this.gtkVersion < 4 )
prefsWidget.margin = 18;
2021-11-09 15:21:21 +01:00
this.prefWidgets = { };
// Add a simple title and add it to the prefsWidget
let title = new Gtk.Label({
label: '<b>' + _('Monito Preferences') + '</b>',
2021-11-09 15:21:21 +01:00
halign: Gtk.Align.START,
use_markup: true,
});
prefsWidget.attach(title, 0, 0, 2, 1);
2021-11-17 14:44:04 +01:00
let _label = new Gtk.Label({
label: _('Poll delay in seconds'),
2021-11-17 14:44:04 +01:00
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 );
_label = new Gtk.Label({
label: _('Web browser'),
visible: true,
halign: Gtk.Align.START,
});
prefsWidget.attach ( _label, 0, 2, 1, 1 );
_entry = new Gtk.Entry ({
halign: Gtk.Align.FILL,
visible: true,
hexpand: true,
can_focus: true,
});
prefsWidget.attach ( _entry, 1, 2, 1, 1 );
this.settings.bind ( 'web-browser', _entry, 'text', Gio.SettingsBindFlags.DEFAULT );
2021-11-09 15:21:21 +01:00
// Misc Settings (TBD)
mainWidget.append_page ( prefsWidget,
new Gtk.Label ( { label: _('General'), } ) );
2021-11-09 15:21:21 +01:00
// Accounts
2021-11-10 15:52:40 +01:00
this._accountsWidgetContainer = new Gtk.Box( { orientation: Gtk.Orientation.HORIZONTAL,
2022-05-28 22:34:01 +02:00
homogeneous: false, } );
2021-11-10 15:52:40 +01:00
mainWidget.append_page ( this._accountsWidgetContainer,
new Gtk.Label ( { label: _('Servers'), } ) );
2021-11-09 15:21:21 +01:00
let accountsChooserContainer = new Gtk.Box( { orientation: Gtk.Orientation.VERTICAL,
homogeneous: false, } );
2022-05-30 11:27:11 +02:00
if ( this.gtkVersion == 4 )
this._accountsWidgetContainer.append(accountsChooserContainer, true, true, 0);
else
this._accountsWidgetContainer.add(accountsChooserContainer);
2021-11-09 15:21:21 +01:00
2021-11-10 15:52:40 +01:00
this.accountsChooser = new Gtk.ListBox ( { valign: Gtk.Align.FILL,
2021-11-09 15:21:21 +01:00
hexpand: true,
vexpand: true } );
2022-05-30 11:27:11 +02:00
if ( this.gtkVersion == 4 )
accountsChooserContainer.append(this.accountsChooser, true, true, 0);
else
accountsChooserContainer.add(this.accountsChooser);
2021-11-09 15:21:21 +01:00
// Account list
2021-11-10 17:21:03 +01:00
for ( var server_id of this.getServersList ( ) )
2021-11-10 15:52:40 +01:00
this.addAccountLine ( server_id );
this.accountsChooser.connect ( 'row-activated', Lang.bind ( this, this.activateAccountRow ) );
2021-11-09 15:21:21 +01:00
2021-11-10 15:52:40 +01:00
// Action Bar
let accountsChooserActionBar = new Gtk.ActionBar ( { valign: Gtk.Align.END } );
2022-05-30 11:27:11 +02:00
if ( this.gtkVersion == 4 )
accountsChooserContainer.append(accountsChooserActionBar, true, true, 0);
else
accountsChooserContainer.add(accountsChooserActionBar);
2021-11-09 15:21:21 +01:00
2021-11-10 15:52:40 +01:00
let accountCreateButton = Gtk.Button.new_from_icon_name ( 'list-add',
Gtk.IconSize.BUTTON );
accountsChooserActionBar.add ( accountCreateButton );
accountCreateButton.connect ( 'clicked', Lang.bind ( this, this.createAccount ) );
2021-11-09 15:21:21 +01:00
2021-11-10 15:52:40 +01:00
let accountRemoveButton = Gtk.Button.new_from_icon_name ( 'list-remove',
Gtk.IconSize.BUTTON );
accountsChooserActionBar.add ( accountRemoveButton );
accountRemoveButton.connect ( 'clicked', Lang.bind ( this, this.removeAccount ) );
2021-11-09 15:21:21 +01:00
2021-11-10 15:52:40 +01:00
this.createAccountWidgets ( false ) ;
2022-05-30 11:27:11 +02:00
if ( this.gtkVersion == 4 )
mainVbox.append(mainWidget, true, true, 0);
else
mainVbox.add(mainWidget);
2021-11-10 15:52:40 +01:00
mainVbox.show_all();
return mainVbox;
}
2021-11-10 17:21:03 +01:00
function getServersList ( )
{
if ( ! this.settings )
this.settings = ExtensionUtils.getSettings(SETTINGS_SCHEMA);
return this.settings.get_string ( 'servers' ) . split ( ',' );
}
2021-11-10 15:52:40 +01:00
function getAccountSettings ( id )
{
let _path = SETTINGS_SCHEMA_ACCOUNT_PATH + '/' + id + '/';
return Convenience.getSettings(SETTINGS_SCHEMA_ACCOUNT, _path);
}
2021-11-09 15:21:21 +01:00
2021-11-10 15:52:40 +01:00
function getSortOrder ( server )
{
return this.getAccountSettings ( server ) . get_strv ( 'columns-order' );
}
function setSortOrder ( server, sort_order )
{
return this.getAccountSettings ( server ) . set_strv ( 'columns-order', sort_order );
}
function getColumns ( server )
{
2022-09-09 19:22:46 +02:00
let _columns = this.getAccountSettings ( server ) . get_strv ( 'columns' );
let _columnsSizes = this.getAccountSettings ( server ) . get_strv ( 'columns-size' );
const zip = (a1, a2) => a1.map((x, i) => { return { name: x, size: a2[i] } } );
let _result = zip ( _columns, _columnsSizes );
return _result;
}
function setColumns ( server, columns )
{
return this.getAccountSettings ( server ) . set_strv ( 'columns', columns );
}
2021-11-10 15:52:40 +01:00
function createAccountWidgets ( isActive )
{
2021-11-24 17:04:47 +01:00
this.prefWidgets = { };
this._accountsWidget = new Gtk.Notebook( { } );
2022-05-30 11:27:11 +02:00
if ( this.gtkVersion == 4 )
this._accountsWidgetContainer.append(this._accountsWidget, true, true, 0);
else
this._accountsWidgetContainer.add(this._accountsWidget);
2021-11-24 17:04:47 +01:00
for ( var _tab of [ 'Settings', 'Columns', 'Colors', 'Filters', 'Replacements' ] )
2021-11-29 16:07:29 +01:00
{
if ( _tab != 'Columns' )
this.createPrefWidgets ( _accountsWidget, _tab, isActive );
else
this.createColumnsPrefTab ( _accountsWidget, _tab, isActive );
2021-11-29 16:07:29 +01:00
}
2021-11-24 17:04:47 +01:00
// Settings
this.prefWidgets['name'].connect('changed', Lang.bind(this, function () {
let _row = this.accountsChooser.get_selected_row();
_row.get_child().label = this.prefWidgets['name'].text;
} ) );
this._accountsWidget.show_all();
}
function createPrefWidgets ( noteBook, type, isActive )
{
let grid = new Gtk.Grid ( {
2021-11-09 15:21:21 +01:00
halign: Gtk.Align.FILL,
2022-05-28 22:34:01 +02:00
// margin: 18,
2021-11-09 15:21:21 +01:00
column_spacing: 12,
row_spacing: 12,
visible: true,
column_homogeneous: false,
});
2022-05-30 11:27:11 +02:00
if ( this.gtkVersion < 4 )
grid.margin = 18;
2021-11-24 17:04:47 +01:00
noteBook.append_page ( grid, new Gtk.Label ( { label: _(type), } ) );
2021-11-17 20:16:53 +01:00
2021-11-24 17:04:47 +01:00
let y = 0;
2021-11-09 15:21:21 +01:00
for ( var prefEntry of prefs )
{
2021-11-24 17:04:47 +01:00
if ( prefEntry.category != type )
continue;
2021-11-09 15:21:21 +01:00
let _label = new Gtk.Label({
label: prefEntry.label + ':',
visible: true,
halign: Gtk.Align.START,
2021-11-29 23:04:17 +01:00
use_markup: true,
2021-11-09 15:21:21 +01:00
});
2021-11-24 17:04:47 +01:00
grid.attach ( _label, 0, y, 1, 1 );
2021-11-09 15:21:21 +01:00
2021-11-17 14:44:04 +01:00
this.prefWidgets[prefEntry.key] = new prefEntry.type ( {
halign: ( prefEntry.align ? prefEntry.align : Gtk.Align.FILL ),
2021-11-09 15:21:21 +01:00
visible: true,
hexpand: true,
2021-11-10 15:52:40 +01:00
can_focus: isActive,
2021-11-17 14:44:04 +01:00
} );
2021-11-09 15:21:21 +01:00
let boundValue = 'text';
if ( prefEntry.key == 'password' )
{
this.prefWidgets[prefEntry.key].set_input_purpose ( Gtk.InputPurpose.PASSWORD );
this.prefWidgets[prefEntry.key].set_visibility ( false );
}
else if ( prefEntry.key == 'type' )
{
2021-12-01 15:27:37 +01:00
[ { name: 'Icinga', value: 'Icinga server' },
2021-12-06 11:34:55 +01:00
{ name: 'Icinga2API', value: 'Icinga2 server (using API, prefered)' },
{ name: 'Icinga2', value: 'Icinga2 server (using Icingaweb2, limited)' } ].forEach((item) => {
2021-12-01 15:27:37 +01:00
this.prefWidgets[prefEntry.key].insert ( -1, item.name, item.value );
2021-11-09 15:21:21 +01:00
} );
}
2021-11-24 17:04:47 +01:00
grid.attach(this.prefWidgets[prefEntry.key], 1, y, 1, 1);
2021-11-09 15:21:21 +01:00
if ( prefEntry.type != Gtk.ComboBoxText )
{
2021-11-09 15:26:18 +01:00
// this.account_settings.bind(
// prefEntry.key,
// _entry,
// boundValue,
// Gio.SettingsBindFlags.DEFAULT
// );
2021-11-09 15:21:21 +01:00
}
y++;
}
}
function createColumnsPrefTab ( noteBook, type, isActive )
{
let grid = new Gtk.Grid ( {
halign: Gtk.Align.FILL,
2022-05-28 22:34:01 +02:00
// margin: 18,
column_spacing: 12,
row_spacing: 12,
visible: true,
column_homogeneous: false,
});
2022-05-30 11:27:11 +02:00
if ( this.gtkVersion < 4 )
grid.margin = 18;
noteBook.append_page ( grid, new Gtk.Label ( { label: _(type), } ) );
this.treeView = new Gtk.TreeView ( { headers_visible: true,
reorderable: true,
hexpand: true,
vexpand: true });
let columnNumbers = new Gtk.TreeViewColumn ( { title: _("Column") } );
2022-09-09 19:22:46 +02:00
this.ColumnNameRenderer = new Gtk.CellRendererCombo ( { editable: true,
has_entry: false,
text_column: 0 } );
this.ColumnNameRenderer.col = 0;
2022-05-30 11:27:11 +02:00
if ( this.gtkVersion == 4 )
2022-09-09 19:22:46 +02:00
columnNumbers.append(this.ColumnNameRenderer, true);
2022-05-30 11:27:11 +02:00
else
2022-09-09 19:22:46 +02:00
columnNumbers.pack_start(this.ColumnNameRenderer, true);
columnNumbers.add_attribute(this.ColumnNameRenderer, 'text', 0);
this.treeView.append_column(columnNumbers);
2022-09-09 19:22:46 +02:00
this.ColumnNameRenderer.connect('edited', Lang.bind ( this, this.editColumn ) );
let _colSize = new Gtk.TreeViewColumn ( { title: _("Size") } );
2022-09-09 19:22:46 +02:00
let _colRenderer = new Gtk.CellRendererSpin ( { adjustment: new Gtk.Adjustment ( { lower: 0, upper: 999, step_increment: 1, page_increment: 10 } ),
editable: true } );
2022-05-30 11:27:11 +02:00
if ( this.gtkVersion == 4 )
_colSize.append(_colRenderer, true);
else
_colSize.pack_start(_colRenderer, true);
_colSize.add_attribute(_colRenderer, 'text', 1);
this.treeView.append_column(_colSize);
_colRenderer.col = 1;
_colRenderer.connect('edited', Lang.bind ( this, this.editColumn ) );
// _treeView.connect('row-activated', this._editPath.bind(this));
grid.attach ( this.treeView, 0, 1, 1, 1 );
2022-05-30 17:16:27 +02:00
// Action Bar
let rowsChooserActionBar = new Gtk.ActionBar ( { valign: Gtk.Align.END } );
let rowCreateButton = Gtk.Button.new_from_icon_name ( 'list-add',
Gtk.IconSize.BUTTON );
rowsChooserActionBar.add ( rowCreateButton );
rowCreateButton.connect ( 'clicked', Lang.bind ( this, this.createColumnRow ) );
let rowRemoveButton = Gtk.Button.new_from_icon_name ( 'list-remove',
Gtk.IconSize.BUTTON );
rowsChooserActionBar.add ( rowRemoveButton );
rowRemoveButton.connect ( 'clicked', Lang.bind ( this, this.removeColumnRow ) );
grid.attach ( rowsChooserActionBar, 0, 2, 1, 1 );
2022-05-30 17:16:27 +02:00
}
2021-11-10 15:52:40 +01:00
function activateAccountRow ( ) {
if ( this._accountsWidget )
this._accountsWidget.destroy ( );
this.createAccountWidgets ( true );
this.store = new Gtk.ListStore();
this.store.set_column_types([GObject.TYPE_STRING, GObject.TYPE_INT]);
this.store.filter_new(null);
this.treeView.model = this.store;
2021-11-10 15:52:40 +01:00
let _row = this.accountsChooser.get_selected_row();
let _account_settings = getAccountSettings ( _row.server );
2022-09-09 19:22:46 +02:00
this.columnsModel = new Gtk.ListStore ( );
this.columnsModel.set_column_types([GObject.TYPE_STRING,GObject.TYPE_STRING]);
for ( let [ _colName, _colDef ] of Object.entries(column_definitions) )
{
let _iter = this.columnsModel.append();
this.columnsModel.set_value(_iter, 0, _colName );
this.columnsModel.set_value(_iter, 1, _colDef.label );
}
this.ColumnNameRenderer.model = this.columnsModel;
2021-11-10 15:52:40 +01:00
if ( ! _account_settings )
return;
2022-09-09 19:22:46 +02:00
let _columns = _account_settings . get_strv ( 'columns' );
let _columnsSizes = _account_settings . get_strv ( 'columns-size' );
for ( var i in _columns )
{
let _iter = this.store.append();
this.store.set_value(_iter, 0, _columns [ i ] );
this.store.set_value(_iter, 1, parseInt(_columnsSizes [ i ]) );
}
2021-11-10 15:52:40 +01:00
for ( var prefEntry of prefs )
{
if ( prefEntry.type == Gtk.Entry )
{
_account_settings.bind (
prefEntry.key,
this.prefWidgets[prefEntry.key],
'text',
Gio.SettingsBindFlags.DEFAULT
);
}
else if ( prefEntry.type == Gtk.Switch )
{
_account_settings.bind (
prefEntry.key,
this.prefWidgets[prefEntry.key],
'active',
Gio.SettingsBindFlags.DEFAULT
);
}
2021-11-17 14:44:04 +01:00
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 );
2021-11-17 15:10:47 +01:00
this.prefWidgets[prefEntry.key].connect('color-set', Lang.bind ( { key: prefEntry.key, settings: _account_settings }, setColor ) );
2021-11-17 14:44:04 +01:00
}
2021-11-10 15:52:40 +01:00
else if ( prefEntry.type == Gtk.ComboBoxText )
{
this.prefWidgets[prefEntry.key].set_active(_account_settings.get_enum('type'));
this.prefWidgets[prefEntry.key].connect('changed', Lang.bind(this, function (e) {
let _account_settings = getAccountSettings ( _row.server );
_account_settings.set_enum('type', this.prefWidgets['type'].get_active());
}));
}
}
}
function addAccountLine ( server_id )
2021-11-09 15:21:21 +01:00
{
2021-11-17 17:15:21 +01:00
monitoLog ( '> Add line ' + server_id );
2021-11-10 15:52:40 +01:00
let _account_settings = getAccountSettings ( server_id );
let row = new Gtk.ListBoxRow ( { hexpand: true,
2022-05-28 22:34:01 +02:00
halign: Gtk.Align.FILL } );
2021-11-10 15:52:40 +01:00
row.server = server_id;
let _label = new Gtk.Label ( { label: _account_settings.get_string('name'),
hexpand: true,
2022-05-28 22:34:01 +02:00
// margin: 5,
halign: Gtk.Align.START } );
2022-05-30 11:27:11 +02:00
if ( this.gtkVersion == 4 )
{
row.set_child ( _label );
this.accountsChooser.append ( row );
}
else
{
_label.margin = 5;
row.add ( _label );
this.accountsChooser.add ( row );
this.accountsChooser.show_all();
}
2021-11-10 15:52:40 +01:00
}
function createAccount ( ) {
2021-11-17 17:15:21 +01:00
monitoLog ( '> Create Account' );
2021-11-10 15:52:40 +01:00
2021-11-10 17:21:03 +01:00
let _servers = this.getServersList ( );
2021-11-10 15:52:40 +01:00
let _max = Math.max ( ..._servers );
let _server_id = _max + 1;
_servers.push ( _server_id );
let _account_settings = getAccountSettings ( _server_id );
_account_settings.set_string ( 'name', _('New server #') + _server_id );
this.addAccountLine ( _max + 1 );
this.settings.set_string ( 'servers', _servers.join(',') );
2021-11-09 15:21:21 +01:00
}
2021-11-10 15:52:40 +01:00
function removeAccount ( ) {
let _row = this.accountsChooser.get_selected_row();
2021-11-17 17:15:21 +01:00
monitoLog('Active:' + _row.server);
2021-11-10 15:52:40 +01:00
2021-11-10 17:21:03 +01:00
let _servers = this.getServersList ( );
2021-11-10 15:52:40 +01:00
_servers.splice ( _servers.indexOf ( _row.server ), 1 );
2021-11-17 17:15:21 +01:00
monitoLog ( _servers );
2021-11-10 15:52:40 +01:00
if ( ! _row )
return;
_row.destroy();
this.settings.set_string ( 'servers', _servers . join ( ',' ) );
}
2021-11-17 14:44:04 +01:00
function setColor ( color )
{
2021-11-17 17:15:21 +01:00
monitoLog ( 'Color ' + + ': ' + color );
2021-11-17 14:44:04 +01:00
let _output = '#%02x%02x%02x'.format(
255 * color.get_rgba().red,
255 * color.get_rgba().green,
255 * color.get_rgba().blue );
2021-11-17 15:10:47 +01:00
this.settings.set_string('' + this.key, _output);
2021-11-17 14:44:04 +01:00
}
2021-11-17 17:15:21 +01:00
2022-05-30 17:16:27 +02:00
function createColumnRow ( ) {
2022-09-09 19:22:46 +02:00
let _item = this.store.append();
this.store.set_value(_item, 0, 'status' );
this.store.set_value(_item, 1, 300 );
2022-05-30 17:16:27 +02:00
2022-09-09 19:22:46 +02:00
let _row = this.accountsChooser.get_selected_row();
let _account_settings = getAccountSettings ( _row.server );
let prefKey;
let _defs = _account_settings . get_strv ( 'columns' );
_defs.push ( 'status' );
_account_settings . set_strv ( 'columns', _defs );
_defs = _account_settings . get_strv ( 'columns-size' );
_defs.push ( '300' );
_account_settings . set_strv ( 'columns-size', _defs );
2022-05-30 17:16:27 +02:00
}
function removeColumnRow ( widget ) {
let [any, model, _iter] = this.treeView.get_selection().get_selected ( );
2022-09-09 19:22:46 +02:00
let _row = this.accountsChooser.get_selected_row();
let _account_settings = getAccountSettings ( _row.server );
let prefKey;
for ( var foo of this.treeView.get_selection().get_selected_rows()[0] )
{
let _defs = _account_settings . get_strv ( 'columns' );
_defs.splice ( foo.get_indices ( ), 1 );
_account_settings . set_strv ( 'columns', _defs );
_defs = _account_settings . get_strv ( 'columns-size' );
_defs.splice ( foo.get_indices ( ), 1 );
_account_settings . set_strv ( 'columns-size', _defs );
}
this.store.remove ( _iter );
}
function editColumn ( widget, path, text )
{
2022-09-09 19:22:46 +02:00
let _row = this.accountsChooser.get_selected_row();
let _account_settings = getAccountSettings ( _row.server );
let prefKey;
let _iter = this.store.get_iter ( Gtk.TreePath.new_from_string ( path ) );
if ( widget.col == 1 )
2022-09-09 19:22:46 +02:00
{
this.store.set_value(_iter[1], widget.col, parseInt(text) );
2022-09-09 19:22:46 +02:00
prefKey = 'columns-size';
}
else
2022-09-09 19:22:46 +02:00
{
this.store.set_value(_iter[1], widget.col, text );
2022-09-09 19:22:46 +02:00
prefKey = 'columns';
}
let _defs = _account_settings . get_strv ( prefKey );
_defs [ parseInt(path) ] = text;
_account_settings . set_strv ( prefKey, _defs );
return true;
2022-05-30 17:16:27 +02:00
}
2021-11-17 17:15:21 +01:00
function monitoLog ( msg )
{
log ( 'Monito: ' + msg );
}