Warning, file /office/calligra/filters/libmsooxml/MsooXmlRelationships.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002  * This file is part of Office 2007 Filters for Calligra
0003  *
0004  * Copyright (C) 2009-2010 Nokia Corporation and/or its subsidiary(-ies).
0005  *
0006  * Contact: Suresh Chande suresh.chande@nokia.com
0007  *
0008  * This library is free software; you can redistribute it and/or
0009  * modify it under the terms of the GNU Lesser General Public License
0010  * version 2.1 as published by the Free Software Foundation.
0011  *
0012  * This library is distributed in the hope that it will be useful, but
0013  * WITHOUT ANY WARRANTY; without even the implied warranty of
0014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
0015  * Lesser General Public License for more details.
0016  *
0017  * You should have received a copy of the GNU Lesser General Public
0018  * License along with this library; if not, write to the Free Software
0019  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
0020  * 02110-1301 USA
0021  *
0022  */
0023 
0024 #include "MsooXmlRelationships.h"
0025 #include "MsooXmlRelationshipsReader.h"
0026 #include "MsooXmlImport.h"
0027 
0028 #include <KoOdfExporter.h>
0029 
0030 #include <klocalizedstring.h>
0031 
0032 #include <QSet>
0033 #include "MsooXmlDebug.h"
0034 
0035 using namespace MSOOXML;
0036 
0037 class Q_DECL_HIDDEN MsooXmlRelationships::Private
0038 {
0039 public:
0040     Private()
0041     {
0042     }
0043     ~Private() {
0044     }
0045     KoFilter::ConversionStatus loadRels(const QString& path, const QString& file);
0046 
0047     MsooXmlImport* importer;
0048     KoOdfWriters* writers;
0049     QString *errorMessage;
0050     QMap<QString, QString> rels;
0051     QMap<QString, QString> targetsForTypes;
0052     QSet<QString> loadedFiles;
0053 };
0054 
0055 KoFilter::ConversionStatus MsooXmlRelationships::Private::loadRels(const QString& path, const QString& file)
0056 {
0057     debugMsooXml << (path + '/' + file) << "...";
0058     loadedFiles.insert(path + '/' + file);
0059     MsooXmlRelationshipsReaderContext context(path, file, rels, targetsForTypes);
0060     MsooXmlRelationshipsReader reader(writers);
0061 
0062     const QString realPath(path + "/_rels/" + file + ".rels");
0063     return importer->loadAndParseDocument(
0064                &reader, realPath, *errorMessage, &context);
0065 }
0066 
0067 MsooXmlRelationships::MsooXmlRelationships(MsooXmlImport& importer, KoOdfWriters *writers, QString& errorMessage)
0068         : d(new Private)
0069 {
0070     d->importer = &importer;
0071     d->writers = writers;
0072     d->errorMessage = &errorMessage;
0073 }
0074 
0075 MsooXmlRelationships::~MsooXmlRelationships()
0076 {
0077     delete d;
0078 }
0079 
0080 unsigned MsooXmlRelationships::targetCountWithWord(const QString& searchTerm)
0081 {
0082     unsigned count = 0;
0083     QMapIterator<QString, QString> i(d->rels);
0084     while (i.hasNext()) {
0085         i.next();
0086         if (i.value().contains(searchTerm)) {
0087             ++count;
0088         }
0089     }
0090     return count;
0091 }
0092 
0093 
0094 QString MsooXmlRelationships::target(const QString& path, const QString& file, const QString& id)
0095 {
0096     const QString key(MsooXmlRelationshipsReader::relKey(path, file, id));
0097     const QString result(d->rels.value(key));
0098     if (!result.isEmpty())
0099         return result;
0100     const QString filePath = path + QLatin1Char('/') + file;
0101     if (d->loadedFiles.contains(filePath)) {
0102         *d->errorMessage = i18n("Could not find target for id \"%1\" in file \"%2\"", id, filePath);
0103         return QString(); // cannot be found
0104     }
0105     if (d->loadRels(path, file) != KoFilter::OK) {
0106         *d->errorMessage = i18n("Could not find relationships file \"%1\"", filePath);
0107         return QString();
0108     }
0109     return d->rels.value(key);
0110 }
0111 
0112 QString MsooXmlRelationships::targetForType(const QString& path, const QString& file, const QString& relType)
0113 {
0114     const QString filePath = path + QLatin1Char('/') + file;
0115     const QString key(MsooXmlRelationshipsReader::targetKey(filePath, relType));
0116     //debugMsooXml << key;
0117     const QString target(d->targetsForTypes.value(key));
0118     if (!target.isEmpty())
0119         return target;
0120     if (d->loadedFiles.contains(filePath)) {
0121         *d->errorMessage = i18n("Could not find target for relationship \"%1\" in file \"%2\"",
0122                                 relType, filePath);
0123         return QString(); // cannot be found
0124     }
0125     if (d->loadRels(path, file) != KoFilter::OK) {
0126         *d->errorMessage = i18n("Could not find relationships file \"%1\"", filePath);
0127         return QString();
0128     }
0129     return d->targetsForTypes.value(key);
0130 }