Debian Codegraph @

src/librssguard/network-web/googlesuggest.cpp

raw symbols SCIP
1 // For license of this file, see <project-root-folder>/LICENSE.md.
2
3 // You may use this file under the terms of the BSD license as follows:
4 //
5 // "Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in
12 // the documentation and/or other materials provided with the
13 // distribution.
14 // * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
15 // of its contributors may be used to endorse or promote products derived
16 // from this software without specific prior written permission.
17 //
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
30
31 #include "network-web/googlesuggest.h"
32
33 #include "definitions/definitions.h"
34 #include "gui/reusable/locationlineedit.h"
35 #include "network-web/downloader.h"
36
37 #include <QDomDocument>
38 #include <QKeyEvent>
39 #include <QListWidget>
40 #include <QNetworkReply>
41 #include <QNetworkRequest>
42 #include <QTextCodec>
43 #include <QTimer>
44 #include <QXmlStreamReader>
45
46 GoogleSuggest::GoogleSuggest(LocationLineEdit* editor, QObject* parent)
47 : QObject(parent), m_editor(editor), m_downloader(new Downloader(this)), m_popup(new QListWidget()),
48 m_enteredText(QString()) {
49 m_popup->setWindowFlags(Qt::WindowType::Popup);
50 m_popup->setFocusPolicy(Qt::FocusPolicy::NoFocus);
51 m_popup->setFocusProxy(editor);
52 m_popup->setMouseTracking(true);
53 m_popup->setSelectionBehavior(QAbstractItemView::SelectionBehavior::SelectRows);
54 m_popup->setFrameStyle(QFrame::Shape::Box | QFrame::Shadow::Plain);
55 m_popup->setHorizontalScrollBarPolicy(Qt::ScrollBarPolicy::ScrollBarAlwaysOff);
56 m_popup->installEventFilter(this);
57 m_timer = new QTimer(this);
58 m_timer->setSingleShot(true);
59 m_timer->setInterval(1500);
60
61 connect(m_popup.data(), &QListWidget::itemClicked, this, &GoogleSuggest::doneCompletion);
62 connect(m_timer, &QTimer::timeout, this, &GoogleSuggest::autoSuggest);
63 connect(editor, &LocationLineEdit::textEdited, m_timer, static_cast<void (QTimer::*)()>(&QTimer::start));
64 connect(m_downloader.data(), &Downloader::completed, this, &GoogleSuggest::handleNetworkData);
65 }
66
67 bool GoogleSuggest::eventFilter(QObject* object, QEvent* event) {
68 if (object != m_popup.data()) {
69 return false;
70 }
71
72 if (event->type() == QEvent::MouseButtonPress) {
73 m_popup->hide();
74 m_editor->setFocus();
75 return true;
76 }
77
78 if (event->type() == QEvent::KeyPress) {
79 bool consumed = false;
80 const int key = static_cast<QKeyEvent*>(event)->key();
81
82 switch (key) {
83 case Qt::Key_Enter:
84 case Qt::Key_Return:
85 doneCompletion();
86 consumed = true;
87 break;
88
89 case Qt::Key_Escape:
90 m_editor->setFocus();
91 m_popup->hide();
92 consumed = true;
93 break;
94
95 case Qt::Key_Up:
96 case Qt::Key_Down:
97 case Qt::Key_Home:
98 case Qt::Key_End:
99 case Qt::Key_PageUp:
100 case Qt::Key_PageDown:
101 break;
102
103 default:
104 m_editor->setFocus();
105 m_editor->event(event);
106 m_popup->hide();
107 break;
108 }
109
110 return consumed;
111 }
112
113 return false;
114 }
115
116 void GoogleSuggest::showCompletion(const QStringList& choices) {
117 if (choices.isEmpty()) {
118 return;
119 }
120
121 m_popup->setUpdatesEnabled(false);
122 m_popup->clear();
123
124 for (const QString& choice : choices) {
125 new QListWidgetItem(choice, m_popup.data());
126 }
127
128 m_popup->setCurrentItem(m_popup->item(0));
129 m_popup->adjustSize();
130 m_popup->setUpdatesEnabled(true);
131 m_popup->resize(m_editor->width(), m_popup->sizeHintForRow(0) * qMin(7, choices.count()) + 3);
132 m_popup->move(m_editor->mapToGlobal(QPoint(0, m_editor->height())));
133 // m_popup->setFocus();
134 m_popup->show();
135 }
136
137 void GoogleSuggest::doneCompletion() {
138 m_timer->stop();
139 m_popup->hide();
140 m_editor->setFocus();
141 QListWidgetItem* item = m_popup->currentItem();
142
143 if (item != nullptr) {
144 m_editor->submit(QSL(GOOGLE_SEARCH_URL).arg(item->text()));
145 }
146 }
147
148 void GoogleSuggest::preventSuggest() {
149 m_timer->stop();
150 }
151
152 void GoogleSuggest::autoSuggest() {
153 QUrl entered_url = QUrl(m_editor->text());
154
155 // auto a = entered_url.isValid();
156 // auto b = entered_url.isLocalFile();
157 // auto c = entered_url.scheme();
158 // auto d = entered_url.host();
159
160 if (m_editor->text().size() < 3 || m_editor->text().startsWith(QSL("http")) ||
161 m_editor->text().startsWith(QSL("www")) ||
162 (entered_url.isValid() && !entered_url.isLocalFile() &&
163 (!entered_url.scheme().isEmpty() || entered_url.host().contains(QL1C('.'))))) {
164 // Do not suggest when entered URL.
165 preventSuggest();
166 return;
167 }
168
169 m_enteredText = QUrl::toPercentEncoding(m_editor->text());
170 QString url = QSL(GOOGLE_SUGGEST_URL).arg(m_enteredText);
171
172 m_downloader->downloadFile(url);
173 }
174
175 void GoogleSuggest::handleNetworkData(const QUrl& url,
176 QNetworkReply::NetworkError status,
177 int http_code,
178 const QByteArray& contents) {
179 Q_UNUSED(url)
180 Q_UNUSED(http_code)
181
182 if (status == QNetworkReply::NetworkError::NoError) {
183 const QTextCodec* c = QTextCodec::codecForUtfText(contents);
184
185 QDomDocument xml;
186 xml.setContent(c->toUnicode(contents));
187 QDomNodeList suggestions = xml.elementsByTagName(QSL("suggestion"));
188 QStringList choices;
189
190 choices.reserve(suggestions.size());
191
192 for (int i = 0; i < suggestions.size(); i++) {
193 const QDomElement element = suggestions.at(i).toElement();
194
195 if (element.attributes().contains(QSL("data"))) {
196 choices.append(element.attribute(QSL("data")));
197 }
198 }
199
200 if (choices.isEmpty()) {
201 choices.append(m_enteredText);
202 }
203
204 showCompletion(choices);
205 }
206 }