1
mirror of https://invent.kde.org/network/falkon.git synced 2024-11-11 01:22:10 +01:00

SearchEngines: Added support for POST method.

Closes #715
This commit is contained in:
nowrep 2013-11-09 15:48:24 +01:00
parent c9ff4f1c94
commit 2211fbf2b3
9 changed files with 139 additions and 39 deletions

View File

@ -9,6 +9,7 @@ Version 1.5.0
* added KWallet password backend plugin
* added Gnome-Keyring password backend plugin
* added StatusBar Icons plugin that adds extra icons to statusbar
* added support for POST method in search engines manager
* great performance improvement for matching basic rules in AdBlock
* themes can now be loaded from profile directories
* pagescreen can now save output into number of formats, including PDF

View File

@ -37,7 +37,7 @@ EditSearchEngine::EditSearchEngine(const QString &title, QWidget* parent)
QString EditSearchEngine::name()
{
return ui->name->text();
return ui->name->text().trimmed();
}
void EditSearchEngine::setName(const QString &name)
@ -48,7 +48,12 @@ void EditSearchEngine::setName(const QString &name)
QString EditSearchEngine::url()
{
return ui->url->text();
return ui->url->text().trimmed();
}
QString EditSearchEngine::postData()
{
return ui->postData->text().trimmed();
}
void EditSearchEngine::setUrl(const QString &url)
@ -57,9 +62,15 @@ void EditSearchEngine::setUrl(const QString &url)
ui->url->setCursorPosition(0);
}
void EditSearchEngine::setPostData(const QString &postData)
{
ui->postData->setText(postData);
ui->postData->setCursorPosition(0);
}
QString EditSearchEngine::shortcut()
{
return ui->shortcut->text();
return ui->shortcut->text().trimmed();
}
void EditSearchEngine::setShortcut(const QString &shortcut)

View File

@ -35,11 +35,13 @@ public:
void setName(const QString &name);
void setUrl(const QString &url);
void setPostData(const QString &postData);
void setShortcut(const QString &shortcut);
void setIcon(const QIcon &icon);
QString name();
QString url();
QString postData();
QString shortcut();
QIcon icon();

View File

@ -6,11 +6,14 @@
<rect>
<x>0</x>
<y>0</y>
<width>322</width>
<height>196</height>
<width>366</width>
<height>204</height>
</rect>
</property>
<layout class="QFormLayout" name="formLayout">
<property name="fieldGrowthPolicy">
<enum>QFormLayout::ExpandingFieldsGrow</enum>
</property>
<item row="0" column="0">
<widget class="QLabel" name="label">
<property name="text">
@ -35,41 +38,24 @@
</property>
</widget>
</item>
<item row="2" column="0">
<item row="3" column="0">
<widget class="QLabel" name="label_3">
<property name="text">
<string>Shortcut:</string>
</property>
</widget>
</item>
<item row="2" column="1">
<item row="3" column="1">
<widget class="QLineEdit" name="shortcut"/>
</item>
<item row="3" column="0">
<item row="4" column="0">
<widget class="QLabel" name="iconLabel">
<property name="text">
<string>Icon:</string>
</property>
</widget>
</item>
<item row="4" column="0" colspan="2">
<widget class="QLabel" name="label_4">
<property name="text">
<string>&lt;b&gt;Note: &lt;/b&gt;%s in url represent searched string</string>
</property>
</widget>
</item>
<item row="5" column="1">
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
</property>
</widget>
</item>
<item row="3" column="1">
<item row="4" column="1">
<widget class="QFrame" name="editIconFrame">
<layout class="QHBoxLayout" name="editIconLayout">
<property name="margin">
@ -114,6 +100,33 @@
</layout>
</widget>
</item>
<item row="5" column="0" colspan="2">
<widget class="QLabel" name="label_4">
<property name="text">
<string>&lt;b&gt;Note: &lt;/b&gt;%s in url or post data represent searched string</string>
</property>
</widget>
</item>
<item row="6" column="1">
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QLabel" name="label_5">
<property name="text">
<string>Post Data:</string>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QLineEdit" name="postData"/>
</item>
</layout>
</widget>
<resources/>

View File

