This repository has been archived by the owner on Mar 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 184
/
Copy pathscrobbler.h
160 lines (144 loc) · 4.9 KB
/
scrobbler.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
* Cantata
*
* Copyright (c) 2011-2022 Craig Drummond <[email protected]>
*
* This file contains code from QMPDClient:
* Copyright (C) 2005-2008 Håvard Tautra Knutsen <[email protected]>
* Copyright (C) 2009 Voker57 <[email protected]>
*
* ----
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifndef SCROBBLER_H
#define SCROBBLER_H
#include <QString>
#include <QStringList>
#include <QQueue>
#include <QObject>
#include <QUrl>
#include <QMap>
#include <time.h>
#include "mpd-interface/mpdstatus.h"
class QTimer;
class QNetworkReply;
class PausableTimer;
struct Song;
struct MPDStatusValues;
class Scrobbler : public QObject
{
Q_OBJECT
public:
struct Track {
Track() : track(0), length(0), timestamp(0) { }
Track(const Song &s);
bool operator==(const Track &o) const { return track==o.track && title==o.title && artist==o.artist &&
albumartist==o.albumartist && album==o.album; }
bool operator!=(const Track &o) const { return !(*this==o); }
void clear() { title=artist=albumartist=album=QString(); track=length=0; timestamp=0; }
bool isEmpty() { return 0==track && 0==length && 0==timestamp && title.isEmpty() && artist.isEmpty() && albumartist.isEmpty() && album.isEmpty(); }
QString title;
QString artist;
QString albumartist;
QString album;
quint32 track;
quint32 length;
time_t timestamp;
};
static Scrobbler * self();
static void enableDebug();
static const QLatin1String constCacheDir;
static const QLatin1String constCacheFile;
static bool viaMpd(const QString &sc) { return !sc.startsWith("http"); }
Scrobbler();
~Scrobbler() override;
QMap<QString, QString> availableScrobblers() { loadScrobblers(); return scrobblers; }
void stop();
bool isEnabled() const { return scrobblingEnabled; }
bool isLoveEnabled() const { return loveIsEnabled; }
bool lovedTrack() const { return lovePending || loveSent; }
bool haveLoginDetails() const { return !userName.isEmpty() && !password.isEmpty(); }
void setDetails(const QString &s, const QString &u, const QString &p);
const QString & user() const { return userName; }
const QString & pass() const { return password; }
const QString & activeScrobbler() const { return scrobbler; }
bool isAuthenticated() const { return !sessionKey.isEmpty(); }
Q_SIGNALS:
void error(const QString &msg);
void authenticated(bool a);
void enabled(bool e);
void loveEnabled(bool e);
void songChanged(bool valid);
void scrobblerChanged();
// send love via client message...
void clientMessage(const QString &client, const QString &msg, const QString &clientName);
public Q_SLOTS:
void love();
void setEnabled(bool e);
void setLoveEnabled(bool e);
private Q_SLOTS:
void setSong(const Song &s);
void scrobbleCurrent();
void scrobbleQueued();
void scrobbleNowPlaying();
void handleResp();
void authenticate();
void authResp();
void scrobbleFinished();
void mpdStateUpdated(bool songChanged=false);
void mpdStatusUpdated(const MPDStatusValues &vals);
void clientMessageFailed(const QString &client, const QString &msg);
private:
void setActive();
void loadSettings();
bool ensureAuthenticated();
void loadCache();
void saveCache();
void calcScrobbleIntervals();
void cancelJobs();
void reset();
void loadScrobblers();
QString scrobblerUrl();
private:
bool scrobblingEnabled;
bool loveIsEnabled;
QMap<QString, QString> scrobblers;
QString scrobbler;
QString userName;
QString password;
QString sessionKey;
QQueue<Track> songQueue;
QQueue<Track> lastScrobbledSongs;
Track inactiveSong; // Song set whilst inactive
Track currentSong;
PausableTimer * scrobbleTimer;
QTimer * retryTimer;
PausableTimer * nowPlayingTimer;
time_t lastNowPlaying;
QTimer * hardFailTimer;
bool nowPlayingIsPending;
bool lovePending;
bool nowPlayingSent;
bool loveSent;
bool scrobbledCurrent;
bool scrobbleViaMpd;
int failedCount;
MPDState lastState;
QNetworkReply *authJob;
QNetworkReply *scrobbleJob;
};
#endif