diff --git a/extension.js b/extension.js index 63ea243..e1003e5 100644 --- a/extension.js +++ b/extension.js @@ -82,7 +82,11 @@ var VitalsMenuButton = GObject.registerClass({ this._addSettingChangedSignal('menu-centered', this._positionInPanelChanged.bind(this)); this._addSettingChangedSignal('icon-style', this._iconStyleChanged.bind(this)); - let settings = [ 'use-higher-precision', 'alphabetize', 'hide-zeros', 'fixed-widths', 'hide-icons', 'unit', 'memory-measurement', 'include-public-ip', 'network-speed-format', 'storage-measurement', 'include-static-info', 'include-static-gpu-info' ]; + let settings = [ 'use-higher-precision', 'alphabetize', 'hide-zeros', + 'fixed-widths', 'hide-icons', 'unit', + 'memory-measurement', 'include-public-ip', 'network-public-ip-interval', + 'network-public-ip-show-flag', 'network-speed-format', 'storage-measurement', + 'include-static-info', 'include-static-gpu-info' ]; for (let setting of Object.values(settings)) this._addSettingChangedSignal(setting, this._redrawMenu.bind(this)); @@ -276,7 +280,10 @@ var VitalsMenuButton = GObject.registerClass({ this._hotItems[key] = item; this._menuLayout.add_child(item); - if (!this._settings.get_boolean('hide-icons') || key == '_default_icon_') { + let isPublicIP = key.includes('public_ip'); + let shouldHideIcon = isPublicIP && this._settings.get_boolean('network-public-ip-show-flag'); + + if ((!this._settings.get_boolean('hide-icons') || key == '_default_icon_') && !shouldHideIcon) { let icon = this._defaultIcon(key); if (gicon) icon.gicon = gicon; item.add_child(icon); diff --git a/prefs.js b/prefs.js index 07916bd..bdeb9a8 100644 --- a/prefs.js +++ b/prefs.js @@ -47,8 +47,8 @@ const Settings = new GObject.Class({ 'show-memory', 'show-processor', 'show-system', 'show-network', 'show-storage', 'use-higher-precision', 'alphabetize', 'hide-zeros', 'include-public-ip', - 'show-battery', 'fixed-widths', 'hide-icons', - 'menu-centered', 'include-static-info', + 'network-public-ip-show-flag', 'show-battery', 'fixed-widths', + 'hide-icons', 'menu-centered', 'include-static-info', 'show-gpu', 'include-static-gpu-info' ]; for (let key in sensors) { @@ -75,6 +75,9 @@ const Settings = new GObject.Class({ this._settings.bind('update-time', this.builder.get_object('update-time'), 'value', Gio.SettingsBindFlags.DEFAULT); + this._settings.bind('network-public-ip-interval', this.builder.get_object('network-public-ip-interval'), + 'value', Gio.SettingsBindFlags.DEFAULT); + // process individual text entry sensor preferences sensors = [ 'storage-path', 'monitor-cmd' ]; for (let key in sensors) { diff --git a/prefs.ui b/prefs.ui index 950c1b0..b3c8c4b 100644 --- a/prefs.ui +++ b/prefs.ui @@ -8,7 +8,7 @@ 10 - 0 + 1 12 12 6 @@ -16,10 +16,10 @@ vertical - 0 + 1 - 0 + 1 none @@ -52,6 +52,68 @@ + + + 1 + + + horizontal + 12 + 6 + + + 1 + start + Update in minutes + + + + + + 1 + 1 + if-valid + + + 1 + 1440 + 1 + + + + + + + + + + + True + + + True + 10 + 10 + 10 + 10 + + + True + Show country flag + 0 + + + + + True + end + True + + + + + + 100 diff --git a/schemas/org.gnome.shell.extensions.vitals.gschema.xml b/schemas/org.gnome.shell.extensions.vitals.gschema.xml index 7a0decd..fef102d 100644 --- a/schemas/org.gnome.shell.extensions.vitals.gschema.xml +++ b/schemas/org.gnome.shell.extensions.vitals.gschema.xml @@ -81,6 +81,14 @@ Include public IP address Display public IP address of internet connection + + 60 + Public IP refresh interval in minutes + + + true + Show country flag for public IP + 0 Network speed format diff --git a/sensors.js b/sensors.js index cb1669f..7491a45 100644 --- a/sensors.js +++ b/sensors.js @@ -31,6 +31,18 @@ import * as FileModule from './helpers/file.js'; import { gettext as _ } from 'resource:///org/gnome/shell/extensions/extension.js'; import NM from 'gi://NM'; + +function getFlagEmoji(countryCode) { + if (!countryCode || countryCode.length !== 2) return '🌐'; + try { + return countryCode + .toUpperCase() + .replace(/./g, char => String.fromCodePoint(char.charCodeAt(0) + 127397)); + } catch (e) { + return '🌐'; + } +} + let GTop, hasGTop = true; try { ({default: GTop} = await import('gi://GTop')); @@ -53,6 +65,7 @@ export const Sensors = GObject.registerClass({ this._settingChangedSignals = []; this._addSettingChangedSignal('show-gpu', this._reconfigureNvidiaSmiProcess.bind(this)); this._addSettingChangedSignal('update-time', this._reconfigureNvidiaSmiProcess.bind(this)); + this._addSettingChangedSignal('network-public-ip-interval', () => {this._lastPublicIPCheck = 0;}); //this._addSettingChangedSignal('include-static-gpu-info', this._reconfigureNvidiaSmiProcess.bind(this)); this._gpu_drm_vendors = null; @@ -82,14 +95,22 @@ export const Sensors = GObject.registerClass({ } _refreshIPAddress(callback) { - // check IP address new FileModule.File('https://ipv4.corecoding.com').read().then(contents => { - let obj = JSON.parse(contents); - let cc = (obj && typeof obj['countryCode'] === 'string') ? obj['countryCode'].trim().toLowerCase() : ''; - let ip = (obj && typeof obj['IPv4'] === 'string') ? obj['IPv4'].trim() : ''; - let typeOut = (/^[a-z]{2}$/.test(cc)) ? ('network-' + cc) : 'network'; - this._returnValue(callback, 'Public IP', ip, typeOut, 'string'); - }).catch(err => { }); + try { + let obj = JSON.parse(contents); + if (!obj.IPv4) throw new Error("No IP"); + + let flag = getFlagEmoji(obj.countryCode); + let display = this._settings.get_boolean('network-public-ip-show-flag') + ? `${flag} ${obj.IPv4}` + : obj.IPv4; + this._returnValue(callback, 'Public IP', display, 'network', 'string'); + } catch (e) { + this._returnValue(callback, 'Public IP', '🏳️ Parse Error', 'network', 'string'); + } + }).catch(err => { + this._returnValue(callback, 'Public IP', '📡 Offline', 'network', 'string'); + }); } _findStorageDevice() { @@ -312,7 +333,8 @@ export const Sensors = GObject.registerClass({ if (this._settings.get_boolean('include-public-ip')) { // check the public ip every hour or when waking from sleep if (this._next_public_ip_check <= 0) { - this._next_public_ip_check = 3600; + let intervalMinutes = this._settings.get_int('network-public-ip-interval'); + this._next_public_ip_check = intervalMinutes * 60; this._refreshIPAddress(callback); }