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

Aller à la documentation de ce fichier.
00001 /* This file is part of the KDE project
00002    Copyright (C) 1998, 1999, 2000 Torben Weis <weis@kde.org>
00003    Copyright (C) 2004 David Faure <faure@kde.org>
00004 
00005    This library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Library General Public
00007    License as published by the Free Software Foundation; either
00008    version 2 of the License, or (at your option) any later version.
00009 
00010    This library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Library General Public License for more details.
00014 
00015    You should have received a copy of the GNU Library General Public License
00016    along with this library; see the file COPYING.LIB.  If not, write to
00017    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00018  * Boston, MA 02110-1301, USA.
00019 */
00020 
00021 #include "KoDocumentInfo.h"
00022 #include "KoDom.h"
00023 #include "KoDocument.h"
00024 #include "kofficeversion.h"
00025 
00026 #include <QDateTime>
00027 #include <KoStoreDevice.h>
00028 #include <KoXmlWriter.h>
00029 
00030 #include <kconfig.h>
00031 #include <kdebug.h>
00032 #include <kglobal.h>
00033 #include <klocale.h>
00034 #include <kuser.h>
00035 
00036 #include "KoXmlNS.h"
00037 
00038 KoDocumentInfo::KoDocumentInfo( QObject* parent ) : QObject( parent )
00039 {
00040     m_aboutTags << "title" << "description" << "subject" << "comments"
00041         << "keyword" << "initial-creator" << "editing-cycles"
00042         << "date" << "creation-date";
00043 
00044     m_authorTags << "creator" << "initial" << "author-title"
00045         << "email" << "telephone" << "telephone-work"
00046         << "fax" << "country" << "postal-code" << "city"
00047         << "street" << "position" << "company";
00048 
00049     setAboutInfo( "editing-cycles", "0" );
00050     setAboutInfo( "initial-creator", i18n( "Unknown" ) );
00051     setAboutInfo( "creation-date", QDateTime::currentDateTime()
00052             .toString( Qt::ISODate ) );
00053     KUser user( KUser::UseRealUserID );
00054     setAuthorInfo( "creator", user.fullName() );
00055     m_firstSave = false;
00056 }
00057 
00058 KoDocumentInfo::~KoDocumentInfo()
00059 {
00060 }
00061 
00062 bool KoDocumentInfo::load( const KoXmlDocument& doc )
00063 {
00064     if( !loadAboutInfo( doc.documentElement() ) )
00065         return false;
00066 
00067     if( !loadAuthorInfo( doc.documentElement() ) )
00068         return false;
00069 
00070     return true;
00071 }
00072 
00073 bool KoDocumentInfo::loadOasis( const KoXmlDocument& metaDoc )
00074 {
00075     KoXmlNode t = KoDom::namedItemNS( metaDoc, KoXmlNS::office, "document-meta" );
00076     KoXmlNode office = KoDom::namedItemNS( t, KoXmlNS::office, "meta" );
00077 
00078     if( office.isNull() )
00079         return false;
00080 
00081     if( !loadOasisAboutInfo( office ) )
00082         return false;
00083 
00084     if( !loadOasisAuthorInfo( office ) )
00085         return false;
00086 
00087     return true;
00088 }
00089 
00090 QDomDocument KoDocumentInfo::save()
00091 {
00092     saveParameters();
00093 
00094     QDomDocument doc = KoDocument::createDomDocument( "document-info"
00095             /*DTD name*/, "document-info" /*tag name*/, "1.1" );
00096 
00097     QDomElement s = saveAboutInfo( doc );
00098     if ( !s.isNull() )
00099         doc.documentElement().appendChild( s );
00100 
00101     s = saveAuthorInfo( doc );
00102     if ( !s.isNull() )
00103         doc.documentElement().appendChild( s );
00104 
00105 
00106     if( doc.documentElement().isNull() )
00107         return QDomDocument();
00108 
00109     return doc;
00110 }
00111 
00112 bool KoDocumentInfo::saveOasis( KoStore* store )
00113 {
00114     saveParameters();
00115 
00116     KoStoreDevice dev( store );
00117     KoXmlWriter* xmlWriter = KoDocument::createOasisXmlWriter( &dev,
00118             "office:document-meta" );
00119     xmlWriter->startElement( "office:meta" );
00120 
00121     xmlWriter->startElement( "meta:generator");
00122     xmlWriter->addTextNode( QString( "KOffice/%1" )
00123             .arg( KOFFICE_VERSION_STRING ) );
00124     xmlWriter->endElement();
00125 
00126     if( !saveOasisAboutInfo( *xmlWriter ) )
00127         return false;
00128     if( !saveOasisAuthorInfo( *xmlWriter ) )
00129         return false;
00130 
00131     xmlWriter->endElement();
00132     xmlWriter->endElement(); // root element
00133     xmlWriter->endDocument();
00134     delete xmlWriter;
00135     return true;
00136 }
00137 
00138 void KoDocumentInfo::setAuthorInfo( const QString& info, const QString& data )
00139 {
00140     if( !m_authorTags.contains( info ) )
00141         return;
00142 
00143     m_authorInfo.insert( info, data );
00144 }
00145 
00146 QString KoDocumentInfo::authorInfo( const QString& info ) const
00147 {
00148     if( !m_authorTags.contains( info ) )
00149         return QString();
00150 
00151     return m_authorInfo[ info ];
00152 }
00153 
00154 void KoDocumentInfo::setAboutInfo( const QString& info, const QString& data )
00155 {
00156     m_aboutInfo.insert( info, data );
00157 }
00158 
00159 QString KoDocumentInfo::aboutInfo( const QString& info ) const
00160 {
00161     if( !m_aboutTags.contains( info ) )
00162     {
00163         kWarning() << info + " page not found in documentInfo !" << endl;
00164         return QString();
00165     }
00166 
00167     return m_aboutInfo[ info ];
00168 }
00169 
00170 bool KoDocumentInfo::saveOasisAuthorInfo( KoXmlWriter &xmlWriter )
00171 {
00172     foreach( QString tag, m_authorTags )
00173     {
00174         if( !authorInfo( tag ).isEmpty() && tag == "creator" )
00175         {
00176             xmlWriter.startElement( "dc:creator");
00177             xmlWriter.addTextNode( authorInfo( "creator" ) );
00178             xmlWriter.endElement();
00179         }
00180         else if( !authorInfo( tag ).isEmpty() )
00181         {
00182             xmlWriter.startElement( "meta:user-defined");
00183             xmlWriter.addAttribute( "meta:name", tag );
00184             xmlWriter.addTextNode( authorInfo( tag ) );
00185             xmlWriter.endElement();
00186         }
00187     }
00188 
00189     return true;
00190 }
00191 
00192 bool KoDocumentInfo::loadOasisAuthorInfo( const KoXmlNode& metaDoc )
00193 {
00194     KoXmlElement e = KoDom::namedItemNS( metaDoc, KoXmlNS::dc, "creator" );
00195     if ( !e.isNull() && !e.text().isEmpty() )
00196         setAuthorInfo( "creator", e.text() );
00197 
00198     KoXmlNode n = metaDoc.firstChild();
00199     for ( ; !n.isNull(); n = n.nextSibling() )
00200     {
00201         if ( !n.isElement())
00202             continue;
00203 
00204         KoXmlElement e = n.toElement();
00205         if ( !( e.namespaceURI() == KoXmlNS::meta &&
00206                     e.localName() == "user-defined" && !e.text().isEmpty() ) )
00207             continue;
00208 
00209         QString name = e.attributeNS( KoXmlNS::meta, "name", QString::null );
00210         setAuthorInfo( name, e.text() );
00211     }
00212 
00213     return true;
00214 }
00215 
00216 bool KoDocumentInfo::loadAuthorInfo( const KoXmlElement& e )
00217 {
00218     KoXmlNode n = e.namedItem( "author" ).firstChild();
00219     for( ; !n.isNull(); n = n.nextSibling() )
00220     {
00221         KoXmlElement e = n.toElement();
00222         if( e.isNull() )
00223             continue;
00224 
00225         if( e.tagName() == "full-name" )
00226             setAuthorInfo( "creator", e.text() );
00227         else
00228             setAuthorInfo( e.tagName(), e.text() );
00229     }
00230 
00231     return true;
00232 }
00233 
00234 QDomElement KoDocumentInfo::saveAuthorInfo( QDomDocument& doc )
00235 {
00236     QDomElement e = doc.createElement( "author" );
00237     QDomElement t;
00238 
00239     foreach( QString tag, m_authorTags )
00240     {
00241         if( tag == "creator" )
00242             t = doc.createElement( "full-name" );
00243         else
00244             t = doc.createElement( tag );
00245 
00246         e.appendChild( t );
00247         t.appendChild( doc.createTextNode( authorInfo( tag ) ) );
00248     }
00249 
00250     return e;
00251 }
00252 
00253 bool KoDocumentInfo::saveOasisAboutInfo( KoXmlWriter &xmlWriter )
00254 {
00255     saveParameters();
00256 
00257     foreach( QString tag, m_aboutTags )
00258     {
00259         if( !aboutInfo( tag ).isEmpty() || tag == "title" )
00260         {
00261             if( tag == "keyword" )
00262             {
00263                 foreach( QString tmp, aboutInfo( "keyword" ).split(";") )
00264                 {
00265                     xmlWriter.startElement( "meta:keyword" );
00266                     xmlWriter.addTextNode( tmp );
00267                     xmlWriter.endElement();
00268                 }
00269             }
00270             else if( tag == "title" || tag == "description" || tag == "subject" ||
00271                     tag == "date" )
00272             {
00273               if ( tag == "title" && aboutInfo( tag ).isEmpty() )
00274                 {
00275                   KoDocument* doc = static_cast< KoDocument* >( parent() );
00276                   setAboutInfo( "title", doc->url().fileName() );
00277                 }
00278                 QByteArray elementName( QString( "dc:" + tag ).toLatin1() );
00279                 xmlWriter.startElement( elementName );
00280                 xmlWriter.addTextNode( aboutInfo( tag ) );
00281                 xmlWriter.endElement();
00282             }
00283             else
00284             {
00285                 QByteArray elementName( QString( "meta:" + tag).toLatin1() );
00286                 xmlWriter.startElement( elementName );
00287                 xmlWriter.addTextNode( aboutInfo( tag ) );
00288                 xmlWriter.endElement();
00289             }
00290         }
00291     }
00292 
00293     return true;
00294 }
00295 
00296 bool KoDocumentInfo::loadOasisAboutInfo( const KoXmlNode& metaDoc )
00297 {
00298     KoXmlElement e;
00299 
00300     foreach( QString tag, m_aboutTags )
00301     {
00302         if( tag == "keyword" )
00303         {
00304             // this aren't all tags -> FIXME
00305             e  = KoDom::namedItemNS( metaDoc, KoXmlNS::meta, tag.toLatin1().constData() );
00306 
00307             if ( !e.isNull() && !e.text().isEmpty() )
00308                 setAboutInfo( tag, e.text() );
00309         }
00310         else if( tag == "title" || tag == "description" || tag == "subject" ||
00311                 tag == "date" )
00312         {
00313             e  = KoDom::namedItemNS( metaDoc, KoXmlNS::dc, tag.toLatin1().constData() );
00314 
00315             if ( !e.isNull() && !e.text().isEmpty() )
00316                 setAboutInfo( tag, e.text() );
00317         }
00318         else
00319         {
00320             e  = KoDom::namedItemNS( metaDoc, KoXmlNS::meta, tag.toLatin1().constData() );
00321 
00322             if ( !e.isNull() && !e.text().isEmpty() )
00323                 setAboutInfo( tag, e.text() );
00324         }
00325     }
00326 
00327     return true;
00328 }
00329 
00330 bool KoDocumentInfo::loadAboutInfo( const KoXmlElement& e )
00331 {
00332     KoXmlNode n = e.namedItem( "about" ).firstChild();
00333     KoXmlElement tmp;
00334     for( ; !n.isNull(); n = n.nextSibling()  )
00335     {
00336         tmp = n.toElement();
00337         if ( tmp.isNull() )
00338             continue;
00339 
00340         if ( tmp.tagName() == "abstract" )
00341             setAboutInfo( "comments", tmp.text() );
00342 
00343         setAboutInfo( tmp.tagName(), tmp.text() );
00344     }
00345 
00346     return true;
00347 }
00348 
00349 QDomElement KoDocumentInfo::saveAboutInfo( QDomDocument& doc )
00350 {
00351     saveParameters();
00352 
00353     QDomElement e = doc.createElement( "about" );
00354     QDomElement t;
00355 
00356     foreach( QString tag, m_aboutTags )
00357     {
00358         if( tag == "comments" )
00359         {
00360             t = doc.createElement( "abstract" );
00361             e.appendChild( t );
00362             t.appendChild( doc.createCDATASection( aboutInfo( tag ) ) );
00363         }
00364         else
00365         {
00366             t = doc.createElement( tag );
00367             e.appendChild( t );
00368             t.appendChild( doc.createTextNode( aboutInfo( tag ) ) );
00369         }
00370     }
00371 
00372     return e;
00373 }
00374 
00375 void KoDocumentInfo::saveParameters()
00376 {
00377     KoDocument* doc = dynamic_cast< KoDocument* >( parent() );
00378     if ( doc && doc->isAutosaving() )
00379         return;
00380 
00381     int cycles = aboutInfo( "editing-cycles" ).toInt();
00382     setAboutInfo( "editing-cycles", QString::number( cycles ) );
00383     if(m_firstSave) {
00384         cycles++;
00385         m_firstSave = false;
00386     }
00387     setAboutInfo( "date", QDateTime::currentDateTime().toString( Qt::ISODate ) );
00388 }
00389 
00390 void KoDocumentInfo::resetMetaData()
00391 {
00392     setAboutInfo( "editing-cycles", QString::number( 0 ) );
00393     setAboutInfo( "initial-creator", authorInfo( "creator" ) );
00394     setAboutInfo( "creation-date", QDateTime::currentDateTime().toString( Qt::ISODate ) );
00395 }
00396 
00397 #include "KoDocumentInfo.moc"

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