#
# gPrime - A web-based genealogy program
#
# Copyright (C) 2004-2007  Donald N. Allingham
# Copyright (C) 2008,2011  Gary Burton
# Copyright (C) 2010       Jakim Friant
# Copyright (C) 2011-2012  Paul Franklin
# Copyright (C) 2012       Brian G. Matherly
#
# 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.
#

# Written by Alex Roitman

"""
Report option handling, including saving and parsing.
"""

#-------------------------------------------------------------------------
#
# Standard Python modules
#
#-------------------------------------------------------------------------
import os
import copy

#-------------------------------------------------------------------------
#
# SAX interface
#
#-------------------------------------------------------------------------
from xml.sax import make_parser, SAXParseException
from xml.sax.saxutils import escape

#------------------------------------------------------------------------
#
# Set up logging
#
#------------------------------------------------------------------------
import logging
LOG = logging.getLogger(".plug.report.options")

#-------------------------------------------------------------------------
#
# gramps modules
#
# (do not import anything from 'gui' as this is in 'gen')
#
#-------------------------------------------------------------------------
#from ...const import USER_HOME, REPORT_OPTIONS
from ...config import config
from ..docgen import PAPER_PORTRAIT
from .. import _options
from .. import MenuOptions
from gprime.utils.cast import get_type_converter

def escxml(word):
    return escape(word, {'"' : '&quot;'})

#-------------------------------------------------------------------------
#
# List of options for a single report
#
#-------------------------------------------------------------------------
class OptionList(_options.OptionList):
    """
    Implements a set of options to parse and store for a given report.
    """

    def __init__(self):
        _options.OptionList.__init__(self)
        self.style_name = None
        self.paper_metric = None
        self.paper_name = None
        self.orientation = None
        self.custom_paper_size = [29.7, 21.0]
        self.margins = [2.54, 2.54, 2.54, 2.54]
        self.format_name = None
        self.css_filename = None
        self.output = None

    def set_style_name(self, style_name):
        """
        Set the style name for the OptionList.

        :param style_name: name of the style to set.
        :type style_name: str
        """
        self.style_name = style_name

    def get_style_name(self):
        """
        Return the style name of the OptionList.

        :returns: string representing the style name
        :rtype: str
        """
        return self.style_name

    def set_paper_metric(self, paper_metric):
        """
        Set the paper metric for the OptionList.

        :param paper_metric: whether to use metric.
        :type paper_name: boolean
        """
        self.paper_metric = paper_metric

    def get_paper_metric(self):
        """
        Return the paper metric of the OptionList.

        :returns: returns whether to use metric
        :rtype: boolean
        """
        return self.paper_metric

    def set_paper_name(self, paper_name):
        """
        Set the paper name for the OptionList.

        :param paper_name: name of the paper to set.
        :type paper_name: str
        """
        self.paper_name = paper_name

    def get_paper_name(self):
        """
        Return the paper name of the OptionList.

        :returns: returns the paper name
        :rtype: str
        """
        return self.paper_name

    def set_orientation(self, orientation):
        """
        Set the orientation for the OptionList.

        :param orientation: orientation to set. Possible values are
                            PAPER_LANDSCAPE or PAPER_PORTRAIT
        :type orientation: int
        """
        self.orientation = orientation

    def get_orientation(self):
        """
        Return the orientation for the OptionList.

        :returns: returns the selected orientation. Valid values are
                  PAPER_LANDSCAPE or PAPER_PORTRAIT
        :rtype: int
        """
        return self.orientation

    def set_custom_paper_size(self, paper_size):
        """
        Set the custom paper size for the OptionList.

        :param paper_size: paper size to set in cm.
        :type paper_size: [float, float]
        """
        self.custom_paper_size = paper_size

    def get_custom_paper_size(self):
        """
        Return the custom paper size for the OptionList.

        :returns: returns the custom paper size in cm
        :rtype: [float, float]
        """
        return self.custom_paper_size

    def set_margins(self, margins):
        """
        Set the margins for the OptionList.

        :param margins: margins to set. Possible values are floats in cm
        :type margins: [float, float, float, float]
        """
        self.margins = copy.copy(margins)

    def get_margins(self):
        """
        Return the margins for the OptionList.

        :returns margins: returns the margins, floats in cm
        :rtype margins: [float, float, float, float]
        """
        return copy.copy(self.margins)

    def set_margin(self, pos, value):
        """
        Set a margin for the OptionList.

        :param pos: Position of margin [left, right, top, bottom]
        :type pos: int
        :param value: floating point in cm
        :type value: float
        """
        self.margins[pos] = value

    def get_margin(self, pos):
        """
        Return a margin for the OptionList.

        :param pos: Position of margin [left, right, top, bottom]
        :type pos: int
        :returns: float cm of margin
        :rtype: float
        """
        return self.margins[pos]

    def set_css_filename(self, css_filename):
        """
        Set the template name for the OptionList.

        :param template_name: name of the template to set.
        :type template_name: str
        """
        self.css_filename = css_filename

    def get_css_filename(self):
      
  """
        Return the template name of the OptionList.

        :returns: template name
        :rtype: str
        """
        return self.css_filename

    def set_fo
rmat_name(self, format_name):
        """
        Set the format name for the OptionList.

        :param format_name: name of the format to set.
        :type format_name: str
        """
        self.format_name = format_name

    def get_format_name(self):
        """
        Return the format name of the OptionList.

        :returns: returns the format name
        :rtype: str
        """
        return self.format_name

    def set_output(self, output):
        """
        Set the output for the OptionList.

        :param output: name of the output to set.
        :type output: str
        """
        self.output = output

    def get_output(self):
        """
        Return the output of the OptionList.

        :returns: returns the output name
        :rtype: str
        """
        return self.output

#-------------------------------------------------------------------------
#
# Collection of option lists
#
#-------------------------------------------------------------------------
class OptionListCollection(_options.OptionListCollection):
    """
    Implements a collection of option lists.
    """
    def __init__(self, filename):
        _options.OptionListCollection.__init__(s