1 /***************************************************************************
2 * copyright : (C) 2006 Chris Muehlhaeuser <chris@chris.de> *
3 * : (C) 2006 Seb Ruiz <ruiz@kde.org> *
4 * : (C) 2006 Ian Monroe <ian@monroe.nu> *
5 * : (C) 2006 Mark Kretschmann <markey@web.de> *
6 **************************************************************************/
8 /***************************************************************************
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. *
15 ***************************************************************************/
17 #ifndef AMAROK_LASTFM_H
18 #define AMAROK_LASTFM_H
20 #include "metabundle.h"
24 #include <q3serversocket.h>
26 #include <q3valuelist.h>
30 #include <kconfigdialog.h>
38 namespace KIO
{ class Job
; }
40 /* AmarokHttp is a hack written so that lastfm code could easily use something proxy aware.
41 DO NOT use this class for anything else, use KIO directly instead. */
42 class AmarokHttp
: public QObject
47 explicit AmarokHttp ( const QString
& hostname
, quint16 port
= 80, QObject
* parent
= 0 );
48 int get ( const QString
& path
);
49 Q3Http::State
state() const;
50 QByteArray
readAll ();
51 Q3Http::Error
error();
54 void requestFinished ( int id
, bool error
);
57 void slotData(KIO::Job
*, const QByteArray
& );
58 void slotResult(KJob
*);
64 Q3Http::State m_state
;
65 Q3Http::Error m_error
;
75 class Controller
: public QObject
80 static Controller
* instance();
82 KUrl
getNewProxy( QString genreUrl
);
84 bool isPlaying() const { return m_service
!= 0; }
85 WebService
* getService() const { return m_service
; }
86 QString
getGenreUrl() const { return m_genreUrl
; }
88 static bool checkCredentials();
89 static QString
createCustomStation();
90 static QString
stationDescription( QString url
= QString() ); // necessary for translation
93 void playbackStopped();
100 void setActionsEnabled( bool enable
);
102 static Controller
*s_instance
;
103 Q3PtrList
<KAction
> m_actionList
;
106 WebService
* m_service
;
109 class WebService
: public QObject
114 enum DataType
{ Artist
, Album
, Track
};
116 WebService( QObject
* parent
);
119 bool handshake( const QString
& username
, const QString
& password
);
121 bool changeStation( QString url
);
122 QString
currentUsername() const { return m_username
; }
123 QString
currentPassword() const { return m_password
; }
124 QString
currentStation() const { return m_station
; }
125 QString
session() const { return m_session
; }
126 Q3Url
streamUrl() const { return m_streamUrl
; }
128 void friends( QString username
);
129 void neighbours( QString username
);
131 void recentTracks( QString username
);
132 void userTags( QString username
);
134 void recommend( int type
, QString username
, QString artist
, QString token
= QString() );
136 void recommendArtist( QString username
, QString artist
)
137 { recommend( WebService::Artist
, username
, artist
); }
139 void recommendAlbum( QString username
, QString artist
, QString album
)
140 { recommend( WebService::Album
, username
, artist
, album
); }
142 void recommendTrack( QString username
, QString artist
, QString track
)
143 { recommend( WebService::Track
, username
, artist
, track
); }
146 Verify with server that a supplied user/pass combo is valid. Password
147 should be MD5 hashed.
149 void verifyUser( const QString
& user
, const QString
& pass
);
151 ProcIO
* getServer() { return m_server
; }
152 QString
proxyUrl() { return m_proxyUrl
; }
154 void showError( int code
, QString message
= QString() );
155 QString
parameter( const QString keyName
, const QString data
) const;
157 void addObserver( QObject
* observer
) { m_metaDataObservers
.append( observer
); }
160 void requestMetaData();
161 void enableScrobbling( bool enabled
);
168 void actionStarted();
169 void actionFinished();
171 void stationChanged( QString url
, QString name
);
174 void metaDataResult( const MetaBundle
&bundle
);
175 void enableScrobblingDone();
181 void friendsResult( const QString
& username
, const QStringList
& friends
);
182 void neighboursResult( const QString
& username
, const QStringList
& friends
);
184 void recentTracksResult( const QString
& username
, Q3ValueList
< QPair
<QString
, QString
> > songs
);
185 void userTagsResult( const QString
& username
, const QStringList
& tags
);
188 enum errorCode
{ E_NOCONTENT
= 1, E_NOMEMBERS
= 2, E_NOFANS
= 3, E_NOAVAIL
= 4, E_NOSUBSCRIBER
= 5,
189 E_NONEIGHBOURS
= 6, E_NOSTOPPED
= 7, E_OTHER
= 0 };
193 QStringList
parameterArray( const QString keyName
, const QString data
) const;
194 QStringList
parameterKeys( const QString keyName
, const QString data
) const;
196 QString m_username
; // login username
197 QString m_password
; // login password
198 QString m_station
; // the url of the station
199 QString m_session
; // session id that last.fm provides
200 QString m_baseHost
; // who are we connecting to?
201 QString m_basePath
; // where are we connecting to!
202 Q3Url m_streamUrl
; // last.fm webserver for direct connection (proxy connects to this)
203 bool m_subscriber
; // self explanatory
207 MetaBundle m_metaBundle
;
209 QList
<QObject
*> m_metaDataObservers
; //temporary hack
213 void metaDataFinished( int id
, bool error
);
214 void fetchImageFinished( KIO::Job
* );
215 void enableScrobblingFinished( int id
, bool error
);
217 void loveFinished( int id
, bool error
);
218 void skipFinished( int id
, bool error
);
219 void banFinished( int id
, bool error
);
221 void friendsFinished( int id
, bool error
);
222 void neighboursFinished( int id
, bool error
);
224 void recentTracksFinished( int id
, bool error
);
225 void userTagsFinished( int id
, bool error
);
227 void recommendFinished( int id
, bool error
);
234 Bundle( const Bundle
& bundle
);
235 QString
imageUrl() const { return m_imageUrl
; }
236 void setImageUrl( const QString
& imageUrl
) { m_imageUrl
= imageUrl
; }
238 QString
artistUrl() const { return m_artistUrl
; }
239 void setArtistUrl( const QString
& theValue
) { m_artistUrl
= theValue
; }
241 QString
albumUrl() const { return m_albumUrl
; }
242 void setAlbumUrl( const QString
& theValue
) { m_albumUrl
= theValue
; }
244 QString
titleUrl() const { return m_titleUrl
; }
245 void setTitleUrl( const QString
& theValue
) { m_titleUrl
= theValue
; }
254 // We must implement this because QServerSocket has one pure virtual method.
255 // It's just used for finding a free port.
256 class MyServerSocket
: public Q3ServerSocket
259 MyServerSocket() : Q3ServerSocket( quint16( 0 ) ) {}
262 void newConnection( int ) {}
266 class LoginDialog
: public KDialog
271 LoginDialog( QWidget
*parent
);
274 void slotButtonClicked( ButtonCode button
);
277 KLineEdit
*m_userLineEdit
;
278 KLineEdit
*m_passLineEdit
;
282 class CustomStationDialog
: public KDialog
287 CustomStationDialog( QWidget
*parent
);
289 QString
text() const;
296 #endif /*AMAROK_LASTFM_H*/