Skip to content

Commit 9a6144e

Browse files
committed
Autoconnect: Add option to set connect interval
The plugin now also stops the timer if no adapter is powered and for safety also refuses to start a timer when manager_state is False.
1 parent 2489d5b commit 9a6144e

2 files changed

Lines changed: 40 additions & 4 deletions

File tree

blueman/plugins/applet/AutoConnect.py

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,51 @@ class AutoConnect(AppletPlugin):
3636
"name": "Show notification",
3737
"desc": "Show a notification on successful autoconnect."
3838
},
39+
"interval": {
40+
"type": int,
41+
"default": 60,
42+
"range": (30, 1800),
43+
"name": "Connect interval",
44+
"desc": "Set the time in seconds an autoconnect is attempted."
45+
}
3946
}
4047

4148
def __init__(self, parent: "BluemanApplet"):
4249
super().__init__(parent)
43-
GLib.timeout_add(60000, self._run)
50+
self.__applet = parent
51+
self.__event_source: int | None = None
4452

4553
def on_manager_state_changed(self, state: bool) -> None:
4654
if state:
47-
self._run()
55+
self.start_timer()
56+
else:
57+
self.stop_timer()
58+
59+
def option_changed(self, key: str, value: Any) -> None:
60+
if key == "interval":
61+
self.start_timer()
4862

4963
def on_adapter_property_changed(self, path: ObjectPath, key: str, value: Any) -> None:
50-
if key == "Powered" and value:
51-
self._run()
64+
if key == "Powered":
65+
if any(adapter["Powered"] for adapter in self.parent.Manager.get_adapters()):
66+
self.start_timer()
67+
else:
68+
self.stop_timer()
69+
70+
def start_timer(self) -> None:
71+
if not self.__applet.manager_state:
72+
return
73+
74+
self.stop_timer()
75+
76+
powered = any(adapter["Powered"] for adapter in self.parent.Manager.get_adapters())
77+
if powered:
78+
self.__event_source = GLib.timeout_add_seconds(self.get_option("interval"), self._run)
79+
80+
def stop_timer(self) -> None:
81+
if self.__event_source is not None:
82+
GLib.Source.remove(self.__event_source)
83+
self.__event_source = None
5284

5385
@staticmethod
5486
def __fix_settings(path: ObjectPath, uuid: str) -> BtAddress:

data/org.blueman.gschema.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,10 @@
158158
<default>true</default>
159159
<summary>Show notification on successful autoconnect</summary>
160160
</key>
161+
<key type="i" name="interval">
162+
<default>60</default>
163+
<summary>Connect interval</summary>
164+
</key>
161165
</schema>
162166
<schema id="org.blueman.plugins.recentconns" path="/org/blueman/plugins/recentconns/">
163167
<key type="i" name="max-items">

0 commit comments

Comments
 (0)