SATIPClient: Fixed handling of tuner from DVBViewer Recording Service
[enigma2-plugins.git] / satipclient / src / SatIPTunerSetup.py
1 from Components.ActionMap import ActionMap
2 from Components.Console import Console
3 from Components.MenuList import MenuList
4 from Components.Sources.StaticText import StaticText
5 from Screens.ChoiceBox import ChoiceBox
6 from Screens.Screen import Screen
7 from Tools.Log import Log
8
9 from SatIPTuner import SatIPTunerOverview, SatIPTuner
10 from ClientConfig import vtunerClientConfig, TunerEntry
11
12
13 class SatIPTunerSetup(Screen):
14         SATIP_CLIENT_SERVICE_NAME = "satip-client"
15         skin = """
16                 <screen name="SatIPTunerSetup" position="center,120" size="720,520" title="Sat>IP Tuner Setup">
17                         <ePixmap pixmap="skin_default/buttons/red.png" position="0,0" size="200,40" alphatest="on" />
18                         <ePixmap pixmap="skin_default/buttons/green.png" position="200,0" size="200,40" alphatest="on" />
19                         <widget source="key_red" render="Label" position="0,0" zPosition="1" size="200,40" font="Regular;20" halign="center" valign="center" backgroundColor="#9f1313" transparent="1" />
20                         <widget source="key_green" render="Label" position="200,0" zPosition="1" size="200,40" font="Regular;20" halign="center" valign="center" backgroundColor="#1f771f" transparent="1" />
21                         <widget name="list" position="5,50" size="710,450" scrollbarMode="showOnDemand" zPosition="1"/>
22                 </screen>"""
23
24         _console = None
25
26         def __init__(self, session):
27                 Screen.__init__(self, session)
28                 self.setTitle(_("Sat>IP Tuner Setup"))
29                 self["list"] = MenuList([], enableWrapAround=True)
30                 self["key_red"] = StaticText(_("Remove"))
31                 self["key_green"] = StaticText(_("Add"))
32
33                 self["setupActions"] = ActionMap(["OkCancelActions", "ColorActions"],
34                 {
35                         "ok" : self._close,
36                         "cancel" : self._close,
37                         "green" : self._add,
38                         "red" : self._remove,
39                 })
40                 self._changed = False
41                 self._console = Console()
42                 self._check_count_lut = {}
43                 self._reload()
44
45         def _countTuner(self, tunerEntry):
46                 if not tunerEntry.ipAddress in self._check_count_lut:
47                         self._check_count_lut[tunerEntry.ipAddress] = {}
48                 host_entry = self._check_count_lut[tunerEntry.ipAddress]
49                 if not tunerEntry.tunerType in host_entry:
50                         host_entry[tunerEntry.tunerType] = 0
51                 host_entry[tunerEntry.tunerType] += 1
52                 Log.i("tuner count for %s is now %s" % (tunerEntry, host_entry[tunerEntry.tunerType]))
53
54         def _getTunerCount(self, ip, type):
55                 if not ip in self._check_count_lut:
56                         Log.w("ip not in lut!")
57                         return 0
58                 host_entry = self._check_count_lut[ip]
59                 if not type in host_entry:
60                         Log.w("type not in lut!")
61                         return 0
62                 count = host_entry[type]
63                 Log.w(count)
64                 return count
65
66         def _reload(self):
67                 self._check_count_lut = {}
68                 content = [(_("Count - Type (host)"),)]
69                 entries = vtunerClientConfig.getConfig()
70                 for index, tunerEntry in entries.iteritems():
71                         if not tunerEntry.isValidSatIPEntry():
72                                 #Log.w("filtering %s, it's not a satip tuner!" % (tunerEntry,))
73                                 text = "%s - %s (%s)" % (index, tunerEntry.tunerType, tunerEntry.entryType)
74                                 content.append((text,))
75                                 continue
76                         self._countTuner(tunerEntry)
77                         text = "%s - %s (%s)" % (index, tunerEntry.tunerType, tunerEntry.ipAddress)
78                         content.append((text, tunerEntry, index))
79                 self["list"].setList(content)
80
81         def _close(self):
82                 if self._changed:
83                         self._restartDaemon()
84                 self.close()
85
86         def _add(self):
87                 self.session.openWithCallback(self._onTunerSelected, SatIPTunerOverview)
88
89         def _remove(self):
90                 current = self["list"].getCurrent()
91                 Log.w(current)
92                 if current and len(current) == 3:
93                         text, tunerEntry, index = current
94                         if vtunerClientConfig.removeTuner(index, tunerEntry):
95                                 self._changed = True
96                                 vtunerClientConfig.save()
97                                 self.session.toastManager.showToast(_("Tuner {0} / {1} removed").format(tunerEntry.ipAddress, tunerEntry.tunerType))
98                         else:
99                                 self.session.toastManager.showToast(_("Tuner {0} / {1} NOT REMOVED. Consicentcy Check failed!").format(tunerEntry.ipAddress, tunerEntry.tunerType))
100                         self._reload()
101
102         def _onTunerSelected(self, data):
103                 Log.d(data)
104                 if data and len(data) > 1:
105                         name, tuner = data
106                         self._selectModulationType(tuner)
107
108         def _selectModulationType(self, tuner):
109                 choices = []
110                 caps_keys = tuner.caps.keys()
111                 type = mapped_type = count = None
112                 for caps_key in caps_keys:
113                         type = caps_key
114                         count = tuner.caps[type]
115                         mapped_type = TunerEntry.MAP_FROM_DLNA.get(type)
116                         if count > self._getTunerCount(tuner.host, mapped_type):
117                                 choices.append((type, tuner))
118                 if choices:
119                         if len(choices) == 1:
120                                 self._onModulationTypeSelected(choices[0])
121                                 return
122                         self.session.openWithCallback(
123                                         self._onModulationTypeSelected,
124                                         ChoiceBox,
125                                         title=_("Please select the type of tuner you want to add!"),
126                                         titlebartext=_("Multiple Tuner Types available!"),
127                                         list=choices
128                                 )
129                         return
130                 self.session.toastManager.showToast(_("Maximum amount of tuners for {0} configured!").format(tuner.name))
131
132         def _onModulationTypeSelected(self, choice):
133                 if choice:
134                         type, tuner = choice
135                         mapped_type = TunerEntry.MAP_FROM_DLNA.get(type)
136                         self._addTuner(tuner.host, mapped_type)
137
138         def _addTuner(self, host, type):
139                 self._changed = True
140                 vtunerClientConfig.addTuner(host, type)
141                 vtunerClientConfig.save()
142                 self._reload()
143                 self.session.toastManager.showToast(_("New Tuner added! GUI restart required!"))
144
145         def _restartDaemon(self):
146                 self._console.ePopen('systemctl restart %s' % (self.SATIP_CLIENT_SERVICE_NAME,))