File indexing completed on 2024-05-19 04:49:18

0001 /****************************************************************************************
0002  * Copyright (c) 2010 Rick W. Chen <stuffcorpse@archlinux.us>                           *
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 #define DEBUG_PREFIX "TrashCollectionLocation"
0018 
0019 #include "TrashCollectionLocation.h"
0020 
0021 #include "core/collections/CollectionLocationDelegate.h"
0022 #include "core/logger/Logger.h"
0023 #include "core/support/Components.h"
0024 #include "core/support/Debug.h"
0025 
0026 
0027 #include <KIO/CopyJob>
0028 #include <KLocalizedString>
0029 
0030 #include <QFile>
0031 
0032 namespace Collections {
0033 
0034 TrashCollectionLocation::TrashCollectionLocation()
0035     : CollectionLocation()
0036     , m_trashConfirmed( false )
0037 {
0038 }
0039 
0040 TrashCollectionLocation::~TrashCollectionLocation()
0041 {
0042 }
0043 
0044 QString
0045 TrashCollectionLocation::prettyLocation() const
0046 {
0047     return i18n( "Trash" );
0048 }
0049 
0050 bool
0051 TrashCollectionLocation::isWritable() const
0052 {
0053     return true;
0054 }
0055 
0056 void
0057 TrashCollectionLocation::copyUrlsToCollection( const QMap<Meta::TrackPtr, QUrl> &sources,
0058                                                const Transcoding::Configuration &configuration )
0059 {
0060     DEBUG_BLOCK
0061     Q_UNUSED( configuration );
0062 
0063     if( sources.isEmpty() )
0064     {
0065         debug() << "Error: sources is empty";
0066         abort();
0067         return;
0068     }
0069 
0070     if( m_trashConfirmed )
0071     {
0072         QList<QUrl> files = sources.values();
0073         foreach( const QUrl &file, files )
0074         {
0075             if( !QFile::exists( file.toLocalFile() ) )
0076             {
0077                 debug() << "Error: file does not exist!" << file.toLocalFile();
0078                 abort();
0079                 return;
0080             }
0081         }
0082 
0083         KIO::CopyJob *job = KIO::trash( files, KIO::HideProgressInfo );
0084         connect( job, &KJob::result, this, &TrashCollectionLocation::slotTrashJobFinished );
0085 
0086         Meta::TrackList tracks = sources.keys();
0087         m_trashJobs.insert( job, tracks );
0088         QString name = tracks.takeFirst()->prettyName();
0089         if( !tracks.isEmpty() )
0090         {
0091             int max = 3;
0092             while( !tracks.isEmpty() && (max > 0) )
0093             {
0094                 name += QStringLiteral( ", %1" ).arg( tracks.takeFirst()->prettyName() );
0095                 --max;
0096             }
0097 
0098             if( max == 0 && !tracks.isEmpty() )
0099                 name += QLatin1String(" ...");
0100         }
0101         Amarok::Logger::newProgressOperation( job, i18n( "Moving to trash: %1", name ) );
0102     }
0103 }
0104 
0105 void
0106 TrashCollectionLocation::showDestinationDialog( const Meta::TrackList &tracks, bool removeSources, const Transcoding::Configuration &configuration )
0107 {
0108     Collections::CollectionLocationDelegate *delegate = Amarok::Components::collectionLocationDelegate();
0109     m_trashConfirmed = delegate->reallyTrash( source(), tracks );
0110     if( !m_trashConfirmed )
0111         abort();
0112     else
0113         CollectionLocation::showDestinationDialog( tracks, removeSources, configuration );
0114 }
0115 
0116 void
0117 TrashCollectionLocation::slotTrashJobFinished( KJob *job )
0118 {
0119     DEBUG_BLOCK
0120     if( job->error() )
0121     {
0122         warning() << "An error occurred when moving a file to trash: " << job->errorString();
0123         foreach( Meta::TrackPtr track, m_trashJobs.value( job ) )
0124             source()->transferError( track, KIO::buildErrorString( job->error(), job->errorString() ) );
0125     }
0126     else
0127     {
0128         foreach( Meta::TrackPtr track, m_trashJobs.value( job ) )
0129             source()->transferSuccessful( track );
0130     }
0131 
0132     m_trashJobs.remove( job );
0133     job->deleteLater();
0134     if( m_trashJobs.isEmpty() )
0135         slotCopyOperationFinished();
0136 }
0137 
0138 
0139 } //namespace Collections