File indexing completed on 2024-12-01 13:11:44

0001 /* This file is part of the KDE project
0002  *
0003  * Copyright (C) 2014 Inge Wallin <inge@lysator.liu.se>
0004  *
0005  * This library is free software; you can redistribute it and/or
0006  * modify it under the terms of the GNU Library General Public
0007  * License as published by the Free Software Foundation; either
0008  * version 2 of the License, or (at your option) any later version.
0009  *
0010  * This library is distributed in the hope that it will be useful,
0011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0013  * Library General Public License for more details.
0014  *
0015  * You should have received a copy of the GNU Library General Public License
0016  * along with this library; see the file COPYING.LIB.  If not, write to
0017  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0018  * Boston, MA 02110-1301, USA.
0019  */
0020 
0021 
0022 // Own
0023 #include "KoOdfPageLayout.h"
0024 
0025 // Qt
0026 #include <QString>
0027 
0028 // Odflib
0029 #include "KoXmlStreamReader.h"
0030 #include "KoXmlWriter.h"
0031 #include "KoOdfStyleProperties.h"
0032 #include "KoOdfPageLayoutProperties.h"
0033 #include "KoOdfHeaderFooterProperties.h"
0034 
0035 #include "Odf2Debug.h"
0036 
0037 // ================================================================
0038 //                         class KoOdfPageLayout
0039 
0040 
0041 class Q_DECL_HIDDEN KoOdfPageLayout::Private
0042 {
0043 public:
0044     Private();
0045     ~Private();
0046 
0047     QString pageUsage;
0048 
0049     KoOdfPageLayoutProperties   *pageLayoutProperties;
0050     KoOdfHeaderFooterProperties *headerProperties;
0051     KoOdfHeaderFooterProperties *footerProperties;
0052 };
0053 
0054 KoOdfPageLayout::Private::Private()
0055     : pageLayoutProperties(0)
0056     , headerProperties(0)
0057     , footerProperties(0)
0058 {
0059 }
0060 
0061 KoOdfPageLayout::Private::~Private()
0062 {
0063     delete pageLayoutProperties;
0064     delete headerProperties;
0065     delete footerProperties;
0066 }
0067 
0068 
0069 // ----------------------------------------------------------------
0070 
0071 
0072 KoOdfPageLayout::KoOdfPageLayout()
0073     : KoOdfStyleBase(PageLayout)
0074     , d(new KoOdfPageLayout::Private())
0075 {
0076 }
0077 
0078 KoOdfPageLayout::~KoOdfPageLayout()
0079 {
0080     delete d;
0081 }
0082 
0083 
0084 QString KoOdfPageLayout::pageUsage() const
0085 {
0086     return d->pageUsage;
0087 }
0088 
0089 void KoOdfPageLayout::setPageUsage(const QString &pageUsage)
0090 {
0091     d->pageUsage = pageUsage;
0092 }
0093 
0094 
0095 KoOdfPageLayoutProperties *KoOdfPageLayout::pageLayoutProperties() const
0096 {
0097     if (!d->pageLayoutProperties) {
0098         d->pageLayoutProperties = new KoOdfPageLayoutProperties();
0099     }
0100 
0101     return d->pageLayoutProperties;
0102 }
0103 
0104 KoOdfHeaderFooterProperties *KoOdfPageLayout::headerProperties() const
0105 {
0106     if (!d->headerProperties) {
0107         d->headerProperties = new KoOdfHeaderFooterProperties();
0108     }
0109 
0110     return d->headerProperties;
0111 }
0112 
0113 KoOdfHeaderFooterProperties *KoOdfPageLayout::footerProperties() const
0114 {
0115     if (!d->footerProperties) {
0116         d->footerProperties = new KoOdfHeaderFooterProperties();
0117     }
0118 
0119     return d->footerProperties;
0120 }
0121 
0122 
0123 bool KoOdfPageLayout::readOdf(KoXmlStreamReader &reader)
0124 {
0125     bool retval = true;
0126 
0127     // Load style attributes.
0128     KoXmlStreamAttributes  attrs = reader.attributes();
0129     setName(attrs.value("style:name").toString());
0130     setDisplayName(attrs.value("style:display-name").toString());
0131     setPageUsage(attrs.value("style:page-usage").toString());
0132 
0133     debugOdf2 << "PageLayout:" << name() << displayName() << pageUsage();
0134 
0135     // Load child elements: property sets and other children.
0136     while (reader.readNextStartElement()) {
0137 
0138         // Create a new propertyset variable depending on the type of properties.
0139         const QString propertiesType = reader.qualifiedName().toString();
0140 
0141         KoOdfStyleProperties *properties = 0;
0142         if (propertiesType == "style:page-layout-properties") {
0143             properties = new KoOdfPageLayoutProperties();
0144             if (!properties->readOdf(reader)) {
0145                 delete properties;
0146                 retval = false;
0147                 break;
0148             }
0149             d->pageLayoutProperties = dynamic_cast<KoOdfPageLayoutProperties*>(properties);
0150         }
0151 
0152         else if (propertiesType == "style:header-style") {
0153             // The header/footer properties are contained inside a
0154             // style element so we need to read past that.
0155             reader.readNextStartElement();
0156             if (reader.qualifiedName() != "style:header-footer-properties") {
0157                 reader.skipCurrentElement();
0158                 return false;
0159             }
0160             properties = new KoOdfHeaderFooterProperties();
0161 
0162             if (!properties->readOdf(reader)) {
0163                 delete properties;
0164                 retval = false;
0165                 break;
0166             }
0167             d->headerProperties = dynamic_cast<KoOdfHeaderFooterProperties*>(properties);
0168 
0169             // Read past the end element for the header style;
0170             reader.skipCurrentElement();
0171         }
0172 
0173         else if (propertiesType == "style:footer-style") {
0174             // The header/footer properties are contained inside a
0175             // style element so we need to read past that.
0176             reader.readNextStartElement();
0177             if (reader.qualifiedName() != "style:header-footer-properties") {
0178                 reader.skipCurrentElement();
0179                 return false;
0180             }
0181             properties = new KoOdfHeaderFooterProperties();
0182 
0183             if (!properties->readOdf(reader)) {
0184                 delete properties;
0185                 return false;
0186             }
0187             d->footerProperties = dynamic_cast<KoOdfHeaderFooterProperties*>(properties);
0188 
0189             // Read past the end element for the footer style;
0190             reader.skipCurrentElement();
0191         }
0192     }
0193 
0194     // Skip rest of each element including children that are not read yet (shouldn't be any).
0195     reader.skipCurrentElement();
0196 
0197     return retval;
0198 }
0199 
0200 bool KoOdfPageLayout::saveOdf(KoXmlWriter *writer)
0201 {
0202     if (isDefaultStyle()) {
0203         writer->startElement("style:default-page-layout");
0204     }
0205     else {
0206         writer->startElement("style:page-layout");
0207         writer->addAttribute("style:name", name());
0208     }
0209 
0210     // Write style attributes
0211     writer->addAttribute("style:page-usage", pageUsage());
0212 
0213     // Write properties
0214     if (d->pageLayoutProperties) {
0215         d->pageLayoutProperties->saveOdf("", writer);
0216     }
0217     if (d->headerProperties) {
0218         writer->startElement("style:header-style");
0219         d->headerProperties->saveOdf("", writer);
0220         writer->endElement();  // style:header-style
0221     }
0222     if (d->footerProperties) {
0223         writer->startElement("style:footer-style");
0224         d->footerProperties->saveOdf("", writer);
0225         writer->endElement();  // style:footer-style
0226     }
0227 
0228     writer->endElement();  // style:{default-,}page-layout
0229     return true;
0230 }