@ -252,6 +252,34 @@ QUrl OpenSearchEngine::searchUrl(const QString &searchTerm) const
return retVal;
}
QByteArray OpenSearchEngine::getPostData(const QString &searchTerm) const
{
if (m_searchMethod != QLatin1String("post")) {
return QByteArray();
}
QUrl retVal = QUrl("http://foo.bar");
#if QT_VERSION >= 0x050000
QUrlQuery query(retVal);
#endif
Parameters::const_iterator end = m_searchParameters.constEnd();
Parameters::const_iterator i = m_searchParameters.constBegin();
for (; i != end; ++i) {
#if QT_VERSION >= 0x050000
query.addQueryItem(i->first, parseTemplate(searchTerm, i->second));
#else
retVal.addQueryItem(i->first, parseTemplate(searchTerm, i->second));
#endif
}
#if QT_VERSION >= 0x050000
retVal.setQuery(query);
#endif
QByteArray data = retVal.toEncoded(QUrl::RemoveScheme);
return data.contains('?') ? data.mid(data.lastIndexOf('?') + 1) : QByteArray();
}
/*!
\property providesSuggestions
\brief indicates whether the engine supports contextual suggestions

View File

@ -88,6 +88,8 @@ public:
void setSearchUrlTemplate(const QString &searchUrl);
QUrl searchUrl(const QString &searchTerm) const;
QByteArray getPostData(const QString &searchTerm) const;
bool providesSuggestions() const;
QString suggestionsUrlTemplate() const;

View File

@ -55,6 +55,7 @@ void SearchEnginesDialog::addEngine()
SearchEngine engine;
engine.name = dialog.name();
engine.url = dialog.url();
engine.postData = dialog.postData().toUtf8();
engine.shortcut = dialog.shortcut();
engine.icon = SearchEnginesManager::iconForSearchEngine(QUrl::fromEncoded(dialog.url().toUtf8()));
@ -103,6 +104,7 @@ void SearchEnginesDialog::editEngine()
dialog.setName(engine.name);
dialog.setUrl(engine.url);
dialog.setPostData(engine.postData);
dialog.setShortcut(engine.shortcut);
dialog.setIcon(engine.icon);
@ -112,6 +114,7 @@ void SearchEnginesDialog::editEngine()
engine.name = dialog.name();
engine.url = dialog.url();
engine.postData = dialog.postData().toUtf8();
engine.shortcut = dialog.shortcut();
engine.icon = dialog.icon();

View File

@ -115,12 +115,24 @@ SearchEnginesManager::SearchResult SearchEnginesManager::searchResult(const Engi
{
ENSURE_LOADED;
QByteArray url = engine.url.toUtf8();
url.replace(QLatin1String("%s"), QUrl::toPercentEncoding(string));
SearchResult result;
result.request = QNetworkRequest(QUrl::fromEncoded(url));
result.operation = QNetworkAccessManager::GetOperation;
if (engine.postData.isEmpty()) {
QByteArray url = engine.url.toUtf8();
url.replace(QLatin1String("%s"), QUrl::toPercentEncoding(string));
result.request = QNetworkRequest(QUrl::fromEncoded(url));
result.operation = QNetworkAccessManager::GetOperation;
}
else {
QByteArray data = engine.postData;
data.replace("%s", QUrl::toPercentEncoding(string));
result.request = QNetworkRequest(QUrl::fromEncoded(engine.url.toUtf8()));
result.request.setHeader(QNetworkRequest::ContentTypeHeader, QByteArray("application/x-www-form-urlencoded"));
result.operation = QNetworkAccessManager::PostOperation;
result.data = data;
}
return result;
}
@ -232,13 +244,23 @@ void SearchEnginesManager::addEngineFromForm(const QWebElement &element, WebView
return;
}
const QString &method = formElement.hasAttribute("method") ? formElement.attribute("method").toUpper() : "GET";
bool isPost = method == QLatin1String("POST");
QUrl actionUrl = QUrl::fromEncoded(formElement.attribute("action").toUtf8());
if (actionUrl.isRelative()) {
actionUrl = view->url().resolved(actionUrl);
}
QUrl parameterUrl = actionUrl;
if (isPost) {
parameterUrl = QUrl("http://foo.bar");
}
#if QT_VERSION >= 0x050000
QUrlQuery query(actionUrl);
QUrlQuery query(parameterUrl);
query.addQueryItem(element.attribute("name"), "%s");
QWebElementCollection allInputs = formElement.findAll("input");
@ -250,11 +272,15 @@ void SearchEnginesManager::addEngineFromForm(const QWebElement &element, WebView
query.addQueryItem(e.attribute("name"), e.evaluateJavaScript("this.value").toString());
}
actionUrl.setQuery(query);
parameterUrl.setQuery(query);
#else
actionUrl.addQueryItem(element.attribute("name"), "%s");
QList<QPair<QByteArray, QByteArray> > queryItems;
QPair<QByteArray, QByteArray> item;
item.first = element.attribute("name").toUtf8();
item.second = "%s";
queryItems.append(item);
QWebElementCollection allInputs = formElement.findAll("input");
foreach (QWebElement e, allInputs) {
if (element == e || !e.hasAttribute("name")) {
@ -267,19 +293,28 @@ void SearchEnginesManager::addEngineFromForm(const QWebElement &element, WebView
queryItems.append(item);
}
actionUrl.setEncodedQueryItems(queryItems + actionUrl.encodedQueryItems());
parameterUrl.setEncodedQueryItems(queryItems + actionUrl.encodedQueryItems());
#endif
if (!isPost) {
actionUrl = parameterUrl;
}
SearchEngine engine;
engine.name = view->title();
engine.icon = view->icon();
engine.url = actionUrl.toString();
if (isPost) {
QByteArray data = parameterUrl.toEncoded(QUrl::RemoveScheme);
engine.postData = data.contains('?') ? data.mid(data.lastIndexOf('?') + 1) : QByteArray();
}
EditSearchEngine dialog(SearchEnginesDialog::tr("Add Search Engine"), view);
dialog.setName(engine.name);
dialog.setIcon(engine.icon);
dialog.setUrl(engine.url);
dialog.setPostData(engine.postData);
if (dialog.exec() != QDialog::Accepted) {
return;
@ -289,6 +324,7 @@ void SearchEnginesManager::addEngineFromForm(const QWebElement &element, WebView
engine.icon = dialog.icon();
engine.url = dialog.url();
engine.shortcut = dialog.shortcut();
engine.postData = dialog.postData().toUtf8();
if (engine.name.isEmpty() || engine.url.isEmpty()) {
return;
@ -304,14 +340,17 @@ void SearchEnginesManager::addEngine(OpenSearchEngine* engine)
Engine en;
en.name = engine->name();
en.url = engine->searchUrl("searchstring").toString().replace(QLatin1String("searchstring"), QLatin1String("%s"));
if (engine->image().isNull()) {
en.icon = iconForSearchEngine(engine->searchUrl(QString()));
}
else {
en.icon = QIcon(QPixmap::fromImage(engine->image()));
}
en.suggestionsUrl = engine->getSuggestionsUrl();
en.suggestionsParameters = engine->getSuggestionsParameters();
en.postData = engine->getPostData("searchstring").replace("searchstring", "%s");
addEngine(en);
@ -453,9 +492,10 @@ void SearchEnginesManager::saveSettings()
QSqlQuery query;
query.exec("DELETE FROM search_engines");
query.exec("ALTER TABLE search_engines ADD COLUMN postData TEXT"); // FIXME: Delete on release!
foreach (const Engine &en, m_allEngines) {
query.prepare("INSERT INTO search_engines (name, icon, url, shortcut, suggestionsUrl, suggestionsParameters, postData) VALUES (?, ?, ?, ?, ?, ?)");
query.prepare("INSERT INTO search_engines (name, icon, url, shortcut, suggestionsUrl, suggestionsParameters, postData) VALUES (?, ?, ?, ?, ?, ?, ?)");
query.addBindValue(en.name);
query.addBindValue(qIconProvider->iconToBase64(en.icon));
query.addBindValue(en.url);

View File

@ -777,7 +777,7 @@ void WebView::checkForForm(QMenu* menu, const QWebElement &element)
const QString &url = parentElement.attribute("action");
const QString &method = parentElement.hasAttribute("method") ? parentElement.attribute("method").toUpper() : "GET";
if (!url.isEmpty() && method == QLatin1String("GET")) {
if (!url.isEmpty() && (method == QLatin1String("GET") || method == QLatin1String("POST"))) {
menu->addAction(QIcon(":icons/menu/search-icon.png"), tr("Create Search Engine"), this, SLOT(createSearchEngine()));
m_clickedElement = element;