cq: Format with black and isort
This commit is contained in:
@@ -16,29 +16,28 @@
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
from pathlib import Path
|
||||
from typing import TYPE_CHECKING, Any
|
||||
|
||||
from gi.repository import Gdk
|
||||
from gi.repository import Gtk
|
||||
|
||||
from gajim.common import app
|
||||
from gajim.common.helpers import play_sound_file
|
||||
from gajim.common.util.status import get_uf_show
|
||||
from gajim.plugins.helpers import get_builder
|
||||
from gajim.plugins.plugins_i18n import _
|
||||
from gi.repository import Gdk, Gtk
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from ..triggers import Triggers
|
||||
|
||||
EVENTS: dict[str, Any] = {
|
||||
'message_received': [],
|
||||
"message_received": [],
|
||||
}
|
||||
|
||||
RECIPIENT_TYPES = [
|
||||
'contact',
|
||||
'group',
|
||||
'groupchat',
|
||||
'all'
|
||||
]
|
||||
RECIPIENT_TYPES = ["contact", "group", "groupchat", "all"]
|
||||
|
||||
|
||||
class ConfigDialog(Gtk.ApplicationWindow):
|
||||
@@ -46,7 +45,7 @@ class ConfigDialog(Gtk.ApplicationWindow):
|
||||
Gtk.ApplicationWindow.__init__(self)
|
||||
self.set_application(app.app)
|
||||
self.set_show_menubar(False)
|
||||
self.set_title(_('Triggers Configuration'))
|
||||
self.set_title(_("Triggers Configuration"))
|
||||
self.set_transient_for(transient)
|
||||
self.set_default_size(600, 800)
|
||||
self.set_type_hint(Gdk.WindowTypeHint.DIALOG)
|
||||
@@ -54,7 +53,7 @@ class ConfigDialog(Gtk.ApplicationWindow):
|
||||
self.set_destroy_with_parent(True)
|
||||
|
||||
ui_path = Path(__file__).parent
|
||||
self._ui = get_builder(str(ui_path.resolve() / 'config.ui'))
|
||||
self._ui = get_builder(str(ui_path.resolve() / "config.ui"))
|
||||
|
||||
self._plugin = plugin
|
||||
|
||||
@@ -67,7 +66,7 @@ class ConfigDialog(Gtk.ApplicationWindow):
|
||||
self._initialize()
|
||||
|
||||
self._ui.connect_signals(self)
|
||||
self.connect('destroy', self._on_destroy)
|
||||
self.connect("destroy", self._on_destroy)
|
||||
|
||||
def _on_destroy(self, *args: Any) -> None:
|
||||
for num in list(self._plugin.config.keys()):
|
||||
@@ -78,31 +77,31 @@ class ConfigDialog(Gtk.ApplicationWindow):
|
||||
def _initialize(self) -> None:
|
||||
# Fill window
|
||||
widgets = [
|
||||
'conditions_treeview',
|
||||
'config_box',
|
||||
'event_combobox',
|
||||
'recipient_type_combobox',
|
||||
'recipient_list_entry',
|
||||
'delete_button',
|
||||
'online_cb',
|
||||
'away_cb',
|
||||
'xa_cb',
|
||||
'dnd_cb',
|
||||
'use_sound_cb',
|
||||
'disable_sound_cb',
|
||||
'use_popup_cb',
|
||||
'disable_popup_cb',
|
||||
'tab_opened_cb',
|
||||
'not_tab_opened_cb',
|
||||
'has_focus_cb',
|
||||
'not_has_focus_cb',
|
||||
'filechooser',
|
||||
'sound_file_box',
|
||||
'up_button',
|
||||
'down_button',
|
||||
'run_command_cb',
|
||||
'command_entry',
|
||||
'one_shot_cb'
|
||||
"conditions_treeview",
|
||||
"config_box",
|
||||
"event_combobox",
|
||||
"recipient_type_combobox",
|
||||
"recipient_list_entry",
|
||||
"delete_button",
|
||||
"online_cb",
|
||||
"away_cb",
|
||||
"xa_cb",
|
||||
"dnd_cb",
|
||||
"use_sound_cb",
|
||||
"disable_sound_cb",
|
||||
"use_popup_cb",
|
||||
"disable_popup_cb",
|
||||
"tab_opened_cb",
|
||||
"not_tab_opened_cb",
|
||||
"has_focus_cb",
|
||||
"not_has_focus_cb",
|
||||
"filechooser",
|
||||
"sound_file_box",
|
||||
"up_button",
|
||||
"down_button",
|
||||
"run_command_cb",
|
||||
"command_entry",
|
||||
"one_shot_cb",
|
||||
]
|
||||
for widget in widgets:
|
||||
self._ui.__dict__[widget] = self._ui.get_object(widget)
|
||||
@@ -118,17 +117,17 @@ class ConfigDialog(Gtk.ApplicationWindow):
|
||||
self._ui.conditions_treeview.set_model(model)
|
||||
|
||||
# '#' Means number
|
||||
col = Gtk.TreeViewColumn(_('#'))
|
||||
col = Gtk.TreeViewColumn(_("#"))
|
||||
self._ui.conditions_treeview.append_column(col)
|
||||
renderer = Gtk.CellRendererText()
|
||||
col.pack_start(renderer, expand=False)
|
||||
col.add_attribute(renderer, 'text', 0)
|
||||
col.add_attribute(renderer, "text", 0)
|
||||
|
||||
col = Gtk.TreeViewColumn(_('Condition'))
|
||||
col = Gtk.TreeViewColumn(_("Condition"))
|
||||
self._ui.conditions_treeview.append_column(col)
|
||||
renderer = Gtk.CellRendererText()
|
||||
col.pack_start(renderer, expand=True)
|
||||
col.add_attribute(renderer, 'text', 1)
|
||||
col.add_attribute(renderer, "text", 1)
|
||||
else:
|
||||
model = self._ui.conditions_treeview.get_model()
|
||||
|
||||
@@ -137,7 +136,7 @@ class ConfigDialog(Gtk.ApplicationWindow):
|
||||
# Fill conditions_treeview
|
||||
num = 0
|
||||
while num in self._config:
|
||||
iter_ = model.append((num, ''))
|
||||
iter_ = model.append((num, ""))
|
||||
path = model.get_path(iter_)
|
||||
self._ui.conditions_treeview.set_cursor(path)
|
||||
self._active_num = num
|
||||
@@ -154,13 +153,13 @@ class ConfigDialog(Gtk.ApplicationWindow):
|
||||
self._ui.up_button.set_sensitive(False)
|
||||
|
||||
filter_ = Gtk.FileFilter()
|
||||
filter_.set_name(_('All Files'))
|
||||
filter_.add_pattern('*')
|
||||
filter_.set_name(_("All Files"))
|
||||
filter_.add_pattern("*")
|
||||
self._ui.filechooser.add_filter(filter_)
|
||||
|
||||
filter_ = Gtk.FileFilter()
|
||||
filter_.set_name(_('Wav Sounds'))
|
||||
filter_.add_pattern('*.wav')
|
||||
filter_.set_name(_("Wav Sounds"))
|
||||
filter_.add_pattern("*.wav")
|
||||
self._ui.filechooser.add_filter(filter_)
|
||||
self._ui.filechooser.set_filter(filter_)
|
||||
|
||||
@@ -172,96 +171,95 @@ class ConfigDialog(Gtk.ApplicationWindow):
|
||||
return
|
||||
|
||||
# event
|
||||
value = self._config[self._active_num]['event']
|
||||
value = self._config[self._active_num]["event"]
|
||||
legacy_values = [
|
||||
'contact_connected',
|
||||
'contact_disconnected',
|
||||
'contact_status_change']
|
||||
"contact_connected",
|
||||
"contact_disconnected",
|
||||
"contact_status_change",
|
||||
]
|
||||
if value and value not in legacy_values:
|
||||
self._ui.event_combobox.set_active(
|
||||
list(EVENTS.keys()).index(value))
|
||||
self._ui.event_combobox.set_active(list(EVENTS.keys()).index(value))
|
||||
else:
|
||||
self._ui.event_combobox.set_active(-1)
|
||||
|
||||
# recipient_type
|
||||
value = self._config[self._active_num]['recipient_type']
|
||||
value = self._config[self._active_num]["recipient_type"]
|
||||
if value:
|
||||
self._ui.recipient_type_combobox.set_active(
|
||||
RECIPIENT_TYPES.index(value))
|
||||
self._ui.recipient_type_combobox.set_active(RECIPIENT_TYPES.index(value))
|
||||
else:
|
||||
self._ui.recipient_type_combobox.set_active(-1)
|
||||
|
||||
# recipient
|
||||
value = self._config[self._active_num]['recipients']
|
||||
value = self._config[self._active_num]["recipients"]
|
||||
if not value:
|
||||
value = ''
|
||||
value = ""
|
||||
self._ui.recipient_list_entry.set_text(value)
|
||||
|
||||
# status
|
||||
value = self._config[self._active_num]['status']
|
||||
if value == 'all':
|
||||
value = self._config[self._active_num]["status"]
|
||||
if value == "all":
|
||||
self._ui.all_status_rb.set_active(True)
|
||||
else:
|
||||
self._ui.special_status_rb.set_active(True)
|
||||
values = value.split()
|
||||
for val in ('online', 'away', 'xa', 'dnd'):
|
||||
for val in ("online", "away", "xa", "dnd"):
|
||||
if val in values:
|
||||
self._ui.__dict__[val + '_cb'].set_active(True)
|
||||
self._ui.__dict__[val + "_cb"].set_active(True)
|
||||
else:
|
||||
self._ui.__dict__[val + '_cb'].set_active(False)
|
||||
self._ui.__dict__[val + "_cb"].set_active(False)
|
||||
|
||||
self._on_status_radiobutton_toggled(self._ui.all_status_rb)
|
||||
|
||||
# tab_opened
|
||||
value = self._config[self._active_num]['tab_opened']
|
||||
value = self._config[self._active_num]["tab_opened"]
|
||||
self._ui.tab_opened_cb.set_active(True)
|
||||
self._ui.not_tab_opened_cb.set_active(True)
|
||||
if value == 'no':
|
||||
if value == "no":
|
||||
self._ui.tab_opened_cb.set_active(False)
|
||||
elif value == 'yes':
|
||||
elif value == "yes":
|
||||
self._ui.not_tab_opened_cb.set_active(False)
|
||||
|
||||
# has_focus
|
||||
if 'has_focus' not in self._config[self._active_num]:
|
||||
self._config[self._active_num]['has_focus'] = 'both'
|
||||
value = self._config[self._active_num]['has_focus']
|
||||
if "has_focus" not in self._config[self._active_num]:
|
||||
self._config[self._active_num]["has_focus"] = "both"
|
||||
value = self._config[self._active_num]["has_focus"]
|
||||
self._ui.has_focus_cb.set_active(True)
|
||||
self._ui.not_has_focus_cb.set_active(True)
|
||||
if value == 'no':
|
||||
if value == "no":
|
||||
self._ui.has_focus_cb.set_active(False)
|
||||
elif value == 'yes':
|
||||
elif value == "yes":
|
||||
self._ui.not_has_focus_cb.set_active(False)
|
||||
|
||||
# sound_file
|
||||
value = self._config[self._active_num]['sound_file']
|
||||
value = self._config[self._active_num]["sound_file"]
|
||||
if value is None:
|
||||
self._ui.filechooser.unselect_all()
|
||||
else:
|
||||
self._ui.filechooser.set_filename(value)
|
||||
|
||||
# sound, popup, auto_open, systray, roster
|
||||
for option in ('sound', 'popup'):
|
||||
for option in ("sound", "popup"):
|
||||
value = self._config[self._active_num][option]
|
||||
if value == 'yes':
|
||||
self._ui.__dict__['use_' + option + '_cb'].set_active(True)
|
||||
if value == "yes":
|
||||
self._ui.__dict__["use_" + option + "_cb"].set_active(True)
|
||||
else:
|
||||
self._ui.__dict__['use_' + option + '_cb'].set_active(False)
|
||||
if value == 'no':
|
||||
self._ui.__dict__['disable_' + option + '_cb'].set_active(True)
|
||||
self._ui.__dict__["use_" + option + "_cb"].set_active(False)
|
||||
if value == "no":
|
||||
self._ui.__dict__["disable_" + option + "_cb"].set_active(True)
|
||||
else:
|
||||
self._ui.__dict__['disable_' + option + '_cb'].set_active(False)
|
||||
self._ui.__dict__["disable_" + option + "_cb"].set_active(False)
|
||||
|
||||
# run_command
|
||||
value = self._config[self._active_num]['run_command']
|
||||
value = self._config[self._active_num]["run_command"]
|
||||
self._ui.run_command_cb.set_active(value)
|
||||
|
||||
# command
|
||||
value = self._config[self._active_num]['command']
|
||||
value = self._config[self._active_num]["command"]
|
||||
self._ui.command_entry.set_text(value)
|
||||
|
||||
# one shot
|
||||
if 'one_shot' in self._config[self._active_num]:
|
||||
value = self._config[self._active_num]['one_shot']
|
||||
if "one_shot" in self._config[self._active_num]:
|
||||
value = self._config[self._active_num]["one_shot"]
|
||||
else:
|
||||
value = False
|
||||
self._ui.one_shot_cb.set_active(value)
|
||||
@@ -272,34 +270,34 @@ class ConfigDialog(Gtk.ApplicationWindow):
|
||||
if not iter_:
|
||||
return
|
||||
ind = self._ui.event_combobox.get_active()
|
||||
event = ''
|
||||
event = ""
|
||||
if ind > -1:
|
||||
event = self._ui.event_combobox.get_model()[ind][0]
|
||||
ind = self._ui.recipient_type_combobox.get_active()
|
||||
recipient_type = ''
|
||||
recipient_type = ""
|
||||
if ind > -1:
|
||||
recipient_type_model = self._ui.recipient_type_combobox.get_model()
|
||||
recipient_type = recipient_type_model[ind][0]
|
||||
recipient = ''
|
||||
if recipient_type != 'everybody':
|
||||
recipient = ""
|
||||
if recipient_type != "everybody":
|
||||
recipient = self._ui.recipient_list_entry.get_text()
|
||||
if self._ui.all_status_rb.get_active():
|
||||
status = ''
|
||||
status = ""
|
||||
else:
|
||||
status = _('and I am ')
|
||||
for st in ('online', 'away', 'xa', 'dnd'):
|
||||
if self._ui.__dict__[st + '_cb'].get_active():
|
||||
status += get_uf_show(st) + ' '
|
||||
model[iter_][1] = _('%(event)s (%(recipient_type)s) %(recipient)s '
|
||||
'%(status)s') % {
|
||||
'event': event,
|
||||
'recipient_type': recipient_type,
|
||||
'recipient': recipient,
|
||||
'status': status}
|
||||
status = _("and I am ")
|
||||
for st in ("online", "away", "xa", "dnd"):
|
||||
if self._ui.__dict__[st + "_cb"].get_active():
|
||||
status += get_uf_show(st) + " "
|
||||
model[iter_][1] = _(
|
||||
"%(event)s (%(recipient_type)s) %(recipient)s " "%(status)s"
|
||||
) % {
|
||||
"event": event,
|
||||
"recipient_type": recipient_type,
|
||||
"recipient": recipient,
|
||||
"status": status,
|
||||
}
|
||||
|
||||
def _on_conditions_treeview_cursor_changed(self,
|
||||
widget: Gtk.TreeView
|
||||
) -> None:
|
||||
def _on_conditions_treeview_cursor_changed(self, widget: Gtk.TreeView) -> None:
|
||||
|
||||
(model, iter_) = widget.get_selection().get_selected()
|
||||
if not iter_:
|
||||
@@ -325,20 +323,20 @@ class ConfigDialog(Gtk.ApplicationWindow):
|
||||
model = self._ui.conditions_treeview.get_model()
|
||||
num = self._ui.conditions_treeview.get_model().iter_n_children(None)
|
||||
self._config[num] = {
|
||||
'event': 'message_received',
|
||||
'recipient_type': 'all',
|
||||
'recipients': '',
|
||||
'status': 'all',
|
||||
'tab_opened': 'both',
|
||||
'has_focus': 'both',
|
||||
'sound': '',
|
||||
'sound_file': '',
|
||||
'popup': '',
|
||||
'run_command': False,
|
||||
'command': '',
|
||||
'one_shot': False,
|
||||
"event": "message_received",
|
||||
"recipient_type": "all",
|
||||
"recipients": "",
|
||||
"status": "all",
|
||||
"tab_opened": "both",
|
||||
"has_focus": "both",
|
||||
"sound": "",
|
||||
"sound_file": "",
|
||||
"popup": "",
|
||||
"run_command": False,
|
||||
"command": "",
|
||||
"one_shot": False,
|
||||
}
|
||||
iter_ = model.append((num, ''))
|
||||
iter_ = model.append((num, ""))
|
||||
path = model.get_path(iter_)
|
||||
self._ui.conditions_treeview.set_cursor(path)
|
||||
self._active_num = num
|
||||
@@ -380,8 +378,7 @@ class ConfigDialog(Gtk.ApplicationWindow):
|
||||
path = model.get_path(iter_)
|
||||
iter_ = model.get_iter((path[0] - 1,))
|
||||
model[iter_][0] = self._active_num
|
||||
self._on_conditions_treeview_cursor_changed(
|
||||
self._ui.conditions_treeview)
|
||||
self._on_conditions_treeview_cursor_changed(self._ui.conditions_treeview)
|
||||
|
||||
def _on_down_button_clicked(self, _button: Gtk.Button) -> None:
|
||||
selection = self._ui.conditions_treeview.get_selection()
|
||||
@@ -395,8 +392,7 @@ class ConfigDialog(Gtk.ApplicationWindow):
|
||||
model[iter_][0] = self._active_num + 1
|
||||
iter_ = model.iter_next(iter_)
|
||||
model[iter_][0] = self._active_num
|
||||
self._on_conditions_treeview_cursor_changed(
|
||||
self._ui.conditions_treeview)
|
||||
self._on_conditions_treeview_cursor_changed(self._ui.conditions_treeview)
|
||||
|
||||
def _on_event_combobox_changed(self, combo: Gtk.ComboBox) -> None:
|
||||
if self._active_num < 0:
|
||||
@@ -405,21 +401,19 @@ class ConfigDialog(Gtk.ApplicationWindow):
|
||||
if active == -1:
|
||||
return
|
||||
event = list(EVENTS.keys())[active]
|
||||
self._config[self._active_num]['event'] = event
|
||||
self._config[self._active_num]["event"] = event
|
||||
for widget in EVENTS[event]:
|
||||
self._ui.__dict__[widget].set_sensitive(False)
|
||||
self._ui.__dict__[widget].set_state(False)
|
||||
self._set_treeview_string()
|
||||
|
||||
def _on_recipient_type_combobox_changed(self,
|
||||
widget: Gtk.ComboBox
|
||||
) -> None:
|
||||
def _on_recipient_type_combobox_changed(self, widget: Gtk.ComboBox) -> None:
|
||||
|
||||
if self._active_num < 0:
|
||||
return
|
||||
recipient_type = RECIPIENT_TYPES[widget.get_active()]
|
||||
self._config[self._active_num]['recipient_type'] = recipient_type
|
||||
if recipient_type == 'all':
|
||||
self._config[self._active_num]["recipient_type"] = recipient_type
|
||||
if recipient_type == "all":
|
||||
self._ui.recipient_list_entry.set_sensitive(False)
|
||||
else:
|
||||
self._ui.recipient_list_entry.set_sensitive(True)
|
||||
@@ -430,19 +424,19 @@ class ConfigDialog(Gtk.ApplicationWindow):
|
||||
return
|
||||
recipients = widget.get_text()
|
||||
# TODO: do some check
|
||||
self._config[self._active_num]['recipients'] = recipients
|
||||
self._config[self._active_num]["recipients"] = recipients
|
||||
self._set_treeview_string()
|
||||
|
||||
def _set_status_config(self) -> None:
|
||||
if self._active_num < 0:
|
||||
return
|
||||
status = ''
|
||||
for st in ('online', 'away', 'xa', 'dnd'):
|
||||
if self._ui.__dict__[st + '_cb'].get_active():
|
||||
status += st + ' '
|
||||
status = ""
|
||||
for st in ("online", "away", "xa", "dnd"):
|
||||
if self._ui.__dict__[st + "_cb"].get_active():
|
||||
status += st + " "
|
||||
if status:
|
||||
status = status[:-1]
|
||||
self._config[self._active_num]['status'] = status
|
||||
self._config[self._active_num]["status"] = status
|
||||
self._set_treeview_string()
|
||||
|
||||
def _on_status_radiobutton_toggled(self, _widget: Gtk.RadioButton) -> None:
|
||||
@@ -450,16 +444,16 @@ class ConfigDialog(Gtk.ApplicationWindow):
|
||||
return
|
||||
if self._ui.all_status_rb.get_active():
|
||||
self._ui.status_expander.set_expanded(False)
|
||||
self._config[self._active_num]['status'] = 'all'
|
||||
self._config[self._active_num]["status"] = "all"
|
||||
# 'All status' clicked
|
||||
for st in ('online', 'away', 'xa', 'dnd'):
|
||||
self._ui.__dict__[st + '_cb'].set_sensitive(False)
|
||||
for st in ("online", "away", "xa", "dnd"):
|
||||
self._ui.__dict__[st + "_cb"].set_sensitive(False)
|
||||
else:
|
||||
self._ui.status_expander.set_expanded(True)
|
||||
self._set_status_config()
|
||||
# 'special status' clicked
|
||||
for st in ('online', 'away', 'xa', 'dnd'):
|
||||
self._ui.__dict__[st + '_cb'].set_sensitive(True)
|
||||
for st in ("online", "away", "xa", "dnd"):
|
||||
self._ui.__dict__[st + "_cb"].set_sensitive(True)
|
||||
|
||||
self._set_treeview_string()
|
||||
|
||||
@@ -476,26 +470,26 @@ class ConfigDialog(Gtk.ApplicationWindow):
|
||||
self._ui.has_focus_cb.set_sensitive(True)
|
||||
self._ui.not_has_focus_cb.set_sensitive(True)
|
||||
if self._ui.not_tab_opened_cb.get_active():
|
||||
self._config[self._active_num]['tab_opened'] = 'both'
|
||||
self._config[self._active_num]["tab_opened"] = "both"
|
||||
else:
|
||||
self._config[self._active_num]['tab_opened'] = 'yes'
|
||||
self._config[self._active_num]["tab_opened"] = "yes"
|
||||
else:
|
||||
self._ui.has_focus_cb.set_sensitive(False)
|
||||
self._ui.not_has_focus_cb.set_sensitive(False)
|
||||
self._ui.not_tab_opened_cb.set_active(True)
|
||||
self._config[self._active_num]['tab_opened'] = 'no'
|
||||
self._config[self._active_num]["tab_opened"] = "no"
|
||||
|
||||
def _on_not_tab_opened_cb_toggled(self, widget: Gtk.CheckButton) -> None:
|
||||
if self._active_num < 0:
|
||||
return
|
||||
if widget.get_active():
|
||||
if self._ui.tab_opened_cb.get_active():
|
||||
self._config[self._active_num]['tab_opened'] = 'both'
|
||||
self._config[self._active_num]["tab_opened"] = "both"
|
||||
else:
|
||||
self._config[self._active_num]['tab_opened'] = 'no'
|
||||
self._config[self._active_num]["tab_opened"] = "no"
|
||||
else:
|
||||
self._ui.tab_opened_cb.set_active(True)
|
||||
self._config[self._active_num]['tab_opened'] = 'yes'
|
||||
self._config[self._active_num]["tab_opened"] = "yes"
|
||||
|
||||
# has_focus OR (not xor) not_has_focus must be active
|
||||
def _on_has_focus_cb_toggled(self, widget: Gtk.CheckButton) -> None:
|
||||
@@ -503,87 +497,83 @@ class ConfigDialog(Gtk.ApplicationWindow):
|
||||
return
|
||||
if widget.get_active():
|
||||
if self._ui.not_has_focus_cb.get_active():
|
||||
self._config[self._active_num]['has_focus'] = 'both'
|
||||
self._config[self._active_num]["has_focus"] = "both"
|
||||
else:
|
||||
self._config[self._active_num]['has_focus'] = 'yes'
|
||||
self._config[self._active_num]["has_focus"] = "yes"
|
||||
else:
|
||||
self._ui.not_has_focus_cb.set_active(True)
|
||||
self._config[self._active_num]['has_focus'] = 'no'
|
||||
self._config[self._active_num]["has_focus"] = "no"
|
||||
|
||||
def _on_not_has_focus_cb_toggled(self, widget: Gtk.CheckButton) -> None:
|
||||
if self._active_num < 0:
|
||||
return
|
||||
if widget.get_active():
|
||||
if self._ui.has_focus_cb.get_active():
|
||||
self._config[self._active_num]['has_focus'] = 'both'
|
||||
self._config[self._active_num]["has_focus"] = "both"
|
||||
else:
|
||||
self._config[self._active_num]['has_focus'] = 'no'
|
||||
self._config[self._active_num]["has_focus"] = "no"
|
||||
else:
|
||||
self._ui.has_focus_cb.set_active(True)
|
||||
self._config[self._active_num]['has_focus'] = 'yes'
|
||||
self._config[self._active_num]["has_focus"] = "yes"
|
||||
|
||||
def _on_use_it_toggled(self,
|
||||
widget: Gtk.CheckButton,
|
||||
opposite_widget: Gtk.CheckButton,
|
||||
option: str
|
||||
) -> None:
|
||||
def _on_use_it_toggled(
|
||||
self, widget: Gtk.CheckButton, opposite_widget: Gtk.CheckButton, option: str
|
||||
) -> None:
|
||||
|
||||
if widget.get_active():
|
||||
if opposite_widget.get_active():
|
||||
opposite_widget.set_active(False)
|
||||
self._config[self._active_num][option] = 'yes'
|
||||
self._config[self._active_num][option] = "yes"
|
||||
elif opposite_widget.get_active():
|
||||
self._config[self._active_num][option] = 'no'
|
||||
self._config[self._active_num][option] = "no"
|
||||
else:
|
||||
self._config[self._active_num][option] = ''
|
||||
self._config[self._active_num][option] = ""
|
||||
|
||||
def _on_disable_it_toggled(self,
|
||||
widget: Gtk.CheckButton,
|
||||
opposite_widget: Gtk.CheckButton,
|
||||
option: str
|
||||
) -> None:
|
||||
def _on_disable_it_toggled(
|
||||
self, widget: Gtk.CheckButton, opposite_widget: Gtk.CheckButton, option: str
|
||||
) -> None:
|
||||
|
||||
if widget.get_active():
|
||||
if opposite_widget.get_active():
|
||||
opposite_widget.set_active(False)
|
||||
self._config[self._active_num][option] = 'no'
|
||||
self._config[self._active_num][option] = "no"
|
||||
elif opposite_widget.get_active():
|
||||
self._config[self._active_num][option] = 'yes'
|
||||
self._config[self._active_num][option] = "yes"
|
||||
else:
|
||||
self._config[self._active_num][option] = ''
|
||||
self._config[self._active_num][option] = ""
|
||||
|
||||
def _on_use_sound_cb_toggled(self, widget: Gtk.CheckButton) -> None:
|
||||
self._on_use_it_toggled(widget, self._ui.disable_sound_cb, 'sound')
|
||||
self._on_use_it_toggled(widget, self._ui.disable_sound_cb, "sound")
|
||||
if widget.get_active():
|
||||
self._ui.sound_file_box.set_sensitive(True)
|
||||
else:
|
||||
self._ui.sound_file_box.set_sensitive(False)
|
||||
|
||||
def _on_sound_file_set(self, widget: Gtk.FileChooserButton) -> None:
|
||||
self._config[self._active_num]['sound_file'] = widget.get_filename()
|
||||
self._config[self._active_num]["sound_file"] = widget.get_filename()
|
||||
|
||||
def _on_play_button_clicked(self, _button: Gtk.Button) -> None:
|
||||
play_sound_file(self._ui.filechooser.get_filename())
|
||||
|
||||
def _on_disable_sound_cb_toggled(self, widget: Gtk.CheckButton) -> None:
|
||||
self._on_disable_it_toggled(widget, self._ui.use_sound_cb, 'sound')
|
||||
self._on_disable_it_toggled(widget, self._ui.use_sound_cb, "sound")
|
||||
|
||||
def _on_use_popup_cb_toggled(self, widget: Gtk.CheckButton) -> None:
|
||||
self._on_use_it_toggled(widget, self._ui.disable_popup_cb, 'popup')
|
||||
self._on_use_it_toggled(widget, self._ui.disable_popup_cb, "popup")
|
||||
|
||||
def _on_disable_popup_cb_toggled(self, widget: Gtk.CheckButton) -> None:
|
||||
self._on_disable_it_toggled(widget, self._ui.use_popup_cb, 'popup')
|
||||
self._on_disable_it_toggled(widget, self._ui.use_popup_cb, "popup")
|
||||
|
||||
def _on_run_command_cb_toggled(self, widget: Gtk.CheckButton) -> None:
|
||||
self._config[self._active_num]['run_command'] = widget.get_active()
|
||||
self._config[self._active_num]["run_command"] = widget.get_active()
|
||||
if widget.get_active():
|
||||
self._ui.command_entry.set_sensitive(True)
|
||||
else:
|
||||
self._ui.command_entry.set_sensitive(False)
|
||||
|
||||
def _on_command_entry_changed(self, widget: Gtk.Entry) -> None:
|
||||
self._config[self._active_num]['command'] = widget.get_text()
|
||||
self._config[self._active_num]["command"] = widget.get_text()
|
||||
|
||||
def _on_one_shot_cb_toggled(self, widget: Gtk.CheckButton) -> None:
|
||||
self._config[self._active_num]['one_shot'] = widget.get_active()
|
||||
self._config[self._active_num]["one_shot"] = widget.get_active()
|
||||
self._ui.command_entry.set_sensitive(widget.get_active())
|
||||
|
||||
@@ -17,23 +17,33 @@
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any
|
||||
from typing import Callable
|
||||
from typing import cast
|
||||
from typing import Union
|
||||
|
||||
import logging
|
||||
import subprocess
|
||||
from functools import partial
|
||||
from typing import Any, Callable, Union, cast
|
||||
|
||||
from gajim.common import app, ged
|
||||
from gajim.common.const import PROPAGATE_EVENT, STOP_EVENT
|
||||
from gajim.common.events import MessageReceived, Notification, PresenceReceived
|
||||
from nbxmpp.protocol import JID
|
||||
|
||||
from gajim.common import app
|
||||
from gajim.common import ged
|
||||
from gajim.common.const import PROPAGATE_EVENT
|
||||
from gajim.common.const import STOP_EVENT
|
||||
from gajim.common.events import MessageReceived
|
||||
from gajim.common.events import Notification
|
||||
from gajim.common.events import PresenceReceived
|
||||
from gajim.common.helpers import play_sound_file
|
||||
from gajim.plugins import GajimPlugin
|
||||
from gajim.plugins.plugins_i18n import _
|
||||
from nbxmpp.protocol import JID
|
||||
|
||||
from triggers.gtk.config import ConfigDialog
|
||||
from triggers.util import RuleResult, log_result
|
||||
from triggers.util import log_result
|
||||
from triggers.util import RuleResult
|
||||
|
||||
log = logging.getLogger('gajim.p.triggers')
|
||||
log = logging.getLogger("gajim.p.triggers")
|
||||
|
||||
ProcessableEventsT = Union[MessageReceived, Notification, PresenceReceived]
|
||||
RuleT = dict[str, Any]
|
||||
@@ -42,36 +52,37 @@ RuleT = dict[str, Any]
|
||||
class Triggers(GajimPlugin):
|
||||
def init(self) -> None:
|
||||
self.description = _(
|
||||
'Configure Gajim’s behaviour with triggers for each contact')
|
||||
"Configure Gajim’s behaviour with triggers for each contact"
|
||||
)
|
||||
self.config_dialog = partial(ConfigDialog, self)
|
||||
self.config_default_values = {}
|
||||
|
||||
self.events_handlers = {
|
||||
'notification': (ged.PREGUI, self._on_notification),
|
||||
'message-received': (ged.PREGUI2, self._on_message_received),
|
||||
'gc-message-received': (ged.PREGUI2, self._on_message_received),
|
||||
"notification": (ged.PREGUI, self._on_notification),
|
||||
"message-received": (ged.PREGUI2, self._on_message_received),
|
||||
"gc-message-received": (ged.PREGUI2, self._on_message_received),
|
||||
# 'presence-received': (ged.PREGUI, self._on_presence_received),
|
||||
}
|
||||
|
||||
def _on_notification(self, event: Notification) -> bool:
|
||||
log.info('Process %s, %s', event.name, event.type)
|
||||
result = self._check_all(event,
|
||||
self._check_rule_apply_notification,
|
||||
self._apply_rule)
|
||||
log.info('Result: %s', result)
|
||||
log.info("Process %s, %s", event.name, event.type)
|
||||
result = self._check_all(
|
||||
event, self._check_rule_apply_notification, self._apply_rule
|
||||
)
|
||||
log.info("Result: %s", result)
|
||||
return self._excecute_notification_rules(result, event)
|
||||
|
||||
def _on_message_received(self, event: MessageReceived) -> bool:
|
||||
log.info('Process %s', event.name)
|
||||
log.info("Process %s", event.name)
|
||||
message = event.message
|
||||
if message.text is None:
|
||||
log.info('Discard event because it has no message text')
|
||||
log.info("Discard event because it has no message text")
|
||||
return PROPAGATE_EVENT
|
||||
|
||||
result = self._check_all(event,
|
||||
self._check_rule_apply_msg_received,
|
||||
self._apply_rule)
|
||||
log.info('Result: %s', result)
|
||||
result = self._check_all(
|
||||
event, self._check_rule_apply_msg_received, self._apply_rule
|
||||
)
|
||||
log.info("Result: %s", result)
|
||||
return self._excecute_message_rules(result)
|
||||
|
||||
def _on_presence_received(self, event: PresenceReceived) -> None:
|
||||
@@ -86,11 +97,12 @@ class Triggers(GajimPlugin):
|
||||
check_func = self._check_rule_apply_status_changed
|
||||
self._check_all(event, check_func, self._apply_rule)
|
||||
|
||||
def _check_all(self,
|
||||
event: ProcessableEventsT,
|
||||
check_func: Callable[..., bool],
|
||||
apply_func: Callable[..., Any]
|
||||
) -> RuleResult:
|
||||
def _check_all(
|
||||
self,
|
||||
event: ProcessableEventsT,
|
||||
check_func: Callable[..., bool],
|
||||
apply_func: Callable[..., Any],
|
||||
) -> RuleResult:
|
||||
|
||||
result = RuleResult()
|
||||
|
||||
@@ -101,7 +113,7 @@ class Triggers(GajimPlugin):
|
||||
rule = cast(RuleT, self.config[str(num)])
|
||||
if check_func(event, rule):
|
||||
apply_func(result, rule)
|
||||
if 'one_shot' in rule and rule['one_shot']:
|
||||
if "one_shot" in rule and rule["one_shot"]:
|
||||
to_remove.append(num)
|
||||
|
||||
decal = 0
|
||||
@@ -121,47 +133,38 @@ class Triggers(GajimPlugin):
|
||||
return result
|
||||
|
||||
@log_result
|
||||
def _check_rule_apply_msg_received(self,
|
||||
event: MessageReceived,
|
||||
rule: RuleT
|
||||
) -> bool:
|
||||
def _check_rule_apply_msg_received(
|
||||
self, event: MessageReceived, rule: RuleT
|
||||
) -> bool:
|
||||
|
||||
return self._check_rule_all('message_received', event, rule)
|
||||
return self._check_rule_all("message_received", event, rule)
|
||||
|
||||
@log_result
|
||||
def _check_rule_apply_connected(self,
|
||||
event: PresenceReceived,
|
||||
rule: RuleT
|
||||
) -> bool:
|
||||
def _check_rule_apply_connected(self, event: PresenceReceived, rule: RuleT) -> bool:
|
||||
|
||||
return self._check_rule_all('contact_connected', event, rule)
|
||||
return self._check_rule_all("contact_connected", event, rule)
|
||||
|
||||
@log_result
|
||||
def _check_rule_apply_disconnected(self,
|
||||
event: PresenceReceived,
|
||||
rule: RuleT
|
||||
) -> bool:
|
||||
def _check_rule_apply_disconnected(
|
||||
self, event: PresenceReceived, rule: RuleT
|
||||
) -> bool:
|
||||
|
||||
return self._check_rule_all('contact_disconnected', event, rule)
|
||||
return self._check_rule_all("contact_disconnected", event, rule)
|
||||
|
||||
@log_result
|
||||
def _check_rule_apply_status_changed(self,
|
||||
event: PresenceReceived,
|
||||
rule: RuleT
|
||||
) -> bool:
|
||||
def _check_rule_apply_status_changed(
|
||||
self, event: PresenceReceived, rule: RuleT
|
||||
) -> bool:
|
||||
|
||||
return self._check_rule_all('contact_status_change', event, rule)
|
||||
return self._check_rule_all("contact_status_change", event, rule)
|
||||
|
||||
@log_result
|
||||
def _check_rule_apply_notification(self,
|
||||
event: Notification,
|
||||
rule: RuleT
|
||||
) -> bool:
|
||||
def _check_rule_apply_notification(self, event: Notification, rule: RuleT) -> bool:
|
||||
|
||||
# Check notification type
|
||||
notif_type = ''
|
||||
if event.type == 'incoming-message':
|
||||
notif_type = 'message_received'
|
||||
notif_type = ""
|
||||
if event.type == "incoming-message":
|
||||
notif_type = "message_received"
|
||||
# if event.type == 'pres':
|
||||
# # TODO:
|
||||
# if (event.base_event.old_show < 2 and
|
||||
@@ -175,14 +178,12 @@ class Triggers(GajimPlugin):
|
||||
|
||||
return self._check_rule_all(notif_type, event, rule)
|
||||
|
||||
def _check_rule_all(self,
|
||||
notif_type: str,
|
||||
event: ProcessableEventsT,
|
||||
rule: RuleT
|
||||
) -> bool:
|
||||
def _check_rule_all(
|
||||
self, notif_type: str, event: ProcessableEventsT, rule: RuleT
|
||||
) -> bool:
|
||||
|
||||
# Check notification type
|
||||
if rule['event'] != notif_type:
|
||||
if rule["event"] != notif_type:
|
||||
return False
|
||||
|
||||
# notification type is ok. Now check recipient
|
||||
@@ -205,21 +206,17 @@ class Triggers(GajimPlugin):
|
||||
return True
|
||||
|
||||
@log_result
|
||||
def _check_rule_recipients(self,
|
||||
event: ProcessableEventsT,
|
||||
rule: RuleT
|
||||
) -> bool:
|
||||
def _check_rule_recipients(self, event: ProcessableEventsT, rule: RuleT) -> bool:
|
||||
|
||||
rule_recipients = [t.strip() for t in rule['recipients'].split(',')]
|
||||
if rule['recipient_type'] == 'groupchat':
|
||||
rule_recipients = [t.strip() for t in rule["recipients"].split(",")]
|
||||
if rule["recipient_type"] == "groupchat":
|
||||
if event.jid in rule_recipients:
|
||||
return True
|
||||
return False
|
||||
if (rule['recipient_type'] == 'contact' and event.jid not in
|
||||
rule_recipients):
|
||||
if rule["recipient_type"] == "contact" and event.jid not in rule_recipients:
|
||||
return False
|
||||
client = app.get_client(event.account)
|
||||
contact = client.get_module('Contacts').get_contact(event.jid)
|
||||
contact = client.get_module("Contacts").get_contact(event.jid)
|
||||
|
||||
if contact.is_groupchat or not contact.is_in_roster:
|
||||
return False
|
||||
@@ -229,84 +226,74 @@ class Triggers(GajimPlugin):
|
||||
if group in rule_recipients:
|
||||
group_found = True
|
||||
break
|
||||
if rule['recipient_type'] == 'group' and not group_found:
|
||||
if rule["recipient_type"] == "group" and not group_found:
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
@log_result
|
||||
def _check_rule_status(self,
|
||||
event: ProcessableEventsT,
|
||||
rule: RuleT
|
||||
) -> bool:
|
||||
def _check_rule_status(self, event: ProcessableEventsT, rule: RuleT) -> bool:
|
||||
|
||||
rule_statuses = rule['status'].split()
|
||||
rule_statuses = rule["status"].split()
|
||||
client = app.get_client(event.account)
|
||||
if rule['status'] != 'all' and client.status not in rule_statuses:
|
||||
if rule["status"] != "all" and client.status not in rule_statuses:
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
@log_result
|
||||
def _check_rule_tab_opened(self,
|
||||
event: ProcessableEventsT,
|
||||
rule: RuleT
|
||||
) -> bool:
|
||||
def _check_rule_tab_opened(self, event: ProcessableEventsT, rule: RuleT) -> bool:
|
||||
|
||||
if rule['tab_opened'] == 'both':
|
||||
if rule["tab_opened"] == "both":
|
||||
return True
|
||||
tab_opened = False
|
||||
assert isinstance(event.jid, JID)
|
||||
if app.window.chat_exists(event.account, event.jid):
|
||||
tab_opened = True
|
||||
if tab_opened and rule['tab_opened'] == 'no':
|
||||
if tab_opened and rule["tab_opened"] == "no":
|
||||
return False
|
||||
elif not tab_opened and rule['tab_opened'] == 'yes':
|
||||
elif not tab_opened and rule["tab_opened"] == "yes":
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
@log_result
|
||||
def _check_rule_has_focus(self,
|
||||
event: ProcessableEventsT,
|
||||
rule: RuleT
|
||||
) -> bool:
|
||||
def _check_rule_has_focus(self, event: ProcessableEventsT, rule: RuleT) -> bool:
|
||||
|
||||
if rule['has_focus'] == 'both':
|
||||
if rule["has_focus"] == "both":
|
||||
return True
|
||||
if rule['tab_opened'] == 'no':
|
||||
if rule["tab_opened"] == "no":
|
||||
# Does not apply in this case
|
||||
return True
|
||||
assert isinstance(event.jid, JID)
|
||||
chat_active = app.window.is_chat_active(event.account, event.jid)
|
||||
if chat_active and rule['has_focus'] == 'no':
|
||||
if chat_active and rule["has_focus"] == "no":
|
||||
return False
|
||||
elif not chat_active and rule['has_focus'] == 'yes':
|
||||
elif not chat_active and rule["has_focus"] == "yes":
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
def _apply_rule(self, result: RuleResult, rule: RuleT) -> None:
|
||||
if rule['sound'] == 'no':
|
||||
if rule["sound"] == "no":
|
||||
result.sound = False
|
||||
result.sound_file = None
|
||||
|
||||
elif rule['sound'] == 'yes':
|
||||
elif rule["sound"] == "yes":
|
||||
result.sound = False
|
||||
result.sound_file = rule['sound_file']
|
||||
result.sound_file = rule["sound_file"]
|
||||
|
||||
if rule['run_command']:
|
||||
result.command = rule['command']
|
||||
if rule["run_command"]:
|
||||
result.command = rule["command"]
|
||||
|
||||
if rule['popup'] == 'no':
|
||||
if rule["popup"] == "no":
|
||||
result.show_notification = False
|
||||
elif rule['popup'] == 'yes':
|
||||
elif rule["popup"] == "yes":
|
||||
result.show_notification = True
|
||||
|
||||
def _excecute_notification_rules(self,
|
||||
result: RuleResult,
|
||||
event: Notification
|
||||
) -> bool:
|
||||
def _excecute_notification_rules(
|
||||
self, result: RuleResult, event: Notification
|
||||
) -> bool:
|
||||
|
||||
if result.sound is False:
|
||||
event.sound = None
|
||||
@@ -324,7 +311,7 @@ class Triggers(GajimPlugin):
|
||||
|
||||
if result.command is not None:
|
||||
try:
|
||||
subprocess.Popen(f'{result.command} &', shell=True).wait()
|
||||
subprocess.Popen(f"{result.command} &", shell=True).wait()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
|
||||
@@ -25,14 +25,15 @@ if TYPE_CHECKING:
|
||||
from .triggers import ProcessableEventsT
|
||||
from .triggers import RuleT
|
||||
|
||||
log = logging.getLogger('gajim.p.triggers')
|
||||
log = logging.getLogger("gajim.p.triggers")
|
||||
|
||||
|
||||
def log_result(func: Callable[..., Any]) -> Callable[..., bool]:
|
||||
def wrapper(self: Any, event: ProcessableEventsT, rule: RuleT):
|
||||
res = func(self, event, rule)
|
||||
log.info(f'{event.name} -> {func.__name__} -> {res}')
|
||||
log.info(f"{event.name} -> {func.__name__} -> {res}")
|
||||
return res
|
||||
|
||||
return wrapper
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user