Commit 1fc69df5 authored by Michael Pyne's avatar Michael Pyne

scrobbler: Change Scrobbler owner to be JuK, not PlayerManager.

PlayerManager will always emit a track changed signal, which means the
Scrobbler will always have to receive it even if we're not scrobbling
(my last change to avoid making network requests when not using last.fm
broke this, it seems).

Instead move the Scrobbler into the base application class where we can
track whether scrobbling is enabled or not, and Do the Right Thing. I
don't have a last.fm account so although this should still work (I
think), I can't verify. I have verified that we at least don't crash
anymore. Martin, can you double-check when you get a chance?

FIXED-IN:4.11.2
BUG:323703
parent 5cddfbdf
......@@ -50,6 +50,7 @@
#include "keydialog.h"
#include "tagguesserconfigdlg.h"
#include "filerenamerconfigdlg.h"
#include "scrobbler.h"
#include "scrobbleconfigdlg.h"
#include "actioncollection.h"
#include "cache.h"
......@@ -79,6 +80,7 @@ JuK::JuK(QWidget *parent) :
m_statusLabel(0),
m_systemTray(0),
m_player(new PlayerManager),
m_scrobbler(0),
m_shuttingDown(false)
{
// Expect segfaults if you change this order.
......@@ -97,7 +99,6 @@ JuK::JuK(QWidget *parent) :
setupActions();
setupLayout();
bool firstRun = !KGlobal::config()->hasGroup("MainWindow");
if(firstRun) {
......@@ -124,6 +125,7 @@ JuK::JuK(QWidget *parent) :
connect(m_splitter, SIGNAL(guiReady()), SLOT(slotSetupSystemTray()));
readConfig();
setupGlobalAccels();
activateScrobblerIfEnabled();
connect(QCoreApplication::instance(), SIGNAL(aboutToQuit()), SLOT(slotAboutToQuit()));
......@@ -321,9 +323,9 @@ void JuK::setupActions()
act = collection->addAction("fileRenamerConfig", this, SLOT(slotConfigureFileRenamer()));
act->setText(i18n("&File Renamer..."));
act = collection->addAction("scrobblerConfig", this, SLOT(slotConfigureScrobbling()));
act->setText(i18n("&Configure scrobbling..."));
act->setText(i18n("&Configure scrobbling..."));
//////////////////////////////////////////////////
// just in the toolbar
......@@ -606,8 +608,23 @@ void JuK::slotConfigureFileRenamer()
void JuK::slotConfigureScrobbling()
{
ScrobbleConfigDlg(this).exec();
activateScrobblerIfEnabled();
}
void JuK::activateScrobblerIfEnabled()
{
bool isScrobbling = Scrobbler::isScrobblingEnabled();
if (!m_scrobbler && isScrobbling) {
m_scrobbler = new Scrobbler(this);
connect (m_player, SIGNAL(signalItemChanged(FileHandle)),
m_scrobbler, SLOT(nowPlaying(FileHandle)));
}
else if (m_scrobbler && !isScrobbling) {
delete m_scrobbler;
m_scrobbler = 0;
}
}
void JuK::slotUndo()
{
......
......@@ -30,6 +30,7 @@ class StatusLabel;
class SystemTray;
class PlayerManager;
class PlaylistSplitter;
class Scrobbler;
class JuK : public KXmlGuiWindow
{
......@@ -53,6 +54,8 @@ private:
void keyPressEvent(QKeyEvent *);
void activateScrobblerIfEnabled();
/**
* readSettings() is separate from readConfig() in that it contains settings
* that need to be read before the GUI is setup.
......@@ -91,6 +94,7 @@ private:
KToggleAction *m_toggleSplashAction;
PlayerManager *m_player;
Scrobbler *m_scrobbler;
bool m_startDocked;
bool m_showSplash;
......
......@@ -71,10 +71,6 @@ PlayerManager::PlayerManager() :
// later, just disable it here. -- mpyne
// setup();
new PlayerAdaptor( this );
m_scrobbler = new Scrobbler(this);
connect(this, SIGNAL(signalItemChanged(FileHandle)), m_scrobbler, SLOT(nowPlaying(FileHandle)));
}
PlayerManager::~PlayerManager()
......@@ -432,7 +428,6 @@ bool PlayerManager::mute()
void PlayerManager::slotNeedNextUrl()
{
m_scrobbler->scrobble();
if(m_file.isNull() || !m_crossfadeTracks)
return;
......@@ -537,7 +532,7 @@ void PlayerManager::slotStateChanged(Phonon::State newstate, Phonon::State oldst
QTimer::singleShot(2000, this, SLOT(slotUpdateGuiIfStopped()));
JuK::JuKInstance()->setWindowTitle(i18n("JuK"));
emit signalStop();
}
else if(newstate == Phonon::PlayingState) {
......
......@@ -29,7 +29,6 @@
#include <Phonon/Global>
#include <Phonon/Path>
class Scrobbler;
class KSelectAction;
class StatusLabel;
class PlaylistInterface;
......@@ -143,7 +142,6 @@ private:
FileHandle m_file;
PlaylistInterface *m_playlistInterface;
StatusLabel *m_statusLabel;
Scrobbler *m_scrobbler;
bool m_muted;
bool m_setup;
bool m_crossfadeTracks;
......
......@@ -33,7 +33,6 @@
Scrobbler::Scrobbler(QObject* parent)
: QObject(parent)
, m_startedPlaying(0)
, m_networkAccessManager(0)
{
KConfigGroup config(KGlobal::config(), "Scrobbling");
......@@ -49,6 +48,17 @@ Scrobbler::~Scrobbler()
{
}
bool Scrobbler::isScrobblingEnabled()
{
KConfigGroup config(KGlobal::config(), "Scrobbling");
// TODO: use kwallet
QString username = config.readEntry("Username", "");
QString password = config.readEntry("Password", "");
return (!username.isEmpty() && !password.isEmpty());
}
QByteArray Scrobbler::md5(QByteArray data)
{
return QCryptographicHash::hash(data, QCryptographicHash::Md5)
......@@ -153,6 +163,10 @@ void Scrobbler::nowPlaying(const FileHandle& file)
return;
}
if (!m_file.isNull()) {
scrobble(); // Update time-played info for last track
}
QMap<QString, QString> params;
params["method"] = "track.updateNowPlaying";
params["sk"] = sessionKey;
......@@ -165,8 +179,8 @@ void Scrobbler::nowPlaying(const FileHandle& file)
sign(params);
post(params);
m_file = file;
m_startedPlaying = QDateTime::currentMSecsSinceEpoch() / 1000;
m_file = file; // May be FileHandle::null()
m_playbackTimer = QDateTime::currentDateTime();
}
void Scrobbler::scrobble()
......@@ -181,6 +195,13 @@ void Scrobbler::scrobble()
return;
}
int halfDuration = m_file.tag()->seconds() / 2;
int timeElapsed = m_playbackTimer.secsTo(QDateTime::currentDateTime());
if (timeElapsed < 30 || timeElapsed < halfDuration) {
return; // API says not to scrobble if the user didn't play long enough
}
kDebug() << "Scrobbling" << m_file.tag()->title();
QMap<QString, QString> params;
......@@ -189,7 +210,7 @@ void Scrobbler::scrobble()
params["track"] = m_file.tag()->title();
params["artist"] = m_file.tag()->artist();
params["album"] = m_file.tag()->album();
params["timestamp"] = QString::number(m_startedPlaying);
params["timestamp"] = QString::number(m_playbackTimer.toTime_t());
params["trackNumber"] = QString::number(m_file.tag()->track());
params["duration"] = QString::number(m_file.tag()->seconds());
......
......@@ -18,6 +18,7 @@
#include <QObject>
#include <QMap>
#include <QDateTime>
#include "filehandle.h"
......@@ -33,6 +34,8 @@ public:
explicit Scrobbler(QObject* parent = 0);
virtual ~Scrobbler();
static bool isScrobblingEnabled();
public slots:
void nowPlaying(const FileHandle&);
void scrobble();
......@@ -52,7 +55,7 @@ private:
void post(QMap<QString, QString> &request);
QByteArray md5(QByteArray data);
qint64 m_startedPlaying;
QDateTime m_playbackTimer;
FileHandle m_file;
QNetworkAccessManager *m_networkAccessManager;
};
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment