From 1bb69c0fc95facfa6f4651ce70ba2fd69ac12fe3 Mon Sep 17 00:00:00 2001
From: Leo Franchi <lfranchi@kde.org>
Date: Mon, 2 May 2011 18:56:30 -0400
Subject: [PATCH] add Open command to url handling

---
 src/globalactionmanager.cpp | 141 +++++++++++++++++++++---------------
 src/globalactionmanager.h   |   3 +
 2 files changed, 87 insertions(+), 57 deletions(-)

diff --git a/src/globalactionmanager.cpp b/src/globalactionmanager.cpp
index 040bf24bc..8667d5ed2 100644
--- a/src/globalactionmanager.cpp
+++ b/src/globalactionmanager.cpp
@@ -89,7 +89,9 @@ GlobalActionManager::parseTomahawkLink( const QString& url )
         } else if( cmdType == "play" ) {
             return handlePlayCommand( u );
         } else if( cmdType == "bookmark" ) {
-            return handleBookmarkCommand( u );
+            return handlePlayCommand( u );
+        } else if( cmdType == "open" ) {
+            return handleOpenCommand( u );
         } else {
             qDebug() << "Tomahawk link not supported, command not known!" << cmdType << u.path();
             return false;
@@ -130,7 +132,7 @@ GlobalActionManager::handlePlaylistCommand( const QUrl& url )
         pl->createNewRevision( uuid(), pl->currentrevision(), QList< Tomahawk::plentry_ptr >() );
         ViewManager::instance()->show( pl );
     } else if( parts[ 0 ] == "add" ) {
-        if( !url.hasQueryItem( "playlistid" ) || !url.hasQueryItem( "track" ) || !url.hasQueryItem( "artist" ) ) {
+        if( !url.hasQueryItem( "playlistid" ) || !url.hasQueryItem( "title" ) || !url.hasQueryItem( "artist" ) ) {
             qDebug() << "Add to playlist command needs playlistid, track, and artist..." << url.toString();
             return false;
         }
@@ -157,6 +159,19 @@ GlobalActionManager::handleCollectionCommand( const QUrl& url )
     return false;
 }
 
+bool
+GlobalActionManager::handleOpenCommand(const QUrl& url)
+{
+    QStringList parts = url.path().split( "/" ).mid( 1 );
+    if( parts.isEmpty() ) {
+        qDebug() << "No specific type to open:" << url.toString();
+        return false;
+    }
+    // TODO user configurable in the UI
+    return doQueueAdd( parts, url.queryItems() );
+}
+
+
 bool
 GlobalActionManager::handleQueueCommand( const QUrl& url )
 {
@@ -167,61 +182,72 @@ GlobalActionManager::handleQueueCommand( const QUrl& url )
     }
 
     if( parts[ 0 ] == "add" ) {
-        if( parts.size() > 1 && parts[ 1 ] == "track" ) {
-            QPair< QString, QString > pair;
-
-            QString title, artist, album, urlStr;
-            foreach( pair, url.queryItems() ) {
-                if( pair.first == "track" )
-                    title = pair.second;
-                else if( pair.first == "artist" )
-                    artist = pair.second;
-                else if( pair.first == "album" )
-                    album = pair.second;
-                else if( pair.first == "url" )
-                    urlStr = pair.second;
-            }
-            if( !title.isEmpty() || !artist.isEmpty() || !album.isEmpty() ) { // an individual; query to add to queue
-                Tomahawk::query_ptr q = Tomahawk::Query::get( artist, title, album );
-                if( !urlStr.isEmpty() )
-                    q->setResultHint( urlStr );
-                Tomahawk::Pipeline::instance()->resolve( q, true );
-
-                if( !AudioEngine::instance()->isPlaying() ) {
-                    connect( q.data(), SIGNAL( resolvingFinished( bool ) ), this, SLOT( waitingForResolved( bool ) ) );
-                    m_waitingToPlay = q;
-                }
-                return true;
-
-            } else { // a list of urls to add to the queue
-                foreach( pair, url.queryItems() ) {
-                    if( pair.first != "url" )
-                        continue;
-                    QUrl track = QUrl::fromUserInput( pair.second  );
-                    //FIXME: isLocalFile is Qt 4.8
-                    if( track.toString().startsWith( "file://" ) ) { // it's local, so we see if it's in the DB and load it if so
-                        // TODO
-                    } else { // give it a web result hint
-                        QFileInfo info( track.path() );
-                        Tomahawk::query_ptr q = Tomahawk::Query::get( QString(), info.baseName(), QString() );
-                        q->setResultHint( track.toString() );
-                        Tomahawk::Pipeline::instance()->resolve( q, true );
-
-                        ViewManager::instance()->queue()->model()->append( q );
-                        ViewManager::instance()->showQueue();
-                    }
-                    return true;
-                }
-            }
-        } else {
-            qDebug() << "Only queue/add/track is support at the moment, got:" << parts;
-            return false;
-        }
+        doQueueAdd( parts.mid( 1 ), url.queryItems() );
+    } else {
+        qDebug() << "Only queue/add/track is support at the moment, got:" << parts;
+        return false;
     }
 
     return false;
 }
 
