File indexing completed on 2024-05-05 04:47:22

0001 /****************************************************************************************
0002  * Copyright (c) 2009 Casey Link <unnamedrambler@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 "PlayUrlRunner.h"
0018 
0019 #include "core/support/Debug.h"
0020 #include "amarokconfig.h"
0021 #include "AmarokUrl.h"
0022 #include "AmarokUrlHandler.h"
0023 #include "core-impl/storage/StorageManager.h"
0024 #include "core-impl/collections/support/CollectionManager.h"
0025 #include "EngineController.h"
0026 #include "playlist/PlaylistController.h"
0027 #include "playlist/PlaylistModelStack.h"
0028 #include "playlist/proxymodels/AbstractModel.h"
0029 #include <core/storage/SqlStorage.h>
0030 #include "core/meta/Meta.h"
0031 
0032 PlayUrlRunner::PlayUrlRunner() : AmarokUrlRunnerBase()
0033 {
0034 }
0035 
0036 PlayUrlRunner::~PlayUrlRunner()
0037 {
0038     The::amarokUrlHandler()->unRegisterRunner ( this );
0039 }
0040 
0041 bool PlayUrlRunner::run (const AmarokUrl &url )
0042 {
0043     DEBUG_BLOCK
0044     if ( url.isNull() )
0045         return false;
0046 
0047     QUrl track_url = QUrl::fromEncoded ( QByteArray::fromBase64 ( url.path().toUtf8() ) );
0048     debug() << "decoded track url: " << track_url.toString();
0049 
0050     //get the position
0051     qint64 pos = 0;
0052     if ( url.args().keys().contains( QStringLiteral("pos") ) )
0053     {
0054         pos = (qint64) ( url.args().value( QStringLiteral("pos") ).toDouble() * 1000.0 );
0055     }
0056 
0057     debug() << "seek pos: " << pos;
0058     Meta::TrackPtr track = CollectionManager::instance()->trackForUrl( track_url );
0059     if ( !track )
0060         return false;
0061 
0062     The::engineController()->play ( track, pos );
0063 
0064     Playlist::AbstractModel *model = The::playlist();
0065 
0066     int row = model->firstRowForTrack( track );
0067     if( row != -1 )
0068         model->setActiveRow( row );
0069     else
0070     {
0071         row = AmarokConfig::dynamicMode() ? model->activeRow() + 1 : model->qaim()->rowCount();
0072         The::playlistController()->insertTrack( row, track );
0073         model->setActiveRow( row );
0074     }
0075 
0076     return true;
0077 }
0078 
0079 QString PlayUrlRunner::command() const
0080 {
0081     return QStringLiteral("play");
0082 }
0083 
0084 QString PlayUrlRunner::prettyCommand() const
0085 {
0086     return i18nc( "A type of command that starts playing at a specific position in a track", "Play" );
0087 }
0088 
0089 BookmarkList PlayUrlRunner::bookmarksFromUrl( const QUrl &url )
0090 {
0091     BookmarkList list;
0092 
0093     //See PlayUrlGenerator for the description of a 'play' amarokurl
0094     QString track_encoded = url.toEncoded().toBase64();
0095 
0096     // The last character of a base64 encoded string is always '=', which
0097     // chokes the SQL. Since we are using a substring like text comparison
0098     // and every url in the database will have the '=', just chop it off.
0099 
0100     // some tracks even seem to have multiple '='s at the end... chop them all off!
0101     while( track_encoded.endsWith( '=' ) )
0102         track_encoded.chop ( 1 );
0103 
0104     // Queries the database for bookmarks where the url field contains
0105     // the base64 encoded url (minus the '=').
0106     QString query = QStringLiteral("SELECT id, parent_id, name, url, description, custom FROM bookmarks WHERE url LIKE '%%1%'");
0107     query = query.arg ( track_encoded );
0108     QStringList result = StorageManager::instance()->sqlStorage()->query ( query );
0109 
0110     int resultRows = result.count() / 6;
0111 
0112     for ( int i = 0; i < resultRows; i++ )
0113     {
0114         QStringList row = result.mid ( i*6, 6 );
0115         list << AmarokUrlPtr ( new AmarokUrl ( row ) );
0116     }
0117     return list;
0118 }
0119 
0120 QIcon PlayUrlRunner::icon() const
0121 {
0122     return QIcon::fromTheme( QStringLiteral("x-media-podcast-amarok") );
0123 }
0124