File indexing completed on 2024-05-12 04:58:11

0001 /*
0002  * Copyright 2009 Jakub Wieczorek <faw217@gmail.com>
0003  *
0004  * This program is free software; you can redistribute it and/or modify
0005  * it under the terms of the GNU General Public License as published by
0006  * the Free Software Foundation; either version 2 of the License, or
0007  * (at your option) any later version.
0008  *
0009  * This program is distributed in the hope that it will be useful,
0010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0012  * GNU General Public License for more details.
0013  *
0014  * You should have received a copy of the GNU General Public License
0015  * along with this program; if not, write to the Free Software
0016  * Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017  * Boston, MA  02110-1301  USA
0018  */
0019 /* ============================================================
0020 * Falkon - Qt web browser
0021 * Copyright (C) 2010-2014  David Rosca <nowrep@gmail.com>
0022 *
0023 * This program is free software: you can redistribute it and/or modify
0024 * it under the terms of the GNU General Public License as published by
0025 * the Free Software Foundation, either version 3 of the License, or
0026 * (at your option) any later version.
0027 *
0028 * This program is distributed in the hope that it will be useful,
0029 * but WITHOUT ANY WARRANTY; without even the implied warranty of
0030 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0031 * GNU General Public License for more details.
0032 *
0033 * You should have received a copy of the GNU General Public License
0034 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0035 * ============================================================ */
0036 
0037 #ifndef OPENSEARCHENGINE_H
0038 #define OPENSEARCHENGINE_H
0039 
0040 #include "qzcommon.h"
0041 
0042 #include <qpair.h>
0043 #include <qimage.h>
0044 #include <qmap.h>
0045 #include <qnetworkaccessmanager.h>
0046 #include <qstring.h>
0047 #include <qurl.h>
0048 
0049 class QNetworkReply;
0050 
0051 class OpenSearchEngineDelegate;
0052 class FALKON_EXPORT OpenSearchEngine : public QObject
0053 {
0054     Q_OBJECT
0055 
0056 Q_SIGNALS:
0057     void imageChanged();
0058     void suggestions(const QStringList &suggestions);
0059 
0060 public:
0061     using Parameter = QPair<QString, QString>;
0062     using Parameters = QList<Parameter>;
0063 
0064     Q_PROPERTY(QString name READ name WRITE setName)
0065     Q_PROPERTY(QString description READ description WRITE setDescription)
0066     Q_PROPERTY(QString searchUrlTemplate READ searchUrlTemplate WRITE setSearchUrlTemplate)
0067     Q_PROPERTY(Parameters searchParameters READ searchParameters WRITE setSearchParameters)
0068     Q_PROPERTY(QString searchMethod READ searchMethod WRITE setSearchMethod)
0069     Q_PROPERTY(QString suggestionsUrlTemplate READ suggestionsUrlTemplate WRITE setSuggestionsUrlTemplate)
0070     Q_PROPERTY(Parameters suggestionsParameters READ suggestionsParameters WRITE setSuggestionsParameters)
0071     Q_PROPERTY(QString suggestionsMethod READ suggestionsMethod WRITE setSuggestionsMethod)
0072     Q_PROPERTY(bool providesSuggestions READ providesSuggestions)
0073     Q_PROPERTY(QString imageUrl READ imageUrl WRITE setImageUrl)
0074     Q_PROPERTY(bool valid READ isValid)
0075     Q_PROPERTY(QNetworkAccessManager* networkAccessManager READ networkAccessManager WRITE setNetworkAccessManager)
0076 
0077     OpenSearchEngine(QObject* parent = nullptr);
0078     ~OpenSearchEngine();
0079 
0080     QString name() const;
0081     void setName(const QString &name);
0082 
0083     QString description() const;
0084     void setDescription(const QString &description);
0085 
0086     QString searchUrlTemplate() const;
0087     void setSearchUrlTemplate(const QString &searchUrl);
0088     QUrl searchUrl(const QString &searchTerm) const;
0089 
0090     QByteArray getPostData(const QString &searchTerm) const;
0091 
0092     bool providesSuggestions() const;
0093 
0094     QString suggestionsUrlTemplate() const;
0095     void setSuggestionsUrlTemplate(const QString &suggestionsUrl);
0096     QUrl suggestionsUrl(const QString &searchTerm) const;
0097 
0098     Parameters searchParameters() const;
0099     void setSearchParameters(const Parameters &searchParameters);
0100 
0101     Parameters suggestionsParameters() const;
0102     void setSuggestionsParameters(const Parameters &suggestionsParameters);
0103 
0104     QString searchMethod() const;
0105     void setSearchMethod(const QString &method);
0106 
0107     QString suggestionsMethod() const;
0108     void setSuggestionsMethod(const QString &method);
0109 
0110     QString imageUrl() const;
0111     void setImageUrl(const QString &url);
0112 
0113     QImage image() const;
0114     void setImage(const QImage &image);
0115 
0116     bool isValid() const;
0117 
0118     void setSuggestionsUrl(const QString &string);
0119     void setSuggestionsParameters(const QByteArray &parameters);
0120     QString getSuggestionsUrl();
0121     QByteArray getSuggestionsParameters();
0122 
0123     QNetworkAccessManager* networkAccessManager() const;
0124     void setNetworkAccessManager(QNetworkAccessManager* networkAccessManager);
0125 
0126     OpenSearchEngineDelegate* delegate() const;
0127     void setDelegate(OpenSearchEngineDelegate* delegate);
0128 
0129     bool operator==(const OpenSearchEngine &other) const;
0130     bool operator<(const OpenSearchEngine &other) const;
0131 
0132 public Q_SLOTS:
0133     void requestSuggestions(const QString &searchTerm);
0134     void requestSearchResults(const QString &searchTerm);
0135 
0136 protected:
0137     static QString parseTemplate(const QString &searchTerm, const QString &searchTemplate);
0138     void loadImage() const;
0139 
0140 private Q_SLOTS:
0141     void imageObtained();
0142     void suggestionsObtained();
0143 
0144 private:
0145     QString m_name;
0146     QString m_description;
0147 
0148     QString m_imageUrl;
0149     QImage m_image;
0150 
0151     QString m_searchUrlTemplate;
0152     QString m_suggestionsUrlTemplate;
0153     Parameters m_searchParameters;
0154     Parameters m_suggestionsParameters;
0155     QString m_searchMethod;
0156     QString m_suggestionsMethod;
0157 
0158     QByteArray m_preparedSuggestionsParameters;
0159     QString m_preparedSuggestionsUrl;
0160 
0161     QMap<QString, QNetworkAccessManager::Operation> m_requestMethods;
0162 
0163     QNetworkAccessManager* m_networkAccessManager;
0164     QNetworkReply* m_suggestionsReply;
0165 
0166     OpenSearchEngineDelegate* m_delegate;
0167 };
0168 
0169 #endif // OPENSEARCHENGINE_H
0170