Support duo4kse.
[vuplus_dvbapp] / lib / python / Screens / LanguageSelection.py
1 from Screen import Screen
2
3 from Components.ActionMap import ActionMap
4 from Components.Language import language
5 from Components.config import config
6 from Components.Sources.List import List
7 from Components.Label import Label
8 from Components.Pixmap import Pixmap
9 from Components.language_cache import LANG_TEXT
10
11 def _cached(x):
12         return LANG_TEXT.get(config.osd.language.value, {}).get(x, "")
13
14 from Screens.Rc import Rc
15
16 from Tools.Directories import resolveFilename, SCOPE_CURRENT_SKIN
17
18 from Tools.LoadPixmap import LoadPixmap
19
20 def LanguageEntryComponent(file, name, index):
21         png = LoadPixmap(resolveFilename(SCOPE_CURRENT_SKIN, "countries/" + file + ".png"))
22         if png == None:
23                 png = LoadPixmap(resolveFilename(SCOPE_CURRENT_SKIN, "countries/missing.png"))
24         res = (index, name, png)
25         return res
26
27 class LanguageSelection(Screen):
28         def __init__(self, session):
29                 Screen.__init__(self, session)
30
31                 self.oldActiveLanguage = language.getActiveLanguage()
32
33                 self.list = []
34                 self["languages"] = List(self.list)
35                 self["languages"].onSelectionChanged.append(self.changed)
36
37                 self.updateList()
38                 self.onLayoutFinish.append(self.selectActiveLanguage)
39                 self["actions"] = ActionMap(["OkCancelActions"],
40                 {
41                         "ok": self.save,
42                         "cancel": self.cancel,
43                 }, -1)
44
45         def selectActiveLanguage(self):
46                 activeLanguage = language.getActiveLanguage()
47                 pos = 0
48                 for x in self.list:
49                         if x[0] == activeLanguage:
50                                 self["languages"].index = pos
51                                 break
52                         pos += 1
53
54         def save(self):
55                 self.run()
56                 self.close()
57
58         def cancel(self):
59                 language.activateLanguage(self.oldActiveLanguage)
60                 self.close()
61
62         def run(self, justlocal = False):
63                 print "updating language..."
64                 lang = self["languages"].getCurrent()[0]
65                 config.osd.language.value = lang
66                 config.osd.language.save()
67                 self.setTitle(_cached("T2"))
68                 
69                 if justlocal:
70                         return
71
72                 language.activateLanguage(lang)
73                 config.misc.languageselected.value = 0
74                 config.misc.languageselected.save()
75                 print "ok"
76
77         def updateList(self):
78                 first_time = not self.list
79
80                 languageList = language.getLanguageList()
81                 if not languageList: # no language available => display only english
82                         list = [ LanguageEntryComponent("en", _cached("en_EN"), "en_EN") ]
83                 else:
84                         list = [ LanguageEntryComponent(file = x[1][2].lower(), name = _cached("%s_%s" % x[1][1:3]), index = x[0]) for x in languageList]
85                 self.list = list
86
87                 #list.sort(key=lambda x: x[1][7])
88
89                 print "updateList"
90                 if first_time:
91                         self["languages"].list = list
92                 else:
93                         self["languages"].updateList(list)
94                 print "done"
95
96         def changed(self):
97                 self.run(justlocal = True)
98                 self.updateList()
99
100 class LanguageWizard(LanguageSelection, Rc):
101         def __init__(self, session):
102                 LanguageSelection.__init__(self, session)
103                 Rc.__init__(self)
104                 self.onLayoutFinish.append(self.selectKeys)
105                                 
106                 self["wizard"] = Pixmap()
107                 self["text"] = Label()
108                 self.setText()
109                 
110         def selectKeys(self):
111                 self.clearSelectedKeys()
112                 self.selectKey("UP")
113                 self.selectKey("DOWN")
114                 
115         def changed(self):
116                 self.run(justlocal = True)
117                 self.updateList()
118                 self.setText()
119                 
120         def setText(self):
121                 
122                 self["text"].setText(_cached("T1"))