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