File indexing completed on 2024-05-05 04:48:44

0001 /****************************************************************************************
0002  * Copyright (c) 2013 Tatjana Gornak <t.gornak@gmail.com>                               *
0003  *                                                                                      *
0004  * This program is free software; you can redistribute it and/or modify it under        *
0005  * the terms of the GNU General Public License as published by the Free Software        *
0006  * Foundation; either version 2 of the License, or (at your option) any later           *
0007  * version.                                                                             *
0008  *                                                                                      *
0009  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
0010  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
0011  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
0012  *                                                                                      *
0013  * You should have received a copy of the GNU General Public License along with         *
0014  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
0015  ****************************************************************************************/
0016 
0017 #include "PlaylistRestorer.h"
0018 
0019 #include "amarokconfig.h"
0020 #include "core/logger/Logger.h"
0021 #include "core-impl/collections/support/CollectionManager.h"
0022 #include "core-impl/playlists/types/file/PlaylistFileSupport.h"
0023 #include "playlist/PlaylistActions.h"
0024 #include "playlist/PlaylistController.h"
0025 #include "playlist/PlaylistDock.h"
0026 #include "playlist/PlaylistModelStack.h"
0027 #include "playlistmanager/PlaylistManager.h"
0028 
0029 #include <QStandardPaths>
0030 
0031 using namespace Playlist;
0032 
0033 Restorer::Restorer()
0034          : m_position( m_tracks )
0035 {
0036 }
0037 
0038 void
0039 Restorer::restore( const QUrl &defaultPath )
0040 {
0041     m_tracks.clear();
0042     m_playlistToRestore = Playlists::loadPlaylistFile( defaultPath );
0043     if ( m_playlistToRestore ) // This pointer will be 0 on first startup
0044     {
0045         subscribeTo( Playlists::PlaylistPtr::staticCast( m_playlistToRestore ) );
0046         m_playlistToRestore->triggerTrackLoad();
0047     }
0048     else
0049         runJingle();
0050 }
0051 
0052 void
0053 Restorer::runJingle()
0054 {
0055     DEBUG_BLOCK
0056     if( AmarokConfig::playFirstRunJingle() )
0057     {
0058         QString jingle = QStandardPaths::locate( QStandardPaths::GenericDataLocation, QStringLiteral("amarok/data/first_run_jingle.ogg") );
0059         The::playlistController()->clear();
0060         The::playlistController()->insertTrack( 0, CollectionManager::instance()->trackForUrl( QUrl::fromLocalFile(jingle) ) );
0061         AmarokConfig::setPlayFirstRunJingle( false );
0062     }
0063     Q_EMIT restoreFinished();
0064 }
0065 
0066 void
0067 Restorer::tracksLoaded(Playlists::PlaylistPtr playlist )
0068 {
0069     if( m_playlistToRestore == playlist )
0070     {
0071         m_tracks = playlist->tracks();
0072         m_position = m_tracks;
0073         processTracks();
0074     }
0075     else
0076     {
0077         // process child playlists
0078         Meta::TrackList newtracks = playlist->tracks();
0079         foreach( Meta::TrackPtr t, newtracks )
0080             if( t )
0081                 m_position.insert( t );
0082         processTracks();
0083     }
0084 }
0085 
0086 void
0087 Restorer::processTracks()
0088 {
0089     while( m_position.hasNext() )
0090     {
0091         m_position.next();
0092         Meta::TrackPtr track = m_position.value();
0093         if( ! track )
0094             m_position.remove();
0095         else if( Playlists::canExpand( track ) )
0096         {
0097             Playlists::PlaylistPtr playlist = Playlists::expand( track );
0098             //expand() can return 0 if the KIO job errors out
0099             if( playlist )
0100             {
0101                 m_position.remove();
0102                 subscribeTo( playlist );
0103                 playlist->triggerTrackLoad(); //playlist track loading is on demand.
0104                 // Execution will be envoked after loading of playlist is finished
0105                 return;
0106            }
0107         }
0108     }
0109     // This code executes only ones after there is no more
0110     // playlists in m_tracks
0111     The::playlistController()->insertTracks( 0, m_tracks );
0112     Actions::instance()->queue( m_playlistToRestore->queue() );
0113 
0114     //Select previously playing track
0115     const int lastPlayingRow = AmarokConfig::lastPlaying();
0116     if( lastPlayingRow >= 0 )
0117         ModelStack::instance()->bottom()->setActiveRow( lastPlayingRow );
0118     Q_EMIT restoreFinished();
0119 }