File indexing completed on 2024-12-08 12:56:11

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