File indexing completed on 2024-04-28 05:08:19

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 "entryupdatejob.h"
0026 #include "entrycomparison.h"
0027 #include "entry.h"
0028 #include "collection.h"
0029 #include "utils/mergeconflictresolver.h"
0030 #include "tellico_debug.h"
0031 
0032 #include <QTimer>
0033 
0034 using namespace Tellico;
0035 using Tellico::EntryUpdateJob;
0036 
0037 EntryUpdateJob::EntryUpdateJob(QObject* parent_, Data::EntryPtr entry_, Fetch::Fetcher::Ptr fetcher_, Mode mode_)
0038     : KJob(parent_), m_entry(entry_), m_fetcher(fetcher_), m_mode(mode_), m_bestMatchScore(-1) {
0039   setCapabilities(KJob::Killable);
0040   connect(m_fetcher.data(), &Fetch::Fetcher::signalResultFound,
0041           this, &EntryUpdateJob::slotResult);
0042   connect(m_fetcher.data(), &Fetch::Fetcher::signalDone,
0043           this, &EntryUpdateJob::slotDone);
0044 }
0045 
0046 void EntryUpdateJob::start() {
0047   QTimer::singleShot(0, this, &EntryUpdateJob::startUpdate);
0048 }
0049 
0050 void EntryUpdateJob::startUpdate() {
0051   m_fetcher->startUpdate(m_entry);
0052 }
0053 
0054 void EntryUpdateJob::slotResult(Tellico::Fetch::FetchResult* result_) {
0055   if(!result_) {
0056     myDebug() << "null result";
0057     return;
0058   }
0059 
0060   Data::EntryPtr entry = result_->fetchEntry();
0061   Q_ASSERT(entry);
0062 
0063   const int match = m_entry->collection()->sameEntry(m_entry, entry);
0064   if(match > m_bestMatchScore) {
0065     myLog() << "Found better match:" << entry->title() << "; score =" << match;
0066     m_bestMatchScore = match;
0067     m_bestMatchEntry = entry;
0068   }
0069   // if perfect match, go ahead and top
0070   if(match >= EntryComparison::ENTRY_PERFECT_MATCH) {
0071     myLog() << "Score exceeds high confidence threshold, stopping search";
0072     doKill();
0073   }
0074 }
0075 
0076 void EntryUpdateJob::slotDone() {
0077   if(m_bestMatchEntry) {
0078     const int matchToBeat = (m_mode == PerfectMatchOnly ? EntryComparison::ENTRY_PERFECT_MATCH
0079                                                         : EntryComparison::ENTRY_GOOD_MATCH);
0080     if(m_bestMatchScore >= matchToBeat) {
0081       myLog() << "Best match is good enough, updating the entry";
0082       Merge::mergeEntry(m_entry, m_bestMatchEntry);
0083     } else {
0084       myLog() << "Best match is not good enough, not updating the entry";
0085     }
0086   }
0087   emitResult();
0088 }
0089 
0090 bool EntryUpdateJob::doKill() {
0091   m_fetcher->stop();
0092   return true;
0093 }