Merge remote branch 'remotes/origin/bug_597_crashlog_show_skinname'
[enigma2.git] / lib / python / Plugins / Extensions / DVDBurn / DVDProject.py
1 from Tools.Directories import fileExists
2 from Components.config import config, ConfigSubsection, ConfigInteger, ConfigText, ConfigSelection, getConfigListEntry, ConfigSequence, ConfigSubList
3 import DVDTitle
4 import xml.dom.minidom
5
6 class ConfigColor(ConfigSequence):
7         def __init__(self, default = [128,128,128]):
8                 ConfigSequence.__init__(self, seperator = "#", limits = [(0,255),(0,255),(0,255)], default = default)
9
10 class ConfigFilename(ConfigText):
11         def __init__(self):
12                 ConfigText.__init__(self, default = "", fixed_size = True, visible_width = False)
13
14         def getMulti(self, selected):
15                 if self.text == "":
16                         return ("mtext"[1-selected:], "", 0)
17                 cut_len = min(len(self.text),40)
18                 filename = (self.text.rstrip("/").rsplit("/",1))[1].encode("utf-8")[:cut_len] + " "
19                 if self.allmarked:
20                         mark = range(0, len(filename))
21                 else:
22                         mark = [filename]
23                 return ("mtext"[1-selected:], filename, mark)
24         
25 class DVDProject:
26         MAX_SL = 4480
27         MAX_DL = 8150
28         def __init__(self):
29                 self.titles = [ ]
30                 self.target = None
31                 self.settings = ConfigSubsection()
32                 self.settings.name = ConfigText(fixed_size = False, visible_width = 40)
33                 self.settings.authormode = ConfigSelection(choices = [("menu_linked", _("Linked titles with a DVD menu")), ("just_linked", _("Direct playback of linked titles without menu")), ("menu_seperate", _("Seperate titles with a main menu")), ("data_ts", _("Dreambox format data DVD (HDTV compatible)"))])
34                 self.settings.titlesetmode = ConfigSelection(choices = [("single", _("Simple titleset (compatibility for legacy players)")), ("multi", _("Complex (allows mixing audio tracks and aspects)"))], default="multi")
35                 self.settings.output = ConfigSelection(choices = [("iso", _("Create DVD-ISO")), ("dvd", _("Burn DVD"))])
36                 self.settings.isopath = ConfigText(fixed_size = False, visible_width = 40)
37                 self.settings.dataformat = ConfigSelection(choices = [("iso9660_1", ("ISO9660 Level 1")), ("iso9660_4", ("ISO9660 version 2")), ("udf", ("UDF"))])
38                 self.settings.menutemplate = ConfigFilename()
39                 self.settings.vmgm = ConfigFilename()
40                 self.filekeys = ["vmgm", "isopath", "menutemplate"]
41                 self.menutemplate = MenuTemplate()
42                 self.error = ""
43                 self.session = None
44
45         def addService(self, service):
46                 title = DVDTitle.DVDTitle(self)
47                 title.addService(service)
48                 self.titles.append(title)
49                 return title
50
51         def saveProject(self, path):
52                 from Tools.XMLTools import stringToXML
53                 list = []
54                 list.append('<?xml version="1.0" encoding="utf-8" ?>\n')
55                 list.append('<DreamDVDBurnerProject>\n')
56                 list.append('\t<settings ')
57                 for key, val in self.settings.dict().iteritems():
58                         list.append( key + '="' + str(val.getValue()) + '" ' )
59                 list.append('/>\n')
60                 list.append('\t<titles>\n')
61                 for title in self.titles:
62                         list.append('\t\t<title>\n')
63                         list.append('\t\t\t<path>')
64                         list.append(stringToXML(title.source.getPath()))
65                         list.append('</path>\n')
66                         list.append('\t\t\t<properties ')
67                         audiotracks = []
68                         for key, val in title.properties.dict().iteritems():
69                                 if type(val) is ConfigSubList:
70                                         audiotracks.append('\t\t\t<audiotracks>\n')
71                                         for audiotrack in val:
72                                                 audiotracks.append('\t\t\t\t<audiotrack ')
73                                                 for subkey, subval in audiotrack.dict().iteritems():
74                                                         audiotracks.append( subkey + '="' + str(subval.getValue()) + '" ' )
75                                                 audiotracks.append(' />\n')
76                                         audiotracks.append('\t\t\t</audiotracks>\n')
77                                 else:
78                                         list.append( key + '="' + str(val.getValue()) + '" ' )
79                         list.append('/>\n')
80                         for line in audiotracks:
81                                 list.append(line)
82                         list.append('\t\t</title>\n')
83                 list.append('\t</titles>\n')
84                 list.append('</DreamDVDBurnerProject>\n')
85
86                 name = self.settings.name.getValue()
87                 i = 0
88                 filename = path + name + ".ddvdp.xml"
89                 while fileExists(filename):
90                         i = i+1
91                         filename = path + name + str(i).zfill(3) + ".ddvdp.xml"
92                 try:    
93                         file = open(filename, "w")
94                         for x in list:
95                                 file.write(x)
96                         file.close()
97                 except:
98                         return False
99                 return filename
100
101         def load(self, filename):
102                 ret = self.loadProject(filename)
103                 if ret:
104                         ret = self.menutemplate.loadTemplate(self.settings.menutemplate.getValue())
105                         self.error += self.menutemplate.error
106                 return ret
107
108         def loadProject(self, filename):
109                 #try:
110                         if not fileExists(filename):
111                                 self.error = "xml file not found!"
112                                 #raise AttributeError
113                         file = open(filename, "r")
114                         data = file.read().decode("utf-8").replace('&',"&amp;").encode("ascii",'xmlcharrefreplace')
115                         file.close()
116                         projectfiledom = xml.dom.minidom.parseString(data)
117                         for node in projectfiledom.childNodes[0].childNodes:
118                           print "node:", node
119                           if node.nodeType == xml.dom.minidom.Element.nodeType:
120                             if node.tagName == 'settings':
121                                 self.xmlAttributesToConfig(node, self.settings)
122                             elif node.tagName == 'titles':
123                                 self.xmlGetTitleNodeRecursive(node)
124                                 
125                         for key in self.filekeys:
126                                 val = self.settings.dict()[key].getValue()
127                                 if not fileExists(val):
128                                         self.error += "\n%s '%s' not found" % (key, val)
129                 #except AttributeError:
130                         #print "loadProject AttributeError", self.error
131                         #self.error += (" in project '%s'") % (filename)
132                         #return False
133                         return True
134
135         def xmlAttributesToConfig(self, node, config):
136                 try:
137                         i = 0
138                         #if node.attributes.length < len(config.dict())-1:
139                                 #self.error = "project attributes missing"
140                                 #raise AttributeError
141                         while i < node.attributes.length:
142                                 item = node.attributes.item(i)
143                                 key = item.name.encode("utf-8")
144                                 try:
145                                         val = eval(item.nodeValue)
146                                 except (NameError, SyntaxError):
147                                         val = item.nodeValue.encode("utf-8")
148                                 try:
149                                         print "config[%s].setValue(%s)" % (key, val)
150                                         config.dict()[key].setValue(val)
151                                 except (KeyError):
152                                         self.error = "unknown attribute '%s'" % (key)
153                                         print "KeyError", self.error
154                                         raise AttributeError
155                                 i += 1
156                 except AttributeError:
157                         self.error += (" XML attribute error '%s'") % node.toxml()
158                         return False
159
160         def xmlGetTitleNodeRecursive(self, node, title_idx = -1):
161                 print "[xmlGetTitleNodeRecursive]", title_idx, node
162                 print node.childNodes
163                 for subnode in node.childNodes:
164                   print "xmlGetTitleNodeRecursive subnode:", subnode
165                   if subnode.nodeType == xml.dom.minidom.Element.nodeType:
166                     if subnode.tagName == 'title':
167                         title_idx += 1
168                         title = DVDTitle.DVDTitle(self)
169                         self.titles.append(title)
170                         self.xmlGetTitleNodeRecursive(subnode, title_idx)
171                     if subnode.tagName == 'path':
172                         print "path:", subnode.firstChild.data
173                         filename = subnode.firstChild.data
174                         self.titles[title_idx].addFile(filename.encode("utf-8"))
175                     if subnode.tagName == 'properties':
176                         self.xmlAttributesToConfig(node, self.titles[title_idx].properties)
177                     if subnode.tagName == 'audiotracks':
178                         self.xmlGetTitleNodeRecursive(subnode, title_idx)
179                     if subnode.tagName == 'audiotrack':
180                         print "audiotrack...", subnode.toxml()
181
182         def getSize(self):
183                 totalsize = 0
184                 for title in self.titles:
185                         totalsize += title.estimatedDiskspace
186                 return totalsize
187
188         size = property(getSize)
189
190 class MenuTemplate(DVDProject):
191         def __init__(self):
192                 self.settings = ConfigSubsection()
193                 self.settings.titleformat = ConfigText(fixed_size = False, visible_width = 40)
194                 self.settings.subtitleformat = ConfigText(fixed_size = False, visible_width = 40)
195                 self.settings.menubg = ConfigFilename()
196                 self.settings.menuaudio = ConfigFilename()
197                 self.settings.dimensions = ConfigSequence(seperator = ',', default = [576,720], limits = [(352,720),(480,576)])
198                 self.settings.rows = ConfigInteger(default = 4, limits = (1, 10))
199                 self.settings.cols = ConfigInteger(default = 1, limits = (1, 4))
200                 self.settings.color_headline = ConfigColor()
201                 self.settings.color_headline = ConfigColor()
202                 self.settings.color_highlight = ConfigColor()
203                 self.settings.color_button = ConfigColor()
204                 self.settings.fontface_headline = ConfigFilename()
205                 self.settings.fontface_title = ConfigFilename()
206                 self.settings.fontface_subtitle = ConfigFilename()
207                 self.settings.fontsize_headline = ConfigInteger(default = 46, limits = (0, 199))
208                 self.settings.fontsize_title = ConfigInteger(default = 24, limits = (0, 199))
209                 self.settings.fontsize_subtitle = ConfigInteger(default = 14, limits = (0, 199))
210                 self.settings.margin_top = ConfigInteger(default = 120, limits = (0, 500))
211                 self.settings.margin_bottom = ConfigInteger(default = 40, limits = (0, 500))
212                 self.settings.margin_left = ConfigInteger(default = 56, limits = (0, 500))
213                 self.settings.margin_right = ConfigInteger(default = 56, limits = (0, 500))
214                 self.settings.space_rows = ConfigInteger(default = 32, limits = (0, 500))
215                 self.settings.space_cols = ConfigInteger(default = 24, limits = (0, 500))
216                 self.settings.prev_page_text = ConfigText(default = "<<<", fixed_size = False)
217                 self.settings.next_page_text = ConfigText(default = ">>>", fixed_size = False)
218                 self.settings.offset_headline = ConfigSequence(seperator = ',', default = [0,0], limits = [(-1,500),(-1,500)])
219                 self.settings.offset_title = ConfigSequence(seperator = ',', default = [0,0], limits = [(-1,500),(-1,500)])
220                 self.settings.offset_subtitle = ConfigSequence(seperator = ',', default = [20,0], limits = [(-1,500),(-1,500)])
221                 self.settings.offset_thumb = ConfigSequence(seperator = ',', default = [40,0], limits = [(-1,500),(-1,500)])
222                 self.settings.thumb_size = ConfigSequence(seperator = ',', default = [200,158], limits = [(0,576),(-1,720)])
223                 self.settings.thumb_border = ConfigInteger(default = 2, limits = (0, 20))
224                 self.filekeys = ["menubg", "menuaudio", "fontface_headline", "fontface_title", "fontface_subtitle"]
225                 from TitleProperties import languageChoices
226                 self.settings.menulang = ConfigSelection(choices = languageChoices.choices, default=languageChoices.choices[1][0])
227                 self.error = ""
228
229         def loadTemplate(self, filename):
230                 ret = DVDProject.loadProject(self, filename)
231                 DVDProject.error = self.error
232                 return ret