1 /* This file is part of the KDE project
2   Copyright (C) 2008 Pierre Stirnweiss <pierre.stirnweiss_calligra@gadz.org>
3    Copyright (C) 2010 Thomas Zander <zander@kde.org>
4 
5    This library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Library General Public
7    License as published by the Free Software Foundation; either
8    version 2 of the License, or (at your option) any later version.
9 
10    This library is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    Library General Public License for more details.
14 
15    You should have received a copy of the GNU Library General Public License
16    along with this library; see the file COPYING.LIB.  If not, write to
17    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19 */
20 
21 #include "KoGenChanges.h"
22 #include <KoXmlWriter.h>
23 #include <KoElementReference.h>
24 
25 #include <QList>
26 #include <QMap>
27 #include <QString>
28 
29 #include <OdfDebug.h>
30 
31 class Q_DECL_HIDDEN KoGenChanges::Private
32 {
33 public:
Private(KoGenChanges * q)34     Private(KoGenChanges *q)
35        : q(q)
36     { }
37 
38 
39     struct NamedChange {
40         const KoGenChange* change; ///< @note owned by the collection
41         QString name;
42     };
43 
44     /// style definition -> name
45     QMap<KoGenChange, QString>  changeMap;
46 
47     /// List of styles (used to preserve ordering)
48     QList<NamedChange> changeArray;
49 
50     QMap<KoGenChange, QString> ::iterator insertChange(const KoGenChange &change);
51 
52     KoGenChanges *q;
53 };
54 
KoGenChanges()55 KoGenChanges::KoGenChanges()
56     : d(new Private(this))
57 {
58 }
59 
~KoGenChanges()60 KoGenChanges::~KoGenChanges()
61 {
62     delete d;
63 }
64 
insert(const KoGenChange & change)65 QString KoGenChanges::insert(const KoGenChange& change)
66 {
67     QMap<KoGenChange, QString> ::iterator it = d->changeMap.find(change);
68     if (it == d->changeMap.end()) {
69         it = d->insertChange(change);
70     }
71     return it.value();
72 }
73 
insertChange(const KoGenChange & change)74 QMap<KoGenChange, QString>::iterator KoGenChanges::Private::insertChange(const KoGenChange &change)
75 {
76     QString changeName;
77     switch (change.type()) {
78     case KoGenChange::InsertChange: changeName = 'I'; break;
79     case KoGenChange::FormatChange: changeName = 'F'; break;
80     case KoGenChange::DeleteChange: changeName = 'D'; break;
81     default:
82         changeName = 'C';
83     }
84     KoElementReference ref(changeName);
85     changeName = ref.toString();
86 
87     QMap<KoGenChange, QString>::iterator it = changeMap.insert(change, changeName);
88     NamedChange s;
89     s.change = &it.key();
90     s.name = changeName;
91     changeArray.append(s);
92 
93     return it;
94 }
95 
saveOdfChanges(KoXmlWriter * xmlWriter,bool trackChanges) const96 void KoGenChanges::saveOdfChanges(KoXmlWriter* xmlWriter, bool trackChanges) const
97 {
98     QMap<KoGenChange, QString>::const_iterator it = d->changeMap.constBegin();
99 
100     if ((it != d->changeMap.constEnd()) && (it.key().changeFormat() == KoGenChange::DELTAXML)) {
101         xmlWriter->startElement("delta:tracked-changes");
102     } else {
103         xmlWriter->startElement("text:tracked-changes");
104         xmlWriter->addAttribute("text:track-changes", trackChanges);
105    }
106 
107     for (; it != d->changeMap.constEnd() ; ++it) {
108         KoGenChange change = it.key();
109         change.writeChange(xmlWriter, it.value());
110     }
111 
112     xmlWriter->endElement(); // text:tracked-changes
113 }
114