File indexing completed on 2024-05-12 16:45:48
0001 /*************************************************************************** 0002 Copyright (C) 2003-2009 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_FETCHER_H 0026 #define TELLICO_FETCHER_H 0027 0028 #include "fetch.h" 0029 #include "fetchrequest.h" 0030 #include "fetchresult.h" 0031 #include "messagehandler.h" 0032 #include "../datavectors.h" 0033 0034 #include <QObject> 0035 #include <QString> 0036 #include <QStringList> 0037 #include <QPointer> 0038 #include <QUrl> 0039 0040 #include <KConfigGroup> 0041 0042 namespace Tellico { 0043 namespace Fetch { 0044 0045 class ConfigWidget; 0046 0047 /** 0048 * The top-level abstract class for fetching data. 0049 * 0050 * @author Robby Stephenson 0051 */ 0052 class Fetcher : public QObject { 0053 Q_OBJECT 0054 0055 public: 0056 typedef QPointer<Fetcher> Ptr; 0057 0058 /** 0059 */ 0060 Fetcher(QObject* parent); 0061 /** 0062 */ 0063 virtual ~Fetcher(); 0064 0065 /** 0066 * Returns true if the fetcher might return entries from a certain collection type. 0067 */ 0068 virtual bool canFetch(int type) const = 0; 0069 /** 0070 * Returns true if the fetcher can search using a certain key. 0071 */ 0072 virtual bool canSearch(FetchKey key) const = 0; 0073 virtual bool canUpdate() const; 0074 /** 0075 * Returns true if the fetcher requires a user agent to be sent 0076 */ 0077 virtual bool needsUserAgent() const { return false; } 0078 0079 /** 0080 * Returns the type of the data source. 0081 */ 0082 virtual Type type() const = 0; 0083 /** 0084 * Returns the name of the data source, as defined by the user. 0085 */ 0086 virtual QString source() const = 0; 0087 virtual QString attribution() const { return QString(); } 0088 /** 0089 * Returns the collection type of the most recent search 0090 */ 0091 int collectionType() const; 0092 /** 0093 * Returns whether the fetcher will overwite existing info when updating 0094 */ 0095 bool updateOverwrite() const; 0096 const FetchRequest& request() const; 0097 QStringList optionalFields() const { return m_fields; } 0098 QString uuid() const { return m_uuid; } 0099 /** 0100 * Starts a search, using a key and value. Calls search() 0101 */ 0102 void startSearch(const FetchRequest& request); 0103 virtual void continueSearch() {} 0104 void startUpdate(Data::EntryPtr entry); 0105 /** 0106 * Returns true if the fetcher is currently searching. 0107 */ 0108 virtual bool isSearching() const = 0; 0109 /** 0110 * Returns true if the fetcher can continue and fetch more results 0111 * The fetcher is responsible for remembering state. 0112 */ 0113 virtual bool hasMoreResults() const { return m_hasMoreResults; } 0114 /** 0115 * Stops the fetcher. 0116 */ 0117 virtual void stop() = 0; 0118 /** 0119 * Fetches an entry, given the uid of the search result. 0120 */ 0121 Data::EntryPtr fetchEntry(uint uid); 0122 0123 void setMessageHandler(MessageHandler* handler); 0124 MessageHandler* messageHandler() const { return m_messager; } 0125 /** 0126 */ 0127 void message(const QString& message, int type) const; 0128 void infoList(const QString& message, const QStringList& list) const; 0129 0130 /** 0131 * Reads the config for the widget, given a config group. 0132 */ 0133 void readConfig(const KConfigGroup& config); 0134 void saveConfig(); 0135 void setConfigGroup(const KConfigGroup& config); 0136 /** 0137 * Returns a widget for modifying the fetcher's config. 0138 */ 0139 virtual ConfigWidget* configWidget(QWidget* parent) const = 0; 0140 0141 static QString favIcon(const char* url); 0142 static QString favIcon(const QUrl& url); 0143 0144 Q_SIGNALS: 0145 void signalResultFound(Tellico::Fetch::FetchResult* result); 0146 void signalDone(Tellico::Fetch::Fetcher* fetcher); 0147 0148 protected: 0149 QString m_name; 0150 FetchRequest m_request; 0151 bool m_updateOverwrite; 0152 bool m_hasMoreResults; 0153 0154 private: 0155 /** 0156 * Starts a search, using a key and value. 0157 */ 0158 virtual void search() = 0; 0159 virtual FetchRequest updateRequest(Data::EntryPtr entry) = 0; 0160 virtual void readConfigHook(const KConfigGroup&) = 0; 0161 virtual void saveConfigHook(KConfigGroup&) {} 0162 virtual Data::EntryPtr fetchEntryHook(uint uid) = 0; 0163 0164 MessageHandler* m_messager; 0165 KConfigGroup m_configGroup; 0166 QStringList m_fields; 0167 QString m_uuid; 0168 QHash<uint, Data::EntryPtr> m_entries; 0169 }; 0170 0171 } // end namespace 0172 } // end namespace 0173 0174 #endif