/thirdparty/liblastfm2/demos/demo1.cpp

http://github.com/tomahawk-player/tomahawk · C++ · 95 lines · 61 code · 17 blank · 17 comment · 1 complexity · eebde632f884bd96016c9c17991065be MD5 · raw file

  1. /*
  2. This software is in the public domain, furnished "as is", without technical
  3. support, and with no warranty, express or implied, as to its usefulness for
  4. any purpose.
  5. */
  6. #include <lastfm.h> // this includes everything in liblastfm, you may prefer
  7. #include <QtCore> // to just include what you need with your project. Still
  8. #include <QtGui> // we've given you the option.
  9. #include <QPointer>
  10. #include <QNetworkReply>
  11. class ArtistList : public QListWidget
  12. {
  13. Q_OBJECT
  14. QPointer<QNetworkReply> reply;
  15. QString artist;
  16. public:
  17. ArtistList()
  18. {
  19. connect( this,
  20. SIGNAL(itemActivated( QListWidgetItem* )),
  21. SLOT(onItemActivated( QListWidgetItem* )) );
  22. }
  23. void getSimilar( const QString& artist )
  24. {
  25. this->artist = artist;
  26. setWindowTitle( "Loading " + artist + "..." );
  27. // deleting a reply cancels the request and disconnects all signals
  28. delete reply;
  29. reply = lastfm::Artist( artist ).getSimilar();
  30. connect( reply, SIGNAL(finished()), SLOT(onGotSimilar()) );
  31. }
  32. private slots:
  33. void onGotSimilar()
  34. {
  35. QNetworkReply* r = static_cast<QNetworkReply*>(sender());
  36. // always enclose retrieval functions in a try block, as they will
  37. // throw if they can't parse the data
  38. try
  39. {
  40. // you decode the response using the equivalent static function
  41. QMap<int, QString> artists = lastfm::Artist::getSimilar( r );
  42. clear();
  43. // we iterate backwards because best match is last because the map
  44. // sorts itself by key
  45. QStringListIterator i( artists.values() );
  46. i.toBack();
  47. while (i.hasPrevious())
  48. addItem( i.previous() );
  49. setWindowTitle( artist );
  50. }
  51. catch (std::runtime_error& e)
  52. {
  53. // if getSimilar() failed to parse the QNetworkReply, then e will
  54. // be of type lastfm::ws::ParseError, which derives
  55. // std::runtime_error
  56. qWarning() << e.what();
  57. }
  58. }
  59. void onItemActivated( QListWidgetItem* item )
  60. {
  61. getSimilar( item->text() );
  62. }
  63. };
  64. int main( int argc, char** argv )
  65. {
  66. QApplication app( argc, argv );
  67. app.setApplicationName( "liblastfm" ); // used to generate UserAgent
  68. // all you need for non-authenticated webservices is your API key
  69. // this one is a public one, it can only do artist.getSimilar calls, so
  70. // I suggest you don't use it :P
  71. lastfm::ws::ApiKey = "b25b959554ed76058ac220b7b2e0a026";
  72. ArtistList artists;
  73. artists.getSimilar( "nirvana" );
  74. artists.resize( 300, 400 ); // Qt picks truly asanine default sizes for its widgets
  75. artists.show();
  76. return app.exec();
  77. }
  78. #include "demo1.moc"