kword

KWOasisSaver.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2005 David Faure <faure@kde.org>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License version 2 as published by the Free Software Foundation.
7
8 This library is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 Library General Public License for more details.
12
13 You should have received a copy of the GNU Library General Public License
14 along with this library; see the file COPYING.LIB. If not, write to
15 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16 * Boston, MA 02110-1301, USA.
17*/
18
19#include "KWOasisSaver.h"
20#include <KoStore.h>
21#include <KoOasisStore.h>
22#include <KoOasisContext.h>
23#include <KoXmlWriter.h>
24#include "KWDocument.h"
25
27 : m_doc( doc )
28{
29 const TQCString mimeType = selectionMimeType();
30 m_store = KoStore::createStore( &m_buffer, KoStore::Write, mimeType );
31 Q_ASSERT( m_store );
32 Q_ASSERT( !m_store->bad() );
33
34 m_oasisStore = new KoOasisStore( m_store );
35
36 //KoXmlWriter* manifestWriter = oasisStore.manifestWriter( mimeType );
37
38 m_savingContext = new KoSavingContext( m_mainStyles, 0, false, KoSavingContext::Store );
39
40 KoXmlWriter* bodyWriter = m_oasisStore->bodyWriter();
41 bodyWriter->startElement( "office:body" );
42 bodyWriter->startElement( "office:text" );
43}
44
45void KWOasisSaver::saveParagraphs( const TQValueList<const KoTextParag *>& paragraphs )
46{
47 for ( TQValueList<const KoTextParag *>::const_iterator it = paragraphs.begin(),
48 end = paragraphs.end();
49 it != end ; ++it ) {
50 saveParagraph( *it );
51 }
52}
53
54void KWOasisSaver::saveParagraph( const KoTextParag* parag )
55{
56 // keep in sync with KoTextDocument::copySelection
57 KoXmlWriter* bodyWriter = m_oasisStore->bodyWriter();
58 parag->saveOasis( *bodyWriter, *m_savingContext, 0, parag->length()-2, true );
59}
60
61TQByteArray KWOasisSaver::data() const
62{
63 Q_ASSERT( !m_store ); // forgot to call finish() (or to check its return value) ?
64 return const_cast<TQBuffer&>(m_buffer).buffer();
65}
66
67KWOasisSaver::~KWOasisSaver()
68{
69 delete m_store;
70 delete m_oasisStore;
71 delete m_savingContext;
72}
73
75{
76 KoXmlWriter* bodyWriter = m_oasisStore->bodyWriter();
77 bodyWriter->endElement(); // office:text
78 bodyWriter->endElement(); // office:body
79
80 KoXmlWriter* contentWriter = m_oasisStore->contentWriter();
81 Q_ASSERT( contentWriter );
82
83 m_savingContext->writeFontFaces( *contentWriter );
84 contentWriter->startElement( "office:automatic-styles" );
85 writeAutomaticStyles( *contentWriter, m_mainStyles, false );
86 contentWriter->endElement(); // office:automatic-styles
87
88 m_oasisStore->closeContentWriter();
89
90 if ( !m_store->open( "styles.xml" ) )
91 return false;
92 //manifestWriter->addManifestEntry( "styles.xml", "text/xml" );
93 m_doc->saveOasisDocumentStyles( m_store, m_mainStyles, *m_savingContext,
94 KWDocument::SaveSelected /* simply means not SaveAll */,
95 TQByteArray() /* no headers/footers */ );
96 if ( !m_store->close() ) // done with styles.xml
97 return false;
98
99 delete m_oasisStore; m_oasisStore = 0;
100 delete m_store; m_store = 0;
101
102 return true;
103}
104
105void KWOasisSaver::writeAutomaticStyles( KoXmlWriter& contentWriter, KoGenStyles& mainStyles, bool stylesDotXml )
106{
107 TQValueList<KoGenStyles::NamedStyle> styles = mainStyles.styles( KoGenStyle::STYLE_AUTO, stylesDotXml );
108 TQValueList<KoGenStyles::NamedStyle>::const_iterator it = styles.begin();
109 for ( ; it != styles.end() ; ++it ) {
110 (*it).style->writeStyle( &contentWriter, mainStyles, "style:style", (*it).name, "style:paragraph-properties" );
111 }
112
113 styles = mainStyles.styles( KoGenStyle::STYLE_AUTO_LIST, stylesDotXml );
114 it = styles.begin();
115 for ( ; it != styles.end() ; ++it ) {
116 (*it).style->writeStyle( &contentWriter, mainStyles, "text:list-style", (*it).name, 0 );
117 }
118
119 styles = mainStyles.styles( KWDocument::STYLE_FRAME_AUTO, stylesDotXml );
120 it = styles.begin();
121 for ( ; it != styles.end() ; ++it ) {
122 (*it).style->writeStyle( &contentWriter, mainStyles, "style:style", (*it).name , "style:graphic-properties" );
123 }
124
125 styles = mainStyles.styles( KWDocument::STYLE_TABLE, stylesDotXml );
126 it = styles.begin();
127 for ( ; it != styles.end() ; ++it ) {
128 (*it).style->writeStyle( &contentWriter, mainStyles, "style:style", (*it).name , "style:table-properties" );
129 }
130
131 styles = mainStyles.styles( KWDocument::STYLE_TABLE_COLUMN, stylesDotXml );
132 it = styles.begin();
133 for ( ; it != styles.end() ; ++it ) {
134 (*it).style->writeStyle( &contentWriter, mainStyles, "style:style", (*it).name , "style:table-column-properties" );
135 }
136
137 styles = mainStyles.styles( KWDocument::STYLE_TABLE_CELL_AUTO, stylesDotXml );
138 it = styles.begin();
139 for ( ; it != styles.end() ; ++it ) {
140 (*it).style->writeStyle( &contentWriter, mainStyles, "style:style", (*it).name , "style:table-cell-properties" );
141 }
142
143 styles = mainStyles.styles( KoGenStyle::STYLE_NUMERIC_DATE, stylesDotXml );
144 it = styles.begin();
145 for ( ; it != styles.end() ; ++it ) {
146 (*it).style->writeStyle( &contentWriter, mainStyles, "number:date-style", (*it).name, 0 /*TODO ????*/ );
147 }
148 styles = mainStyles.styles( KoGenStyle::STYLE_NUMERIC_TIME, stylesDotXml );
149 it = styles.begin();
150 for ( ; it != styles.end() ; ++it ) {
151 (*it).style->writeStyle( &contentWriter, mainStyles, "number:time-style", (*it).name, 0 /*TODO ????*/ );
152 }
153}
154
155const char* KWOasisSaver::selectionMimeType()
156{
157 return "application/vnd.oasis.opendocument.text";
158}
KWOasisSaver(KWDocument *doc)
TQByteArray data() const
Return the entire ZIP store as a byte array.
KoXmlWriter & bodyWriter()
Another way to save paragraphs is to use KoTextObject::saveOasisContent It needs a body writer and a ...
void saveParagraph(const KoTextParag *parag)
Save a paragraph.
bool finish()
You MUST call this after saving the paragraphs and before calling data() Return true on success.
static void writeAutomaticStyles(KoXmlWriter &contentWriter, KoGenStyles &mainStyles, bool stylesDotXml)
Helper function, also used by KWDocument.
void saveParagraphs(const TQValueList< const KoTextParag * > &paragraphs)
Save a list of paragraphs.