1 /*
2  *  Copyright (c) 2011 Boudewijn Rempt <boud@valdyas.org>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with this library; see the file COPYING.LIB.  If not, write to
16  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  */
19 
20 // clazy:excludeall=qstring-arg
21 #include "KoElementReference.h"
22 
23 #include "KoXmlReader.h"
24 #include "KoXmlWriter.h"
25 #include <KoXmlNS.h>
26 
KoElementReference()27 KoElementReference::KoElementReference()
28     : d(new KoElementReferenceData())
29 {
30     d->xmlid = "id-" + d->xmlid;
31 }
32 
KoElementReference(const QString & prefix)33 KoElementReference::KoElementReference(const QString &prefix)
34     : d(new KoElementReferenceData)
35 {
36     d->xmlid = prefix + "-" + d->xmlid;
37 }
38 
KoElementReference(const QString & prefix,int counter)39 KoElementReference::KoElementReference(const QString &prefix, int counter)
40     : d(new KoElementReferenceData)
41 {
42     d->xmlid = QString("%1-%2").arg(prefix).arg(counter);
43 }
44 
KoElementReference(const KoElementReference & other)45 KoElementReference::KoElementReference(const KoElementReference &other)
46     : d(other.d)
47 {
48 }
49 
operator =(const KoElementReference & rhs)50 KoElementReference &KoElementReference::operator=(const KoElementReference &rhs)
51 {
52     if (this == &rhs) return *this;
53     d = rhs.d;
54 
55     return *this;
56 }
57 
operator ==(const KoElementReference & other) const58 bool KoElementReference::operator==(const KoElementReference &other) const
59 {
60     return d->xmlid == other.d->xmlid;
61 }
62 
operator !=(const KoElementReference & other) const63 bool KoElementReference::operator!=(const KoElementReference &other) const
64 {
65     return !(*this == other);
66 }
67 
isValid() const68 bool KoElementReference::isValid() const
69 {
70     return (!d->xmlid.isEmpty());
71 }
72 
saveOdf(KoXmlWriter * writer,SaveOption saveOptions) const73 void KoElementReference::saveOdf(KoXmlWriter *writer, SaveOption saveOptions) const
74 {
75     if (d->xmlid.isEmpty()) return;
76 
77     writer->addAttribute("xml:id", d->xmlid);
78 
79     if (saveOptions & DrawId) {
80         writer->addAttribute("draw:id", d->xmlid);
81     }
82     if (saveOptions & TextId) {
83         writer->addAttribute("text:id", d->xmlid);
84     }
85 }
86 
toString() const87 QString KoElementReference::toString() const
88 {
89     return d->xmlid;
90 }
91 
92 
loadOdf(const KoXmlElement & element)93 KoElementReference KoElementReference::loadOdf(const KoXmlElement &element)
94 {
95     QString xmlid;
96 
97     if (element.hasAttributeNS(KoXmlNS::xml, "id")) {
98         xmlid = element.attributeNS(KoXmlNS::xml, "id");
99     }
100     else if (element.hasAttributeNS(KoXmlNS::draw, "id")) {
101         xmlid = element.attributeNS(KoXmlNS::draw, "id");
102     }
103     else if (element.hasAttributeNS(KoXmlNS::text, "id")) {
104         xmlid = element.attributeNS(KoXmlNS::text, "id");
105     }
106 
107     d->xmlid = xmlid;
108 
109     return *this;
110 }
111 
invalidate()112 void KoElementReference::invalidate()
113 {
114     d->xmlid.clear();
115 }
116