From ac8703c9e78d4b127c9655a73b58c1d6a95aca98 Mon Sep 17 00:00:00 2001
From: Christian Muehlhaeuser <muesli@gmail.com>
Date: Tue, 22 May 2012 11:38:12 +0200
Subject: [PATCH] * Robuster error / retry handling.

---
 src/accounts/lastfm/LastFmConfig.cpp | 29 +++++++++++++++++-----------
 src/accounts/lastfm/LastFmConfig.h   |  4 +++-
 2 files changed, 21 insertions(+), 12 deletions(-)

diff --git a/src/accounts/lastfm/LastFmConfig.cpp b/src/accounts/lastfm/LastFmConfig.cpp
index a0e5caa6b..5b98e76be 100644
--- a/src/accounts/lastfm/LastFmConfig.cpp
+++ b/src/accounts/lastfm/LastFmConfig.cpp
@@ -34,6 +34,7 @@ using namespace Tomahawk::Accounts;
 LastFmConfig::LastFmConfig( LastFmAccount* account )
     : QWidget( 0 )
     , m_account( account )
+    , m_page( 1 )
 {
     m_ui = new Ui_LastFmConfig;
     m_ui->setupUi( this );
@@ -105,9 +106,9 @@ LastFmConfig::enableButton()
 
 
 void
-LastFmConfig::loadHistory( int page )
+LastFmConfig::loadHistory()
 {
-    if ( page == 1 )
+    if ( m_page == 1 )
     {
         m_ui->importHistory->setText( tr( "Importing History..." ) );
         m_ui->importHistory->setEnabled( false );
@@ -115,7 +116,7 @@ LastFmConfig::loadHistory( int page )
         m_ui->progressBar->show();
     }
 
-    QNetworkReply* reply = lastfm::User( m_ui->username->text().toLower() ).getRecentTracks( 200, page );
+    QNetworkReply* reply = lastfm::User( m_ui->username->text().toLower() ).getRecentTracks( 200, m_page );
     connect( reply, SIGNAL( finished() ), SLOT( onHistoryLoaded() ) );
 }
 
@@ -123,6 +124,7 @@ LastFmConfig::loadHistory( int page )
 void
 LastFmConfig::onHistoryLoaded()
 {
+    int total = 0;
     bool finished = false;
     QNetworkReply* reply = qobject_cast< QNetworkReply* >( sender() );
     
@@ -132,7 +134,7 @@ LastFmConfig::onHistoryLoaded()
 
         foreach ( lastfm::XmlQuery e, lfm.children( "track" ) )
         {
-            tDebug() << "Found:" << e["artist"].text() << e["name"].text() << e["date"].attribute( "uts" ).toUInt();
+//            tDebug() << "Found:" << e["artist"].text() << e["name"].text() << e["date"].attribute( "uts" ).toUInt();
             Tomahawk::query_ptr query = Query::get( e["artist"].text(), e["name"].text(), QString(), QString(), false );
             uint timeStamp = e["date"].attribute( "uts" ).toUInt();
             
@@ -145,14 +147,16 @@ LastFmConfig::onHistoryLoaded()
             lastfm::XmlQuery stats = lfm.children( "recenttracks" ).first();
             
             int page = stats.attribute( "page" ).toInt();
-            int total = stats.attribute( "totalPages" ).toInt();
-            tDebug() << "page:" << page << "total:" << total;
+            total = stats.attribute( "totalPages" ).toInt();
             
             m_ui->progressBar->setMaximum( total );
             m_ui->progressBar->setValue( page );
             
             if ( page < total )
-                loadHistory( ++page );
+            {
+                m_page = page + 1;
+                loadHistory();
+            }
             else
                 finished = true;
         }
@@ -167,12 +171,15 @@ LastFmConfig::onHistoryLoaded()
     
     if ( finished )
     {
-        if ( m_ui->progressBar->value() != m_ui->progressBar->maximum() )
+        if ( m_page != total )
+        {
             m_ui->importHistory->setText( tr( "History Incomplete. Retry" ) );
+            m_ui->importHistory->setEnabled( true );
+        }
         else
-            m_ui->importHistory->setText( tr( "Import Playback History" ) );
-
-        m_ui->importHistory->setEnabled( true );
+        {
+            m_ui->importHistory->setText( tr( "Playback History Imported" ) );
+        }
     }
 }
 
diff --git a/src/accounts/lastfm/LastFmConfig.h b/src/accounts/lastfm/LastFmConfig.h
index 765f4a831..f50416c22 100644
--- a/src/accounts/lastfm/LastFmConfig.h
+++ b/src/accounts/lastfm/LastFmConfig.h
@@ -46,12 +46,14 @@ public slots:
 private slots:
     void enableButton();
     
-    void loadHistory( int page = 1 );
+    void loadHistory();
     void onHistoryLoaded();
 
 private:
     LastFmAccount* m_account;
     Ui_LastFmConfig* m_ui;
+    
+    unsigned int m_page;
 };
 
 }