F:/KPlato/koffice/libs/kofficecore/KoOasisLoadingContext.cpp

Aller à la documentation de ce fichier.
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 "KoOasisLoadingContext.h"
00020 #include <KoOasisStore.h>
00021 #include <KoOasisStyles.h>
00022 #include <KoStore.h>
00023 #include <KoXmlNS.h>
00024 #include <kdebug.h>
00025 #include <KoDom.h>
00026 
00027 KoOasisLoadingContext::KoOasisLoadingContext( KoDocument* doc,
00028                                               KoOasisStyles& styles, KoStore* store )
00029     : m_doc( doc ), m_store( store ), m_styles( styles ),
00030       m_metaXmlParsed( false ), m_useStylesAutoStyles( false )
00031 {
00032     // Ideally this should be done by KoDocument and passed as argument here...
00033     KoOasisStore oasisStore( store );
00034     QString dummy;
00035     (void)oasisStore.loadAndParse( "tar:/META-INF/manifest.xml", m_manifestDoc, dummy );
00036 }
00037 
00038 
00039 KoOasisLoadingContext::~KoOasisLoadingContext()
00040 {
00041 
00042 }
00043 
00044 void KoOasisLoadingContext::fillStyleStack( const KoXmlElement& object, const char* nsURI, const char* attrName, const char* family )
00045 {
00046     // find all styles associated with an object and push them on the stack
00047     if ( object.hasAttributeNS( nsURI, attrName ) ) {
00048         const QString styleName = object.attributeNS( nsURI, attrName, QString::null );
00049         const KoXmlElement* style = 0;
00050         bool isStyleAutoStyle = false;
00051         if ( m_useStylesAutoStyles ) {
00052             // When loading something from styles.xml, look into the styles.xml auto styles first
00053             style = m_styles.findStyleAutoStyle( styleName, family );
00054             // and fallback to looking at styles(), which includes the user styles from styles.xml
00055             if ( style )
00056                 isStyleAutoStyle = true;
00057         }
00058         if ( !style )
00059             style = m_styles.findStyle( styleName, family );
00060         if ( style )
00061             addStyles( style, family, isStyleAutoStyle );
00062         else
00063             kWarning(32500) << "fillStyleStack: no style named " << styleName << " found." << endl;
00064     }
00065 }
00066 
00067 void KoOasisLoadingContext::addStyles( const KoXmlElement* style, const char* family, bool usingStylesAutoStyles )
00068 {
00069     Q_ASSERT( style );
00070     if ( !style ) return;
00071     // this recursive function is necessary as parent styles can have parents themselves
00072     if ( style->hasAttributeNS( KoXmlNS::style, "parent-style-name" ) ) {
00073         const QString parentStyleName = style->attributeNS( KoXmlNS::style, "parent-style-name", QString::null );
00074         const KoXmlElement* parentStyle = 0;
00075         if ( usingStylesAutoStyles ) {
00076             // When loading something from styles.xml, look into the styles.xml auto styles first
00077             parentStyle = m_styles.findStyleAutoStyle( parentStyleName, family );
00078             // and fallback to looking at styles(), which includes the user styles from styles.xml
00079         }
00080         if ( !parentStyle )
00081             parentStyle = m_styles.findStyle( parentStyleName, family );
00082         if ( parentStyle )
00083             addStyles( parentStyle, family, usingStylesAutoStyles );
00084         else
00085             kWarning(32500) << "Parent style not found: " << parentStyleName << endl;
00086     }
00087     else if ( family ) {
00088         const KoXmlElement* def = m_styles.defaultStyle( family );
00089         if ( def ) { // on top of all, the default style for this family
00090             //kDebug(32500) << "pushing default style " << style->attributeNS( KoXmlNS::style, "name", QString::null ) << endl;
00091             m_styleStack.push( *def );
00092         }
00093     }
00094 
00095     //kDebug(32500) << "pushing style " << style->attributeNS( KoXmlNS::style, "name", QString::null ) << endl;
00096     m_styleStack.push( *style );
00097 }
00098 
00099 QString KoOasisLoadingContext::generator() const
00100 {
00101     parseMeta();
00102     return m_generator;
00103 }
00104 
00105 void KoOasisLoadingContext::parseMeta() const
00106 {
00107     if ( !m_metaXmlParsed && m_store )
00108     {
00109         if ( m_store->hasFile( "meta.xml" ) )
00110         {
00111             KoXmlDocument metaDoc;
00112             KoOasisStore oasisStore( m_store );
00113             QString errorMsg;
00114             if ( oasisStore.loadAndParse( "meta.xml", metaDoc, errorMsg ) ) {
00115                 KoXmlNode meta   = KoDom::namedItemNS( metaDoc, KoXmlNS::office, "document-meta" );
00116                 KoXmlNode office = KoDom::namedItemNS( meta, KoXmlNS::office, "meta" );
00117                 KoXmlElement generator = KoDom::namedItemNS( office, KoXmlNS::meta, "generator" );
00118                 if ( !generator.isNull() )
00119                     m_generator = generator.text();
00120             }
00121         }
00122         m_metaXmlParsed = true;
00123     }
00124 }

Généré le Wed Nov 22 23:41:03 2006 pour KPlato par  doxygen 1.5.1-p1