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