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

0001 /****************************************************************************************
0002  * Copyright (c) 2009 Maximilian Kossick <maximilian.kossick@googlemail.com>            *
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 "OneWaySynchronizationJob.h"
0018 
0019 #include "core/collections/Collection.h"
0020 #include "core/collections/CollectionLocation.h"
0021 #include "core/support/Debug.h"
0022 #include "core/meta/Meta.h"
0023 
0024 OneWaySynchronizationJob::OneWaySynchronizationJob()
0025         : SynchronizationBaseJob()
0026         , m_source( nullptr )
0027         , m_target( nullptr )
0028 {
0029 }
0030 
0031 OneWaySynchronizationJob::~OneWaySynchronizationJob()
0032 {
0033     //nothing to do
0034 }
0035 
0036 void
0037 OneWaySynchronizationJob::setSource( Collections::Collection *source )
0038 {
0039     m_source = source;
0040     setCollectionA( source );
0041 }
0042 
0043 void
0044 OneWaySynchronizationJob::setTarget( Collections::Collection *target )
0045 {
0046     m_target = target;
0047     //this will be slightly inefficient as SynchronizationBaseJob will figure out
0048     //the tracks that are in target but not in source, even though we do not care about
0049     //those.
0050     setCollectionB( target );
0051 }
0052 
0053 void
0054 OneWaySynchronizationJob::doSynchronization( const Meta::TrackList &tracks, InSet syncDirection, Collections::Collection *collA, Collections::Collection* collB )
0055 {
0056     DEBUG_BLOCK
0057     if( !( syncDirection == OnlyInA || syncDirection == OnlyInB ) )
0058     {
0059         debug() << "warning, received an unexpected syncDirection";
0060         return;
0061     }
0062     if( !( m_source == collA || m_source == collB ) || !( m_target == collA || m_target == collB ) )
0063     {
0064         debug() << "warning, received an unknown collection";
0065         return;
0066     }
0067     if( ( syncDirection == OnlyInA && collA == m_source ) || ( syncDirection == OnlyInB && collB == m_source ) )
0068     {
0069         debug() << "Master " << m_source->collectionId() << " has to sync " << tracks.count() << " track(s) to " << m_target->collectionId();
0070         //show confirmation dialog, actually do stuff
0071         Collections::CollectionLocation *locSource = m_source->location();
0072         Collections::CollectionLocation *locTarget = m_target->location();
0073         if( !locTarget->isWritable() )
0074         {
0075             debug() << "target collection " << m_target->collectionId() << " is not writable, what am I doing here?";
0076             locSource->deleteLater();
0077             locTarget->deleteLater();
0078             return;
0079         }
0080         //might be nice to ask the user if the tracks should really be copied here...
0081 
0082         //although not named particularly well, this method actually starts the workflow too
0083         locSource->prepareCopy( tracks, locTarget );
0084         //the collection locations will take care of the remaining work flow, we are done
0085     }
0086 }