green -> blue
[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
8 from Tools.Directories import *
9
10 from Tools.LoadPixmap import LoadPixmap
11
12 def LanguageEntryComponent(file, name, index):
13         png = LoadPixmap(resolveFilename(SCOPE_SKIN_IMAGE, "countries/" + file + ".png"))
14         if png == None:
15                 png = LoadPixmap(resolveFilename(SCOPE_SKIN_IMAGE, "countries/missing.png"))
16         res = (index, name, png)
17         return res
18
19 class LanguageSelection(Screen):
20         def __init__(self, session):
21                 Screen.__init__(self, session)
22
23                 self.oldActiveLanguage = language.getActiveLanguage()
24
25                 self.list = []
26                 self["languages"] = List(self.list)
27                 self["languages"].onSelectionChanged.append(self.changed)
28
29                 self.updateList()
30                 self.onLayoutFinish.append(self.selectActiveLanguage)
31
32                 self["actions"] = ActionMap(["OkCancelActions"], 
33                 {
34                         "ok": self.save,
35                         "cancel": self.cancel,
36                 }, -1)
37
38         def selectActiveLanguage(self):
39                 activeLanguage = language.getActiveLanguage()
40                 pos = 0
41                 for x in self.list:
42                         if x[0] == activeLanguage:
43                                 self["languages"].index = pos
44                                 break
45                         pos += 1
46
47         def save(self):
48                 self.run()
49                 self.close()
50
51         def cancel(self):
52                 language.activateLanguage(self.oldActiveLanguage)
53                 self.close()
54
55         def run(self):
56                 print "updating language..."
57                 lang = self["languages"].getCurrent()[0]
58                 language.activateLanguage(lang)
59                 config.osd.language.value = lang
60                 config.osd.language.save()
61                 config.misc.languageselected.value = 0
62                 config.misc.languageselected.save()
63                 self.setTitle(_("Language selection"))
64                 print "ok"
65
66         def updateList(self):
67                 print "update list"
68                 first_time = len(self.list) == 0
69
70                 self.list = []
71                 if len(language.getLanguageList()) == 0: # no language available => display only english
72                         self.list.append(LanguageEntryComponent("en", _("English"), "en_EN"))
73                 else:
74                         for x in language.getLanguageList():
75                                 self.list.append(LanguageEntryComponent(file = x[1][2].lower(), name = _(x[1][0]), index = x[0]))
76                 #self.list.sort(key=lambda x: x[1][7])
77
78                 print "updateList"
79                 if first_time:
80                         self["languages"].list = self.list
81                 else:
82                         self["languages"].updateList(self.list)
83                 print "done"
84
85         def changed(self):
86                 self.run()
87                 self.updateList()