You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

song.cpp 1.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #include "song.h"
  2. Song::Song(QObject *parent) : QObject(parent), cover(0, 0)
  3. {
  4. tries = 0;
  5. }
  6. QString Song::getArtist() const
  7. {
  8. return artist;
  9. }
  10. QString Song::getTitle() const
  11. {
  12. return title;
  13. }
  14. QUrl Song::getCoverUrl() const
  15. {
  16. return coverUrl;
  17. }
  18. int Song::getDuration() const
  19. {
  20. return duration;
  21. }
  22. void Song::setArtist(QString a)
  23. {
  24. artist = a;
  25. }
  26. void Song::setTitle(QString t)
  27. {
  28. title = t;
  29. }
  30. void Song::setCoverUrl(QUrl u)
  31. {
  32. coverUrl = u;
  33. }
  34. void Song::setDuration(int d)
  35. {
  36. duration = d;
  37. }
  38. void Song::downloadCover()
  39. {
  40. if(cover.isNull())
  41. {
  42. if(mgr != 0)
  43. {
  44. tries = 0;
  45. QNetworkReply* reply = mgr->get(QNetworkRequest(coverUrl));
  46. connect(reply, SIGNAL(finished()), this, SLOT(onCoverFinished()));
  47. connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(onCoverError(QNetworkReply::NetworkError)));
  48. }
  49. }
  50. else
  51. emit coverDownloaded(cover);
  52. }
  53. void Song::setNetworkManager(QNetworkAccessManager *m)
  54. {
  55. mgr = m;
  56. }
  57. void Song::onCoverFinished()
  58. {
  59. QNetworkReply* reply = (QNetworkReply*)sender();
  60. if(cover.loadFromData(reply->readAll()) && !cover.isNull())
  61. emit coverDownloaded(cover);
  62. }
  63. void Song::onCoverError(QNetworkReply::NetworkError)
  64. {
  65. if(tries < 3)
  66. {
  67. ++tries;
  68. downloadCover();
  69. }
  70. }