File indexing completed on 2024-05-19 04:50:18
0001 /**************************************************************************************** 0002 * Copyright (c) 2007 Nikolaj Hald Nielsen <nhn@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 "MagnatuneMeta.h" 0018 #include "MagnatuneStore.h" 0019 0020 #include "core/support/Amarok.h" 0021 #include "core/support/Debug.h" 0022 #include "core-impl/support/UrlStatisticsStore.h" 0023 #include "MagnatuneActions.h" 0024 #include "MagnatuneConfig.h" 0025 #include "SvgHandler.h" 0026 0027 #include <QObject> 0028 #include <QStandardPaths> 0029 0030 #include <KLocalizedString> 0031 0032 using namespace Meta; 0033 0034 MagnatuneMetaFactory::MagnatuneMetaFactory( const QString & dbPrefix, MagnatuneStore * store ) 0035 : ServiceMetaFactory( dbPrefix ) 0036 , m_membershipPrefix( QString() ) 0037 , m_streamType( OGG ) 0038 , m_userName( QString() ) 0039 , m_password( QString() ) 0040 , m_store( store ) 0041 {} 0042 0043 void MagnatuneMetaFactory::setMembershipInfo( const QString &prefix, const QString &userName, const QString &password ) 0044 { 0045 m_membershipPrefix = prefix; 0046 m_userName = userName; 0047 m_password = password; 0048 } 0049 0050 void MagnatuneMetaFactory::setStreamType(int type) 0051 { 0052 m_streamType = type; 0053 } 0054 0055 0056 int MagnatuneMetaFactory::getTrackSqlRowCount() 0057 { 0058 return ServiceMetaFactory::getTrackSqlRowCount() + 2; 0059 } 0060 0061 QString MagnatuneMetaFactory::getTrackSqlRows() 0062 { 0063 QString sqlRows = ServiceMetaFactory::getTrackSqlRows(); 0064 0065 sqlRows += ", "; 0066 sqlRows += tablePrefix() + "_tracks.preview_lofi, "; 0067 sqlRows += tablePrefix() + "_tracks.preview_ogg "; 0068 0069 return sqlRows; 0070 } 0071 0072 TrackPtr 0073 MagnatuneMetaFactory::createTrack(const QStringList & rows) 0074 { 0075 MagnatuneTrack *track = new MagnatuneTrack( rows ); 0076 0077 if ( m_streamType == OGG ) { 0078 track->setUidUrl( track->oggUrl() ); 0079 } else if ( m_streamType == LOFI ) { 0080 track->setUidUrl( track->lofiUrl() ); 0081 } 0082 track->setStatisticsProvider( Meta::StatisticsPtr( new UrlStatisticsStore( track ) ) ); 0083 0084 if ( !m_membershipPrefix.isEmpty() ) { 0085 QString url = track->uidUrl(); 0086 url.replace( "http://he3.", "http://" + m_userName + ":" + m_password + "@" + m_membershipPrefix + "." ); 0087 0088 if ( m_streamType == MP3 ) { 0089 url.replace( ".mp3", "_nospeech.mp3" ); 0090 } else if ( m_streamType == OGG ) { 0091 url.replace( ".ogg", "_nospeech.ogg" ); 0092 } 0093 0094 track->setUidUrl( url ); 0095 0096 if ( m_membershipPrefix == "download" ) 0097 track->setDownloadMembership(); 0098 } 0099 0100 return Meta::TrackPtr( track ); 0101 } 0102 0103 int MagnatuneMetaFactory::getAlbumSqlRowCount() 0104 { 0105 return ServiceMetaFactory::getAlbumSqlRowCount() + 3; 0106 } 0107 0108 QString MagnatuneMetaFactory::getAlbumSqlRows() 0109 { 0110 QString sqlRows = ServiceMetaFactory::getAlbumSqlRows(); 0111 0112 sqlRows += ", "; 0113 sqlRows += tablePrefix() + "_albums.cover_url, "; 0114 sqlRows += tablePrefix() + "_albums.year, "; 0115 sqlRows += tablePrefix() + "_albums.album_code "; 0116 0117 0118 return sqlRows; 0119 } 0120 0121 AlbumPtr MagnatuneMetaFactory::createAlbum(const QStringList & rows) 0122 { 0123 MagnatuneAlbum * album = new MagnatuneAlbum( rows ); 0124 album->setStore( m_store ); 0125 0126 if ( m_membershipPrefix == "download" ) 0127 album->setDownloadMembership(); 0128 0129 album->setSourceName( "Magnatune.com" ); 0130 return AlbumPtr( album ); 0131 } 0132 0133 int MagnatuneMetaFactory::getArtistSqlRowCount() 0134 { 0135 return ServiceMetaFactory::getArtistSqlRowCount() + 2; 0136 } 0137 0138 QString MagnatuneMetaFactory::getArtistSqlRows() 0139 { 0140 QString sqlRows = ServiceMetaFactory::getArtistSqlRows(); 0141 0142 sqlRows += ", "; 0143 sqlRows += tablePrefix() + "_artists.photo_url, "; 0144 sqlRows += tablePrefix() + "_artists.artist_page "; 0145 0146 return sqlRows; 0147 } 0148 0149 ArtistPtr MagnatuneMetaFactory::createArtist(const QStringList & rows) 0150 { 0151 MagnatuneArtist * artist = new MagnatuneArtist( rows ); 0152 artist->setSourceName( "Magnatune.com" ); 0153 return ArtistPtr( artist ); 0154 0155 0156 } 0157 0158 GenrePtr MagnatuneMetaFactory::createGenre(const QStringList & rows) 0159 { 0160 MagnatuneGenre * genre = new MagnatuneGenre( rows ); 0161 genre->setSourceName( "Magnatune.com" ); 0162 return GenrePtr( genre ); 0163 } 0164 0165 0166 /////////////////////////////////////////////////////////////////////////////// 0167 // class MagnatuneTrack 0168 /////////////////////////////////////////////////////////////////////////////// 0169 0170 MagnatuneTrack::MagnatuneTrack( const QString &name ) 0171 : ServiceTrack( name ) 0172 , m_downloadMembership ( false ) 0173 {} 0174 0175 MagnatuneTrack::MagnatuneTrack(const QStringList & resultRow) 0176 : ServiceTrack( resultRow ) 0177 , m_downloadMembership ( false ) 0178 { 0179 m_lofiUrl = resultRow[7]; 0180 m_oggUrl = resultRow[8]; 0181 } 0182 0183 QString MagnatuneTrack::lofiUrl() 0184 { 0185 return m_lofiUrl; 0186 } 0187 0188 void MagnatuneTrack::setLofiUrl(const QString & url) 0189 { 0190 m_lofiUrl = url; 0191 } 0192 0193 QString Meta::MagnatuneTrack::oggUrl() const 0194 { 0195 return m_oggUrl; 0196 } 0197 0198 void Meta::MagnatuneTrack::setOggUrl( const QString& url ) 0199 { 0200 m_oggUrl = url; 0201 } 0202 0203 void Meta::MagnatuneTrack::setDownloadMembership() 0204 { 0205 m_downloadMembership = true; 0206 } 0207 0208 QString Meta::MagnatuneTrack::sourceName() 0209 { 0210 return "Magnatune.com"; 0211 } 0212 0213 QString Meta::MagnatuneTrack::sourceDescription() 0214 { 0215 return i18n( "The non evil record label that is fair to artists and customers alike" ); 0216 } 0217 0218 QPixmap Meta::MagnatuneTrack::emblem() 0219 { 0220 return QPixmap( QStandardPaths::locate( QStandardPaths::GenericDataLocation, "amarok/images/emblem-magnatune.png" ) ); 0221 } 0222 0223 0224 QList< QString > Meta::MagnatuneTrack::moods() const 0225 { 0226 return m_moods; 0227 } 0228 0229 void Meta::MagnatuneTrack::setMoods(const QList<QString> &moods) 0230 { 0231 m_moods = moods; 0232 } 0233 0234 void Meta::MagnatuneTrack::download() 0235 { 0236 DEBUG_BLOCK 0237 MagnatuneAlbum * mAlbum = dynamic_cast<MagnatuneAlbum *> ( album().data() ); 0238 if ( mAlbum ) 0239 mAlbum->store()->downloadTrack( this ); 0240 } 0241 0242 void Meta::MagnatuneTrack::setAlbumPtr(const AlbumPtr &album ) 0243 { 0244 ServiceTrack::setAlbumPtr( album ); 0245 0246 //get year from magnatune album: 0247 MagnatuneAlbum * ma = dynamic_cast<MagnatuneAlbum *>( album.data() ); 0248 if ( ma ) 0249 { 0250 YearPtr year = YearPtr( new ServiceYear( QString::number( ma->launchYear() ) ) ); 0251 setYear( year ); 0252 } 0253 } 0254 0255 0256 /////////////////////////////////////////////////////////////////////////////// 0257 // class MagnatuneArtist 0258 /////////////////////////////////////////////////////////////////////////////// 0259 0260 MagnatuneArtist::MagnatuneArtist( const QString &name ) 0261 : ServiceArtist( name ) 0262 {} 0263 0264 MagnatuneArtist::MagnatuneArtist(const QStringList & resultRow) 0265 : ServiceArtist( resultRow ) 0266 { 0267 auto list = QUrl::fromStringList( resultRow ); 0268 0269 if( list.size() < 5 ) 0270 return; 0271 0272 m_photoUrl = list.at(3); 0273 m_magnatuneUrl = list.at(4); 0274 } 0275 0276 void MagnatuneArtist::setPhotoUrl( const QUrl &photoUrl ) 0277 { 0278 m_photoUrl = photoUrl; 0279 } 0280 0281 QUrl MagnatuneArtist::photoUrl( ) const 0282 { 0283 return m_photoUrl; 0284 } 0285 0286 void MagnatuneArtist::setMagnatuneUrl( const QUrl & magnatuneUrl ) 0287 { 0288 m_magnatuneUrl = magnatuneUrl; 0289 } 0290 0291 QUrl MagnatuneArtist::magnatuneUrl() const 0292 { 0293 return m_magnatuneUrl; 0294 } 0295 0296 0297 /////////////////////////////////////////////////////////////////////////////// 0298 // class MagnatuneAlbum 0299 /////////////////////////////////////////////////////////////////////////////// 0300 0301 MagnatuneAlbum::MagnatuneAlbum( const QString &name ) 0302 : ServiceAlbumWithCover( name ) 0303 , m_coverUrl() 0304 , m_launchYear( 0 ) 0305 , m_albumCode() 0306 , m_store( nullptr ) 0307 , m_downloadMembership( false ) 0308 0309 {} 0310 0311 MagnatuneAlbum::MagnatuneAlbum(const QStringList & resultRow) 0312 : ServiceAlbumWithCover( resultRow ) 0313 , m_downloadMembership ( false ) 0314 { 0315 m_coverUrl = resultRow[4]; 0316 m_launchYear = resultRow[5].toInt(); 0317 m_albumCode = resultRow[6]; 0318 0319 m_store = nullptr; 0320 } 0321 0322 MagnatuneAlbum::~ MagnatuneAlbum() 0323 {} 0324 0325 0326 void MagnatuneAlbum::setLaunchYear( int launchYear ) 0327 { 0328 m_launchYear = launchYear; 0329 } 0330 0331 int MagnatuneAlbum::launchYear( ) const 0332 { 0333 return m_launchYear; 0334 } 0335 0336 void MagnatuneAlbum::setAlbumCode(const QString & albumCode) 0337 { 0338 m_albumCode = albumCode; 0339 } 0340 0341 QString MagnatuneAlbum::albumCode() 0342 { 0343 return m_albumCode; 0344 0345 } 0346 0347 void MagnatuneAlbum::setCoverUrl(const QString & coverUrl) 0348 { 0349 m_coverUrl = coverUrl; 0350 } 0351 0352 QString MagnatuneAlbum::coverUrl() const 0353 { 0354 return m_coverUrl; 0355 } 0356 0357 void Meta::MagnatuneAlbum::setStore(MagnatuneStore * store) 0358 { 0359 m_store = store; 0360 } 0361 0362 MagnatuneStore * Meta::MagnatuneAlbum::store() 0363 { 0364 return m_store; 0365 } 0366 0367 void Meta::MagnatuneAlbum::setDownloadMembership() 0368 { 0369 DEBUG_BLOCK 0370 m_downloadMembership = true; 0371 } 0372 0373 void Meta::MagnatuneAlbum::download() 0374 { 0375 DEBUG_BLOCK 0376 if ( store() ) 0377 store()->downloadAlbum( this ); 0378 } 0379 0380 void Meta::MagnatuneAlbum::addToFavorites() 0381 { 0382 DEBUG_BLOCK 0383 if ( store() ) 0384 store()->addToFavorites( albumCode() ); 0385 } 0386 0387 /////////////////////////////////////////////////////////////////////////////// 0388 // class MagnatuneGenre 0389 /////////////////////////////////////////////////////////////////////////////// 0390 0391 MagnatuneGenre::MagnatuneGenre( const QString & name ) 0392 : ServiceGenre( name ) 0393 {} 0394 0395 MagnatuneGenre::MagnatuneGenre( const QStringList & resultRow ) 0396 : ServiceGenre( resultRow ) 0397 {} 0398