Skip to content

Commit

Permalink
Set identifier after loading definition
Browse files Browse the repository at this point in the history
  • Loading branch information
Emdek committed Jan 13, 2020
1 parent 909fb11 commit 47b1f69
Showing 1 changed file with 73 additions and 75 deletions.
148 changes: 73 additions & 75 deletions src/core/SearchEnginesManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -268,111 +268,109 @@ void SearchEnginesManager::setupQuery(const QString &query, const SearchUrl &sea
SearchEnginesManager::SearchEngineDefinition SearchEnginesManager::loadSearchEngine(QIODevice *device, const QString &identifier, bool checkKeyword)
{
SearchEngineDefinition searchEngine;
searchEngine.identifier = identifier;

QXmlStreamReader reader(device);

if (reader.readNextStartElement() && reader.name() == QLatin1String("OpenSearchDescription"))
if (!reader.readNextStartElement() || reader.name() != QLatin1String("OpenSearchDescription"))
{
SearchUrl *currentUrl(nullptr);
return searchEngine;
}

while (!reader.atEnd())
{
reader.readNext();
SearchUrl *currentUrl(nullptr);

if (reader.isStartElement())
{
const QXmlStreamAttributes attributes(reader.attributes());
searchEngine.identifier = identifier;

if (reader.name() == QLatin1String("Url"))
{
const QString rel(attributes.value(QLatin1String("rel")).toString());
const QString type(attributes.value(QLatin1String("type")).toString());
while (!reader.atEnd())
{
reader.readNext();

if (rel == QLatin1String("self") || type == QLatin1String("application/opensearchdescription+xml"))
{
searchEngine.selfUrl = QUrl(attributes.value(QLatin1String("template")).toString());
if (reader.isStartElement())
{
const QXmlStreamAttributes attributes(reader.attributes());

currentUrl = nullptr;
}
else if ((rel == QLatin1String("suggestions") && type != QLatin1String("x-suggestions+xm")) || type == QLatin1String("application/x-suggestions+json"))
{
currentUrl = &searchEngine.suggestionsUrl;
}
else if ((rel.isEmpty() || rel == QLatin1String("results")) && !type.contains(QLatin1String("suggestions")))
{
currentUrl = &searchEngine.resultsUrl;
}
else
{
currentUrl = nullptr;
}
if (reader.name() == QLatin1String("Url"))
{
const QString rel(attributes.value(QLatin1String("rel")).toString());
const QString type(attributes.value(QLatin1String("type")).toString());

if (currentUrl)
{
currentUrl->url = attributes.value(QLatin1String("template")).toString();
currentUrl->enctype = attributes.value(QLatin1String("enctype")).toString().toLower();
currentUrl->method = attributes.value(QLatin1String("method")).toString().toLower();
}
}
else if (currentUrl && (reader.name() == QLatin1String("Param") || reader.name() == QLatin1String("Parameter")))
{
currentUrl->parameters.addQueryItem(attributes.value(QLatin1String("name")).toString(), attributes.value(QLatin1String("value")).toString());
}
else if (reader.name() == QLatin1String("Shortcut"))
if (rel == QLatin1String("self") || type == QLatin1String("application/opensearchdescription+xml"))
{
const QString keyword(reader.readElementText());
searchEngine.selfUrl = QUrl(attributes.value(QLatin1String("template")).toString());

if (!keyword.isEmpty())
{
if (!m_searchKeywords.contains(keyword))
{
searchEngine.keyword = keyword;

m_searchKeywords.append(keyword);
}
else if (!checkKeyword)
{
searchEngine.keyword = keyword;
}
}
currentUrl = nullptr;
}
else if (reader.name() == QLatin1String("ShortName"))
else if ((rel == QLatin1String("suggestions") && type != QLatin1String("x-suggestions+xm")) || type == QLatin1String("application/x-suggestions+json"))
{
searchEngine.title = reader.readElementText();
currentUrl = &searchEngine.suggestionsUrl;
}
else if (reader.name() == QLatin1String("Description"))
else if ((rel.isEmpty() || rel == QLatin1String("results")) && !type.contains(QLatin1String("suggestions")))
{
searchEngine.description = reader.readElementText();
currentUrl = &searchEngine.resultsUrl;
}
else if (reader.name() == QLatin1String("InputEncoding"))
else
{
searchEngine.encoding = reader.readElementText();
currentUrl = nullptr;
}
else if (reader.name() == QLatin1String("Image"))

if (currentUrl)
{
const QString data(reader.readElementText());
currentUrl->url = attributes.value(QLatin1String("template")).toString();
currentUrl->enctype = attributes.value(QLatin1String("enctype")).toString().toLower();
currentUrl->method = attributes.value(QLatin1String("method")).toString().toLower();
}
}
else if (currentUrl && (reader.name() == QLatin1String("Param") || reader.name() == QLatin1String("Parameter")))
{
currentUrl->parameters.addQueryItem(attributes.value(QLatin1String("name")).toString(), attributes.value(QLatin1String("value")).toString());
}
else if (reader.name() == QLatin1String("Shortcut"))
{
const QString keyword(reader.readElementText());

if (data.startsWith(QLatin1String("data:image/")))
if (!keyword.isEmpty())
{
if (!m_searchKeywords.contains(keyword))
{
searchEngine.icon = QIcon(Utils::loadPixmapFromDataUri(data));
searchEngine.keyword = keyword;

m_searchKeywords.append(keyword);
}
else
else if (!checkKeyword)
{
searchEngine.iconUrl = QUrl(data);
searchEngine.keyword = keyword;
}
}
else if (reader.name() == QLatin1String("SearchForm"))
}
else if (reader.name() == QLatin1String("ShortName"))
{
searchEngine.title = reader.readElementText();
}
else if (reader.name() == QLatin1String("Description"))
{
searchEngine.description = reader.readElementText();
}
else if (reader.name() == QLatin1String("InputEncoding"))
{
searchEngine.encoding = reader.readElementText();
}
else if (reader.name() == QLatin1String("Image"))
{
const QString data(reader.readElementText());

if (data.startsWith(QLatin1String("data:image/")))
{
searchEngine.icon = QIcon(Utils::loadPixmapFromDataUri(data));
}
else
{
searchEngine.formUrl = QUrl(reader.readElementText());
searchEngine.iconUrl = QUrl(data);
}
}
else if (reader.name() == QLatin1String("SearchForm"))
{
searchEngine.formUrl = QUrl(reader.readElementText());
}
}
}
else
{
searchEngine.identifier.clear();
}

return searchEngine;
}
Expand Down

0 comments on commit 47b1f69

Please sign in to comment.