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

0001 /***************************************************************************
0002     Copyright (C) 2019 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 #ifndef TELLICO_COLNECTFETCHER_H
0026 #define TELLICO_COLNECTFETCHER_H
0027 
0028 #include "fetcher.h"
0029 #include "configwidget.h"
0030 #include "../datavectors.h"
0031 
0032 #include <QPointer>
0033 #include <QVariantMap>
0034 
0035 class KJob;
0036 namespace KIO {
0037   class StoredTransferJob;
0038 }
0039 
0040 class ColnectFetcherTest;
0041 namespace Tellico {
0042 
0043   namespace GUI {
0044     class ComboBox;
0045   }
0046 
0047   namespace Fetch {
0048 
0049 /**
0050  * A fetcher for colnect.org
0051  *
0052  * @author Robby Stephenson
0053  */
0054 class ColnectFetcher : public Fetcher {
0055 Q_OBJECT
0056 
0057 friend class ::ColnectFetcherTest;
0058 
0059 public:
0060   /**
0061    */
0062   ColnectFetcher(QObject* parent);
0063   /**
0064    */
0065   virtual ~ColnectFetcher();
0066 
0067   /**
0068    */
0069   virtual QString source() const Q_DECL_OVERRIDE;
0070   virtual QString attribution() const Q_DECL_OVERRIDE;
0071   virtual bool isSearching() const Q_DECL_OVERRIDE { return m_started; }
0072   virtual bool canSearch(FetchKey k) const Q_DECL_OVERRIDE;
0073   virtual void stop() Q_DECL_OVERRIDE;
0074   virtual Data::EntryPtr fetchEntryHook(uint uid) Q_DECL_OVERRIDE;
0075   virtual Type type() const Q_DECL_OVERRIDE { return Colnect; }
0076   virtual bool canFetch(int type) const Q_DECL_OVERRIDE;
0077   virtual void readConfigHook(const KConfigGroup& config) Q_DECL_OVERRIDE;
0078 
0079   /**
0080    * Returns a widget for modifying the fetcher's config.
0081    */
0082   virtual Fetch::ConfigWidget* configWidget(QWidget* parent) const Q_DECL_OVERRIDE;
0083 
0084   class ConfigWidget;
0085   friend class ConfigWidget;
0086 
0087   static QString defaultName();
0088   static QString defaultIcon();
0089   static StringHash allOptionalFields();
0090 
0091 private Q_SLOTS:
0092   void slotComplete(KJob* job);
0093 
0094 private:
0095   static QString URLize(const QString& name);
0096 
0097   virtual void search() Q_DECL_OVERRIDE;
0098   virtual FetchRequest updateRequest(Data::EntryPtr entry) Q_DECL_OVERRIDE;
0099   void populateEntry(Data::EntryPtr entry, const QVariantList& resultList);
0100   void populateCoinEntry(Data::EntryPtr entry, const QVariantList& resultList);
0101   void populateStampEntry(Data::EntryPtr entry, const QVariantList& resultList);
0102   void populateComicEntry(Data::EntryPtr entry, const QVariantList& resultList);
0103   void populateCardEntry(Data::EntryPtr entry, const QVariantList& resultList);
0104   void populateGameEntry(Data::EntryPtr entry, const QVariantList& resultList);
0105   void loadImage(Data::EntryPtr entry, const QString& fieldName);
0106 
0107   QString imageUrl(const QString& name, const QString& id);
0108   void readDataList();
0109   void readItemNames(const QByteArray& item);
0110 
0111   QHash<uint, Data::EntryPtr> m_entries;
0112 
0113   bool m_started;
0114   QString m_locale;
0115   QPointer<KIO::StoredTransferJob> m_job;
0116   QString m_year;
0117   QString m_category;
0118 
0119   // map from field name to position in result list
0120   QHash<QString, int> m_colnectFields;
0121   // color names, for stamp collections
0122   // players and teams for sports cards
0123   QHash<QByteArray, QHash<int, QString> > m_itemNames;
0124 
0125   enum ImageSize {
0126     NoImage=0,
0127     SmallImage=1, // small is really the thumb size
0128     LargeImage=2
0129   };
0130   ImageSize m_imageSize;
0131   int m_lastCollType;
0132 };
0133 
0134 class ColnectFetcher::ConfigWidget : public Fetch::ConfigWidget {
0135 Q_OBJECT
0136 
0137 public:
0138   explicit ConfigWidget(QWidget* parent_, const ColnectFetcher* fetcher = nullptr);
0139   virtual void saveConfigHook(KConfigGroup&) Q_DECL_OVERRIDE;
0140   virtual QString preferredName() const Q_DECL_OVERRIDE;
0141 
0142 private Q_SLOTS:
0143   void slotLangChanged();
0144 
0145 private:
0146   GUI::ComboBox* m_langCombo;
0147   GUI::ComboBox* m_imageCombo;
0148 };
0149 
0150   } // end namespace
0151 } // end namespace
0152 #endif