[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-commits
Subject:    extragear/office/tellico/src
From:       Robby Stephenson <robby () periapsis ! org>
Date:       2010-01-01 20:06:43
Message-ID: 1262376403.995080.9292.nullmailer () svn ! kde ! org
[Download RAW message or body]

SVN commit 1068664 by rstephenson:

have Discogs fetcher use the XML base fetcher class

 M  +17 -145   fetch/discogsfetcher.cpp  
 M  +6 -28     fetch/discogsfetcher.h  
 M  +0 -1      fetch/fetcherjob.cpp  
 M  +5 -8      fetch/xmlfetcher.cpp  
 M  +1 -1      fetch/xmlfetcher.h  
 M  +4 -2      tests/discogsfetchertest.cpp  


--- trunk/extragear/office/tellico/src/fetch/discogsfetcher.cpp #1068663:1068664
@@ -25,17 +25,11 @@
 #include "discogsfetcher.h"
 #include "../translators/xslthandler.h"
 #include "../translators/tellicoimporter.h"
-#include "../images/imagefactory.h"
 #include "../gui/guiproxy.h"
 #include "../tellico_utils.h"
-#include "../collection.h"
-#include "../entry.h"
 #include "../tellico_debug.h"
 
 #include <klocale.h>
-#include <kstandarddirs.h>
-#include <kio/job.h>
-#include <kio/jobuidelegate.h>
 #include <KConfigGroup>
 
 #include <QLabel>
@@ -57,17 +51,13 @@
 using Tellico::Fetch::DiscogsFetcher;
 
 DiscogsFetcher::DiscogsFetcher(QObject* parent_)
-    : Fetcher(parent_)
-    , m_xsltHandler(0)
-    , m_limit(DISCOGS_MAX_RETURNS_TOTAL)
-    , m_job(0)
-    , m_started(false)
+    : XMLFetcher(parent_)
     , m_apiKey(QLatin1String(DISCOGS_API_KEY)) {
+  setLimit(DISCOGS_MAX_RETURNS_TOTAL);
+  setXSLTFilename(QLatin1String("discogs2tellico.xsl"));
 }
 
 DiscogsFetcher::~DiscogsFetcher() {
-  delete m_xsltHandler;
-  m_xsltHandler = 0;
 }
 
 QString DiscogsFetcher::source() const {
@@ -86,19 +76,11 @@
   m_fetchImages = config_.readEntry("Fetch Images", true);
 }
 
-void DiscogsFetcher::search() {
-  m_started = true;
-  m_start = 1;
+void DiscogsFetcher::resetSearch() {
   m_total = -1;
-  doSearch();
 }
 
-void DiscogsFetcher::continueSearch() {
-  m_started = true;
-  doSearch();
-}
-
-void DiscogsFetcher::doSearch() {
+KUrl DiscogsFetcher::searchUrl() {
   KUrl u(DISCOGS_API_URL);
   u.addQueryItem(QLatin1String("f"), QLatin1String("xml"));
   u.addQueryItem(QLatin1String("api_key"), m_apiKey);
@@ -123,66 +105,19 @@
     default:
       myWarning() << "key not recognized: " << request().key;
       stop();
-      return;
+      return KUrl();
   }
 
 #ifdef DISCOGS_TEST
   u = KUrl("/home/robby/discogs-results.xml");
 #endif
 //  myDebug() << "url: " << u.url();
-
-  m_job = KIO::storedGet(u, KIO::NoReload, KIO::HideProgressInfo);
-  m_job->ui()->setWindow(GUI::Proxy::widget());
-  connect(m_job, SIGNAL(result(KJob*)),
-          SLOT(slotComplete(KJob*)));
+  return u;
 }
 
-void DiscogsFetcher::stop() {
-  if(!m_started) {
-    return;
-  }
-  if(m_job) {
-    m_job->kill();
-    m_job = 0;
-  }
-  m_started = false;
-  emit signalDone(this);
-}
-
-void DiscogsFetcher::slotComplete(KJob* ) {
-//  myDebug();
-  if(m_job->error()) {
-    m_job->ui()->showErrorMessage();
-    stop();
-    return;
-  }
-
-  QByteArray data = m_job->data();
-  if(data.isEmpty()) {
-    myDebug() << "no data";
-    stop();
-    return;
-  }
-
+void DiscogsFetcher::parseData(const QByteArray& data_) {
+  Q_UNUSED(data_);
 #if 0
-  myWarning() << "Remove debug from discogsfetcher.cpp";
-  QFile f(QLatin1String("/tmp/test1.xml"));
-  if(f.open(QIODevice::WriteOnly)) {
-    QTextStream t(&f);
-    t.setCodec(QTextCodec::codecForName("UTF-8"));
-    t << data;
-  }
-  f.close();
-#endif
-
-  if(!m_xsltHandler) {
-    initXSLTHandler();
-    if(!m_xsltHandler) { // probably an error somewhere in the stylesheet loading
-      stop();
-      return;
-    }
-  }
-
   if(m_total == -1) {
     QDomDocument dom;
     if(!dom.setContent(data, false)) {
@@ -198,57 +133,19 @@
       myDebug() << "total = " << m_total;
     }
   }
-
-  // assume discogs is always utf-8
-  QString str = m_xsltHandler->applyStylesheet(QString::fromUtf8(data, data.size()));
-  Import::TellicoImporter imp(str);
-  Data::CollPtr coll = imp.collection();
-  if(!coll) {
-    myDebug() << "no collection pointer";
-    stop();
-    return;
-  }
-
-  int count = 0;
-  Data::EntryList entries = coll->entries();
-  foreach(Data::EntryPtr entry, entries) {
-    if(count >= m_limit) {
-      break;
-    }
-    if(!m_started) {
-      // might get aborted
-      break;
-    }
-
-    FetchResult* r = new FetchResult(Fetcher::Ptr(this), entry);
-    m_entries.insert(r->uid, Data::EntryPtr(entry));
-    emit signalResultFound(r);
-    ++count;
-  }
   m_start = m_entries.count() + 1;
   // not sure how to specify start in the REST url
   //  m_hasMoreResults = m_start <= m_total;
-
-  stop(); // required
+#endif
 }
 
-Tellico::Data::EntryPtr DiscogsFetcher::fetchEntryHook(uint uid_) {
-  Data::EntryPtr entry = m_entries[uid_];
-  if(!entry) {
-    myWarning() << "no entry in dict";
-    return Data::EntryPtr();
-  }
-  // one way we tell if this entry has been fully initialized is to
-  // check for a cover image
-  if(!entry->field(QLatin1String("cover")).isEmpty()) {
-    myLog() << "already downloaded " << entry->title();
-    return entry;
-  }
+Tellico::Data::EntryPtr DiscogsFetcher::fetchEntryHookData(Data::EntryPtr entry_) {
+  Q_ASSERT(entry_);
 
-  QString release = entry->field(QLatin1String("discogs-id"));
+  QString release = entry_->field(QLatin1String("discogs-id"));
   if(release.isEmpty()) {
     myDebug() << "no discogs release found";
-    return entry;
+    return entry_;
   }
 
 #ifdef DISCOGS_TEST
@@ -275,12 +172,12 @@
   f.close();
 #endif
 
-  Import::TellicoImporter imp(m_xsltHandler->applyStylesheet(output));
+  Import::TellicoImporter imp(xsltHandler()->applyStylesheet(output));
   Data::CollPtr coll = imp.collection();
 //  getTracks(entry);
   if(!coll) {
     myWarning() << "no collection pointer";
-    return entry;
+    return entry_;
   }
 
   if(coll->entryCount() > 1) {
@@ -289,35 +186,10 @@
 
   // don't want to include id
   coll->removeField(QLatin1String("discogs-id"));
-
-  entry = coll->entries().front();
-  m_entries.insert(uid_, entry); // replaces old value
-  return entry;
+  return coll->entries().front();
 }
 
-void DiscogsFetcher::initXSLTHandler() {
-  QString xsltfile = KStandardDirs::locate("appdata", QLatin1String("discogs2tellico.xsl"));
-  if(xsltfile.isEmpty()) {
-    myWarning() << "can not locate discogs2tellico.xsl.";
-    return;
-  }
-
-  KUrl u;
-  u.setPath(xsltfile);
-
-  delete m_xsltHandler;
-  m_xsltHandler = new XSLTHandler(u);
-  if(!m_xsltHandler->isValid()) {
-    myWarning() << "error in discogs2tellico.xsl.";
-    delete m_xsltHandler;
-    m_xsltHandler = 0;
-    return;
-  }
-}
-
 Tellico::Fetch::FetchRequest DiscogsFetcher::updateRequest(Data::EntryPtr entry_) {
-//  myDebug();
-
   QString title = entry_->field(QLatin1String("title"));
   if(!title.isEmpty()) {
     return FetchRequest(Title, title);
--- trunk/extragear/office/tellico/src/fetch/discogsfetcher.h #1068663:1068664
@@ -25,23 +25,14 @@
 #ifndef TELLICO_DISCOGSFETCHER_H
 #define TELLICO_DISCOGSFETCHER_H
 
-#include "fetcher.h"
+#include "xmlfetcher.h"
 #include "configwidget.h"
 #include "../datavectors.h"
 
 #include <klineedit.h>
 
-#include <QPointer>
-
-class KJob;
-namespace KIO {
-  class StoredTransferJob;
-}
-
 namespace Tellico {
 
-  class XSLTHandler;
-
   namespace Fetch {
 
 /**
@@ -49,7 +40,7 @@
  *
  * @author Robby Stephenson
  */
-class DiscogsFetcher : public Fetcher {
+class DiscogsFetcher : public XMLFetcher {
 Q_OBJECT
 
 public:
@@ -63,12 +54,8 @@
   /**
    */
   virtual QString source() const;
-  virtual bool isSearching() const { return m_started; }
-  virtual void continueSearch();
   // amazon can search title or person
   virtual bool canSearch(FetchKey k) const { return k == Title || k == Person || k == Keyword; }
-  virtual void stop();
-  virtual Data::EntryPtr fetchEntryHook(uint uid);
   virtual Type type() const { return Discogs; }
   virtual bool canFetch(int type) const;
   virtual void readConfigHook(const KConfigGroup& config);
@@ -93,25 +80,16 @@
   static QString defaultIcon();
   static StringHash allOptionalFields();
 
-private slots:
-  void slotComplete(KJob* job);
-
 private:
-  virtual void search();
   virtual FetchRequest updateRequest(Data::EntryPtr entry);
-  void initXSLTHandler();
-  void doSearch();
+  virtual void resetSearch();
+  virtual KUrl searchUrl();
+  virtual void parseData(const QByteArray& data);
+  virtual Data::EntryPtr fetchEntryHookData(Data::EntryPtr entry);
 
-  XSLTHandler* m_xsltHandler;
-  int m_limit;
   int m_start;
   int m_total;
 
-  QHash<int, Data::EntryPtr> m_entries;
-  QPointer<KIO::StoredTransferJob> m_job;
-
-  bool m_started;
-
   bool m_fetchImages;
   QString m_apiKey;
 };
--- trunk/extragear/office/tellico/src/fetch/fetcherjob.cpp #1068663:1068664
@@ -32,7 +32,6 @@
 using namespace Tellico::Fetch;
 using Tellico::Fetch::FetcherJob;
 
-
 FetcherJob::FetcherJob(QObject* parent_, Fetcher::Ptr fetcher_, const FetchRequest& request_)
     : KJob(parent_), m_fetcher(fetcher_), m_request(request_) {
   connect(m_fetcher.data(), SIGNAL(signalResultFound(Tellico::Fetch::FetchResult*)),
--- trunk/extragear/office/tellico/src/fetch/xmlfetcher.cpp #1068663:1068664
@@ -27,11 +27,8 @@
 #include "../translators/tellicoimporter.h"
 #include "../gui/guiproxy.h"
 #include "../tellico_utils.h"
-#include "../collection.h"
-#include "../entry.h"
 #include "../tellico_debug.h"
 
-#include <klocale.h>
 #include <kstandarddirs.h>
 #include <kio/job.h>
 #include <kio/jobuidelegate.h>
@@ -65,7 +62,7 @@
 }
 
 void XMLFetcher::doSearch() {
-  KUrl u = searchUrl();
+  const KUrl u = searchUrl();
   Q_ASSERT(!u.isEmpty());
   if(u.isEmpty()) {
     stop();
@@ -98,7 +95,7 @@
     return;
   }
 
-  QByteArray data = m_job->data();
+  const QByteArray data = m_job->data();
   if(data.isEmpty()) {
     myDebug() << "no data";
     stop();
@@ -106,7 +103,7 @@
   }
 
 #if 0
-  myWarning() << "Remove debug from XMLFetcher.cpp";
+  myWarning() << "Remove debug from xmlfetcher.cpp";
   QFile f(QLatin1String("/tmp/test.xml"));
   if(f.open(QIODevice::WriteOnly)) {
     QTextStream t(&f);
@@ -155,7 +152,7 @@
   stop(); // required
 }
 
-Tellico::Data::EntryPtr XMLFetcher::fetchEntryHookHook(uint uid_) {
+Tellico::Data::EntryPtr XMLFetcher::fetchEntryHook(uint uid_) {
   Data::EntryPtr entry = m_entries[uid_];
   if(!entry) {
     myWarning() << "no entry in dict";
@@ -196,7 +193,7 @@
 }
 
 void XMLFetcher::setLimit(int limit_) {
-  Q_ASSERT(m_limit > 0);
+  Q_ASSERT(limit_ > 0);
   m_limit = limit_;
 }
 
--- trunk/extragear/office/tellico/src/fetch/xmlfetcher.h #1068663:1068664
@@ -60,7 +60,7 @@
   virtual bool isSearching() const { return m_started; }
   virtual void continueSearch();
   virtual void stop();
-  virtual Data::EntryPtr fetchEntryHookHook(uint uid);
+  virtual Data::EntryPtr fetchEntryHook(uint uid);
 
 protected:
   void setXSLTFilename(const QString& filename);
--- trunk/extragear/office/tellico/src/tests/discogsfetchertest.cpp #1068663:1068664
@@ -57,11 +57,12 @@
   // don't use 'this' as job parent, it crashes
   Tellico::Fetch::FetcherJob* job = new Tellico::Fetch::FetcherJob(0, fetcher, request);
   connect(job, SIGNAL(result(KJob*)), this, SLOT(slotResult(KJob*)));
+  job->setMaximumResults(1);
 
   job->start();
   m_loop.exec();
 
-  QVERIFY(m_results.size() > 0);
+  QCOMPARE(m_results.size(), 1);
 
   Tellico::Data::EntryPtr entry = m_results.at(0);
 //  QCOMPARE(entry->field(QLatin1String("title")), QLatin1String("Fallen"));
@@ -75,11 +76,12 @@
   // don't use 'this' as job parent, it crashes
   Tellico::Fetch::FetcherJob* job = new Tellico::Fetch::FetcherJob(0, fetcher, request);
   connect(job, SIGNAL(result(KJob*)), this, SLOT(slotResult(KJob*)));
+  job->setMaximumResults(1);
 
   job->start();
   m_loop.exec();
 
-  QVERIFY(m_results.size() > 0);
+  QCOMPARE(m_results.size(), 1);
 
   Tellico::Data::EntryPtr entry = m_results.at(0);
   QCOMPARE(entry->field(QLatin1String("artist")), QLatin1String("Evanescence"));
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic