add possibility to disable user input in Messageboxes
[enigma2.git] / 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_SKIN_IMAGE
17
18 from Tools.LoadPixmap import LoadPixmap
19
20 def LanguageEntryComponent(file, name, index):
21         png = LoadPixmap(resolveFilename(SCOPE_SKIN_IMAGE, "countries/" + file + ".png"))
22         if png == None:
23                 png = LoadPixmap(resolveFilename(SCOPE_SKIN_IMAGE, "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
40                 self["actions"] = ActionMap(["OkCancelActions"], 
41                 {
42                         "ok": self.save,
43                         "cancel": self.cancel,
44                 }, -1)
45
46         def selectActiveLanguage(self):
47                 activeLanguage = language.getActiveLanguage()
48                 pos = 0
49                 for x in self.list:
50                         if x[0] == activeLanguage:
51                                 self["languages"].index = pos
52                                 break
53                         pos += 1
54
55         def save(self):
56                 self.run()
57                 self.close()
58
59         def cancel(self):
60                 language.activateLanguage(self.oldActiveLanguage)
61                 self.close()
62
63         def run(self, justlocal = False):
64                 print "updating language..."
65                 lang = self["languages"].getCurrent()[0]
66                 config.osd.language.value = lang
67                 config.osd.language.save()
68                 self.setTitle(_cached("T2"))
69                 
70                 if justlocal:
71                         return
72
73                 language.activateLanguage(lang)
74                 config.misc.languageselected.value = 0
75                 config.misc.languageselected.save()
76                 print "ok"
77
78         def updateList(self):
79                 first_time = len(self.list) == 0
80
81                 self.list = []
82                 if len(language.getLanguageList()) == 0: # no language available => display only english
83                         self.list.append(LanguageEntryComponent("en", _cached("en_EN"), "en_EN"))
84                 else:
85                         for x in language.getLanguageList():
86                                 self.list.append(LanguageEntryComponent(file = x[1][2].lower(), name = _cached("%s_%s" % x[1][1:3]), index = x[0]))
87                 #self.list.sort(key=lambda x: x[1][7])
88
89                 print "updateList"
90                 if first_time:
91                         self["languages"].list = self.list
92                 else:
93                         self["languages"].updateList(self.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"))