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

0001 /***************************************************************************
0002     Copyright (C) 2004-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 #ifndef AMAZONFETCHER_H
0026 #define AMAZONFETCHER_H
0027 
0028 #include "fetcher.h"
0029 #include "configwidget.h"
0030 #include "../datavectors.h"
0031 
0032 #include <QUrl>
0033 
0034 #include <QPointer>
0035 #include <QLabel>
0036 
0037 class QLineEdit;
0038 class QCheckBox;
0039 class QLabel;
0040 
0041 class KJob;
0042 namespace KIO {
0043   class StoredTransferJob;
0044 }
0045 
0046 class AmazonFetcherTest;
0047 namespace Tellico {
0048 
0049   namespace GUI {
0050     class ComboBox;
0051   }
0052 
0053   namespace Fetch {
0054 
0055 /**
0056  * A fetcher for Amazon.com.
0057  *
0058  * @author Robby Stephenson
0059  */
0060 class AmazonFetcher : public Fetcher {
0061 Q_OBJECT
0062 
0063 friend class ::AmazonFetcherTest;
0064 
0065 public:
0066   AmazonFetcher(QObject* parent);
0067   virtual ~AmazonFetcher();
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 void continueSearch() Q_DECL_OVERRIDE;
0073   // amazon can search title, person, isbn, or keyword. No Raw for now.
0074   virtual bool canSearch(FetchKey k) const Q_DECL_OVERRIDE;
0075   virtual void stop() Q_DECL_OVERRIDE;
0076   virtual Data::EntryPtr fetchEntryHook(uint uid) Q_DECL_OVERRIDE;
0077   virtual Type type() const Q_DECL_OVERRIDE { return Amazon; }
0078   virtual bool canFetch(int type) const Q_DECL_OVERRIDE;
0079 
0080   struct SiteData {
0081     QString title;
0082     QByteArray host;
0083     QByteArray region;
0084     QString country;
0085     QString countryName;
0086   };
0087   static const SiteData& siteData(int site);
0088 
0089   /**
0090    * Returns a widget for modifying the fetcher's config.
0091    */
0092   virtual Fetch::ConfigWidget* configWidget(QWidget* parent) const Q_DECL_OVERRIDE;
0093 
0094   class ConfigWidget;
0095   friend class ConfigWidget;
0096 
0097   static QString defaultName();
0098   static QString defaultIcon();
0099   static StringHash allOptionalFields();
0100 
0101 private Q_SLOTS:
0102   void slotComplete(KJob* job);
0103 
0104 private:
0105   virtual void search() Q_DECL_OVERRIDE;
0106   virtual FetchRequest updateRequest(Data::EntryPtr entry) Q_DECL_OVERRIDE;
0107   virtual void readConfigHook(const KConfigGroup& config) Q_DECL_OVERRIDE;
0108   void doSearch();
0109   QByteArray requestPayload(const FetchRequest& request);
0110   Data::CollPtr createCollection();
0111   void populateEntry(Data::EntryPtr entry, const QJsonObject& info);
0112   void parseTitle(Data::EntryPtr entry);
0113   bool parseTitleToken(Data::EntryPtr entry, const QString& token);
0114 
0115   enum Site {
0116     Unknown = -1,
0117     US = 0,
0118     UK = 1,
0119     DE = 2,
0120     JP = 3,
0121     FR = 4,
0122     CA = 5,
0123     CN = 6,
0124     ES = 7,
0125     IT = 8,
0126     BR = 9,
0127     AU = 10,
0128     IN = 11,
0129     MX = 12,
0130     TR = 13,
0131     SG = 14,
0132     AE = 15,
0133     XX = 16
0134   };
0135 
0136   enum ImageSize {
0137     SmallImage=0,
0138     MediumImage=1,
0139     LargeImage=2,
0140     NoImage=3
0141   };
0142 
0143   Site m_site;
0144   ImageSize m_imageSize;
0145 
0146   QString m_assoc;
0147   QString m_accessKey;
0148   QString m_secretKey;
0149   int m_limit;
0150   int m_countOffset;
0151 
0152   int m_page;
0153   int m_total;
0154   int m_numResults;
0155   QHash<uint, Data::EntryPtr> m_entries; // they get modified after collection is created, so can't be const
0156   QPointer<KIO::StoredTransferJob> m_job;
0157 
0158   bool m_started;
0159   QString m_testResultsFile;
0160   bool m_enableLog;
0161 };
0162 
0163 class AmazonFetcher::ConfigWidget : public Fetch::ConfigWidget {
0164 Q_OBJECT
0165 
0166 public:
0167   explicit ConfigWidget(QWidget* parent_, const AmazonFetcher* fetcher = nullptr);
0168 
0169   virtual void saveConfigHook(KConfigGroup& config) Q_DECL_OVERRIDE;
0170   virtual QString preferredName() const Q_DECL_OVERRIDE;
0171 
0172 private Q_SLOTS:
0173   void slotSiteChanged();
0174 
0175 private:
0176   QLineEdit* m_accessEdit;
0177   QLineEdit* m_secretKeyEdit;
0178   QLineEdit* m_assocEdit;
0179   GUI::ComboBox* m_siteCombo;
0180   GUI::ComboBox* m_imageCombo;
0181 };
0182 
0183   } // end namespace
0184 } // end namespace
0185 #endif