kword

KWOasisSaver.cpp

00001 /* This file is part of the KDE project
00002    Copyright (C) 2005 David Faure <faure@kde.org>
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License version 2 as published by the Free Software Foundation.
00007 
00008    This library is distributed in the hope that it will be useful,
00009    but WITHOUT ANY WARRANTY; without even the implied warranty of
00010    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011    Library General Public License for more details.
00012 
00013    You should have received a copy of the GNU Library General Public License
00014    along with this library; see the file COPYING.LIB.  If not, write to
00015    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00016  * Boston, MA 02110-1301, USA.
00017 */
00018 
00019 #include "KWOasisSaver.h"
00020 #include <KoStore.h>
00021 #include <KoOasisStore.h>
00022 #include <KoOasisContext.h>
00023 #include <KoXmlWriter.h>
00024 #include "KWDocument.h"
00025 
00026 KWOasisSaver::KWOasisSaver( KWDocument* doc )
00027     : m_doc( doc )
00028 {
00029     const QCString mimeType = selectionMimeType();
00030     m_store = KoStore::createStore( &m_buffer, KoStore::Write, mimeType );
00031     Q_ASSERT( m_store );
00032     Q_ASSERT( !m_store->bad() );
00033 
00034     m_oasisStore = new KoOasisStore( m_store );
00035 
00036     //KoXmlWriter* manifestWriter = oasisStore.manifestWriter( mimeType );
00037 
00038     m_savingContext = new KoSavingContext( m_mainStyles, 0, false, KoSavingContext::Store );
00039 
00040     KoXmlWriter* bodyWriter = m_oasisStore->bodyWriter();
00041     bodyWriter->startElement( "office:body" );
00042     bodyWriter->startElement( "office:text" );
00043 }
00044 
00045 void KWOasisSaver::saveParagraphs( const QValueList<const KoTextParag *>& paragraphs )
00046 {
00047     for ( QValueList<const KoTextParag *>::const_iterator it = paragraphs.begin(),
00048                                                    end = paragraphs.end();
00049                   it != end ; ++it ) {
00050         saveParagraph( *it );
00051     }
00052 }
00053 
00054 void KWOasisSaver::saveParagraph( const KoTextParag* parag )
00055 {
00056     // keep in sync with KoTextDocument::copySelection
00057     KoXmlWriter* bodyWriter = m_oasisStore->bodyWriter();
00058     parag->saveOasis( *bodyWriter, *m_savingContext, 0, parag->length()-2, true );
00059 }
00060 
00061 QByteArray KWOasisSaver::data() const
00062 {
00063     Q_ASSERT( !m_store ); // forgot to call finish() (or to check its return value) ?
00064     return m_buffer.buffer();
00065 }
00066 
00067 KWOasisSaver::~KWOasisSaver()
00068 {
00069     delete m_store;
00070     delete m_oasisStore;
00071     delete m_savingContext;
00072 }
00073 
00074 bool KWOasisSaver::finish()
00075 {
00076     KoXmlWriter* bodyWriter = m_oasisStore->bodyWriter();
00077     bodyWriter->endElement(); // office:text
00078     bodyWriter->endElement(); // office:body
00079 
00080     KoXmlWriter* contentWriter = m_oasisStore->contentWriter();
00081     Q_ASSERT( contentWriter );
00082 
00083     m_savingContext->writeFontFaces( *contentWriter );
00084     contentWriter->startElement( "office:automatic-styles" );
00085     writeAutomaticStyles( *contentWriter, m_mainStyles, false );
00086     contentWriter->endElement(); // office:automatic-styles
00087 
00088     m_oasisStore->closeContentWriter();
00089 
00090     if ( !m_store->open( "styles.xml" ) )
00091         return false;
00092     //manifestWriter->addManifestEntry( "styles.xml", "text/xml" );
00093     m_doc->saveOasisDocumentStyles( m_store, m_mainStyles, *m_savingContext,
00094                                     KWDocument::SaveSelected /* simply means not SaveAll */,
00095                                     QByteArray() /* no headers/footers */ );
00096     if ( !m_store->close() ) // done with styles.xml
00097         return false;
00098 
00099     delete m_oasisStore; m_oasisStore = 0;
00100     delete m_store; m_store = 0;
00101 
00102     return true;
00103 }
00104 
00105 void KWOasisSaver::writeAutomaticStyles( KoXmlWriter& contentWriter, KoGenStyles& mainStyles, bool stylesDotXml )
00106 {
00107     QValueList<KoGenStyles::NamedStyle> styles = mainStyles.styles( KoGenStyle::STYLE_AUTO, stylesDotXml );
00108     QValueList<KoGenStyles::NamedStyle>::const_iterator it = styles.begin();
00109     for ( ; it != styles.end() ; ++it ) {
00110         (*it).style->writeStyle( &contentWriter, mainStyles, "style:style", (*it).name, "style:paragraph-properties" );
00111     }
00112 
00113     styles = mainStyles.styles( KoGenStyle::STYLE_AUTO_LIST, stylesDotXml );
00114     it = styles.begin();
00115     for ( ; it != styles.end() ; ++it ) {
00116         (*it).style->writeStyle( &contentWriter, mainStyles, "text:list-style", (*it).name, 0 );
00117     }
00118 
00119     styles = mainStyles.styles( KWDocument::STYLE_FRAME_AUTO, stylesDotXml );
00120     it = styles.begin();
00121     for ( ; it != styles.end() ; ++it ) {
00122         (*it).style->writeStyle( &contentWriter, mainStyles, "style:style", (*it).name , "style:graphic-properties"  );
00123     }
00124 
00125     styles = mainStyles.styles( KWDocument::STYLE_TABLE, stylesDotXml );
00126     it = styles.begin();
00127     for ( ; it != styles.end() ; ++it ) {
00128         (*it).style->writeStyle( &contentWriter, mainStyles, "style:style", (*it).name , "style:table-properties"  );
00129     }
00130 
00131     styles = mainStyles.styles( KWDocument::STYLE_TABLE_COLUMN, stylesDotXml );
00132     it = styles.begin();
00133     for ( ; it != styles.end() ; ++it ) {
00134         (*it).style->writeStyle( &contentWriter, mainStyles, "style:style", (*it).name , "style:table-column-properties"  );
00135     }
00136 
00137     styles = mainStyles.styles( KWDocument::STYLE_TABLE_CELL_AUTO, stylesDotXml );
00138     it = styles.begin();
00139     for ( ; it != styles.end() ; ++it ) {
00140         (*it).style->writeStyle( &contentWriter, mainStyles, "style:style", (*it).name , "style:table-cell-properties"  );
00141     }
00142 
00143     styles = mainStyles.styles( KoGenStyle::STYLE_NUMERIC_DATE, stylesDotXml );
00144     it = styles.begin();
00145     for ( ; it != styles.end() ; ++it ) {
00146         (*it).style->writeStyle( &contentWriter, mainStyles, "number:date-style", (*it).name, 0 /*TODO ????*/  );
00147     }
00148     styles = mainStyles.styles( KoGenStyle::STYLE_NUMERIC_TIME, stylesDotXml );
00149     it = styles.begin();
00150     for ( ; it != styles.end() ; ++it ) {
00151         (*it).style->writeStyle( &contentWriter, mainStyles, "number:time-style", (*it).name, 0 /*TODO ????*/  );
00152     }
00153 }
00154 
00155 const char* KWOasisSaver::selectionMimeType()
00156 {
00157     return "application/vnd.oasis.opendocument.text";
00158 }
KDE Home | KDE Accessibility Home | Description of Access Keys