File indexing completed on 2024-05-12 05:09:32

0001 /***************************************************************************
0002     Copyright (C) 2005-2009 Robby Stephenson <robby@periapsis.org>
0003  ***************************************************************************/
0004 
0005 /***************************************************************************
0006  *                                                                         *
0007  *   This program is free software; you can redistribute it and/or         *
0008  *   modify it under the terms of the GNU General Public License as        *
0009  *   published by the Free Software Foundation; either version 2 of        *
0010  *   the License or (at your option) version 3 or any later version        *
0011  *   accepted by the membership of KDE e.V. (or its successor approved     *
0012  *   by the membership of KDE e.V.), which shall act as a proxy            *
0013  *   defined in Section 14 of version 3 of the license.                    *
0014  *                                                                         *
0015  *   This program is distributed in the hope that it will be useful,       *
0016  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
0017  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
0018  *   GNU General Public License for more details.                          *
0019  *                                                                         *
0020  *   You should have received a copy of the GNU General Public License     *
0021  *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
0022  *                                                                         *
0023  ***************************************************************************/
0024 
0025 #include "fetcherjob.h"
0026 #include "../tellico_debug.h"
0027 
0028 #include <QTimer>
0029 
0030 using namespace Tellico;
0031 using namespace Tellico::Fetch;
0032 using Tellico::Fetch::FetcherJob;
0033 
0034 FetcherJob::FetcherJob(QObject* parent_, Fetcher::Ptr fetcher_, const FetchRequest& request_)
0035     : KJob(parent_), m_fetcher(fetcher_), m_request(request_), m_maximumResults(0) {
0036   connect(m_fetcher.data(), &Fetcher::signalResultFound,
0037           this, &FetcherJob::slotResult);
0038   connect(m_fetcher.data(), &Fetcher::signalDone,
0039           this, &FetcherJob::slotDone);
0040 }
0041 
0042 FetcherJob::~FetcherJob() {
0043   qDeleteAll(m_results);
0044   m_results.clear();
0045 }
0046 
0047 Tellico::Data::EntryList FetcherJob::entries() {
0048   Data::EntryList list;
0049   foreach(FetchResult* result, m_results) {
0050     Data::EntryPtr entry = result->fetchEntry();
0051     if(entry) {
0052       list << entry;
0053     }
0054   }
0055   return list;
0056 }
0057 
0058 void FetcherJob::setMaximumResults(int count_) {
0059   Q_ASSERT(count_ >= 0);
0060   m_maximumResults = count_;
0061 }
0062 
0063 void FetcherJob::start() {
0064   QTimer::singleShot(0, this, &FetcherJob::startSearch);
0065 }
0066 
0067 void FetcherJob::startSearch() {
0068   m_fetcher->startSearch(m_request);
0069 }
0070 
0071 void FetcherJob::slotResult(Tellico::Fetch::FetchResult* result_) {
0072   if(!result_) {
0073     myDebug() << "null result";
0074     return;
0075   }
0076   m_results.append(result_);
0077   if(m_maximumResults > 0 && m_results.count() >= m_maximumResults) {
0078     doKill();
0079   }
0080 }
0081 
0082 void FetcherJob::slotDone() {
0083   // only continue if more results were specifically asked for
0084   if(m_fetcher->hasMoreResults() && m_results.count() < m_maximumResults) {
0085     m_fetcher->continueSearch();
0086   } else {
0087     emitResult();
0088   }
0089 }
0090 
0091 bool FetcherJob::doKill() {
0092   m_fetcher->stop();
0093   return true;
0094 }