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

0001 /* ============================================================
0002 * Falkon - Qt web browser
0003 * Copyright (C) 2010-2018 David Rosca <nowrep@gmail.com>
0004 *
0005 * This program is free software: you can redistribute it and/or modify
0006 * it under the terms of the GNU General Public License as published by
0007 * the Free Software Foundation, either version 3 of the License, or
0008 * (at your option) any later version.
0009 *
0010 * This program is distributed in the hope that it will be useful,
0011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
0012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0013 * GNU General Public License for more details.
0014 *
0015 * You should have received a copy of the GNU General Public License
0016 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0017 * ============================================================ */
0018 #ifndef SEARCHENGINESMANAGER_H
0019 #define SEARCHENGINESMANAGER_H
0020 
0021 #include <QObject>
0022 #include <QIcon>
0023 #include <QList>
0024 #include <QVariant>
0025 
0026 #include "qzcommon.h"
0027 #include "opensearchengine.h"
0028 
0029 class QWebElement;
0030 
0031 class WebView;
0032 class LoadRequest;
0033 
0034 class FALKON_EXPORT SearchEnginesManager : public QObject
0035 {
0036     Q_OBJECT
0037 public:
0038     explicit SearchEnginesManager(QObject* parent = nullptr);
0039 
0040     struct Engine {
0041         QString name;
0042         QIcon icon;
0043         QString url;
0044         QString shortcut;
0045 
0046         QString suggestionsUrl;
0047         QByteArray suggestionsParameters;
0048         QByteArray postData;
0049 
0050         bool isValid() const {
0051             return !name.isEmpty() && !url.isEmpty();
0052         }
0053 
0054         bool operator==(const Engine &other) const {
0055             return (this->name == other.name &&
0056                     this->url == other.url &&
0057                     this->suggestionsUrl == other.suggestionsUrl &&
0058                     this->shortcut == other.shortcut);
0059         }
0060     };
0061 
0062     LoadRequest searchResult(const Engine &engine, const QString &string);
0063     LoadRequest searchResult(const QString &string);
0064 
0065     void addEngine(const QUrl &url);
0066     void addEngine(OpenSearchEngine* engine);
0067     void addEngine(const Engine &engine);
0068 
0069     void addEngineFromForm(const QVariantMap &formData, WebView *view);
0070 
0071     void removeEngine(const Engine &engine);
0072 
0073     void setActiveEngine(const Engine &engine);
0074     Engine activeEngine() const { return m_activeEngine; }
0075 
0076     void setDefaultEngine(const Engine &engine);
0077     Engine defaultEngine() const { return m_defaultEngine; }
0078 
0079     void editEngine(const Engine &before, const Engine &after);
0080 
0081     Engine engineForShortcut(const QString &shortcut);
0082 
0083     void setAllEngines(const QVector<Engine> &engines);
0084     QVector<Engine> allEngines();
0085 
0086     QString startingEngineName() { return m_startingEngineName; }
0087 
0088     void saveSettings();
0089     void restoreDefaults();
0090 
0091     static QIcon iconForSearchEngine(const QUrl &url);
0092 
0093 Q_SIGNALS:
0094     void enginesChanged();
0095     void activeEngineChanged();
0096     void defaultEngineChanged();
0097 
0098 public Q_SLOTS:
0099 
0100 private Q_SLOTS:
0101     void engineChangedImage();
0102     void replyFinished();
0103 
0104     void scheduleSave() { m_saveScheduled = true; }
0105 
0106 private:
0107     bool checkEngine(OpenSearchEngine* engine);
0108 
0109     void loadSettings();
0110 
0111     bool m_settingsLoaded;
0112     bool m_saveScheduled;
0113 
0114     QString m_startingEngineName;
0115     QString m_defaultEngineName;
0116     QVector<Engine> m_allEngines;
0117     Engine m_activeEngine;
0118     Engine m_defaultEngine;
0119 };
0120 
0121 using SearchEngine = SearchEnginesManager::Engine;
0122 
0123 // Hint to QVector to use std::realloc on item moving
0124 Q_DECLARE_TYPEINFO(SearchEngine, Q_MOVABLE_TYPE);
0125 
0126 Q_DECLARE_METATYPE(SearchEngine)
0127 
0128 #endif // SEARCHENGINESMANAGER_H