1 /***************************************************************************
2     Copyright (C) 2003-2021 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_FETCHMANAGER_H
26 #define TELLICO_FETCHMANAGER_H
27 
28 #include "fetcher.h"
29 
30 #include <KConfigGroup>
31 #include <KSharedConfig>
32 
33 #include <QObject>
34 #include <QMap>
35 #include <QList>
36 #include <QPixmap>
37 
38 class QUrl;
39 class FetcherTest;
40 class MultiFetcherTest;
41 
42 namespace Tellico {
43   namespace Fetch {
44 
45 class FetchResult;
46 class ConfigWidget;
47 class ManagerMessage;
48 class FetcherInitializer;
49 
50 typedef QMap<QString, Type> NameTypeMap; // map fetcher name to type
51 typedef QMap<FetchKey, QString> KeyMap; // map key type to name of key
52 typedef QList<Fetcher::Ptr> FetcherVec;
53 
54 /**
55  * A manager for handling all the different classes of Fetcher.
56  *
57  * @author Robby Stephenson
58  */
59 class Manager : public QObject {
60 Q_OBJECT
61 
62   /**
63    * Keep a hash of all the function pointers to create classes and provide
64    * functions to "fake" static virtual methods
65    */
66   typedef Fetcher::Ptr (*FETCHER_CREATE_FN)(QObject*);
67   typedef QString (*FETCHER_NAME_FN)(void);
68   typedef QString (*FETCHER_ICON_FN)(void);
69   typedef StringHash (*FETCHER_OPTIONALFIELDS_FN)(void);
70   typedef ConfigWidget* (*FETCHER_CONFIGWIDGET_FN)(QWidget*);
71 
72 public:
73   struct FetcherFunction  {
74     FETCHER_CREATE_FN create;
75     FETCHER_NAME_FN name;
76     FETCHER_ICON_FN icon;
77     FETCHER_OPTIONALFIELDS_FN optionalFields;
78     FETCHER_CONFIGWIDGET_FN configWidget;
79   };
80   static Manager* self();
81 
82   ~Manager();
83 
84   KeyMap keyMap(const QString& source = QString());
85   void startSearch(const QString& source, FetchKey key, const QString& value, Data::Collection::Type collType);
86   void continueSearch();
87   bool canFetch(Data::Collection::Type collType) const;
88   bool hasMoreResults() const;
89   void loadFetchers();
90   const FetcherVec& fetchers();
91   FetcherVec fetchers(int type);
92   Fetcher::Ptr fetcherByUuid(const QString& uuid);
93   NameTypeMap nameTypeMap();
94   ConfigWidget* configWidget(QWidget* parent, Type type, const QString& name);
95 
96   // create fetcher for updating an entry
97   FetcherVec createUpdateFetchers(int collType);
98   FetcherVec createUpdateFetchers(int collType, FetchKey key);
99   Fetcher::Ptr createUpdateFetcher(int collType, const QString& source);
100 
101   /**
102    * Classes derived from Fetcher call this function once
103    * per program to register the class ID key.
104    */
105   void registerFunction(int type, const FetcherFunction& func);
106 
107   static QString typeName(Type type);
108   static QPixmap fetcherIcon(Type type, int iconGroup=3 /*Small*/, int size=0 /* default */);
109   static QPixmap fetcherIcon(Fetcher* ptr, int iconGroup=3 /*Small*/, int size=0 /* default*/);
110   static StringHash optionalFields(Type type);
111 
112 Q_SIGNALS:
113   void signalStatus(const QString& status);
114   void signalResultFound(Tellico::Fetch::FetchResult* result);
115   void signalDone();
116 
117 public Q_SLOTS:
118   void stop();
119 
120 private Q_SLOTS:
121   void slotFetcherDone(Tellico::Fetch::Fetcher* fetcher);
122 
123 private:
124   friend class ManagerMessage;
125   friend class FetcherInitializer;
126   friend class ::FetcherTest;
127   friend class ::MultiFetcherTest;
128 
129   Manager();
130   void addFetcher(Fetcher::Ptr fetcher);
131   Fetcher::Ptr createFetcher(KSharedConfigPtr config, const QString& configGroup);
132   FetcherVec defaultFetchers();
133   void updateStatus(const QString& message);
134 
135   static bool bundledScriptHasExecPath(const QString& specFile, KConfigGroup& config);
136 
137   typedef QHash<int, FetcherFunction> FunctionRegistry;
138   FunctionRegistry functionRegistry;
139 
140   FetcherVec m_fetchers;
141   int m_currentFetcherIndex;
142   KeyMap m_keyMap;
143   QHash<QString, Fetcher::Ptr> m_uuidHash;
144 
145   StringMap m_scriptMap;
146   ManagerMessage* m_messager;
147   uint m_count;
148   bool m_loadDefaults;
149 };
150 
151   } // end namespace
152 } // end namespace
153 #endif
154