sort language choices by translated string instead of code
[enigma2-plugins.git] / trackautoselect / src / TrackAutoselectConfig.py
1 from Components.config import config, ConfigSubsection, ConfigYesNo, ConfigInteger, ConfigSet
2 from Components.Language import language
3 from Tools.ISO639 import ISO639Language
4 from enigma import eServiceReference
5
6 class Autoselect639Language(ISO639Language):
7                 def __init__(self):
8                         ISO639Language.__init__(self, self.SECONDARY)
9
10                 def getTranslatedChoicesDictAndSortedListAndDefaults(self):
11                         syslang = language.getLanguage()[:2]
12                         choices_dict = {}
13                         choices_list = []
14                         defaults = []
15                         for lang, id_list in self.idlist_by_name.iteritems():
16                                 if syslang not in id_list and 'en' not in id_list:
17                                         name = _(lang)
18                                         short_id = sorted(id_list, key=len)[0]
19                                         choices_dict[short_id] = name
20                                         choices_list.append((short_id, name))
21                         choices_list.sort(key=lambda x: x[1])
22                         syslangname = _(self.name_by_shortid[syslang])
23                         choices_list.insert(0, (syslang, syslangname))
24                         choices_dict[syslang] = syslangname
25                         defaults.append(syslang)
26                         if syslang != "en":
27                                 enlangname = _(self.name_by_shortid["en"])
28                                 choices_list.insert(1, ("en", enlangname))
29                                 choices_dict["en"] = enlangname
30                                 defaults.append("en")
31                         return (choices_dict, choices_list, defaults)
32
33 class TrackAutoselectConfig():
34         CATEGORY_AUDIO_ORDER = _("Sort order of audio parameters")
35         CATEGORY_AUDIO_LANGUAGE = _("Preference of audio language")
36         CATEGORY_AUDIO_FORMAT = _("Preference of audio format")
37         CATEGORY_AUDIO_DESCRIPTION = _("Preference of audio description")
38         CATEGORY_SUBTITLE_ENABLE = _("Auto-enable conditions for subtitles")
39         CATEGORY_SUBTITLE_ORDER = _("Sort order of subtitle parameters")
40         CATEGORY_SUBTITLE_LANGUAGE = _("Preference of subtitle language")
41         CATEGORY_SUBTITLE_FORMAT = _("Preference of subtitle format")
42         CATEGORY_HANDLE_SERVICES = _("Selection of handled Services")
43
44         audio_order_choices = [("saved",_("saved")), ("default",_("default")), ("format",_("format")), ("language",_("Language")), ("Description",_("Description"))]
45         [AUDIO_ORDER_SAVED, AUDIO_ORDER_DEFAULT, AUDIO_ORDER_FORMAT, AUDIO_ORDER_LANGUAGE, AUDIO_ORDER_DESCRIPTION] = [x[0] for x in audio_order_choices]
46
47         subtitle_order_choices = [("saved",_("saved")), ("default",_("default")), ("forced",_("forced")), ("language",_("Language")), ("format",_("format"))]
48         [SUBTITLE_ORDER_SAVED, SUBTITLE_ORDER_DEFAULT, SUBTITLE_ORDER_FORCED, SUBTITLE_ORDER_LANGUAGE, SUBTITLE_ORDER_FORMAT] = [x[0] for x in subtitle_order_choices]
49
50         subtitle_enable_choices = [("saved",_("saved")), ("default",_("default")), ("forced",_("forced")), ("nofirstlanguage",_("if audio isn't first language")), ("notanylanguage",_("if no prefered audio language")), ("always",_("always"))]
51         [SUBTITLE_ENABLE_SAVED, SUBTITLE_ENABLE_DEFAULT, SUBTITLE_ENABLE_FORCED, SUBTITLE_ENABLE_NOTFIRSTLANG, SUBTITLE_ENABLE_NOTANYLANG, SUBTITLE_ENABLE_ALWAYS] = [x[0] for x in subtitle_enable_choices]
52
53         handle_services_choices = [("servicegst",_("Media Files")), ("servicedvb",_("TV Services")), ("servicedvd",_("DVD"))]
54         [SERVICE_GST, SERVICE_DVB, SERVICE_DVD] = [x[0] for x in handle_services_choices]
55
56         services_dict = { eServiceReference.idDVB: SERVICE_DVB, 0x1001: SERVICE_GST, 0x1111: SERVICE_DVD }
57
58         def __init__(self):
59                 from Screens.AudioSelection import AUDIO_FORMATS, SUB_FORMATS, GST_SUB_FORMATS
60
61                 self.iso639 = Autoselect639Language()
62                 (self.language_dict, self.language_choices, self.language_defaults) = self.iso639.getTranslatedChoicesDictAndSortedListAndDefaults()
63                 self.language_comparison_dict = {}
64
65                 self.audio_format_choices = []
66                 self.sub_format_choices = []
67
68                 self.audio_format_dict = {}
69                 for idx, (short, text, rank) in sorted(AUDIO_FORMATS.items(), key=lambda x: x[1][2]):
70                         if rank > 0:
71                                 self.audio_format_choices.append((short, text))
72                                 self.audio_format_dict[idx] = short
73
74                 self.sub_format_dict = {}
75                 self.gstsub_format_dict= {}
76                 for idx, (short, text, rank) in sorted(SUB_FORMATS.items(), key=lambda x: x[1][2]):
77                         if rank > 0:
78                                 self.sub_format_choices.append((short, text))
79                                 self.sub_format_dict[idx] = short
80                 for idx, (short, text, rank) in sorted(GST_SUB_FORMATS.items(), key=lambda x: x[1][2]):
81                         if rank > 0:
82                                 self.sub_format_choices.append((short, short))
83                                 self.gstsub_format_dict[idx] = short
84
85                 config.plugins.TrackAutoselect = ConfigSubsection()
86
87                 config.plugins.TrackAutoselect.audio_autoselect_enable = ConfigYesNo(default=True)
88                 config.plugins.TrackAutoselect.audio_parameter_order = ConfigSet(self.audio_order_choices, default=[x[0] for x in self.audio_order_choices[:-1]], resort=False)
89                 config.plugins.TrackAutoselect.audio_language_preference = ConfigSet(self.language_choices, default=self.language_defaults, resort=False)
90                 config.plugins.TrackAutoselect.audio_format_preference = ConfigSet(self.audio_format_choices, default=[x[0] for x in self.audio_format_choices], resort=False)
91                 #config.plugins.TrackAutoselect.audio_description_preference = ConfigSet(self.audio_format_choices, default=[x[0] for x in self.audio_description_choices], resort=False)
92
93                 config.plugins.TrackAutoselect.subtitle_autoselect_enable = ConfigYesNo(default=True)
94                 config.plugins.TrackAutoselect.subtitle_enable_conditions = ConfigSet(self.subtitle_enable_choices, default=[x[0] for x in self.subtitle_enable_choices[:3]], resort=False)
95                 config.plugins.TrackAutoselect.subtitle_parameter_order = ConfigSet(self.subtitle_order_choices, default=[x[0] for x in self.subtitle_order_choices], resort=False)
96                 config.plugins.TrackAutoselect.subtitle_language_preference = ConfigSet(self.language_choices, default=self.language_defaults, resort=False)
97                 config.plugins.TrackAutoselect.subtitle_format_preference = ConfigSet(self.sub_format_choices, default=[x[0] for x in self.sub_format_choices], resort=False)
98
99                 config.plugins.TrackAutoselect.handle_services = ConfigSet(self.handle_services_choices, default=[x[0] for x in self.handle_services_choices], resort=False)
100                 config.plugins.TrackAutoselect.same_languages = ConfigYesNo(default=False)
101                 config.plugins.TrackAutoselect.wait_for_eit = ConfigInteger(default=1500, limits = (0, 99999))
102
103                 config.plugins.TrackAutoselect.audio_language_preference.addNotifier(self.updateLanguageComparisonDict, initial_call=True)
104                 config.plugins.TrackAutoselect.subtitle_language_preference.addNotifier(self.updateLanguageComparisonDict, initial_call=True)
105
106         def updateLanguageComparisonDict(self, config_element):
107                 id_list = config_element.getValue()
108                 for shortid in id_list:
109                         name = self.iso639.name_by_shortid[shortid]
110                         for identry in self.iso639.idlist_by_name[name]:
111                                 self.language_comparison_dict[identry] = shortid
112
113         def getAvailableChoicesAndPreferenceTuplelist(self, category, preference_list):
114                 #print "[TrackAutoselectConfig] getAvailableChoices for category", category, "minus preference_list", preference_list
115                 preference_tuplelist = []
116                 choicelist = []
117                 choicedict = {}
118                 if category in [self.CATEGORY_AUDIO_LANGUAGE, self.CATEGORY_SUBTITLE_LANGUAGE]:
119                         choicelist = self.language_choices
120                         choicedict = self.language_dict.copy()
121                 else:
122                         if category == self.CATEGORY_AUDIO_FORMAT:
123                                 choicelist = self.audio_format_choices
124                         elif category == self.CATEGORY_SUBTITLE_FORMAT:
125                                 choicelist = self.sub_format_choices
126                         elif category == self.CATEGORY_AUDIO_ORDER:
127                                 choicelist = self.audio_order_choices
128                         elif category == self.CATEGORY_SUBTITLE_ORDER:
129                                 choicelist = self.subtitle_order_choices
130                         elif category == self.CATEGORY_SUBTITLE_ENABLE:
131                                 choicelist = self.subtitle_enable_choices
132                         elif category == self.CATEGORY_HANDLE_SERVICES:
133                                 choicelist = self.handle_services_choices
134                         choicedict = dict(choicelist)
135
136                 choicelist = [tuple(reversed(x)) for x in choicelist]
137
138                 for key in preference_list:
139                         try:
140                                 value = choicedict[key]
141                                 preference_tuplelist.append((key, value))
142                                 choicelist.remove((value, key))
143                         except KeyError as e:
144                                 print "[TrackAutoselectConfig] getAvailableChoicesAndPreferenceTuplelist Error in settings! couldn't find key '%s'" % e
145
146                 return (choicelist, preference_tuplelist)
147