add mp3 and ogg torrent url info to JamendoAlbum
[amarok.git] / src / scrobbler.h
blobf08b810a9414882ae450aa5a6c556be6818c4857
1 /*
2 Copyright (c) 2004 Christian Muehlhaeuser <chris@chris.de>
3 Copyright (c) 2004 Sami Nieminen <sami.nieminen@iki.fi>
4 Copyright (c) 2006 Shane King <kde@dontletsstart.com>
5 Copyright (c) 2006 Iain Benson <iain@arctos.me.uk>
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License
9 as published by the Free Software Foundation; either version 2
10 of the License, or (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 #ifndef AMAROK_SCROBBLER_H
23 #define AMAROK_SCROBBLER_H
25 #include "engineobserver.h"
27 #include <QDomDocument>
28 #include <QDomElement>
29 #include <QHash>
30 #include <QList>
31 #include <QObject>
32 #include <QTimer>
34 //some setups require this
35 #undef PROTOCOL_VERSION
37 namespace KIO { class Job; }
39 class MediaDevice;
40 class QStringList;
41 class SubmitItem;
42 class SubmitQueue;
43 class ScrobblerSubmitter;
45 class Scrobbler : public QObject, public EngineObserver
47 friend class MediaDevice;
49 Q_OBJECT
51 public:
52 static Scrobbler *instance();
54 void similarArtists( const QString & /*artist*/ );
55 void applySettings();
57 signals:
58 void similarArtistsFetched( const QString& artist, const QStringList& suggestions );
60 public slots:
61 void subTrack( long currentPos, long startPos, long endPos ); // cuefiles can update length without track change
63 protected:
64 Scrobbler();
65 ~Scrobbler();
67 void engineNewMetaData( const MetaBundle& /*bundle*/, bool /*state*/ );
68 void engineTrackPositionChanged( long /*position*/ , bool /*userSeek*/ );
70 private slots:
71 void audioScrobblerSimilarArtistsResult( KIO::Job* /*job*/ );
72 void audioScrobblerSimilarArtistsData(
73 KIO::Job* /*job*/, const QByteArray& /*data*/ );
75 private:
76 QTimer m_timer; //works around xine bug
77 //http://sourceforge.net/tracker/index.php?func=detail&aid=1401026&group_id=9655&atid=109655
78 QByteArray m_similarArtistsBuffer;
79 KIO::Job* m_similarArtistsJob;
80 QString m_artist;
81 bool m_validForSending;
82 long m_startPos;
83 ScrobblerSubmitter* m_submitter;
84 SubmitItem* m_item;
88 class SubmitItem
90 friend class ScrobblerSubmitter;
92 public:
93 SubmitItem(
94 const QString& /*artist*/,
95 const QString& /*album*/,
96 const QString& /*title*/,
97 int /*length*/,
98 bool now = true );
99 SubmitItem( const QDomElement& /* domElement */ );
100 SubmitItem();
102 bool operator==( const SubmitItem& item );
104 void setArtist( const QString& artist ) { m_artist = artist; }
105 void setAlbum( const QString& album ) { m_album = album; }
106 void setTitle( const QString& title ) { m_title = title; }
107 const QString artist() const { return m_artist; }
108 const QString album() const { return m_album; }
109 const QString title() const { return m_title; }
110 int length() const { return m_length; }
111 uint playStartTime() const { return m_playStartTime; }
113 QDomElement toDomElement( QDomDocument& /* document */ ) const;
115 bool valid() const { return !m_artist.isEmpty() && !m_title.isEmpty() && m_length >= 30; }
117 private:
118 QString m_artist;
119 QString m_album;
120 QString m_title;
121 int m_length;
122 uint m_playStartTime;
126 class SubmitQueue : public QList<SubmitItem*>
128 public:
129 static bool compareItems( SubmitItem *sItem1, SubmitItem *sItem2 );
133 class ScrobblerSubmitter : public QObject
135 Q_OBJECT
137 public:
138 static QString PROTOCOL_VERSION;
139 static QString CLIENT_ID;
140 static QString CLIENT_VERSION;
141 static QString HANDSHAKE_URL;
143 ScrobblerSubmitter();
144 ~ScrobblerSubmitter();
146 void submitItem( SubmitItem* /* item */ );
148 void configure( const QString& /*username*/, const QString& /* password*/, bool /*enabled*/ );
150 void syncComplete();
152 private slots:
153 void scheduledTimeReached();
154 void audioScrobblerHandshakeResult( KIO::Job* /*job*/ );
155 void audioScrobblerSubmitResult( KIO::Job* /*job*/ );
156 void audioScrobblerSubmitData(
157 KIO::Job* /*job*/, const QByteArray& /*data*/ );
159 private:
160 bool canSubmit() const;
161 void enqueueItem( SubmitItem* /* item */ );
162 SubmitItem* dequeueItem();
163 void enqueueJob( KIO::Job* /* job */ );
164 void finishJob( KIO::Job* /* job */ );
165 void announceSubmit(
166 SubmitItem* /* item */, int /* tracks */, bool /* success */ ) const;
167 void saveSubmitQueue();
168 void readSubmitQueue();
169 bool schedule( bool failure );
170 void performHandshake();
171 void performSubmit();
173 // on failure, start at MIN_BACKOFF, and double on subsequent failures
174 // until MAX_BACKOFF is reached
175 static const int MIN_BACKOFF = 60;
176 static const int MAX_BACKOFF = 60 * 60;
178 QString m_submitResultBuffer;
179 QString m_username;
180 QString m_password;
181 QString m_submitUrl;
182 QString m_challenge;
183 QString m_savePath;
184 bool m_scrobblerEnabled;
185 bool m_holdFakeQueue;
186 bool m_inProgress;
187 bool m_needHandshake;
188 uint m_prevSubmitTime;
189 uint m_interval;
190 uint m_backoff;
191 uint m_lastSubmissionFinishTime;
192 uint m_fakeQueueLength;
194 //Q3PtrDict<SubmitItem> m_ongoingSubmits;
195 QHash<KIO::Job*, SubmitItem* > m_ongoingSubmits;
196 SubmitQueue m_submitQueue; // songs played by Amarok
197 SubmitQueue m_fakeQueue; // songs for which play times have to be faked (e.g. when submitting from media device)
199 QTimer m_timer;
203 #endif /* AMAROK_SCROBBLER_H */