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 "PlayUrlGenerator.h"
0018 
0019 #include "AmarokUrl.h"
0020 #include "AmarokUrlHandler.h"
0021 #include "core/meta/Meta.h"
0022 #include "core/meta/support/MetaUtility.h"
0023 #include "core/support/Debug.h"
0024 #include "EngineController.h"
0025 #include "BookmarkModel.h"
0026 
0027 #include <KLocalizedString>
0028 
0029 PlayUrlGenerator * PlayUrlGenerator::s_instance = nullptr;
0030 
0031 PlayUrlGenerator * PlayUrlGenerator::instance()
0032 {
0033     if( s_instance == nullptr)
0034         s_instance = new PlayUrlGenerator();
0035 
0036     return s_instance;
0037 }
0038 
0039 PlayUrlGenerator::PlayUrlGenerator()
0040 {
0041 }
0042 
0043 
0044 PlayUrlGenerator::~PlayUrlGenerator()
0045 {
0046     The::amarokUrlHandler()->unRegisterGenerator( this );
0047 }
0048 
0049 AmarokUrl
0050 PlayUrlGenerator::createCurrentTrackBookmark()
0051 {
0052     Meta::TrackPtr track = The::engineController()->currentTrack();
0053     const qint64 miliseconds = The::engineController()->trackPositionMs();
0054 
0055     return createTrackBookmark( track, miliseconds );
0056 }
0057 
0058 AmarokUrl
0059 PlayUrlGenerator::createTrackBookmark( Meta::TrackPtr track, qint64 miliseconds, const QString &name )
0060 {
0061     DEBUG_BLOCK
0062 
0063     const int seconds = miliseconds / 1000;
0064     const qreal accurateSeconds = (qreal) miliseconds / 1000.0;
0065     QString secondsString = QString::number( accurateSeconds );
0066 
0067     AmarokUrl url;
0068     if( !track )
0069         return url;
0070 
0071     const QString trackUrl = track->playableUrl().toEncoded().toBase64();
0072     url.setCommand( QStringLiteral("play") );
0073     url.setPath( trackUrl );
0074     url.setArg( QStringLiteral("pos"), secondsString );
0075 
0076     if( name.isEmpty() )
0077         url.setName( track->prettyName() + " - " + Meta::secToPrettyTime( seconds ) );
0078     else
0079         url.setName( name + " - " + Meta::secToPrettyTime( seconds ) );
0080 
0081     debug() << "concocted url: " << url.url();
0082     debug() << "pos: " << accurateSeconds;
0083     return url;
0084 }
0085 
0086 void
0087 PlayUrlGenerator::moveTrackBookmark( Meta::TrackPtr track, qint64 newMiliseconds, const QString &name )
0088 {
0089     qreal seconds = qreal ( newMiliseconds ) / 1000;
0090     QString trackPosition;
0091     trackPosition.setNum( seconds );
0092 
0093     QString trackName = track->prettyName();
0094     QString newName = ( trackName + " - " + Meta::msToPrettyTime( newMiliseconds ) );
0095 
0096     BookmarkModel::instance()->setBookmarkArg( name, QStringLiteral("pos"), trackPosition );
0097     BookmarkModel::instance()->renameBookmark( name, newName );
0098 }
0099 
0100 QString
0101 PlayUrlGenerator::description()
0102 {
0103     return i18n( "Bookmark Track Position" );
0104 }
0105 
0106 QIcon PlayUrlGenerator::icon()
0107 {
0108     return QIcon::fromTheme( QStringLiteral("x-media-podcast-amarok") );
0109 }
0110 
0111 AmarokUrl
0112 PlayUrlGenerator::createUrl()
0113 {
0114     return createCurrentTrackBookmark();
0115 }
0116