Warning, file /office/calligra/libs/text/KoBookmark.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) 2007-2008 Fredy Yanardi <fyanardi@gmail.com> 0003 * Copyright (C) 2011 Boudewijn Rempt <boud@kogmbh.com> 0004 * Copyright (C) 2012 C. Boemann <cbo@boemann.dk> 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 #include "KoBookmark.h" 0023 0024 #include <KoShapeSavingContext.h> 0025 #include <KoXmlWriter.h> 0026 #include <KoXmlReader.h> 0027 #include <KoTextInlineRdf.h> 0028 #include <KoTextRangeManager.h> 0029 #include <KoXmlNS.h> 0030 0031 #include <QTextDocument> 0032 #include <QTextBlock> 0033 #include <QTextCursor> 0034 #include "TextDebug.h" 0035 0036 // Include Q_UNSUSED classes, for building on Windows 0037 #include <KoShapeLoadingContext.h> 0038 0039 class Q_DECL_HIDDEN KoBookmark::Private 0040 { 0041 public: 0042 Private(const QTextDocument *doc) 0043 : document(doc) 0044 { 0045 } 0046 const QTextDocument *document; 0047 QString name; 0048 }; 0049 0050 KoBookmark::KoBookmark(const QTextCursor &cursor) 0051 : KoTextRange(cursor), 0052 d(new Private(cursor.block().document())) 0053 { 0054 } 0055 0056 KoBookmark::~KoBookmark() 0057 { 0058 delete d; 0059 } 0060 0061 void KoBookmark::setName(const QString &name) 0062 { 0063 d->name = name; 0064 } 0065 0066 QString KoBookmark::name() const 0067 { 0068 return d->name; 0069 } 0070 0071 bool KoBookmark::loadOdf(const KoXmlElement &element, KoShapeLoadingContext &context) 0072 { 0073 Q_UNUSED(context); 0074 0075 QString bookmarkName = element.attribute("name"); 0076 const QString localName(element.localName()); 0077 0078 if (manager()) { 0079 // For cut and paste, make sure that the name is unique. 0080 d->name = createUniqueBookmarkName(manager()->bookmarkManager(), bookmarkName, false); 0081 0082 if (localName == "bookmark" || localName == "bookmark-start") { 0083 setPositionOnlyMode(localName == "bookmark"); 0084 0085 // Add inline Rdf to the bookmark. 0086 if (element.hasAttributeNS(KoXmlNS::xhtml, "property") || element.hasAttribute("id")) { 0087 KoTextInlineRdf* inlineRdf = new KoTextInlineRdf(const_cast<QTextDocument*>(d->document), this); 0088 if (inlineRdf->loadOdf(element)) { 0089 setInlineRdf(inlineRdf); 0090 } 0091 else { 0092 delete inlineRdf; 0093 inlineRdf = 0; 0094 } 0095 } 0096 } 0097 else { 0098 // NOTE: "bookmark-end" is handled in KoTextLoader 0099 // if we ever come here then something pretty weird is going on... 0100 return false; 0101 } 0102 return true; 0103 } 0104 return false; 0105 } 0106 0107 void KoBookmark::saveOdf(KoShapeSavingContext &context, int position, TagType tagType) const 0108 { 0109 KoXmlWriter *writer = &context.xmlWriter(); 0110 0111 if (!hasRange()) { 0112 if (tagType == StartTag) { 0113 writer->startElement("text:bookmark", false); 0114 writer->addAttribute("text:name", d->name.toUtf8()); 0115 if (inlineRdf()) { 0116 inlineRdf()->saveOdf(context, writer); 0117 } 0118 writer->endElement(); 0119 } 0120 } else if ((tagType == StartTag) && (position == rangeStart())) { 0121 writer->startElement("text:bookmark-start", false); 0122 writer->addAttribute("text:name", d->name.toUtf8()); 0123 if (inlineRdf()) { 0124 inlineRdf()->saveOdf(context, writer); 0125 } 0126 writer->endElement(); 0127 } else if ((tagType == EndTag) && (position == rangeEnd())) { 0128 writer->startElement("text:bookmark-end", false); 0129 writer->addAttribute("text:name", d->name.toUtf8()); 0130 writer->endElement(); 0131 } 0132 // else nothing 0133 } 0134 0135 QString KoBookmark::createUniqueBookmarkName(const KoBookmarkManager* bmm, const QString &bookmarkName, bool isEndMarker) 0136 { 0137 QString ret = bookmarkName; 0138 int uniqID = 0; 0139 0140 while (true) { 0141 if (bmm->bookmark(ret)) { 0142 ret = QString("%1_%2").arg(bookmarkName).arg(++uniqID); 0143 } else { 0144 if (isEndMarker) { 0145 --uniqID; 0146 if (!uniqID) 0147 ret = bookmarkName; 0148 else 0149 ret = QString("%1_%2").arg(bookmarkName).arg(uniqID); 0150 } 0151 break; 0152 } 0153 } 0154 return ret; 0155 } 0156