1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
|
/* This file is part of the KDE project
Copyright (C) 2005 David Faure <faure@kde.org>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License version 2 as published by the Free Software Foundation.
This library 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
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "KWOasisLoader.h"
#include "KWDocument.h"
#include "KWTextDocument.h"
#include "KWTextFrameSet.h"
#include "KWFrameSet.h"
#include "KWPictureFrameSet.h"
#include "KWLoadingInfo.h"
#include "KWTableFrameSet.h"
#include "KWPartFrameSet.h"
#include "KWPageManager.h"
#include "KWFormulaFrameSet.h"
#include <KoStore.h>
#include <KoOasisContext.h>
#include <KoOasisStyles.h>
#include <KoOasisStore.h>
#include <KoOasisSettings.h>
#include <KoXmlNS.h>
#include <KoDom.h>
#include <KoXmlNS.h>
#include <kdebug.h>
#include <tdelocale.h>
#include <tqvaluelist.h>
#include <tqdom.h>
KWOasisLoader::KWOasisLoader( KWDocument* doc )
: m_doc( doc )
{
}
// Warning, this method has no undo/redo support, it is *called* by the undo/redo commands.
// cursor is set when pasting into a textframesetedit (kwcommand), 0 otherwise.
TQValueList<KWFrame *> KWOasisLoader::insertOasisData( KoStore* store, KoTextCursor* cursor )
{
TQValueList<KWFrame *> frames;
if ( store->bad() || !store->hasFile( "content.xml" ) )
{
kdError(32001) << "Invalid ZIP store in memory" << endl;
if ( !store->hasFile( "content.xml" ) )
kdError(32001) << "No content.xml file" << endl;
return frames;
}
store->disallowNameExpansion();
KoOasisStore oasisStore( store );
TQDomDocument contentDoc;
TQString errorMessage;
bool ok = oasisStore.loadAndParse( "content.xml", contentDoc, errorMessage );
if ( !ok ) {
kdError(32001) << "Error parsing content.xml: " << errorMessage << endl;
return frames;
}
KoOasisStyles oasisStyles;
TQDomDocument stylesDoc;
(void)oasisStore.loadAndParse( "styles.xml", stylesDoc, errorMessage );
// Load styles from style.xml
oasisStyles.createStyleMap( stylesDoc, true );
// Also load styles from content.xml
oasisStyles.createStyleMap( contentDoc, false );
m_doc->createLoadingInfo();
TQDomElement content = contentDoc.documentElement();
TQDomElement body( KoDom::namedItemNS( content, KoXmlNS::office, "body" ) );
if ( body.isNull() ) {
kdError(32001) << "No office:body found!" << endl;
return frames;
}
// We then want to use whichever element is the child of <office:body>,
// whether that's <office:text> or <office:presentation> or whatever.
TQDomElement iter, realBody;
forEachElement( iter, body ) {
realBody = iter;
}
if ( realBody.isNull() ) {
kdError(32001) << "No element found inside office:body!" << endl;
return frames;
}
KoOasisContext context( m_doc, *m_doc->variableCollection(), oasisStyles, store );
if ( cursor )
{
KWTextDocument * textdoc = static_cast<KWTextDocument *>(cursor->parag()->document());
KWTextFrameSet * textFs = textdoc->textFrameSet();
*cursor = textFs->textObject()->pasteOasisText( realBody, context, *cursor, m_doc->styleCollection() );
textFs->textObject()->setNeedSpellCheck( true );
}
else // No cursor available, load only the frames
{
// The main loop from KoTextDocument::loadOasisText but for frames only
// (can't paste text if there is no text-frameset being edited, where would it go?)
TQDomElement tag;
forEachElement( tag, realBody )
{
context.styleStack().save();
const TQString bodyTagLocalName = tag.localName();
kdDebug() << k_funcinfo << bodyTagLocalName << endl;
if ( bodyTagLocalName == "frame" && tag.namespaceURI() == KoXmlNS::draw )
{
KWFrame * frame = loadFrame( tag, context, KoPoint( 10, 10 ) /*offset pasted object*/ );
if ( frame )
{
frames.append( frame );
}
}
#if 0 // TODO OASIS table:table
else if ( bodyTagLocalName == "table" && tag.namespaceURI() == KoXmlNS::table )
;
#endif
}
}
//kdDebug() << "KWOasisLoader::execute calling doc->completePasting" << endl;
m_doc->completeOasisPasting();
m_doc->deleteLoadingInfo();
return frames;
}
void KWOasisLoader::loadOasisSettings( const TQDomDocument& settingsDoc )
{
KoOasisSettings settings( settingsDoc );
KoOasisSettings::Items viewSettings = settings.itemSet( "view-settings" );
if ( !viewSettings.isNull() )
{
m_doc->setUnit( KoUnit::unit(viewSettings.parseConfigItemString("unit")) );
}
loadOasisIgnoreList( settings );
m_doc->variableCollection()->variableSetting()->loadOasis( settings );
}
static TQString headerTypeToFramesetName( const TQString& localName, bool hasEvenOdd )
{
if ( localName == "header" )
return hasEvenOdd ? i18n("Odd Pages Header") : i18n( "Header" );
if ( localName == "header-left" )
return i18n("Even Pages Header");
if ( localName == "footer" )
return hasEvenOdd ? i18n("Odd Pages Footer") : i18n( "Footer" );
if ( localName == "footer-left" )
return i18n("Even Pages Footer");
if ( localName == "header-first" ) // NOT OASIS COMPLIANT
return i18n("First Page Header");
if ( localName == "footer-first" ) // NOT OASIS COMPLIANT
return i18n("First Page Footer");
kdWarning(32001) << "Unknown tag in headerTypeToFramesetName: " << localName << endl;
return TQString();
}
static KWFrameSet::Info headerTypeToFrameInfo( const TQString& localName, bool /*hasEvenOdd*/ )
{
if ( localName == "header" )
return KWFrameSet::FI_ODD_HEADER;
if ( localName == "header-left" )
return KWFrameSet::FI_EVEN_HEADER;
if ( localName == "footer" )
return KWFrameSet::FI_ODD_FOOTER;
if ( localName == "footer-left" )
return KWFrameSet::FI_EVEN_FOOTER;
// ######## KWord extension, because I'm too lazy.
// TODO: the real solution is a separate page layout for the first page.
if ( localName == "header-first" ) // NOT OASIS COMPLIANT
return KWFrameSet::FI_FIRST_HEADER;
if ( localName == "footer-first" ) // NOT OASIS COMPLIANT
return KWFrameSet::FI_FIRST_FOOTER;
return KWFrameSet::FI_BODY;
}
void KWOasisLoader::loadOasisHeaderFooter( const TQDomElement& headerFooter, bool hasEvenOdd, TQDomElement& style, KoOasisContext& context )
{
const TQString localName = headerFooter.localName();
bool isHeader = localName.startsWith( "header" );
KWTextFrameSet *fs = new KWTextFrameSet( m_doc, headerTypeToFramesetName( localName, hasEvenOdd ) );
fs->setFrameSetInfo( headerTypeToFrameInfo( localName, hasEvenOdd ) );
m_doc->addFrameSet( fs, false );
if ( !style.isNull() )
context.styleStack().push( style );
KWFrame* frame = new KWFrame( fs, 29, isHeader?0:567, 798-29, 41 );
frame->loadCommonOasisProperties( context, fs, "header-footer" );
const TQString minHeight = context.styleStack().attributeNS( KoXmlNS::fo, "min-height" );
if ( !minHeight.isEmpty() )
frame->setMinimumFrameHeight( KoUnit::parseValue( minHeight ) );
frame->setFrameBehavior( KWFrame::AutoExtendFrame );
frame->setNewFrameBehavior( KWFrame::Copy );
fs->addFrame( frame );
if ( !style.isNull() )
context.styleStack().pop(); // don't let it be active when parsing the text
context.setUseStylesAutoStyles( true ); // use auto-styles from styles.xml, not those from content.xml
fs->loadOasisContent( headerFooter, context );
context.setUseStylesAutoStyles( false );
if ( isHeader )
m_doc->m_headerVisible = true;
else
m_doc->m_footerVisible = true;
}
void KWOasisLoader::loadOasisIgnoreList( const KoOasisSettings& settings )
{
KoOasisSettings::Items configurationSettings = settings.itemSet( "configuration-settings" );
if ( !configurationSettings.isNull() )
{
const TQString ignorelist = configurationSettings.parseConfigItemString( "SpellCheckerIgnoreList" );
kdDebug()<<" ignorelist :"<<ignorelist<<endl;
m_doc->setSpellCheckIgnoreList( TQStringList::split( ',', ignorelist ) );
}
}
KWFrame* KWOasisLoader::loadFrame( const TQDomElement& frameTag, KoOasisContext& context, const KoPoint& offset )
{
KWFrame* frame = 0;
TQDomElement elem;
forEachElement( elem, frameTag )
{
if ( elem.namespaceURI() != KoXmlNS::draw )
continue;
const TQString localName = elem.localName();
if ( localName == "text-box" )
{
//kdDebug()<<" append text-box\n";
frame = loadOasisTextBox( frameTag, elem, context );
break;
}
else if ( localName == "image" )
{
KWFrameSet* fs = new KWPictureFrameSet( m_doc, frameTag, elem, context );
m_doc->addFrameSet( fs, false );
frame = fs->frame(0);
break;
} else if ( localName == "object" )
{
TQDomElement mathElem; // will be set if we find <math:math>
TQDomElement childElem;
forEachElement( childElem, elem )
{
if ( childElem.localName() == "math"
&& childElem.namespaceURI() == KoXmlNS::math ) {
mathElem = childElem;
}
}
if ( !mathElem.isNull() ) {
KWFormulaFrameSet* fs = new KWFormulaFrameSet( m_doc, frameTag, mathElem, context );
m_doc->addFrameSet( fs, false );
frame = fs->frame(0);
} else {
KWPartFrameSet* fs = new KWPartFrameSet( m_doc, frameTag, elem, context );
m_doc->addFrameSet( fs, false );
frame = fs->frame(0);
}
break;
}
}
if ( frame ) {
const TQString anchorType = frameTag.attributeNS( KoXmlNS::text, "anchor-type", TQString() );
if ( anchorType == "page" ) {
double x = KoUnit::parseValue( frameTag.attributeNS( KoXmlNS::svg, "x", TQString() ) );
double y = KoUnit::parseValue( frameTag.attributeNS( KoXmlNS::svg, "y", TQString() ) );
int pageNum = frameTag.attributeNS( KoXmlNS::text, "anchor-page-number", TQString() ).toInt();
// Ensure that we have enough pages
KWPageManager* pageManager = m_doc->pageManager();
while ( pageNum > pageManager->lastPageNumber() )
pageManager->appendPage();
frame->moveTopLeft( KoPoint( x, y + pageManager->topOfPage(pageNum) ) );
}
frame->moveBy( offset.x(), offset.y() );
}
return frame;
}
KWFrame* KWOasisLoader::loadOasisTextBox( const TQDomElement& frameTag, const TQDomElement& tag,
KoOasisContext& context )
{
// Text frame chains. When seeing frame 'B' is chained to this frame A when loading,
// we store 'B' -> A, so that when loading B we can add it to A's frameset.
// If we load B first, no problem: when loading A we can chain.
// This is all made simpler by the fact that we don't have manually configurable order in KWord...
// But it's made more complex by the fact that frames don't have names in KWord (framesets do).
// Hence the framename temporary storage in KWLoadingInfo
KWLoadingInfo* loadingInfo = m_doc->loadingInfo();
KWTextFrameSet* fs = 0;
TQString frameName = frameTag.attributeNS( KoXmlNS::draw, "name", TQString() );
TQString chainNextName = tag.attributeNS( KoXmlNS::draw, "chain-next-name", TQString() );
if ( !chainNextName.isEmpty() && loadingInfo ) { // 'B' in the above example
kdDebug(32001) << "Loading " << frameName << " : next-in-chain=" << chainNextName << endl;
// Check if we already loaded that frame (then we need to go 'before' it)
KWFrame* nextFrame = loadingInfo->frameByName( chainNextName );
if ( nextFrame ) {
fs = dynamic_cast<KWTextFrameSet *>( nextFrame->frameSet() );
chainNextName = TQString(); // already found, no need to store it
kdDebug(32001) << " found " << nextFrame << " -> frameset " << ( fs ? fs->name() : TQString() ) << endl;
}
}
KWFrame* prevFrame = loadingInfo->chainPrevFrame( frameName );
//kdDebug(32001) << "Loading " << frameName << " : chainPrevFrame=" << prevFrame << endl;
if ( prevFrame ) {
if ( fs ) // we are between prevFrame and nextFrame. They'd better be for the same fs!!
Q_ASSERT( fs == prevFrame->frameSet() );
fs = dynamic_cast<KWTextFrameSet *>( prevFrame->frameSet() );
//kdDebug(32001) << " found " << prevFrame << " -> frameset " << fs->name() << endl;
}
KWFrame* frame = 0;
if ( !fs ) {
fs = new KWTextFrameSet( m_doc, frameTag, context );
m_doc->addFrameSet( fs, false );
frame = fs->loadOasis( frameTag, tag, context );
} else { // Adding frame to existing frameset
context.styleStack().save();
context.fillStyleStack( frameTag, KoXmlNS::draw, "style-name", "graphic" ); // get the style for the graphics element
frame = fs->loadOasisTextFrame( frameTag, tag, context );
context.styleStack().restore();
}
loadingInfo->storeFrameName( frame, frameName );
if ( !chainNextName.isEmpty() ) {
loadingInfo->storeNextFrame( frame, chainNextName );
}
return frame;
}
KWTableFrameSet* KWOasisLoader::loadOasisTable( const TQDomElement& tag,
KoOasisContext& context )
{
const TQString name = tag.attributeNS( KoXmlNS::table, "name", i18n( "Unnamed Table" ) ); // ### check for unicity?
KWTableFrameSet* table = new KWTableFrameSet( m_doc, name );
table->loadOasis( tag, context );
m_doc->addFrameSet(table, false);
return table;
}
|