PEP8: Fix whitespace
[openblackhole/openblackhole-enigma2.git] / lib / python / Plugins / Extensions / DVDBurn / ProjectSettings.py
1 from Screens.Screen import Screen
2 from Screens.ChoiceBox import ChoiceBox
3 from Screens.InputBox import InputBox
4 from Screens.MessageBox import MessageBox
5 from Screens.HelpMenu import HelpableScreen
6 from Components.ActionMap import HelpableActionMap, ActionMap
7 from Components.Sources.List import List
8 from Components.Sources.StaticText import StaticText
9 from Components.Sources.Progress import Progress
10 from Components.FileList import FileList
11 from Tools.Directories import fileExists, resolveFilename, SCOPE_PLUGINS, SCOPE_FONTS, SCOPE_HDD
12 from Components.config import config, getConfigListEntry
13 from Components.ConfigList import ConfigListScreen
14
15 class FileBrowser(Screen, HelpableScreen):
16
17         def __init__(self, session, scope, configRef):
18                 Screen.__init__(self, session)
19                 # for the skin: first try FileBrowser_DVDBurn, then FileBrowser, this allows individual skinning
20                 self.skinName = ["FileBrowser_DVDBurn", "FileBrowser" ]
21
22                 HelpableScreen.__init__(self)
23                 self.scope = scope
24                 pattern = ""
25                 self.configRef = configRef
26                 currDir = "/"
27                 if self.scope == "project":
28                         currDir = self.getDir()
29                         pattern = "(?i)^.*\.(ddvdp\.xml)"
30                 elif self.scope == "menutemplate":
31                         currDir = self.getDir()
32                         pattern = "(?i)^.*\.(ddvdm\.xml)"
33                 if self.scope == "menubg":
34                         currDir = self.getDir(configRef.getValue())
35                         pattern = "(?i)^.*\.(jpeg|jpg|jpe|png|bmp)"
36                 elif self.scope == "menuaudio":
37                         currDir = self.getDir(configRef.getValue())
38                         pattern = "(?i)^.*\.(mp2|m2a|ac3)"
39                 elif self.scope == "vmgm":
40                         currDir = self.getDir(configRef.getValue())
41                         pattern = "(?i)^.*\.(mpg|mpeg)"
42                 elif self.scope == "font_face":
43                         currDir = self.getDir(configRef.getValue(), resolveFilename(SCOPE_FONTS))
44                         pattern = "(?i)^.*\.(ttf)"
45                 elif self.scope == "isopath":
46                         currDir = configRef.getValue()
47                 elif self.scope == "image":
48                         currDir = resolveFilename(SCOPE_HDD)
49                         pattern = "(?i)^.*\.(iso)"
50
51                 self.filelist = FileList(currDir, matchingPattern=pattern)
52                 self["filelist"] = self.filelist
53
54                 self["FilelistActions"] = ActionMap(["SetupActions"],
55                         {
56                                 "save": self.ok,
57                                 "ok": self.ok,
58                                 "cancel": self.exit
59                         })
60                 self["key_red"] = StaticText(_("Cancel"))
61                 self["key_green"] = StaticText(_("OK"))
62                 self.onLayoutFinish.append(self.layoutFinished)
63
64         def layoutFinished(self):
65                 self.setTitle(_("DVD file browser"))
66
67         def getDir(self, currentVal=None, defaultDir=None):
68                 if currentVal:
69                         return (currentVal.rstrip("/").rsplit("/",1))[0]
70                 return defaultDir or (resolveFilename(SCOPE_PLUGINS)+"Extensions/DVDBurn/")
71
72         def ok(self):
73                 if self.filelist.canDescent():
74                         self.filelist.descent()
75                         if self.scope == "image":
76                                 path = self["filelist"].getCurrentDirectory() or ""
77                                 if fileExists(path+"VIDEO_TS"):
78                                         self.close(path,self.scope,self.configRef)
79                 else:
80                         ret = self["filelist"].getCurrentDirectory() + '/' + self["filelist"].getFilename()
81                         self.close(ret,self.scope,self.configRef)
82
83         def exit(self):
84                 if self.scope == "isopath":
85                         self.close(self["filelist"].getCurrentDirectory(),self.scope,self.configRef)
86                 self.close(None,False,None)
87
88 class ProjectSettings(Screen,ConfigListScreen):
89         skin = """
90                 <screen name="ProjectSettings" position="center,center" size="560,440" title="Collection settings" >
91                         <ePixmap pixmap="skin_default/buttons/red.png" position="0,0" size="140,40" alphatest="on" />
92                         <ePixmap pixmap="skin_default/buttons/green.png" position="140,0" size="140,40" alphatest="on" />
93                         <ePixmap pixmap="skin_default/buttons/yellow.png" position="280,0" size="140,40" alphatest="on" />
94                         <ePixmap pixmap="skin_default/buttons/blue.png" position="420,0" size="140,40" alphatest="on" />
95                         <widget source="key_red" render="Label" position="0,0" zPosition="1" size="140,40" font="Regular;19" halign="center" valign="center" backgroundColor="#9f1313" transparent="1" />
96                         <widget source="key_green" render="Label" position="140,0" zPosition="1" size="140,40" font="Regular;19" halign="center" valign="center" backgroundColor="#1f771f" transparent="1" />
97                         <widget source="key_yellow" render="Label" position="280,0" zPosition="1" size="140,40" font="Regular;19" halign="center" valign="center" backgroundColor="#a08500" transparent="1" />
98                         <widget source="key_blue" render="Label" position="420,0" zPosition="1" size="140,40" font="Regular;19" halign="center" valign="center" backgroundColor="#18188b" transparent="1" />
99                         <widget name="config" position="5,50" size="550,276" scrollbarMode="showOnDemand" />
100                         <ePixmap pixmap="skin_default/div-h.png" position="0,350" zPosition="1" size="560,2" />
101                         <widget source="info" render="Label" position="10,360" size="550,80" font="Regular;18" halign="center" valign="center" />
102                 </screen>"""
103
104         def __init__(self, session, project = None):
105                 Screen.__init__(self, session)
106                 self.project = project
107
108                 self["key_red"] = StaticText(_("Cancel"))
109                 self["key_green"] = StaticText(_("OK"))
110                 self["key_yellow"] = StaticText(_("Load"))
111                 if config.usage.setup_level.index >= 2: # expert+
112                         self["key_blue"] = StaticText(_("Save"))
113                 else:
114                         self["key_blue"] = StaticText()
115
116                 if config.usage.setup_level.index >= 2: # expert+
117                         infotext = _("Available format variables") + ":\n$i=" + _("Track") + ", $t=" + _("Title") + ", $d=" + _("Description") + ", $l=" + _("length") + ", $c=" + _("chapters") + ",\n" + _("Record") + " $T=" + _("Begin time") + ", $Y=" + _("Year") + ", $M=" + _("month") + ", $D=" + _("day") + ",\n$A=" + _("audio tracks") + ", $C=" + _("Channel") + ", $f=" + _("filename")
118                 else:
119                         infotext = ""
120                 self["info"] = StaticText(infotext)
121
122                 self.keydict = {}
123                 self.settings = project.settings
124                 ConfigListScreen.__init__(self, [])
125                 self.initConfigList()
126
127                 self["setupActions"] = ActionMap(["SetupActions", "ColorActions"],
128                 {
129                     "green": self.exit,
130                     "red": self.cancel,
131                     "blue": self.saveProject,
132                     "yellow": self.loadProject,
133                     "cancel": self.cancel,
134                     "ok": self.ok,
135                 }, -2)
136                 self.onLayoutFinish.append(self.layoutFinished)
137
138         def layoutFinished(self):
139                 self.setTitle(_("Collection settings"))
140
141         def changedConfigList(self):
142                 key = self.keydict[self["config"].getCurrent()[1]]
143                 if key == "authormode" or key == "output":
144                         self.initConfigList()
145
146         def initConfigList(self):
147                 authormode = self.settings.authormode.getValue()
148                 output = self.settings.output.getValue()
149                 self.list = []
150                 self.list.append(getConfigListEntry(_("Collection name"), self.settings.name))
151                 self.list.append(getConfigListEntry(_("Authoring mode"), self.settings.authormode))
152                 self.list.append(getConfigListEntry(_("Output"), self.settings.output))
153                 if output == "iso":
154                         self.list.append(getConfigListEntry(_("ISO path"), self.settings.isopath))
155                 if authormode.startswith("menu"):
156                         self.list.append(getConfigListEntry(_("Menu")+' '+_("template file"), self.settings.menutemplate))
157                         if config.usage.setup_level.index >= 2: # expert+
158                                 self.list.append(getConfigListEntry(_("Menu")+' '+_("Title"), self.project.menutemplate.settings.titleformat))
159                                 self.list.append(getConfigListEntry(_("Menu")+' '+_("Subtitles"), self.project.menutemplate.settings.subtitleformat))
160                                 self.list.append(getConfigListEntry(_("Menu")+' '+_("background image"), self.project.menutemplate.settings.menubg))
161                                 self.list.append(getConfigListEntry(_("Menu")+' '+_("Language selection"), self.project.menutemplate.settings.menulang))
162                         #self.list.append(getConfigListEntry(_("Menu")+' '+_("headline")+' '+_("color"), self.settings.color_headline))
163                         #self.list.append(getConfigListEntry(_("Menu")+' '+_("text")+' '+_("color"), self.settings.color_button))
164                         #self.list.append(getConfigListEntry(_("Menu")+' '+_("highlighted button")+' '+_("color"), self.settings.color_highlight))
165                         #self.list.append(getConfigListEntry(_("Menu")+' '+_("font face"), self.settings.font_face))
166                         #self.list.append(getConfigListEntry(_("Font size")+' ('+_("headline")+', '+_("Title")+', '+_("Subtitles")+')', self.settings.font_size))
167                         #self.list.append(getConfigListEntry(_("Menu")+' '+_("spaces (top, between rows, left)"), self.settings.space))
168                         #self.list.append(getConfigListEntry(_("Menu")+' '+_("Audio"), self.settings.menuaudio))
169                 if config.usage.setup_level.index >= 2: # expert+
170                         if authormode != "data_ts":
171                                 self.list.append(getConfigListEntry(_("Titleset mode"), self.settings.titlesetmode))
172                                 if self.settings.titlesetmode.getValue() == "single" or authormode == "just_linked":
173                                         self.list.append(getConfigListEntry(_("VMGM (intro trailer)"), self.settings.vmgm))
174                         else:
175                                 self.list.append(getConfigListEntry(_("DVD data format"), self.settings.dataformat))
176
177                 self["config"].setList(self.list)
178                 self.keydict = {}
179                 for key, val in self.settings.dict().iteritems():
180                         self.keydict[val] = key
181                 for key, val in self.project.menutemplate.settings.dict().iteritems():
182                         self.keydict[val] = key
183
184         def keyLeft(self):
185                 ConfigListScreen.keyLeft(self)
186                 key = self.keydict[self["config"].getCurrent()[1]]
187                 if key == "authormode" or key == "output" or key=="titlesetmode":
188                         self.initConfigList()
189
190         def keyRight(self):
191                 ConfigListScreen.keyRight(self)
192                 key = self.keydict[self["config"].getCurrent()[1]]
193                 if key == "authormode" or key == "output" or key=="titlesetmode":
194                         self.initConfigList()
195
196         def exit(self):
197                 self.applySettings()
198                 self.close(True)
199
200         def applySettings(self):
201                 for x in self["config"].list:
202                         x[1].save()
203
204         def ok(self):
205                 key = self.keydict[self["config"].getCurrent()[1]]
206                 from DVDProject import ConfigFilename
207                 if type(self["config"].getCurrent()[1]) == ConfigFilename:
208                         self.session.openWithCallback(self.FileBrowserClosed, FileBrowser, key, self["config"].getCurrent()[1])
209
210         def cancel(self):
211                 self.close(False)
212
213         def loadProject(self):
214                 self.session.openWithCallback(self.FileBrowserClosed, FileBrowser, "project", self.settings)
215
216         def saveProject(self):
217                 if config.usage.setup_level.index >= 2: # expert+
218                         self.applySettings()
219                         ret = self.project.saveProject(resolveFilename(SCOPE_PLUGINS)+"Extensions/DVDBurn/")
220                         if ret.startswith:
221                                 text = _("Save")+' '+_('OK')+':\n'+ret
222                                 self.session.open(MessageBox,text,type = MessageBox.TYPE_INFO)
223                         else:
224                                 text = _("Save")+' '+_('Error')
225                                 self.session.open(MessageBox,text,type = MessageBox.TYPE_ERROR)
226
227         def FileBrowserClosed(self, path, scope, configRef):
228                 if scope == "menutemplate":
229                         if self.project.menutemplate.loadTemplate(path):
230                                 print "[ProjectSettings] menu template loaded"
231                                 configRef.setValue(path)
232                                 self.initConfigList()
233                         else:
234                                 self.session.open(MessageBox,self.project.error,MessageBox.TYPE_ERROR)
235                 elif scope == "project":
236                         self.path = path
237                         print "len(self.titles)", len(self.project.titles)
238                         if len(self.project.titles):
239                                 self.session.openWithCallback(self.askLoadCB, MessageBox,text = _("Your current collection will get lost!") + "\n" + _("Do you want to restore your settings?"), type = MessageBox.TYPE_YESNO)
240                         else:
241                                 self.askLoadCB(True)
242                 elif scope:
243                         configRef.setValue(path)
244                         self.initConfigList()
245
246         def askLoadCB(self, answer):
247                 if answer is not None and answer:
248                         if self.project.loadProject(self.path):
249                                 self.initConfigList()
250                         else:
251                                 self.session.open(MessageBox,self.project.error,MessageBox.TYPE_ERROR)