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

0001 /***************************************************************************
0002     Copyright (C) 2007-2009 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 "deliciousimporter.h"
0026 #include "../collection.h"
0027 #include "../images/imagefactory.h"
0028 #include "../utils/datafileregistry.h"
0029 #include "../tellico_debug.h"
0030 
0031 #include "rtf2html/rtf2html.h"
0032 
0033 #include <QFile>
0034 
0035 using Tellico::Import::DeliciousImporter;
0036 
0037 DeliciousImporter::DeliciousImporter(const QUrl& url_) : XSLTImporter(url_) {
0038   QString xsltFile = DataFileRegistry::self()->locate(QStringLiteral("delicious2tellico.xsl"));
0039   if(!xsltFile.isEmpty()) {
0040     QUrl u = QUrl::fromLocalFile(xsltFile);
0041     XSLTImporter::setXSLTURL(u);
0042   } else {
0043     myWarning() << "unable to find delicious2tellico.xml!";
0044   }
0045 }
0046 
0047 bool DeliciousImporter::canImport(int type) const {
0048   return type == Data::Collection::Book ||
0049          type == Data::Collection::Video ||
0050          type == Data::Collection::Album ||
0051          type == Data::Collection::Game;
0052 }
0053 
0054 Tellico::Data::CollPtr DeliciousImporter::collection() {
0055   Data::CollPtr coll = XSLTImporter::collection();
0056   if(!coll) {
0057     return Data::CollPtr();
0058   }
0059 
0060   QUrl libraryDir = url();
0061   libraryDir.setPath(url().adjusted(QUrl::StripTrailingSlash|QUrl::RemoveFilename).path() + QLatin1String("Images/"));
0062   const QStringList imageDirs = QStringList()
0063                              << QStringLiteral("Large Covers/")
0064                              << QStringLiteral("Medium Covers/")
0065                              << QStringLiteral("Small Covers/")
0066                              << QStringLiteral("Plain Covers/");
0067   QString commField;
0068   switch(coll->type()) {
0069     case Data::Collection::Book:
0070     case Data::Collection::Album:
0071       commField = QStringLiteral("comments"); break;
0072     case Data::Collection::Video:
0073       commField = QStringLiteral("plot"); break;
0074     case Data::Collection::Game:
0075       commField = QStringLiteral("description"); break;
0076     default:
0077       myWarning() << "bad collection type:" << coll->type();
0078   }
0079 
0080   const QString uuidField = QStringLiteral("uuid");
0081   const QString coverField = QStringLiteral("cover");
0082   const bool isLocal = url().isLocalFile();
0083 
0084   foreach(Data::EntryPtr entry, coll->entries()) {
0085     const QString comments = entry->field(commField);
0086     if(!comments.isEmpty()) {
0087       RTF2HTML rtf2html(comments);
0088       entry->setField(commField, rtf2html.toHTML(), false /* no change to mdate */);
0089     }
0090 
0091     //try to add images
0092     const QString uuid = entry->field(uuidField);
0093     if(!uuid.isEmpty() && isLocal) {
0094       foreach(const QString& imageDir, imageDirs) {
0095         QString imgPath = libraryDir.path() + imageDir + uuid;
0096         if(!QFile::exists(imgPath)) {
0097           continue;
0098         }
0099         QString imgID = ImageFactory::addImage(QUrl::fromLocalFile(imgPath), true);
0100         if(!imgID.isEmpty()) {
0101           entry->setField(coverField, imgID, false /* no change to mdate */);
0102         }
0103         break;
0104       }
0105       // not needed anymore
0106       entry->setField(uuidField, QString(), false /* no change to mdate */);
0107     }
0108   }
0109 
0110   coll->removeField(uuidField);
0111 
0112   return coll;
0113 }