SearchWidget::SearchWidget(MainWindow *mainWindow)
    : QWidget(mainWindow)
    , m_ui(new Ui::SearchWidget())
    , m_mainWindow(mainWindow)
    , m_isNewQueryString(false)
    , m_noSearchResults(true)
{
    m_ui->setupUi(this);

    QString searchPatternHint;
    QTextStream stream(&searchPatternHint, QIODevice::WriteOnly);
    stream << "<html><head/><body><p>"
           << tr("A phrase to search for.") << "<br>"
           << tr("Spaces in a search term may be protected by double quotes.")
           << "</p><p>"
           << tr("Example:", "Search phrase example")
           << "<br>"
           << tr("<b>foo bar</b>: search for <b>foo</b> and <b>bar</b>",
                 "Search phrase example, illustrates quotes usage, a pair of "
                 "space delimited words, individal words are highlighted")
           << "<br>"
           << tr("<b>&quot;foo bar&quot;</b>: search for <b>foo bar</b>",
                 "Search phrase example, illustrates quotes usage, double quoted"
                 "pair of space delimited words, the whole pair is highlighted")
           << "</p></body></html>" << flush;
    m_ui->m_searchPattern->setToolTip(searchPatternHint);

    // Icons
    m_ui->searchButton->setIcon(GuiIconProvider::instance()->getIcon("edit-find"));
    m_ui->downloadButton->setIcon(GuiIconProvider::instance()->getIcon("download"));
    m_ui->goToDescBtn->setIcon(GuiIconProvider::instance()->getIcon("application-x-mswinurl"));
    m_ui->pluginsButton->setIcon(GuiIconProvider::instance()->getIcon("preferences-system-network"));
    m_ui->copyURLBtn->setIcon(GuiIconProvider::instance()->getIcon("edit-copy"));
    connect(m_ui->tabWidget, SIGNAL(tabCloseRequested(int)), this, SLOT(closeTab(int)));

    m_searchEngine = new SearchEngine;
    connect(m_searchEngine, SIGNAL(searchStarted()), SLOT(searchStarted()));
    connect(m_searchEngine, SIGNAL(newSearchResults(QList<SearchResult>)), SLOT(appendSearchResults(QList<SearchResult>)));
    connect(m_searchEngine, SIGNAL(searchFinished(bool)), SLOT(searchFinished(bool)));
    connect(m_searchEngine, SIGNAL(searchFailed()), SLOT(searchFailed()));
    connect(m_searchEngine, SIGNAL(torrentFileDownloaded(QString)), SLOT(addTorrentToSession(QString)));

    // Fill in category combobox
    fillCatCombobox();
    fillPluginComboBox();

    connect(m_ui->m_searchPattern, SIGNAL(returnPressed()), m_ui->searchButton, SLOT(click()));
    connect(m_ui->m_searchPattern, SIGNAL(textEdited(QString)), this, SLOT(searchTextEdited(QString)));
    connect(m_ui->selectPlugin, SIGNAL(currentIndexChanged(int)), this, SLOT(selectMultipleBox(int)));
}
Exemple #2
0
void
SearchModel::downloadFailed(Job *job, const QString & error)
{
  if (job == searchJob)
    emit searchFailed(error);
}
void SearchController::startAction()
{
    checkParams({"pattern", "category", "plugins"});

    if (!Utils::ForeignApps::pythonInfo().isValid())
        throw APIError(APIErrorType::Conflict, "Python must be installed to use the Search Engine.");

    const QString pattern = params()["pattern"].trimmed();
    const QString category = params()["category"].trimmed();
    const QStringList plugins = params()["plugins"].split('|');

    QStringList pluginsToUse;
    if (plugins.size() == 1) {
        const QString pluginsLower = plugins[0].toLower();
        if (pluginsLower == "all")
            pluginsToUse = SearchPluginManager::instance()->allPlugins();
        else if ((pluginsLower == "enabled") || (pluginsLower == "multi"))
            pluginsToUse = SearchPluginManager::instance()->enabledPlugins();
        else
            pluginsToUse << plugins;
    }
    else {
        pluginsToUse << plugins;
    }

    ISession *const session = sessionManager()->session();
    auto activeSearches = session->getData<QSet<int>>(ACTIVE_SEARCHES);
    if (activeSearches.size() >= MAX_CONCURRENT_SEARCHES)
        throw APIError(APIErrorType::Conflict, QString("Unable to create more than %1 concurrent searches.").arg(MAX_CONCURRENT_SEARCHES));

    const auto id = generateSearchId();
    const SearchHandlerPtr searchHandler {SearchPluginManager::instance()->startSearch(pattern, category, pluginsToUse)};
    QObject::connect(searchHandler.data(), &SearchHandler::searchFinished, this, [session, id, this]() { searchFinished(session, id); });
    QObject::connect(searchHandler.data(), &SearchHandler::searchFailed, this, [session, id, this]() { searchFailed(session, id); });

    auto searchHandlers = session->getData<SearchHandlerDict>(SEARCH_HANDLERS);
    searchHandlers.insert(id, searchHandler);
    session->setData(SEARCH_HANDLERS, QVariant::fromValue(searchHandlers));

    activeSearches.insert(id);
    session->setData(ACTIVE_SEARCHES, QVariant::fromValue(activeSearches));

    const QJsonObject result = {{"id", id}};
    setResult(result);
}