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

0001 /****************************************************************************************
0002  * Copyright (c) 2009 Rick W. Chen <stuffcorpse@archlinux.us>                           *
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 "CoverFetchQueue.h"
0018 
0019 
0020 CoverFetchQueue::CoverFetchQueue( QObject *parent )
0021     : QObject( parent )
0022 {
0023 }
0024 
0025 CoverFetchQueue::~CoverFetchQueue()
0026 {
0027 }
0028 
0029 void
0030 CoverFetchQueue::add( const CoverFetchUnit::Ptr &unit )
0031 {
0032     m_queue.append( unit );
0033     Q_EMIT fetchUnitAdded( unit );
0034 }
0035 
0036 void
0037 CoverFetchQueue::add( const Meta::AlbumPtr &album,
0038                       const CoverFetch::Option opt,
0039                       const CoverFetch::Source src,
0040                       const QByteArray &xml )
0041 {
0042     CoverFetchPayload *payload;
0043     if( xml.isEmpty() )
0044     {
0045         payload = new CoverFetchInfoPayload( album, src );
0046     }
0047     else
0048     {
0049         CoverFetch::ImageSize imageSize;
0050         if( opt == CoverFetch::Automatic )
0051             imageSize = CoverFetch::NormalSize;
0052         else
0053             imageSize = CoverFetch::ThumbSize;
0054 
0055         const bool wild = ( opt == CoverFetch::WildInteractive ) ? true : false;
0056         CoverFetchArtPayload *art = new CoverFetchArtPayload( album, imageSize, src, wild );
0057         art->setXml( xml );
0058         payload = art;
0059     }
0060     add( AmarokSharedPointer< CoverFetchUnit >( new CoverFetchUnit( album, payload, opt ) ) );
0061 }
0062 
0063 void
0064 CoverFetchQueue::add( const CoverFetch::Option opt,
0065                       const CoverFetch::Source src,
0066                       const QByteArray &xml )
0067 {
0068     switch( src )
0069     {
0070         default:
0071         case CoverFetch::Google:
0072         case CoverFetch::LastFm:
0073         {
0074             typedef CoverFetchArtPayload CFAP;
0075             const bool wild = ( opt == CoverFetch::WildInteractive ) ? true : false;
0076             CFAP *payload = new CFAP( CoverFetch::ThumbSize, src, wild );
0077             payload->setXml( xml );
0078             add( AmarokSharedPointer< CoverFetchUnit >( new CoverFetchUnit( payload, opt ) ) );
0079         }
0080         break;
0081 
0082         case CoverFetch::Discogs:
0083         {
0084             typedef CoverFetchInfoPayload CFIP;
0085             CFIP *payload = new CFIP( src, xml );
0086             add( AmarokSharedPointer< CoverFetchUnit >( new CoverFetchUnit( payload, opt ) ) );
0087         }
0088         break;
0089     }
0090 }
0091 
0092 void
0093 CoverFetchQueue::addQuery( const QString &query,
0094                            const CoverFetch::Source src,
0095                            unsigned int page,
0096                            const Meta::AlbumPtr &album )
0097 {
0098     CoverFetchSearchPayload *payload = new CoverFetchSearchPayload( query, src, page, album );
0099     add( AmarokSharedPointer< CoverFetchUnit >( new CoverFetchUnit( payload ) ) );
0100 }
0101 
0102 void
0103 CoverFetchQueue::clear()
0104 {
0105     m_queue.clear();
0106 }
0107 
0108 void
0109 CoverFetchQueue::remove( const CoverFetchUnit::Ptr &unit )
0110 {
0111     m_queue.removeAll( unit );
0112 }
0113 
0114 void
0115 CoverFetchQueue::remove( const Meta::AlbumPtr &album )
0116 {
0117     m_queue.removeAt( index( album ) );
0118 }
0119 
0120 bool
0121 CoverFetchQueue::contains( const Meta::AlbumPtr &album ) const
0122 {
0123     typedef CoverFetchUnitList::const_iterator ListIter;
0124     ListIter it   = m_queue.constBegin();
0125     ListIter last = m_queue.constEnd();
0126     while( it != last )
0127     {
0128         Meta::AlbumPtr t_album = (*it)->album();
0129         if( t_album == album )
0130             return true;
0131         ++it;
0132     }
0133     return false;
0134 }
0135 
0136 int
0137 CoverFetchQueue::index( const Meta::AlbumPtr &album ) const
0138 {
0139     for( int i = 0, len = m_queue.size(); i < len; ++i )
0140     {
0141         if( m_queue.at( i )->album() == album )
0142             return i;
0143     }
0144     return -1;
0145 }
0146 
0147 const CoverFetchUnit::Ptr
0148 CoverFetchQueue::take( const Meta::AlbumPtr &album )
0149 {
0150     for( int i = 0, end = m_queue.size(); i < end; ++i )
0151     {
0152         const CoverFetchUnit::Ptr unit = m_queue.at( i );
0153         if( unit->album() == album )
0154         {
0155             m_queue.removeAt( i );
0156             return unit;
0157         }
0158     }
0159     // need to test if album exists with contains() first
0160     return AmarokSharedPointer< CoverFetchUnit >();
0161 }
0162