2011-01-24 01:09:57 +01:00
|
|
|
/* This file is part of Clementine.
|
|
|
|
|
|
|
|
Copyright 2010, David Sansome <me@davidsansome.com>
|
|
|
|
|
|
|
|
Clementine is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Clementine is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with Clementine. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "core/albumcoverloader.h"
|
2011-01-24 18:53:31 +01:00
|
|
|
#include "library/librarybackend.h"
|
2011-01-24 01:09:57 +01:00
|
|
|
#include "ui/albumcoverchoicecontroller.h"
|
|
|
|
#include "ui/albumcovermanager.h"
|
|
|
|
#include "ui/coverfromurldialog.h"
|
|
|
|
#include "ui/iconloader.h"
|
|
|
|
|
2011-01-31 16:21:32 +01:00
|
|
|
#ifdef HAVE_LIBLASTFM
|
|
|
|
# include "ui/albumcoversearcher.h"
|
|
|
|
# include "core/albumcoverfetcher.h"
|
|
|
|
#endif
|
|
|
|
|
2011-01-26 00:33:27 +01:00
|
|
|
#include <QAction>
|
2011-01-24 18:53:31 +01:00
|
|
|
#include <QCryptographicHash>
|
2011-01-24 01:09:57 +01:00
|
|
|
#include <QDialog>
|
|
|
|
#include <QFileDialog>
|
2011-02-02 17:22:04 +01:00
|
|
|
#include <QImageWriter>
|
2011-01-24 01:09:57 +01:00
|
|
|
#include <QLabel>
|
2011-01-26 00:33:27 +01:00
|
|
|
#include <QList>
|
|
|
|
#include <QMenu>
|
2011-01-24 01:09:57 +01:00
|
|
|
|
2011-02-02 17:22:04 +01:00
|
|
|
const char* AlbumCoverChoiceController::kLoadImageFileFilter =
|
|
|
|
QT_TR_NOOP("Images (*.png *.jpg *.jpeg *.bmp *.gif *.xpm *.pbm *.pgm *.ppm *.xbm)");
|
|
|
|
const char* AlbumCoverChoiceController::kSaveImageFileFilter =
|
|
|
|
QT_TR_NOOP("Images (*.png *.jpg *.jpeg *.bmp *.xpm *.pbm *.ppm *.xbm)");
|
2011-01-24 01:09:57 +01:00
|
|
|
const char* AlbumCoverChoiceController::kAllFilesFilter =
|
|
|
|
QT_TR_NOOP("All files (*)");
|
|
|
|
|
2011-01-26 00:33:27 +01:00
|
|
|
AlbumCoverChoiceController::AlbumCoverChoiceController(QWidget* parent)
|
2011-01-24 01:09:57 +01:00
|
|
|
: QWidget(parent),
|
|
|
|
#ifdef HAVE_LIBLASTFM
|
|
|
|
cover_searcher_(new AlbumCoverSearcher(QIcon(":/nocover.png"), this)),
|
|
|
|
cover_fetcher_(new AlbumCoverFetcher(this)),
|
|
|
|
#endif
|
2011-02-02 17:22:04 +01:00
|
|
|
save_file_dialog_(NULL),
|
2011-01-24 18:53:31 +01:00
|
|
|
cover_from_url_dialog_(NULL),
|
2011-02-02 17:22:04 +01:00
|
|
|
library_(NULL)
|
2011-01-24 01:09:57 +01:00
|
|
|
{
|
|
|
|
#ifdef HAVE_LIBLASTFM
|
|
|
|
cover_searcher_->Init(cover_fetcher_);
|
|
|
|
#endif
|
2011-01-26 00:33:27 +01:00
|
|
|
|
2011-02-02 17:22:04 +01:00
|
|
|
cover_from_file_ = new QAction(IconLoader::Load("document-open"), tr("Load cover from disk..."), this);
|
|
|
|
cover_to_file_ = new QAction(IconLoader::Load("document-save"), tr("Save cover to disk..."), this);
|
|
|
|
cover_from_url_ = new QAction(IconLoader::Load("download"), tr("Load cover from URL..."), this);
|
|
|
|
search_for_cover_ = new QAction(IconLoader::Load("find"), tr("Search for album covers..."), this);
|
|
|
|
unset_cover_ = new QAction(IconLoader::Load("list-remove"), tr("Unset cover"), this);
|
|
|
|
show_cover_ = new QAction(IconLoader::Load("zoom-in"), tr("Show fullsize..."), this);
|
|
|
|
|
|
|
|
separator_ = new QAction(this);
|
|
|
|
separator_->setSeparator(true);
|
2011-01-24 01:09:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
AlbumCoverChoiceController::~AlbumCoverChoiceController()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2011-01-26 00:33:27 +01:00
|
|
|
QList<QAction*> AlbumCoverChoiceController::GetAllActions() {
|
2011-02-02 17:22:04 +01:00
|
|
|
return QList<QAction*>() << cover_from_file_ << cover_to_file_
|
|
|
|
<< separator_
|
|
|
|
<< cover_from_url_ << search_for_cover_
|
2011-01-31 21:53:38 +01:00
|
|
|
<< unset_cover_ << show_cover_;
|
2011-01-26 00:33:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void AlbumCoverChoiceController::SetLibrary(LibraryBackend* library) {
|
|
|
|
library_ = library;
|
2011-01-24 19:32:09 +01:00
|
|
|
}
|
|
|
|
|
2011-01-24 18:53:31 +01:00
|
|
|
QString AlbumCoverChoiceController::LoadCoverFromFile(Song* song) {
|
2011-01-24 01:09:57 +01:00
|
|
|
QString cover = QFileDialog::getOpenFileName(
|
2011-02-08 00:53:32 +01:00
|
|
|
this, tr("Choose manual cover"), GetInitialPathForFileDialog(*song, QString()),
|
2011-02-02 17:22:04 +01:00
|
|
|
tr(kLoadImageFileFilter) + ";;" + tr(kAllFilesFilter));
|
2011-01-24 01:09:57 +01:00
|
|
|
|
|
|
|
if (cover.isNull())
|
2011-01-24 18:53:31 +01:00
|
|
|
return QString();
|
2011-01-24 01:09:57 +01:00
|
|
|
|
|
|
|
// Can we load the image?
|
|
|
|
QImage image(cover);
|
|
|
|
|
2011-01-24 18:53:31 +01:00
|
|
|
if(!image.isNull()) {
|
|
|
|
SaveCover(song, cover);
|
|
|
|
return cover;
|
|
|
|
} else {
|
|
|
|
return QString();
|
|
|
|
}
|
2011-01-24 01:09:57 +01:00
|
|
|
}
|
|
|
|
|
2011-02-02 17:22:04 +01:00
|
|
|
void AlbumCoverChoiceController::SaveCoverToFile(const Song& song, const QImage& image) {
|
2011-02-08 00:53:32 +01:00
|
|
|
QString initial_file_name = "/" + (song.album().isEmpty()
|
|
|
|
? tr("Unknown")
|
|
|
|
: song.album()) + ".jpg";
|
2011-02-02 17:22:04 +01:00
|
|
|
|
2011-02-08 00:53:32 +01:00
|
|
|
QString save_filename = QFileDialog::getSaveFileName(
|
|
|
|
this, tr("Save album cover"), GetInitialPathForFileDialog(song, initial_file_name),
|
|
|
|
tr(kSaveImageFileFilter) + ";;" + tr(kAllFilesFilter));
|
2011-02-02 17:22:04 +01:00
|
|
|
|
2011-02-08 00:53:32 +01:00
|
|
|
if(save_filename.isNull())
|
2011-02-02 17:22:04 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
QString extension = save_filename.right(4);
|
|
|
|
if (!extension.startsWith('.') ||
|
|
|
|
!QImageWriter::supportedImageFormats().contains(extension.right(3).toUtf8())) {
|
|
|
|
save_filename.append(".jpg");
|
|
|
|
}
|
|
|
|
|
|
|
|
image.save(save_filename);
|
|
|
|
}
|
|
|
|
|
2011-02-08 00:53:32 +01:00
|
|
|
QString AlbumCoverChoiceController::GetInitialPathForFileDialog(const Song& song,
|
|
|
|
const QString& filename) {
|
|
|
|
// art automatic is first to show user which cover the album may be
|
|
|
|
// using now; the song is using it if there's no manual path but we
|
|
|
|
// cannot use manual path here because it can contain cached paths
|
|
|
|
if (!song.art_automatic().isEmpty() && !song.has_embedded_cover()) {
|
|
|
|
return song.art_automatic();
|
|
|
|
|
|
|
|
// if no automatic art, start in the song's folder
|
|
|
|
} else if (!song.filename().isEmpty() && song.filename().contains('/')) {
|
|
|
|
return song.filename().section('/', 0, -2) + filename;
|
|
|
|
|
|
|
|
// fallback - start in home
|
|
|
|
} else {
|
|
|
|
return QDir::home().absolutePath() + filename;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-24 18:53:31 +01:00
|
|
|
QString AlbumCoverChoiceController::LoadCoverFromURL(Song* song) {
|
2011-01-24 01:09:57 +01:00
|
|
|
if(!cover_from_url_dialog_) {
|
|
|
|
cover_from_url_dialog_ = new CoverFromURLDialog(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
QImage image = cover_from_url_dialog_->Exec();
|
2011-01-24 18:53:31 +01:00
|
|
|
|
|
|
|
if(!image.isNull()) {
|
|
|
|
QString cover = SaveCoverInCache(song->artist(), song->album(), image);
|
|
|
|
SaveCover(song, cover);
|
|
|
|
|
|
|
|
return cover;
|
|
|
|
} else {
|
|
|
|
return QString();
|
|
|
|
}
|
2011-01-24 01:09:57 +01:00
|
|
|
}
|
|
|
|
|
2011-01-24 18:53:31 +01:00
|
|
|
QString AlbumCoverChoiceController::SearchForCover(Song* song) {
|
2011-01-24 01:09:57 +01:00
|
|
|
#ifdef HAVE_LIBLASTFM
|
|
|
|
// Get something sensible to stick in the search box
|
2011-01-24 18:53:31 +01:00
|
|
|
QString query = song->artist();
|
2011-01-24 01:09:57 +01:00
|
|
|
if (!query.isEmpty())
|
|
|
|
query += " ";
|
2011-01-24 18:53:31 +01:00
|
|
|
query += song->album();
|
2011-01-24 01:09:57 +01:00
|
|
|
|
|
|
|
QImage image = cover_searcher_->Exec(query);
|
2011-01-24 18:53:31 +01:00
|
|
|
|
|
|
|
if(!image.isNull()) {
|
|
|
|
QString cover = SaveCoverInCache(song->artist(), song->album(), image);
|
|
|
|
SaveCover(song, cover);
|
|
|
|
|
|
|
|
return cover;
|
|
|
|
} else {
|
|
|
|
return QString();
|
|
|
|
}
|
2011-01-24 01:09:57 +01:00
|
|
|
#else
|
2011-01-24 18:53:31 +01:00
|
|
|
return QString();
|
2011-01-24 01:09:57 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2011-01-24 18:53:31 +01:00
|
|
|
QString AlbumCoverChoiceController::UnsetCover(Song* song) {
|
2011-02-02 22:01:08 +01:00
|
|
|
QString cover = Song::kManuallyUnsetCover;
|
2011-01-24 18:53:31 +01:00
|
|
|
SaveCover(song, cover);
|
|
|
|
|
|
|
|
return cover;
|
2011-01-24 01:09:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void AlbumCoverChoiceController::ShowCover(const Song& song) {
|
|
|
|
QDialog* dialog = new QDialog(this);
|
|
|
|
dialog->setAttribute(Qt::WA_DeleteOnClose, true);
|
|
|
|
dialog->setWindowTitle(song.title());
|
|
|
|
|
|
|
|
QLabel* label = new QLabel(dialog);
|
|
|
|
label->setPixmap(AlbumCoverLoader::TryLoadPixmap(
|
|
|
|
song.art_automatic(), song.art_manual(), song.filename()));
|
|
|
|
|
|
|
|
dialog->resize(label->pixmap()->size());
|
|
|
|
dialog->show();
|
|
|
|
}
|
2011-01-24 18:53:31 +01:00
|
|
|
|
|
|
|
void AlbumCoverChoiceController::SaveCover(Song* song, const QString &cover) {
|
|
|
|
if(song->is_valid() && song->id() != -1) {
|
|
|
|
song->set_art_manual(cover);
|
|
|
|
library_->UpdateManualAlbumArtAsync(song->artist(), song->album(), cover);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QString AlbumCoverChoiceController::SaveCoverInCache(
|
|
|
|
const QString& artist, const QString& album, const QImage& image) {
|
|
|
|
|
|
|
|
// Hash the artist and album into a filename for the image
|
|
|
|
QCryptographicHash hash(QCryptographicHash::Sha1);
|
|
|
|
hash.addData(artist.toLower().toUtf8().constData());
|
|
|
|
hash.addData(album.toLower().toUtf8().constData());
|
|
|
|
|
|
|
|
QString filename = hash.result().toHex() + ".jpg";
|
|
|
|
QString path = AlbumCoverLoader::ImageCacheDir() + "/" + filename;
|
|
|
|
|
|
|
|
// Make sure this directory exists first
|
|
|
|
QDir dir;
|
|
|
|
dir.mkdir(AlbumCoverLoader::ImageCacheDir());
|
|
|
|
|
|
|
|
// Save the image to disk
|
|
|
|
image.save(path, "JPG");
|
|
|
|
|
|
|
|
return path;
|
|
|
|
|
|
|
|
}
|