1 /***************************************************************************
2     Copyright (C) 2012-2019 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 TELLICO_THEGAMESDBFETCHER_H
26 #define TELLICO_THEGAMESDBFETCHER_H
27 
28 #include "fetcher.h"
29 #include "configwidget.h"
30 #include "../datavectors.h"
31 
32 #include <QPointer>
33 
34 class QLineEdit;
35 
36 class KJob;
37 namespace KIO {
38   class StoredTransferJob;
39 }
40 
41 class TheGamesDBFetcherTest;
42 namespace Tellico {
43   namespace GUI {
44     class ComboBox;
45   }
46 
47   namespace Fetch {
48 
49 /**
50  * A fetcher for thegamesdb.net
51  *
52  * @author Robby Stephenson
53  */
54 class TheGamesDBFetcher : public Fetcher {
55 Q_OBJECT
56 
57 friend class ::TheGamesDBFetcherTest;
58 
59 public:
60   /**
61    */
62   TheGamesDBFetcher(QObject* parent);
63   /**
64    */
65   virtual ~TheGamesDBFetcher();
66 
67   /**
68    */
69   virtual QString source() const Q_DECL_OVERRIDE;
isSearching()70   virtual bool isSearching() const Q_DECL_OVERRIDE { return m_started; }
71   virtual void stop() Q_DECL_OVERRIDE;
72   virtual Data::EntryPtr fetchEntryHook(uint uid) Q_DECL_OVERRIDE;
73   virtual bool canSearch(FetchKey k) const Q_DECL_OVERRIDE;
type()74   virtual Type type() const Q_DECL_OVERRIDE { return TheGamesDB; }
75   virtual bool canFetch(int type) const Q_DECL_OVERRIDE;
76   virtual void readConfigHook(const KConfigGroup& config) Q_DECL_OVERRIDE;
77 
78   /**
79    * Returns a widget for modifying the fetcher's config.
80    */
81   virtual Fetch::ConfigWidget* configWidget(QWidget* parent) const Q_DECL_OVERRIDE;
82 
83   class ConfigWidget : public Fetch::ConfigWidget {
84   public:
85     explicit ConfigWidget(QWidget* parent_, const TheGamesDBFetcher* fetcher = nullptr);
86     virtual void saveConfigHook(KConfigGroup&) Q_DECL_OVERRIDE;
87     virtual QString preferredName() const Q_DECL_OVERRIDE;
88 
89   private:
90     QLineEdit* m_apiKeyEdit;
91     GUI::ComboBox* m_imageCombo;
92   };
93   friend class ConfigWidget;
94 
95   static QString defaultName();
96   static QString defaultIcon();
97   static StringHash allOptionalFields();
98 
99 private Q_SLOTS:
100   void slotComplete(KJob* job);
101   // read all cached data
102   void loadCachedData();
103 
104 private:
105   virtual void search() Q_DECL_OVERRIDE;
106   virtual FetchRequest updateRequest(Data::EntryPtr entry) Q_DECL_OVERRIDE;
107   void populateEntry(Data::EntryPtr entry, const QVariantMap& resultMap);
108   void readPlatformList(const QVariantMap& platformMap);
109   void readCoverList(const QVariantMap& platformMap);
110 
111   // right now, Tgdb has three data types for which the whole list must be read at once
112   // caching the platforms in addition, helps the UpdateRequest
113   enum TgdbDataType { Genre, Publisher, Developer, Platform };
114   static QString dataFileName(TgdbDataType dataType);
115 
116   // update cached data
117   void updateData(TgdbDataType dataType, const QByteArray& data);
118   // download data list from Tgdb and update cache
119   void readDataList(TgdbDataType dataType);
120   void writeDataList(TgdbDataType dataType, const QByteArray& data);
121 
122   enum ImageSize {
123     SmallImage=0, // small is really the thumb size
124     MediumImage=1,
125     LargeImage=2,
126     NoImage=3
127   };
128 
129   bool m_started;
130   QString m_apiKey;
131   ImageSize m_imageSize;
132 
133   QHash<uint, Data::EntryPtr> m_entries;
134   QPointer<KIO::StoredTransferJob> m_job;
135   QHash<QString, QString> m_covers;
136   QHash<int, QString> m_genres;
137   QHash<int, QString> m_publishers;
138   QHash<int, QString> m_developers;
139   QHash<int, QString> m_platforms;
140 };
141 
142   } // end namespace
143 } // end namespace
144 #endif
145