Skip to content

Commit

Permalink
feat: upgrade to Gnome 45
Browse files Browse the repository at this point in the history
- Updated the extension to be compatible with GNOME Shell version 45.
- Refactored the code to use ES6 import syntax.
- Replaced deprecated imports and classes with their updated equivalents.
- Fixed some minor bugs and improved error handling.
- Updated the metadata.json file to reflect the changes.

This commit updates the snx-vpn-indicator extension to be compatible with GNOME Shell version 45.
The code has been refactored to use ES6 import syntax and to replace deprecated imports and classes
with their updated equivalents. Some minor bugs have been fixed and error handling has been improved.
The metadata.json file has also been updated to reflect the changes.
  • Loading branch information
diegodario88 committed Sep 21, 2023
1 parent 0b6a287 commit 3905a03
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 132 deletions.
Binary file modified assets/screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
43 changes: 13 additions & 30 deletions snx-vpn-indicator@diegodario88.github.io/extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,14 @@
* If this extension breaks your desktop you get to keep all of the pieces...
*/

const { NM } = imports.gi;

const ExtensionUtils = imports.misc.extensionUtils;
const Me = ExtensionUtils.getCurrentExtension();
const Util = Me.imports.util;
const Indicator = Me.imports.indicator;

class Extension {
constructor() {
import NM from 'gi://NM';
import { Extension } from 'resource:///org/gnome/shell/extensions/extension.js';
import { SnxIndicator } from './indicator.js';
import { CONSTANTS, NMDeviceStateReason } from './util.js';

export default class SnxVPNExtension extends Extension {
constructor(metadata) {
super(metadata);
this._indicator = null;
this._networkManagerClient = null;
}
Expand All @@ -41,7 +40,7 @@ class Extension {
enable() {
this._networkManagerClient = NM.Client.new(null);
const hasSNX = this.isTunsnxDevicePresent();
this._indicator = new Indicator.SnxIndicator(hasSNX);
this._indicator = new SnxIndicator(hasSNX, this.path);

this._networkManagerClient.connect(
'any-device-added',
Expand Down Expand Up @@ -83,7 +82,7 @@ class Extension {
return this._networkManagerClient
.get_devices()
.map((device) => device.get_description().trim())
.some((description) => description === Util.CONSTANTS['SNX_DEVICE_NAME']);
.some((description) => description === CONSTANTS['SNX_DEVICE_NAME']);
}

/**
Expand All @@ -94,7 +93,7 @@ class Extension {
*/
handleOnAnyDeviceAdded(_, device) {
const description = device.get_description();
const shouldAvoid = description !== Util.CONSTANTS['SNX_DEVICE_NAME'];
const shouldAvoid = description !== CONSTANTS['SNX_DEVICE_NAME'];

if (shouldAvoid) {
return;
Expand All @@ -111,30 +110,14 @@ class Extension {
*/
handleOnAnyDeviceRemoved(_, device) {
const description = device.get_description();
const shouldAvoid = description !== Util.CONSTANTS['SNX_DEVICE_NAME'];
const shouldAvoid = description !== CONSTANTS['SNX_DEVICE_NAME'];

if (shouldAvoid) {
return;
}

const stateReasonCode = device.get_state_reason();
const reason = Util.NMDeviceStateReason[stateReasonCode];
const reason = NMDeviceStateReason[stateReasonCode];
this._indicator.hide(reason);
}
}

/**
* This function is called once when your extension is loaded, not enabled. This
* is a good time to setup translations or anything else you only do once.
*
* You MUST NOT make any changes to GNOME Shell, connect any signals or add any
* MainLoop sources here.
*
* @param {ExtensionMeta} meta - An extension meta object, described below.
* @returns {Object} an object with enable() and disable() methods
*/
function init(meta) {
log(`initializing ${meta.metadata.name} version ${meta.metadata.version}`);
ExtensionUtils.initTranslations();
return new Extension();
}
50 changes: 17 additions & 33 deletions snx-vpn-indicator@diegodario88.github.io/indicator.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,28 @@
const { GObject } = imports.gi;
const QuickSettingsMenu = imports.ui.main.panel.statusArea.quickSettings;
const QuickSettings = imports.ui.quickSettings;

const ExtensionUtils = imports.misc.extensionUtils;
const Me = ExtensionUtils.getCurrentExtension();
const Util = Me.imports.util;
const Toggle = Me.imports.toggle;

var SnxIndicator = GObject.registerClass(
class SnxIndicator extends QuickSettings.SystemIndicator {
_init(hasTunsnxDevice = false) {
import GObject from 'gi://GObject';
import * as Main from 'resource:///org/gnome/shell/ui/main.js';
import {
QuickSettingsMenu,
SystemIndicator
} from 'resource:///org/gnome/shell/ui/quickSettings.js';
import { SnxToggle } from './toggle.js';
import { CONSTANTS } from './util.js';

export const SnxIndicator = GObject.registerClass(
class SnxIndicator extends SystemIndicator {
_init(hasTunsnxDevice = false, cwd) {
super._init();

this._indicator = this._addIndicator();
this._indicator.icon_name = Util.CONSTANTS['ENABLED_VPN_ICON'];
this._indicator.icon_name = CONSTANTS['ENABLED_VPN_ICON'];
this._indicator.visible = hasTunsnxDevice;
this._snxToggle = new Toggle.SnxToggle(hasTunsnxDevice);
this._snxToggle = new SnxToggle(hasTunsnxDevice, cwd);
this.quickSettingsItems.push(this._snxToggle);

this.connect('destroy', () => {
this.quickSettingsItems.forEach((item) => item.destroy());
});

QuickSettingsMenu._indicators.insert_child_at_index(this, 0);
this.addQuickSettingsItems();
}

addQuickSettingsItems() {
QuickSettingsMenu._addItems(this.quickSettingsItems);

if (Util.getGnomeShellVersion() < 44) {
return;
}

for (const item of this.quickSettingsItems) {
QuickSettingsMenu.menu._grid.set_child_below_sibling(
item,
QuickSettingsMenu._backgroundApps.quickSettingsItems[0]
);
}
Main.panel.statusArea.quickSettings.addExternalIndicator(this);
}

/**
Expand All @@ -48,14 +32,14 @@ var SnxIndicator = GObject.registerClass(
log(`[SnxIndicator] hide: ${reason}`);
this._indicator.visible = false;
this._snxToggle.checked = false;
this._snxToggle.icon_name = Util.CONSTANTS['DISABLED_VPN_ICON'];
this._snxToggle.icon_name = CONSTANTS['DISABLED_VPN_ICON'];
this._snxToggle._removeSessionParameters();
}

show() {
this._indicator.visible = true;
this._snxToggle.checked = true;
this._snxToggle.icon_name = Util.CONSTANTS['ENABLED_VPN_ICON'];
this._snxToggle.icon_name = CONSTANTS['ENABLED_VPN_ICON'];
}
}
);
7 changes: 2 additions & 5 deletions snx-vpn-indicator@diegodario88.github.io/metadata.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
{
"uuid": "snx-vpn-indicator@diegodario88.github.io",
"version": 4,
"shell-version": [
"43",
"44"
],
"version": 5,
"shell-version": [ "45" ],
"url": "https://github.com/diegodario88/snx-vpn-indicator",
"name": "SNX VPN Indicator",
"description": "This extension adds VPN functionality to the quickSettings by integrating the SSL Network Extender (SNX CLI) client",
Expand Down
84 changes: 42 additions & 42 deletions snx-vpn-indicator@diegodario88.github.io/toggle.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,48 @@
const Main = imports.ui.main;
const { Gio, GObject } = imports.gi;
const QuickSettings = imports.ui.quickSettings;
const PopupMenu = imports.ui.popupMenu;
const ExtensionUtils = imports.misc.extensionUtils;
const Me = ExtensionUtils.getCurrentExtension();
const Util = Me.imports.util;

var SnxToggle = GObject.registerClass(
class SnxToggle extends QuickSettings.QuickMenuToggle {
_init(hasTunsnxDevice = false) {
import GObject from 'gi://GObject';
import Gio from 'gi://Gio';
import {
PopupMenuSection,
PopupSeparatorMenuItem,
PopupSwitchMenuItem,
PopupMenuItem
} from 'resource:///org/gnome/shell/ui/popupMenu.js';
import { QuickMenuToggle } from 'resource:///org/gnome/shell/ui/quickSettings.js';
import {
CONSTANTS,
VPN_NOTIFY,
execCommunicate,
parseSessionParameters
} from './util.js';

export const SnxToggle = GObject.registerClass(
class SnxToggle extends QuickMenuToggle {
_init(hasTunsnxDevice = false, cwd) {
const config = {
toggleMode: true,
hasMenu: true,
checked: hasTunsnxDevice
};

if (Util.getGnomeShellVersion() > 43) {
config.title = Util.CONSTANTS['SNX_LABEL'];
} else {
config.label = Util.CONSTANTS['SNX_LABEL'];
}
config.title = CONSTANTS['SNX_LABEL'];

super._init(config);

this.cwd = cwd;
this.icon_name = hasTunsnxDevice
? Util.CONSTANTS['ENABLED_VPN_ICON']
: Util.CONSTANTS['DISABLED_VPN_ICON'];
? CONSTANTS['ENABLED_VPN_ICON']
: CONSTANTS['DISABLED_VPN_ICON'];

this.previousCancellable = null;
this._mainItemsSection = new PopupMenu.PopupMenuSection();
this._separator = new PopupMenu.PopupSeparatorMenuItem('Connector');
this._mainItemsSection = new PopupMenuSection();
this._separator = new PopupSeparatorMenuItem('Connector');

this._popupSwitchMenuItem = new PopupMenu.PopupSwitchMenuItem(
Util.CONSTANTS['SNX_LABEL_EXTENDED'],
this._popupSwitchMenuItem = new PopupSwitchMenuItem(
CONSTANTS['SNX_LABEL_EXTENDED'],
this.checked
);

this._mainItemsSection.addMenuItem(this._popupSwitchMenuItem);
this.menu.setHeader(Util.CONSTANTS['ENABLED_VPN_ICON'], _('VPN'));
this.menu.setHeader(CONSTANTS['ENABLED_VPN_ICON'], _('VPN'));
this.menu.addMenuItem(this._mainItemsSection);
this.menu.addMenuItem(this._separator);

Expand Down Expand Up @@ -67,13 +72,11 @@ var SnxToggle = GObject.registerClass(
*/
_addSessionParameters(loginResponse) {
this._removeSessionParameters();
const sessionParams = Util.parseSessionParameters(loginResponse);
const sessionParams = parseSessionParameters(loginResponse);

sessionParams.forEach((session) =>
this.menu.addMenuItem(
new PopupMenu.PopupMenuItem(
`${session.label.trim()} : ${session.value.trim()}`
)
new PopupMenuItem(`${session.label.trim()} : ${session.value.trim()}`)
)
);

Expand All @@ -83,7 +86,7 @@ var SnxToggle = GObject.registerClass(
_removeSessionParameters() {
const items = this.menu._getMenuItems();
items.forEach((item) => {
if (item instanceof PopupMenu.PopupMenuItem) {
if (item instanceof PopupMenuItem) {
item.destroy();
}
});
Expand All @@ -98,7 +101,7 @@ var SnxToggle = GObject.registerClass(
*/
async _handleCheckedAction(cancellable) {
try {
const passwordPromptOutput = await Util.execCommunicate(
const passwordPromptOutput = await execCommunicate(
[
'zenity',
'--password',
Expand All @@ -116,8 +119,8 @@ var SnxToggle = GObject.registerClass(
});
}

const stdout = await Util.execCommunicate(
[`${Me.dir.get_path()}/bridge-snx-cli.sh`, passwordPromptOutput],
const stdout = await execCommunicate(
[`${this.cwd}/bridge-snx-cli.sh`, passwordPromptOutput],
null
);

Expand All @@ -136,21 +139,18 @@ var SnxToggle = GObject.registerClass(

this._addSessionParameters(loginResponse);

Util.VPN_NOTIFY(
VPN_NOTIFY(
_('Successfully connected to VPN'),
Util.CONSTANTS['ENABLED_VPN_ICON']
CONSTANTS['ENABLED_VPN_ICON']
);
} catch (error) {
logError(error);
if (error.code !== 14) {
Util.VPN_NOTIFY(
_(error.message),
Util.CONSTANTS['NO_ROUTE_VPN_ICON']
);
VPN_NOTIFY(_(error.message), CONSTANTS['NO_ROUTE_VPN_ICON']);
}

this.checked = false;
this.icon_name = Util.CONSTANTS['DISABLED_VPN_ICON'];
this.icon_name = CONSTANTS['DISABLED_VPN_ICON'];
}
}

Expand All @@ -161,16 +161,16 @@ var SnxToggle = GObject.registerClass(
*/
async _handleUncheckedAction(cancellable) {
try {
const output = await Util.execCommunicate(
const output = await execCommunicate(
['/usr/bin/snx', '-d'],
null,
cancellable
);

Util.VPN_NOTIFY(_(output), Util.CONSTANTS['DISCONNECTED_VPN_ICON']);
VPN_NOTIFY(_(output), CONSTANTS['DISCONNECTED_VPN_ICON']);
} catch (error) {
logError(error);
Util.VPN_NOTIFY(_(error.message), Util.CONSTANTS['NO_ROUTE_VPN_ICON']);
VPN_NOTIFY(_(error.message), CONSTANTS['NO_ROUTE_VPN_ICON']);
}
}

Expand All @@ -180,7 +180,7 @@ var SnxToggle = GObject.registerClass(
}

const cancellable = new Gio.Cancellable();
this.icon_name = Util.CONSTANTS['ACQUIRING_VPN_ICON'];
this.icon_name = CONSTANTS['ACQUIRING_VPN_ICON'];

if (this.checked) {
this._handleCheckedAction(cancellable);
Expand Down
Loading

0 comments on commit 3905a03

Please sign in to comment.