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