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