1 /***************************************************************************
2     Copyright (C) 2004-2020 Robby Stephenson <robby@periapsis.org>
3  ***************************************************************************/
4 
5 /***************************************************************************
6  *                                                                         *
7  *   This program is free software; you can redistribute it and/or         *
8  *   modify it under the terms of the GNU General Public License as        *
9  *   published by the Free Software Foundation; either version 2 of        *
10  *   the License or (at your option) version 3 or any later version        *
11  *   accepted by the membership of KDE e.V. (or its successor approved     *
12  *   by the membership of KDE e.V.), which shall act as a proxy            *
13  *   defined in Section 14 of version 3 of the license.                    *
14  *                                                                         *
15  *   This program is distributed in the hope that it will be useful,       *
16  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
17  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
18  *   GNU General Public License for more details.                          *
19  *                                                                         *
20  *   You should have received a copy of the GNU General Public License     *
21  *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
22  *                                                                         *
23  ***************************************************************************/
24 
25 #ifndef AMAZONFETCHER_H
26 #define AMAZONFETCHER_H
27 
28 #include "fetcher.h"
29 #include "configwidget.h"
30 #include "../datavectors.h"
31 
32 #include <QUrl>
33 
34 #include <QPointer>
35 #include <QLabel>
36 
37 class QLineEdit;
38 class QCheckBox;
39 class QLabel;
40 
41 class KJob;
42 namespace KIO {
43   class StoredTransferJob;
44 }
45 
46 class AmazonFetcherTest;
47 namespace Tellico {
48 
49   namespace GUI {
50     class ComboBox;
51   }
52 
53   namespace Fetch {
54 
55 /**
56  * A fetcher for Amazon.com.
57  *
58  * @author Robby Stephenson
59  */
60 class AmazonFetcher : public Fetcher {
61 Q_OBJECT
62 
63 friend class ::AmazonFetcherTest;
64 
65 public:
66   AmazonFetcher(QObject* parent);
67   virtual ~AmazonFetcher();
68 
69   virtual QString source() const Q_DECL_OVERRIDE;
70   virtual QString attribution() const Q_DECL_OVERRIDE;
isSearching()71   virtual bool isSearching() const Q_DECL_OVERRIDE { return m_started; }
72   virtual void continueSearch() Q_DECL_OVERRIDE;
73   // amazon can search title, person, isbn, or keyword. No Raw for now.
74   virtual bool canSearch(FetchKey k) const Q_DECL_OVERRIDE;
75   virtual void stop() Q_DECL_OVERRIDE;
76   virtual Data::EntryPtr fetchEntryHook(uint uid) Q_DECL_OVERRIDE;
type()77   virtual Type type() const Q_DECL_OVERRIDE { return Amazon; }
78   virtual bool canFetch(int type) const Q_DECL_OVERRIDE;
79 
80   struct SiteData {
81     QString title;
82     QByteArray host;
83     QByteArray region;
84     QString country;
85     QString countryName;
86   };
87   static const SiteData& siteData(int site);
88 
89   /**
90    * Returns a widget for modifying the fetcher's config.
91    */
92   virtual Fetch::ConfigWidget* configWidget(QWidget* parent) const Q_DECL_OVERRIDE;
93 
94   class ConfigWidget;
95   friend class ConfigWidget;
96 
97   static QString defaultName();
98   static QString defaultIcon();
99   static StringHash allOptionalFields();
100 
101 private Q_SLOTS:
102   void slotComplete(KJob* job);
103 
104 private:
105   virtual void search() Q_DECL_OVERRIDE;
106   virtual FetchRequest updateRequest(Data::EntryPtr entry) Q_DECL_OVERRIDE;
107   virtual void readConfigHook(const KConfigGroup& config) Q_DECL_OVERRIDE;
108   void doSearch();
109   QByteArray requestPayload(FetchRequest request);
110   Data::CollPtr createCollection();
111   void populateEntry(Data::EntryPtr entry, const QJsonObject& info);
112   void parseTitle(Data::EntryPtr entry);
113   bool parseTitleToken(Data::EntryPtr entry, const QString& token);
114 
115   enum Site {
116     Unknown = -1,
117     US = 0,
118     UK = 1,
119     DE = 2,
120     JP = 3,
121     FR = 4,
122     CA = 5,
123     CN = 6,
124     ES = 7,
125     IT = 8,
126     BR = 9,
127     AU = 10,
128     IN = 11,
129     MX = 12,
130     TR = 13,
131     SG = 14,
132     AE = 15,
133     XX = 16
134   };
135 
136   enum ImageSize {
137     SmallImage=0,
138     MediumImage=1,
139     LargeImage=2,
140     NoImage=3
141   };
142 
143   Site m_site;
144   ImageSize m_imageSize;
145 
146   QString m_assoc;
147   QString m_accessKey;
148   QString m_secretKey;
149   int m_limit;
150   int m_countOffset;
151 
152   int m_page;
153   int m_total;
154   int m_numResults;
155   QHash<uint, Data::EntryPtr> m_entries; // they get modified after collection is created, so can't be const
156   QPointer<KIO::StoredTransferJob> m_job;
157 
158   bool m_started;
159   QString m_testResultsFile;
160   bool m_enableLog;
161 };
162 
163 class AmazonFetcher::ConfigWidget : public Fetch::ConfigWidget {
164 Q_OBJECT
165 
166 public:
167   explicit ConfigWidget(QWidget* parent_, const AmazonFetcher* fetcher = nullptr);
168 
169   virtual void saveConfigHook(KConfigGroup& config) Q_DECL_OVERRIDE;
170   virtual QString preferredName() const Q_DECL_OVERRIDE;
171 
172 private Q_SLOTS:
173   void slotSiteChanged();
174 
175 private:
176   QLineEdit* m_accessEdit;
177   QLineEdit* m_secretKeyEdit;
178   QLineEdit* m_assocEdit;
179   GUI::ComboBox* m_siteCombo;
180   GUI::ComboBox* m_imageCombo;
181 };
182 
183   } // end namespace
184 } // end namespace
185 #endif
186