Merge branch 'master' of fraxinas@git.opendreambox.org:/git/enigma2
[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
3
4 class ConfigColor(ConfigSequence):
5         def __init__(self):
6                 ConfigSequence.__init__(self, seperator = "#", limits = [(0,255),(0,255),(0,255)])
7
8 class ConfigPixelvals(ConfigSequence):
9         def __init__(self):
10                 ConfigSequence.__init__(self, seperator = ",", limits = [(0,200),(0,200),(0,200)])
11
12 class ConfigPixelvals(ConfigSequence):
13         def __init__(self):
14                 ConfigSequence.__init__(self, seperator = ",", limits = [(0,200),(0,200),(0,200)])
15
16 class ConfigFilename(ConfigText):
17         def __init__(self):
18                 ConfigText.__init__(self, default = "", fixed_size = True, visible_width = False)
19
20         def getMulti(self, selected):
21                 filename = (self.text.rstrip("/").rsplit("/",1))[1].encode("utf-8")[:40] + " "
22                 if self.allmarked:
23                         mark = range(0, len(filename))
24                 else:
25                         mark = [filename]
26                 return ("mtext"[1-selected:], filename, mark)
27         
28 class DVDProject:
29         def __init__(self):
30                 self.titles = [ ]
31                 self.target = None
32                 self.settings = ConfigSubsection()
33                 self.settings.name = ConfigText(fixed_size = False, visible_width = 40)
34                 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)"))])
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.menubg = ConfigFilename()
39                 self.settings.menuaudio = ConfigFilename()
40                 self.settings.titleformat = ConfigText(fixed_size = False, visible_width = 40)
41                 self.settings.subtitleformat = ConfigText(fixed_size = False, visible_width = 40)
42                 self.settings.color_headline = ConfigColor()
43                 self.settings.color_highlight = ConfigColor()
44                 self.settings.color_button = ConfigColor()
45                 self.settings.font_face = ConfigFilename()
46                 self.settings.font_size = ConfigPixelvals()
47                 self.settings.space = ConfigPixelvals()
48                 self.settings.vmgm = ConfigFilename()
49                 self.settings.autochapter = ConfigInteger(default = 0, limits = (0, 99))
50                 self.filekeys = ["vmgm", "menubg", "menuaudio", "font_face", "isopath"]
51
52         def addService(self, service):
53                 import DVDTitle
54                 title = DVDTitle.DVDTitle()
55                 title.addService(service)
56                 self.titles.append(title)
57                 return title
58
59         def saveProject(self, path):
60                 import xml.dom.minidom
61                 from Tools.XMLTools import elementsWithTag, mergeText, stringToXML
62                 list = []
63                 list.append('<?xml version="1.0" encoding="utf-8" ?>\n')
64                 list.append('<DreamDVDBurnerProject>\n')
65                 list.append('\t<settings ')
66                 for key, val in self.settings.dict().iteritems():
67                         list.append( key + '="' + str(val.getValue()) + '" ' )
68                 list.append(' />\n')            
69                 list.append('\t<titles>\n')
70                 for title in self.titles:
71                         list.append('\t\t<path>')
72                         list.append(stringToXML(title.source.getPath()))
73                         list.append('</path>\n')
74                 list.append('\t</titles>\n')
75                 list.append('</DreamDVDBurnerProject>\n')
76
77                 name = self.settings.name.getValue()
78                 i = 0
79                 filename = path + name + ".ddvdp.xml"
80                 while fileExists(filename):
81                         i = i+1
82                         filename = path + name + str(i).zfill(3) + ".ddvdp.xml"
83                 try:    
84                         file = open(filename, "w")
85                         for x in list:
86                                 file.write(x)
87                         file.close()
88                 except:
89                         return False
90                 return filename
91
92         def loadProject(self, filename):
93                 import xml.dom.minidom
94                 try:
95                         if not fileExists(filename):
96                                 self.error = "xml file not found!"
97                                 raise AttributeError
98                         else:
99                                 self.error = ""
100                         file = open(filename, "r")
101                         data = file.read().decode("utf-8").replace('&',"&amp;").encode("ascii",'xmlcharrefreplace')
102                         file.close()
103                         projectfiledom = xml.dom.minidom.parseString(data)
104                         for project in projectfiledom.childNodes[0].childNodes:
105                           if project.nodeType == xml.dom.minidom.Element.nodeType:
106                             if project.tagName == 'settings':
107                                 i = 0
108                                 if project.attributes.length < 11:
109                                         self.error = "project attributes missing"
110                                         raise AttributeError                    
111                                 while i < project.attributes.length:
112                                         item = project.attributes.item(i)
113                                         key = item.name.encode("utf-8")
114                                         try:
115                                                 val = eval(item.nodeValue)
116                                         except (NameError, SyntaxError):
117                                                 val = item.nodeValue.encode("utf-8")
118                                         try:
119                                                 self.settings.dict()[key].setValue(val)
120                                         except (KeyError):
121                                                 self.error = "unknown attribute '%s'" % (key)
122                                                 raise AttributeError
123                                         i += 1
124                         for key in self.filekeys:
125                                 val = self.settings.dict()[key].getValue()
126                                 if not fileExists(val):
127                                         self.error += "\n%s '%s' not found" % (key, val)
128                         if len(self.error):
129                                 raise AttributeError
130                 except AttributeError:
131                         self.error += (" in project '%s'") % (filename)
132                         return False
133                 return True