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