comparison cagou/plugins/plugin_wid_remote.py @ 491:203755bbe0fe

massive refactoring from camelCase -> snake_case. See backend commit log for more details
author Goffi <goffi@goffi.org>
date Sat, 08 Apr 2023 13:44:32 +0200
parents 3c9ba4a694ef
children 5114bbb5daa3
comparison
equal deleted inserted replaced
490:962d17c4078c 491:203755bbe0fe
73 73
74 @property 74 @property
75 def profile(self): 75 def profile(self):
76 return self.main_wid.profile 76 return self.main_wid.profile
77 77
78 def updateUI(self, action_data): 78 def update_ui(self, action_data):
79 xmlui_raw = action_data['xmlui'] 79 xmlui_raw = action_data['xmlui']
80 ui_tpl = template_xmlui.create(G.host, xmlui_raw) 80 ui_tpl = template_xmlui.create(G.host, xmlui_raw)
81 self.ui_tpl = ui_tpl 81 self.ui_tpl = ui_tpl
82 for prop in ('Title', 'Identity'): 82 for prop in ('Title', 'Identity'):
83 try: 83 try:
90 elif playback_status == "Paused": 90 elif playback_status == "Paused":
91 self.status = "play" 91 self.status = "play"
92 elif playback_status == "Stopped": 92 elif playback_status == "Stopped":
93 self.status = "play" 93 self.status = "play"
94 else: 94 else:
95 G.host.addNote( 95 G.host.add_note(
96 title=NOTE_TITLE, 96 title=NOTE_TITLE,
97 message=_("Unknown playback status: playback_status") 97 message=_("Unknown playback status: playback_status")
98 .format(playback_status=playback_status), 98 .format(playback_status=playback_status),
99 level=C.XMLUI_DATA_LVL_WARNING) 99 level=C.XMLUI_DATA_LVL_WARNING)
100 self.commands = {v:k for k,v in ui_tpl.widgets['command'].options} 100 self.commands = {v:k for k,v in ui_tpl.widgets['command'].options}
101 101
102 def adHocRunCb(self, xmlui_raw): 102 def ad_hoc_run_cb(self, xmlui_raw):
103 ui_tpl = template_xmlui.create(G.host, xmlui_raw) 103 ui_tpl = template_xmlui.create(G.host, xmlui_raw)
104 data = {xmlui.XMLUIPanel.escape("media_player"): self.remote_item.node, 104 data = {xmlui.XMLUIPanel.escape("media_player"): self.remote_item.node,
105 "session_id": ui_tpl.session_id} 105 "session_id": ui_tpl.session_id}
106 G.host.bridge.launchAction( 106 G.host.bridge.action_launch(
107 ui_tpl.submit_id, data, self.profile, 107 ui_tpl.submit_id, data, self.profile,
108 callback=self.updateUI, 108 callback=self.update_ui,
109 errback=self.main_wid.errback) 109 errback=self.main_wid.errback)
110 110
111 def on_remote_item(self, __, remote): 111 def on_remote_item(self, __, remote):
112 NS_MEDIA_PLAYER = G.host.ns_map["mediaplayer"] 112 NS_MEDIA_PLAYER = G.host.ns_map["mediaplayer"]
113 G.host.bridge.adHocRun(str(remote.device_jid), NS_MEDIA_PLAYER, self.profile, 113 G.host.bridge.ad_hoc_run(str(remote.device_jid), NS_MEDIA_PLAYER, self.profile,
114 callback=self.adHocRunCb, 114 callback=self.ad_hoc_run_cb,
115 errback=self.main_wid.errback) 115 errback=self.main_wid.errback)
116 116
117 def do_cmd(self, command): 117 def do_cmd(self, command):
118 try: 118 try:
119 cmd_value = self.commands[command] 119 cmd_value = self.commands[command]
120 except KeyError: 120 except KeyError:
121 G.host.addNote( 121 G.host.add_note(
122 title=NOTE_TITLE, 122 title=NOTE_TITLE,
123 message=_("{command} command is not managed").format(command=command), 123 message=_("{command} command is not managed").format(command=command),
124 level=C.XMLUI_DATA_LVL_WARNING) 124 level=C.XMLUI_DATA_LVL_WARNING)
125 else: 125 else:
126 data = {xmlui.XMLUIPanel.escape("command"): cmd_value, 126 data = {xmlui.XMLUIPanel.escape("command"): cmd_value,
128 # hidden values are normally transparently managed by XMLUIPanel 128 # hidden values are normally transparently managed by XMLUIPanel
129 # but here we have to add them by hand 129 # but here we have to add them by hand
130 hidden = {xmlui.XMLUIPanel.escape(k):v 130 hidden = {xmlui.XMLUIPanel.escape(k):v
131 for k,v in self.ui_tpl.hidden.items()} 131 for k,v in self.ui_tpl.hidden.items()}
132 data.update(hidden) 132 data.update(hidden)
133 G.host.bridge.launchAction( 133 G.host.bridge.action_launch(
134 self.ui_tpl.submit_id, data, self.profile, 134 self.ui_tpl.submit_id, data, self.profile,
135 callback=self.updateUI, 135 callback=self.update_ui,
136 errback=self.main_wid.errback) 136 errback=self.main_wid.errback)
137 137
138 138
139 class RemoteDeviceWidget(DeviceWidget): 139 class RemoteDeviceWidget(DeviceWidget):
140 140
141 def xmluiCb(self, data, cb_id, profile): 141 def xmlui_cb(self, data, cb_id, profile):
142 if 'xmlui' in data: 142 if 'xmlui' in data:
143 xml_ui = xmlui.create( 143 xml_ui = xmlui.create(
144 G.host, data['xmlui'], callback=self.xmluiCb, profile=profile) 144 G.host, data['xmlui'], callback=self.xmlui_cb, profile=profile)
145 if isinstance(xml_ui, xmlui.XMLUIDialog): 145 if isinstance(xml_ui, xmlui.XMLUIDialog):
146 self.main_wid.showRootWidget() 146 self.main_wid.show_root_widget()
147 xml_ui.show() 147 xml_ui.show()
148 else: 148 else:
149 xml_ui.setCloseCb(self.onClose) 149 xml_ui.set_close_cb(self.on_close)
150 self.main_wid.layout.add_widget(xml_ui) 150 self.main_wid.layout.add_widget(xml_ui)
151 else: 151 else:
152 if data: 152 if data:
153 log.warning(_("Unhandled data: {data}").format(data=data)) 153 log.warning(_("Unhandled data: {data}").format(data=data))
154 self.main_wid.showRootWidget() 154 self.main_wid.show_root_widget()
155 155
156 def onClose(self, __, reason): 156 def on_close(self, __, reason):
157 if reason == C.XMLUI_DATA_CANCELLED: 157 if reason == C.XMLUI_DATA_CANCELLED:
158 self.main_wid.showRootWidget() 158 self.main_wid.show_root_widget()
159 else: 159 else:
160 self.main_wid.layout.clear_widgets() 160 self.main_wid.layout.clear_widgets()
161 161
162 def adHocRunCb(self, data): 162 def ad_hoc_run_cb(self, data):
163 xml_ui = xmlui.create(G.host, data, callback=self.xmluiCb, profile=self.profile) 163 xml_ui = xmlui.create(G.host, data, callback=self.xmlui_cb, profile=self.profile)
164 xml_ui.setCloseCb(self.onClose) 164 xml_ui.set_close_cb(self.on_close)
165 self.main_wid.layout.add_widget(xml_ui) 165 self.main_wid.layout.add_widget(xml_ui)
166 166
167 def do_item_action(self, touch): 167 def do_item_action(self, touch):
168 self.main_wid.layout.clear_widgets() 168 self.main_wid.layout.clear_widgets()
169 G.host.bridge.adHocRun(str(self.entity_jid), '', self.profile, 169 G.host.bridge.ad_hoc_run(str(self.entity_jid), '', self.profile,
170 callback=self.adHocRunCb, errback=self.main_wid.errback) 170 callback=self.ad_hoc_run_cb, errback=self.main_wid.errback)
171 171
172 172
173 class DevicesLayout(FloatLayout): 173 class DevicesLayout(FloatLayout):
174 """Layout used to show devices""" 174 """Layout used to show devices"""
175 layout = properties.ObjectProperty() 175 layout = properties.ObjectProperty()
184 quick_widgets.QuickWidget.__init__(self, host, target, profiles) 184 quick_widgets.QuickWidget.__init__(self, host, target, profiles)
185 cagou_widget.CagouWidget.__init__(self) 185 cagou_widget.CagouWidget.__init__(self)
186 FilterBehavior.__init__(self) 186 FilterBehavior.__init__(self)
187 TouchMenuBehavior.__init__(self) 187 TouchMenuBehavior.__init__(self)
188 self.stack_layout = None 188 self.stack_layout = None
189 self.showRootWidget() 189 self.show_root_widget()
190 190
191 def errback(self, failure_): 191 def errback(self, failure_):
192 """Generic errback which add a warning note and go back to root widget""" 192 """Generic errback which add a warning note and go back to root widget"""
193 G.host.addNote( 193 G.host.add_note(
194 title=NOTE_TITLE, 194 title=NOTE_TITLE,
195 message=_("Can't use remote control: {reason}").format(reason=failure_), 195 message=_("Can't use remote control: {reason}").format(reason=failure_),
196 level=C.XMLUI_DATA_LVL_WARNING) 196 level=C.XMLUI_DATA_LVL_WARNING)
197 self.showRootWidget() 197 self.show_root_widget()
198 198
199 def key_input(self, window, key, scancode, codepoint, modifier): 199 def key_input(self, window, key, scancode, codepoint, modifier):
200 if key == 27: 200 if key == 27:
201 self.showRootWidget() 201 self.show_root_widget()
202 return True 202 return True
203 203
204 def showRootWidget(self): 204 def show_root_widget(self):
205 self.layout.clear_widgets() 205 self.layout.clear_widgets()
206 devices_layout = DevicesLayout() 206 devices_layout = DevicesLayout()
207 self.stack_layout = devices_layout.layout 207 self.stack_layout = devices_layout.layout
208 self.layout.add_widget(devices_layout) 208 self.layout.add_widget(devices_layout)
209 found = [] 209 found = []
210 self.get_remotes(found) 210 self.get_remotes(found)
211 self.discover_devices(found) 211 self.discover_devices(found)
212 212
213 def adHocRemotesGetCb(self, remotes_data, found): 213 def ad_hoc_remotes_get_cb(self, remotes_data, found):
214 found.insert(0, remotes_data) 214 found.insert(0, remotes_data)
215 if len(found) == 2: 215 if len(found) == 2:
216 self.show_devices(found) 216 self.show_devices(found)
217 217
218 def adHocRemotesGetEb(self, failure_, found): 218 def ad_hoc_remotes_get_eb(self, failure_, found):
219 G.host.errback(failure_, title=_("discovery error"), 219 G.host.errback(failure_, title=_("discovery error"),
220 message=_("can't check remote controllers: {msg}")) 220 message=_("can't check remote controllers: {msg}"))
221 found.insert(0, []) 221 found.insert(0, [])
222 if len(found) == 2: 222 if len(found) == 2:
223 self.show_devices(found) 223 self.show_devices(found)
224 224
225 def get_remotes(self, found): 225 def get_remotes(self, found):
226 self.host.bridge.adHocRemotesGet( 226 self.host.bridge.ad_hoc_remotes_get(
227 self.profile, 227 self.profile,
228 callback=partial(self.adHocRemotesGetCb, found=found), 228 callback=partial(self.ad_hoc_remotes_get_cb, found=found),
229 errback=partial(self.adHocRemotesGetEb,found=found)) 229 errback=partial(self.ad_hoc_remotes_get_eb,found=found))
230 230
231 def _discoFindByFeaturesCb(self, data, found): 231 def _disco_find_by_features_cb(self, data, found):
232 found.append(data) 232 found.append(data)
233 if len(found) == 2: 233 if len(found) == 2:
234 self.show_devices(found) 234 self.show_devices(found)
235 235
236 def _discoFindByFeaturesEb(self, failure_, found): 236 def _disco_find_by_features_eb(self, failure_, found):
237 G.host.errback(failure_, title=_("discovery error"), 237 G.host.errback(failure_, title=_("discovery error"),
238 message=_("can't check devices: {msg}")) 238 message=_("can't check devices: {msg}"))
239 found.append(({}, {}, {})) 239 found.append(({}, {}, {}))
240 if len(found) == 2: 240 if len(found) == 2:
241 self.show_devices(found) 241 self.show_devices(found)
245 try: 245 try:
246 namespace = self.host.ns_map['commands'] 246 namespace = self.host.ns_map['commands']
247 except KeyError: 247 except KeyError:
248 msg = _("can't find ad-hoc commands namespace, is the plugin running?") 248 msg = _("can't find ad-hoc commands namespace, is the plugin running?")
249 log.warning(msg) 249 log.warning(msg)
250 G.host.addNote(_("missing plugin"), msg, C.XMLUI_DATA_LVL_ERROR) 250 G.host.add_note(_("missing plugin"), msg, C.XMLUI_DATA_LVL_ERROR)
251 return 251 return
252 self.host.bridge.discoFindByFeatures( 252 self.host.bridge.disco_find_by_features(
253 [namespace], [], False, True, True, True, False, self.profile, 253 [namespace], [], False, True, True, True, False, self.profile,
254 callback=partial(self._discoFindByFeaturesCb, found=found), 254 callback=partial(self._disco_find_by_features_cb, found=found),
255 errback=partial(self._discoFindByFeaturesEb, found=found)) 255 errback=partial(self._disco_find_by_features_eb, found=found))
256 256
257 def show_devices(self, found): 257 def show_devices(self, found):
258 remotes_data, (entities_services, entities_own, entities_roster) = found 258 remotes_data, (entities_services, entities_own, entities_roster) = found
259 if remotes_data: 259 if remotes_data:
260 title = _("media players remote controls") 260 title = _("media players remote controls")