1 /***************************************************************************
2  *   Copyright (C) 2010-2016 by Ilya Kotov                                 *
3  *   forkotov02@ya.ru                                                      *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) any later version.                                   *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
19  ***************************************************************************/
20 #ifndef SCROBBLER_H
21 #define SCROBBLER_H
22 
23 #include <QMap>
24 #include <QObject>
25 #include <qmmp/qmmp.h>
26 #include "scrobblercache.h"
27 
28 class QNetworkAccessManager;
29 class QNetworkReply;
30 class QIODevice;
31 class QElapsedTimer;
32 class SoundCore;
33 
34 
35 /**
36     @author Ilya Kotov <forkotov02@ya.ru>
37 */
38 class ScrobblerResponse
39 {
40 public:
41     QString status;
42     QString token;
43     QString code;
44     QString error;
45     QString key;
46     QString name;
47     QString subscriber;
48 
49     void parse(QIODevice *device);
50 };
51 
52 /**
53     @author Ilya Kotov <forkotov02@ya.ru>
54 */
55 class Scrobbler : public QObject
56 {
57     Q_OBJECT
58 public:
59     Scrobbler(const QString &scrobblerUrl, const QString &name,QObject *parent = nullptr);
60     ~Scrobbler();
61 
62 private slots:
63     void setState(Qmmp::State state);
64     void updateMetaData();
65     void processResponse(QNetworkReply *reply);
66     void setupProxy();
67     void submit();
68 
69 private:
70     enum { MIN_SONG_LENGTH = 30000 };
71 
72     void sendNotification(const SongInfo &info);
73     SongInfo m_song;
74     QList <SongInfo> m_cachedSongs;
75     QByteArray m_ua;
76     int m_submitedSongs = 0;
77     QString m_session;
78     QNetworkAccessManager *m_http;
79     SoundCore *m_core;
80     QNetworkReply *m_submitReply = nullptr, *m_notificationReply = nullptr;
81     QElapsedTimer *m_time;
82     ListenCache *m_cache;
83     QString m_scrobblerUrl, m_name;
84     Qmmp::State m_previousState = Qmmp::Stopped;
85     qint64 m_elapsed = 0;
86 };
87 
88 /**
89     @author Ilya Kotov <forkotov02@ya.ru>
90 */
91 class ScrobblerAuth : public QObject
92 {
93     Q_OBJECT
94 public:
95     explicit ScrobblerAuth(const QString &scrobblerUrl, const QString &authUrl,
96                            const QString &name, QObject *parent = nullptr);
97     void getToken();
98     void getSession();
99     void checkSession(const QString &session);
100     QString session() const;
101 
102     enum ErrorType
103     {
104         NO_ERROR = 0,
105         NETWORK_ERROR,
106         LASTFM_ERROR
107     };
108 
109 signals:
110     void tokenRequestFinished(int error);
111     void sessionRequestFinished(int error);
112     void checkSessionFinished(int error);
113 
114 private slots:
115     void processResponse(QNetworkReply *reply);
116 
117 private:
118     QString m_token, m_session;
119     QByteArray m_ua;
120     QNetworkAccessManager *m_http;
121     QNetworkReply *m_getTokenReply = nullptr, *m_getSessionReply = nullptr, *m_checkSessionReply = nullptr;
122     QString m_scrobblerUrl, m_authUrl, m_name;
123 };
124 
125 #endif //SCROBBLER_H
126