Warning, file /office/calligra/libs/text/KoBibliographyInfo.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) 2011 Smit Patel <smitpatel24@gmail.com>
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 "KoBibliographyInfo.h"
0021 
0022 #include <KoXmlReader.h>
0023 #include <KoXmlWriter.h>
0024 #include <KoXmlNS.h>
0025 #include <KoTextSharedLoadingData.h>
0026 #include <KoParagraphStyle.h>
0027 #include <KoOdfBibliographyConfiguration.h>
0028 
0029 int KoBibliographyInfo::styleNameToStyleId(KoTextSharedLoadingData *sharedLoadingData, const QString &styleName)
0030 {
0031     KoParagraphStyle * style = sharedLoadingData->paragraphStyle(styleName, true);
0032     if (style) {
0033         return style->styleId();
0034     }
0035 
0036     return 0;
0037 }
0038 
0039 KoBibliographyInfo::KoBibliographyInfo()
0040   : m_generator(0)
0041 {
0042 }
0043 
0044 KoBibliographyInfo::~KoBibliographyInfo()
0045 {
0046     foreach (const BibliographyEntryTemplate &entryTemplate, m_entryTemplate) {
0047         qDeleteAll(entryTemplate.indexEntries);
0048     }
0049     delete m_generator;
0050     m_generator = 0; // just to be safe
0051 }
0052 
0053 void KoBibliographyInfo::loadOdf(KoTextSharedLoadingData *sharedLoadingData, const KoXmlElement& element)
0054 {
0055     Q_ASSERT(element.localName() == "bibliography-source" && element.namespaceURI() == KoXmlNS::text);
0056 
0057     KoXmlElement p;
0058     forEachElement(p, element) {
0059         if (p.namespaceURI() != KoXmlNS::text) {
0060             continue;
0061         }
0062 
0063         // first child
0064         if (p.localName() == "index-title-template") {
0065             m_indexTitleTemplate.styleName = p.attribute("style-name");
0066             m_indexTitleTemplate.styleId = styleNameToStyleId(sharedLoadingData, m_indexTitleTemplate.styleName);
0067             m_indexTitleTemplate.text = p.text();
0068         // second child
0069         } else if (p.localName() == "bibliography-entry-template") {
0070             BibliographyEntryTemplate bibEntryTemplate;
0071             bibEntryTemplate.styleName = p.attribute("style-name");
0072             bibEntryTemplate.bibliographyType = p.attribute("bibliography-type");
0073             bibEntryTemplate.styleId = styleNameToStyleId(sharedLoadingData, bibEntryTemplate.styleName );
0074 
0075             KoXmlElement indexEntry;
0076             forEachElement(indexEntry, p) {
0077                 if (indexEntry.namespaceURI() != KoXmlNS::text) {
0078                     continue;
0079                 }
0080 
0081                 if (indexEntry.localName() == "index-entry-bibliography") {
0082                     // use null String if the style name is not present, it means that we inherit it from the parent
0083                     IndexEntryBibliography * entryBibliography = new IndexEntryBibliography(
0084                         indexEntry.attribute("style-name", QString())
0085                     );
0086 
0087                     entryBibliography->dataField = indexEntry.attribute("bibliography-data-field", "article");
0088                     bibEntryTemplate.indexEntries.append(static_cast<IndexEntry*>(entryBibliography));
0089 
0090                 } else if (indexEntry.localName() == "index-entry-span") {
0091                     IndexEntrySpan * entrySpan = new IndexEntrySpan(indexEntry.attribute("style-name", QString()));
0092                     entrySpan->text = indexEntry.text();
0093                     bibEntryTemplate.indexEntries.append(static_cast<IndexEntry*>(entrySpan));
0094 
0095                 } else if (indexEntry.localName() == "index-entry-tab-stop") {
0096                     IndexEntryTabStop * entryTabStop = new IndexEntryTabStop(indexEntry.attribute("style-name", QString()));
0097 
0098                     QString type = indexEntry.attribute("type","right"); // left or right
0099                     if (type == "left") {
0100                         entryTabStop->tab.type = QTextOption::LeftTab;
0101                     } else {
0102                         entryTabStop->tab.type = QTextOption::RightTab;
0103                     }
0104                     entryTabStop->setPosition(indexEntry.attribute("position", QString()));
0105                     entryTabStop->tab.leaderText = indexEntry.attribute("leader-char",".");
0106                     bibEntryTemplate.indexEntries.append(static_cast<IndexEntry*>(entryTabStop));
0107                 }
0108             }
0109             m_entryTemplate[bibEntryTemplate.bibliographyType] = bibEntryTemplate;
0110 
0111         // third child
0112         }
0113     }// forEachElement
0114 }
0115 
0116 void KoBibliographyInfo::saveOdf(KoXmlWriter * writer) const
0117 {
0118     writer->startElement("text:bibliography-source");
0119 
0120         m_indexTitleTemplate.saveOdf(writer);
0121 
0122         foreach (const BibliographyEntryTemplate &entry, m_entryTemplate) {
0123             entry.saveOdf(writer);
0124         }
0125 
0126     writer->endElement();
0127 }
0128 
0129 void KoBibliographyInfo::setGenerator(BibliographyGeneratorInterface *generator)
0130 {
0131     delete m_generator;
0132     m_generator = generator;
0133 }
0134 
0135 void KoBibliographyInfo::setEntryTemplates(QMap<QString, BibliographyEntryTemplate> &entryTemplates)
0136 {
0137     m_entryTemplate = entryTemplates;
0138 }
0139 
0140 KoBibliographyInfo *KoBibliographyInfo::clone()
0141 {
0142     KoBibliographyInfo *newBibInfo = new KoBibliographyInfo();
0143     newBibInfo->m_entryTemplate.clear();
0144     newBibInfo->m_name = QString(m_name);
0145     newBibInfo->m_styleName = QString(m_name);
0146     newBibInfo->m_indexTitleTemplate = m_indexTitleTemplate;
0147 
0148     for (int i = 0; i < m_entryTemplate.size() ; i++) {
0149         newBibInfo->m_entryTemplate.insert(KoOdfBibliographyConfiguration::bibTypes.at(i),
0150                                            m_entryTemplate[KoOdfBibliographyConfiguration::bibTypes.at(i)]);
0151     }
0152 
0153     return newBibInfo;
0154 }
0155 
0156 BibliographyGeneratorInterface *KoBibliographyInfo::generator() const
0157 {
0158     return m_generator;
0159 }