1 /****************************************************************************************
2  * Copyright (c) 2012 Ryan McCoskrie <ryan.mccoskrie@gmail.com>                         *
3  * Copyright (c) 2010 Maximilian Kossick <maximilian.kossick@googlemail.com>            *
4  * Copyright (c) 2010 Casey Link <unnamedrambler@gmail.com>                             *
5  *                                                                                      *
6  * This program is free software; you can redistribute it and/or modify it under        *
7  * the terms of the GNU General Public License as published by the Free Software        *
8  * Foundation; either version 2 of the License, or (at your option) any later           *
9  * version.                                                                             *
10  *                                                                                      *
11  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
13  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
14  *                                                                                      *
15  * You should have received a copy of the GNU General Public License along with         *
16  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
17  ****************************************************************************************/
18 
19 #include "CollectionLocationDelegateImpl.h"
20 
21 #include "core/logger/Logger.h"
22 #include "core/collections/CollectionLocation.h"
23 #include "core/support/Components.h"
24 #include "transcoding/TranscodingAssistantDialog.h"
25 
26 #include <KLocalizedString>
27 #include <KMessageBox>
28 
29 using namespace Collections;
30 
31 bool
reallyDelete(CollectionLocation * loc,const Meta::TrackList & tracks) const32 CollectionLocationDelegateImpl::reallyDelete( CollectionLocation *loc, const Meta::TrackList &tracks ) const
33 {
34     QStringList files = trackList( tracks );
35     const QString text( i18ncp( "@info",
36         "Do you really want to delete this track? It will be removed from %2 and from underlying storage medium.",
37         "Do you really want to delete these %1 tracks? They will be removed from %2 and from underlying storage medium.",
38          tracks.count(), loc->prettyLocation()) );
39     int ret = KMessageBox::warningContinueCancelList(nullptr, text, files,
40         i18nc( "@title:window", "Confirm Delete" ), KStandardGuiItem::del() );
41     return ret == KMessageBox::Continue;
42 }
43 
44 bool
reallyTrash(CollectionLocation * loc,const Meta::TrackList & tracks) const45 CollectionLocationDelegateImpl::reallyTrash( CollectionLocation *loc, const Meta::TrackList &tracks ) const
46 {
47     QStringList files = trackList( tracks );
48     const QString text( i18ncp( "@info",
49         "Do you really want to move this track to the trash? It will be removed from %2.",
50         "Do you really want to move these %1 tracks to the trash? They will be removed from %2.",
51         tracks.count(), loc->prettyLocation() ) );
52     int ret = KMessageBox::warningContinueCancelList( nullptr, text, files,
53         i18nc( "@title:window", "Confirm Move to Trash" ), KStandardGuiItem::remove() );
54     return ret == KMessageBox::Continue;
55 }
56 
57 bool
reallyMove(CollectionLocation * loc,const Meta::TrackList & tracks) const58 CollectionLocationDelegateImpl::reallyMove( CollectionLocation *loc, const Meta::TrackList &tracks ) const
59 {
60     Q_UNUSED( loc )
61     QStringList files = trackList( tracks );
62     const QString text( i18ncp( "@info",
63         "Do you really want to move this track? It will be renamed and the original deleted.",
64         "Do you really want to move these %1 tracks? They will be renamed and the originals deleted.",
65         tracks.count() ) );
66     int ret = KMessageBox::warningContinueCancelList( nullptr, text, files,
67         i18nc( "@title:window", "Move Files" ), KGuiItem( i18nc( "rename files button", "&Rename" ), QStringLiteral("go-jump") ) );
68     return ret == KMessageBox::Continue;
69 }
70 
71 void
errorDeleting(CollectionLocation * loc,const Meta::TrackList & tracks) const72 CollectionLocationDelegateImpl::errorDeleting( CollectionLocation *loc, const Meta::TrackList &tracks ) const
73 {
74     Q_UNUSED( loc )
75     QStringList files = trackList( tracks );
76     const QString text( i18ncp( "@info",
77         "There was a problem and this track could not be removed. Make sure the directory is writable.",
78         "There was a problem and %1 tracks could not be removed. Make sure the directory is writable.",
79         files.count() ) );
80     KMessageBox::informationList( nullptr, text, files, i18n( "Unable to remove tracks") );
81 }
82 
83 void
notWriteable(CollectionLocation * loc) const84 CollectionLocationDelegateImpl::notWriteable( CollectionLocation *loc ) const
85 {
86     Q_UNUSED( loc )
87     Amarok::Logger::longMessage(
88             i18n( "The collection does not have enough free space available or is not writable." ),
89             Amarok::Logger::Error );
90 }
91 
92 bool
deleteEmptyDirs(CollectionLocation * loc) const93 CollectionLocationDelegateImpl::deleteEmptyDirs( CollectionLocation *loc ) const
94 {
95     const QString text( i18n( "Do you want to remove empty folders?" ) );
96     const QString caption( i18n( "Remove empty folders?" ) );
97     int result = KMessageBox::questionYesNo( nullptr, text, caption, KStandardGuiItem::yes(),
98         KStandardGuiItem::no(), QString( "Delete empty dirs from " + loc->prettyLocation() ) );
99     return result == KMessageBox::Yes;
100 }
101 
102 Transcoding::Configuration
transcode(const QStringList & playableFileTypes,bool * remember,OperationType operation,const QString & destCollectionName,const Transcoding::Configuration & prevConfiguration) const103 CollectionLocationDelegateImpl::transcode( const QStringList &playableFileTypes,
104                                            bool *remember, OperationType operation,
105                                            const QString &destCollectionName,
106                                            const Transcoding::Configuration &prevConfiguration ) const
107 {
108     Transcoding::AssistantDialog dialog( playableFileTypes, remember != 0, operation,
109                                          destCollectionName, prevConfiguration );
110     if( dialog.exec() )
111     {
112         if( remember )
113             *remember = dialog.shouldSave();
114         return dialog.configuration();
115     }
116     return Transcoding::Configuration( Transcoding::INVALID );
117 }
118 
119 QStringList
trackList(const Meta::TrackList & tracks) const120 CollectionLocationDelegateImpl::trackList( const Meta::TrackList &tracks ) const
121 {
122     QStringList trackList;
123     foreach( Meta::TrackPtr track, tracks )
124     {
125         QString url = track->prettyUrl();
126         Meta::ArtistPtr artist = track->artist();
127         QString artistName = artist ? artist->name() : QString();
128         QString trackName = track->name();
129 
130         QString str;
131         // Add track and artist name if available
132         if( !trackName.isEmpty() && !artistName.isEmpty() )
133             str = i18nc( "%1 is track url, %2 track title, %3 track artist",
134                          "%1 (%2 by %3)", url, trackName, artistName );
135         else if( !trackName.isEmpty() )
136             str = i18nc( "%1 is track url, %2 track name", "%1 (%2)", url, trackName );
137         else if( !artistName.isEmpty() )
138             str = i18nc( "%1 is track url, %2 artist name", "%1 (by %2)", url, artistName );
139         else
140             str = url;
141 
142         trackList << str;
143     }
144 
145     return trackList;
146 }
147