File indexing completed on 2024-05-05 04:48:20
0001 /**************************************************************************************** 0002 * Copyright (c) 2008 Seb Ruiz <ruiz@kde.org> * 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 "CoverFetchingActions" 0018 0019 #include "CoverFetchingActions.h" 0020 0021 #include "core/support/Debug.h" 0022 #include "MainWindow.h" 0023 #include "CoverFetcher.h" 0024 #include "CoverManager.h" 0025 #include "CoverViewDialog.h" 0026 0027 #include <QApplication> 0028 #include <QDesktopWidget> 0029 #include <QFileDialog> 0030 #include <QIcon> 0031 #include <QImageReader> 0032 #include <QTemporaryDir> 0033 0034 #include <KConfigGroup> 0035 #include <KDirOperator> 0036 #include <KFile> 0037 #include <KIO/CopyJob> 0038 #include <KLocalizedString> 0039 #include <KMessageBox> 0040 0041 ///////////////////////////////////// 0042 // FetchCoverAction 0043 ///////////////////////////////////// 0044 0045 void FetchCoverAction::init() 0046 { 0047 setText( i18np("Fetch Cover", "Fetch Covers", m_albums.count()) ); 0048 setIcon( QIcon::fromTheme(QStringLiteral("insert-image")) ); 0049 setToolTip( i18np("Fetch the artwork for this album", "Fetch artwork for %1 albums", m_albums.count()) ); 0050 0051 bool enabled = !m_albums.isEmpty(); 0052 foreach( Meta::AlbumPtr album, m_albums ) 0053 { 0054 enabled &= album->canUpdateImage(); 0055 } 0056 setEnabled( enabled ); 0057 } 0058 0059 void FetchCoverAction::slotTriggered() 0060 { 0061 // Queuing multiple albums causes the fetcher to automatically assign values without asking 0062 // Such as case would be the CoverManager's Fetch All Missing feature 0063 if( m_albums.size() == 1 ) 0064 CoverFetcher::instance()->manualFetch( m_albums.first() ); 0065 else 0066 CoverFetcher::instance()->queueAlbums( m_albums ); 0067 } 0068 0069 0070 ///////////////////////////////////// 0071 // DisplayCoverAction 0072 ///////////////////////////////////// 0073 0074 void DisplayCoverAction::init() 0075 { 0076 setText( i18n("Display Cover") ); 0077 setIcon( QIcon::fromTheme(QStringLiteral("zoom-original")) ); 0078 setToolTip( i18n("Display artwork for this album") ); 0079 Meta::AlbumPtr album = m_albums.first(); 0080 if( album ) 0081 setEnabled( album->hasImage() ); 0082 } 0083 0084 void DisplayCoverAction::slotTriggered() 0085 { 0086 ( new CoverViewDialog( m_albums.first(), The::mainWindow() ) )->show(); 0087 } 0088 0089 0090 ///////////////////////////////////// 0091 // UnsetCoverAction 0092 ///////////////////////////////////// 0093 0094 void UnsetCoverAction::init() 0095 { 0096 setText( i18np("Unset Cover", "Unset Covers", m_albums.count()) ); 0097 setIcon( QIcon::fromTheme(QStringLiteral("list-remove")) ); 0098 setToolTip( i18np("Remove artwork for this album", "Remove artwork for %1 albums", m_albums.count()) ); 0099 0100 // this action is enabled if any one of the albums has an image and can be updated 0101 bool enabled = false; 0102 foreach( Meta::AlbumPtr album, m_albums ) 0103 enabled |= ( album->hasImage() && album->canUpdateImage() ); 0104 setEnabled( enabled ); 0105 } 0106 0107 void 0108 UnsetCoverAction::slotTriggered() 0109 { 0110 int button = KMessageBox::warningContinueCancel( qobject_cast<QWidget*>( parent() ), 0111 i18np( "Are you sure you want to remove this cover from the Collection?", 0112 "Are you sure you want to delete these %1 covers from the Collection?", 0113 m_albums.count() ), 0114 QString(), 0115 KStandardGuiItem::del() ); 0116 0117 if ( button == KMessageBox::Continue ) 0118 { 0119 foreach( Meta::AlbumPtr album, m_albums ) 0120 { 0121 if( album && album->canUpdateImage() ) 0122 album->removeImage(); 0123 } 0124 qApp->processEvents(); 0125 } 0126 } 0127 0128 ///////////////////////////////////// 0129 // SetCustomCoverAction 0130 ///////////////////////////////////// 0131 0132 void SetCustomCoverAction::init() 0133 { 0134 setText( i18n("Set Custom Cover") ); 0135 setIcon( QIcon::fromTheme(QStringLiteral("document-open")) ); 0136 setToolTip( i18np("Set custom artwork for this album", "Set custom artwork for these %1 albums", m_albums.count()) ); 0137 0138 // this action is enabled if any one of the albums can be updated 0139 bool enabled = false; 0140 0141 foreach( Meta::AlbumPtr album, m_albums ) 0142 if( album ) 0143 enabled |= album->canUpdateImage(); 0144 0145 setEnabled( enabled ); 0146 } 0147 0148 void 0149 SetCustomCoverAction::slotTriggered() 0150 { 0151 if( m_albums.isEmpty() || m_albums.first()->tracks().isEmpty() ) 0152 return; 0153 0154 const QString& startPath = m_albums.first()->tracks().first()->playableUrl().adjusted(QUrl::RemoveFilename).path(); 0155 0156 const auto mt( QImageReader::supportedMimeTypes() ); 0157 QStringList mimetypes; 0158 for( const auto &mimetype : mt ) 0159 mimetypes << QString( mimetype ); 0160 0161 QFileDialog dlg; 0162 0163 dlg.setDirectory( startPath ); 0164 dlg.setAcceptMode( QFileDialog::AcceptOpen ); 0165 dlg.setFileMode( QFileDialog::ExistingFile ); 0166 dlg.setMimeTypeFilters( mimetypes ); 0167 dlg.setWindowTitle( i18n("Select Cover Image File") ); 0168 0169 dlg.exec(); 0170 QUrl file = dlg.selectedUrls().value( 0 ); 0171 0172 if( !file.isEmpty() ) 0173 { 0174 QImage image; 0175 0176 if( file.isLocalFile() ) 0177 { 0178 image.load( file.path() ); 0179 } 0180 else 0181 { 0182 debug() << "Custom Cover Fetch: " << file.toDisplayString(); 0183 0184 QTemporaryDir tempDir; 0185 tempDir.setAutoRemove( true ); 0186 0187 const QString coverDownloadPath = tempDir.path() + QLatin1Char('/') + file.fileName(); 0188 0189 auto copyJob = KIO::copy( file, QUrl::fromLocalFile( coverDownloadPath ) ); 0190 bool ret = copyJob->exec(); 0191 0192 if( ret ) 0193 image.load( coverDownloadPath ); 0194 } 0195 0196 if( !image.isNull() ) 0197 { 0198 foreach( Meta::AlbumPtr album, m_albums ) 0199 { 0200 if( album && album->canUpdateImage() ) 0201 album->setImage( image ); 0202 } 0203 } 0204 } 0205 } 0206 0207