57 lines
1.7 KiB
C++
57 lines
1.7 KiB
C++
#include "./suggestionslookup.h"
|
|
#include "./manager.h"
|
|
#include "./repository.h"
|
|
|
|
#include <QJsonObject>
|
|
#include <QJsonArray>
|
|
|
|
#include <assert.h>
|
|
|
|
namespace RepoIndex {
|
|
|
|
SuggestionsLookup::SuggestionsLookup(Manager &manager, const QJsonObject &request, QObject *parent) :
|
|
PackageLookup(parent)
|
|
{
|
|
m_id = request.value(QStringLiteral("id"));
|
|
const auto searchTerm = request.value(QStringLiteral("term")).toString();
|
|
if(searchTerm.isEmpty()) {
|
|
m_errors << QStringLiteral("No search term specified.");
|
|
}
|
|
const auto repos = request.value(QStringLiteral("repos")).toArray();
|
|
if(repos.isEmpty()) {
|
|
m_errors << QStringLiteral("No repositories specified.");
|
|
}
|
|
if(m_errors.isEmpty()) {
|
|
for(const auto &repoName : repos) {
|
|
if(auto *repo = manager.repositoryByName(repoName.toString())) {
|
|
if(const auto *reply = repo->requestSuggestions(searchTerm)) {
|
|
connect(reply, &SuggestionsReply::resultsAvailable, this, &SuggestionsLookup::addResults);
|
|
++m_remainingReplies;
|
|
} else {
|
|
m_results << repo->suggestions(searchTerm);
|
|
}
|
|
|
|
} else {
|
|
m_errors << QStringLiteral("The specified repository \"%1\" does not exist.").arg(repoName.toString());
|
|
}
|
|
}
|
|
return;
|
|
}
|
|
deleteLater();
|
|
}
|
|
|
|
void SuggestionsLookup::addResults()
|
|
{
|
|
assert(m_remainingReplies);
|
|
auto *reply = static_cast<SuggestionsReply *>(sender());
|
|
m_results << reply->suggestions();
|
|
reply->deleteLater();
|
|
if(!--m_remainingReplies) {
|
|
emit resultsAvailable(QStringLiteral("suggestions"), m_id, m_results);
|
|
deleteLater();
|
|
}
|
|
}
|
|
|
|
} // namespace RepoIndex
|
|
|