Add satipclient setup utility
[enigma2-plugins.git] / satipclient / src / SatIPTuner.py
1 from Components.ActionMap import ActionMap
2 from Components.MenuList import MenuList
3 from Components.ResourceManager import resourcemanager
4 from Screens.Screen import Screen
5 from Tools.Log import Log
6
7 class SatIPTuner(object):
8         TUNER_TYPE_S = "DVBS"
9         TUNER_TYPE_S2 = "DVBS2"
10         TUNER_TYPE_T = "DVBT"
11         TUNER_TYPE_T2 = "DVBT2"
12         TUNER_TYPE_C = "DVBC"
13         TUNER_TYPE_C2 = "DVBC2"
14
15         def __repr__(self):
16                 return "Sat>IPTuner~%s (%s) %s" % (self.name, self.host, self.readableCaps)
17
18         def __init__(self, device):
19                 self._host = self._name = None
20                 self._caps = {}
21                 self._parseDevice(device)
22
23         def _parseDevice(self, device):
24                 self._name = str(device.get_friendly_name())
25                 self._host = str(device.get_host())
26                 self._parseCaps(device.get_satipcap())
27
28         def _parseCaps(self, caps):
29                 caps = str(caps)
30                 caps = caps.split(",")
31                 for cap in caps:
32                         type, count = cap.split("-")
33                         self._caps[type] = int(count)
34
35         def getName(self):
36                 return self._name
37         name = property(getName)
38
39         def getHost(self):
40                 return self._host
41         host = property(getHost)
42
43         def getCaps(self):
44                 return self._caps
45         caps = property(getCaps)
46
47         def getReadableCaps(self):
48                 caps = []
49                 for ttype, tcount in self.caps.iteritems():
50                         caps.append(_("{0}x {1}").format(tcount, ttype))
51                 return ", ".join(caps)
52         readableCaps = property(getReadableCaps)
53
54 class SatIPTunerOverview(Screen):
55         skin = """
56                 <screen name="SatIPTunerOverview" position="center,center" size="560,400" title="Sat>IP Tuner Overview">
57                         <widget name="list" position="5,5" size="550,390" scrollbarMode="showOnDemand" zPosition="1"/>
58                 </screen>
59         """
60         def __init__(self, session):
61                 Screen.__init__(self, session)
62                 self._cp = resourcemanager.getResource("UPnPControlPoint")
63                 assert self._cp is not None
64
65                 self.setTitle(_("Sat>IP Tuner Overview"))
66                 self["list"] = MenuList([], enableWrapAround=True)
67                 self["tunerActions"] = ActionMap(["OkCancelActions"],
68                 {
69                         "ok": self._ok,
70                         "cancel": self._close,
71                 }, -2)
72
73                 self._devices = []
74                 self._reload()
75
76                 self._cp.onSatIpServerDetected.append(self._reload)
77                 self._cp.onSatIpServerRemoved.append(self._reload)
78                 self.onClose.append(self.__onClose)
79
80         def _ok(self):
81                 self.close(self["list"].getCurrent())
82
83         def _close(self):
84                 self.close(None)
85
86         def __onClose(self):
87                 self._cp.onSatIpServerDetected.remove(self._reload)
88                 self._cp.onSatIpServerRemoved.remove(self._reload)
89
90         def _reload(self, *args):
91                 devices = self._cp.getSatIPDevices()
92                 lst = []
93                 for device in devices:
94                         tuner = SatIPTuner(device)
95                         text = str("%s: %s" % (tuner.name, tuner.readableCaps))
96                         lst.append((text, tuner))
97                 self["list"].setList(lst)