1 /* ============================================================
2 * Falkon - Qt web browser
3 * Copyright (C) 2010-2018 David Rosca <nowrep@gmail.com>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 * ============================================================ */
18 #ifndef SEARCHENGINESMANAGER_H
19 #define SEARCHENGINESMANAGER_H
20 
21 #include <QObject>
22 #include <QIcon>
23 #include <QList>
24 #include <QVariant>
25 
26 #include "qzcommon.h"
27 #include "opensearchengine.h"
28 
29 class QWebElement;
30 
31 class WebView;
32 class LoadRequest;
33 
34 class FALKON_EXPORT SearchEnginesManager : public QObject
35 {
36     Q_OBJECT
37 public:
38     explicit SearchEnginesManager(QObject* parent = 0);
39 
40     struct Engine {
41         QString name;
42         QIcon icon;
43         QString url;
44         QString shortcut;
45 
46         QString suggestionsUrl;
47         QByteArray suggestionsParameters;
48         QByteArray postData;
49 
isValidEngine50         bool isValid() const {
51             return !name.isEmpty() && !url.isEmpty();
52         }
53 
54         bool operator==(const Engine &other) const {
55             return (this->name == other.name &&
56                     this->url == other.url &&
57                     this->suggestionsUrl == other.suggestionsUrl &&
58                     this->shortcut == other.shortcut);
59         }
60     };
61 
62     LoadRequest searchResult(const Engine &engine, const QString &string);
63     LoadRequest searchResult(const QString &string);
64 
65     void addEngine(const QUrl &url);
66     void addEngine(OpenSearchEngine* engine);
67     void addEngine(const Engine &engine);
68 
69     void addEngineFromForm(const QVariantMap &formData, WebView *view);
70 
71     void removeEngine(const Engine &engine);
72 
73     void setActiveEngine(const Engine &engine);
activeEngine()74     Engine activeEngine() const { return m_activeEngine; }
75 
76     void setDefaultEngine(const Engine &engine);
defaultEngine()77     Engine defaultEngine() const { return m_defaultEngine; }
78 
79     void editEngine(const Engine &before, const Engine &after);
80 
81     Engine engineForShortcut(const QString &shortcut);
82 
83     void setAllEngines(const QVector<Engine> &engines);
84     QVector<Engine> allEngines();
85 
startingEngineName()86     QString startingEngineName() { return m_startingEngineName; }
87 
88     void saveSettings();
89     void restoreDefaults();
90 
91     static QIcon iconForSearchEngine(const QUrl &url);
92 
93 Q_SIGNALS:
94     void enginesChanged();
95     void activeEngineChanged();
96     void defaultEngineChanged();
97 
98 public Q_SLOTS:
99 
100 private Q_SLOTS:
101     void engineChangedImage();
102     void replyFinished();
103 
scheduleSave()104     void scheduleSave() { m_saveScheduled = true; }
105 
106 private:
107     bool checkEngine(OpenSearchEngine* engine);
108 
109     void loadSettings();
110 
111     bool m_settingsLoaded;
112     bool m_saveScheduled;
113 
114     QString m_startingEngineName;
115     QString m_defaultEngineName;
116     QVector<Engine> m_allEngines;
117     Engine m_activeEngine;
118     Engine m_defaultEngine;
119 };
120 
121 typedef SearchEnginesManager::Engine SearchEngine;
122 
123 // Hint to QVector to use std::realloc on item moving
124 Q_DECLARE_TYPEINFO(SearchEngine, Q_MOVABLE_TYPE);
125 
126 Q_DECLARE_METATYPE(SearchEngine)
127 
128 #endif // SEARCHENGINESMANAGER_H
129