Mercurial > libervia-backend
annotate sat/plugins/plugin_adhoc_dbus.py @ 3085:c048fc192739
plugin XEP-0384: update to latest python-omemo + better trust management:
- python-omemo minimal version is now 0.11.0
- `undecided` devices are now handled, fixing the "accept all or none" issue: if a device
is `undecided`, the trust manager will be shown with the missing devices before sending
the message.
- `untrusted` devices are discarded
author | Goffi <goffi@goffi.org> |
---|---|
date | Fri, 20 Dec 2019 12:28:01 +0100 |
parents | fee60f17ebac |
children | 7f7cdc6ecfd8 |
rev | line source |
---|---|
3028 | 1 #!/usr/bin/env python3 |
822 | 2 # -*- coding: utf-8 -*- |
3 | |
4 # SAT plugin for adding D-Bus to Ad-Hoc Commands | |
2771 | 5 # Copyright (C) 2009-2019 Jérôme Poisson (goffi@goffi.org) |
822 | 6 |
7 # This program is free software: you can redistribute it and/or modify | |
8 # it under the terms of the GNU Affero General Public License as published by | |
9 # the Free Software Foundation, either version 3 of the License, or | |
10 # (at your option) any later version. | |
11 | |
12 # This program is distributed in the hope that it will be useful, | |
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 # GNU Affero General Public License for more details. | |
16 | |
17 # You should have received a copy of the GNU Affero General Public License | |
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 | |
2667
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
20 from sat.core.i18n import D_, _ |
916
1a759096ccbd
core: use of Const for profile_key + replaced '@DEFAULT@' default profile_key by '@NONE@'
Goffi <goffi@goffi.org>
parents:
822
diff
changeset
|
21 from sat.core.constants import Const as C |
993
301b342c697a
core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
22 from sat.core.log import getLogger |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
23 |
993
301b342c697a
core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
24 log = getLogger(__name__) |
1540
9a4e95c62380
plugin ad-hoc D-Bus: exception fixe + minor fixes
Goffi <goffi@goffi.org>
parents:
1396
diff
changeset
|
25 from twisted.internet import defer |
2667
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
26 from twisted.words.protocols.jabber import jid |
822 | 27 from wokkel import data_form |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
28 |
1542
94901070478e
plugins: added new MissingModule exceptions to plugins using third party modules
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
29 try: |
94901070478e
plugins: added new MissingModule exceptions to plugins using third party modules
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
30 from lxml import etree |
94901070478e
plugins: added new MissingModule exceptions to plugins using third party modules
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
31 except ImportError: |
2667
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
32 etree = None |
3028 | 33 log.warning("Missing module lxml, please download/install it from http://lxml.de/ ." |
34 "Auto D-Bus discovery will be disabled") | |
2667
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
35 from collections import OrderedDict |
1542
94901070478e
plugins: added new MissingModule exceptions to plugins using third party modules
Goffi <goffi@goffi.org>
parents:
1540
diff
changeset
|
36 import os.path |
822 | 37 import uuid |
2667
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
38 try: |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
39 import dbus |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
40 from dbus.mainloop.glib import DBusGMainLoop |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
41 except ImportError: |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
42 dbus = None |
3028 | 43 log.warning("Missing module dbus, please download/install it" |
44 "auto D-Bus discovery will be disabled") | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
45 |
2667
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
46 else: |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
47 DBusGMainLoop(set_as_default=True) |
822 | 48 |
2667
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
49 NS_MEDIA_PLAYER = "org.salutatoi.mediaplayer" |
822 | 50 FD_NAME = "org.freedesktop.DBus" |
51 FD_PATH = "/org/freedekstop/DBus" | |
52 INTROSPECT_IFACE = "org.freedesktop.DBus.Introspectable" | |
3028 | 53 MPRIS_PREFIX = "org.mpris.MediaPlayer2" |
54 CMD_GO_BACK = "GoBack" | |
55 CMD_GO_FWD = "GoFW" | |
2667
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
56 SEEK_OFFSET = 5 * 1000 * 1000 |
3028 | 57 MPRIS_COMMANDS = ["org.mpris.MediaPlayer2.Player." + cmd for cmd in ( |
58 "Previous", CMD_GO_BACK, "PlayPause", CMD_GO_FWD, "Next")] | |
59 MPRIS_PATH = "/org/mpris/MediaPlayer2" | |
2667
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
60 MPRIS_PROPERTIES = OrderedDict(( |
3028 | 61 ("org.mpris.MediaPlayer2", ( |
2667
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
62 "Identity", |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
63 )), |
3028 | 64 ("org.mpris.MediaPlayer2.Player", ( |
2667
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
65 "Metadata", |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
66 "PlaybackStatus", |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
67 "Volume", |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
68 )), |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
69 )) |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
70 MPRIS_METADATA_KEY = "Metadata" |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
71 MPRIS_METADATA_MAP = OrderedDict(( |
3028 | 72 ("xesam:title", "Title"), |
2667
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
73 )) |
822 | 74 |
75 INTROSPECT_METHOD = "Introspect" | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
76 IGNORED_IFACES_START = ( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
77 "org.freedesktop", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
78 "org.qtproject", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
79 "org.kde.KMainWindow", |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
80 ) # commands in interface starting with these values will be ignored |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
81 FLAG_LOOP = "LOOP" |
822 | 82 |
83 PLUGIN_INFO = { | |
2145
33c8c4973743
core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
84 C.PI_NAME: "Ad-Hoc Commands - D-Bus", |
33c8c4973743
core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
85 C.PI_IMPORT_NAME: "AD_HOC_DBUS", |
33c8c4973743
core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
86 C.PI_TYPE: "Misc", |
33c8c4973743
core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
87 C.PI_PROTOCOLS: [], |
33c8c4973743
core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
88 C.PI_DEPENDENCIES: ["XEP-0050"], |
33c8c4973743
core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
89 C.PI_MAIN: "AdHocDBus", |
33c8c4973743
core (plugins): added missing contants + use of new constants in PLUGIN_INFO
Goffi <goffi@goffi.org>
parents:
1934
diff
changeset
|
90 C.PI_HANDLER: "no", |
3028 | 91 C.PI_DESCRIPTION: _("""Add D-Bus management to Ad-Hoc commands"""), |
822 | 92 } |
93 | |
94 | |
95 class AdHocDBus(object): | |
2667
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
96 |
822 | 97 def __init__(self, host): |
993
301b342c697a
core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
98 log.info(_("plugin Ad-Hoc D-Bus initialization")) |
822 | 99 self.host = host |
2667
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
100 if etree is not None: |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
101 host.bridge.addMethod( |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
102 "adHocDBusAddAuto", |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
103 ".plugin", |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
104 in_sign="sasasasasasass", |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
105 out_sign="(sa(sss))", |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
106 method=self._adHocDBusAddAuto, |
3028 | 107 async_=True, |
2667
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
108 ) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
109 host.bridge.addMethod( |
2667
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
110 "adHocRemotesGet", |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
111 ".plugin", |
2667
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
112 in_sign="s", |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
113 out_sign="a(sss)", |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
114 method=self._adHocRemotesGet, |
3028 | 115 async_=True, |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
116 ) |
2667
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
117 self._c = host.plugins["XEP-0050"] |
3028 | 118 host.registerNamespace("mediaplayer", NS_MEDIA_PLAYER) |
2667
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
119 if dbus is not None: |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
120 self.session_bus = dbus.SessionBus() |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
121 self.fd_object = self.session_bus.get_object( |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
122 FD_NAME, FD_PATH, introspect=False) |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
123 |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
124 def profileConnected(self, client): |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
125 if dbus is not None: |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
126 self._c.addAdHocCommand( |
3028 | 127 client, self.localMediaCb, D_("Media Players"), |
2667
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
128 node=NS_MEDIA_PLAYER, |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
129 timeout=60*60*6 # 6 hours timeout, to avoid breaking remote |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
130 # in the middle of a movie |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
131 ) |
822 | 132 |
133 def _DBusAsyncCall(self, proxy, method, *args, **kwargs): | |
134 """ Call a DBus method asynchronously and return a deferred | |
2667
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
135 |
822 | 136 @param proxy: DBus object proxy, as returner by get_object |
137 @param method: name of the method to call | |
138 @param args: will be transmitted to the method | |
2667
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
139 @param kwargs: will be transmetted to the method, except for the following poped |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
140 values: |
822 | 141 - interface: name of the interface to use |
142 @return: a deferred | |
143 | |
144 """ | |
145 d = defer.Deferred() | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
146 interface = kwargs.pop("interface", None) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
147 kwargs["reply_handler"] = lambda ret=None: d.callback(ret) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
148 kwargs["error_handler"] = d.errback |
822 | 149 proxy.get_dbus_method(method, dbus_interface=interface)(*args, **kwargs) |
150 return d | |
151 | |
2667
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
152 def _DBusGetProperty(self, proxy, interface, name): |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
153 return self._DBusAsyncCall( |
3028 | 154 proxy, "Get", interface, name, interface="org.freedesktop.DBus.Properties") |
2667
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
155 |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
156 |
822 | 157 def _DBusListNames(self): |
158 return self._DBusAsyncCall(self.fd_object, "ListNames") | |
159 | |
160 def _DBusIntrospect(self, proxy): | |
161 return self._DBusAsyncCall(proxy, INTROSPECT_METHOD, interface=INTROSPECT_IFACE) | |
162 | |
163 def _acceptMethod(self, method): | |
164 """ Return True if we accept the method for a command | |
165 @param method: etree.Element | |
166 @return: True if the method is acceptable | |
167 | |
168 """ | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
169 if method.xpath( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
170 "arg[@direction='in']" |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
171 ): # we don't accept method with argument for the moment |
822 | 172 return False |
173 return True | |
174 | |
175 @defer.inlineCallbacks | |
176 def _introspect(self, methods, bus_name, proxy): | |
993
301b342c697a
core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
177 log.debug("introspecting path [%s]" % proxy.object_path) |
822 | 178 introspect_xml = yield self._DBusIntrospect(proxy) |
179 el = etree.fromstring(introspect_xml) | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
180 for node in el.iterchildren("node", "interface"): |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
181 if node.tag == "node": |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
182 new_path = os.path.join(proxy.object_path, node.get("name")) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
183 new_proxy = self.session_bus.get_object( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
184 bus_name, new_path, introspect=False |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
185 ) |
822 | 186 yield self._introspect(methods, bus_name, new_proxy) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
187 elif node.tag == "interface": |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
188 name = node.get("name") |
822 | 189 if any(name.startswith(ignored) for ignored in IGNORED_IFACES_START): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
190 log.debug("interface [%s] is ignored" % name) |
822 | 191 continue |
993
301b342c697a
core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
192 log.debug("introspecting interface [%s]" % name) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
193 for method in node.iterchildren("method"): |
822 | 194 if self._acceptMethod(method): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
195 method_name = method.get("name") |
993
301b342c697a
core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
196 log.debug("method accepted: [%s]" % method_name) |
822 | 197 methods.add((proxy.object_path, name, method_name)) |
198 | |
2667
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
199 def _adHocDBusAddAuto(self, prog_name, allowed_jids, allowed_groups, allowed_magics, |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
200 forbidden_jids, forbidden_groups, flags, profile_key): |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
201 client = self.host.getClient(profile_key) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
202 return self.adHocDBusAddAuto( |
2667
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
203 client, prog_name, allowed_jids, allowed_groups, allowed_magics, |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
204 forbidden_jids, forbidden_groups, flags) |
822 | 205 |
206 @defer.inlineCallbacks | |
2667
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
207 def adHocDBusAddAuto(self, client, prog_name, allowed_jids=None, allowed_groups=None, |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
208 allowed_magics=None, forbidden_jids=None, forbidden_groups=None, |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
209 flags=None): |
822 | 210 bus_names = yield self._DBusListNames() |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
211 bus_names = [bus_name for bus_name in bus_names if "." + prog_name in bus_name] |
822 | 212 if not bus_names: |
993
301b342c697a
core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
213 log.info("Can't find any bus for [%s]" % prog_name) |
1933
16e65f15f31f
plugin adhoc_dbus, jp (cmd/adhoc): fixes adHocDBusAddAuto when no bus is found
souliane <souliane@mailoo.org>
parents:
1766
diff
changeset
|
214 defer.returnValue(("", [])) |
822 | 215 bus_names.sort() |
216 for bus_name in bus_names: | |
217 if bus_name.endswith(prog_name): | |
218 break | |
993
301b342c697a
core: use of the new core.log module:
Goffi <goffi@goffi.org>
parents:
916
diff
changeset
|
219 log.info("bus name found: [%s]" % bus_name) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
220 proxy = self.session_bus.get_object(bus_name, "/", introspect=False) |
822 | 221 methods = set() |
222 | |
223 yield self._introspect(methods, bus_name, proxy) | |
224 | |
225 if methods: | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
226 self._addCommand( |
2667
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
227 client, |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
228 prog_name, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
229 bus_name, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
230 methods, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
231 allowed_jids=allowed_jids, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
232 allowed_groups=allowed_groups, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
233 allowed_magics=allowed_magics, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
234 forbidden_jids=forbidden_jids, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
235 forbidden_groups=forbidden_groups, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
236 flags=flags, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
237 ) |
822 | 238 |
3040 | 239 defer.returnValue((str(bus_name), methods)) |
822 | 240 |
2667
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
241 def _addCommand(self, client, adhoc_name, bus_name, methods, allowed_jids=None, |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
242 allowed_groups=None, allowed_magics=None, forbidden_jids=None, |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
243 forbidden_groups=None, flags=None): |
822 | 244 if flags is None: |
245 flags = set() | |
246 | |
2667
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
247 def DBusCallback(client, command_elt, session_data, action, node): |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
248 actions = session_data.setdefault("actions", []) |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
249 names_map = session_data.setdefault("names_map", {}) |
822 | 250 actions.append(action) |
251 | |
252 if len(actions) == 1: | |
253 # it's our first request, we ask the desired new status | |
2667
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
254 status = self._c.STATUS.EXECUTING |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
255 form = data_form.Form("form", title=_("Command selection")) |
822 | 256 options = [] |
257 for path, iface, command in methods: | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
258 label = command.rsplit(".", 1)[-1] |
822 | 259 name = str(uuid.uuid4()) |
260 names_map[name] = (path, iface, command) | |
261 options.append(data_form.Option(name, label)) | |
262 | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
263 field = data_form.Field( |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
264 "list-single", "command", options=options, required=True |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
265 ) |
822 | 266 form.addField(field) |
267 | |
268 payload = form.toElement() | |
269 note = None | |
270 | |
271 elif len(actions) == 2: | |
272 # we should have the answer here | |
273 try: | |
3028 | 274 x_elt = next(command_elt.elements(data_form.NS_X_DATA, "x")) |
822 | 275 answer_form = data_form.Form.fromElement(x_elt) |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
276 command = answer_form["command"] |
1540
9a4e95c62380
plugin ad-hoc D-Bus: exception fixe + minor fixes
Goffi <goffi@goffi.org>
parents:
1396
diff
changeset
|
277 except (KeyError, StopIteration): |
2667
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
278 raise self._c.AdHocError(self._c.ERROR.BAD_PAYLOAD) |
822 | 279 |
280 if command not in names_map: | |
2667
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
281 raise self._c.AdHocError(self._c.ERROR.BAD_PAYLOAD) |
822 | 282 |
283 path, iface, command = names_map[command] | |
284 proxy = self.session_bus.get_object(bus_name, path) | |
285 | |
1540
9a4e95c62380
plugin ad-hoc D-Bus: exception fixe + minor fixes
Goffi <goffi@goffi.org>
parents:
1396
diff
changeset
|
286 self._DBusAsyncCall(proxy, command, interface=iface) |
822 | 287 |
288 # job done, we can end the session, except if we have FLAG_LOOP | |
289 if FLAG_LOOP in flags: | |
2667
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
290 # We have a loop, so we clear everything and we execute again the |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
291 # command as we had a first call (command_elt is not used, so None |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
292 # is OK) |
822 | 293 del actions[:] |
294 names_map.clear() | |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
295 return DBusCallback( |
2667
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
296 client, None, session_data, self._c.ACTION.EXECUTE, node |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
297 ) |
3028 | 298 form = data_form.Form("form", title=_("Updated")) |
299 form.addField(data_form.Field("fixed", "Command sent")) | |
2667
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
300 status = self._c.STATUS.COMPLETED |
822 | 301 payload = None |
3028 | 302 note = (self._c.NOTE.INFO, _("Command sent")) |
822 | 303 else: |
2667
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
304 raise self._c.AdHocError(self._c.ERROR.INTERNAL) |
822 | 305 |
306 return (payload, status, None, note) | |
307 | |
2667
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
308 self._c.addAdHocCommand( |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
309 client, |
2624
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
310 DBusCallback, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
311 adhoc_name, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
312 allowed_jids=allowed_jids, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
313 allowed_groups=allowed_groups, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
314 allowed_magics=allowed_magics, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
315 forbidden_jids=forbidden_jids, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
316 forbidden_groups=forbidden_groups, |
56f94936df1e
code style reformatting using black
Goffi <goffi@goffi.org>
parents:
2562
diff
changeset
|
317 ) |
2667
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
318 |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
319 ## Local media ## |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
320 |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
321 def _adHocRemotesGet(self, profile): |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
322 return self.adHocRemotesGet(self.host.getClient(profile)) |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
323 |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
324 @defer.inlineCallbacks |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
325 def adHocRemotesGet(self, client): |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
326 """Retrieve available remote media controlers in our devices |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
327 @return (list[tuple[unicode, unicode, unicode]]): list of devices with: |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
328 - entity full jid |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
329 - device name |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
330 - device label |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
331 """ |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
332 found_data = yield self.host.findByFeatures( |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
333 client, [self.host.ns_map['commands']], service=False, roster=False, |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
334 own_jid=True, local_device=True) |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
335 |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
336 remotes = [] |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
337 |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
338 for found in found_data: |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
339 for device_jid_s in found: |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
340 device_jid = jid.JID(device_jid_s) |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
341 cmd_list = yield self._c.list(client, device_jid) |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
342 for cmd in cmd_list: |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
343 if cmd.nodeIdentifier == NS_MEDIA_PLAYER: |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
344 try: |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
345 result_elt = yield self._c.do(client, device_jid, |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
346 NS_MEDIA_PLAYER, timeout=5) |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
347 command_elt = self._c.getCommandElt(result_elt) |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
348 form = data_form.findForm(command_elt, NS_MEDIA_PLAYER) |
3014
b6abf8af87db
plugin ad-hoc D-Bus: fixed warning when no media player is found
Goffi <goffi@goffi.org>
parents:
3009
diff
changeset
|
349 if form is None: |
b6abf8af87db
plugin ad-hoc D-Bus: fixed warning when no media player is found
Goffi <goffi@goffi.org>
parents:
3009
diff
changeset
|
350 continue |
2667
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
351 mp_options = form.fields['media_player'].options |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
352 session_id = command_elt.getAttribute('sessionid') |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
353 if mp_options and session_id: |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
354 # we just want to discover player, so we cancel the |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
355 # session |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
356 self._c.do(client, device_jid, NS_MEDIA_PLAYER, |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
357 action=self._c.ACTION.CANCEL, |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
358 session_id=session_id) |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
359 |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
360 for opt in mp_options: |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
361 remotes.append((device_jid_s, |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
362 opt.value, |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
363 opt.label or opt.value)) |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
364 except Exception as e: |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
365 log.warning(_( |
3028 | 366 "Can't retrieve remote controllers on {device_jid}: " |
367 "{reason}".format(device_jid=device_jid, reason=e))) | |
2667
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
368 break |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
369 defer.returnValue(remotes) |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
370 |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
371 def doMPRISCommand(self, proxy, command): |
3028 | 372 iface, command = command.rsplit(".", 1) |
2667
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
373 if command == CMD_GO_BACK: |
3028 | 374 command = 'Seek' |
2667
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
375 args = [-SEEK_OFFSET] |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
376 elif command == CMD_GO_FWD: |
3028 | 377 command = 'Seek' |
2667
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
378 args = [SEEK_OFFSET] |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
379 else: |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
380 args = [] |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
381 return self._DBusAsyncCall(proxy, command, *args, interface=iface) |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
382 |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
383 def addMPRISMetadata(self, form, metadata): |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
384 """Serialise MRPIS Metadata according to MPRIS_METADATA_MAP""" |
3028 | 385 for mpris_key, name in MPRIS_METADATA_MAP.items(): |
2667
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
386 if mpris_key in metadata: |
3028 | 387 value = str(metadata[mpris_key]) |
388 form.addField(data_form.Field(fieldType="fixed", | |
2667
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
389 var=name, |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
390 value=value)) |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
391 |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
392 @defer.inlineCallbacks |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
393 def localMediaCb(self, client, command_elt, session_data, action, node): |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
394 try: |
3028 | 395 x_elt = next(command_elt.elements(data_form.NS_X_DATA, "x")) |
2667
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
396 command_form = data_form.Form.fromElement(x_elt) |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
397 except StopIteration: |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
398 command_form = None |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
399 |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
400 if command_form is None or len(command_form.fields) == 0: |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
401 # root request, we looks for media players |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
402 bus_names = yield self._DBusListNames() |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
403 bus_names = [b for b in bus_names if b.startswith(MPRIS_PREFIX)] |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
404 if len(bus_names) == 0: |
3028 | 405 note = (self._c.NOTE.INFO, D_("No media player found.")) |
2667
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
406 defer.returnValue((None, self._c.STATUS.COMPLETED, None, note)) |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
407 options = [] |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
408 status = self._c.STATUS.EXECUTING |
3028 | 409 form = data_form.Form("form", title=D_("Media Player Selection"), |
2667
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
410 formNamespace=NS_MEDIA_PLAYER) |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
411 for bus in bus_names: |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
412 player_name = bus[len(MPRIS_PREFIX)+1:] |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
413 if not player_name: |
3028 | 414 log.warning(_("Ignoring MPRIS bus without suffix")) |
2667
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
415 continue |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
416 options.append(data_form.Option(bus, player_name)) |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
417 field = data_form.Field( |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
418 "list-single", "media_player", options=options, required=True |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
419 ) |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
420 form.addField(field) |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
421 payload = form.toElement() |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
422 defer.returnValue((payload, status, None, None)) |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
423 else: |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
424 # player request |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
425 try: |
3028 | 426 bus_name = command_form["media_player"] |
2667
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
427 except KeyError: |
3028 | 428 raise ValueError(_("missing media_player value")) |
2667
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
429 |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
430 if not bus_name.startswith(MPRIS_PREFIX): |
3028 | 431 log.warning(_("Media player ad-hoc command trying to use non MPRIS bus. " |
432 "Hack attempt? Refused bus: {bus_name}").format( | |
2667
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
433 bus_name=bus_name)) |
3028 | 434 note = (self._c.NOTE.ERROR, D_("Invalid player name.")) |
2667
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
435 defer.returnValue((None, self._c.STATUS.COMPLETED, None, note)) |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
436 |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
437 try: |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
438 proxy = self.session_bus.get_object(bus_name, MPRIS_PATH) |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
439 except dbus.exceptions.DBusException as e: |
3028 | 440 log.warning(_("Can't get D-Bus proxy: {reason}").format(reason=e)) |
441 note = (self._c.NOTE.ERROR, D_("Media player is not available anymore")) | |
2667
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
442 defer.returnValue((None, self._c.STATUS.COMPLETED, None, note)) |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
443 try: |
3028 | 444 command = command_form["command"] |
2667
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
445 except KeyError: |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
446 pass |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
447 else: |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
448 yield self.doMPRISCommand(proxy, command) |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
449 |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
450 # we construct the remote control form |
3028 | 451 form = data_form.Form("form", title=D_("Media Player Selection")) |
452 form.addField(data_form.Field(fieldType="hidden", | |
453 var="media_player", | |
2667
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
454 value=bus_name)) |
3028 | 455 for iface, properties_names in MPRIS_PROPERTIES.items(): |
2667
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
456 for name in properties_names: |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
457 try: |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
458 value = yield self._DBusGetProperty(proxy, iface, name) |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
459 except Exception as e: |
3028 | 460 log.warning(_("Can't retrieve attribute {name}: {reason}") |
2667
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
461 .format(name=name, reason=e)) |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
462 continue |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
463 if name == MPRIS_METADATA_KEY: |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
464 self.addMPRISMetadata(form, value) |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
465 else: |
3028 | 466 form.addField(data_form.Field(fieldType="fixed", |
2667
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
467 var=name, |
3028 | 468 value=str(value))) |
2667
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
469 |
3028 | 470 commands = [data_form.Option(c, c.rsplit(".", 1)[1]) for c in MPRIS_COMMANDS] |
471 form.addField(data_form.Field(fieldType="list-single", | |
472 var="command", | |
2667
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
473 options=commands, |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
474 required=True)) |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
475 |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
476 payload = form.toElement() |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
477 status = self._c.STATUS.EXECUTING |
8dd9db785ac8
plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents:
2624
diff
changeset
|
478 defer.returnValue((payload, status, None, None)) |