From b51e7390ba944e271da973e3650acdc5aa698cd7 Mon Sep 17 00:00:00 2001
From: Christian Muehlhaeuser <muesli@gmail.com>
Date: Thu, 24 May 2012 10:44:33 +0200
Subject: [PATCH] * Ok, let's try that publicly. AudioState queue to work
 around the Phonon state mess.

---
 src/libtomahawk/audio/AudioEngine.cpp | 62 ++++++++++++++++++++++++---
 src/libtomahawk/audio/AudioEngine.h   | 11 +++--
 2 files changed, 63 insertions(+), 10 deletions(-)

diff --git a/src/libtomahawk/audio/AudioEngine.cpp b/src/libtomahawk/audio/AudioEngine.cpp
index c13815534..1c8fce52a 100644
--- a/src/libtomahawk/audio/AudioEngine.cpp
+++ b/src/libtomahawk/audio/AudioEngine.cpp
@@ -133,9 +133,7 @@ AudioEngine::play()
 
     if ( isPaused() )
     {
-        setVolume( m_volume );
-        m_mediaObject->play();
-        setVolume( m_volume );
+        queueState( Playing );
         emit resumed();
 
         sendNowPlayingNotification( Tomahawk::InfoSystem::InfoNowResumed );
@@ -151,7 +149,8 @@ AudioEngine::pause()
     tDebug( LOGEXTRA ) << Q_FUNC_INFO;
 
     m_volume = volume();
-    m_mediaObject->pause();
+    
+    queueState( Paused );
     emit paused();
 
     Tomahawk::InfoSystem::InfoSystem::instance()->pushInfo( Tomahawk::InfoSystem::InfoPushData( s_aeInfoIdentifier, Tomahawk::InfoSystem::InfoNowPaused, QVariant(), Tomahawk::InfoSystem::PushNoFlag ) );
@@ -185,7 +184,6 @@ AudioEngine::stop( AudioErrorCode errorCode )
         sendWaitingNotification();
 
     Tomahawk::InfoSystem::InfoPushData pushData( s_aeInfoIdentifier, Tomahawk::InfoSystem::InfoNowStopped, QVariant(), Tomahawk::InfoSystem::PushNoFlag );
-
     Tomahawk::InfoSystem::InfoSystem::instance()->pushInfo( pushData );
 }
 
@@ -485,8 +483,7 @@ AudioEngine::loadTrack( const Tomahawk::result_ptr& result )
                 m_input.clear();
             }
             m_input = io;
-            m_mediaObject->play();
-
+            queueState( Playing );
             emit started( m_currentTrack );
 
             if ( TomahawkSettings::instance()->privateListeningMode() != TomahawkSettings::FullyPrivate )
@@ -734,6 +731,16 @@ AudioEngine::onStateChanged( Phonon::State newState, Phonon::State oldState )
             }
         }
     }
+    
+    if ( newState == Phonon::PausedState || newState == Phonon::PlayingState )
+    {
+        tDebug() << "Phonon state now:" << newState;
+        if ( m_stateQueue.count() )
+        {
+            AudioState qState = m_stateQueue.dequeue();
+            checkStateQueue();
+        }
+    }
 }
 
 
@@ -835,6 +842,47 @@ AudioEngine::isLocalResult( const QString& url ) const
 }
 
 
+void
+AudioEngine::checkStateQueue()
+{
+    if ( m_stateQueue.count() )
+    {
+        AudioState state = m_stateQueue.head();
+        tDebug() << "Applying state command:" << state;
+        switch ( state )
+        {
+            case Playing:
+            {
+                setVolume( m_volume );
+                m_mediaObject->play();
+                setVolume( m_volume );
+            }
+            
+            case Paused:
+            {
+                m_mediaObject->pause();
+            }
+        }
+    }
+    else
+        tDebug() << "Queue is empty";
+}
+
+
+void
+AudioEngine::queueState( AudioState state )
+{
+    tDebug() << "Enqueuing state command:" << state << m_stateQueue.count();
+
+    m_stateQueue.enqueue( state );
+
+    if ( m_stateQueue.count() == 1 )
+    {
+        checkStateQueue();
+    }
+}
+
+
 void
 AudioEngine::setState( AudioState state )
 {
diff --git a/src/libtomahawk/audio/AudioEngine.h b/src/libtomahawk/audio/AudioEngine.h
index ac1c2ded4..040552c15 100644
--- a/src/libtomahawk/audio/AudioEngine.h
+++ b/src/libtomahawk/audio/AudioEngine.h
@@ -22,6 +22,7 @@
 
 #include <QtCore/QObject>
 #include <QtCore/QTimer>
+#include <QtCore/QQueue>
 
 #include <phonon/MediaObject>
 #include <phonon/AudioOutput>
@@ -29,13 +30,12 @@
 
 #include "libtomahawk/infosystem/InfoSystem.h"
 
-#include "Result.h"
 #include "Typedefs.h"
+#include "Result.h"
 #include "PlaylistInterface.h"
 
 #include "DllMacro.h"
 
-
 class DLLEXPORT AudioEngine : public QObject
 {
 Q_OBJECT
@@ -139,6 +139,9 @@ private slots:
     void sendWaitingNotification() const;
 
 private:
+    void checkStateQueue();
+    void queueState( AudioState state );
+
     void setState( AudioState state );
 
     bool isHttpResult( const QString& ) const;
@@ -162,9 +165,11 @@ private:
     bool m_waitingOnNewTrack;
 
     mutable QStringList m_supportedMimeTypes;
-    AudioState m_state;
     unsigned int m_volume;
 
+    AudioState m_state;
+    QQueue< AudioState > m_stateQueue;
+
     static AudioEngine* s_instance;
 };