2010-03-24 00:11:46 +01:00
|
|
|
/* This file is part of Clementine.
|
|
|
|
|
|
|
|
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/>.
|
|
|
|
*/
|
|
|
|
|
2010-02-28 19:04:50 +01:00
|
|
|
#include "albumcovermanager.h"
|
2010-06-12 19:13:01 +02:00
|
|
|
#include "albumcoversearcher.h"
|
2010-05-19 17:45:29 +02:00
|
|
|
#include "iconloader.h"
|
2010-05-10 23:50:31 +02:00
|
|
|
#include "ui_albumcovermanager.h"
|
|
|
|
#include "core/albumcoverfetcher.h"
|
|
|
|
#include "library/librarybackend.h"
|
|
|
|
#include "library/libraryquery.h"
|
2010-02-28 19:04:50 +01:00
|
|
|
|
|
|
|
#include <QSettings>
|
|
|
|
#include <QPainter>
|
|
|
|
#include <QMenu>
|
|
|
|
#include <QActionGroup>
|
2010-02-28 20:25:52 +01:00
|
|
|
#include <QListWidget>
|
|
|
|
#include <QCryptographicHash>
|
|
|
|
#include <QDir>
|
2010-02-28 20:56:18 +01:00
|
|
|
#include <QEvent>
|
|
|
|
#include <QScrollBar>
|
2010-03-03 15:29:53 +01:00
|
|
|
#include <QContextMenuEvent>
|
|
|
|
#include <QLabel>
|
|
|
|
#include <QFileDialog>
|
|
|
|
#include <QMessageBox>
|
2010-06-12 01:07:53 +02:00
|
|
|
#include <QProgressBar>
|
|
|
|
#include <QTimer>
|
2010-02-28 19:04:50 +01:00
|
|
|
|
|
|
|
const char* AlbumCoverManager::kSettingsGroup = "CoverManager";
|
|
|
|
|
2010-05-10 15:15:52 +02:00
|
|
|
AlbumCoverManager::AlbumCoverManager(NetworkAccessManager* network,
|
2010-05-09 02:10:26 +02:00
|
|
|
LibraryBackend* backend, QWidget *parent)
|
2010-06-12 00:35:41 +02:00
|
|
|
: QMainWindow(parent),
|
2010-02-28 20:56:18 +01:00
|
|
|
constructed_(false),
|
2010-05-10 23:50:31 +02:00
|
|
|
ui_(new Ui_CoverManager),
|
2010-05-09 02:10:26 +02:00
|
|
|
backend_(backend),
|
2010-05-10 15:15:52 +02:00
|
|
|
network_(network),
|
2010-03-23 18:26:54 +01:00
|
|
|
cover_loader_(new BackgroundThreadImplementation<AlbumCoverLoader, AlbumCoverLoader>(this)),
|
2010-03-03 21:35:19 +01:00
|
|
|
cover_fetcher_(new AlbumCoverFetcher(network, this)),
|
2010-06-12 19:13:01 +02:00
|
|
|
cover_searcher_(new AlbumCoverSearcher(this)),
|
2010-05-19 17:45:29 +02:00
|
|
|
artist_icon_(IconLoader::Load("x-clementine-artist")),
|
|
|
|
all_artists_icon_(IconLoader::Load("x-clementine-album")),
|
2010-06-12 01:07:53 +02:00
|
|
|
context_menu_(new QMenu(this)),
|
|
|
|
progress_bar_(new QProgressBar(this)),
|
|
|
|
jobs_(0),
|
|
|
|
got_covers_(0),
|
|
|
|
missing_covers_(0)
|
|
|
|
{
|
2010-05-10 23:50:31 +02:00
|
|
|
ui_->setupUi(this);
|
2010-06-12 17:18:16 +02:00
|
|
|
ui_->albums->set_cover_manager(this);
|
2010-02-28 19:04:50 +01:00
|
|
|
|
2010-05-19 17:45:29 +02:00
|
|
|
// Icons
|
|
|
|
ui_->action_fetch->setIcon(IconLoader::Load("download"));
|
|
|
|
ui_->action_choose_manual->setIcon(IconLoader::Load("document-open"));
|
|
|
|
ui_->action_show_fullsize->setIcon(IconLoader::Load("zoom-in"));
|
|
|
|
ui_->action_unset_cover->setIcon(IconLoader::Load("list-remove"));
|
|
|
|
ui_->clear->setIcon(IconLoader::Load("edit-clear-locationbar-ltr"));
|
|
|
|
ui_->view->setIcon(IconLoader::Load("view-choose"));
|
|
|
|
ui_->fetch->setIcon(IconLoader::Load("download"));
|
2010-06-12 19:13:01 +02:00
|
|
|
ui_->action_search_manual->setIcon(IconLoader::Load("download"));
|
2010-06-12 17:18:16 +02:00
|
|
|
ui_->action_add_to_playlist->setIcon(IconLoader::Load("media-playback-start"));
|
|
|
|
ui_->action_load->setIcon(IconLoader::Load("media-playback-start"));
|
2010-05-19 17:45:29 +02:00
|
|
|
|
2010-02-28 19:04:50 +01:00
|
|
|
// Get a square version of nocover.png
|
|
|
|
QImage nocover(":/nocover.png");
|
|
|
|
nocover = nocover.scaled(120, 120, Qt::KeepAspectRatio, Qt::SmoothTransformation);
|
|
|
|
QImage square_nocover(120, 120, QImage::Format_ARGB32);
|
|
|
|
square_nocover.fill(0);
|
|
|
|
QPainter p(&square_nocover);
|
|
|
|
p.drawImage((120 - nocover.width()) / 2, (120 - nocover.height()) / 2, nocover);
|
|
|
|
p.end();
|
|
|
|
no_cover_icon_ = QPixmap::fromImage(square_nocover);
|
2010-06-12 01:07:53 +02:00
|
|
|
|
|
|
|
// Set up the status bar
|
|
|
|
statusBar()->addPermanentWidget(progress_bar_);
|
|
|
|
progress_bar_->hide();
|
2010-03-26 13:22:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
AlbumCoverManager::~AlbumCoverManager() {
|
|
|
|
CancelRequests();
|
2010-05-10 23:50:31 +02:00
|
|
|
|
|
|
|
delete ui_;
|
2010-03-26 13:22:19 +01:00
|
|
|
}
|
2010-02-28 19:04:50 +01:00
|
|
|
|
2010-03-26 13:22:19 +01:00
|
|
|
void AlbumCoverManager::Init() {
|
2010-02-28 19:04:50 +01:00
|
|
|
// View menu
|
|
|
|
QActionGroup* filter_group = new QActionGroup(this);
|
2010-03-03 15:29:53 +01:00
|
|
|
filter_all_ = filter_group->addAction(tr("All albums"));
|
|
|
|
filter_with_covers_ = filter_group->addAction(tr("Albums with covers"));
|
|
|
|
filter_without_covers_ = filter_group->addAction(tr("Albums without covers"));
|
2010-02-28 19:04:50 +01:00
|
|
|
filter_all_->setCheckable(true);
|
|
|
|
filter_with_covers_->setCheckable(true);
|
|
|
|
filter_without_covers_->setCheckable(true);
|
|
|
|
filter_group->setExclusive(true);
|
|
|
|
filter_all_->setChecked(true);
|
|
|
|
|
|
|
|
QMenu* view_menu = new QMenu(this);
|
|
|
|
view_menu->addActions(filter_group->actions());
|
|
|
|
|
2010-05-10 23:50:31 +02:00
|
|
|
ui_->view->setMenu(view_menu);
|
2010-02-28 19:04:50 +01:00
|
|
|
|
2010-03-03 15:29:53 +01:00
|
|
|
// Context menu
|
2010-05-10 23:50:31 +02:00
|
|
|
context_menu_->addAction(ui_->action_show_fullsize);
|
2010-06-12 19:13:01 +02:00
|
|
|
context_menu_->addAction(ui_->action_search_manual);
|
2010-05-10 23:50:31 +02:00
|
|
|
context_menu_->addAction(ui_->action_choose_manual);
|
2010-03-03 15:29:53 +01:00
|
|
|
context_menu_->addSeparator();
|
2010-05-10 23:50:31 +02:00
|
|
|
context_menu_->addAction(ui_->action_unset_cover);
|
2010-06-12 17:18:16 +02:00
|
|
|
context_menu_->addSeparator();
|
|
|
|
context_menu_->addAction(ui_->action_load);
|
|
|
|
context_menu_->addAction(ui_->action_add_to_playlist);
|
2010-05-10 23:50:31 +02:00
|
|
|
ui_->albums->installEventFilter(this);
|
2010-03-03 15:29:53 +01:00
|
|
|
|
2010-02-28 19:04:50 +01:00
|
|
|
// Connections
|
|
|
|
connect(cover_loader_, SIGNAL(Initialised()), SLOT(CoverLoaderInitialised()));
|
2010-05-10 23:50:31 +02:00
|
|
|
connect(ui_->artists, SIGNAL(currentItemChanged(QListWidgetItem*,QListWidgetItem*)),
|
2010-02-28 19:04:50 +01:00
|
|
|
SLOT(ArtistChanged(QListWidgetItem*)));
|
2010-05-10 23:50:31 +02:00
|
|
|
connect(ui_->filter, SIGNAL(textChanged(QString)), SLOT(UpdateFilter()));
|
2010-02-28 19:04:50 +01:00
|
|
|
connect(filter_group, SIGNAL(triggered(QAction*)), SLOT(UpdateFilter()));
|
2010-05-10 23:50:31 +02:00
|
|
|
connect(ui_->view, SIGNAL(clicked()), ui_->view, SLOT(showMenu()));
|
|
|
|
connect(ui_->fetch, SIGNAL(clicked()), SLOT(FetchAlbumCovers()));
|
2010-02-28 20:25:52 +01:00
|
|
|
connect(cover_fetcher_, SIGNAL(AlbumCoverFetched(quint64,QImage)),
|
|
|
|
SLOT(AlbumCoverFetched(quint64,QImage)));
|
2010-05-10 23:50:31 +02:00
|
|
|
connect(ui_->action_show_fullsize, SIGNAL(triggered()), SLOT(ShowFullsize()));
|
|
|
|
connect(ui_->action_fetch, SIGNAL(triggered()), SLOT(FetchSingleCover()));
|
|
|
|
connect(ui_->action_choose_manual, SIGNAL(triggered()), SLOT(ChooseManualCover()));
|
|
|
|
connect(ui_->action_unset_cover, SIGNAL(triggered()), SLOT(UnsetCover()));
|
2010-06-12 17:18:16 +02:00
|
|
|
connect(ui_->albums, SIGNAL(doubleClicked(QModelIndex)), SLOT(AlbumDoubleClicked(QModelIndex)));
|
|
|
|
connect(ui_->action_add_to_playlist, SIGNAL(triggered()), SLOT(AddSelectedToPlaylist()));
|
|
|
|
connect(ui_->action_load, SIGNAL(triggered()), SLOT(LoadSelectedToPlaylist()));
|
2010-06-12 19:13:01 +02:00
|
|
|
connect(ui_->action_search_manual, SIGNAL(triggered()), SLOT(SearchManual()));
|
2010-02-28 19:04:50 +01:00
|
|
|
|
|
|
|
// Restore settings
|
|
|
|
QSettings s;
|
|
|
|
s.beginGroup(kSettingsGroup);
|
|
|
|
|
|
|
|
restoreGeometry(s.value("geometry").toByteArray());
|
2010-05-10 23:50:31 +02:00
|
|
|
if (!ui_->splitter->restoreState(s.value("splitter_state").toByteArray())) {
|
2010-02-28 19:04:50 +01:00
|
|
|
// Sensible default size for the artists view
|
2010-05-10 23:50:31 +02:00
|
|
|
ui_->splitter->setSizes(QList<int>() << 200 << width() - 200);
|
2010-02-28 19:04:50 +01:00
|
|
|
}
|
|
|
|
|
2010-03-23 18:26:54 +01:00
|
|
|
cover_loader_->Start();
|
2010-02-28 20:56:18 +01:00
|
|
|
constructed_ = true;
|
2010-02-28 19:04:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void AlbumCoverManager::CoverLoaderInitialised() {
|
2010-05-10 15:15:52 +02:00
|
|
|
cover_loader_->Worker()->SetNetwork(network_);
|
2010-05-17 00:53:42 +02:00
|
|
|
cover_loader_->Worker()->SetDefaultOutputImage(QImage(":nocover.png"));
|
2010-02-28 19:04:50 +01:00
|
|
|
connect(cover_loader_->Worker().get(), SIGNAL(ImageLoaded(quint64,QImage)),
|
|
|
|
SLOT(CoverImageLoaded(quint64,QImage)));
|
2010-06-12 19:13:01 +02:00
|
|
|
|
|
|
|
cover_searcher_->Init(cover_loader_->Worker(), cover_fetcher_);
|
2010-02-28 19:04:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void AlbumCoverManager::showEvent(QShowEvent *) {
|
|
|
|
Reset();
|
|
|
|
}
|
|
|
|
|
2010-06-12 01:07:53 +02:00
|
|
|
void AlbumCoverManager::closeEvent(QCloseEvent* e) {
|
|
|
|
if (!cover_fetching_tasks_.isEmpty()) {
|
|
|
|
boost::scoped_ptr<QMessageBox> message_box(new QMessageBox(
|
|
|
|
QMessageBox::Question, tr("Really cancel?"),
|
|
|
|
tr("Closing this window will stop searching for album covers."),
|
|
|
|
QMessageBox::Abort, this));
|
|
|
|
message_box->addButton(tr("Don't stop!"), QMessageBox::AcceptRole);
|
|
|
|
|
|
|
|
if (message_box->exec() != QMessageBox::Abort) {
|
|
|
|
e->ignore();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-28 20:25:52 +01:00
|
|
|
// Save geometry
|
2010-02-28 19:04:50 +01:00
|
|
|
QSettings s;
|
|
|
|
s.beginGroup(kSettingsGroup);
|
|
|
|
|
|
|
|
s.setValue("geometry", saveGeometry());
|
2010-05-10 23:50:31 +02:00
|
|
|
s.setValue("splitter_state", ui_->splitter->saveState());
|
2010-02-28 20:25:52 +01:00
|
|
|
|
|
|
|
// Cancel any outstanding requests
|
|
|
|
CancelRequests();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AlbumCoverManager::CancelRequests() {
|
|
|
|
cover_loading_tasks_.clear();
|
2010-03-26 13:22:19 +01:00
|
|
|
if (cover_loader_ && cover_loader_->Worker()) {
|
|
|
|
cover_loader_->Worker()->Clear();
|
|
|
|
}
|
2010-02-28 20:25:52 +01:00
|
|
|
|
|
|
|
cover_fetching_tasks_.clear();
|
|
|
|
cover_fetcher_->Clear();
|
2010-05-10 23:50:31 +02:00
|
|
|
ui_->fetch->setEnabled(true);
|
2010-06-12 01:07:53 +02:00
|
|
|
progress_bar_->hide();
|
|
|
|
statusBar()->clearMessage();
|
2010-02-28 19:04:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void AlbumCoverManager::Reset() {
|
|
|
|
if (!backend_)
|
|
|
|
return;
|
|
|
|
|
2010-05-10 23:50:31 +02:00
|
|
|
ui_->artists->clear();
|
|
|
|
new QListWidgetItem(all_artists_icon_, tr("All artists"), ui_->artists, All_Artists);
|
|
|
|
new QListWidgetItem(artist_icon_, tr("Various artists"), ui_->artists, Various_Artists);
|
2010-02-28 19:04:50 +01:00
|
|
|
|
2010-06-12 00:35:41 +02:00
|
|
|
foreach (const QString& artist, backend_->GetAllArtistsWithAlbums()) {
|
2010-02-28 19:04:50 +01:00
|
|
|
if (artist.isEmpty())
|
|
|
|
continue;
|
|
|
|
|
2010-05-10 23:50:31 +02:00
|
|
|
new QListWidgetItem(artist_icon_, artist, ui_->artists, Specific_Artist);
|
2010-02-28 19:04:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AlbumCoverManager::ArtistChanged(QListWidgetItem* current) {
|
|
|
|
if (!backend_ || !cover_loader_->Worker())
|
|
|
|
return;
|
|
|
|
if (!current)
|
|
|
|
return;
|
|
|
|
|
|
|
|
QString artist;
|
|
|
|
if (current->type() == Specific_Artist)
|
|
|
|
artist = current->text();
|
|
|
|
|
2010-05-10 23:50:31 +02:00
|
|
|
ui_->albums->clear();
|
2010-03-03 15:29:53 +01:00
|
|
|
context_menu_items_.clear();
|
2010-02-28 20:25:52 +01:00
|
|
|
CancelRequests();
|
2010-02-28 19:04:50 +01:00
|
|
|
|
2010-03-12 01:54:18 +01:00
|
|
|
// Get the list of albums. How we do it depends on what thing we have
|
|
|
|
// selected in the artist list.
|
2010-05-09 02:10:26 +02:00
|
|
|
LibraryBackend::AlbumList albums;
|
2010-03-12 01:54:18 +01:00
|
|
|
switch (current->type()) {
|
|
|
|
case Various_Artists: albums = backend_->GetCompilationAlbums(); break;
|
|
|
|
case Specific_Artist: albums = backend_->GetAlbumsByArtist(current->text()); break;
|
|
|
|
case All_Artists:
|
|
|
|
default: albums = backend_->GetAllAlbums(); break;
|
|
|
|
}
|
|
|
|
|
2010-05-09 02:10:26 +02:00
|
|
|
foreach (const LibraryBackend::Album& info, albums) {
|
2010-03-12 01:54:18 +01:00
|
|
|
// Don't show songs without an album, obviously
|
|
|
|
if (info.album_name.isEmpty())
|
|
|
|
continue;
|
|
|
|
|
2010-05-10 23:50:31 +02:00
|
|
|
QListWidgetItem* item = new QListWidgetItem(no_cover_icon_, info.album_name, ui_->albums);
|
2010-02-28 20:25:52 +01:00
|
|
|
item->setData(Role_ArtistName, info.artist);
|
|
|
|
item->setData(Role_AlbumName, info.album_name);
|
2010-06-12 17:18:16 +02:00
|
|
|
item->setData(Qt::TextAlignmentRole, QVariant(Qt::AlignTop | Qt::AlignHCenter));
|
|
|
|
item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsDragEnabled);
|
2010-02-28 19:04:50 +01:00
|
|
|
|
|
|
|
if (!info.art_automatic.isEmpty() || !info.art_manual.isEmpty()) {
|
|
|
|
quint64 id = cover_loader_->Worker()->LoadImageAsync(
|
|
|
|
info.art_automatic, info.art_manual);
|
2010-03-03 15:29:53 +01:00
|
|
|
item->setData(Role_PathAutomatic, info.art_automatic);
|
|
|
|
item->setData(Role_PathManual, info.art_manual);
|
2010-02-28 19:04:50 +01:00
|
|
|
cover_loading_tasks_[id] = item;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
UpdateFilter();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AlbumCoverManager::CoverImageLoaded(quint64 id, const QImage &image) {
|
|
|
|
if (!cover_loading_tasks_.contains(id))
|
|
|
|
return;
|
|
|
|
|
|
|
|
QListWidgetItem* item = cover_loading_tasks_.take(id);
|
|
|
|
|
|
|
|
if (image.isNull())
|
|
|
|
return;
|
|
|
|
|
|
|
|
item->setIcon(QPixmap::fromImage(image));
|
|
|
|
UpdateFilter();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AlbumCoverManager::UpdateFilter() {
|
2010-05-10 23:50:31 +02:00
|
|
|
const QString filter = ui_->filter->text().toLower();
|
2010-02-28 19:04:50 +01:00
|
|
|
const bool hide_with_covers = filter_without_covers_->isChecked();
|
|
|
|
const bool hide_without_covers = filter_with_covers_->isChecked();
|
|
|
|
|
2010-03-26 13:22:19 +01:00
|
|
|
HideCovers hide = Hide_None;
|
|
|
|
if (hide_with_covers) {
|
|
|
|
hide = Hide_WithCovers;
|
|
|
|
} else if (hide_without_covers) {
|
|
|
|
hide = Hide_WithoutCovers;
|
|
|
|
}
|
|
|
|
|
2010-05-10 23:50:31 +02:00
|
|
|
for (int i = 0; i < ui_->albums->count(); ++i) {
|
|
|
|
QListWidgetItem* item = ui_->albums->item(i);
|
2010-03-26 13:22:19 +01:00
|
|
|
item->setHidden(ShouldHide(*item, filter, hide));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AlbumCoverManager::ShouldHide(
|
|
|
|
const QListWidgetItem& item, const QString& filter, HideCovers hide) const {
|
|
|
|
bool has_cover = item.icon().cacheKey() != no_cover_icon_.cacheKey();
|
|
|
|
if (hide == Hide_WithCovers && has_cover) {
|
|
|
|
return true;
|
|
|
|
} else if (hide == Hide_WithoutCovers && !has_cover) {
|
|
|
|
return true;
|
|
|
|
}
|
2010-02-28 19:04:50 +01:00
|
|
|
|
2010-03-26 13:22:19 +01:00
|
|
|
if (filter.isEmpty()) {
|
|
|
|
return false;
|
2010-02-28 19:04:50 +01:00
|
|
|
}
|
2010-03-26 13:22:19 +01:00
|
|
|
|
|
|
|
QStringList query = filter.split(' ');
|
|
|
|
foreach (const QString& s, query) {
|
2010-03-26 16:04:38 +01:00
|
|
|
if (!item.text().contains(s, Qt::CaseInsensitive)) {
|
|
|
|
return true;
|
2010-03-26 13:22:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-26 16:04:38 +01:00
|
|
|
return false;
|
2010-02-28 19:04:50 +01:00
|
|
|
}
|
2010-02-28 20:25:52 +01:00
|
|
|
|
|
|
|
void AlbumCoverManager::FetchAlbumCovers() {
|
2010-05-10 23:50:31 +02:00
|
|
|
for (int i=0 ; i<ui_->albums->count() ; ++i) {
|
|
|
|
QListWidgetItem* item = ui_->albums->item(i);
|
2010-02-28 20:25:52 +01:00
|
|
|
if (item->isHidden())
|
|
|
|
continue;
|
|
|
|
if (item->icon().cacheKey() != no_cover_icon_.cacheKey())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
quint64 id = cover_fetcher_->FetchAlbumCover(
|
|
|
|
item->data(Role_ArtistName).toString(), item->data(Role_AlbumName).toString());
|
|
|
|
cover_fetching_tasks_[id] = item;
|
2010-06-12 01:07:53 +02:00
|
|
|
jobs_ ++;
|
2010-02-28 20:25:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!cover_fetching_tasks_.isEmpty())
|
2010-05-10 23:50:31 +02:00
|
|
|
ui_->fetch->setEnabled(false);
|
2010-06-12 01:07:53 +02:00
|
|
|
|
|
|
|
progress_bar_->setMaximum(jobs_);
|
|
|
|
progress_bar_->show();
|
|
|
|
UpdateStatusText();
|
2010-02-28 20:25:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void AlbumCoverManager::AlbumCoverFetched(quint64 id, const QImage &image) {
|
|
|
|
if (!cover_fetching_tasks_.contains(id))
|
|
|
|
return;
|
|
|
|
|
|
|
|
QListWidgetItem* item = cover_fetching_tasks_.take(id);
|
2010-06-12 01:07:53 +02:00
|
|
|
if (image.isNull()) {
|
|
|
|
missing_covers_ ++;
|
|
|
|
} else {
|
|
|
|
got_covers_ ++;
|
|
|
|
|
2010-06-12 19:13:01 +02:00
|
|
|
SaveAndSetCover(item, image);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cover_fetching_tasks_.isEmpty())
|
|
|
|
ui_->fetch->setEnabled(true);
|
2010-02-28 20:25:52 +01:00
|
|
|
|
2010-06-12 19:13:01 +02:00
|
|
|
UpdateStatusText();
|
|
|
|
}
|
2010-02-28 20:25:52 +01:00
|
|
|
|
2010-06-12 19:13:01 +02:00
|
|
|
void AlbumCoverManager::SaveAndSetCover(QListWidgetItem *item, const QImage &image) {
|
|
|
|
const QString artist = item->data(Role_ArtistName).toString();
|
|
|
|
const QString album = item->data(Role_AlbumName).toString();
|
2010-02-28 20:25:52 +01:00
|
|
|
|
2010-06-12 19:13:01 +02:00
|
|
|
// 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());
|
2010-02-28 20:25:52 +01:00
|
|
|
|
2010-06-12 19:13:01 +02:00
|
|
|
QString filename = hash.result().toHex() + ".jpg";
|
|
|
|
QString path = AlbumCoverLoader::ImageCacheDir() + "/" + filename;
|
2010-02-28 20:25:52 +01:00
|
|
|
|
2010-06-12 19:13:01 +02:00
|
|
|
// Make sure this directory exists first
|
|
|
|
QDir dir;
|
|
|
|
dir.mkdir(AlbumCoverLoader::ImageCacheDir());
|
2010-02-28 20:25:52 +01:00
|
|
|
|
2010-06-12 19:13:01 +02:00
|
|
|
// Save the image to disk
|
|
|
|
image.save(path, "JPG");
|
2010-02-28 20:25:52 +01:00
|
|
|
|
2010-06-12 19:13:01 +02:00
|
|
|
// Save the image in the database
|
|
|
|
backend_->UpdateManualAlbumArtAsync(artist, album, path);
|
2010-06-12 01:07:53 +02:00
|
|
|
|
2010-06-12 19:13:01 +02:00
|
|
|
// Update the icon in our list
|
|
|
|
quint64 id = cover_loader_->Worker()->LoadImageAsync(QString(), path);
|
|
|
|
item->setData(Role_PathManual, path);
|
|
|
|
cover_loading_tasks_[id] = item;
|
2010-06-12 01:07:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void AlbumCoverManager::UpdateStatusText() {
|
|
|
|
QString message = tr("Got %1 covers out of %2 (%3 failed)")
|
|
|
|
.arg(got_covers_).arg(jobs_).arg(missing_covers_);
|
|
|
|
statusBar()->showMessage(message);
|
|
|
|
progress_bar_->setValue(got_covers_ + missing_covers_);
|
|
|
|
|
|
|
|
if (cover_fetching_tasks_.isEmpty()) {
|
|
|
|
QTimer::singleShot(2000, statusBar(), SLOT(clearMessage()));
|
|
|
|
progress_bar_->hide();
|
|
|
|
|
|
|
|
jobs_ = 0;
|
|
|
|
got_covers_ = 0;
|
|
|
|
missing_covers_ = 0;
|
|
|
|
}
|
2010-02-28 20:25:52 +01:00
|
|
|
}
|
2010-02-28 20:56:18 +01:00
|
|
|
|
|
|
|
bool AlbumCoverManager::event(QEvent* e) {
|
|
|
|
if (constructed_) {
|
2010-06-12 00:35:41 +02:00
|
|
|
// I think KDE styles override these, and ScrollPerItem is really confusing
|
2010-02-28 20:56:18 +01:00
|
|
|
// when you have huge items.
|
|
|
|
// We seem to have to reset them to sensible values each time the contents
|
2010-05-10 23:50:31 +02:00
|
|
|
// of ui_->albums changes.
|
|
|
|
ui_->albums->setVerticalScrollMode(QListWidget::ScrollPerPixel);
|
|
|
|
ui_->albums->verticalScrollBar()->setSingleStep(20);
|
2010-02-28 20:56:18 +01:00
|
|
|
}
|
|
|
|
|
2010-06-12 00:35:41 +02:00
|
|
|
QMainWindow::event(e);
|
2010-02-28 20:56:18 +01:00
|
|
|
return false;
|
|
|
|
}
|
2010-03-03 15:29:53 +01:00
|
|
|
|
|
|
|
bool AlbumCoverManager::eventFilter(QObject *obj, QEvent *event) {
|
2010-05-10 23:50:31 +02:00
|
|
|
if (obj == ui_->albums && event->type() == QEvent::ContextMenu) {
|
|
|
|
context_menu_items_ = ui_->albums->selectedItems();
|
2010-03-03 15:29:53 +01:00
|
|
|
if (context_menu_items_.isEmpty())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
bool some_with_covers = false;
|
|
|
|
|
|
|
|
foreach (QListWidgetItem* item, context_menu_items_) {
|
|
|
|
if (item->icon().cacheKey() != no_cover_icon_.cacheKey())
|
|
|
|
some_with_covers = true;
|
|
|
|
}
|
|
|
|
|
2010-05-10 23:50:31 +02:00
|
|
|
ui_->action_show_fullsize->setEnabled(some_with_covers && context_menu_items_.size() == 1);
|
|
|
|
ui_->action_choose_manual->setEnabled(context_menu_items_.size() == 1);
|
|
|
|
ui_->action_unset_cover->setEnabled(some_with_covers);
|
2010-03-03 15:29:53 +01:00
|
|
|
|
|
|
|
QContextMenuEvent* e = static_cast<QContextMenuEvent*>(event);
|
|
|
|
context_menu_->popup(e->globalPos());
|
|
|
|
return true;
|
|
|
|
}
|
2010-06-12 00:35:41 +02:00
|
|
|
return QMainWindow::eventFilter(obj, event);
|
2010-03-03 15:29:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void AlbumCoverManager::ShowFullsize() {
|
|
|
|
if (context_menu_items_.size() != 1)
|
|
|
|
return;
|
|
|
|
QListWidgetItem* item = context_menu_items_[0];
|
|
|
|
|
|
|
|
QString title = item->data(Role_AlbumName).toString();
|
|
|
|
if (!item->data(Role_ArtistName).toString().isNull())
|
|
|
|
title = item->data(Role_ArtistName).toString() + " - " + title;
|
|
|
|
|
|
|
|
QDialog* dialog = new QDialog(this);
|
|
|
|
dialog->setAttribute(Qt::WA_DeleteOnClose, true);
|
|
|
|
dialog->setWindowTitle(title);
|
|
|
|
|
|
|
|
QLabel* label = new QLabel(dialog);
|
|
|
|
label->setPixmap(AlbumCoverLoader::TryLoadPixmap(
|
|
|
|
item->data(Role_PathAutomatic).toString(),
|
|
|
|
item->data(Role_PathManual).toString()));
|
|
|
|
|
|
|
|
dialog->resize(label->pixmap()->size());
|
|
|
|
dialog->show();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AlbumCoverManager::FetchSingleCover() {
|
|
|
|
foreach (QListWidgetItem* item, context_menu_items_) {
|
|
|
|
quint64 id = cover_fetcher_->FetchAlbumCover(
|
|
|
|
item->data(Role_ArtistName).toString(), item->data(Role_AlbumName).toString());
|
|
|
|
cover_fetching_tasks_[id] = item;
|
2010-06-12 01:07:53 +02:00
|
|
|
jobs_ ++;
|
2010-03-03 15:29:53 +01:00
|
|
|
}
|
2010-06-12 01:07:53 +02:00
|
|
|
|
|
|
|
progress_bar_->setMaximum(jobs_);
|
|
|
|
progress_bar_->show();
|
|
|
|
UpdateStatusText();
|
2010-03-03 15:29:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void AlbumCoverManager::ChooseManualCover() {
|
|
|
|
if (context_menu_items_.size() != 1)
|
|
|
|
return;
|
|
|
|
QListWidgetItem* item = context_menu_items_[0];
|
|
|
|
|
|
|
|
QString cover = QFileDialog::getOpenFileName(
|
2010-04-04 15:42:56 +02:00
|
|
|
this, tr("Choose manual cover"), item->data(Role_PathAutomatic).toString(),
|
|
|
|
tr("Images (*.png *.jpg *.jpeg *.bmp *.gif *.xpm *.pbm *.pgm *.ppm *.xbm *.tiff)") + ";;" +
|
|
|
|
tr("All files (*)"));
|
2010-03-03 15:29:53 +01:00
|
|
|
if (cover.isNull())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Can we load the image?
|
|
|
|
QImage image(cover);
|
|
|
|
if (image.isNull())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Update database
|
|
|
|
backend_->UpdateManualAlbumArtAsync(item->data(Role_ArtistName).toString(),
|
|
|
|
item->data(Role_AlbumName).toString(),
|
|
|
|
cover);
|
|
|
|
|
|
|
|
// Update the icon in our list
|
|
|
|
quint64 id = cover_loader_->Worker()->LoadImageAsync(QString(), cover);
|
|
|
|
item->setData(Role_PathManual, cover);
|
|
|
|
cover_loading_tasks_[id] = item;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AlbumCoverManager::UnsetCover() {
|
|
|
|
foreach (QListWidgetItem* item, context_menu_items_) {
|
|
|
|
item->setIcon(no_cover_icon_);
|
|
|
|
item->setData(Role_PathManual, AlbumCoverLoader::kManuallyUnsetCover);
|
|
|
|
backend_->UpdateManualAlbumArtAsync(item->data(Role_ArtistName).toString(),
|
|
|
|
item->data(Role_AlbumName).toString(),
|
|
|
|
AlbumCoverLoader::kManuallyUnsetCover);
|
|
|
|
}
|
|
|
|
}
|
2010-06-12 17:18:16 +02:00
|
|
|
|
|
|
|
SongList AlbumCoverManager::GetSongsInAlbum(const QModelIndex& index) const {
|
|
|
|
SongList ret;
|
|
|
|
|
|
|
|
LibraryQuery q;
|
|
|
|
q.SetColumnSpec("ROWID," + Song::kColumnSpec);
|
|
|
|
q.AddWhere("album", index.data(Role_AlbumName).toString());
|
|
|
|
q.SetOrderBy("disc, track, title");
|
|
|
|
|
|
|
|
QString artist = index.data(Role_ArtistName).toString();
|
|
|
|
q.AddCompilationRequirement(artist.isEmpty());
|
|
|
|
if (!artist.isEmpty())
|
|
|
|
q.AddWhere("artist", artist);
|
|
|
|
|
|
|
|
if (!backend_->ExecQuery(&q))
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
while (q.Next()) {
|
|
|
|
Song song;
|
|
|
|
song.InitFromQuery(q);
|
|
|
|
ret << song;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
SongList AlbumCoverManager::GetSongsInAlbums(const QModelIndexList& indexes) const {
|
|
|
|
SongList ret;
|
|
|
|
foreach (const QModelIndex& index, indexes) {
|
|
|
|
ret << GetSongsInAlbum(index);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AlbumCoverManager::AlbumDoubleClicked(const QModelIndex &index) {
|
|
|
|
emit SongsDoubleClicked(GetSongsInAlbum(index));
|
|
|
|
}
|
|
|
|
|
|
|
|
void AlbumCoverManager::AddSelectedToPlaylist() {
|
|
|
|
emit AddSongsToPlaylist(GetSongsInAlbums(
|
|
|
|
ui_->albums->selectionModel()->selectedIndexes()));
|
|
|
|
}
|
|
|
|
|
|
|
|
void AlbumCoverManager::LoadSelectedToPlaylist() {
|
|
|
|
emit LoadSongsToPlaylist(GetSongsInAlbums(
|
|
|
|
ui_->albums->selectionModel()->selectedIndexes()));
|
|
|
|
}
|
2010-06-12 19:13:01 +02:00
|
|
|
|
|
|
|
void AlbumCoverManager::SearchManual() {
|
|
|
|
if (context_menu_items_.isEmpty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Get something sensible to stick in the search box
|
|
|
|
QString query = context_menu_items_[0]->data(Role_ArtistName).toString();
|
|
|
|
if (!query.isEmpty())
|
|
|
|
query += " ";
|
|
|
|
query += context_menu_items_[0]->data(Role_AlbumName).toString();
|
|
|
|
|
|
|
|
QImage image = cover_searcher_->Exec(query);
|
|
|
|
if (image.isNull())
|
|
|
|
return;
|
|
|
|
|
|
|
|
foreach (QListWidgetItem* item, context_menu_items_) {
|
|
|
|
SaveAndSetCover(item, image);
|
|
|
|
}
|
|
|
|
}
|