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

0001 /****************************************************************************************
0002  * Copyright (c) 2010 Maximilian Kossick <maximilian.kossick@googlemail.com>            *
0003  * Copyright (c) 2013 Konrad Zemek <konrad.zemek@gmail.com>                             *
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 #ifndef COLLECTIONTESTIMPL_H
0019 #define COLLECTIONTESTIMPL_H
0020 
0021 #include "core/collections/Collection.h"
0022 #include "core/collections/CollectionLocation.h"
0023 #include "core-impl/collections/support/MemoryCollection.h"
0024 #include "core-impl/collections/support/MemoryQueryMaker.h"
0025 
0026 #include <QIcon>
0027 #include <QSharedPointer>
0028 
0029 namespace Collections {
0030 
0031 /**
0032  * A simple Collections::Collection implementation based on MemoryCollection
0033  */
0034 class CollectionTestImpl : public Collection
0035 {
0036 public:
0037     CollectionTestImpl( const QString &collectionId )
0038         : Collection()
0039         , id( collectionId )
0040         , mc( new MemoryCollection() )
0041     {
0042     }
0043 
0044     QueryMaker* queryMaker() override
0045     {
0046         return new MemoryQueryMaker( mc.toWeakRef(), id );
0047     }
0048 
0049     QIcon icon() const override
0050     {
0051         return QIcon();
0052     }
0053 
0054     QString collectionId() const override
0055     {
0056         return id;
0057     }
0058 
0059     QString prettyName() const override
0060     {
0061         return id;
0062     }
0063 
0064     CollectionLocation *location() override
0065     {
0066         return new CollectionLocationTestImpl( mc, this );
0067     }
0068 
0069     bool possiblyContainsTrack( const QUrl &url ) const override
0070     {
0071         return findTrackForUrl( url );
0072     }
0073 
0074     Meta::TrackPtr trackForUrl( const QUrl &url ) override
0075     {
0076         return findTrackForUrl( url );
0077     }
0078 
0079     QString id;
0080     QSharedPointer<MemoryCollection> mc;
0081 
0082 private:
0083     Meta::TrackPtr findTrackForUrl( const QUrl &url ) const
0084     {
0085         QReadLocker( mc->mapLock() );
0086 
0087         foreach( const Meta::TrackPtr track, mc->trackMap().values() )
0088             if( track->playableUrl() == url )
0089                 return track;
0090 
0091         return Meta::TrackPtr( nullptr );
0092     }
0093 
0094     class CollectionLocationTestImpl : public CollectionLocation
0095     {
0096     public:
0097         CollectionLocationTestImpl( QSharedPointer<MemoryCollection> mc,
0098                                     Collection *parentCollection )
0099             : CollectionLocation( parentCollection )
0100             , m_mc( mc )
0101         {
0102         }
0103 
0104         QString prettyLocation() const override
0105         {
0106             return "/" + collection()->prettyName();
0107         }
0108 
0109         bool isWritable() const override
0110         {
0111             return true;
0112         }
0113 
0114         void copyUrlsToCollection( const QMap<Meta::TrackPtr, QUrl> &sources,
0115                                    const Transcoding::Configuration &configuration ) override
0116         {
0117             Q_UNUSED( configuration );
0118 
0119             QWriteLocker( m_mc->mapLock() );
0120 
0121             foreach( Meta::TrackPtr track, sources.keys() )
0122             {
0123                 m_mc->addTrack( track );
0124                 transferSuccessful( track );
0125             }
0126 
0127             slotCopyOperationFinished();
0128         }
0129 
0130         bool insert( const Meta::TrackPtr &track, const QString &url ) override
0131         {
0132             Q_UNUSED( url );
0133 
0134             QWriteLocker( m_mc->mapLock() );
0135 
0136             if( m_mc->trackMap().contains( track->uidUrl() ) )
0137                 return false;
0138 
0139             m_mc->addTrack( track );
0140             return true;
0141         }
0142 
0143         QStringList actualLocation() const override
0144         {
0145             return QStringList() << prettyLocation();
0146         }
0147 
0148     private:
0149         QSharedPointer<MemoryCollection> m_mc;
0150     };
0151 };
0152 
0153 } //namespace Collections
0154 
0155 #endif