9ee3bdb955e50281a8d0327638b405cfabbd6f37
[enigma2.git] / lib / python / Plugins / Extensions / DVDBurn / DVDProject.py
1 from Tools.Directories import fileExists
2 from Components.config import config, ConfigSubsection, ConfigInteger, ConfigYesNo, 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 DVDProject:
13         def __init__(self):
14                 self.titles = [ ]
15                 self.target = None
16                 self.settings = ConfigSubsection()
17                 self.settings.name = ConfigText(visible_width = 40)
18                 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")), ])
19                 self.settings.menubg = ConfigText(visible_width = 40)
20                 self.settings.menuaudio = ConfigText(visible_width = 40)
21                 self.settings.titleformat = ConfigText(visible_width = 40)
22                 self.settings.subtitleformat = ConfigText(visible_width = 40)
23                 self.settings.color_headline = ConfigColor()
24                 self.settings.color_highlight = ConfigColor()
25                 self.settings.color_button = ConfigColor()
26                 self.settings.font_face = ConfigText(visible_width = 40)
27                 self.settings.font_size = ConfigPixelvals()
28                 self.settings.space = ConfigPixelvals()
29                 self.settings.vmgm = ConfigText(visible_width = 40)
30
31         def addService(self, service):
32                 import DVDTitle
33                 title = DVDTitle.DVDTitle()
34                 title.addService(service)
35                 self.titles.append(title)
36                 return title
37
38         def saveProject(self, path):
39                 import xml.dom.minidom
40                 from Tools.XMLTools import elementsWithTag, mergeText, stringToXML
41                 list = []
42                 list.append('<?xml version="1.0" encoding="utf-8" ?>\n')
43                 list.append('<DreamDVDBurnerProject>\n')
44                 list.append('\t<settings ')
45                 for key, val in self.settings.dict().iteritems():
46                         list.append( key + '="' + str(val.getValue()) + '" ' )
47                 list.append(' />\n')            
48                 list.append('\t<titles>\n')
49                 for title in self.titles:
50                         list.append('\t\t<path>')
51                         list.append(stringToXML(title.source.getPath()))
52                         list.append('</path>\n')
53                 list.append('\t</titles>\n')
54                 list.append('</DreamDVDBurnerProject>\n')
55
56                 name = self.settings.name.getValue()
57                 i = 0
58                 filename = path + name + ".ddvdp.xml"
59                 while fileExists(filename):
60                         i = i+1
61                         filename = path + name + str(i).zfill(3) + ".ddvdp.xml"
62                 try:    
63                         file = open(filename, "w")
64                         for x in list:
65                                 file.write(x)
66                         file.close()
67                 except:
68                         return False
69                 return filename
70
71         def loadProject(self, filename):
72                 import xml.dom.minidom
73                 try:
74                         if not fileExists(filename):
75                                 self.error = "xml file not found!"
76                                 raise AttributeError
77                         else:
78                                 self.error = ""
79                         file = open(filename, "r")
80                         data = file.read().decode("utf-8").replace('&',"&amp;").encode("ascii",'xmlcharrefreplace')
81                         file.close()
82                         projectfiledom = xml.dom.minidom.parseString(data)
83                         for project in projectfiledom.childNodes[0].childNodes:
84                           if project.nodeType == xml.dom.minidom.Element.nodeType:
85                             if project.tagName == 'settings':
86                                 i = 0
87                                 if project.attributes.length < 11:
88                                         self.error = "project attributes missing"
89                                         raise AttributeError                    
90                                 while i < project.attributes.length:
91                                         item = project.attributes.item(i)
92                                         key = item.name.encode("utf-8")
93                                         try:
94                                                 val = eval(item.nodeValue)
95                                         except (NameError, SyntaxError):
96                                                 val = item.nodeValue.encode("utf-8")
97                                         try:
98                                                 self.settings.dict()[key].setValue(val)
99                                         except (KeyError):
100                                                 self.error = "unknown attribute '%s'" % (key)
101                                                 raise AttributeError
102                                         i += 1
103                         checkfiles = ["vmgm", "menubg", "menuaudio", "font_face"]
104                         for key in checkfiles:
105                                 val = self.settings.dict()[key].getValue()
106                                 if not fileExists(val):
107                                         self.error += "\n%s '%s' not found" % (key, val)
108                         if len(self.error):
109                                 raise AttributeError
110                 except AttributeError:
111                         self.error += (" in project '%s'") % (filename)
112                         return False
113                 return True