#
# This file is part of the LibreOffice project.
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# This file incorporates work covered by the following license notice:
#
#   Licensed to the Apache Software Foundation (ASF) under one or more
#   contributor license agreements. See the NOTICE file distributed
#   with this work for additional information regarding copyright
#   ownership. The ASF licenses this file to you under the Apache
#   License, Version 2.0 (the "License"); you may not use this file
#   except in compliance with the License. You may obtain a copy of
#   the License at http://www.apache.org/licenses/LICENSE-2.0 .
#
from ..text.TextDocument import TextDocument, traceback, \
    TextFieldHandler, Configuration
from ..text.TextSectionHandler import TextSectionHandler
from ..common.PropertyNames import PropertyNames

from com.sun.star.text.ControlCharacter import PARAGRAPH_BREAK
from com.sun.star.style.ParagraphAdjust import CENTER
from com.sun.star.text.PageNumberType import CURRENT
from com.sun.star.style.NumberingType import ARABIC

class FaxDocument(TextDocument):

    def __init__(self, xMSF, listener):
        super(FaxDocument,self).__init__(xMSF, listener, None,
            "WIZARD_LIVE_PREVIEW")
        self.keepLogoFrame = True
        self.keepTypeFrame = True

    def switchElement(self, sElement, bState):
        try:
            mySectionHandler = TextSectionHandler(self.xMSF,
                self.xTextDocument)
            oSection = \
                mySectionHandler.xTextDocument.TextSections.getByName(sElement)
            oSection.IsVisible = bState
        except Exception:
            traceback.print_exc()

    def updateDateFields(self):
        FH = TextFieldHandler(
            self.xTextDocument, self.xTextDocument)
        FH.updateDateFields()

    def switchFooter(self, sPageStyle, bState, bPageNumber, sText):
        if self.xTextDocument is not None:
            try:
                self.xTextDocument.lockControllers()
                xPageStyleCollection = \
                    self.xTextDocument.StyleFamilies.getByName("PageStyles")
                xPageStyle = xPageStyleCollection.getByName(sPageStyle)

                if bState:
                    xPageStyle.setPropertyValue("FooterIsOn", True)
                    xFooterText = xPageStyle.FooterText
                    xFooterText.String = sText

                    if bPageNumber:
                        #Adding the Page Number
                        myCursor = xFooterText.Text.createTextCursor()
                        myCursor.gotoEnd(False)
                        xFooterText.insertControlCharacter(myCursor,
                            PARAGRAPH_BREAK, False)
                        myCursor.setPropertyValue("ParaAdjust", CENTER )

                        xPageNumberField = \
                            self.xTextDocument.createInstance(
                                "com.sun.star.text.TextField.PageNumber")
                        xPageNumberField.setPropertyValue("SubType", CURRENT)
                        xPageNumberField.NumberingType = ARABIC
                        xFooterText.insertTextContent(xFooterText.End,
                            xPageNumberField, False)
                else:
                    xPageStyle.FooterIsOn = False

                self.xTextDocument.unlockControllers()
            except Exception:
                self.xTextDocument.lockControllers()
                traceback.print_exc()

    def hasElement(self, sElement):
        if self.xTextDocument is not None:
            mySectionHandler = TextSectionHandler(self.xMSF,
                self.xTextDocument)
            return mySectionHandler.hasTextSectionByName(sElement)
        else:
            return False

    def switchUserField(self, sFieldName, sNewContent, bState):
        myFieldHandler = TextFieldHandler( self.xMSF, self.xTextDocument)
        if bState:
            myFieldHandler.changeUserFieldContent(sFieldName, sNewContent)
        else:
            myFieldHandler.changeUserFieldContent(sFieldName, "")

    def fillSenderWithUserData(self):
        try:
            myFieldHandler = TextFieldHandler(self.xTextDocument,
                self.xTextDocument)
            oUserDataAccess = Configuration.getConfigurationRoot(
                self.xMSF, "org.openoffice.UserProfile/Data", False)
            myFieldHandler.changeUserFieldContent("Company",
                oUserDataAccess.hasByName("o"))
            myFieldHandler.changeUserFieldContent("Street",
                oUserDataAccess.hasByName("street"))
            myFieldHandler.changeUserFieldContent("PostCode",
                oUserDataAccess.hasByName("postalcode"))
            myFieldHandler.changeUserFieldContent(
                PropertyNames.PROPERTY_STATE, oUserDataAccess.hasByName("st"))
            myFieldHandler.changeUserFieldContent("City",
                oUserDataAccess.hasByName("l"))
            myFieldHandler.changeUserFieldContent("Fax",
                oUserDataAccess.hasByName("facsimiletelephonenumber"))
        except Exception:
            traceback.print_exc()

    def killEmptyUserFields(self):
        myFieldHandler = TextFieldHandler(
            self.xMSF, self.xTextDocument)
        myFieldHandler.removeUserFieldByContent()

    def killEmptyFrames(self):
        try:
            if not self.keepLogoFrame:
                xTF = self.getFrameByName("Company Logo",
                self.xTextDocument)
                if xTF is not None:
                    xTF.dispose()

            if not self.keepTypeFrame:
                xTF = self.getFrameByName("Communication Type",
                self.xTextDocument)
                if xTF is not None:
                    xTF.dispose()

        except Exception:
            traceback.print_exc()

@@ -4572,18 +4572,8 @@
 ent(
-%22Company%22,
 %0A   
@@ -4576,32 +4576,43 @@
 %0A               
+ %22Company%22,
  oUserDataAccess
@@ -4604,35 +4604,35 @@
 oUserDataAccess.
-has
+get
 ByName(%22o%22))%0A   
@@ -4682,17 +4682,8 @@
 ent(
-%22Street%22,
 %0A   
@@ -4686,32 +4686,42 @@
 %0A               
+ %22Street%22,
  oUserDataAccess
@@ -4713,35 +4713,35 @@
 oUserDataAccess.
-has
+get
 ByName(%22street%22)
@@ -4796,19 +4796,8 @@
 ent(
-%22PostCode%22,
 %0A   
@@ -4800,32 +4800,44 @@
 %0A               
+ %22PostCode%22,
  oUserDataAccess
@@ -4829,35 +4829,35 @@
 oUserDataAccess.
-has
+get
 ByName(%22postalco
@@ -4967,35 +4967,35 @@
 oUserDataAccess.
-has
+get
 ByName(%22st%22))%0A  
@@ -5046,15 +5046,8 @@
 ent(
-%22City%22,
 %0A   
@@ -5050,32 +5050,40 @@
 %0A               
+ %22City%22,
  oUserDataAccess
@@ -5075,35 +5075,35 @@
 oUserDataAccess.
-has
+get
 ByName(%22l%22))%0A   
@@ -5153,14 +5153,8 @@
 ent(
-%22Fax%22,
 %0A   
@@ -5157,32 +5157,39 @@
 %0A               
+ %22Fax%22,
  oUserDataAccess
@@ -5189,19 +5189,19 @@
 aAccess.
-has
+get
 ByName(%22

fix a mistake i did yesterday