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