# vPresent is an immersive presentation creation and display application.
# Copyright (C) 2007-2008 by Infiscape Corporation
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 2 of the License, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

from PyQt4 import QtCore, QtXml

import xml
import gui.util.doctype as doctype
import gui.util.qthelpers as qthelpers


class Reader(xml.Reader):
   def __init__(self, fileName, builder):
      xml.Reader.__init__(self, builder)
      self.fileName = fileName

   def construct(self, workDir):
      xml_file = QtCore.QFile(self.fileName)

      if not xml_file.open(QtCore.QIODevice.ReadWrite):
         raise IOError(qthelpers.getFileErrorMesage(xml_file.error()))

      self.document = QtXml.QDomDocument()
      parsed, err_msg, err_line, err_col = self.document.setContent(xml_file,
                                                                    True)

      if not parsed:
         # TODO: Make this more specific.
         raise Exception("%s at line %d, column %d" % (err_msg, err_line,
                                                       err_col))

      root = self.document.documentElement()

      content_node  = root.firstChildElement('document-content')
      meta_node     = root.firstChildElement('document-meta')
      settings_node = root.firstChildElement('document-settings')
      styles_node   = root.firstChildElement('document-styles')

      xml.Reader.construct(self, content_node, meta_node, styles_node,
                              settings_node)

class Writer(xml.Writer):
   def __init__(self, fileName):
      xml.Writer.__init__(self)
      self.fileName = fileName

   def write(self, document, junk):
      xml_document = xml.createXMLDocument()

      content_root = xml.createContentRoot(xml_document)
      xml_document.appendChild(content_root)

      meta_root = xml.createMetaRoot(xml_document,
                                        document.getContentType())
      xml_document.appendChild(meta_root)

      settings_root = xml.createSettingsRoot(xml_document,
                                                document.getContentType())
      xml_document.appendChild(settings_root)

      styles_root = xml.createStylesRoot(xml_document)
      xml_document.appendChild(styles_root)

      xml.Writer.write(self, document, xml_document, content_root,
                          xml_document, meta_root, xml_document,
                          settings_root, xml_document, styles_root)

      self._cleanChildren(xml_document, content_root)
      self._cleanChildren(xml_document, meta_root)
      self._cleanChildren(xml_document, settings_root)
      self._cleanChildren(xml_document, styles_root)

      doc_str = xml_document.toString(3)
      f = open(self.fileName, 'w')
      f.write(content_doc_str)
      f.close()

class ReaderWriter:
   sFileType = None

   def getFileTypesByExt(self):
      return self.sFileType.getExtensions()

   def getDocumentType():
      return 'text/xml'

   getDocumentType = staticmethod(getDocumentType)

   def init(env):
      dt = \
         doctype.DocType('XML files',
                         {'xml' : ReaderWriter.getDocumentType()},
                         Reader, Writer)
      env.registerDocType(dt)
      ReaderWriter.sFileType = dt

   init = staticmethod(init)
