Warning, file /office/calligra/libs/odf/KoGenChange.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /* This file is part of the KDE project
0002    Copyright (C) 2008 Pierre Stirnweiss <pierre.stirnweiss_calligra@gadz.org>
0003 
0004    This library is free software; you can redistribute it and/or
0005    modify it under the terms of the GNU Library General Public
0006    License as published by the Free Software Foundation; either
0007    version 2 of the License, or (at your option) any later version.
0008 
0009    This library is distributed in the hope that it will be useful,
0010    but WITHOUT ANY WARRANTY; without even the implied warranty of
0011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012    Library General Public License for more details.
0013 
0014    You should have received a copy of the GNU Library General Public License
0015    along with this library; see the file COPYING.LIB.  If not, write to
0016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017  * Boston, MA 02110-1301, USA.
0018 */
0019 
0020 #ifndef KOGENCHANGE_H
0021 #define KOGENCHANGE_H
0022 
0023 #include <QMap>
0024 #include <QString>
0025 #include "koodf_export.h"
0026 
0027 #include <OdfDebug.h>
0028 
0029 class KoGenChanges;
0030 class KoXmlWriter;
0031 
0032 /**
0033  * A generic change, i.e. basically a collection of properties and a name.
0034  * Instances of KoGenChange can either be held in the KoGenChanges collection,
0035  * or created (e.g. on the stack) and given to KoGenChanges::insert.
0036  *
0037  * Derived from code from KoGenStyle
0038  */
0039 class KOODF_EXPORT KoGenChange
0040 {
0041 public:
0042     /**
0043      * Possible values for the "type" of the KoGenChange.
0044      * If there is a still missing add it here so that it is possible to use the same
0045      * saving code in all applications.
0046      */
0047     enum Type {
0048         InsertChange,
0049         FormatChange,
0050         DeleteChange,
0051         UNKNOWN = 9999
0052     };
0053 
0054     enum ChangeFormat {
0055         ODF_1_2,
0056         DELTAXML
0057     };
0058 
0059     /**
0060      * Start the definition of a new change. Its name will be set later by KoGenChanges::insert(),
0061      * but first you must define its properties and attributes.
0062      *
0063      */
0064     explicit KoGenChange(KoGenChange::ChangeFormat changeFormat = KoGenChange::ODF_1_2);
0065     ~KoGenChange();
0066 
0067     /// Set the type of this change
0068     void setType(KoGenChange::Type type) {
0069         m_type = type;
0070     }
0071 
0072     /// set the format to be used to save changes
0073     void setChangeFormat(KoGenChange::ChangeFormat changeFormat) {
0074         m_changeFormat = changeFormat;
0075     }
0076 
0077     /// Return the type of this style
0078     Type type() const {
0079         return m_type;
0080     }
0081 
0082     /// Return the format to be used to save changes
0083     KoGenChange::ChangeFormat changeFormat() const {
0084         return m_changeFormat;
0085     }
0086 
0087     /// Add a property to the style
0088     void addChangeMetaData(const QString &propName, const QString &propValue) {
0089         m_changeMetaData.insert(propName, propValue);
0090     }
0091 
0092     /// Overloaded version of addProperty that takes a char*, usually for "..."
0093     void addChangeMetaData(const QString &propName, const char *propValue) {
0094         m_changeMetaData.insert(propName, propValue);
0095     }
0096     /// Overloaded version of addProperty that converts an int to a string
0097     void addChangeMetaData(const QString &propName, int propValue) {
0098         m_changeMetaData.insert(propName, QString::number(propValue));
0099     }
0100     /// Overloaded version of addProperty that converts a bool to a string (false/true)
0101     void addChangeMetaData(const QString &propName, bool propValue) {
0102         m_changeMetaData.insert(propName, propValue ? "true" : "false");
0103     }
0104 
0105     /**
0106      * @brief Add a child element to the properties.
0107      *
0108      * What is meant here is that the contents of the QString
0109      * will be written out literally. This means you should use
0110      * KoXmlWriter to generate it:
0111      * @code
0112      * QBuffer buffer;
0113      * buffer.open( QIODevice::WriteOnly );
0114      * KoXmlWriter elementWriter( &buffer );  // TODO pass indentation level
0115      * elementWriter.startElement( "..." );
0116      * ...
0117      * elementWriter.endElement();
0118      * QString elementContents = QString::fromUtf8( buffer.buffer(), buffer.buffer().size() );
0119      * gs.addChildElement( "...", elementContents );
0120      * @endcode
0121      *
0122      * The value of @p elementName isn't used, except that it must be unique.
0123      */
0124     void addChildElement(const QString &elementName, const QString &elementContents) {
0125         m_literalData.insert(elementName, elementContents);
0126     }
0127 
0128     /**
0129      *  Write the definition of this change to @p writer, using the OASIS format.
0130      *  @param writer the KoXmlWriter in which the element will be created and filled in
0131      *  @param name must come from the collection.
0132      */
0133     void writeChange(KoXmlWriter *writer, const QString &name) const;
0134 
0135     /**
0136      *  QMap requires a complete sorting order.
0137      *  Another solution would have been a qdict and a key() here, a la KoTextFormat,
0138      *  but the key was difficult to generate.
0139      *  Solutions with only a hash value (not representative of the whole data)
0140      *  require us to write a hashtable by hand....
0141      */
0142     bool operator<(const KoGenChange &other) const;
0143 
0144     /// Not needed for QMap, but can still be useful
0145     bool operator==(const KoGenChange &other) const;
0146 
0147 private:
0148     QString changeMetaData(const QString &propName) const {
0149         QMap<QString, QString>::const_iterator it = m_changeMetaData.find(propName);
0150         if (it != m_changeMetaData.end())
0151             return it.value();
0152         return QString();
0153     }
0154 
0155     void writeChangeMetaData(KoXmlWriter *writer) const;
0156 
0157     void writeODF12Change(KoXmlWriter *writer, const QString &name) const;
0158 
0159     void writeDeltaXmlChange(KoXmlWriter *writer, const QString &name) const;
0160 
0161 private:
0162     // Note that the copy constructor and assignment operator are allowed.
0163     // Better not use pointers below!
0164     ChangeFormat m_changeFormat;
0165     Type m_type;
0166     /// We use QMaps since they provide automatic sorting on the key (important for unicity!)
0167     QMap<QString, QString> m_changeMetaData;
0168     QMap<QString, QString> m_literalData;
0169 };
0170 
0171 #endif /* KOGENCHANGE_H */