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

0001 /***************************************************************************
0002     Copyright (C) 2005-2020 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 "fetchresult.h"
0026 #include "fetcher.h"
0027 #include "../entry.h"
0028 #include "../collection.h"
0029 #include "../tellico_debug.h"
0030 
0031 #include <QPixmap>
0032 #include <QRandomGenerator>
0033 
0034 namespace {
0035   bool append(QString& text, Tellico::Data::EntryPtr entry, const char* field) {
0036     const QString value = entry->field(QLatin1String(field));
0037     if(value.isEmpty()) {
0038       return false;
0039     }
0040     if(!text.isEmpty()) {
0041       text += QLatin1Char('/');
0042     }
0043     text += value;
0044     return true;
0045   }
0046 }
0047 
0048 using namespace Tellico;
0049 using namespace Tellico::Fetch;
0050 using Tellico::Fetch::FetchResult;
0051 
0052 FetchResult::FetchResult(Fetcher* fetcher_, Data::EntryPtr entry_)
0053    : uid(QRandomGenerator::global()->generate())
0054    , title(entry_->title())
0055    , desc(makeDescription(entry_))
0056    , isbn(entry_->field(QStringLiteral("isbn")))
0057    , m_fetcher(fetcher_) {
0058   Q_ASSERT(fetcher_);
0059 }
0060 
0061 FetchResult::FetchResult(Fetcher* fetcher_, const QString& title_, const QString& desc_, const QString& isbn_)
0062    : uid(QRandomGenerator::global()->generate())
0063    , title(title_)
0064    , desc(desc_)
0065    , isbn(isbn_)
0066    , m_fetcher(fetcher_) {
0067   Q_ASSERT(fetcher_);
0068 }
0069 
0070 Tellico::Data::EntryPtr FetchResult::fetchEntry() {
0071   return m_fetcher ? m_fetcher->fetchEntry(uid) : Data::EntryPtr();
0072 }
0073 
0074 Tellico::Fetch::Fetcher* FetchResult::fetcher() {
0075   Q_ASSERT(m_fetcher);
0076   if(!m_fetcher) myDebug() << "FetchResult::fetcher() - null pointer";
0077   return m_fetcher;
0078 }
0079 
0080 QString FetchResult::makeDescription(Data::EntryPtr entry) {
0081   Q_ASSERT(entry);
0082   QString desc;
0083   switch(entry->collection()->type()) {
0084     case Data::Collection::Book:
0085     case Data::Collection::Bibtex:
0086       append(desc, entry, "author");
0087       append(desc, entry, "publisher");
0088       append(desc, entry, "cr_year") || append(desc, entry, "pub_year") || append(desc, entry, "year");
0089       append(desc, entry, "issue");
0090       break;
0091 
0092     case Data::Collection::ComicBook:
0093       append(desc, entry, "series");
0094       append(desc, entry, "issue");
0095       append(desc, entry, "publisher");
0096       append(desc, entry, "pub_year") || append(desc, entry, "year");
0097       break;
0098 
0099     case Data::Collection::Video:
0100       append(desc, entry, "studio");
0101       append(desc, entry, "director");
0102       append(desc, entry, "year");
0103       append(desc, entry, "medium");
0104       break;
0105 
0106     case Data::Collection::Album:
0107       append(desc, entry, "artist");
0108       append(desc, entry, "label");
0109       append(desc, entry, "year");
0110       break;
0111 
0112     case Data::Collection::Game:
0113       append(desc, entry, "platform");
0114       append(desc, entry, "year");
0115       break;
0116 
0117     case Data::Collection::Card:
0118       append(desc, entry, "number");
0119       append(desc, entry, "series");
0120       break;
0121 
0122     case Data::Collection::BoardGame:
0123       append(desc, entry, "publisher");
0124       append(desc, entry, "designer");
0125       append(desc, entry, "year");
0126       break;
0127 
0128     case Data::Collection::Wine:
0129       append(desc, entry, "appellation");
0130       break;
0131 
0132     case Data::Collection::Coin:
0133       append(desc, entry, "country");
0134       append(desc, entry, "description");
0135       break;
0136 
0137     case Data::Collection::Stamp:
0138       append(desc, entry, "country");
0139       break;
0140 
0141     case Data::Collection::Base:
0142       // some guesses for custom collections
0143       append(desc, entry, "description");
0144       append(desc, entry, "publisher");
0145       append(desc, entry, "year");
0146       break;
0147 
0148     default:
0149       myDebug() << "no result description for collection type =" << entry->collection()->type();
0150       break;
0151   }
0152 
0153   return desc;
0154 }