KoOasisLoadingContext.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 "KoOasisLoadingContext.h"
20#include <KoOasisStore.h>
21#include <KoOasisStyles.h>
22#include <KoStore.h>
23#include <KoXmlNS.h>
24#include <kdebug.h>
25#include <KoDom.h>
26
28 KoOasisStyles& styles, KoStore* store )
29 : m_doc( doc ), m_store( store ), m_styles( styles ),
30 m_metaXmlParsed( false ), m_useStylesAutoStyles( false )
31{
32 // Ideally this should be done by KoDocument and passed as argument here...
33 KoOasisStore oasisStore( store );
34 TQString dummy;
35 (void)oasisStore.loadAndParse( "tar:/META-INF/manifest.xml", m_manifestDoc, dummy );
36}
37
38
39KoOasisLoadingContext::~KoOasisLoadingContext()
40{
41
42}
43
44void KoOasisLoadingContext::fillStyleStack( const TQDomElement& object, const char* nsURI, const char* attrName, const char* family )
45{
46 // find all styles associated with an object and push them on the stack
47 if ( object.hasAttributeNS( nsURI, attrName ) ) {
48 const TQString styleName = object.attributeNS( nsURI, attrName, TQString() );
49 const TQDomElement* style = 0;
50 bool isStyleAutoStyle = false;
51 if ( m_useStylesAutoStyles ) {
52 // When loading something from styles.xml, look into the styles.xml auto styles first
53 style = m_styles.findStyleAutoStyle( styleName, family );
54 // and fallback to looking at styles(), which includes the user styles from styles.xml
55 if ( style )
56 isStyleAutoStyle = true;
57 }
58 if ( !style )
59 style = m_styles.findStyle( styleName, family );
60 if ( style )
61 addStyles( style, family, isStyleAutoStyle );
62 else
63 kdWarning(32500) << "fillStyleStack: no style named " << styleName << " found." << endl;
64 }
65}
66
67void KoOasisLoadingContext::addStyles( const TQDomElement* style, const char* family, bool usingStylesAutoStyles )
68{
69 Q_ASSERT( style );
70 if ( !style ) return;
71 // this recursive function is necessary as parent styles can have parents themselves
72 if ( style->hasAttributeNS( KoXmlNS::style, "parent-style-name" ) ) {
73 const TQString parentStyleName = style->attributeNS( KoXmlNS::style, "parent-style-name", TQString() );
74 const TQDomElement* parentStyle = 0;
75 if ( usingStylesAutoStyles ) {
76 // When loading something from styles.xml, look into the styles.xml auto styles first
77 parentStyle = m_styles.findStyleAutoStyle( parentStyleName, family );
78 // and fallback to looking at styles(), which includes the user styles from styles.xml
79 }
80 if ( !parentStyle )
81 parentStyle = m_styles.findStyle( parentStyleName, family );
82 if ( parentStyle )
83 addStyles( parentStyle, family, usingStylesAutoStyles );
84 else
85 kdWarning(32500) << "Parent style not found: " << parentStyleName << endl;
86 }
87 else if ( family ) {
88 const TQDomElement* def = m_styles.defaultStyle( family );
89 if ( def ) { // on top of all, the default style for this family
90 //kdDebug(32500) << "pushing default style " << style->attributeNS( KoXmlNS::style, "name", TQString() ) << endl;
91 m_styleStack.push( *def );
92 }
93 }
94
95 //kdDebug(32500) << "pushing style " << style->attributeNS( KoXmlNS::style, "name", TQString() ) << endl;
96 m_styleStack.push( *style );
97}
98
100{
101 parseMeta();
102 return m_generator;
103}
104
105void KoOasisLoadingContext::parseMeta() const
106{
107 if ( !m_metaXmlParsed && m_store )
108 {
109 if ( m_store->hasFile( "meta.xml" ) )
110 {
111 TQDomDocument metaDoc;
112 KoOasisStore oasisStore( m_store );
113 TQString errorMsg;
114 if ( oasisStore.loadAndParse( "meta.xml", metaDoc, errorMsg ) ) {
115 TQDomNode meta = KoDom::namedItemNS( metaDoc, KoXmlNS::office, "document-meta" );
116 TQDomNode office = KoDom::namedItemNS( meta, KoXmlNS::office, "meta" );
117 TQDomElement generator = KoDom::namedItemNS( office, KoXmlNS::meta, "generator" );
118 if ( !generator.isNull() )
119 m_generator = generator.text();
120 }
121 }
122 m_metaXmlParsed = true;
123 }
124}
The KOffice document class.
Definition KoDocument.h:61
void addStyles(const TQDomElement *style, const char *family, bool usingStylesAutoStyles=false)
Add style to the stack, as well as all its parent styles and the default style for this style family.
KoOasisLoadingContext(KoDocument *doc, KoOasisStyles &styles, KoStore *store)
Stores reference to the KoOasisStyles and stored passed by KoDocument.
void fillStyleStack(const TQDomElement &element, const char *nsURI, const char *attrName, const char *family)
Convenience method for loading the style of an object before loading that object.
TQString generator() const
Return the <meta:generator> of the document, e.g. "KOffice/1.4.0a".
Helper class around KoStore for writing out OASIS files.
bool loadAndParse(const TQString &fileName, TQDomDocument &doc, TQString &errorMessage)
A completely unrelated method, for loading a file from an oasis store.
Repository of styles used during loading of OASIS/OOo file.
const TQDomElement * findStyleAutoStyle(const TQString &name, const TQString &family) const
Similar to findStyle but for auto-styles in styles.xml only.
const TQDomElement * findStyle(const TQString &name) const
Look up a style by name.
const TQDomElement * defaultStyle(const TQString &family) const
Saves and loads KOffice documents using various backends.
Definition KoStore.h:40
bool hasFile(const TQString &fileName) const
Definition KoStore.cpp:625
void push(const TQDomElement &style)
Pushes the new style onto the stack.
KOFFICECORE_EXPORT TQDomElement namedItemNS(const TQDomNode &node, const char *nsURI, const char *localName)
A namespace-aware version of TQDomNode::namedItem(), which also takes care of casting to a TQDomEleme...
Definition KoDom.cpp:22