File indexing completed on 2024-05-12 16:36:42

0001 /* This file is part of the KDE project
0002 *  Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
0003 *
0004 *  Contact: Amit Aggarwal <amitcs06@gmail.com> 
0005 *            <amit.5.aggarwal@nokia.com>
0006 *
0007 *  Copyright (C) 2010 Thorsten Zachmann <zachmann@kde.org>
0008 *
0009 *  This library is free software; you can redistribute it and/or
0010 *  modify it under the terms of the GNU Library General Public
0011 *  License as published by the Free Software Foundation; either
0012 *  version 2 of the License, or (at your option) any later version.
0013 *
0014 *  This library is distributed in the hope that it will be useful,
0015 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
0016 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0017 *  Library General Public License for more details.
0018 *
0019 *  You should have received a copy of the GNU Library General Public License
0020 *  along with this library; see the file COPYING.LIB.  If not, write to
0021 *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0022 *  Boston, MA 02110-1301, USA.
0023 */
0024 #include "KPrDeclarations.h"
0025 #include <QDateTime>
0026 #include <QVariant>
0027 #include <KoXmlNS.h>
0028 #include <KoXmlWriter.h>
0029 #include <KoXmlReader.h>
0030 #include <KoPALoadingContext.h>
0031 #include <KoPASavingContext.h>
0032 #include <KoOdfStylesReader.h>
0033 #include <KoOdfLoadingContext.h>
0034 #include <KoOdfNumberStyles.h>
0035 
0036 KPrDeclarations::KPrDeclarations()
0037 {
0038 }
0039 
0040 KPrDeclarations::~KPrDeclarations()
0041 {
0042 }
0043 
0044 bool KPrDeclarations::loadOdf(const KoXmlElement &body, KoPALoadingContext &context)
0045 {
0046     Q_UNUSED(context);
0047 
0048     KoXmlElement element;
0049     forEachElement( element, body ) {
0050         if (element.namespaceURI() == KoXmlNS::presentation) {
0051             if (element.tagName() == "header-decl") {
0052                 const QString name = element.attributeNS(KoXmlNS::presentation, "name", QString());
0053                 m_declarations[Header].insert(name, element.text());
0054             }
0055             else if(element.tagName() == "footer-decl") {
0056                 const QString name = element.attributeNS(KoXmlNS::presentation, "name", QString());
0057                 m_declarations[Footer].insert(name, element.text());
0058             }
0059             else if(element.tagName() == "date-time-decl") {
0060                 QMap<QString, QVariant> data;
0061                 const QString name = element.attributeNS(KoXmlNS::presentation, "name", QString());
0062                 data["fixed"] = element.attributeNS(KoXmlNS::presentation, "source", "fixed") == "fixed";
0063 
0064                 QString styleName = element.attributeNS(KoXmlNS::style, "data-style-name", "");
0065                 if (!styleName.isEmpty()) {
0066                     KoOdfStylesReader::DataFormatsMap::const_iterator it = context.odfLoadingContext().stylesReader().dataFormats().constFind(styleName);
0067                     if (it != context.odfLoadingContext().stylesReader().dataFormats().constEnd()) {
0068 
0069                         QString formatString = (*it).first.prefix + (*it).first.formatStr + (*it).first.suffix;
0070                         data["format"] = formatString;
0071                     }
0072                 }
0073                 else {
0074                     data["format"] = QString("");
0075                     data["fixed value"] = element.text();
0076                 }
0077 
0078                 m_declarations[DateTime].insert(name, data);
0079             }
0080         }
0081         else if (element.tagName() == "page" && element.namespaceURI() == KoXmlNS::draw) {
0082             break;
0083         }
0084     }
0085     return true;
0086 }
0087 
0088 
0089 bool KPrDeclarations::saveOdf(KoPASavingContext &paContext) const
0090 {
0091     /*
0092        <presentation:header-decl presentation:name="hdr1">header</presentation:header-decl>
0093        <presentation:footer-decl presentation:name="ftr1">Footer for the slide</presentation:footer-decl>
0094        <presentation:footer-decl presentation:name="ftr2">footer</presentation:footer-decl>
0095        <presentation:date-time-decl presentation:name="dtd1" presentation:source="current-date" style:data-style-name="D3"/>
0096     */
0097     KoXmlWriter &writer(paContext.xmlWriter());
0098 
0099     QHash<Type, QHash<QString, QVariant> >::const_iterator typeIt(m_declarations.constBegin());
0100     for (; typeIt != m_declarations.constEnd(); ++typeIt) {
0101         QHash<QString, QVariant>::const_iterator keyIt(typeIt.value().begin());
0102         for (; keyIt != typeIt.value().constEnd(); ++keyIt) {
0103             switch (typeIt.key()) {
0104             case Footer:
0105                 writer.startElement("presentation:footer-decl");
0106                 break;
0107             case Header:
0108                 writer.startElement("presentation:header-decl");
0109                 break;
0110             case DateTime:
0111                 writer.startElement("presentation:date-time-decl");
0112                 break;
0113             }
0114 
0115             writer.addAttribute("presentation:name", keyIt.key());
0116             if (typeIt.key() == DateTime) {
0117                 const QMap<QString, QVariant> data = keyIt.value().value<QMap<QString, QVariant> >();
0118                 bool fixed = data["fixed"].toBool();
0119                 writer.addAttribute("presentation:source", fixed ? "fixed" : "current-date");
0120                 QString format = data["format"].toString();
0121                 if (format.isEmpty()) {
0122                     writer.addTextNode(data["fixed value"].toString());
0123                 }
0124                 else {
0125                     QString styleName = KoOdfNumberStyles::saveOdfDateStyle(paContext.mainStyles(), format, false);
0126                     writer.addAttribute("style:data-style-name", styleName);
0127                 }
0128             }
0129             else {
0130                 writer.addTextNode(keyIt.value().value<QString>());
0131             }
0132             writer.endElement();
0133         }
0134     }
0135     return true;
0136 }
0137 
0138 const QString KPrDeclarations::declaration(Type type, const QString &key)
0139 {
0140     QString retVal;
0141     if (type == DateTime) {
0142         QMap<QString, QVariant> dateTimeDefinition =
0143                 m_declarations.value(type).value(key).value<QMap<QString, QVariant> >();
0144 
0145         // if there is no presentation declaration don't set a value
0146         if (!dateTimeDefinition.isEmpty()) {
0147             if (dateTimeDefinition["fixed"].toBool()) {
0148                 retVal = dateTimeDefinition["fixed value"].toString();
0149             }
0150             else  {
0151                 QDateTime target = QDateTime::currentDateTime();
0152 
0153                 QString formatString = dateTimeDefinition["format"].toString();
0154                 if (!formatString.isEmpty()) {
0155                     retVal = target.toString(formatString);
0156                 }
0157                 else {
0158                     // XXX: What do we do here?
0159                     retVal = target.date().toString(Qt::ISODate);
0160                 }
0161             }
0162         }
0163     }
0164     else {
0165         retVal = m_declarations.value(type).value(key).toString();
0166     }
0167     return retVal;
0168 }