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