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

0001 /***************************************************************************
0002     Copyright (C) 2005-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_EXECEXTERNALFETCHER_H
0026 #define TELLICO_EXECEXTERNALFETCHER_H
0027 
0028 #include "fetcher.h"
0029 #include "configwidget.h"
0030 
0031 #include <QHash>
0032 #include <QPointer>
0033 
0034 class KProcess;
0035 class KUrlRequester;
0036 class KComboBox;
0037 class KConfig;
0038 
0039 class QCheckBox;
0040 class QLineEdit;
0041 
0042 class ExternalFetcherTest;
0043 namespace Tellico {
0044   namespace GUI {
0045     class ComboBox;
0046     class LineEdit;
0047     class CollectionTypeCombo;
0048   }
0049   namespace Fetch {
0050 
0051 /**
0052  * @author Robby Stephenson
0053  */
0054 class ExecExternalFetcher : public Fetcher {
0055 Q_OBJECT
0056 
0057 public:
0058   ExecExternalFetcher(QObject* parent);
0059   /**
0060    */
0061   virtual ~ExecExternalFetcher();
0062 
0063   virtual QString source() const Q_DECL_OVERRIDE;
0064   virtual bool isSearching() const Q_DECL_OVERRIDE { return m_started; }
0065   virtual bool canSearch(FetchKey k) const Q_DECL_OVERRIDE;
0066   virtual bool canUpdate() const Q_DECL_OVERRIDE { return m_canUpdate; }
0067   virtual void stop() Q_DECL_OVERRIDE;
0068   virtual Data::EntryPtr fetchEntryHook(uint uid) Q_DECL_OVERRIDE;
0069   virtual Type type() const Q_DECL_OVERRIDE { return ExecExternal; }
0070   virtual bool canFetch(int type) const Q_DECL_OVERRIDE;
0071   virtual void readConfigHook(const KConfigGroup& config) Q_DECL_OVERRIDE;
0072   virtual Fetch::ConfigWidget* configWidget(QWidget* parent) const Q_DECL_OVERRIDE;
0073 
0074   const QString& execPath() const { return m_path; }
0075 
0076   class ConfigWidget : public Fetch::ConfigWidget {
0077   public:
0078     explicit ConfigWidget(QWidget* parent = nullptr, const ExecExternalFetcher* fetcher = nullptr);
0079     ~ConfigWidget();
0080 
0081     void readConfig(const KConfigGroup& config) Q_DECL_OVERRIDE;
0082     virtual void saveConfigHook(KConfigGroup& config) Q_DECL_OVERRIDE;
0083     virtual void removed() Q_DECL_OVERRIDE;
0084     virtual QString preferredName() const Q_DECL_OVERRIDE;
0085 
0086   private:
0087     bool m_deleteOnRemove;
0088     QString m_name, m_newStuffName;
0089     KUrlRequester* m_pathEdit;
0090     GUI::CollectionTypeCombo* m_collCombo;
0091     GUI::ComboBox* m_formatCombo;
0092     QHash<int, QCheckBox*> m_cbDict;
0093     QHash<int, GUI::LineEdit*> m_leDict;
0094     QCheckBox* m_cbUpdate;
0095     GUI::LineEdit* m_leUpdate;
0096   };
0097   friend class ConfigWidget;
0098 
0099   static QString defaultName();
0100   static QString defaultIcon();
0101   static StringHash allOptionalFields() { return StringHash(); }
0102 
0103 private Q_SLOTS:
0104   void slotData();
0105   void slotError();
0106   void slotProcessExited();
0107 
0108 private:
0109   friend class ::ExternalFetcherTest;
0110   static QStringList parseArguments(const QString& str);
0111 
0112   virtual void search() Q_DECL_OVERRIDE;
0113   virtual FetchRequest updateRequest(Data::EntryPtr entry) Q_DECL_OVERRIDE;
0114   void startSearch(const QStringList& args);
0115 
0116   bool m_started;
0117   int m_collType;
0118   int m_formatType;
0119   QString m_path;
0120   QHash<FetchKey, QString> m_args;
0121   bool m_canUpdate : 1;
0122   QString m_updateArgs;
0123   QPointer<KProcess> m_process;
0124   QByteArray m_data;
0125   QHash<uint, Data::EntryPtr> m_entries; // map from search result id to entry
0126   QStringList m_errors;
0127   bool m_deleteOnRemove : 1;
0128   QString m_newStuffName;
0129 };
0130 
0131   } // end namespace
0132 } // end namespace
0133 
0134 #endif