annotate sat/plugins/plugin_adhoc_dbus.py @ 2879:5ba98fd6c9a4

core: minor style improvments
author Goffi <goffi@goffi.org>
date Wed, 27 Mar 2019 21:15:25 +0100
parents 003b8b4b56a7
children e0429ff7f6b6
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1934
2daf7b4c6756 use of /usr/bin/env instead of /usr/bin/python in shebang
Goffi <goffi@goffi.org>
parents: 1933
diff changeset
1 #!/usr/bin/env python2
822
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
3
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
4 # SAT plugin for adding D-Bus to Ad-Hoc Commands
2771
003b8b4b56a7 date update
Goffi <goffi@goffi.org>
parents: 2667
diff changeset
5 # Copyright (C) 2009-2019 Jérôme Poisson (goffi@goffi.org)
822
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
6
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
7 # This program is free software: you can redistribute it and/or modify
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
8 # it under the terms of the GNU Affero General Public License as published by
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
9 # the Free Software Foundation, either version 3 of the License, or
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
10 # (at your option) any later version.
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
11
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
12 # This program is distributed in the hope that it will be useful,
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
15 # GNU Affero General Public License for more details.
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
16
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
17 # You should have received a copy of the GNU Affero General Public License
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
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
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
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
8dd9db785ac8 plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
33 log.warning(u"Missing module lxml, please download/install it from http://lxml.de/"
8dd9db785ac8 plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
34 u"auto D-Bus discovery will be disabled")
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
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
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
8dd9db785ac8 plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
43 log.warning(u"Missing module dbus, please download/install it"
8dd9db785ac8 plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
44 u"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
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
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
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
50 FD_NAME = "org.freedesktop.DBus"
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
51 FD_PATH = "/org/freedekstop/DBus"
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
52 INTROSPECT_IFACE = "org.freedesktop.DBus.Introspectable"
2667
8dd9db785ac8 plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
53 MPRIS_PREFIX = u"org.mpris.MediaPlayer2"
8dd9db785ac8 plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
54 CMD_GO_BACK = u"GoBack"
8dd9db785ac8 plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
55 CMD_GO_FWD = u"GoFW"
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
8dd9db785ac8 plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
57 MPRIS_COMMANDS = [u"org.mpris.MediaPlayer2.Player." + cmd for cmd in (
8dd9db785ac8 plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
58 u"Previous", CMD_GO_BACK, u"PlayPause", CMD_GO_FWD, u"Next")]
8dd9db785ac8 plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
59 MPRIS_PATH = u"/org/mpris/MediaPlayer2"
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((
8dd9db785ac8 plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
61 (u"org.mpris.MediaPlayer2", (
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 )),
8dd9db785ac8 plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
64 (u"org.mpris.MediaPlayer2.Player", (
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((
8dd9db785ac8 plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
72 ("xesam:title", u"Title"),
8dd9db785ac8 plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
73 ))
822
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
74
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
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
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
82
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
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",
2667
8dd9db785ac8 plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
91 C.PI_DESCRIPTION: _(u"""Add D-Bus management to Ad-Hoc commands"""),
822
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
92 }
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
93
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
94
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
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
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
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
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
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,
8dd9db785ac8 plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
107 async=True,
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,
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
115 async=True,
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"]
8dd9db785ac8 plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
118 host.registerNamespace(u"mediaplayer", NS_MEDIA_PLAYER)
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(
8dd9db785ac8 plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
127 client, self.localMediaCb, D_(u"Media Players"),
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
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
132
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
133 def _DBusAsyncCall(self, proxy, method, *args, **kwargs):
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
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
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
136 @param proxy: DBus object proxy, as returner by get_object
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
137 @param method: name of the method to call
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
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
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
141 - interface: name of the interface to use
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
142 @return: a deferred
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
143
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
144 """
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
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
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
149 proxy.get_dbus_method(method, dbus_interface=interface)(*args, **kwargs)
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
150 return d
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
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(
8dd9db785ac8 plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
154 proxy, u"Get", interface, name, interface=u"org.freedesktop.DBus.Properties")
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
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
157 def _DBusListNames(self):
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
158 return self._DBusAsyncCall(self.fd_object, "ListNames")
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
159
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
160 def _DBusIntrospect(self, proxy):
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
161 return self._DBusAsyncCall(proxy, INTROSPECT_METHOD, interface=INTROSPECT_IFACE)
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
162
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
163 def _acceptMethod(self, method):
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
164 """ Return True if we accept the method for a command
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
165 @param method: etree.Element
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
166 @return: True if the method is acceptable
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
167
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
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
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
172 return False
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
173 return True
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
174
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
175 @defer.inlineCallbacks
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
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
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
178 introspect_xml = yield self._DBusIntrospect(proxy)
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
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
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
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
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
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
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
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
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
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
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
197 methods.add((proxy.object_path, name, method_name))
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
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
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
205
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
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
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
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
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
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
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
215 bus_names.sort()
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
216 for bus_name in bus_names:
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
217 if bus_name.endswith(prog_name):
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
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
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
221 methods = set()
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
222
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
223 yield self._introspect(methods, bus_name, proxy)
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
224
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
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
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
238
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
239 defer.returnValue((bus_name, methods))
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
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
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
244 if flags is None:
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
245 flags = set()
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
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
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
250 actions.append(action)
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
251
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
252 if len(actions) == 1:
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
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
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
256 options = []
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
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
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
259 name = str(uuid.uuid4())
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
260 names_map[name] = (path, iface, command)
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
261 options.append(data_form.Option(name, label))
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
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
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
266 form.addField(field)
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
267
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
268 payload = form.toElement()
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
269 note = None
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
270
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
271 elif len(actions) == 2:
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
272 # we should have the answer here
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
273 try:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
274 x_elt = command_elt.elements(data_form.NS_X_DATA, "x").next()
822
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
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
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
279
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
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
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
282
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
283 path, iface, command = names_map[command]
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
284 proxy = self.session_bus.get_object(bus_name, path)
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
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
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
287
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
288 # job done, we can end the session, except if we have FLAG_LOOP
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
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
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
293 del actions[:]
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
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 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
298 form = data_form.Form("form", title=_(u"Updated"))
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
299 form.addField(data_form.Field("fixed", u"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
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
301 payload = None
2667
8dd9db785ac8 plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
302 note = (self._c.NOTE.INFO, _(u"Command sent"))
822
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
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
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
305
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
306 return (payload, status, None, note)
30ed86ea1c82 plugin Ad-Hoc D-Bus: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
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)
8dd9db785ac8 plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
349 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
350 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
351 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
352 # 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
353 # session
8dd9db785ac8 plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
354 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
355 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
356 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
357
8dd9db785ac8 plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
358 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
359 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
360 opt.value,
8dd9db785ac8 plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
361 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
362 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
363 log.warning(_(
8dd9db785ac8 plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
364 u"Can't retrieve remote controllers on {device_jid}: "
8dd9db785ac8 plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
365 u"{reason}".format(device_jid=device_jid, reason=e)))
8dd9db785ac8 plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
366 break
8dd9db785ac8 plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
367 defer.returnValue(remotes)
8dd9db785ac8 plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
368
8dd9db785ac8 plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
369 def doMPRISCommand(self, proxy, command):
8dd9db785ac8 plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
370 iface, command = command.rsplit(u".", 1)
8dd9db785ac8 plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
371 if command == CMD_GO_BACK:
8dd9db785ac8 plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
372 command = u'Seek'
8dd9db785ac8 plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
373 args = [-SEEK_OFFSET]
8dd9db785ac8 plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
374 elif command == CMD_GO_FWD:
8dd9db785ac8 plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
375 command = u'Seek'
8dd9db785ac8 plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
376 args = [SEEK_OFFSET]
8dd9db785ac8 plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
377 else:
8dd9db785ac8 plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
378 args = []
8dd9db785ac8 plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
379 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
380
8dd9db785ac8 plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
381 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
382 """Serialise MRPIS Metadata according to MPRIS_METADATA_MAP"""
8dd9db785ac8 plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
383 for mpris_key, name in MPRIS_METADATA_MAP.iteritems():
8dd9db785ac8 plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
384 if mpris_key in metadata:
8dd9db785ac8 plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
385 value = unicode(metadata[mpris_key])
8dd9db785ac8 plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
386 form.addField(data_form.Field(fieldType=u"fixed",
8dd9db785ac8 plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
387 var=name,
8dd9db785ac8 plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
388 value=value))
8dd9db785ac8 plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
389
8dd9db785ac8 plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
390 @defer.inlineCallbacks
8dd9db785ac8 plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
391 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
392 try:
8dd9db785ac8 plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
393 x_elt = command_elt.elements(data_form.NS_X_DATA, "x").next()
8dd9db785ac8 plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
394 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
395 except StopIteration:
8dd9db785ac8 plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
396 command_form = None
8dd9db785ac8 plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
397
8dd9db785ac8 plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
398 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
399 # 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
400 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
401 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
402 if len(bus_names) == 0:
8dd9db785ac8 plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
403 note = (self._c.NOTE.INFO, D_(u"No media player found."))
8dd9db785ac8 plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
404 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
405 options = []
8dd9db785ac8 plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
406 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
407 form = data_form.Form("form", title=D_(u"Media Player Selection"),
8dd9db785ac8 plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
408 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
409 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
410 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
411 if not player_name:
8dd9db785ac8 plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
412 log.warning(_(u"Ignoring MPRIS bus without suffix"))
8dd9db785ac8 plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
413 continue
8dd9db785ac8 plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
414 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
415 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
416 "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
417 )
8dd9db785ac8 plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
418 form.addField(field)
8dd9db785ac8 plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
419 payload = form.toElement()
8dd9db785ac8 plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
420 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
421 else:
8dd9db785ac8 plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
422 # player request
8dd9db785ac8 plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
423 try:
8dd9db785ac8 plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
424 bus_name = command_form[u"media_player"]
8dd9db785ac8 plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
425 except KeyError:
8dd9db785ac8 plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
426 raise ValueError(_(u"missing media_player value"))
8dd9db785ac8 plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
427
8dd9db785ac8 plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
428 if not bus_name.startswith(MPRIS_PREFIX):
8dd9db785ac8 plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
429 log.warning(_(u"Media player ad-hoc command trying to use non MPRIS bus. "
8dd9db785ac8 plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
430 u"Hack attempt? Refused bus: {bus_name}").format(
8dd9db785ac8 plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
431 bus_name=bus_name))
8dd9db785ac8 plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
432 note = (self._c.NOTE.ERROR, D_(u"Invalid player name."))
8dd9db785ac8 plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
433 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
434
8dd9db785ac8 plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
435 try:
8dd9db785ac8 plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
436 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
437 except dbus.exceptions.DBusException as e:
8dd9db785ac8 plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
438 log.warning(_(u"Can't get D-Bus proxy: {reason}").format(reason=e))
8dd9db785ac8 plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
439 note = (self._c.NOTE.ERROR, D_(u"Media player is not available anymore"))
8dd9db785ac8 plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
440 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
441 try:
8dd9db785ac8 plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
442 command = command_form[u"command"]
8dd9db785ac8 plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
443 except KeyError:
8dd9db785ac8 plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
444 pass
8dd9db785ac8 plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
445 else:
8dd9db785ac8 plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
446 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
447
8dd9db785ac8 plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
448 # we construct the remote control form
8dd9db785ac8 plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
449 form = data_form.Form("form", title=D_(u"Media Player Selection"))
8dd9db785ac8 plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
450 form.addField(data_form.Field(fieldType=u"hidden",
8dd9db785ac8 plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
451 var=u"media_player",
8dd9db785ac8 plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
452 value=bus_name))
8dd9db785ac8 plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
453 for iface, properties_names in MPRIS_PROPERTIES.iteritems():
8dd9db785ac8 plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
454 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
455 try:
8dd9db785ac8 plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
456 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
457 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
458 log.warning(_(u"Can't retrieve attribute {name}: {reason}")
8dd9db785ac8 plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
459 .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
460 continue
8dd9db785ac8 plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
461 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
462 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
463 else:
8dd9db785ac8 plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
464 form.addField(data_form.Field(fieldType=u"fixed",
8dd9db785ac8 plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
465 var=name,
8dd9db785ac8 plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
466 value=unicode(value)))
8dd9db785ac8 plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
467
8dd9db785ac8 plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
468 commands = [data_form.Option(c, c.rsplit(u".", 1)[1]) for c in MPRIS_COMMANDS]
8dd9db785ac8 plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
469 form.addField(data_form.Field(fieldType=u"list-single",
8dd9db785ac8 plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
470 var=u"command",
8dd9db785ac8 plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
471 options=commands,
8dd9db785ac8 plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
472 required=True))
8dd9db785ac8 plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
473
8dd9db785ac8 plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
474 payload = form.toElement()
8dd9db785ac8 plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
475 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
476 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
477
8dd9db785ac8 plugin XEP-0050, adhoc D-Bus: Ad-Hoc improvment + remote media control:
Goffi <goffi@goffi.org>
parents: 2624
diff changeset
478