generated from StreamController/PluginTemplate
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathTwitchActionBase.py
102 lines (85 loc) · 4.05 KB
/
TwitchActionBase.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
from gi.repository import Gtk, Adw
import gi
from src.backend.PluginManager.ActionBase import ActionBase
gi.require_version("Gtk", "4.0")
gi.require_version("Adw", "1")
class TwitchActionBase(ActionBase):
def get_config_rows(self) -> list:
authed = self.plugin_base.backend.is_authed()
if not authed:
label = "actions.base.credentials.no-credentials"
css_style = "twitch-controller-red"
else:
label = "actions.base.credentials.authenticated"
css_style = "twitch-controller-green"
self.status_label = Gtk.Label(
label=self.plugin_base.lm.get(label), css_classes=[css_style])
self.client_id = Adw.EntryRow(
title=self.plugin_base.lm.get("actions.base.twitch_client_id"))
self.client_secret = Adw.PasswordEntryRow(
title=self.plugin_base.lm.get("actions.base.twitch_client_secret"))
self.auth_button = Gtk.Button(label=self.plugin_base.lm.get(
"actions.base.credentials.validate"))
self.auth_button.set_margin_top(10)
self.auth_button.set_margin_bottom(10)
self.client_id.connect("notify::text", self._on_change_client_id)
self.client_secret.connect(
"notify::text", self._on_change_client_secret)
self.auth_button.connect("clicked", self._on_auth_clicked)
group = Adw.PreferencesGroup()
group.set_title(self.plugin_base.lm.get(
"actions.base.credentials.title"))
group.add(self.client_id)
group.add(self.client_secret)
group.add(self.status_label)
group.add(self.auth_button)
self.load_config()
return [group]
def get_custom_config_area(self):
label = Gtk.Label(
use_markup=True,
label=f"{self.plugin_base.lm.get('actions.info.link.label')} <a href=\"https://github.com/ImDevinC/StreamControllerTwitchPlugin\">{self.plugin_base.lm.get('actions.info.link.text')}</a>")
return label
def load_config(self):
settings = self.plugin_base.get_settings()
client_id = settings.setdefault("client_id", "")
client_secret = settings.setdefault("client_secret", "")
self.client_id.set_text(client_id)
self.client_secret.set_text(client_secret)
self.plugin_base.set_settings(settings)
def _on_change_client_id(self, entry, _):
settings = self.plugin_base.get_settings()
settings["client_id"] = entry.get_text()
self.plugin_base.set_settings(settings)
def _on_change_client_secret(self, entry, _):
settings = self.plugin_base.get_settings()
settings["client_secret"] = entry.get_text()
self.plugin_base.set_settings(settings)
def _on_auth_clicked(self, _):
settings = self.plugin_base.get_settings()
client_id = settings.get('client_id')
client_secret = settings.get('client_secret')
if not client_id or not client_secret:
self._set_status(self.plugin_base.lm.get(
"actions.base.credentials.no-credentials"), True)
return
self.auth_button.set_sensitive(False)
self.plugin_base.auth_callback_fn = self.on_auth_completed
self.plugin_base.backend.update_client_credentials(
client_id, client_secret)
def _set_status(self, message: str, is_error: bool = False):
self.status_label.set_label(message)
if is_error:
self.status_label.remove_css_class("twitch-controller-green")
self.status_label.add_css_class("twitch-controller-red")
else:
self.status_label.remove_css_class("twitch-controller-red")
self.status_label.add_css_class("twitch-controller-green")
def on_auth_completed(self, success: bool) -> None:
self.auth_button.set_sensitive(True)
if success:
self._set_status(self.plugin_base.lm.get(
"actions.base.credentials.authenticated"), False)
else:
self._set_status(self.plugin_base.lm.get(
"actions.base.credentials.failed"), True)