1 /****************************************************************************************
2  * Copyright (c) 2010 Rick W. Chen <stuffcorpse@archlinux.us>                           *
3  *                                                                                      *
4  * This program is free software; you can redistribute it and/or modify it under        *
5  * the terms of the GNU General Public License as published by the Free Software        *
6  * Foundation; either version 2 of the License, or (at your option) any later           *
7  * version.                                                                             *
8  *                                                                                      *
9  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
10  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
11  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
12  *                                                                                      *
13  * You should have received a copy of the GNU General Public License along with         *
14  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
15  ****************************************************************************************/
16 
17 #define DEBUG_PREFIX "TrashCollectionLocation"
18 
19 #include "TrashCollectionLocation.h"
20 
21 #include "core/collections/CollectionLocationDelegate.h"
22 #include "core/logger/Logger.h"
23 #include "core/support/Components.h"
24 #include "core/support/Debug.h"
25 
26 
27 #include <KIO/CopyJob>
28 #include <KLocalizedString>
29 
30 #include <QFile>
31 
32 namespace Collections {
33 
TrashCollectionLocation()34 TrashCollectionLocation::TrashCollectionLocation()
35     : CollectionLocation()
36     , m_trashConfirmed( false )
37 {
38 }
39 
~TrashCollectionLocation()40 TrashCollectionLocation::~TrashCollectionLocation()
41 {
42 }
43 
44 QString
prettyLocation() const45 TrashCollectionLocation::prettyLocation() const
46 {
47     return i18n( "Trash" );
48 }
49 
50 bool
isWritable() const51 TrashCollectionLocation::isWritable() const
52 {
53     return true;
54 }
55 
56 void
copyUrlsToCollection(const QMap<Meta::TrackPtr,QUrl> & sources,const Transcoding::Configuration & configuration)57 TrashCollectionLocation::copyUrlsToCollection( const QMap<Meta::TrackPtr, QUrl> &sources,
58                                                const Transcoding::Configuration &configuration )
59 {
60     DEBUG_BLOCK
61     Q_UNUSED( configuration );
62 
63     if( sources.isEmpty() )
64     {
65         debug() << "Error: sources is empty";
66         abort();
67         return;
68     }
69 
70     if( m_trashConfirmed )
71     {
72         QList<QUrl> files = sources.values();
73         foreach( const QUrl &file, files )
74         {
75             if( !QFile::exists( file.toLocalFile() ) )
76             {
77                 debug() << "Error: file does not exist!" << file.toLocalFile();
78                 abort();
79                 return;
80             }
81         }
82 
83         KIO::CopyJob *job = KIO::trash( files, KIO::HideProgressInfo );
84         connect( job, &KJob::result, this, &TrashCollectionLocation::slotTrashJobFinished );
85 
86         Meta::TrackList tracks = sources.keys();
87         m_trashJobs.insert( job, tracks );
88         QString name = tracks.takeFirst()->prettyName();
89         if( !tracks.isEmpty() )
90         {
91             int max = 3;
92             while( !tracks.isEmpty() && (max > 0) )
93             {
94                 name += QStringLiteral( ", %1" ).arg( tracks.takeFirst()->prettyName() );
95                 --max;
96             }
97 
98             if( max == 0 && !tracks.isEmpty() )
99                 name += QLatin1String(" ...");
100         }
101         Amarok::Logger::newProgressOperation( job, i18n( "Moving to trash: %1", name ) );
102     }
103 }
104 
105 void
showDestinationDialog(const Meta::TrackList & tracks,bool removeSources,const Transcoding::Configuration & configuration)106 TrashCollectionLocation::showDestinationDialog( const Meta::TrackList &tracks, bool removeSources, const Transcoding::Configuration &configuration )
107 {
108     Collections::CollectionLocationDelegate *delegate = Amarok::Components::collectionLocationDelegate();
109     m_trashConfirmed = delegate->reallyTrash( source(), tracks );
110     if( !m_trashConfirmed )
111         abort();
112     else
113         CollectionLocation::showDestinationDialog( tracks, removeSources, configuration );
114 }
115 
116 void
slotTrashJobFinished(KJob * job)117 TrashCollectionLocation::slotTrashJobFinished( KJob *job )
118 {
119     DEBUG_BLOCK
120     if( job->error() )
121     {
122         warning() << "An error occurred when moving a file to trash: " << job->errorString();
123         foreach( Meta::TrackPtr track, m_trashJobs.value( job ) )
124             source()->transferError( track, KIO::buildErrorString( job->error(), job->errorString() ) );
125     }
126     else
127     {
128         foreach( Meta::TrackPtr track, m_trashJobs.value( job ) )
129             source()->transferSuccessful( track );
130     }
131 
132     m_trashJobs.remove( job );
133     job->deleteLater();
134     if( m_trashJobs.isEmpty() )
135         slotCopyOperationFinished();
136 }
137 
138 
139 } //namespace Collections
140