[TeleText] new added favorite did not appear while zapping, sorted zap list
[enigma2-plugins.git] / teletext / src / plugin.py
1 from __init__ import _, _debug , _log
2
3 from enigma import iServiceInformation, iPlayableService, eSocketNotifier, getDesktop, ePoint, eSize, eServiceReference
4
5 from Screens.ChannelSelection import service_types_tv
6 from Screens.HelpMenu import HelpMenu
7 from Screens.MessageBox import MessageBox
8 from Screens.Screen import Screen
9 from Components.ActionMap import ActionMap, NumberActionMap
10 from Components.ConfigList import ConfigListScreen
11 from Components.Label import Label
12 from Components.MenuList import MenuList 
13 from Components.Pixmap import Pixmap
14 from Components.ServiceEventTracker import ServiceEventTracker
15 from Components.config import config, configfile, getConfigListEntry, ConfigSubsection, ConfigEnableDisable, ConfigSlider, ConfigSelection, ConfigSequence
16 from GlobalActions import globalActionMap
17 from Plugins.Plugin import PluginDescriptor
18 from Plugins.SystemPlugins.Toolkit.NTIVirtualKeyBoard import NTIVirtualKeyBoard 
19 from Tools.Directories import resolveFilename, SCOPE_PLUGINS
20
21 import array
22 import socket
23 import struct
24
25 import NavigationInstance
26
27 from os import unlink
28 from select import POLLIN, POLLPRI, POLLHUP, POLLERR
29
30 from enigma import Teletext as TeletextInterface
31 from enigma import DISABLED, BILINEAR, ANISOTROPIC, SHARP, SHARPER, BLURRY, ANTI_FLUTTER, ANTI_FLUTTER_BLURRY, ANTI_FLUTTER_SHARP
32
33 from ConfigParser import ConfigParser, DuplicateSectionError 
34
35 PLUGIN_VERSION="20120724"
36
37 CMD_CTL_CACHE=1
38 CMD_SHOW_PAGE=2
39 CMD_PAGE_NEXT=3
40 CMD_PAGE_PREV=4
41 CMD_SUBP_NEXT=5
42 CMD_SUBP_PREV=6
43 CMD_COLKEY_RD=7
44 CMD_COLKEY_GN=8
45 CMD_COLKEY_YE=9
46 CMD_COLKEY_BL=10
47 CMD_CATCHPAGE=11
48 CMD_CONCEALED=12
49 CMD_SET_BRIGH=13
50 CMD_SET_CONTR=14
51 CMD_RQ_UPDATE=15
52 CMD_RZAP_PAGE=16
53 CMD_OPAQUE=17
54 CMD_TRANSPARENCY=18
55 CMD_FLOF=19
56 CMD_PAGEINPUT=20
57 CMD_EDGE_CUT=21
58 CMD_TEXTLEVEL=22
59 CMD_REGION=23
60 CMD_VERSION=24
61 CMD_CLOSE_DMN=99
62
63 SPLIT_MODE_PAT = "pat"
64 SPLIT_MODE_TAP = "tap"
65 SPLIT_MODE_TIP = "tip"
66 splittingModeList = [ (SPLIT_MODE_PAT, _("picture and teletext")), (SPLIT_MODE_TAP, _("teletext and picture")), (SPLIT_MODE_TIP, _("teletext in picture")) ]
67 textlevelModeList = [ ("0", "1.0"), ("1", "1.5"), ("2", "2.5"), ("3", "3.5") ]
68 regionList = [ ("0", _("Western and Central Europe")), ("8", _("Eastern Europe")), ("16", _("Western Europe and Turkey")), ("24", _("Central and Southeast Europe")), ("32", _("Cyrillic")), ("48", _("Turkish / Greek")), ("64", _("Arabic")), ("80", _("Hebrew / Arabic")) ]
69 filterList = [ ("%d"%DISABLED,_("Disabled")), ("%d"%BILINEAR,_("bilinear")), ("%d"%ANISOTROPIC,_("anisotropic")), ("%d"%SHARP,_("sharp")), ("%d"%SHARPER,_("sharper"))]
70
71 HELP_TEXT_POS          = _("Enter values (left, top, right, bottom) or press TEXT to move and resize the teletext graphically.")
72 HELP_TEXT_TIP_POS      = _("Enter values (left, top, right, bottom) or press TEXT to move and resize the teletext graphically.")
73 HELP_TEXT_SPLITTING    = _("Select splitting mode.")
74 HELP_TEXT_BRIGHTNESS   = _("Select brightness.")
75 HELP_TEXT_CONTRAST     = _("Select contrast.")
76 HELP_TEXT_TRANSPARENCY = _("Select transparency.")
77 HELP_TEXT_EDGE_CUT     = _("Display first and last row.")
78 HELP_TEXT_DEBUG        = _("Print debug messages to /tmp/dbttcp.log.")
79 HELP_TEXT_TEXTLEVEL    = _("Select teletext version to use.")
80 HELP_TEXT_REGION       = _("Select your region to use the proper font.")
81 HELP_TEXT_SCALE_FILTER = _("Select your favourite scale filter.")
82 HELP_TEXT_CACHING      = _("If caching is disabled, each teletext page will searched when you entered its number.")
83
84 dsk_size   = getDesktop(0).size()
85 dsk_width  = dsk_size.width()
86 dsk_height = dsk_size.height()
87
88 MIN_W = 400
89 MIN_H = 300
90
91 NAV_MODE_TEXT          = 0
92 NAV_MODE_SIZE_TEXT     = 1
93 NAV_MODE_SIZE_TIP_TEXT = 2
94
95 # i.d.R. bezeichnet man 90% der vollen Aufloesung als "Title-Safe-Area" --> Default
96
97 DEF_TOP    = dsk_height / 20
98 DEF_LEFT   = dsk_width / 20
99 DEF_RIGHT  = dsk_width - DEF_LEFT
100 DEF_BOTTOM = dsk_height - DEF_TOP
101
102 config.plugins.TeleText = ConfigSubsection()
103 config.plugins.TeleText.scale_filter = ConfigSelection(filterList, default="%d"%BILINEAR)
104 config.plugins.TeleText.scale_filter_zoom = ConfigSelection(filterList, default="%d"%BILINEAR)
105 config.plugins.TeleText.brightness   = ConfigSlider(default=8,  increment=1, limits=(0,15))
106 config.plugins.TeleText.contrast     = ConfigSlider(default=12, increment=1, limits=(0,15))
107 config.plugins.TeleText.transparency = ConfigSlider(default=8,  increment=1, limits=(0,15))
108 config.plugins.TeleText.edge_cut = ConfigEnableDisable(default=False)
109 config.plugins.TeleText.splitting_mode = ConfigSelection(splittingModeList, default=SPLIT_MODE_PAT)
110 config.plugins.TeleText.textlevel      = ConfigSelection(textlevelModeList, default="2")
111 config.plugins.TeleText.region   = ConfigSelection(regionList, default="16")
112 config.plugins.TeleText.debug    = ConfigEnableDisable(default=False)
113 config.plugins.TeleText.pos      = ConfigSequence(default=[DEF_LEFT, DEF_TOP, DEF_RIGHT, DEF_BOTTOM], seperator = ",", limits = [(0,dsk_width>>3),(0,dsk_height>>3),(dsk_width-(dsk_width>>3),dsk_width),(dsk_height-(dsk_height>>3),dsk_height)])
114 config.plugins.TeleText.tip_pos  = ConfigSequence(default=[(dsk_width>>1)+(dsk_width>>2), (dsk_height>>1)+(dsk_height>>2), dsk_width, dsk_height], seperator = ",", limits = [(0,dsk_width-MIN_W),(0,dsk_height-MIN_H),(MIN_W,dsk_width),(MIN_H,dsk_height)])
115 # state
116 config.plugins.TeleText.textOnly = ConfigEnableDisable(default=True)
117 config.plugins.TeleText.opaque   = ConfigEnableDisable(default=False)
118 config.plugins.TeleText.background_caching = ConfigEnableDisable(default=True)
119
120 # global functions
121
122 def log(message):
123   _log(message)
124   if config.plugins.TeleText.debug.value:
125     _debug(message)
126
127 # ----------------------------------------
128
129 class TeleText(Screen):
130
131   pageInput   = 0
132   catchPage   = False
133   naviValue   = True
134   infoValue   = False
135   edgeValue   = False
136   opaqueValue = False
137   nav_mode    = NAV_MODE_TEXT
138   zoom        = TeletextInterface.MODE_FULL
139   filter_mode = BILINEAR
140   pid_list = []
141   pid_index = 0
142   pid_count = 0
143   demux  = -1
144   txtpid = -1
145   txtpid_origin = -1
146   cur_page = "100-00/00"
147   daemonVersion = "0.0"
148   favorites = None
149
150   onChangedEntry = [ ]
151
152   def __init__(self, session):
153     TeleText.skin = """<screen position="0,0" size="%d,%d" title="TeleText" flags="wfNoBorder"/>""" % (dsk_width, dsk_height)
154     Screen.__init__(self, session)
155
156     self.__event_tracker = ServiceEventTracker(screen = self, eventmap={
157       iPlayableService.evStart : self.serviceStarted,
158       iPlayableService.evEnd : self.serviceStopped,
159       iPlayableService.evUpdatedInfo : self.serviceInfoChanged,
160     })
161
162     self["actions"] = NumberActionMap(["OkCancelActions", "TeleTextActions"],
163     {
164       "ok" : self.okPressed,
165       "cancel" : self.cancelPressed,
166       "1" : self.keyNumberGlobal,
167       "2" : self.keyNumberGlobal,
168       "3" : self.keyNumberGlobal,
169       "4" : self.keyNumberGlobal,
170       "5" : self.keyNumberGlobal,
171       "6" : self.keyNumberGlobal,
172       "7" : self.keyNumberGlobal,
173       "8" : self.keyNumberGlobal,
174       "9" : self.keyNumberGlobal,
175       "0" : self.keyNumberGlobal,
176       "prev":   self.prevPressed,
177       "next":   self.nextPressed,
178       "prev_long": self.prevLongPressed,
179       "next_long": self.nextLongPressed,
180       "red":    self.redPressed,
181       "green":  self.greenPressed,
182       "yellow": self.yellowPressed,
183       "blue":   self.bluePressed,
184       "right":  self.rightPressed,
185       "left":   self.leftPressed,
186       "down":   self.downPressed,
187       "up":     self.upPressed,
188       "info":   self.infoPressed,
189       "tv":     self.tvPressed,
190       "radio":  self.radioPressed,
191       "text":   self.textPressed,
192       "menu":   self.menuPressed,
193       "help":   self.helpPressed,
194       "video":  self.videoPressed,
195       "nextBouquet": self.nextBouquetPressed,
196       "prevBouquet": self.prevBouquetPressed,
197       "volUp":       self.volumeUpPressed,
198       "volDown":     self.volumeDownPressed
199     }, -1)
200     self["actions"].setEnabled(True)
201
202     self.helpList.append((self["actions"], "TeleTextActions", [("1", _("enter page number"))]))
203     self.helpList.append((self["actions"], "TeleTextActions", [("2", _("enter page number"))]))
204     self.helpList.append((self["actions"], "TeleTextActions", [("3", _("enter page number"))]))
205     self.helpList.append((self["actions"], "TeleTextActions", [("4", _("enter page number"))]))
206     self.helpList.append((self["actions"], "TeleTextActions", [("5", _("enter page number"))]))
207     self.helpList.append((self["actions"], "TeleTextActions", [("6", _("enter page number"))]))
208     self.helpList.append((self["actions"], "TeleTextActions", [("7", _("enter page number"))]))
209     self.helpList.append((self["actions"], "TeleTextActions", [("8", _("enter page number"))]))
210     self.helpList.append((self["actions"], "TeleTextActions", [("9", _("enter page number / page 100"))]))
211     self.helpList.append((self["actions"], "TeleTextActions", [("prev", _("prev channel / channel list"))]))
212     self.helpList.append((self["actions"], "TeleTextActions", [("0", _("enter page number / rezap"))]))
213     self.helpList.append((self["actions"], "TeleTextActions", [("next", _("next channel / channel list"))]))
214     self.helpList.append((self["actions"], "OkCancelActions", [("cancel", _("exit"))]))
215 #    self.helpList.append((self["actions"], "TeleTextActions", [("volUp",_("increase width"))]))
216 #    self.helpList.append((self["actions"], "TeleTextActions", [("volDown",_("decrease width"))]))
217     self.helpList.append((self["actions"], "TeleTextActions", [("nextBouquet",_("zoom / increase height"))]))
218     self.helpList.append((self["actions"], "TeleTextActions", [("prevBouquet",_("favorites / decrease height"))]))
219     self.helpList.append((self["actions"], "TeleTextActions", [("info", _("toggle info"))]))
220     self.helpList.append((self["actions"], "TeleTextActions", [("menu", _("teletext settings"))]))
221     self.helpList.append((self["actions"], "TeleTextActions", [("up", _("next page / catch page / move"))]))
222     self.helpList.append((self["actions"], "TeleTextActions", [("left", _("prev sub page / move"))]))
223     self.helpList.append((self["actions"], "OkCancelActions", [("ok", _("start page catching / select page"))]))
224     self.helpList.append((self["actions"], "TeleTextActions", [("right", _("next sub page / move"))]))
225     self.helpList.append((self["actions"], "TeleTextActions", [("down", _("prev page / catch page / move"))]))
226     self.helpList.append((self["actions"], "TeleTextActions", [("video", _("toggle flof/top"))]))
227     self.helpList.append((self["actions"], "TeleTextActions", [("red", _("red teletext link"))]))
228     self.helpList.append((self["actions"], "TeleTextActions", [("green", _("green teletext link"))]))
229     self.helpList.append((self["actions"], "TeleTextActions", [("yellow", _("yellow teletext link"))]))
230     self.helpList.append((self["actions"], "TeleTextActions", [("blue", _("blue teletext link"))]))
231     self.helpList.append((self["actions"], "TeleTextActions", [("tv", _("split screen"))]))
232     self.helpList.append((self["actions"], "TeleTextActions", [("radio", _("toggle transparency"))]))
233     self.helpList.append((self["actions"], "TeleTextActions", [("text", _("exit"))]))
234
235     self.inMenu = False
236     self.connected = False
237
238     self.favorites = TeleTextFavorites()
239
240     self.ttx = TeletextInterface()
241
242     self.onLayoutFinish.append(self.__layoutFinished)
243     self.onExecBegin.append(self.__execBegin)
244     self.onClose.append(self.__closed)
245
246     self.socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
247     self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
248     try:
249       unlink('/tmp/dbttcp.socket')
250     except:
251       pass
252     self.socket.bind('/tmp/dbttcp.socket')
253     self.listen_sn = eSocketNotifier(self.socket.fileno(), POLLIN)
254     self.listen_sn.callback.append(self.listen_data_avail)
255     self.socket.listen(1)
256
257   def socketSend(self, buf):
258     log("send %s" % (buf))
259     try:
260       s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
261       s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
262       s.settimeout(5.0)
263       if config.plugins.TeleText.debug.value:
264         log("... connecting")
265       s.connect('/tmp/dbttcd.socket')
266     except socket.error, msg:
267       log("couldn't connect to /tmp/dbttcd.socket")
268       log(msg)
269       return
270
271     try:
272       msg_len = len(buf)
273       totalsent = 0
274       while totalsent < msg_len:
275         if config.plugins.TeleText.debug.value:
276           log("... sending")
277         sent = s.send(buf[totalsent:])
278         if sent == 0:
279           raise RuntimeError("socket connection broken")
280         elif config.plugins.TeleText.debug.value:
281           log("    sent %s bytes" % sent)
282         totalsent = totalsent + sent
283       s.close()
284       s = None
285     except socket.error, msg:
286       log("couldn't send data to /tmp/dbttcd.socket")
287       log(msg)
288
289   def getDaemonVersion(self):
290     x = array.array('B')
291     x.append(CMD_VERSION)
292     self.socketSend(x)
293
294
295   def listen_data_avail(self, what):
296     conn, addr = self.socket.accept()
297     buf = conn.recv(8, socket.MSG_WAITALL)
298     x = array.array('H')
299     x.fromstring(buf)
300     if x[0] == 0:
301       self.catchPage = False
302     elif x[0] == 1:
303       self.ttx.update(0,0,492,250, self.zoom, self.filter_mode)
304       if x[1] == 2303:
305         x[1] = 0x0100
306       self.cur_page = "%s%s%s-%s%s/%s%s" % ((x[1]&0x0F00)>>8, (x[1]&0xF0)>>4, x[1]&0x0F, x[2]>>4, x[2]&0x0F, x[3]>>4, x[3]&0x0F)
307       for i in self.onChangedEntry:
308         i()
309     elif x[0] == 2:
310       self.daemonVersion = "%s.%s" % (x[1], x[2])
311       log("daemon version %s" % self.daemonVersion)
312     conn.close()
313
314   def __execBegin(self):
315     log("execBegin")
316
317     if not (config.plugins.TeleText.background_caching.value or self.inMenu):
318       self.checkServiceInfo(True)
319
320     self.updateLayout()
321
322     # send brightness, contrast and transparency...
323     self.sendSettings()
324
325     renderOffset = self.ttx.getRenderBufferOffset()
326     stride = self.ttx.getRenderBufferStride()
327
328     log("framebuffer offset is %08x stride is %08x" % (renderOffset, stride))
329     x = array.array('B', (CMD_RQ_UPDATE, 1, (renderOffset&0xFF000000)>>24, (renderOffset&0xFF0000)>>16, (renderOffset&0xFF00)>>8, renderOffset&0xFF, (stride&0xFF00) >> 8, stride&0xFF))
330     self.socketSend(x)
331
332     # get daemon version
333     self.getDaemonVersion()
334
335   def __closed(self):
336     log("__closed")
337     renderOffset = self.ttx.getRenderBufferOffset()
338     stride = self.ttx.getRenderBufferStride()
339     x = array.array('B', (CMD_RQ_UPDATE, 0, (renderOffset&0xFF000000)>>24, (renderOffset&0xFF0000)>>16, (renderOffset&0xFF00)>>8, renderOffset&0xFF, (stride&0xFF00) >> 8, stride&0xFF))
340     self.socketSend(x)
341
342     if not (config.plugins.TeleText.background_caching.value or self.inMenu):
343       self.stopCaching()
344
345   def __layoutFinished(self):
346     log("__layoutFinished")
347     self.ttx.show(self.instance)
348
349   def keyNumberGlobal(self, number):
350     if self.nav_mode != NAV_MODE_TEXT or self.catchPage:
351       return
352     log("%s pressed" % (number))
353     x = array.array('B')
354     if self.pageInput == 0:
355       if number == 0:
356         if self.zoom == TeletextInterface.MODE_LOWER_HALF:
357           self.zoom = TeletextInterface.MODE_UPPER_HALF
358         x.append(CMD_RZAP_PAGE)
359       elif number == 9:
360         page = self.fav_list[self.fav_index]
361         if self.fav_index == len(self.fav_list)-1:
362           self.fav_index = 0
363         else:
364           self.fav_index = self.fav_index + 1
365         x.fromlist([CMD_SHOW_PAGE, page/100, (((page%100)/10)<<4) + (page%10), 0])
366       else:
367         x.fromlist([CMD_PAGEINPUT, number])
368         self.pageInput = (self.pageInput + 1) % 3
369     else:
370       x.fromlist([CMD_PAGEINPUT, number])
371       self.pageInput = (self.pageInput + 1) % 3
372     self.socketSend(x)
373
374   def upPressed(self):
375     if self.nav_mode == NAV_MODE_TEXT:
376       if self.pageInput != 0:
377         return
378       log("up pressed")
379       if self.catchPage:
380         x = array.array('B', (CMD_CATCHPAGE, 2, 2))
381       else:
382         if self.zoom == TeletextInterface.MODE_LOWER_HALF:
383           self.zoom = TeletextInterface.MODE_UPPER_HALF
384         x = array.array('B')
385         x.append(CMD_PAGE_NEXT)
386       self.socketSend(x)
387     else:
388       if self.nav_pos[1] > 0 and self.nav_pos[3] > self.nav_config.limits[3][0]:
389         self.nav_pos[1] = self.nav_pos[1] - 1
390         self.nav_pos[3] = self.nav_pos[3] - 1
391       self.updateLayout()
392
393   def downPressed(self):
394     if self.nav_mode == NAV_MODE_TEXT:
395       if self.pageInput != 0:
396         return
397       log("down pressed")
398       if self.catchPage:
399         x = array.array('B', (CMD_CATCHPAGE, 2, 3))
400       else:
401         if self.zoom == TeletextInterface.MODE_LOWER_HALF:
402           self.zoom = TeletextInterface.MODE_UPPER_HALF
403         x = array.array('B')
404         x.append(CMD_PAGE_PREV)
405       self.socketSend(x)
406     else:
407       if self.nav_pos[1] < self.nav_config.limits[1][1] and self.nav_pos[3] < dsk_height:
408         self.nav_pos[1] = self.nav_pos[1] + 1
409         self.nav_pos[3] = self.nav_pos[3] + 1
410         self.updateLayout()
411
412   def leftPressed(self):
413     if self.nav_mode == NAV_MODE_TEXT:
414       if self.pageInput != 0:
415         return
416       log("left pressed")
417       if self.catchPage:
418         x = array.array('B', (CMD_CATCHPAGE, 2, 0))
419       else:
420         if self.zoom == TeletextInterface.MODE_LOWER_HALF:
421           self.zoom = TeletextInterface.MODE_UPPER_HALF
422         x = array.array('B')
423         x.append(CMD_SUBP_PREV)
424       self.socketSend(x)
425     else:
426       if self.nav_pos[0] > 0 and self.nav_pos[2] > self.nav_config.limits[2][0]:
427         self.nav_pos[0] = self.nav_pos[0] - 1
428         self.nav_pos[2] = self.nav_pos[2] - 1
429       self.updateLayout()
430
431   def rightPressed(self):
432     if self.nav_mode == NAV_MODE_TEXT:
433       if self.pageInput != 0:
434         return
435       log("right pressed")
436       if self.catchPage:
437         x = array.array('B', (CMD_CATCHPAGE, 2, 1))
438       else:
439         if self.zoom == TeletextInterface.MODE_LOWER_HALF:
440           self.zoom = TeletextInterface.MODE_UPPER_HALF
441         x = array.array('B')
442         x.append(CMD_SUBP_NEXT)
443       self.socketSend(x)
444     else:
445       if self.nav_pos[0] < self.nav_config.limits[0][1] and self.nav_pos[2] < dsk_width:
446         self.nav_pos[0] = self.nav_pos[0] + 1
447         self.nav_pos[2] = self.nav_pos[2] + 1
448         self.updateLayout()
449
450   def nextBouquetPressed(self):
451     log("bouqet+ pressed")
452     if self.nav_mode == NAV_MODE_TEXT:
453       if self.catchPage or self.pageInput != 0:
454         return
455       # zoom teletext
456       if self.zoom == TeletextInterface.MODE_UPPER_HALF:
457         self.zoom = TeletextInterface.MODE_LOWER_HALF
458       elif self.zoom == TeletextInterface.MODE_LOWER_HALF:
459         self.zoom = TeletextInterface.MODE_FULL
460       else:
461         self.zoom = TeletextInterface.MODE_UPPER_HALF
462
463       if self.zoom:
464         self.filter_mode = int(config.plugins.TeleText.scale_filter_zoom.value)
465       else:
466         self.filter_mode = int(config.plugins.TeleText.scale_filter.value)
467     else:
468       # position setup
469       if self.nav_pos[3] < dsk_height:
470         self.nav_pos[3] = self.nav_pos[3] + 1
471       elif self.nav_pos[1] > 0:
472         self.nav_pos[1] = self.nav_pos[1] - 1
473     self.updateLayout()
474
475   def prevBouquetPressed(self):
476     log("bouqet- pressed")
477     if self.nav_mode == NAV_MODE_TEXT:
478       if self.catchPage or self.pageInput != 0:
479         return
480       # open favorites
481       if len(self.pid_list) > 0:
482         self.inMenu = True
483         self.session.openWithCallback(self.favoritesResult, TeleTextFavoritesMenu, parent = self, service = self.pid_list[self.pid_index], page = self.cur_page.split("-",1)[0], favorites = self.favorites)
484     else:
485       # position setup
486       if self.nav_pos[3] > (self.nav_pos[1] + MIN_H):
487         if self.nav_pos[3] > self.nav_config.limits[3][0]:
488           self.nav_pos[3] = self.nav_pos[3] - 1
489         elif self.nav_pos[1] < self.nav_config.limits[1][1]:
490           self.nav_pos[1] = self.nav_pos[1] + 1
491         self.updateLayout()
492
493   def volumeUpPressed(self):
494     if self.nav_mode == NAV_MODE_TEXT:
495       globalActionMap.action("TeleText", "volumeUp")
496       return
497     log("volume+ pressed")
498     if self.nav_pos[2] < dsk_width:
499       self.nav_pos[2] = self.nav_pos[2] + 1
500     elif self.nav_pos[0] > 0:
501       self.nav_pos[0] = self.nav_pos[0] - 1
502     self.updateLayout()
503
504   def volumeDownPressed(self):
505     if self.nav_mode == NAV_MODE_TEXT:
506       globalActionMap.action("TeleText", "volumeDown")
507       return
508     log("volume- pressed")
509     if self.nav_pos[2] > (self.nav_pos[0] + MIN_W):
510       if self.nav_pos[2] > self.nav_config.limits[2][0]:
511         self.nav_pos[2] = self.nav_pos[2] - 1
512       elif self.nav_pos[0] < self.nav_config.limits[0][1]:
513         self.nav_pos[0] = self.nav_pos[0] + 1
514     self.updateLayout()
515
516   def redPressed(self):
517     if self.nav_mode != NAV_MODE_TEXT or self.catchPage or self.pageInput != 0:
518       return
519     log("red pressed")
520     x = array.array('B')
521     x.append(CMD_COLKEY_RD)
522     self.socketSend(x)
523
524   def greenPressed(self):
525     if self.nav_mode != NAV_MODE_TEXT or self.catchPage or self.pageInput != 0:
526       return
527     log("green pressed")
528     x = array.array('B')
529     x.append(CMD_COLKEY_GN)
530     self.socketSend(x)
531
532   def yellowPressed(self):
533     if self.nav_mode != NAV_MODE_TEXT or self.catchPage or self.pageInput != 0:
534       return
535     log("yellow pressed")
536     x = array.array('B')
537     x.append(CMD_COLKEY_YE)
538     self.socketSend(x)
539
540   def bluePressed(self):
541     if self.nav_mode != NAV_MODE_TEXT or self.catchPage or self.pageInput != 0:
542       return
543     log("blue pressed")
544     x = array.array('B')
545     x.append(CMD_COLKEY_BL)
546     self.socketSend(x)
547
548   def infoPressed(self):
549     if self.nav_mode != NAV_MODE_TEXT or self.catchPage or self.pageInput != 0:
550       return
551     log("info pressed")
552     self.infoValue = not self.infoValue
553     for i in self.onChangedEntry:
554       i()
555     x = array.array('B')
556     x.append(CMD_CONCEALED)
557     self.socketSend(x)
558
559   def videoPressed(self):
560     if self.nav_mode != NAV_MODE_TEXT or self.catchPage or self.pageInput != 0:
561       return
562     log("video pressed")
563     self.naviValue = not self.naviValue
564     for i in self.onChangedEntry:
565       i()
566     x = array.array('B')
567     x.append(CMD_FLOF)
568     self.socketSend(x)
569
570   def tvPressed(self):
571     if self.nav_mode != NAV_MODE_TEXT:
572       return
573     log("tv pressed")
574     if config.plugins.TeleText.textOnly.value:
575       config.plugins.TeleText.textOnly.value = False
576     else:
577       config.plugins.TeleText.textOnly.value = True
578     config.plugins.TeleText.textOnly.save()
579     configfile.save()
580     self.updateLayout()
581
582   def radioPressed(self):
583     if self.nav_mode != NAV_MODE_TEXT or self.catchPage or self.pageInput != 0:
584       return
585     log("radio pressed")
586     if config.plugins.TeleText.opaque.value:
587       config.plugins.TeleText.opaque.value = False
588     else:
589       config.plugins.TeleText.opaque.value = True
590     config.plugins.TeleText.opaque.save()
591     configfile.save()
592     self.opaqueValue = config.plugins.TeleText.opaque.value
593     x = array.array('B')
594     x.append(CMD_OPAQUE)
595     self.socketSend(x)
596
597   def textPressed(self):
598     if self.nav_mode != NAV_MODE_TEXT or self.catchPage:
599       return
600     log("text pressed")
601     if self.pageInput != 0:
602       self.pageInput = 0
603       x = array.array('B')
604       x.fromlist([CMD_PAGEINPUT, 0x0a])
605       self.socketSend(x)
606     self.resetVideo()
607     self.__closed()
608     if self.txtpid != self.txtpid_origin:
609       self.txtpid = self.txtpid_origin
610       self.switchChannel(True)
611     self.close()
612
613   def menuPressed(self):
614     if self.nav_mode != NAV_MODE_TEXT or self.catchPage or self.pageInput != 0:
615       return
616     log("menu pressed")
617     self.__closed()
618     self.resetVideo()
619     self.inMenu = True
620     self.session.openWithCallback(self.menuResult, TeleTextMenu, parent = self)
621
622   def menuResult(self, result):
623     self.inMenu = False
624     if result is None:
625       config.plugins.TeleText.pos.load()
626       config.plugins.TeleText.tip_pos.load()
627       self.updateLayout()
628       return
629
630     self.nav_text_only = config.plugins.TeleText.textOnly.value
631     self.nav_config   = result
632     self.nav_orig_pos = result.value
633     self.nav_pos      = result.value
634
635     if result == config.plugins.TeleText.pos:
636       self.nav_mode = NAV_MODE_SIZE_TEXT
637       config.plugins.TeleText.textOnly.value = True
638     elif result == config.plugins.TeleText.tip_pos:
639       self.nav_mode = NAV_MODE_SIZE_TIP_TEXT
640       config.plugins.TeleText.textOnly.value = False
641       config.plugins.TeleText.textOnly.value = SPLIT_MODE_TIP
642     self.updateLayout()
643
644   def favoritesResult(self, result):
645     # read favorites
646     self.readFavorites()
647
648     self.inMenu = False
649     self.updateLayout()
650     if result is None:
651       return
652
653     # open favorite page
654     x = array.array('B')
655     x.fromlist([CMD_SHOW_PAGE, result/100, (((result%100)/10)<<4) + (result%10), 0])
656     self.socketSend(x)
657
658   def updateLayout(self):
659     if self.nav_mode == NAV_MODE_TEXT:
660       pos = config.plugins.TeleText.pos.value
661     else:
662       pos = self.nav_pos
663     right  = pos[2]
664     bottom = pos[3]
665     mode = config.plugins.TeleText.splitting_mode.value
666
667     if config.plugins.TeleText.textOnly.value == True:
668       left  = pos[0]
669       width  = right - left
670       top    = pos[1]
671       height = bottom - top
672       self.resetVideo()
673     elif mode == SPLIT_MODE_PAT:
674       left   = dsk_width>>1
675       width  = right - (dsk_width>>1)
676       top    = pos[1]
677       height = bottom - top
678       log("splitting video")
679       l=open("/proc/stb/vmpeg/0/dst_left","w")
680       l.write("%x" % 0)
681       l.close()
682       w=open("/proc/stb/vmpeg/0/dst_width","w")
683       w.write("%x" % 360)
684       w.close()
685     elif mode == SPLIT_MODE_TAP:
686       left   = pos[0]
687       width  = (dsk_width>>1) - left
688       top    = pos[1]
689       height = bottom - top
690       log("splitting video")
691       l=open("/proc/stb/vmpeg/0/dst_left","w")
692       l.write("%x" % 360)
693       l.close()
694       w=open("/proc/stb/vmpeg/0/dst_width","w")
695       w.write("%x" % 360)
696       w.close()
697     elif mode == SPLIT_MODE_TIP:
698       if self.nav_mode == NAV_MODE_TEXT:
699         pos = config.plugins.TeleText.tip_pos.value
700       left   = pos[0]
701       width  = pos[2] - left
702       top    = pos[1]
703       height = pos[3] - top
704       self.resetVideo()
705
706     log("screen rect %s %s %s %s" % (left, top, width, height))
707     self.instance.move(ePoint(left,top))
708     self.instance.resize(eSize(*(width, height)))
709
710     self.ttx.hide()
711     self.ttx.show(self.instance)
712     self.ttx.update(0,0,492,250,self.zoom,self.filter_mode)
713
714   def resetVideo(self):
715     log("reset video")
716     l=open("/proc/stb/vmpeg/0/dst_left","w")
717     l.write("%x" % 0)
718     l.close()
719     w=open("/proc/stb/vmpeg/0/dst_width","w")
720     w.write("%x" % 720)
721     w.close()
722
723   def sendSettings(self, result = True):
724     if result:
725       # region
726       x = array.array('B')
727       x.append(CMD_REGION)
728       x.append(int(config.plugins.TeleText.region.value))
729       self.socketSend(x)
730       # Helligkeit
731       x = array.array('B')
732       x.append(CMD_SET_BRIGH)
733       x.append(config.plugins.TeleText.brightness.value<<4)
734       self.socketSend(x)
735       # Kontrast
736       x = array.array('B')
737       x.append(CMD_SET_CONTR)
738       x.append(config.plugins.TeleText.contrast.value<<4)
739       self.socketSend(x)
740       # Transparenz
741       x = array.array('B')
742       x.append(CMD_TRANSPARENCY)
743       x.append(config.plugins.TeleText.transparency.value<<4)
744       self.socketSend(x)
745       # edge cut
746       if self.edgeValue != config.plugins.TeleText.edge_cut.value:
747         self.edgeValue = config.plugins.TeleText.edge_cut.value
748         x = array.array('B')
749         x.append(CMD_EDGE_CUT)
750         self.socketSend(x)
751       # teletext level
752       x = array.array('B')
753       x.append(CMD_TEXTLEVEL)
754       x.append(int(config.plugins.TeleText.textlevel.value))
755       self.socketSend(x)
756       # opaque
757       if self.opaqueValue != config.plugins.TeleText.opaque.value:
758         self.opaqueValue = config.plugins.TeleText.opaque.value
759         x = array.array('B')
760         x.append(CMD_OPAQUE)
761         self.socketSend(x)
762
763   def prevPressed(self):
764     if self.nav_mode == NAV_MODE_TEXT:
765       log("prev pressed")
766
767       if len(self.pid_list) < 2:
768         return
769
770       new_pid = -1
771       while new_pid == -1:
772         if self.pid_index == 0:
773           self.pid_index = len(self.pid_list) - 1
774         else:
775           self.pid_index = self.pid_index - 1
776         new_pid = self.pid_list[self.pid_index][2]
777
778       self.txtpid = new_pid
779       log("new txtpid: %s" % new_pid)
780       self.switchChannel(True)
781
782   def nextPressed(self):
783     if self.nav_mode == NAV_MODE_TEXT:
784       log("next pressed")
785
786       if len(self.pid_list) < 2:
787         return
788
789       new_pid = -1
790       while new_pid == -1:
791         if self.pid_index == (len(self.pid_list) - 1):
792           self.pid_index = 0
793         else:
794           self.pid_index = self.pid_index + 1
795         new_pid = self.pid_list[self.pid_index][2]
796
797       self.txtpid = new_pid
798       log("new txtpid: %s" % new_pid)
799       self.switchChannel(True)
800
801   def prevLongPressed(self):
802     if self.nav_mode == NAV_MODE_TEXT:
803       log("prev long pressed")
804       if len(self.pid_list) > 0:
805         self.__closed()
806         self.resetVideo()
807         self.session.openWithCallback(self.transponderResult, TeleTextTransponderMenu, self.pid_list, self.pid_index)
808
809   def nextLongPressed(self):
810     if self.nav_mode == NAV_MODE_TEXT:
811       log("next long pressed")
812       self.prevLongPressed()
813
814   def transponderResult(self, result):
815     log("transponder result: %s" % result)
816     if result > -1 and result != self.txtpid:
817       self.txtpid = result
818       log("new txtpid: %s" % result)
819       self.switchChannel(True)
820     self.updateLayout()
821
822   def helpPressed(self):
823     self.__closed()
824     self.resetVideo()
825     self.session.open(HelpMenu, self.helpList)
826
827   def okPressed(self):
828     if self.nav_mode == NAV_MODE_TEXT:
829       if self.pageInput != 0:
830         return
831       log("ok pressed")
832       if self.catchPage:
833         x = array.array('B', (CMD_CATCHPAGE, 3, 1))
834         self.catchPage = False
835       else:
836         x = array.array('B', (CMD_CATCHPAGE, 1, 0))
837         self.catchPage = True
838         self.zoom = TeletextInterface.MODE_FULL
839         self.filter_mode = int(config.plugins.TeleText.scale_filter.value)
840       self.socketSend(x)
841     else:
842       if self.nav_mode == NAV_MODE_SIZE_TEXT:
843         config.plugins.TeleText.pos.value = self.nav_pos
844         config.plugins.TeleText.pos.save()
845       else:
846         config.plugins.TeleText.tip_pos.value = self.nav_pos
847         config.plugins.TeleText.tip_pos.save()
848       config.plugins.TeleText.textOnly.value = self.nav_text_only
849       del self.nav_text_only
850       del self.nav_config
851       del self.nav_orig_pos
852       del self.nav_pos
853       self.nav_mode = NAV_MODE_TEXT
854       self.updateLayout()
855
856   def cancelPressed(self):
857     if self.nav_mode == NAV_MODE_TEXT:
858       log("cancel pressed")
859       if self.pageInput != 0:
860         self.pageInput = 0
861         x = array.array('B')
862         x.fromlist([CMD_PAGEINPUT, 0x0a])
863         self.socketSend(x)
864       elif self.catchPage:
865         x = array.array('B', (CMD_CATCHPAGE, 3, 0))
866         self.socketSend(x)
867         self.catchPage = False
868       else:
869         self.resetVideo()
870         self.__closed()
871         if self.txtpid != self.txtpid_origin:
872           self.txtpid = self.txtpid_origin
873           self.switchChannel(True)
874         self.close()
875     else:
876       if self.nav_mode == NAV_MODE_SIZE_TEXT:
877         config.plugins.TeleText.pos.cancel()
878       else:
879         config.plugins.TeleText.tip_pos.cancel()
880       config.plugins.TeleText.textOnly.value = self.nav_text_only
881       del self.nav_text_only
882       del self.nav_config
883       del self.nav_orig_pos
884       del self.nav_pos
885       self.nav_mode = NAV_MODE_TEXT
886       self.updateLayout()
887
888   def serviceStarted(self):
889     log("service started")
890
891   def serviceStopped(self):
892     log("service stopped")
893     self.stopCaching()
894
895   def stopCaching(self):
896     x = array.array('B', (CMD_CTL_CACHE, 0, 0, 0))
897     self.socketSend(x)
898
899   def serviceInfoChanged(self):
900     log("serviceInfoChanged")
901     self.checkServiceInfo(config.plugins.TeleText.background_caching.value or self.inMenu or self.execing)
902
903   def checkServiceInfo(self, do_send = True):
904     service = self.session.nav.getCurrentService()
905     info = service and service.info()
906     self.txtpid_origin = info and info.getInfo(iServiceInformation.sTXTPID)
907     self.txtpid = self.txtpid_origin
908
909     stream = service and service.stream()
910     demux = stream and stream.getStreamingData()
911     self.demux = demux and demux.get("demux", -1)
912
913     log("TXT PID %s DEMUX %s" % (self.txtpid, self.demux))
914     self.switchChannel(do_send)
915
916     # read all txtpids and channels from transponder
917     cur_ref = NavigationInstance.instance.getCurrentlyPlayingServiceReference()
918     if cur_ref is None:
919       self.channel_and_txt_pid = []
920       self.pid_index = 0
921     else:
922       pos = service_types_tv.rfind(':')
923       refstr = '%s (channelID == %08x%04x%04x) && %s ORDER BY name' % (service_types_tv[:pos+1],
924           cur_ref.getUnsignedData(4), # NAMESPACE
925           cur_ref.getUnsignedData(2), # TSID
926           cur_ref.getUnsignedData(3), # ONID
927           service_types_tv[pos+1:])
928       ref = eServiceReference(refstr)
929       self.pid_list = self.ttx.getTextPidsAndName(ref)
930
931     i = 0
932     available = 0
933     for x in self.pid_list:
934       if x[2] != -1:
935         available = available + 1
936       if x[2] == self.txtpid:
937         self.pid_index = i
938       i = i + 1
939     self.pid_count = available
940
941     log("transponder: %s" % self.pid_list)
942
943   def switchChannel(self, do_send = True):
944     if self.demux > -1 and self.txtpid > -1 and do_send:
945       x = array.array('B', (CMD_CTL_CACHE, (self.txtpid & 0xFF00) >> 8, (self.txtpid & 0xFF), self.demux))
946       self.socketSend(x)
947       
948     # read favorites
949     self.readFavorites()
950
951   def readFavorites(self):
952     self.fav_index = 0
953     self.fav_list = []
954     hasStart = False
955     if len(self.pid_list) > 0:
956       for i in range(10):
957         text = self.favorites.getFavorite(self.pid_list[self.pid_index][0], i)
958         if text is not None:
959           page = int(text[0])
960           self.fav_list.append(page)
961           if page == 100:
962             hasStart = True
963     if hasStart == False:
964       self.fav_list.append(100)
965     log("favorites: %s" % self.fav_list)
966   
967   # ---- for summary (lcd) ----
968
969   def createSummary(self):
970     return TeleTextSummary
971
972   def getCurrentPage(self):
973     return self.cur_page
974
975   def getAvailableTxtPidCount(self):
976     return self.pid_count
977
978   def naviEnabled(self):
979     return self.naviValue
980
981   def infoEnabled(self):
982     return self.infoValue
983
984 # ----------------------------------------
985
986 class TeleTextSummary(Screen):
987
988   def __init__(self, session, parent):
989     onPic  = resolveFilename(SCOPE_PLUGINS, "Extensions/TeleText/lcd_on.png")
990     offPic = resolveFilename(SCOPE_PLUGINS, "Extensions/TeleText/lcd_off.png")
991
992     TeleTextSummary.skin = ("""<screen name="TeleTextSummary" position="0,0" size="132,64" id="1">
993       <widget name="page"     position="0,0"   size="132,20" font="Regular;20" valign="center" halign="center" zPosition="1"/>
994
995       <widget name="navi_off" position="12,28"  size="20,20" pixmap="%s" zPosition="1"/>
996       <widget name="info_off" position="100,28" size="20,20" pixmap="%s" zPosition="1"/>
997       <widget name="navi_on"  position="12,28"  size="20,20" pixmap="%s" zPosition="2"/>
998       <widget name="info_on"  position="100,28" size="20,20" pixmap="%s" zPosition="2"/>
999       <widget name="tp_count" position="44,28"  size="44,20" font="Regular;16" valign="center" halign="center" zPosition="1"/>
1000
1001       <widget name="navi_txt" position="0,50"  size="44,12" font="Regular;12" valign="center" halign="center" zPosition="1"/>
1002       <widget name="tp_txt"   position="44,50" size="44,12" font="Regular;12" valign="center" halign="center" zPosition="1"/>
1003       <widget name="info_txt" position="88,50" size="44,12" font="Regular;12" valign="center" halign="center" zPosition="1"/>
1004     </screen>""" % (offPic, offPic, onPic, onPic),
1005     """<screen name="TeleTextSummary" position="0,0" size="96,64" id="2">
1006       <widget name="page"     position="0,0"   size="96,20" font="Regular;20" valign="center" halign="center" zPosition="1"/>
1007
1008       <widget name="navi_off" position="6,28"  size="20,20" pixmap="%s" zPosition="1"/>
1009       <widget name="info_off" position="70,28" size="20,20" pixmap="%s" zPosition="1"/>
1010       <widget name="navi_on"  position="6,28"  size="20,20" pixmap="%s" zPosition="2"/>
1011       <widget name="info_on"  position="70,28" size="20,20" pixmap="%s" zPosition="2"/>
1012       <widget name="tp_count" position="32,28" size="32,20" font="Regular;16" valign="center" halign="center" zPosition="1"/>
1013
1014       <widget name="navi_txt" position="0,50"  size="32,12" font="Regular;12" valign="center" halign="center" zPosition="1"/>
1015       <widget name="tp_txt"   position="36,50" size="32,12" font="Regular;12" valign="center" halign="center" zPosition="1"/>
1016       <widget name="info_txt" position="64,50" size="32,12" font="Regular;12" valign="center" halign="center" zPosition="1"/>
1017     </screen>""" % (offPic, offPic, onPic, onPic))
1018
1019     Screen.__init__(self, session, parent = parent)
1020     self["page"] = Label("")
1021     self["navi_txt"] = Label("NAVI")
1022     self["tp_txt"] = Label("TPT")
1023     self["tp_count"] = Label("<1>")
1024     self["info_txt"] = Label("INFO")
1025     self["navi_off"] = Pixmap()
1026     self["info_off"] = Pixmap()
1027     self["navi_on"]  = Pixmap()
1028     self["info_on"]  = Pixmap()
1029     self.onShow.append(self.addWatcher)
1030     self.onHide.append(self.removeWatcher)
1031
1032   def addWatcher(self):
1033     self.parent.onChangedEntry.append(self.selectionChanged)
1034     self.selectionChanged()
1035
1036   def removeWatcher(self):
1037     self.parent.onChangedEntry.remove(self.selectionChanged)
1038
1039   def selectionChanged(self):
1040     self["tp_count"].setText("< %s >"%self.parent.getAvailableTxtPidCount())
1041     self["page"].setText(self.parent.getCurrentPage())
1042     if self.parent.naviEnabled():
1043       self["navi_off"].hide()
1044       self["navi_on"].show()
1045     else:
1046       self["navi_on"].hide()
1047       self["navi_off"].show()
1048
1049     if self.parent.infoEnabled():
1050       self["info_off"].hide()
1051       self["info_on"].show()
1052     else:
1053       self["info_on"].hide()
1054       self["info_off"].show()
1055
1056 # ----------------------------------------
1057
1058 class TeleTextTransponderMenu(Screen):
1059
1060   ch_list = []
1061   ch_index = 0
1062
1063   cur_service = ""
1064   new_service = ""
1065
1066   onChangedEntry = [ ]
1067
1068   def __init__(self, session, list, index):
1069     log("[transponder] __init__")
1070
1071     self.ch_list = list
1072     self.ch_index = index
1073     self.cur_service = self.ch_list[self.ch_index][1]
1074
1075     width = 420
1076     height = 75
1077     left = (dsk_width - width)>>1
1078     top = (dsk_height - height)>>1
1079     log("[transponder] screen rect %s %s %s %s" % (left, top, width, height))
1080     TeleTextTransponderMenu.skin = """<screen position="%d,%d" size="%d,%d" title="%s">
1081         <widget name="prev"    position="0,5"   size="35,25"  zPosition="4" pixmap="%s" alphatest="on"/>
1082         <widget name="channel" position="40,7"  size="325,20" zPosition="5" valign="center" halign="left"  font="Regular;21" transparent="1" foregroundColor="white"/>
1083         <widget name="zapped"  position="365,7" size="20,20"  zPosition="5" valign="center" halign="right" font="Regular;21" transparent="1" foregroundColor="#888888"/>
1084         <widget name="next"    position="385,5" size="35,25"  zPosition="4" pixmap="%s" alphatest="on"/>
1085
1086         <ePixmap pixmap="skin_default/div-h.png" position="0,32" zPosition="1" size="420,2" />
1087
1088         <ePixmap pixmap="skin_default/buttons/red.png"    position="0,35"   zPosition="0" size="140,40" transparent="1" alphatest="on" />
1089         <ePixmap pixmap="skin_default/buttons/green.png"  position="280,35" zPosition="0" size="140,40" transparent="1" alphatest="on" />
1090         <widget name="key_r" position="0,35"   size="140,40" zPosition="5" valign="center" halign="center" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
1091         <widget name="key_g" position="280,35" size="140,40" zPosition="5" valign="center" halign="center" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
1092       </screen>""" % (left, top, width, height, _("Select teletext"), resolveFilename(SCOPE_PLUGINS, "Extensions/TeleText/key_lt.png"), resolveFilename(SCOPE_PLUGINS, "Extensions/TeleText/key_gt.png"))
1093     Screen.__init__(self, session)
1094
1095     self["actions"] = ActionMap(["OkCancelActions", "TeleTextActions"],
1096     {
1097       "ok"     : self.okPressed,
1098       "cancel" : self.cancelPressed,
1099       "red"    : self.cancelPressed,
1100       "green"  : self.okPressed,
1101       "left"   : self.prevPressed,
1102       "right"  : self.nextPressed,
1103       "prev"   : self.prevPressed,
1104       "next"   : self.nextPressed,
1105       "prev_long": self.prevPressed,
1106       "next_long": self.nextPressed
1107     }, -2)
1108     self["actions"].setEnabled(True)
1109
1110     self["key_r"] = Label(_("Cancel"))
1111     self["key_g"] = Label(_("OK"))
1112
1113     self["channel"] = Label("")
1114     self["prev"] = Pixmap()
1115     self["next"] = Pixmap()
1116     self["zapped"] = Label("*")
1117     self["zapped"].hide()
1118     if len(self.ch_list) == 1:
1119       self["prev"].hide()
1120       self["next"].hide()
1121     else:
1122       self["prev"].show()
1123       self["next"].show()
1124     self.updateLayout()
1125
1126   def updateLayout(self):
1127     if self.ch_list[self.ch_index][2] == -1:
1128       self["zapped"].show()
1129       self["key_g"].setText("")
1130     else:
1131       self["zapped"].hide()
1132       self["key_g"].setText(_("OK"))
1133     self.new_service = self.ch_list[self.ch_index][1]
1134     self["channel"].setText(self.new_service)
1135     for i in self.onChangedEntry:
1136       i()
1137
1138   def prevPressed(self):
1139     log("[transponder] prev pressed")
1140     if self.ch_index == 0:
1141       self.ch_index = len(self.ch_list) - 1
1142     else:
1143       self.ch_index = self.ch_index - 1
1144     self.updateLayout()
1145
1146   def nextPressed(self):
1147     log("[transponder] next pressed")
1148     if self.ch_index == (len(self.ch_list) - 1):
1149       self.ch_index = 0
1150     else:
1151       self.ch_index = self.ch_index + 1
1152     self.updateLayout()
1153
1154   def okPressed(self):
1155     log("[transponder] ok pressed")
1156     if self.ch_list[self.ch_index][2] > -1:
1157       self.close(self.ch_list[self.ch_index][2])
1158
1159   def cancelPressed(self):
1160     log("[transponder] cancel pressed")
1161     self.close(-1)
1162
1163   # ---- for summary (lcd) ----
1164
1165   def createSummary(self):
1166     return TeleTextTransponderSummary
1167
1168   def getCurrentService(self):
1169     return self.cur_service
1170
1171   def getNewService(self):
1172     return self.new_service
1173
1174 # ----------------------------------------
1175
1176 class TeleTextTransponderSummary(Screen):
1177
1178   def __init__(self, session, parent):
1179
1180     TeleTextTransponderSummary.skin = ("""<screen name="TeleTextTransponderSummary" position="0,0" size="132,64" id="1">
1181       <widget name="c_service" position="0,5"   size="100,20" font="Regular;20" halign="left"/>
1182       <ePixmap pixmap="skin_default/div-h.png" position="46,32" size="40,2" zPosition="1"/>
1183       <widget name="n_service" position="32,39" size="100,20" font="Regular;20" halign="right"/>
1184     </screen>""",
1185     """<screen name="TeleTextTransponderSummary" position="0,0" size="96,64" id="2">
1186       <widget name="c_service" position="0,5"   size="90,20" font="Regular;20" halign="left"/>
1187       <ePixmap pixmap="skin_default/div-h.png" position="28,32" size="40,2" zPosition="1"/>
1188       <widget name="n_service" position="6,39" size="90,20" font="Regular;20" halign="right"/>
1189     </screen>""")
1190
1191     Screen.__init__(self, session, parent = parent)
1192     self["c_service"] = Label(self.parent.getCurrentService())
1193     self["n_service"] = Label(self.parent.getNewService())
1194     self.onShow.append(self.addWatcher)
1195     self.onHide.append(self.removeWatcher)
1196
1197   def addWatcher(self):
1198     self.parent.onChangedEntry.append(self.selectionChanged)
1199     self.selectionChanged()
1200
1201   def removeWatcher(self):
1202     self.parent.onChangedEntry.remove(self.selectionChanged)
1203
1204   def selectionChanged(self):
1205     self["n_service"].setText(self.parent.getNewService())
1206
1207 # ----------------------------------------
1208
1209 class TeleTextMenu(ConfigListScreen, Screen):
1210
1211   onChangedEntry = [ ]
1212   isInitialized = False
1213   parent = None
1214
1215   def __init__(self, session, parent):
1216     width = 492
1217     height = 460
1218     left = (dsk_width - width)>>1
1219     top = (dsk_height - height)>>1
1220     log("[menu] screen rect %s %s %s %s" % (left, top, width, height))
1221     TeleTextMenu.skin = """<screen position="%d,%d" size="%d,%d" title="%s">
1222         <widget name="config" position="0,0"   size="492,335" scrollbarMode="showOnDemand" zPosition="1"/>
1223         <ePixmap pixmap="skin_default/div-h.png" position="0,338" zPosition="1" size="492,2" />
1224         <widget name="label"  position="0,340" size="492,70" font="Regular;16" zPosition="1" halign="left" valign="top"/>
1225         <ePixmap pixmap="skin_default/div-h.png" position="0,415" zPosition="1" size="492,2" />
1226         <ePixmap pixmap="skin_default/buttons/red.png"    position="0,420"   zPosition="0" size="140,40" transparent="1" alphatest="on" />
1227         <ePixmap pixmap="skin_default/buttons/green.png"  position="176,420" zPosition="0" size="140,40" transparent="1" alphatest="on" />
1228         <ePixmap pixmap="skin_default/buttons/yellow.png" position="352,420" zPosition="0" size="140,40" transparent="1" alphatest="on" />
1229         <widget name="key_r" position="0,420"   size="140,40" zPosition="5" valign="center" halign="center" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
1230         <widget name="key_g" position="176,420" size="140,40" zPosition="5" valign="center" halign="center" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
1231         <widget name="key_y" position="352,420" size="140,40" zPosition="5" valign="center" halign="center" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
1232       </screen>""" % (left, top, width, height, _("TeleText settings"))
1233
1234     Screen.__init__(self, session, parent)
1235
1236     # create config list
1237     self.list = []
1238     ConfigListScreen.__init__(self, self.list)
1239     self.createConfigList()
1240
1241     self["actions"] = ActionMap(["OkCancelActions", "TeleTextActions"],
1242     {
1243       "ok"     : self.okPressed,
1244       "cancel" : self.cancelPressed,
1245       "red"    : self.cancelPressed,
1246       "green"  : self.okPressed,
1247       "yellow" : self.resetPressed,
1248       "menu"   : self.cancelPressed,
1249       "text"   : self.textPressed,
1250       "info"   : self.infoPressed
1251     }, -2)
1252     self["actions"].setEnabled(True)
1253
1254     self["label"] = Label("Info")
1255     self["key_r"] = Label(_("Cancel"))
1256     self["key_y"] = Label(_("Default"))
1257     self["key_g"] = Label(_("OK"))
1258     self.onLayoutFinish.append(self.__layoutFinished)
1259
1260   def __layoutFinished(self):
1261     self.isInitialized = True
1262     if not self.selectionChanged in self["config"].onSelectionChanged:
1263       self["config"].onSelectionChanged.append(self.selectionChanged)
1264     self.selectionChanged()
1265
1266   def selectionChanged(self):
1267     configele = self["config"].getCurrent()[1]
1268     if configele == config.plugins.TeleText.pos:
1269       self["label"].setText(HELP_TEXT_POS)
1270     elif configele == config.plugins.TeleText.brightness:
1271       self["label"].setText(HELP_TEXT_BRIGHTNESS)
1272     elif configele == config.plugins.TeleText.contrast:
1273       self["label"].setText(HELP_TEXT_CONTRAST)
1274     elif configele == config.plugins.TeleText.transparency:
1275       self["label"].setText(HELP_TEXT_TRANSPARENCY)
1276     elif configele == config.plugins.TeleText.splitting_mode:
1277       self["label"].setText(HELP_TEXT_SPLITTING)
1278     elif configele == config.plugins.TeleText.tip_pos:
1279       self["label"].setText(HELP_TEXT_TIP_POS)
1280     elif configele == config.plugins.TeleText.debug:
1281       self["label"].setText(HELP_TEXT_DEBUG)
1282     elif configele == config.plugins.TeleText.edge_cut:
1283       self["label"].setText(HELP_TEXT_EDGE_CUT)
1284     elif configele == config.plugins.TeleText.textlevel:
1285       self["label"].setText(HELP_TEXT_TEXTLEVEL)
1286     elif configele == config.plugins.TeleText.region:
1287       self["label"].setText(HELP_TEXT_REGION)
1288     elif configele == config.plugins.TeleText.scale_filter:
1289       self["label"].setText(HELP_TEXT_SCALE_FILTER)
1290     elif configele == config.plugins.TeleText.scale_filter_zoom:
1291       self["label"].setText(HELP_TEXT_SCALE_FILTER)
1292     elif configele == config.plugins.TeleText.background_caching:
1293       self["label"].setText(HELP_TEXT_CACHING)
1294
1295   def createConfig(self, configele):
1296     if not self.isInitialized:
1297       return
1298     self.createConfigList()
1299
1300   def createConfigList(self):
1301     self.isInitialized = False
1302
1303     # remove notifiers
1304     for x in self["config"].list:
1305       x[1].clearNotifiers()
1306
1307     self.list = [
1308       getConfigListEntry(_("Scale filter"),      config.plugins.TeleText.scale_filter),
1309       getConfigListEntry(_("Scale filter zoom"), config.plugins.TeleText.scale_filter_zoom),
1310       getConfigListEntry(_("Brightness"),        config.plugins.TeleText.brightness),
1311       getConfigListEntry(_("Contrast"),          config.plugins.TeleText.contrast),
1312       getConfigListEntry(_("Transparency"),      config.plugins.TeleText.transparency),
1313       getConfigListEntry(_("Text level"),        config.plugins.TeleText.textlevel),
1314       getConfigListEntry(_("Region"),            config.plugins.TeleText.region),
1315       getConfigListEntry(_("Position and size"), config.plugins.TeleText.pos),
1316       getConfigListEntry(_("Display edges"),     config.plugins.TeleText.edge_cut),
1317       getConfigListEntry(_("Splitting mode"),    config.plugins.TeleText.splitting_mode)
1318     ]
1319     if config.plugins.TeleText.splitting_mode.value == SPLIT_MODE_TIP:
1320       self.list.append(getConfigListEntry("... %s" % _("Position and size"),   config.plugins.TeleText.tip_pos))
1321     self.list.append(getConfigListEntry(_("Background caching"),config.plugins.TeleText.background_caching))
1322     self.list.append(getConfigListEntry(_("Debug"),   config.plugins.TeleText.debug))
1323
1324     self["config"].list = self.list
1325
1326     # add notifiers (lcd, info)
1327     for x in self["config"].list:
1328       x[1].addNotifier(self.changedEntry)
1329     # add notifiers (menu)
1330     config.plugins.TeleText.splitting_mode.addNotifier(self.createConfig)
1331
1332     self.isInitialized = True
1333
1334   def resetPressed(self):
1335     log("[menu] reset pressed")
1336     config.plugins.TeleText.scale_filter.setValue("%d"%BILINEAR)
1337     config.plugins.TeleText.scale_filter_zoom.setValue("%d"%BILINEAR)
1338     config.plugins.TeleText.brightness.setValue(8)
1339     config.plugins.TeleText.contrast.setValue(12)
1340     config.plugins.TeleText.transparency.setValue(8)
1341     config.plugins.TeleText.edge_cut.setValue(False)
1342     config.plugins.TeleText.splitting_mode.setValue(SPLIT_MODE_PAT)
1343     config.plugins.TeleText.textlevel.setValue("2")
1344     config.plugins.TeleText.region.setValue("16")
1345     config.plugins.TeleText.debug.setValue(False)
1346     config.plugins.TeleText.pos.setValue([DEF_LEFT, DEF_TOP, DEF_RIGHT, DEF_BOTTOM])
1347     config.plugins.TeleText.tip_pos.setValue([(dsk_width>>1)+(dsk_width>>2), (dsk_height>>1)+(dsk_height>>2), dsk_width, dsk_height])
1348     config.plugins.TeleText.background_caching.setValue(True)
1349     self["config"].selectionChanged()
1350
1351   def infoPressed(self):
1352     log("[menu] info pressed")
1353     self.session.open(TeleTextAboutScreen, self.parent.daemonVersion)
1354
1355   def textPressed(self):
1356     log("[menu] text pressed")
1357     if self["config"].getCurrent()[1] == config.plugins.TeleText.pos:
1358       self.close(config.plugins.TeleText.pos)
1359     elif self["config"].getCurrent()[1] == config.plugins.TeleText.tip_pos:
1360       self.close(config.plugins.TeleText.tip_pos)
1361
1362   def okPressed(self):
1363     log("[menu] ok pressed")
1364     self.checkPositionValues(config.plugins.TeleText.pos)
1365     self.checkPositionValues(config.plugins.TeleText.tip_pos)
1366     for x in self["config"].list:
1367       x[1].save()
1368     configfile.save()
1369     self.close(None)
1370
1371   def checkPositionValues(self, configele):
1372     pos = configele.value
1373     log("... old pos: %s %s %s %s" % (pos[0], pos[1], pos[2], pos[3]))
1374     update = False
1375     if pos[0] > pos[2]:
1376       i = pos[0]
1377       pos[0] = pos[2]
1378       pos[2] = i
1379       update = True
1380     if pos[1] > pos[3]:
1381       i = pos[1]
1382       pos[1] = pos[3]
1383       pos[3] = i
1384       update = True
1385     if (pos[2] - pos[0]) < MIN_W:
1386       pos[2] = pos[0] + MIN_W
1387       update = True
1388     if (pos[3] - pos[1]) < MIN_H:
1389       pos[3] = pos[1] + MIN_H
1390       update = True
1391     if pos[2] > dsk_width:
1392       pos[0] = pos[0] + dsk_width - pos[2]
1393       pos[2] = dsk_width
1394       update = True
1395     if pos[3] > dsk_height:
1396       pos[1] = pos[1] + dsk_height - pos[3]
1397       pos[3] = dsk_height
1398       update = True
1399     log("... new pos: %s %s %s %s" % (pos[0], pos[1], pos[2], pos[3]))
1400
1401     if update:
1402       configele.setValue(pos)
1403
1404   def cancelPressed(self):
1405     log("[menu] cancel pressed")
1406     confirm = False
1407     for x in self["config"].list:
1408       confirm = confirm or x[1].isChanged()
1409     if confirm:
1410       self.session.openWithCallback(self.DiscardConfirm, MessageBox, _("Discard changes?"))
1411     else:
1412       self.close(None)
1413
1414   def DiscardConfirm(self, result):
1415     if result:
1416       for x in self["config"].list:
1417         if x[1].isChanged():
1418           x[1].cancel()
1419       self.close(None)
1420
1421   # ---- for summary (lcd) ----
1422
1423   def changedEntry(self, element=None):
1424     for x in self.onChangedEntry:
1425       x()
1426
1427   def getCurrentEntry(self):
1428     return self["config"].getCurrent()[0]
1429
1430   def getCurrentValue(self):
1431     return str(self["config"].getCurrent()[1].getText())
1432
1433   def createSummary(self):
1434     return TeleTextMenuSummary
1435
1436 # ----------------------------------------
1437
1438 class TeleTextMenuSummary(Screen):
1439
1440   skin = ("""<screen name="TeleTextMenuSummary" position="0,0" size="132,64" id="1">
1441       <widget name="SetupTitle" position="6,4"  size="120,20" font="Regular;20" halign="center"/>
1442       <widget name="SetupEntry" position="6,30" size="120,12" font="Regular;12" halign="left"/>
1443       <widget name="SetupValue" position="6,48" size="120,12" font="Regular;12" halign="right"/>
1444     </screen>""",
1445     """<screen name="TeleTextMenuSummary" position="0,0" size="96,64" id="2">
1446       <widget name="SetupTitle" position="3,4"  size="90,20" font="Regular;20" halign="center"/>
1447       <widget name="SetupEntry" position="3,30" size="90,12" font="Regular;12" halign="left"/>
1448       <widget name="SetupValue" position="3,48" size="90,12" font="Regular;12" halign="right"/>
1449     </screen>""")
1450
1451   def __init__(self, session, parent):
1452     Screen.__init__(self, session, parent = parent)
1453     self["SetupTitle"] = Label(_("TeleText settings"))
1454     self["SetupEntry"] = Label("")
1455     self["SetupValue"] = Label("")
1456     self.onShow.append(self.addWatcher)
1457     self.onHide.append(self.removeWatcher)
1458
1459   def addWatcher(self):
1460     self.parent.onChangedEntry.append(self.selectionChanged)
1461     self.parent["config"].onSelectionChanged.append(self.selectionChanged)
1462     self.selectionChanged()
1463
1464   def removeWatcher(self):
1465     self.parent.onChangedEntry.remove(self.selectionChanged)
1466     self.parent["config"].onSelectionChanged.remove(self.selectionChanged)
1467
1468   def selectionChanged(self):
1469     self["SetupEntry"].text = self.parent.getCurrentEntry()
1470     self["SetupValue"].text = self.parent.getCurrentValue()
1471
1472 # ----------------------------------------
1473
1474 class TeleTextAboutScreen(Screen):
1475
1476   daemonVersion = "0.1"
1477
1478   def __init__(self, session, dVersion):
1479     self.daemonVersion = dVersion
1480
1481     width = 360
1482     height = 240
1483     left = (dsk_width - width)>>1
1484     top = (dsk_height - height)>>1
1485     log("[about] screen rect %s %s %s %s" % (left, top, width, height))
1486     TeleTextAboutScreen.skin = """<screen position="%d,%d" size="%d,%d" flags="wfNoBorder">
1487         <widget name="pic"    position="0,0"    size="360,240" zPosition="1" pixmap="%s" alphatest="on"/>
1488         <widget name="text"   position="12,16"  size="336,36"  zPosition="2" font="Console;36" halign="center" backgroundColor="blue"  foregroundColor="white" />
1489         <widget name="daemon" position="12,64"  size="336,24"  zPosition="2" font="Console;20" halign="left"   backgroundColor="white" foregroundColor="black" />
1490         <widget name="plugin" position="12,200" size="336,24"  zPosition="2" font="Console;20" halign="right"  backgroundColor="white" foregroundColor="black" />
1491       </screen>""" % (left, top, width, height, resolveFilename(SCOPE_PLUGINS, "Extensions/TeleText/teletext.png"))
1492
1493     Screen.__init__(self, session)
1494
1495     self["actions"] = ActionMap(["OkCancelActions"],
1496     {
1497       "ok"     : self.okPressed,
1498       "cancel" : self.cancelPressed
1499     }, -2)
1500     self["actions"].setEnabled(True)
1501
1502     self["pic"] = Pixmap()
1503     self["text"] = Label("TeleText")
1504     self["daemon"] = Label("Daemon v%s" % self.daemonVersion)
1505     self["plugin"] = Label("Plugin v%s" % PLUGIN_VERSION)
1506
1507   def okPressed(self):
1508     log("[about] ok pressed")
1509     self.close(None)
1510
1511   def cancelPressed(self):
1512     log("[about] cancel pressed")
1513     self.close(None)
1514
1515   # ---- for summary (lcd) ----
1516
1517   def getDaemonVersion(self):
1518     return self.daemonVersion
1519
1520   def createSummary(self):
1521     return TeleTextAboutSummary
1522
1523 # ----------------------------------------
1524
1525 class TeleTextAboutSummary(Screen):
1526
1527   skin = ("""<screen name="TeleTextAboutSummary" position="0,0" size="132,64" id="1">
1528       <widget name="title"  position="6,4"  size="120,24" font="Regular;20" halign="center"/>
1529       <widget name="daemon" position="6,30" size="120,16" font="Regular;12" halign="left"/>
1530       <widget name="plugin" position="6,48" size="120,16" font="Regular;12" halign="left"/>
1531     </screen>""",
1532     """<screen name="TeleTextAboutSummary" position="0,0" size="96,64" id="2">
1533       <widget name="title"  position="3,4"  size="90,20" font="Regular;20" halign="center"/>
1534       <widget name="daemon" position="3,30" size="90,16" font="Regular;12" halign="left"/>
1535       <widget name="plugin" position="3,48" size="90,16" font="Regular;12" halign="left"/>
1536     </screen>""")
1537
1538   def __init__(self, session, parent):
1539     Screen.__init__(self, session, parent = parent)
1540     self["title"] = Label("TeleText")
1541     self["daemon"] = Label("Daemon v%s" % parent.getDaemonVersion())
1542     self["plugin"] = Label("Plugin v%s" % PLUGIN_VERSION)
1543
1544 # ----------------------------------------
1545
1546 class TeleTextFavorites():
1547
1548   configFile = "/etc/enigma2/teletext.fav"
1549
1550   def __init__(self):
1551     log("[favorites] reading")
1552     self.parser = ConfigParser()
1553     self.parser.read(self.configFile)
1554
1555   def write(self):
1556     log("[favorites] writing")
1557     fp = open(self.configFile,"w")
1558     self.parser.write(fp)
1559     fp.close()   
1560
1561   def getFavorite(self, service, index):
1562     index = str(index)
1563     if self.parser.has_option(service, index) is False:
1564       return None
1565     return self.parser.get(service, index).split(";",1)
1566
1567   def getFavorites(self, service):
1568     result = []
1569     if self.parser.has_section(service) is True:
1570       for x in self.parser.options(service):
1571         result.append(self.getFavorite(service, x))
1572     return result
1573
1574   def setFavorite(self, service, index, page, text):
1575     index = str(index)
1576     page = str(page)
1577     if self.parser.has_section(service) is False:
1578       self.parser.add_section(service)
1579     self.parser.set(service, index, "%s;%s" % (page, text))
1580
1581   def removeFavorite(self, service, index):
1582     index = str(index)
1583     self.parser.remove_option(service, index)
1584     if len(self.parser.options(service)) == 0:
1585       self.parser.remove_section(service)
1586
1587   def removeService(self, service):
1588     self.parser.remove_section(service)
1589
1590 # ----------------------------------------
1591
1592 class TeleTextFavoritesMenu(Screen):
1593
1594   onChangedEntry = [ ]
1595
1596   def __init__(self, session, parent, service, page, favorites):
1597     width = 590
1598     height = 300
1599     left = (dsk_width - width)>>1
1600     top = (dsk_height - height)>>1
1601     log("[fav-menu] screen rect %s %s %s %s" % (left, top, width, height))
1602     TeleTextFavoritesMenu.skin = """<screen position="%d,%d" size="%d,%d" title="%s [%s]">
1603         <ePixmap pixmap="skin_default/buttons/key_1.png" position="0,0"   zPosition="1" size="35,25" transparent="1" alphatest="on"/>
1604         <ePixmap pixmap="skin_default/buttons/key_2.png" position="0,25"  zPosition="1" size="35,25" transparent="1" alphatest="on"/>
1605         <ePixmap pixmap="skin_default/buttons/key_3.png" position="0,50"  zPosition="1" size="35,25" transparent="1" alphatest="on"/>
1606         <ePixmap pixmap="skin_default/buttons/key_4.png" position="0,75"  zPosition="1" size="35,25" transparent="1" alphatest="on"/>
1607         <ePixmap pixmap="skin_default/buttons/key_5.png" position="0,100" zPosition="1" size="35,25" transparent="1" alphatest="on"/>
1608         <ePixmap pixmap="skin_default/buttons/key_6.png" position="0,125" zPosition="1" size="35,25" transparent="1" alphatest="on"/>
1609         <ePixmap pixmap="skin_default/buttons/key_7.png" position="0,150" zPosition="1" size="35,25" transparent="1" alphatest="on"/>
1610         <ePixmap pixmap="skin_default/buttons/key_8.png" position="0,175" zPosition="1" size="35,25" transparent="1" alphatest="on"/>
1611         <ePixmap pixmap="skin_default/buttons/key_9.png" position="0,200" zPosition="1" size="35,25" transparent="1" alphatest="on"/>
1612         <ePixmap pixmap="skin_default/buttons/key_0.png" position="0,225" zPosition="1" size="35,25" transparent="1" alphatest="on"/>
1613         <widget name="fav_list" position="40,0"   size="550,250" zPosition="1"/>
1614
1615         <ePixmap pixmap="skin_default/div-h.png" position="0,255" zPosition="1" size="590,2"/>
1616
1617         <ePixmap pixmap="skin_default/buttons/red.png"    position="0,260"   zPosition="0" size="140,40" transparent="1" alphatest="on" />
1618         <ePixmap pixmap="skin_default/buttons/green.png"  position="150,260" zPosition="0" size="140,40" transparent="1" alphatest="on" />
1619         <ePixmap pixmap="skin_default/buttons/yellow.png" position="300,260" zPosition="0" size="140,40" transparent="1" alphatest="on" />
1620         <ePixmap pixmap="skin_default/buttons/blue.png"   position="450,260" zPosition="0" size="140,40" transparent="1" alphatest="on" />
1621         <widget name="key_r" position="0,260"   size="140,40" zPosition="5" valign="center" halign="center" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
1622         <widget name="key_g" position="150,260" size="140,40" zPosition="5" valign="center" halign="center" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
1623         <widget name="key_y" position="300,260" size="140,40" zPosition="5" valign="center" halign="center" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
1624         <widget name="key_b" position="450,260" size="140,40" zPosition="5" valign="center" halign="center" font="Regular;21" transparent="1" foregroundColor="white" shadowColor="black" shadowOffset="-1,-1" />
1625       </screen>""" % (left, top, width, height, _("TeleText favorites"), service[1])
1626
1627     self.service = service[0]
1628     self.page = page
1629     self.favorites = favorites
1630
1631     Screen.__init__(self, session, parent)
1632     self["fav_list"] = MenuList([], enableWrapAround = True)
1633     self["fav_list"].onSelectionChanged.append(self.updateLayout)
1634
1635     self["actions"] = NumberActionMap(["OkCancelActions", "TeleTextActions"],
1636     {
1637       "1"      : self.keyNumberGlobal,
1638       "2"      : self.keyNumberGlobal,
1639       "3"      : self.keyNumberGlobal,
1640       "4"      : self.keyNumberGlobal,
1641       "5"      : self.keyNumberGlobal,
1642       "6"      : self.keyNumberGlobal,
1643       "7"      : self.keyNumberGlobal,
1644       "8"      : self.keyNumberGlobal,
1645       "9"      : self.keyNumberGlobal,
1646       "0"      : self.keyNumberGlobal,
1647       "ok"     : self.okPressed,
1648       "cancel" : self.cancelPressed,
1649       "red"    : self.redPressed,
1650       "green"  : self.okPressed,
1651       "yellow" : self.yellowPressed,
1652       "blue"   : self.bluePressed
1653     }, -2)
1654     self["actions"].setEnabled(True)
1655
1656     self["key_r"] = Label("")
1657     self["key_g"] = Label("")
1658     self["key_y"] = Label("")
1659     self["key_b"] = Label(_("Clear all"))
1660
1661     self.updateList()
1662     self.updateLayout()
1663
1664   def keyNumberGlobal(self, number):
1665     log("[favorites] %s pressed" % number)
1666     if number == 0:
1667       index = 9
1668     else:
1669       index = number - 1
1670
1671     if self.pages[index] != -1:
1672       self.close(self.pages[index])
1673
1674   def updateList(self):
1675     list = []
1676     self.pages = []
1677
1678     while len(list) < 10:
1679       text = self.favorites.getFavorite(self.service, len(list))
1680       if text is None:
1681         self.pages.append(-1)
1682         list.append(_("<empty>"))
1683       else:
1684         self.pages.append(int(text[0]))
1685         list.append("[%s] %s" % (text[0], text[1]))
1686     self["fav_list"].setList(list)
1687
1688   def updateLayout(self):
1689     value = self["fav_list"].getCurrent()
1690     self.sel_page = self.pages[self["fav_list"].getSelectedIndex()]
1691     if value == _("<empty>"):
1692       self["key_r"].setText("")
1693       self["key_g"].setText("")
1694       self["key_y"].setText(_("Add"))
1695     else:
1696       self["key_r"].setText(_("Delete"))
1697       self["key_g"].setText(_("Select"))
1698       self["key_y"].setText(_("Edit"))
1699
1700     for i in self.onChangedEntry:
1701       i()
1702
1703   def okPressed(self):
1704     if self.sel_page != -1:
1705       log("[favorites] ok pressed")
1706       self.close(self.sel_page)
1707
1708   def cancelPressed(self):
1709     log("[fav-menu] cancel pressed")
1710     self.close(None)
1711
1712   def yellowPressed(self):
1713     log("[fav-menu] yellow pressed")
1714     page = self.sel_page
1715     if page == -1:
1716       page = self.page
1717     value = self["fav_list"].getCurrent()
1718     if value == _("<empty>"):
1719       value = ""
1720     else:
1721       value = value.split(None,1)[1]
1722     self.session.openWithCallback(
1723       self.addFavorite,
1724       NTIVirtualKeyBoard,
1725       title = _("Enter text for page %s")%page,
1726       text = value
1727     )     
1728
1729   def addFavorite(self, text):
1730     if text:
1731       page = self.sel_page
1732       if page == -1:
1733         page = self.page
1734       self.favorites.setFavorite(self.service, self["fav_list"].getSelectedIndex(), page, text)
1735       self.favorites.write()
1736     self.updateList()
1737     self.updateLayout()
1738
1739   def redPressed(self):
1740     log("[fav-menu] red pressed")
1741     if self.sel_page != -1:
1742       self.session.openWithCallback(self.deleteFavorite, MessageBox, _("Delete favorite?"))
1743
1744   def deleteFavorite(self, result):
1745     if result:
1746       self.favorites.removeFavorite(self.service, self["fav_list"].getSelectedIndex())
1747       self.favorites.write()
1748       self.updateList()
1749       self.updateLayout()
1750
1751   def bluePressed(self):
1752     log("[fav-menu] blue pressed")
1753     self.session.openWithCallback(self.cleanupService, MessageBox, _("Delete all favorites?"))
1754
1755   def cleanupService(self, result):
1756     if result:    
1757       self.favorites.removeService(self.service)
1758       self.favorites.write()
1759       self.updateList()
1760       self.updateLayout()
1761
1762   # ---- for summary (lcd) ----
1763
1764   def getCurrentPage(self):
1765     page = self.sel_page
1766     if page == -1:
1767       page = ""
1768     return str(page)
1769
1770   def getCurrentValue(self):
1771     return self["fav_list"].getCurrent()
1772
1773   def createSummary(self):
1774     return TeleTextFavoritesSummary
1775
1776 # ----------------------------------------
1777
1778 class TeleTextFavoritesSummary(Screen):
1779
1780   skin = ("""<screen name="TeleTextFavoritesSummary" position="0,0" size="132,64" id="1">
1781       <widget name="title"  position="6,4"  size="120,24" font="Regular;20" halign="center"/>
1782       <widget name="page"   position="6,30" size="120,16" font="Regular;12" halign="left"/>
1783       <widget name="text"   position="6,48" size="120,16" font="Regular;12" halign="left"/>
1784     </screen>""",
1785     """<screen name="TeleTextFavoritesSummary" position="0,0" size="96,64" id="2">
1786       <widget name="title"  position="3,4"  size="90,20" font="Regular;20" halign="center"/>
1787       <widget name="page"   position="3,30" size="90,16" font="Regular;12" halign="left"/>
1788       <widget name="text"   position="3,48" size="90,16" font="Regular;12" halign="left"/>
1789     </screen>""")
1790
1791   def __init__(self, session, parent):
1792     Screen.__init__(self, session, parent = parent)
1793     self["title"] = Label(_("TeleText favorites"))
1794     self["page"]  = Label()
1795     self["text"]  = Label()
1796
1797     self.onShow.append(self.addWatcher)
1798     self.onHide.append(self.removeWatcher)
1799
1800   def addWatcher(self):
1801     self.parent.onChangedEntry.append(self.selectionChanged)
1802     self.selectionChanged()
1803
1804   def removeWatcher(self):
1805     self.parent.onChangedEntry.remove(self.selectionChanged)
1806
1807   def selectionChanged(self):
1808     self["page"].setText(self.parent.getCurrentPage())
1809     self["text"].setText(self.parent.getCurrentValue())
1810
1811 # ----------------------------------------
1812
1813 def sessionstart(reason, session):
1814   log("----- sessionstart(%s) -----" % session)
1815   # Plugin initialisieren
1816   global ttx_screen
1817   global my_session
1818   ttx_screen = session.instantiateDialog(TeleText)
1819   my_session = session
1820
1821 def autostart(reason, **kwargs):
1822   log("autostart(%s, %s)" % (reason, kwargs))
1823   if reason == 0:
1824     pass
1825   elif reason == 1:
1826     pass
1827
1828 def mainText(session, **kwargs):
1829   global ttx_screen
1830   log("mainText")
1831   if ttx_screen.txtpid != -1:
1832     session.execDialog(ttx_screen)
1833   else:
1834     if len(ttx_screen.pid_list) > 0:
1835       session.openWithCallback(selectText, TeleTextTransponderMenu, ttx_screen.pid_list, ttx_screen.pid_index)
1836     else:
1837       session.open(MessageBox, _("No teletext available."), MessageBox.TYPE_INFO, timeout = 3)
1838
1839 def selectText(result):
1840   global my_session
1841   global ttx_screen
1842   if result > -1:
1843     ttx_screen.txtpid = result
1844     ttx_screen.txtpid_origin = result
1845     ttx_screen.switchChannel(True)
1846     my_session.execDialog(ttx_screen)
1847
1848 def mainMenu(session, **kwargs):
1849   global ttx_screen
1850   log("mainMenu")
1851   session.open(TeleTextMenu)
1852
1853 def Plugins(**kwargs):
1854   log("plugins")
1855   return [
1856     PluginDescriptor(name="TeleText", description="teletext", where = PluginDescriptor.WHERE_TELETEXT, fnc=mainText),
1857     PluginDescriptor(needsRestart = False, where = PluginDescriptor.WHERE_SESSIONSTART, fnc=sessionstart),
1858     PluginDescriptor(needsRestart = False, where = PluginDescriptor.WHERE_AUTOSTART, fnc=autostart)
1859     ]
1860
1861 # EOF