594b4534a3753e54a78c28f27d1aec863dacd9a2
[enigma2.git] / lib / python / Plugins / Extensions / MediaPlayer / settings.py
1 from Screens.Screen import Screen
2 from Screens.HelpMenu import HelpableScreen
3 from Components.Label import Label
4 from Components.FileList import FileList
5 from Components.MediaPlayer import PlayList
6 from Components.config import config, getConfigListEntry, ConfigSubsection, configfile, ConfigText, ConfigYesNo
7 from Components.ConfigList import ConfigListScreen
8 from Components.ActionMap import ActionMap
9
10 class ConfigDirectory(ConfigText):
11         def __init__(self, default="", visible_width=60):
12                 ConfigText.__init__(self, default, fixed_size = True, visible_width = visible_width)
13         def handleKey(self, key):
14                 pass
15         def getValue(self):
16                 if self.text == "":
17                         return None
18                 else:
19                         return ConfigText.getValue(self)
20         def setValue(self, val):
21                 if val == None:
22                         val = ""
23                 ConfigText.setValue(self, val)
24         def getMulti(self, selected):
25                 if self.text == "":
26                         return ("mtext"[1-selected:], _("List of Storage Devices"), range(0))
27                 else:
28                         return ConfigText.getMulti(self, selected)
29                 
30 config.mediaplayer = ConfigSubsection()
31 config.mediaplayer.repeat = ConfigYesNo(default=False)
32 config.mediaplayer.savePlaylistOnExit = ConfigYesNo(default=True)
33 config.mediaplayer.saveDirOnExit = ConfigYesNo(default=False)
34 config.mediaplayer.defaultDir = ConfigDirectory()
35
36 class DirectoryBrowser(Screen, HelpableScreen):
37         skin = """
38                 <screen name="DirectoryBrowser" position="80,150" size="400,376" title="Directory browser" >
39                         <ePixmap pixmap="skin_default/buttons/red.png" position="10,0" size="140,40" alphatest="on" />
40                         <ePixmap pixmap="skin_default/buttons/green.png" position="250,0" size="140,40" alphatest="on" />               <widget source="key_red" render="Label" position="10,0" zPosition="1" size="140,40" font="Regular;20" halign="center" valign="center" backgroundColor="#9f1313" transparent="1" />
41                         <widget source="key_green" render="Label" position="250,0" zPosition="1" size="140,40" font="Regular;20" halign="center" valign="center" backgroundColor="#1f771f" transparent="1" />
42                         <widget name="filelist" position="0,44" size="400,332" scrollbarMode="showOnDemand" />
43                 </screen>"""
44         def __init__(self, session, currDir):
45                 from Components.Sources.StaticText import StaticText
46                 Screen.__init__(self, session)
47                 HelpableScreen.__init__(self)
48                 
49                 self["key_red"] = StaticText(_("Cancel"))
50                 self["key_green"] = StaticText(_("Apply"))
51
52                 self.filelist = FileList(currDir, matchingPattern="")
53                 self["filelist"] = self.filelist
54
55                 self["FilelistActions"] = ActionMap(["SetupActions", "ColorActions"],
56                         {
57                                 "green": self.apply,
58                                 "red": self.exit,
59                                 "ok": self.ok,
60                                 "cancel": self.exit
61                         })
62
63         def ok(self):
64                 if self.filelist.canDescent():
65                         self.filelist.descent()
66
67         def apply(self):
68                 if self.filelist.canDescent() and len(self["filelist"].getFilename()) > len(self["filelist"].getCurrentDirectory()):
69                         self.filelist.descent()
70                 self.close(self["filelist"].getCurrentDirectory())
71
72         def exit(self):
73                 self.close(False)
74
75 class MediaPlayerSettings(Screen,ConfigListScreen):
76         skin = """
77                 <screen name="MediaPlayerSettings" position="160,150" size="400,200" title="Edit settings">
78                         <ePixmap pixmap="skin_default/buttons/red.png" position="10,0" size="140,40" alphatest="on" />
79                         <ePixmap pixmap="skin_default/buttons/green.png" position="250,0" size="140,40" alphatest="on" />
80                         <widget source="key_red" render="Label" position="10,0" zPosition="1" size="140,40" font="Regular;20" halign="center" valign="center" backgroundColor="#9f1313" transparent="1" />
81                         <widget source="key_green" render="Label" position="250,0" zPosition="1" size="140,40" font="Regular;20" halign="center" valign="center" backgroundColor="#1f771f" transparent="1" />
82                         <widget name="config" position="10,44" size="380,146" />
83                 </screen>"""
84
85         def __init__(self, session, parent):
86                 from Components.Sources.StaticText import StaticText
87                 Screen.__init__(self, session)
88                 self["key_red"] = StaticText(_("Cancel"))
89                 self["key_green"] = StaticText(_("Save"))
90
91                 ConfigListScreen.__init__(self, [])
92                 self.parent = parent
93                 self.initConfigList()
94                 config.mediaplayer.saveDirOnExit.addNotifier(self.initConfigList)
95
96                 self["setupActions"] = ActionMap(["SetupActions", "ColorActions"],
97                 {
98                     "green": self.save,
99                     "red": self.cancel,
100                     "cancel": self.cancel,
101                     "ok": self.ok,
102                 }, -2)
103
104         def initConfigList(self, element=None):
105                 print "[initConfigList]", element
106                 try:
107                         self.list = []
108                         self.list.append(getConfigListEntry(_("repeat playlist"), config.mediaplayer.repeat))
109                         self.list.append(getConfigListEntry(_("save playlist on exit"), config.mediaplayer.savePlaylistOnExit))
110                         self.list.append(getConfigListEntry(_("save last directory on exit"), config.mediaplayer.saveDirOnExit))
111                         if not config.mediaplayer.saveDirOnExit.getValue():
112                                 self.list.append(getConfigListEntry(_("start directory"), config.mediaplayer.defaultDir))
113                         self["config"].setList(self.list)
114                 except KeyError:
115                         print "keyError"
116
117         def changedConfigList(self):
118                 self.initConfigList()
119
120         def ok(self):
121                 if self["config"].getCurrent()[1] == config.mediaplayer.defaultDir:
122                         self.session.openWithCallback(self.DirectoryBrowserClosed, DirectoryBrowser, self.parent.filelist.getCurrentDirectory())
123
124         def DirectoryBrowserClosed(self, path):
125                 print "PathBrowserClosed:" + str(path)
126                 if path != False:
127                         config.mediaplayer.defaultDir.setValue(path)
128
129         def save(self):
130                 for x in self["config"].list:
131                         x[1].save()
132                 self.close()
133
134         def cancel(self):
135                 self.close()
136