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