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

0001 /****************************************************************************************
0002  * Copyright (c) 2012 Matěj Laitl <matej@laitl.cz>                                      *
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 "IpodDeleteTracksJob.h"
0018 
0019 #include "core/logger/Logger.h"
0020 #include "core/support/Components.h"
0021 #include "core-impl/collections/ipodcollection/IpodMeta.h"
0022 
0023 #include <QFile>
0024 
0025 #include <gpod/itdb.h>
0026 
0027 IpodDeleteTracksJob::IpodDeleteTracksJob( const Meta::TrackList &sources,
0028                                           const QPointer<IpodCollection> &collection )
0029     : QObject()
0030     , ThreadWeaver::Job()
0031     , m_sources( sources )
0032     , m_coll( collection )
0033 {
0034 }
0035 
0036 void
0037 IpodDeleteTracksJob::run(ThreadWeaver::JobPointer self, ThreadWeaver::Thread *thread)
0038 {
0039     Q_UNUSED(self);
0040     Q_UNUSED(thread);
0041     if( !m_coll )
0042         return;
0043     int trackCount = m_sources.size();
0044     QString operationText = i18np( "Removing one track from iPod",
0045                                    "Removing %1 tracks from iPod", trackCount );
0046     Amarok::Logger::newProgressOperation( this, operationText, trackCount );
0047     itdb_start_sync( m_coll->m_itdb );
0048 
0049     QListIterator<Meta::TrackPtr> it( m_sources );
0050     while( it.hasNext() )
0051     {
0052         if( !m_coll )
0053             break;
0054 
0055         /* delete the file first, then remove from database. Dangling entry in iTunes db
0056          * is better than dangling file */
0057         Meta::TrackPtr track = it.next();
0058         QFile file( track->playableUrl().path() );  // iPod files are always local, QFile suffices
0059         bool success = true;
0060         if( file.exists() )
0061             success = file.remove();
0062         if( success )
0063             m_coll->removeTrack( track );
0064 
0065         incrementProgress();
0066     }
0067 
0068     Q_EMIT endProgressOperation( this );
0069     if( m_coll )
0070         itdb_stop_sync( m_coll->m_itdb );
0071 }
0072 
0073 void
0074 IpodDeleteTracksJob::defaultBegin(const ThreadWeaver::JobPointer& self, ThreadWeaver::Thread *thread)
0075 {
0076     Q_EMIT started(self);
0077     ThreadWeaver::Job::defaultBegin(self, thread);
0078 }
0079 
0080 void
0081 IpodDeleteTracksJob::defaultEnd(const ThreadWeaver::JobPointer& self, ThreadWeaver::Thread *thread)
0082 {
0083     ThreadWeaver::Job::defaultEnd(self, thread);
0084     if (!self->success()) {
0085         Q_EMIT failed(self);
0086     }
0087     Q_EMIT done(self);
0088 }