File indexing completed on 2024-05-05 04:48:25

0001 /****************************************************************************************
0002  * Copyright (c) 2010 Sergey Ivanov <123kash@gmail.com>                                 *
0003  * Copyright (c) 2013 Alberto Villa <avilla@FreeBSD.org>                                *
0004  *                                                                                      *
0005  * This program is free software; you can redistribute it and/or modify it under        *
0006  * the terms of the GNU General Public License as published by the Free Software        *
0007  * Foundation; either version 2 of the License, or (at your option) any later           *
0008  * version.                                                                             *
0009  *                                                                                      *
0010  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
0011  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
0012  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
0013  *                                                                                      *
0014  * You should have received a copy of the GNU General Public License along with         *
0015  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
0016  ****************************************************************************************/
0017 
0018 #define DEBUG_PREFIX "MusicBrainzTagDialog"
0019 
0020 #include "MusicBrainzTagger.h"
0021 
0022 #include "core/meta/Meta.h"
0023 #include "core/meta/support/MetaConstants.h"
0024 #include "core/meta/support/MetaUtility.h"
0025 #include "core/support/Amarok.h"
0026 #include "core/support/Debug.h"
0027 #include "musicbrainz/MusicBrainzFinder.h"
0028 #include "musicbrainz/MusicBrainzTagsModel.h"
0029 #include "musicbrainz/MusicBrainzTagsModelDelegate.h"
0030 #ifdef HAVE_LIBOFA
0031 #include "musicbrainz/MusicDNSFinder.h"
0032 #endif
0033 #include "ui_MusicBrainzTagger.h"
0034 
0035 #include <QIcon>
0036 #include <QSortFilterProxyModel>
0037 #include <QToolBar>
0038 #include <QToolButton>
0039 
0040 #include <KWindowConfig>
0041 
0042 MusicBrainzTagger::MusicBrainzTagger( const Meta::TrackList &tracks,
0043                                       QWidget *parent )
0044     : QDialog( parent )
0045     , ui( new Ui::MusicBrainzTagger() )
0046 {
0047     DEBUG_BLOCK
0048     foreach( Meta::TrackPtr track, tracks )
0049     {
0050         if( !track->playableUrl().toLocalFile().isEmpty() )
0051             m_tracks << track;
0052     }
0053     ui->setupUi( this );
0054     KConfigGroup group = Amarok::config( "MusicBrainzTagDialog" );
0055     KWindowConfig::restoreWindowSize( windowHandle(), group );
0056 
0057     init();
0058     search();
0059 }
0060 
0061 MusicBrainzTagger::~MusicBrainzTagger()
0062 {
0063     KConfigGroup group = Amarok::config( "MusicBrainzTagDialog" );
0064     group.writeEntry( "geometry", saveGeometry() );
0065     delete ui;
0066 }
0067 
0068 void
0069 MusicBrainzTagger::init()
0070 {
0071     DEBUG_BLOCK
0072     setAttribute( Qt::WA_DeleteOnClose );
0073     setMinimumSize( 550, 300 );
0074 
0075     m_resultsModel = new MusicBrainzTagsModel( this );
0076     m_resultsModelDelegate = new MusicBrainzTagsModelDelegate( this );
0077     m_resultsProxyModel = new QSortFilterProxyModel( this );
0078 
0079     m_resultsProxyModel->setSourceModel( m_resultsModel );
0080     m_resultsProxyModel->setSortRole( MusicBrainzTagsModel::SortRole );
0081     m_resultsProxyModel->setDynamicSortFilter( true );
0082 
0083     ui->resultsView->setModel( m_resultsProxyModel );
0084     ui->resultsView->setItemDelegate( m_resultsModelDelegate );
0085     // The column is not important, they all have the same data.
0086     ui->resultsView->sortByColumn( 0, Qt::AscendingOrder );
0087 
0088     if( m_tracks.count() > 1 )
0089     {
0090         QToolBar *toolBar = new QToolBar( this );
0091         toolBar->setToolButtonStyle( Qt::ToolButtonTextBesideIcon );
0092 
0093         QAction *lastAction = toolBar->addAction( QIcon::fromTheme( "tools-wizard" ), i18n( "Choose Best Matches" ), m_resultsModel, &MusicBrainzTagsModel::chooseBestMatches );
0094         lastAction->setToolTip( i18n( "Use the top result for each undecided track. Alternatively, you can click on <b>Choose Best Matches from This Album</b> in the context menu of a good suggestion; it may give even better results because it prevents mixing different album releases together." ) );
0095         lastAction = toolBar->addAction( QIcon::fromTheme( "edit-clear" ), i18n( "Clear Choices" ), m_resultsModel, &MusicBrainzTagsModel::clearChoices );
0096         lastAction->setToolTip( i18n( "Clear all choices, even manually made ones." ) );
0097 
0098         toolBar->addSeparator();
0099 
0100         QToolButton *lastButton = new QToolButton( toolBar );
0101         lastAction = new QAction( i18n( "Collapse Chosen" ), lastButton );
0102         connect( lastAction, &QAction::triggered,
0103                  ui->resultsView, &MusicBrainzTagsView::collapseChosen );
0104         lastButton->setDefaultAction( lastAction );
0105         lastAction = new QAction( i18n( "Collapse All" ), lastButton );
0106         connect( lastAction, &QAction::triggered,
0107                  ui->resultsView, &MusicBrainzTagsView::collapseAll );
0108         lastButton->addAction( lastAction );
0109         toolBar->addWidget( lastButton );
0110 
0111         lastButton = new QToolButton( toolBar );
0112         lastAction = new QAction( i18n( "Expand Unchosen" ), lastButton );
0113         connect( lastAction, &QAction::triggered,
0114                  ui->resultsView, &MusicBrainzTagsView::expandUnchosen );
0115         lastButton->setDefaultAction( lastAction );
0116         lastAction = new QAction( i18n( "Expand All" ), lastButton );
0117         connect( lastAction, &QAction::triggered,
0118                  ui->resultsView, &MusicBrainzTagsView::expandAll );
0119         lastButton->addAction( lastAction );
0120         toolBar->addWidget( lastButton );
0121 
0122         ui->verticalLayout->insertWidget( 0, toolBar );
0123     }
0124 
0125     ui->progressBar->hide();
0126 
0127     mb_finder = new MusicBrainzFinder( this );
0128 #ifdef HAVE_LIBOFA
0129     mdns_finder = new MusicDNSFinder( this );
0130     connect( mdns_finder, &MusicDNSFinder::trackFound,
0131              mb_finder, &MusicBrainzFinder::lookUpByPUID );
0132     connect( mdns_finder, &MusicDNSFinder::progressStep, this, &MusicBrainzTagger::progressStep );
0133     connect( mdns_finder, &MusicDNSFinder::done, this, &MusicBrainzTagger::mdnsSearchDone );
0134 #endif
0135     connect( mb_finder, &MusicBrainzFinder::done, this, &MusicBrainzTagger::searchDone );
0136     connect( mb_finder, &MusicBrainzFinder::trackFound,
0137              m_resultsModel, &MusicBrainzTagsModel::addTrack );
0138     connect( mb_finder, &MusicBrainzFinder::progressStep, this, &MusicBrainzTagger::progressStep );
0139     connect( ui->pushButton_saveAndClose, &QAbstractButton::clicked, this, &MusicBrainzTagger::saveAndExit );
0140     connect( ui->pushButton_cancel, &QAbstractButton::clicked, this, &MusicBrainzTagger::reject );
0141 }
0142 
0143 void
0144 MusicBrainzTagger::search()
0145 {
0146     int barSize = m_tracks.count();
0147     mb_finder->run( m_tracks );
0148 #ifdef HAVE_LIBOFA
0149     barSize *= 2;
0150     mdns_searchDone = false;
0151     mdns_finder->run( m_tracks );
0152 #endif
0153     ui->progressBar->setRange( 0, barSize );
0154     ui->progressBar->setValue( 0 );
0155     ui->horizontalSpacer->changeSize( 0, 0, QSizePolicy::Ignored );
0156     ui->progressBar->show();
0157 }
0158 
0159 void
0160 MusicBrainzTagger::saveAndExit()
0161 {
0162     QMap<Meta::TrackPtr, QVariantMap> result = m_resultsModel->chosenItems();
0163 
0164     if( !result.isEmpty() )
0165         Q_EMIT sendResult( result );
0166 
0167     accept();
0168 }
0169 
0170 void
0171 MusicBrainzTagger::searchDone()
0172 {
0173     DEBUG_BLOCK
0174 #ifdef HAVE_LIBOFA
0175     if( !mdns_searchDone )
0176         return;
0177 #endif
0178     ui->horizontalSpacer->changeSize( 0, 0, QSizePolicy::Expanding );
0179     ui->progressBar->hide();
0180     ui->resultsView->expandAll();
0181     ui->resultsView->header()->resizeSections( QHeaderView::ResizeToContents );
0182 }
0183 
0184 #ifdef HAVE_LIBOFA
0185 void
0186 MusicBrainzTagger::mdnsSearchDone()
0187 {
0188     DEBUG_BLOCK
0189     mdns_searchDone = true;
0190     if( !mb_finder->isRunning() )
0191         searchDone();
0192 }
0193 #endif
0194 
0195 void
0196 MusicBrainzTagger::progressStep()
0197 {
0198     ui->progressBar->setValue( ui->progressBar->value() + 1 );
0199 }
0200