File indexing completed on 2024-05-19 04:50:22
0001 /**************************************************************************************** 0002 * Copyright (c) 2007,2008 Casey Link <unnamedrambler@gmail.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 "Mp3tunesLocker.h" 0018 0019 #include "core/support/Debug.h" 0020 0021 #include <QByteArray> 0022 #include <QStringList> 0023 0024 #include <memory> 0025 0026 0027 Mp3tunesLocker::Mp3tunesLocker ( const QString & partnerToken ) 0028 { 0029 DEBUG_BLOCK 0030 m_locker = 0; 0031 debug() << "Creating New Locker"; 0032 QByteArray partner_token = partnerToken.toLatin1(); 0033 debug() << "Wrapper Token: " << partnerToken; 0034 mp3tunes_locker_init ( &m_locker, partner_token.constData() ); 0035 debug() << "New Locker created"; 0036 } 0037 0038 Mp3tunesLocker::Mp3tunesLocker ( const QString & partnerToken, const QString & userName, 0039 const QString & password ) 0040 { 0041 QByteArray partner_token = partnerToken.toLatin1(); 0042 mp3tunes_locker_init ( &m_locker, partner_token.constData() ); 0043 0044 this->login ( userName, password ); 0045 } 0046 0047 Mp3tunesLocker::~Mp3tunesLocker() 0048 { 0049 mp3tunes_locker_deinit ( &m_locker ); 0050 } 0051 0052 QString 0053 Mp3tunesLocker::login ( const QString &userName, const QString &password ) 0054 { 0055 DEBUG_BLOCK 0056 QByteArray user = userName.toLatin1(); 0057 QByteArray pw = password.toLatin1(); 0058 //result = 0 Login successful 0059 //result != 0 Login failed 0060 debug() << "Wrapper Logging on with: " << userName << ":" << password; 0061 int result = mp3tunes_locker_login ( m_locker, user.constData(), pw.constData() ); 0062 0063 if ( result == 0 ) 0064 { 0065 //login successful 0066 debug() << "Wrapper Login succeeded. result: " << result; 0067 return this->sessionId(); 0068 } 0069 debug() << "Wrapper Login failed. result: " << result; 0070 return QString(); //login failed 0071 } 0072 0073 QString 0074 Mp3tunesLocker::login () 0075 { 0076 return login( userName(), password() ); 0077 } 0078 bool 0079 Mp3tunesLocker::sessionValid() const 0080 { 0081 //result = 0 session valid 0082 //result != 0 session invalid 0083 int result = mp3tunes_locker_session_valid ( m_locker ); 0084 if ( result == 0 ) 0085 return true; 0086 return false; 0087 } 0088 0089 QList<Mp3tunesLockerArtist> 0090 Mp3tunesLocker::artists() const 0091 { 0092 DEBUG_BLOCK 0093 QList<Mp3tunesLockerArtist> artistsQList; // to be returned 0094 mp3tunes_locker_artist_list_t *artists_list; 0095 mp3tunes_locker_list_item_t *artist_item; 0096 0097 //get the list of artists 0098 mp3tunes_locker_artists ( m_locker, &artists_list ); 0099 0100 artist_item = artists_list->first; // the current node 0101 0102 //looping through the list of artists 0103 while ( artist_item != 0 ) 0104 { 0105 // get the artist from the c lib 0106 mp3tunes_locker_artist_t *artist = ( mp3tunes_locker_artist_t* ) artist_item->value; 0107 //debug() << "Wrapper Artist: " << artist->artistName; 0108 0109 //wrap it up 0110 Mp3tunesLockerArtist artistWrapped ( artist ); 0111 //and stick it in the QList 0112 artistsQList.append ( artistWrapped ); 0113 //advance to next artist 0114 //debug() << "Going to next artist"; 0115 artist_item = artist_item->next; 0116 } 0117 mp3tunes_locker_artist_list_deinit ( &artists_list ); 0118 debug() << "Wrapper deinit Complete"; 0119 return artistsQList; 0120 } 0121 0122 QList<Mp3tunesLockerArtist> 0123 Mp3tunesLocker::artistsSearch ( const QString &query ) const 0124 { 0125 DEBUG_BLOCK 0126 Mp3tunesSearchResult container; 0127 container.searchFor = Mp3tunesSearchResult::ArtistQuery; 0128 search ( container, query ); 0129 return container.artistList; 0130 } 0131 0132 QList<Mp3tunesLockerAlbum> 0133 Mp3tunesLocker::albums() const 0134 { 0135 QList<Mp3tunesLockerAlbum> albumsQList; // to be returned 0136 mp3tunes_locker_album_list_t *albums_list; 0137 mp3tunes_locker_list_item_t *album_item; 0138 0139 //get the list of albums 0140 mp3tunes_locker_albums ( m_locker, &albums_list ); 0141 0142 mp3tunes_locker_album_t *album; // the value holder 0143 album_item = albums_list->first; // the current node 0144 0145 //looping through the list of albums 0146 while ( album_item != 0 ) 0147 { 0148 // get the album from the c lib 0149 album = ( mp3tunes_locker_album_t* ) album_item->value; 0150 //wrap it up and stick it in the QList 0151 Mp3tunesLockerAlbum albumWrapped ( album ); 0152 albumsQList.append ( albumWrapped ); 0153 0154 album_item = album_item->next; 0155 } 0156 mp3tunes_locker_album_list_deinit ( &albums_list ); 0157 0158 return albumsQList; 0159 } 0160 0161 QList<Mp3tunesLockerAlbum> 0162 Mp3tunesLocker::albumsSearch ( const QString &query ) const 0163 { 0164 Mp3tunesSearchResult container; 0165 container.searchFor = Mp3tunesSearchResult::AlbumQuery; 0166 search ( container, query ); 0167 return container.albumList; 0168 } 0169 0170 QList<Mp3tunesLockerAlbum> 0171 Mp3tunesLocker::albumsWithArtistId ( int artistId ) const 0172 { 0173 QList<Mp3tunesLockerAlbum> albumsQList; // to be returned 0174 mp3tunes_locker_album_list_t *albums_list; 0175 mp3tunes_locker_list_item_t *album_item; 0176 0177 //get the list of albums 0178 mp3tunes_locker_albums_with_artist_id ( m_locker, &albums_list, artistId ); 0179 0180 mp3tunes_locker_album_t *album; // the value holder 0181 album_item = albums_list->first; // the current node 0182 0183 //looping through the list of albums 0184 while ( album_item != 0 ) 0185 { 0186 // get the album from the c lib 0187 album = ( mp3tunes_locker_album_t* ) album_item->value; 0188 //wrap it up 0189 Mp3tunesLockerAlbum albumWrapped ( album ); 0190 albumsQList.append ( albumWrapped ); 0191 0192 album_item = album_item->next; 0193 } 0194 mp3tunes_locker_album_list_deinit ( &albums_list ); 0195 0196 return albumsQList; 0197 } 0198 0199 QList<Mp3tunesLockerPlaylist> 0200 Mp3tunesLocker::playlists() const 0201 { 0202 QList<Mp3tunesLockerPlaylist> playlistsQList; // to be returned 0203 0204 mp3tunes_locker_playlist_list_t *playlist_list; 0205 mp3tunes_locker_list_item_t *playlist_item; 0206 mp3tunes_locker_playlist_t *playlist; 0207 0208 mp3tunes_locker_playlists ( this->m_locker, &playlist_list ); 0209 0210 playlist_item = playlist_list->first; 0211 while ( playlist_item != 0 ) 0212 { 0213 playlist = ( mp3tunes_locker_playlist_t* ) playlist_item->value; 0214 0215 Mp3tunesLockerPlaylist playlistWrapped ( playlist ); 0216 playlistsQList.append ( playlistWrapped ); 0217 0218 playlist_item = playlist_item->next; 0219 } 0220 mp3tunes_locker_playlist_list_deinit ( &playlist_list ); 0221 0222 return playlistsQList; 0223 } 0224 0225 QList<Mp3tunesLockerTrack> 0226 Mp3tunesLocker::tracks() const 0227 { 0228 QList<Mp3tunesLockerTrack> tracksQList; // to be returned 0229 0230 mp3tunes_locker_track_list_t *tracks_list; 0231 mp3tunes_locker_list_item_t *track_item; 0232 mp3tunes_locker_track_t *track; 0233 0234 mp3tunes_locker_tracks ( m_locker, &tracks_list ); 0235 0236 track_item = tracks_list->first; 0237 while ( track_item != 0 ) 0238 { 0239 track = ( mp3tunes_locker_track_t* ) track_item->value; 0240 0241 Mp3tunesLockerTrack trackWrapped ( track ); 0242 tracksQList.append ( trackWrapped ); 0243 0244 track_item = track_item->next; 0245 } 0246 mp3tunes_locker_track_list_deinit ( &tracks_list ); 0247 0248 return tracksQList; 0249 } 0250 0251 QList<Mp3tunesLockerTrack> 0252 Mp3tunesLocker::tracksSearch ( const QString &query ) const 0253 { 0254 Mp3tunesSearchResult container; 0255 container.searchFor = Mp3tunesSearchResult::TrackQuery; 0256 search ( container, query ); 0257 return container.trackList; 0258 } 0259 0260 QList<Mp3tunesLockerTrack> 0261 Mp3tunesLocker::tracksWithPlaylistId ( const QString & playlistId ) const 0262 { 0263 QByteArray playlistid = playlistId.toLatin1(); 0264 0265 QList<Mp3tunesLockerTrack> tracksQList; // to be returned 0266 0267 mp3tunes_locker_track_list_t *tracks_list; 0268 mp3tunes_locker_list_item_t *track_item; 0269 mp3tunes_locker_track_t *track; 0270 0271 mp3tunes_locker_tracks_with_playlist_id ( m_locker, &tracks_list, playlistid.constData() ); 0272 0273 track_item = tracks_list->first; 0274 while ( track_item != 0 ) 0275 { 0276 track = ( mp3tunes_locker_track_t* ) track_item->value; 0277 0278 Mp3tunesLockerTrack trackWrapped ( track ); 0279 tracksQList.append ( trackWrapped ); 0280 0281 track_item = track_item->next; 0282 } 0283 mp3tunes_locker_track_list_deinit ( &tracks_list ); 0284 0285 return tracksQList; 0286 } 0287 0288 QList<Mp3tunesLockerTrack> 0289 Mp3tunesLocker::tracksWithAlbumId ( int albumId ) const 0290 { 0291 QList<Mp3tunesLockerTrack> tracksQList; // to be returned 0292 0293 mp3tunes_locker_track_list_t *tracks_list; 0294 mp3tunes_locker_list_item_t *track_item; 0295 mp3tunes_locker_track_t *track; 0296 0297 mp3tunes_locker_tracks_with_album_id ( m_locker, &tracks_list, albumId ); 0298 0299 track_item = tracks_list->first; 0300 while ( track_item != 0 ) 0301 { 0302 track = ( mp3tunes_locker_track_t* ) track_item->value; 0303 0304 Mp3tunesLockerTrack trackWrapped ( track ); 0305 tracksQList.append ( trackWrapped ); 0306 0307 track_item = track_item->next; 0308 } 0309 mp3tunes_locker_track_list_deinit ( &tracks_list ); 0310 0311 return tracksQList; 0312 } 0313 0314 QList<Mp3tunesLockerTrack> 0315 Mp3tunesLocker::tracksWithArtistId ( int artistId ) const 0316 { 0317 QList<Mp3tunesLockerTrack> tracksQList; // to be returned 0318 0319 mp3tunes_locker_track_list_t *tracks_list; 0320 mp3tunes_locker_list_item_t *track_item; 0321 mp3tunes_locker_track_t *track; 0322 0323 mp3tunes_locker_tracks_with_artist_id ( m_locker, &tracks_list, artistId ); 0324 0325 track_item = tracks_list->first; 0326 while ( track_item != 0 ) 0327 { 0328 track = ( mp3tunes_locker_track_t* ) track_item->value; 0329 0330 Mp3tunesLockerTrack trackWrapped ( track ); 0331 tracksQList.append ( trackWrapped ); 0332 0333 track_item = track_item->next; 0334 } 0335 mp3tunes_locker_track_list_deinit ( &tracks_list ); 0336 0337 return tracksQList; 0338 } 0339 0340 QList<Mp3tunesLockerTrack> 0341 Mp3tunesLocker::tracksWithFileKeys( const QStringList &filekeys ) const 0342 { 0343 QString keys; 0344 foreach( const QString &key, filekeys ) 0345 { 0346 keys.append(key); 0347 keys.append(","); 0348 } 0349 keys.chop(1); 0350 QByteArray file_keys = keys.toLatin1(); 0351 0352 mp3tunes_locker_track_list_t* tracks_list = 0; 0353 mp3tunes_locker_list_item_t* track_item = 0; 0354 mp3tunes_locker_track_t *track = 0; 0355 QList<Mp3tunesLockerTrack> tracksQList; // to be returned, init'ed to null 0356 tracksQList.clear(); 0357 0358 if ( mp3tunes_locker_tracks_with_file_key ( m_locker, file_keys, &tracks_list ) 0359 || !tracks_list ) 0360 { 0361 mp3tunes_locker_track_list_deinit ( &tracks_list ); 0362 return tracksQList; 0363 } 0364 0365 track_item = tracks_list->first; 0366 while ( track_item ) 0367 { 0368 track = ( mp3tunes_locker_track_t* ) track_item->value; 0369 Mp3tunesLockerTrack trackWrapped ( track ); 0370 tracksQList.append ( trackWrapped ); 0371 0372 track_item = track_item->next; 0373 } 0374 0375 mp3tunes_locker_track_list_deinit ( &tracks_list ); 0376 return tracksQList; 0377 } 0378 0379 Mp3tunesLockerTrack 0380 Mp3tunesLocker::trackWithFileKey( const QString &filekey ) const 0381 { 0382 DEBUG_BLOCK 0383 QByteArray file_key = filekey.toLatin1(); 0384 0385 mp3tunes_locker_track_t *track = 0; 0386 mp3tunes_locker_track_with_file_key ( m_locker, file_key.constData(), &track ); 0387 if ( !track ) 0388 return Mp3tunesLockerTrack( 0 ); 0389 0390 debug() << "Got track: " << track->trackTitle << " from filekey: " << filekey; 0391 Mp3tunesLockerTrack trackWrapped ( track ); 0392 free( track ); 0393 debug() << "returning"; 0394 return trackWrapped; 0395 } 0396 0397 bool 0398 Mp3tunesLocker::search ( Mp3tunesSearchResult &container, const QString &query ) const 0399 { 0400 // setup vars 0401 mp3tunes_locker_artist_list_t *artists_list; 0402 mp3tunes_locker_list_item_t *artist_item; 0403 mp3tunes_locker_artist_t *artist; 0404 0405 mp3tunes_locker_album_list_t *albums_list; 0406 mp3tunes_locker_list_item_t *album_item; 0407 mp3tunes_locker_album_t *album; 0408 0409 mp3tunes_locker_track_list_t *tracks_list; 0410 mp3tunes_locker_list_item_t *track_item; 0411 mp3tunes_locker_track_t *track; 0412 0413 if ( container.searchFor & Mp3tunesSearchResult::ArtistQuery ) 0414 artists_list = 0; 0415 0416 if ( container.searchFor & Mp3tunesSearchResult::AlbumQuery ) 0417 albums_list = 0; 0418 0419 if ( container.searchFor & Mp3tunesSearchResult::TrackQuery ) 0420 tracks_list = 0; 0421 0422 QByteArray search_query = query.toLatin1(); 0423 0424 int res = mp3tunes_locker_search ( m_locker, &artists_list, &albums_list, 0425 &tracks_list, search_query.constData() ); 0426 0427 if ( res != 0 ) 0428 { 0429 if ( artists_list ) 0430 free( artists_list ); 0431 if ( albums_list ) 0432 free( albums_list ); 0433 if ( tracks_list ) 0434 free( tracks_list ); 0435 return false; 0436 } 0437 if ( !artists_list && ( container.searchFor & Mp3tunesSearchResult::ArtistQuery ) ) 0438 { 0439 if ( albums_list ) 0440 free( albums_list ); 0441 if ( tracks_list ) 0442 free( tracks_list ); 0443 0444 return false; 0445 } 0446 if ( !albums_list && ( container.searchFor & Mp3tunesSearchResult::AlbumQuery ) ) 0447 { 0448 if ( artists_list ) 0449 free( artists_list ); 0450 if ( tracks_list ) 0451 free( tracks_list ); 0452 0453 return false; 0454 } 0455 if ( !tracks_list && ( container.searchFor & Mp3tunesSearchResult::TrackQuery ) ) 0456 { 0457 if ( albums_list ) 0458 free( albums_list ); 0459 if ( artists_list ) 0460 free( artists_list ); 0461 0462 return false; 0463 } 0464 0465 if ( container.searchFor & Mp3tunesSearchResult::ArtistQuery ) 0466 { 0467 artist_item = artists_list->first; 0468 while ( artist_item != 0 ) 0469 { 0470 artist = ( mp3tunes_locker_artist_t* ) artist_item->value; 0471 0472 Mp3tunesLockerArtist artistWrapped ( artist ); 0473 container.artistList.append ( artistWrapped ); 0474 0475 artist_item = artist_item->next; 0476 } 0477 mp3tunes_locker_artist_list_deinit ( &artists_list ); 0478 } 0479 0480 if ( container.searchFor & Mp3tunesSearchResult::AlbumQuery ) 0481 { 0482 album_item = albums_list->first; 0483 while ( album_item != 0 ) 0484 { 0485 album = ( mp3tunes_locker_album_t* ) album_item->value; 0486 0487 Mp3tunesLockerAlbum albumWrapped ( album ); 0488 container.albumList.append ( albumWrapped ); 0489 0490 album_item = album_item->next; 0491 } 0492 mp3tunes_locker_album_list_deinit ( &albums_list ); 0493 } 0494 0495 if ( container.searchFor & Mp3tunesSearchResult::TrackQuery ) 0496 { 0497 track_item = tracks_list->first; 0498 while ( track_item != 0 ) 0499 { 0500 track = ( mp3tunes_locker_track_t* ) track_item->value; 0501 0502 Mp3tunesLockerTrack trackWrapped ( track ); 0503 container.trackList.append ( trackWrapped ); 0504 0505 track_item = track_item->next; 0506 } 0507 mp3tunes_locker_track_list_deinit ( &tracks_list ); 0508 } 0509 return true; 0510 } 0511 0512 bool 0513 Mp3tunesLocker::uploadTrack ( const QString &path ) 0514 { 0515 QByteArray track_path = path.toUtf8(); 0516 0517 int res = mp3tunes_locker_upload_track ( m_locker, track_path.constData() ); 0518 0519 return ( res == 0 ); 0520 } 0521 0522 QString 0523 Mp3tunesLocker::fileKey ( const QString &path ) 0524 { 0525 QByteArray fk_path = path.toLatin1(); 0526 0527 char* file_key = mp3tunes_locker_generate_filekey ( fk_path.constData() ); 0528 0529 return QString ( file_key ); 0530 } 0531 0532 bool 0533 Mp3tunesLocker::lockerLoad( const QString &url ) 0534 { 0535 QByteArray locker_url = url.toLatin1(); 0536 0537 int res = mp3tunes_locker_load_track ( m_locker, locker_url.constData() ); 0538 0539 return ( res == 0); 0540 } 0541 0542 QString 0543 Mp3tunesLocker::userName() const 0544 { 0545 return QString ( m_locker->username ); 0546 } 0547 0548 QString 0549 Mp3tunesLocker::password() const 0550 { 0551 return QString ( m_locker->password ); 0552 } 0553 0554 QString 0555 Mp3tunesLocker::sessionId() const 0556 { 0557 return QString ( m_locker->session_id ); 0558 } 0559 0560 QString 0561 Mp3tunesLocker::firstName() const 0562 { 0563 return QString ( m_locker->firstname ); 0564 } 0565 0566 QString 0567 Mp3tunesLocker::lastName() const 0568 { 0569 return QString ( m_locker->lastname ); 0570 } 0571 0572 QString 0573 Mp3tunesLocker::nickName() const 0574 { 0575 return QString ( m_locker->nickname ); 0576 } 0577 0578 QString 0579 Mp3tunesLocker::partnerToken() const 0580 { 0581 return QString ( m_locker->partner_token ); 0582 } 0583 0584 QString 0585 Mp3tunesLocker::serverApi() const 0586 { 0587 return QString ( m_locker->server_api ); 0588 } 0589 0590 QString 0591 Mp3tunesLocker::serverContent() const 0592 { 0593 return QString ( m_locker->server_content ); 0594 } 0595 0596 QString 0597 Mp3tunesLocker::serverLogin() const 0598 { 0599 return QString ( m_locker->server_login ); 0600 } 0601 0602 QString 0603 Mp3tunesLocker::errorMessage() const 0604 { 0605 if( m_locker->error_message != 0 ) 0606 { 0607 return QString ( m_locker->error_message ); 0608 } 0609 return QString(); 0610 } 0611 bool 0612 Mp3tunesLocker::authenticated() const 0613 { 0614 // do it in this order to avoid making an extra http request 0615 if( sessionId().isEmpty() ) 0616 return false; 0617 else if( sessionValid() ) 0618 return true; 0619 return false; 0620 }