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

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 #include "DatabaseImporterDialog.h"
0018 
0019 #include "core/meta/Meta.h"
0020 #include "core/support/Debug.h"
0021 #include "databaseimporter/SqlBatchImporter.h"
0022 #include "databaseimporter/SqlBatchImporterConfig.h"
0023 
0024 #include <KPageWidgetItem>
0025 #include <KLocalizedString>
0026 
0027 #include <QBoxLayout>
0028 #include <QButtonGroup>
0029 #include <QDialog>
0030 #include <QLabel>
0031 #include <QPlainTextEdit>
0032 #include <QRadioButton>
0033 #include <QStyle>
0034 
0035 DatabaseImporterDialog::DatabaseImporterDialog( QWidget *parent )
0036     : KAssistantDialog( parent )
0037     , m_importer( nullptr )
0038     , m_importerConfig( nullptr )
0039 {
0040     setAttribute( Qt::WA_DeleteOnClose );
0041     QWidget::setWindowTitle( i18n( "Import Collection" ) );
0042 
0043     BoxWidget *importerBox = new BoxWidget( true, this );
0044     importerBox->layout()->setSpacing( style()->layoutSpacing( QSizePolicy::DefaultType, QSizePolicy::DefaultType, Qt::Vertical ) );
0045 
0046     m_configBox = new BoxWidget( true, this );
0047     m_configBox->layout()->setSpacing( style()->layoutSpacing( QSizePolicy::DefaultType, QSizePolicy::DefaultType, Qt::Vertical ) );
0048 
0049     m_configPage = addPage( m_configBox, i18n("Import configuration") );
0050 
0051     m_importer = new SqlBatchImporter( this );
0052     connect( m_importer, &SqlBatchImporter::importSucceeded, this, &DatabaseImporterDialog::importSucceeded );
0053     connect( m_importer, &SqlBatchImporter::importFailed, this, &DatabaseImporterDialog::importFailed );
0054     connect( m_importer, &SqlBatchImporter::trackAdded, this, &DatabaseImporterDialog::importedTrack );
0055     connect( m_importer, &SqlBatchImporter::trackDiscarded, this, &DatabaseImporterDialog::discardedTrack );
0056     connect( m_importer, &SqlBatchImporter::trackMatchFound, this, &DatabaseImporterDialog::matchedTrack );
0057     connect( m_importer, &SqlBatchImporter::trackMatchMultiple, this, &DatabaseImporterDialog::ambigousTrack );
0058     connect( m_importer, &SqlBatchImporter::importError, this, &DatabaseImporterDialog::importError );
0059     connect( m_importer, &SqlBatchImporter::showMessage, this, &DatabaseImporterDialog::showMessage );
0060     m_importerConfig = m_importer->configWidget( m_configBox );
0061 
0062     BoxWidget *resultBox = new BoxWidget( true, this );
0063     resultBox->layout()->setSpacing( style()->layoutSpacing( QSizePolicy::DefaultType, QSizePolicy::DefaultType, Qt::Vertical ) );
0064 
0065     m_results = new QPlainTextEdit( resultBox );
0066     m_results->setReadOnly( true );
0067     m_results->setTabChangesFocus( true );
0068 
0069     m_resultsPage = addPage( resultBox, i18n("Migrating") );
0070 
0071     connect( this, &DatabaseImporterDialog::currentPageChanged, this, &DatabaseImporterDialog::pageChanged );
0072 }
0073 
0074 DatabaseImporterDialog::~DatabaseImporterDialog()
0075 {
0076     delete m_importer;
0077 }
0078 
0079 void
0080 DatabaseImporterDialog::pageChanged( KPageWidgetItem *current, KPageWidgetItem *before )
0081 {
0082     DEBUG_BLOCK
0083 
0084     if( before == m_configPage && current == m_resultsPage )
0085     {
0086         if( m_importer && !m_importer->importing() )
0087             m_importer->startImporting();
0088 
0089         QPushButton* user1Button = new QPushButton();
0090         user1Button->setEnabled( false );
0091         return;
0092     }
0093 }
0094 
0095 void
0096 DatabaseImporterDialog::importSucceeded()
0097 {
0098     // Special case the 0 import track count as it is really a failure
0099     QString text;
0100     if( !m_importer->importedCount() )
0101         text = i18n( "<b><font color='red'>Failed:</font></b> No tracks were imported" );
0102     else
0103         text = i18np( "<b><font color='green'>Success:</font></b> Imported %1 track",
0104                       "<b><font color='green'>Success:</font></b> Imported %1 tracks", m_importer->importedCount() );
0105 
0106     m_results->appendHtml( text );
0107 
0108     QPushButton* user1Button = new QPushButton();
0109     user1Button->setEnabled( true );
0110 }
0111 
0112 void
0113 DatabaseImporterDialog::importFailed()
0114 {
0115     QString text = i18n( "<b><font color='red'>Failed:</font></b> Unable to import statistics" );
0116     m_results->appendHtml( text );
0117 
0118     QPushButton* user1Button = new QPushButton();
0119     user1Button->setEnabled( true );
0120 
0121 }
0122 
0123 void
0124 DatabaseImporterDialog::showMessage( const QString &message )
0125 {
0126     m_results->appendHtml( message );
0127 }
0128 
0129 void
0130 DatabaseImporterDialog::importError( const QString &error )
0131 {
0132     QString text = i18n( "<b><font color='red'>Error:</font></b> %1", error );
0133     m_results->appendHtml( text );
0134 }
0135 
0136 void
0137 DatabaseImporterDialog::importedTrack( Meta::TrackPtr track )
0138 {
0139     if( !track ) return;
0140 
0141     QString text;
0142     Meta::ArtistPtr artist = track->artist();
0143     Meta::AlbumPtr album = track->album();
0144 
0145     if( !artist || artist->name().isEmpty() )
0146         text = i18nc( "Track has been imported, format: Track",
0147                       "Imported <b>%1</b>", track->name() );
0148     else if( !album || album->name().isEmpty() )
0149         text = i18nc( "Track has been imported, format: Artist - Track",
0150                       "Imported <b>%1 - %2</b>", artist->name(), track->name() );
0151     else
0152         text = i18nc( "Track has been imported, format: Artist - Track (Album)",
0153                       "Imported <b>%1 - %2 (%3)</b>", artist->name(), track->name(), album->name() );
0154 
0155     m_results->appendHtml( text );
0156 }
0157 
0158 void DatabaseImporterDialog::discardedTrack( const QString &url )
0159 {
0160     QString text;
0161     text = i18nc( "Track has been discarded, format: Url",
0162                   "Discarded <b><font color='gray'>%1</font></b>", url );
0163     m_results->appendHtml( text );
0164 }
0165 
0166 void DatabaseImporterDialog::matchedTrack( Meta::TrackPtr track, const QString &oldUrl )
0167 {
0168     if( !track ) return;
0169 
0170     QString text;
0171     Meta::ArtistPtr artist = track->artist();
0172     Meta::AlbumPtr album = track->album();
0173 
0174     //TODO: help text; also check wording with imported; unify?
0175     if( !artist || artist->name().isEmpty() )
0176         text = i18nc( "Track has been imported by tags, format: Track, from Url, to Url",
0177                       "Imported <b><font color='green'>%1</font></b><br/>&nbsp;&nbsp;from %2<br/>&nbsp;&nbsp;to %3", track->name(), oldUrl, track->prettyUrl() );
0178     else if( !album || album->name().isEmpty() )
0179         text = i18nc( "Track has been imported by tags, format: Artist - Track, from Url, to Url",
0180                       "Imported <b><font color='green'>%1 - %2</font></b><br/>&nbsp;&nbsp;from %3<br/>&nbsp;&nbsp;to %4", artist->name(), track->name(), oldUrl, track->prettyUrl() );
0181     else
0182         text = i18nc( "Track has been imported by tags, format: Artist - Track (Album), from Url, to Url",
0183                       "Imported <b><font color='green'>%1 - %2 (%3)</font></b><br/>&nbsp;&nbsp;from %4<br/>&nbsp;&nbsp;to %5", artist->name(), track->name(), album->name(), oldUrl, track->prettyUrl() );
0184 
0185     m_results->appendHtml( text );
0186 }
0187 
0188 void DatabaseImporterDialog::ambigousTrack( const Meta::TrackList &tracks, const QString &oldUrl )
0189 {
0190     Q_UNUSED( tracks );
0191 
0192     QString text;
0193     // TODO: wording; etc.
0194     text = i18nc( "Track has been matched ambiguously, format: Url",
0195                   "Multiple ambiguous matches found for <b><font color='red'>%1</font></b>, has been discarded.", oldUrl );
0196     m_results->appendHtml( text );
0197 }
0198 
0199