Skip to content

Commit

Permalink
rules: add rule condition for hostname
Browse files Browse the repository at this point in the history
  • Loading branch information
pfps committed Aug 25, 2023
1 parent 12f9c01 commit 01d5e74
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
24 changes: 24 additions & 0 deletions lib/logitech_receiver/diversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -1338,6 +1361,7 @@ def data(self):
'MouseGesture': MouseGesture,
'Active': Active,
'Device': Device,
'Host': Host,
'KeyPress': KeyPress,
'MouseScroll': MouseScroll,
'MouseClick': MouseClick,
Expand Down
35 changes: 34 additions & 1 deletion lib/solaar/ui/diversion_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]),
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 01d5e74

Please sign in to comment.