/* AbiSource Program Utilities
 * Copyright (C) 2002 Dom Lachowicz <cinamod@hotmail.com>
 * 
 * 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., 59 Temple Place - Suite 330, Boston, MA  
 * 02111-1307, USA.
 */

// abiword stuff
#include "fd_Field.h"
#include "fl_AutoNum.h"
#include "fp_PageSize.h"

#include "px_ChangeRecord.h"
#include "px_CR_Object.h"
#include "px_CR_Span.h"
#include "px_CR_Strux.h"
#include "pd_Document.h"
#include "pp_AttrProp.h"

#include "ut_misc.h"
#include "ut_string.h"
#include "ut_string_class.h"
#include "ut_bytebuf.h"
#include "ut_hash.h"
#include "ut_vector.h"
#include "ut_stack.h"
#include "ut_assert.h"
#include "ut_PropVector.h"

#include <gsf/gsf-output.h>

class ABI_EXPORT IE_Exp_OpenWriter : public IE_Exp
{
public:
  IE_Exp_OpenWriter(PD_Document * pDocument);
  virtual ~IE_Exp_OpenWriter();

protected:
  virtual UT_Error	_writeDocument(void);
  virtual bool		_openFile(const char * szFilename);
  virtual bool		_closeFile(void);
  
private:
  GsfOutfile * m_oo;	
};

/*****************************************************************************/
/*****************************************************************************/

class OO_Style
{
public:
   OO_Style(UT_String &props, UT_String &name);

private:
   UT_String props;
   UT_String name;
};

/*****************************************************************************/
/*****************************************************************************/

class OO_ListenerImpl
{
public:
   OO_ListenerImpl() {}
   virtual ~OO_ListenerImpl() {}
   virtual void insertText(const UT_UCSChar * data, UT_uint32 length) = 0;
   virtual void openBlock(UT_String & styleAtts, UT_String & styleProps, UT_String & font, bool bIsHeading = false) = 0;
   virtual void closeBlock() = 0;
   virtual void openSpan(UT_String & props, UT_String & font) = 0;
   virtual void closeSpan() = 0;   
};

class OO_Listener : public PL_Listener
{
public:
   OO_Listener(PD_Document * pDocument, IE_Exp_OpenWriter * pie, OO_ListenerImpl *pListenerImpl);

   virtual bool populate(PL_StruxFmtHandle sfh, const PX_ChangeRecord * pcr);
   virtual bool populateStrux(PL_StruxDocHandle sdh, const PX_ChangeRecord * pcr, PL_StruxFmtHandle * psfh);
   virtual bool change(PL_StruxFmtHandle sfh, const PX_ChangeRecord * pcr);
   virtual bool insertStrux(PL_StruxFmtHandle sfh,
			    const PX_ChangeRecord * pcr,
			    PL_StruxDocHandle sdh,
			    PL_ListenerId lid,
			    void (* pfnBindHandles)(PL_StruxDocHandle sdhNew,
						    PL_ListenerId lid,
						    PL_StruxFmtHandle sfhNew));
   virtual bool signal(UT_uint32 iSignal);

   void endDocument();

private:
   void _openSpan(PT_AttrPropIndex api);
   void _closeSpan();
   void _openBlock(PT_AttrPropIndex apiSpan);
   void _closeBlock();

   bool m_bInBlock;
   bool m_bInSpan;

   OO_ListenerImpl * m_pListenerImpl;
   PD_Document * m_pDocument;
   IE_Exp_OpenWriter * m_pie;

};

class OO_StylesContainer
{
public:
   OO_StylesContainer() {}
   ~OO_StylesContainer() { 
	   m_spanStylesHash.purgeData();
	   m_blockAttsHash.purgeData();
	   m_fontsHash.purgeData();
   }
   void addSpanStyle(UT_String &key);
   void addBlockStyle(UT_String & styleAtts, UT_String & styleProps);
   void addFont(UT_String & font);
   const int getSpanStyleNum(UT_String &key) const;
   const int getBlockStyleNum(UT_String & styleAtts, UT_String & styleProps) const;
   UT_GenericVector<int*> * enumerateSpanStyles() const;
   UT_String * pickBlockAtts(const UT_String *key);
   UT_GenericVector<const UT_String*> * getSpanStylesKeys() const;
   UT_GenericVector<const UT_String*> * getBlockStylesKeys() const;
   UT_GenericVector<const UT_String*> * getFontsKeys() const;
private:
   UT_GenericStringMap<int*> m_spanStylesHash;
   UT_GenericStringMap<UT_String*> m_blockAttsHash;
   UT_GenericStringMap<int*> m_fontsHash;
};

/*!
 * OO_AccumulatorImpl: This class collects style definitions from the document,
 * gathering information from the listener which references it. It does not
 * actually write anything, merely storing 
 */
class OO_AccumulatorImpl : public OO_ListenerImpl
{
public:
   OO_AccumulatorImpl(OO_StylesContainer *pStylesContainer) : OO_ListenerImpl() { m_pStylesContainer = pStylesContainer; }
   virtual void insertText(const UT_UCSChar * data, UT_uint32 length) {}
   virtual void openBlock(UT_String & styleAtts, UT_String & styleProps, UT_String & font, bool bIsHeading = false);
   virtual void closeBlock() {};
   virtual void openSpan(UT_String & props, UT_String & font);
   virtual void closeSpan() {}

private:
   OO_StylesContainer *m_pStylesContainer;
};

/*!
 * OO_WriterImpl: This class writes out the content which reference the style
 * definitions which we collected earlier with the accumulator.
 */
class OO_WriterImpl : public OO_ListenerImpl
{
public:
   OO_WriterImpl(GsfOutfile *pOutfile, OO_StylesContainer *pStylesContainer);
   ~OO_WriterImpl();
   virtual void insertText(const UT_UCSChar * data, UT_uint32 length);
   virtual void openBlock(UT_String & styleAtts, UT_String & styleProps, UT_String & font, bool bIsHeading = false);
   virtual void closeBlock();
   virtual void openSpan(UT_String & props, UT_String & font);
   virtual void closeSpan();

private:
   GsfOutput * m_pContentStream;
   OO_StylesContainer *m_pStylesContainer;
   UT_UTF8String m_blockEnd;
};

/*!
 * OO_StylesWriter: This class writes the "styles.xml" portion
 * of the .sxw file. It can also be used to convert Abi's attributes and
 * properties.
 */
class OO_StylesWriter
{
public:
   /*!
    * Write the styles stream.
    */
   static bool writeStyles(PD_Document * pDoc, GsfOutfile * oo, OO_StylesContainer & stylesContainer);
   /*
    * Add <office:font-decls> section to the buffer
    */
   static void addFontDecls(UT_UTF8String & buffer, OO_StylesContainer & stylesContainer);
   /*!
    * Convert attributes and properties.
    */
   static void map(const PP_AttrProp * pAP, UT_UTF8String & styleAtts, UT_UTF8String & propAtts, UT_UTF8String & font);
private:
  OO_StylesWriter ();
};
