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

0001 /****************************************************************************************
0002  * Copyright (c) 2010 Ralf Engels <ralf-engels@gmx.de>                                  *
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 "SqlBatchImporter.h"
0018 #include "SqlBatchImporterConfig.h"
0019 
0020 #include "core/collections/Collection.h"
0021 #include "core/meta/Meta.h"
0022 #include "core/support/Debug.h"
0023 #include "core/capabilities/CollectionImportCapability.h"
0024 #include "core-impl/collections/support/CollectionManager.h"
0025 
0026 #include <KLocalizedString>
0027 
0028 #include <QFile>
0029 
0030 SqlBatchImporter::SqlBatchImporter( QObject *parent )
0031     : QObject( parent )
0032     , m_config( nullptr )
0033     , m_count( 0 )
0034     , m_importing( false )
0035 {
0036     connect( this, &SqlBatchImporter::importSucceeded, this, &SqlBatchImporter::importingFinished );
0037     connect( this, &SqlBatchImporter::importFailed, this, &SqlBatchImporter::importingFinished );
0038     connect( this, &SqlBatchImporter::trackAdded, this, &SqlBatchImporter::trackImported );
0039     connect( this, &SqlBatchImporter::trackMatchFound, this, &SqlBatchImporter::trackMatched );
0040 }
0041 
0042 SqlBatchImporter::~SqlBatchImporter()
0043 {
0044 }
0045 
0046 SqlBatchImporterConfig*
0047 SqlBatchImporter::configWidget( QWidget *parent )
0048 {
0049     if( !m_config )
0050         m_config = new SqlBatchImporterConfig( parent );
0051     return m_config;
0052 }
0053 
0054 void
0055 SqlBatchImporter::import()
0056 {
0057     DEBUG_BLOCK
0058 
0059     Q_ASSERT( m_config );
0060     if( !m_config )
0061     {
0062         error() << "No configuration exists, bailing out of import";
0063         return;
0064     }
0065 
0066     int numStarted = 0;
0067     // search for a collection with the CollectionImportCapability
0068     foreach( Collections::Collection *coll, CollectionManager::instance()->collections().keys() )
0069     {
0070         debug() << "Collection: "<<coll->prettyName() << "id:"<<coll->collectionId();
0071         QScopedPointer<Capabilities::CollectionImportCapability> cic( coll->create<Capabilities::CollectionImportCapability>());
0072 
0073         if( cic ) {
0074 
0075             QFile *file = new QFile( m_config->inputFilePath() );
0076             if( file->open( QIODevice::ReadOnly ) )
0077             {
0078                 debug() << "importing db";
0079                 cic->import( file, this );
0080                 numStarted++;
0081             } else {
0082                 debug() << "could not open";
0083                 Q_EMIT importError( i18n( "Could not open file \"%1\".", m_config->inputFilePath() ) );
0084                 delete file;
0085             }
0086         }
0087     }
0088 
0089     if( !numStarted )
0090         Q_EMIT importFailed();
0091 }
0092 
0093 int
0094 SqlBatchImporter::importedCount() const
0095 {
0096     return m_count;
0097 }
0098 
0099 void
0100 SqlBatchImporter::trackImported( const Meta::TrackPtr &track )
0101 {
0102     Q_UNUSED( track )
0103     ++m_count;
0104 }
0105 
0106 void
0107 SqlBatchImporter::trackMatched(const Meta::TrackPtr &track, const QString &oldUrl )
0108 {
0109     Q_UNUSED( track )
0110     Q_UNUSED( oldUrl )
0111     ++m_count;
0112 }
0113 
0114 bool
0115 SqlBatchImporter::importing() const
0116 {
0117     return m_importing;
0118 }
0119 
0120 void
0121 SqlBatchImporter::startImporting()
0122 {
0123     DEBUG_BLOCK
0124     m_importing = true;
0125     import();
0126 }
0127 
0128 void
0129 SqlBatchImporter::importingFinished()
0130 {
0131     m_importing = false;
0132 }