From 01d5e744be70d9a4238d87ae8adaf9f485a2ed7e Mon Sep 17 00:00:00 2001 From: "Peter F. Patel-Schneider" Date: Fri, 25 Aug 2023 17:58:15 -0400 Subject: [PATCH] rules: add rule condition for hostname --- lib/logitech_receiver/diversion.py | 24 ++++++++++++++++++++ lib/solaar/ui/diversion_rules.py | 35 +++++++++++++++++++++++++++++- 2 files changed, 58 insertions(+), 1 deletion(-) diff --git a/lib/logitech_receiver/diversion.py b/lib/logitech_receiver/diversion.py index e38ab9a3a..72ce7de73 100644 --- a/lib/logitech_receiver/diversion.py +++ b/lib/logitech_receiver/diversion.py @@ -1040,6 +1040,29 @@ def data(self): return {'Device': self.devID} +class Host(Condition): + + def __init__(self, host, warn=True): + if not (isinstance(host, str)): + if warn: + _log.warn('rule Host Name argument not a string: %s', host) + self.host = '' + self.host = host + + def __str__(self): + return 'Host: ' + str(self.host) + + def evaluate(self, feature, notification, device, status, last_result): + if _log.isEnabledFor(_DEBUG): + _log.debug('evaluate condition: %s', self) + import socket + hostname = socket.getfqdn() + return hostname.startswith(self.host) + + def data(self): + return {'Host': self.host} + + class Action(RuleComponent): def __init__(self, *args): @@ -1338,6 +1361,7 @@ def data(self): 'MouseGesture': MouseGesture, 'Active': Active, 'Device': Device, + 'Host': Host, 'KeyPress': KeyPress, 'MouseScroll': MouseScroll, 'MouseClick': MouseClick, diff --git a/lib/solaar/ui/diversion_rules.py b/lib/solaar/ui/diversion_rules.py index 31d8fa14e..770bb1f94 100644 --- a/lib/solaar/ui/diversion_rules.py +++ b/lib/solaar/ui/diversion_rules.py @@ -521,6 +521,7 @@ def _menu_insert(self, m, it, below=False): (_('KeyIsDown'), _DIV.KeyIsDown, ''), (_('Active'), _DIV.Active, ''), (_('Device'), _DIV.Device, ''), + (_('Host'), _DIV.Host, ''), (_('Setting'), _DIV.Setting, [None, '', None]), (_('Test'), _DIV.Test, next(iter(_DIV.TESTS))), (_('Test bytes'), _DIV.TestBytes, [0, 1, 0]), @@ -2252,13 +2253,44 @@ def left_label(cls, component): class DeviceUI(_DeviceUI, ConditionUI): CLASS = _DIV.Device - label_text = _('Device originated the current notification.') + label_text = _('Device that originated the current notification.') @classmethod def left_label(cls, component): return _('Device') +class HostUI(ConditionUI): + + CLASS = _DIV.Host + + def create_widgets(self): + self.widgets = {} + self.label = Gtk.Label(valign=Gtk.Align.CENTER, hexpand=True) + self.label.set_text(_('Name of host computer.')) + self.widgets[self.label] = (0, 0, 1, 1) + self.field = Gtk.Entry(halign=Gtk.Align.CENTER, valign=Gtk.Align.CENTER, hexpand=True) + self.field.set_size_request(600, 0) + self.field.connect('changed', self._on_update) + self.widgets[self.field] = (0, 1, 1, 1) + + def show(self, component, editable): + super().show(component, editable) + with self.ignore_changes(): + self.field.set_text(component.host) + + def collect_value(self): + return self.field.get_text() + + @classmethod + def left_label(cls, component): + return _('Host') + + @classmethod + def right_label(cls, component): + return str(component.host) + + class _SettingWithValueUI: ALL_SETTINGS = _all_settings() @@ -2624,6 +2656,7 @@ def _on_update(self, *_args): _DIV.MouseProcess: MouseProcessUI, _DIV.Active: ActiveUI, _DIV.Device: DeviceUI, + _DIV.Host: HostUI, _DIV.Feature: FeatureUI, _DIV.Report: ReportUI, _DIV.Modifiers: ModifiersUI,