File indexing completed on 2024-05-12 05:10:09

0001 /***************************************************************************
0002     Copyright (C) 2022 Robby Stephenson <robby@periapsis.org>
0003  ***************************************************************************/
0004 
0005 /***************************************************************************
0006  *                                                                         *
0007  *   This program is free software; you can redistribute it and/or         *
0008  *   modify it under the terms of the GNU General Public License as        *
0009  *   published by the Free Software Foundation; either version 2 of        *
0010  *   the License or (at your option) version 3 or any later version        *
0011  *   accepted by the membership of KDE e.V. (or its successor approved     *
0012  *   by the membership of KDE e.V.), which shall act as a proxy            *
0013  *   defined in Section 14 of version 3 of the license.                    *
0014  *                                                                         *
0015  *   This program is distributed in the hope that it will be useful,       *
0016  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0017  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0018  *   GNU General Public License for more details.                          *
0019  *                                                                         *
0020  *   You should have received a copy of the GNU General Public License     *
0021  *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
0022  *                                                                         *
0023  ***************************************************************************/
0024 
0025 #include "datacrowimporter.h"
0026 #include "../collection.h"
0027 #include "../images/imagefactory.h"
0028 #include "../utils/datafileregistry.h"
0029 #include "../tellico_debug.h"
0030 
0031 #include <QFile>
0032 #include <QDir>
0033 
0034 using Tellico::Import::DataCrowImporter;
0035 
0036 DataCrowImporter::DataCrowImporter(const QUrl& url_) : XSLTImporter(url_) {
0037   QString xsltFile = DataFileRegistry::self()->locate(QStringLiteral("datacrow2tellico.xsl"));
0038   if(!xsltFile.isEmpty()) {
0039     QUrl u = QUrl::fromLocalFile(xsltFile);
0040     XSLTImporter::setXSLTURL(u);
0041   } else {
0042     myWarning() << "unable to find datacrow2tellico.xml!";
0043   }
0044 }
0045 
0046 bool DataCrowImporter::canImport(int type) const {
0047   return type == Data::Collection::Book ||
0048          type == Data::Collection::Video ||
0049          type == Data::Collection::Album;
0050 }
0051 
0052 Tellico::Data::CollPtr DataCrowImporter::collection() {
0053   Data::CollPtr coll = XSLTImporter::collection();
0054   if(!coll) {
0055     return Data::CollPtr();
0056   }
0057 
0058   // only load images from local file
0059   if(!url().isLocalFile()) {
0060     return coll;
0061   }
0062 
0063   const QString baseDir = url().adjusted(QUrl::RemoveFilename).toLocalFile();
0064   const QString coverField(QStringLiteral("cover"));
0065 
0066   // load images
0067   foreach(Data::EntryPtr entry, coll->entries()) {
0068     QString cover = entry->field(coverField);
0069     if(cover.isEmpty() || !cover.contains(QLatin1Char('/'))) {
0070       continue;
0071     }
0072 
0073     // quite possible cover path includes window separator
0074     const QString imgPath = baseDir + cover.replace(QLatin1Char('\\'),
0075                                                     QLatin1Char('/'),
0076                                                     Qt::CaseInsensitive);
0077     if(!QFile::exists(imgPath)) {
0078       myDebug() << "failed to find" << imgPath;
0079       continue;
0080     }
0081     QString imgID = ImageFactory::addImage(QUrl::fromLocalFile(imgPath), true);
0082     if(!imgID.isEmpty()) {
0083       entry->setField(coverField, imgID, false /* no change to mdate */);
0084     }
0085   }
0086 
0087   return coll;
0088 }