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;
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
|
|
|
const Gettext = imports.gettext.domain('monito');
|
|
|
|
const _ = Gettext.gettext;
|
|
|
|
const N_ = function (e) {
|
|
|
|
return e;
|
|
|
|
};
|
|
|
|
|
|
|
|
const SETTINGS_SCHEMA = "org.gnome.shell.extensions.monito@drieu.org";
|
|
|
|
const SETTINGS_SCHEMA_ACCOUNT = "org.gnome.shell.extensions.monito@drieu.org.account";
|
|
|
|
const SETTINGS_SCHEMA_ACCOUNT_PATH = "/org/gnome/shell/extensions/monito@drieu.org/account";
|
|
|
|
|
|
|
|
|
|
|
|
const prefs = [ { type: Gtk.Entry, label: _('Name'), key: 'name' },
|
|
|
|
{ type: Gtk.ComboBoxText, label: _('Type'), key: 'type' },
|
|
|
|
{ type: Gtk.Entry, label: _('Username'), key: 'username' },
|
|
|
|
{ type: Gtk.Entry, label: _('Password'), key: 'password' },
|
|
|
|
{ type: Gtk.Entry, label: _('URL CGI'), key: 'urlcgi' },
|
|
|
|
];
|
|
|
|
|
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-09 15:26:18 +01:00
|
|
|
// log('initializing ${Me.metadata.name} Preferences');
|
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`
|
|
|
|
this.settings = ExtensionUtils.getSettings(SETTINGS_SCHEMA);
|
|
|
|
|
|
|
|
let mainVbox = new Gtk.Box( { orientation: Gtk.Orientation.VERTICAL } );
|
|
|
|
|
|
|
|
let mainWidget = new Gtk.Notebook( { } );
|
|
|
|
|
|
|
|
let prefsWidget = new Gtk.Grid({
|
|
|
|
margin: 18,
|
|
|
|
column_spacing: 12,
|
|
|
|
row_spacing: 12,
|
|
|
|
column_homogeneous: false,
|
|
|
|
});
|
|
|
|
|
|
|
|
this.prefWidgets = { };
|
|
|
|
|
|
|
|
// Add a simple title and add it to the prefsWidget
|
|
|
|
let title = new Gtk.Label({
|
|
|
|
label: `<b>${Me.metadata.name} Preferences</b>`,
|
|
|
|
halign: Gtk.Align.START,
|
|
|
|
use_markup: true,
|
|
|
|
});
|
|
|
|
prefsWidget.attach(title, 0, 0, 2, 1);
|
|
|
|
|
|
|
|
// Misc Settings (TBD)
|
|
|
|
mainWidget.append_page ( prefsWidget,
|
|
|
|
new Gtk.Label ( { label: 'General', } ) );
|
|
|
|
|
|
|
|
|
|
|
|
// Accounts
|
2021-11-10 15:52:40 +01:00
|
|
|
this._accountsWidgetContainer = new Gtk.Box( { orientation: Gtk.Orientation.HORIZONTAL,
|
2021-11-09 15:21:21 +01:00
|
|
|
homogeneous: false, } );
|
2021-11-10 15:52:40 +01:00
|
|
|
mainWidget.append_page ( this._accountsWidgetContainer,
|
2021-11-09 15:21:21 +01:00
|
|
|
new Gtk.Label ( { label: 'Servers', } ) );
|
|
|
|
|
|
|
|
let accountsChooserContainer = new Gtk.Box( { orientation: Gtk.Orientation.VERTICAL,
|
|
|
|
homogeneous: false, } );
|
2021-11-10 15:52:40 +01:00
|
|
|
this._accountsWidgetContainer.pack_start(accountsChooserContainer, true, true, 0);
|
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 } );
|
2021-11-10 15:52:40 +01:00
|
|
|
accountsChooserContainer.pack_start(this.accountsChooser, true, true, 0);
|
2021-11-09 15:21:21 +01:00
|
|
|
|
|
|
|
// Account list
|
2021-11-10 15:52:40 +01:00
|
|
|
for ( var server_id of this.settings.get_string ( 'servers' ) . split ( ',' ) )
|
|
|
|
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 } );
|
|
|
|
accountsChooserContainer.pack_start(accountsChooserActionBar, true, true, 0);
|
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 ) ;
|
|
|
|
|
|
|
|
mainVbox.pack_start(mainWidget, true, true, 0);
|
|
|
|
mainVbox.show_all();
|
|
|
|
|
|
|
|
return mainVbox;
|
|
|
|
}
|
|
|
|
|
|
|
|
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 createAccountWidgets ( isActive )
|
|
|
|
{
|
|
|
|
// Accounts
|
|
|
|
this._accountsWidget = new Gtk.Grid ( {
|
2021-11-09 15:21:21 +01:00
|
|
|
halign: Gtk.Align.FILL,
|
|
|
|
margin: 18,
|
|
|
|
column_spacing: 12,
|
|
|
|
row_spacing: 12,
|
|
|
|
visible: true,
|
|
|
|
column_homogeneous: false,
|
|
|
|
});
|
2021-11-10 15:52:40 +01:00
|
|
|
this._accountsWidgetContainer.pack_start(this._accountsWidget, true, true, 0);
|
|
|
|
|
|
|
|
this.prefWidgets = { };
|
2021-11-09 15:21:21 +01:00
|
|
|
|
|
|
|
let y = 1;
|
|
|
|
for ( var prefEntry of prefs )
|
|
|
|
{
|
|
|
|
let _label = new Gtk.Label({
|
|
|
|
label: prefEntry.label + ':',
|
|
|
|
visible: true,
|
|
|
|
halign: Gtk.Align.START,
|
|
|
|
});
|
2021-11-10 15:52:40 +01:00
|
|
|
this._accountsWidget.attach ( _label, 0, y, 1, 1 );
|
2021-11-09 15:21:21 +01:00
|
|
|
|
|
|
|
this.prefWidgets[prefEntry.key] = new prefEntry.type({
|
|
|
|
halign: Gtk.Align.FILL,
|
|
|
|
visible: true,
|
|
|
|
hexpand: true,
|
2021-11-10 15:52:40 +01:00
|
|
|
editable: isActive,
|
|
|
|
can_focus: isActive,
|
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' )
|
|
|
|
{
|
|
|
|
[ 'Icinga', 'Icinga2' ].forEach((item) => {
|
|
|
|
this.prefWidgets[prefEntry.key].append_text(item);
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
2021-11-10 15:52:40 +01:00
|
|
|
this._accountsWidget.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++;
|
|
|
|
}
|
|
|
|
|
2021-11-10 15:52:40 +01:00
|
|
|
this.prefWidgets['name'].connect('changed', Lang.bind(this, function () {
|
|
|
|
let _row = this.accountsChooser.get_selected_row();
|
|
|
|
_row.get_child().label = this.prefWidgets['name'].text;
|
2021-11-09 15:21:21 +01:00
|
|
|
|
2021-11-10 15:52:40 +01:00
|
|
|
} ) );
|
2021-11-09 15:21:21 +01:00
|
|
|
}
|
|
|
|
|
2021-11-10 15:52:40 +01:00
|
|
|
function activateAccountRow ( ) {
|
|
|
|
|
|
|
|
if ( this._accountsWidget )
|
|
|
|
this._accountsWidget.destroy ( );
|
|
|
|
this.createAccountWidgets ( true );
|
|
|
|
|
|
|
|
let _row = this.accountsChooser.get_selected_row();
|
|
|
|
log('Active:' + _row.server);
|
|
|
|
let _account_settings = getAccountSettings ( _row.server );
|
|
|
|
|
|
|
|
if ( ! _account_settings )
|
|
|
|
return;
|
|
|
|
|
|
|
|
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.ComboBoxText )
|
|
|
|
{
|
|
|
|
this.prefWidgets[prefEntry.key].set_active(_account_settings.get_enum('type'));
|
|
|
|
this.prefWidgets[prefEntry.key].connect('changed', Lang.bind(this, function (e) {
|
|
|
|
log ( e ) ;
|
|
|
|
log('Active:' + this.prefWidgets['type'].get_active());
|
|
|
|
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-10 15:52:40 +01:00
|
|
|
log ( '> Add line ' + server_id );
|
|
|
|
let _account_settings = getAccountSettings ( server_id );
|
|
|
|
let row = new Gtk.ListBoxRow ( { hexpand: true,
|
|
|
|
halign: Gtk.Align.FILL } );
|
|
|
|
row.server = server_id;
|
|
|
|
|
|
|
|
let _label = new Gtk.Label ( { label: _account_settings.get_string('name'),
|
|
|
|
hexpand: true,
|
|
|
|
margin: 5,
|
|
|
|
halign: Gtk.Align.START,
|
|
|
|
expand: true } );
|
|
|
|
row.add ( _label );
|
|
|
|
this.accountsChooser.add ( row );
|
|
|
|
this.accountsChooser.show_all();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function createAccount ( ) {
|
|
|
|
log ( '> Create Account' );
|
|
|
|
|
|
|
|
let _servers = this.settings.get_string ( 'servers' ) . split ( ',' );
|
|
|
|
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();
|
|
|
|
log('Active:' + _row.server);
|
|
|
|
|
|
|
|
let _servers = this.settings.get_string ( 'servers' ) . split ( ',' );
|
|
|
|
_servers.splice ( _servers.indexOf ( _row.server ), 1 );
|
|
|
|
log ( _servers );
|
|
|
|
|
|
|
|
if ( ! _row )
|
|
|
|
return;
|
|
|
|
|
|
|
|
_row.destroy();
|
|
|
|
this.settings.set_string ( 'servers', _servers . join ( ',' ) );
|
|
|
|
}
|