File indexing completed on 2024-05-26 16:15:05

0001 /* This file is part of the KDE project
0002  * Copyright (C) 2007 Thorsten Zachmann <zachmann@kde.org>
0003  *
0004  * This library is free software; you can redistribute it and/or
0005  * modify it under the terms of the GNU Library General Public
0006  * License as published by the Free Software Foundation; either
0007  * version 2 of the License, or (at your option) any later version.
0008  *
0009  * This library is distributed in the hope that it will be useful,
0010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012  * Library General Public License for more details.
0013  *
0014  * You should have received a copy of the GNU Library General Public License
0015  * along with this library; see the file COPYING.LIB.  If not, write to
0016  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017  * Boston, MA 02110-1301, USA.
0018  */
0019 
0020 #include "KoPAOdfPageSaveHelper.h"
0021 
0022 #include <QSet>
0023 
0024 #include <KoXmlWriter.h>
0025 #include "KoPADocument.h"
0026 #include "KoPAPage.h"
0027 #include "KoPASavingContext.h"
0028 #include "KoPAMasterPage.h"
0029 
0030 KoPAOdfPageSaveHelper::KoPAOdfPageSaveHelper( KoPADocument * doc, QList<KoPAPageBase *> pages )
0031     : m_doc(doc),
0032     m_context(0)
0033 {
0034     foreach( KoPAPageBase * page, pages ) {
0035         if ( dynamic_cast<KoPAPage *>( page ) ) {
0036             m_pages.append( page );
0037         }
0038         else {
0039             m_masterPages.append( page );
0040         }
0041     }
0042 
0043     if ( m_pages.size() > 0 ) {
0044         m_masterPages.clear();
0045 
0046         // this might result in a different order of master pages when copying to a different document
0047         QSet<KoPAPageBase *> masterPages;
0048         foreach( KoPAPageBase * page, m_pages ) {
0049             KoPAPage * p = static_cast<KoPAPage *>( page );
0050             masterPages.insert( p->masterPage() );
0051         }
0052         m_masterPages = masterPages.toList();
0053     }
0054 }
0055 
0056 KoPAOdfPageSaveHelper::~KoPAOdfPageSaveHelper()
0057 {
0058     delete m_context;
0059 }
0060 
0061 KoShapeSavingContext * KoPAOdfPageSaveHelper::context( KoXmlWriter * bodyWriter, KoGenStyles & mainStyles, KoEmbeddedDocumentSaver & embeddedSaver )
0062 {
0063     m_context = new KoPASavingContext( *bodyWriter, mainStyles, embeddedSaver, 1 );
0064     return m_context;
0065 }
0066 
0067 bool KoPAOdfPageSaveHelper::writeBody()
0068 {
0069     Q_ASSERT( m_context );
0070     if ( m_context ) {
0071         m_doc->saveOdfDocumentStyles( *( static_cast<KoPASavingContext*>( m_context ) ) );
0072         KoXmlWriter & bodyWriter = static_cast<KoPASavingContext*>( m_context )->xmlWriter();
0073         bodyWriter.startElement( "office:body" );
0074         bodyWriter.startElement( m_doc->odfTagName( true ) );
0075 
0076         if ( !m_doc->saveOdfPages( *( static_cast<KoPASavingContext*>( m_context ) ), m_pages, m_masterPages ) ) {
0077             return false;
0078         }
0079 
0080         bodyWriter.endElement(); // office:odfTagName()
0081         bodyWriter.endElement(); // office:body
0082 
0083         return true;
0084     }
0085     return false;
0086 }