PageRenderTime 143ms CodeModel.GetById 60ms app.highlight 7ms RepoModel.GetById 74ms app.codeStats 0ms

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