Warning, file /office/calligra/libs/odf/KoGenChange.cpp 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 #include "KoGenChange.h" 0021 0022 #include <KoXmlWriter.h> 0023 #include <QDateTime> 0024 0025 #include <OdfDebug.h> 0026 0027 // Returns -1, 0 (equal) or 1 0028 static int compareMap(const QMap<QString, QString> &map1, const QMap<QString, QString> &map2) 0029 { 0030 QMap<QString, QString>::const_iterator it = map1.begin(); 0031 QMap<QString, QString>::const_iterator oit = map2.begin(); 0032 for (; it != map1.end(); ++it, ++oit) { // both maps have been checked for size already 0033 if (it.key() != oit.key()) 0034 return it.key() < oit.key() ? -1 : + 1; 0035 if (it.value() != oit.value()) 0036 return it.value() < oit.value() ? -1 : + 1; 0037 } 0038 return 0; // equal 0039 } 0040 0041 0042 KoGenChange::KoGenChange(KoGenChange::ChangeFormat changeFormat) 0043 : m_changeFormat(changeFormat) 0044 , m_type(UNKNOWN) 0045 { 0046 } 0047 0048 KoGenChange::~KoGenChange() 0049 { 0050 } 0051 0052 void KoGenChange::writeChangeMetaData(KoXmlWriter* writer) const 0053 { 0054 QMap<QString, QString>::const_iterator it = m_changeMetaData.begin(); 0055 const QMap<QString, QString>::const_iterator end = m_changeMetaData.end(); 0056 for (; it != end; ++it) { 0057 //FIXME: if the propName is passed directly as it.key().toUtf8(), the opening tag is correct but the closing tag becomes undefined 0058 //FIXME: example: <dc-creator>.......</`ok> 0059 0060 if (it.key() == "dc-creator") { 0061 writer->startElement("dc:creator"); 0062 writer->addTextNode(it.value()); 0063 writer->endElement(); 0064 } 0065 if (it.key() == "dc-date") { 0066 writer->startElement("dc:date"); 0067 writer->addTextNode(it.value()); 0068 writer->endElement(); 0069 } 0070 } 0071 } 0072 0073 void KoGenChange::writeChange(KoXmlWriter *writer, const QString &name) const 0074 { 0075 if (m_changeFormat == KoGenChange::ODF_1_2) { 0076 writeODF12Change(writer, name); 0077 } else { 0078 writeDeltaXmlChange(writer, name); 0079 } 0080 } 0081 0082 void KoGenChange::writeODF12Change(KoXmlWriter *writer, const QString &name) const 0083 { 0084 writer->startElement("text:changed-region"); 0085 writer->addAttribute("text:id", name); 0086 writer->addAttribute("xml:id", name); 0087 0088 const char* elementName; 0089 switch (m_type) { 0090 case KoGenChange::DeleteChange: 0091 elementName = "text:deletion"; 0092 break; 0093 case KoGenChange::FormatChange: 0094 elementName = "text:format-change"; 0095 break; 0096 case KoGenChange::InsertChange: 0097 elementName = "text:insertion"; 0098 break; 0099 default: 0100 elementName = "text:format-change"; //should not happen, format-change is probably the most harmless of the three. 0101 } 0102 writer->startElement(elementName); 0103 if (!m_changeMetaData.isEmpty()) { 0104 writer->startElement("office:change-info"); 0105 writeChangeMetaData(writer); 0106 if (m_literalData.contains("changeMetaData")) 0107 writer->addCompleteElement(m_literalData.value("changeMetaData").toUtf8()); 0108 writer->endElement(); // office:change-info 0109 } 0110 if ((m_type == KoGenChange::DeleteChange) && m_literalData.contains("deleteChangeXml")) 0111 writer->addCompleteElement(m_literalData.value("deleteChangeXml").toUtf8()); 0112 0113 writer->endElement(); // text:insertion/format/deletion 0114 writer->endElement(); // text:change 0115 } 0116 0117 void KoGenChange::writeDeltaXmlChange(KoXmlWriter *writer, const QString &name) const 0118 { 0119 writer->startElement("delta:change-transaction"); 0120 writer->addAttribute("delta:change-id", name); 0121 if (!m_changeMetaData.isEmpty()) { 0122 writer->startElement("delta:change-info"); 0123 writeChangeMetaData(writer); 0124 writer->endElement(); // delta:change-info 0125 } 0126 writer->endElement(); // delta:change-transaction 0127 } 0128 0129 bool KoGenChange::operator<(const KoGenChange &other) const 0130 { 0131 Q_UNUSED(other); 0132 // if (m_changeMetaData.value("dc-date") != other.m_changeMetaData.value("dc-date")) return QDateTime::fromString(m_changeMetaData.value("dc-date"), Qt::ISODate) < QDateTime::fromString(other.m_changeMetaData.value("dc-date"), Qt::ISODate); 0133 0134 0135 return true; 0136 } 0137 0138 bool KoGenChange::operator==(const KoGenChange &other) const 0139 { 0140 if (m_type != other.m_type) return false; 0141 if (m_changeMetaData.count() != other.m_changeMetaData.count()) return false; 0142 if (m_literalData.count() != other.m_literalData.count()) return false; 0143 int comp = compareMap(m_changeMetaData, other.m_changeMetaData); 0144 if (comp != 0) return false; 0145 return (compareMap(m_literalData, other.m_literalData) == 0); 0146 }