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

0001 /***************************************************************************
0002     Copyright (C) 2003-2021 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_FETCHMANAGER_H
0026 #define TELLICO_FETCHMANAGER_H
0027 
0028 #include "fetcher.h"
0029 
0030 #include <KConfigGroup>
0031 #include <KSharedConfig>
0032 
0033 #include <QObject>
0034 #include <QMap>
0035 #include <QList>
0036 #include <QPixmap>
0037 
0038 class QUrl;
0039 class FetcherTest;
0040 class MultiFetcherTest;
0041 
0042 namespace Tellico {
0043   namespace Fetch {
0044 
0045 class FetchResult;
0046 class ConfigWidget;
0047 class ManagerMessage;
0048 class FetcherInitializer;
0049 
0050 typedef QHash<QString, Type> NameTypeHash; // map fetcher name to type
0051 typedef QMap<FetchKey, QString> KeyMap; // map key type to name of key
0052 typedef QList<Fetcher::Ptr> FetcherVec;
0053 
0054 /**
0055  * A manager for handling all the different classes of Fetcher.
0056  *
0057  * @author Robby Stephenson
0058  */
0059 class Manager : public QObject {
0060 Q_OBJECT
0061 
0062   /**
0063    * Keep a hash of all the function pointers to create classes and provide
0064    * functions to "fake" static virtual methods
0065    */
0066   typedef Fetcher::Ptr (*FETCHER_CREATE_FN)(QObject*);
0067   typedef QString (*FETCHER_NAME_FN)(void);
0068   typedef QString (*FETCHER_ICON_FN)(void);
0069   typedef StringHash (*FETCHER_OPTIONALFIELDS_FN)(void);
0070   typedef ConfigWidget* (*FETCHER_CONFIGWIDGET_FN)(QWidget*);
0071 
0072 public:
0073   struct FetcherFunction  {
0074     FETCHER_CREATE_FN create;
0075     FETCHER_NAME_FN name;
0076     FETCHER_ICON_FN icon;
0077     FETCHER_OPTIONALFIELDS_FN optionalFields;
0078     FETCHER_CONFIGWIDGET_FN configWidget;
0079   };
0080   static Manager* self();
0081 
0082   ~Manager();
0083 
0084   KeyMap keyMap(const QString& source = QString());
0085   void startSearch(const QString& source, FetchKey key, const QString& value, Data::Collection::Type collType);
0086   void continueSearch();
0087   bool canFetch(Data::Collection::Type collType) const;
0088   bool hasMoreResults() const;
0089   void loadFetchers();
0090   const FetcherVec& fetchers();
0091   FetcherVec fetchers(int type);
0092   Fetcher::Ptr fetcherByUuid(const QString& uuid);
0093   NameTypeHash nameTypeHash();
0094   ConfigWidget* configWidget(QWidget* parent, Type type, const QString& name);
0095 
0096   // create fetcher for updating an entry
0097   FetcherVec createUpdateFetchers(int collType);
0098   FetcherVec createUpdateFetchers(int collType, FetchKey key);
0099   Fetcher::Ptr createUpdateFetcher(int collType, const QString& source);
0100 
0101   /**
0102    * Classes derived from Fetcher call this function once
0103    * per program to register the class ID key.
0104    */
0105   void registerFunction(int type, const FetcherFunction& func);
0106 
0107   static QString typeName(Type type);
0108   static QPixmap fetcherIcon(Type type, int iconGroup=3 /*Small*/, int size=0 /* default */);
0109   static QPixmap fetcherIcon(Fetcher* ptr, int iconGroup=3 /*Small*/, int size=0 /* default*/);
0110   static StringHash optionalFields(Type type);
0111 
0112 Q_SIGNALS:
0113   void signalStatus(const QString& status);
0114   void signalResultFound(Tellico::Fetch::FetchResult* result);
0115   void signalDone();
0116 
0117 public Q_SLOTS:
0118   void stop();
0119 
0120 private Q_SLOTS:
0121   void slotResultFound(Tellico::Fetch::FetchResult* result);
0122   void slotFetcherDone(Tellico::Fetch::Fetcher* fetcher);
0123 
0124 private:
0125   friend class ManagerMessage;
0126   friend class FetcherInitializer;
0127   friend class ::FetcherTest;
0128   friend class ::MultiFetcherTest;
0129 
0130   Manager();
0131   void addFetcher(Fetcher::Ptr fetcher);
0132   Fetcher::Ptr createFetcher(KSharedConfigPtr config, const QString& configGroup);
0133   FetcherVec defaultFetchers();
0134   void updateStatus(const QString& message);
0135 
0136   static bool bundledScriptHasExecPath(const QString& specFile, KConfigGroup& config);
0137 
0138   typedef QHash<int, FetcherFunction> FunctionRegistry;
0139   FunctionRegistry functionRegistry;
0140 
0141   FetcherVec m_fetchers;
0142   int m_currentFetcherIndex;
0143   KeyMap m_keyMap;
0144   QHash<QString, Fetcher::Ptr> m_uuidHash;
0145 
0146   StringMap m_scriptMap;
0147   ManagerMessage* m_messager;
0148   uint m_count;
0149   bool m_loadDefaults;
0150 };
0151 
0152   } // end namespace
0153 } // end namespace
0154 #endif