File indexing completed on 2025-01-05 04:25:50

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 
0018 #include "TimecodeWriteCapability.h"
0019 
0020 #include "EngineController.h"
0021 #include "amarokurls/AmarokUrl.h"
0022 #include "amarokurls/AmarokUrlHandler.h"
0023 #include "amarokurls/BookmarkModel.h"
0024 #include "amarokurls/PlayUrlGenerator.h"
0025 #include "core/meta/Meta.h"
0026 #include "core/support/Debug.h"
0027 #include "core-impl/capabilities/timecode/TimecodeLoadCapability.h"
0028 #include "widgets/ProgressWidget.h"
0029 
0030 #include <KLocalizedString>
0031 
0032 namespace Capabilities
0033 {
0034 
0035 TimecodeWriteCapability::~TimecodeWriteCapability()
0036 {}
0037 
0038 bool TimecodeWriteCapability::writeTimecode( qint64 miliseconds, const Meta::TrackPtr &track )
0039 {
0040     DEBUG_BLOCK
0041     AmarokUrl url = PlayUrlGenerator::instance()->createTrackBookmark( track, miliseconds );
0042 
0043     // lets see if we are bookmarking the currently playing track, if so
0044     // we need to update the slider with another icon
0045     Meta::TrackPtr currtrack = The::engineController()->currentTrack();
0046     if( currtrack  == track )
0047     {
0048         debug() << " current track";
0049         debug() << "adding at seconds: " << miliseconds;
0050         The::amarokUrlHandler()->paintNewTimecode( url.name(), miliseconds );
0051 
0052     }
0053 
0054     url.saveToDb();
0055     BookmarkModel::instance()->reloadFromDb(); //Update bookmark manager view.
0056     return true;
0057 }
0058 
0059 bool Capabilities::TimecodeWriteCapability::writeAutoTimecode( qint64 miliseconds, Meta::TrackPtr track )
0060 {
0061     DEBUG_BLOCK
0062 
0063     //first up, find and delete any previously added auto timecodes for this track
0064 
0065     debug() << "deleting old auto timecodes";
0066     if( track->has<Capabilities::TimecodeLoadCapability>() )
0067     {
0068         QScopedPointer<TimecodeLoadCapability> tcl( track->create<TimecodeLoadCapability>() );
0069         BookmarkList list = tcl->loadTimecodes();
0070         foreach( AmarokUrlPtr oldUrl, list )
0071         {
0072             if( oldUrl->command() == QLatin1String("play")  ) {
0073                 if( oldUrl->customValue() == QLatin1String("auto timecode") ) {
0074                     debug() << "deleting: " << oldUrl->name();
0075                     oldUrl->removeFromDb();
0076                 }
0077             }
0078         }
0079     }
0080 
0081     //create url
0082     AmarokUrl url = PlayUrlGenerator::instance()->createTrackBookmark( track, miliseconds );
0083 
0084     // lets see if we are bookmarking the currently playing track, if so
0085     // we need to update the slider with another icon
0086 
0087     Meta::TrackPtr currtrack = The::engineController()->currentTrack();
0088     if( currtrack == track )
0089     {
0090         debug() << " current track";
0091         QMap<QString, QString> args = url.args();
0092         if ( args.keys().contains( QStringLiteral("pos") ) )
0093         {
0094             int pos = args.value( QStringLiteral("pos") ).toInt();
0095             The::amarokUrlHandler()->paintNewTimecode( url.name(), pos * 1000 );
0096         }
0097     }
0098 
0099     //change the name a little bit
0100     url.setCustomValue( QStringLiteral("auto timecode") );
0101 
0102     QString date = QDateTime::currentDateTime().toString( QStringLiteral("dd.MM.yyyy") );
0103     url.setName( i18n( "%1 - Stopped %2", track->prettyName(), date ) );
0104 
0105     debug() << "creating new auto timecode: " << url.name();
0106 
0107     //put in custom group to ensure that we do not clutter the list of bookmarks.
0108     BookmarkGroupPtr parentPtr = BookmarkGroupPtr( new BookmarkGroup( i18n( "Playback Ended Markers" ), QStringLiteral("auto_markers") ) );
0109     url.reparent( parentPtr );
0110 
0111     //save it
0112     url.saveToDb();
0113     BookmarkModel::instance()->reloadFromDb(); //Update bookmark manager view.
0114     return true;
0115 }
0116 
0117 }
0118 
0119 
0120 
0121