+bool
+GlobalActionManager::doQueueAdd( const QStringList& parts, const QList< QPair< QString, QString > >& queryItems )
+{
+    if( parts.size() && parts[ 0 ] == "track" ) {
+        QPair< QString, QString > pair;
+
+        QString title, artist, album, urlStr;
+        foreach( pair, queryItems ) {
+            if( pair.first == "title" )
+                title = pair.second;
+            else if( pair.first == "artist" )
+                artist = pair.second;
+            else if( pair.first == "album" )
+                album = pair.second;
+            else if( pair.first == "url" )
+                urlStr = pair.second;
+        }
+
+        if( !title.isEmpty() || !artist.isEmpty() || !album.isEmpty() ) { // an individual; query to add to queue
+            Tomahawk::query_ptr q = Tomahawk::Query::get( artist, title, album );
+            if( !urlStr.isEmpty() )
+                q->setResultHint( urlStr );
+            Tomahawk::Pipeline::instance()->resolve( q, true );
+
+            ViewManager::instance()->queue()->model()->append( q );
+            ViewManager::instance()->showQueue();
+
+            if( !AudioEngine::instance()->isPlaying() ) {
+                connect( q.data(), SIGNAL( resolvingFinished( bool ) ), this, SLOT( waitingForResolved( bool ) ) );
+                m_waitingToPlay = q;
+            }
+            return true;
+
+        } else { // a list of urls to add to the queue
+            foreach( pair, queryItems ) {
+                if( pair.first != "url" )
+                    continue;
+                QUrl track = QUrl::fromUserInput( pair.second  );
+                //FIXME: isLocalFile is Qt 4.8
+                if( track.toString().startsWith( "file://" ) ) { // it's local, so we see if it's in the DB and load it if so
+                    // TODO
+                } else { // give it a web result hint
+                    QFileInfo info( track.path() );
+                    Tomahawk::query_ptr q = Tomahawk::Query::get( QString(), info.baseName(), QString() );
+                    q->setResultHint( track.toString() );
+                    Tomahawk::Pipeline::instance()->resolve( q, true );
+
+                    ViewManager::instance()->queue()->model()->append( q );
+                    ViewManager::instance()->showQueue();
+                }
+                return true;
+            }
+        }
+    }
+    return false;
+}
+
 bool
 GlobalActionManager::handleSearchCommand( const QUrl& url )
 {
@@ -231,8 +257,8 @@ GlobalActionManager::handleSearchCommand( const QUrl& url )
         query << url.queryItemValue( "artist" );
     if( url.hasQueryItem( "album" ) )
         query << url.queryItemValue( "album" );
-    if( url.hasQueryItem( "track" ) )
-        query << url.queryItemValue( "track" );
+    if( url.hasQueryItem( "title" ) )
+        query << url.queryItemValue( "title" );
     QString queryStr = query.join( " " );
 
     if( queryStr.isEmpty() )
@@ -295,7 +321,7 @@ GlobalActionManager::handlePlayCommand( const QUrl& url )
         QPair< QString, QString > pair;
         QString title, artist, album, urlStr;
         foreach( pair, url.queryItems() ) {
-            if( pair.first == "track" )
+            if( pair.first == "title" )
                 title = pair.second;
             else if( pair.first == "artist" )
                 artist = pair.second;
@@ -330,7 +356,7 @@ bool GlobalActionManager::handleBookmarkCommand(const QUrl& url)
         QPair< QString, QString > pair;
         QString title, artist, album, urlStr;
         foreach( pair, url.queryItems() ) {
-            if( pair.first == "track" )
+            if( pair.first == "title" )
                 title = pair.second;
             else if( pair.first == "artist" )
                 artist = pair.second;
@@ -408,7 +434,8 @@ void
 GlobalActionManager::waitingForResolved( bool success )
 {
     if( success && !m_waitingToPlay.isNull() && !m_waitingToPlay->results().isEmpty() ) { // play it!
-        AudioEngine::instance()->playItem( AudioEngine::instance()->playlist(), m_waitingToPlay->results().first() );
+//         AudioEngine::instance()->playItem( AudioEngine::instance()->playlist(), m_waitingToPlay->results().first() );
+        AudioEngine::instance()->play();
     }
 
     m_waitingToPlay.clear();
diff --git a/src/globalactionmanager.h b/src/globalactionmanager.h
index 95430cd7e..3f6265cf5 100644
--- a/src/globalactionmanager.h
+++ b/src/globalactionmanager.h
@@ -51,6 +51,9 @@ private:
     bool handleSearchCommand(const QUrl& url );
     bool handlePlayCommand(const QUrl& url );
     bool handleBookmarkCommand(const QUrl& url );
+    bool handleOpenCommand(const QUrl& url );
+
+    bool doQueueAdd( const QStringList& parts, const QList< QPair< QString, QString > >& queryItems );
 
     Tomahawk::playlist_ptr m_toShow;
     Tomahawk::query_ptr m_waitingToBookmark;