File indexing completed on 2024-05-12 16:45:50

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