Skip to content

Commit

Permalink
Merge branch 'master' of git://github.com/jonocole/lastfm-desktop
Browse files Browse the repository at this point in the history
  • Loading branch information
RJ committed May 1, 2009
2 parents 245d3e0 + 513755f commit d94b904
Show file tree
Hide file tree
Showing 19 changed files with 662 additions and 46 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
_build
_files.qmake
_version.h
_build_parameters.pl.h
Makefile*
/_bin
/_include
8 changes: 3 additions & 5 deletions Last.fm.pro
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@ SUBDIRS = lib/lastfm/core/libcore.pro \
lib/lastfm/ws/libws.pro \
lib/lastfm/types/libtypes.pro \
lib/lastfm/radio/libradio.pro \
lib/lastfm/fingerprint/libfingerprint.pro \
lib/lastfm/scrobble/libscrobble.pro \
lib/unicorn/libunicorn.pro \
lib/listener/liblistener.pro \
app/clientplugins/localresolver/libresolver.pro \
app/client \
app/twiddly \
app/boffin
app/audioscrobbler \
app/radio \
app/twiddly

debug:win32 {
# make the client the default project in visual studio
Expand Down
129 changes: 129 additions & 0 deletions app/audioscrobbler/Application.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/***************************************************************************
* Copyright 2005-2009 Last.fm Ltd. *
* *
* 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; if not, write to the *
* Free Software Foundation, Inc., *
* 51 Franklin Steet, Fifth Floor, Boston, MA 02110-1301, USA. *
***************************************************************************/

#include "Application.h"
#include "StopWatch.h"
#include "lib/listener/legacy/LegacyPlayerListener.h"
#include "lib/listener/PlayerListener.h"
#include "lib/listener/PlayerMediator.h"
#include <lastfm/Scrobbler>
using audioscrobbler::Application;

Application::Application(int& argc, char** argv) : unicorn::Application(argc, argv)
{
tray = new QSystemTrayIcon(this);
connect(tray, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), SLOT(onTrayActivated(QSystemTrayIcon::ActivationReason)));
tray->show();

as = new Scrobbler("ass");

mediator = new PlayerMediator(this);
connect(mediator, SIGNAL(activeConnectionChanged( PlayerConnection* )), SLOT(setConnection( PlayerConnection* )) );
connect(new LegacyPlayerListener(mediator), SIGNAL(newConnection(PlayerConnection*)), mediator, SLOT(follow(PlayerConnection*)) );

try{
PlayerListener* listener = new PlayerListener(mediator);
connect(listener, SIGNAL(newConnection(PlayerConnection*)), mediator, SLOT(follow(PlayerConnection*)));
}
catch(std::runtime_error& e){
qWarning() << e.what();
//TODO user visible warning
}
}

void
Application::onTrayActivated(QSystemTrayIcon::ActivationReason)
{}

void
Application::setConnection(PlayerConnection*c)
{
if(connection){
disconnect(connection, 0, this, 0);
if(watch)
connection->setElapsed(watch->elapsed());
}
connect(c, SIGNAL(trackStarted(Track, Track)), SLOT(onTrackStarted(Track, Track)));
connect(c, SIGNAL(paused()), SLOT(onTrackPaused()));
connect(c, SIGNAL(resumed()), SLOT(onTrackResumed()));
connect(c, SIGNAL(resumed()), SLOT(onTrackStopped()));
connection = c;

if(c->state() == Playing)
onTrackStarted(c->track(), Track());
}

void
Application::onTrackStarted(const Track& t, const Track& oldtrack)
{
Q_ASSERT(connection);

//TODO move to playerconnection
if(t == oldtrack){
qWarning() << "Trying to start the same track as last time, assuming programmer error and doing nothing";
return;
}
if(t.isNull()){
qWarning() << "Can't start null track!";
return;
}

delete watch;
as->submit();
as->nowPlaying(t);

ScrobblePoint timeout(t.duration()/2);
watch = new StopWatch(timeout, connection->elapsed());
connect(watch, SIGNAL(timeout()), SLOT(onStopWatchTimedOut()));
}

void
Application::onStopWatchTimedOut()
{
Q_ASSERT(connection);
as->cache(connection->track());
}

void
Application::onPaused()
{
Q_ASSERT(connection);
Q_ASSERT(watch);
if(watch)watch->pause();
}

void
Application::onResumed()
{
Q_ASSERT(watch);
Q_ASSERT(connection);
Q_ASSERT(connection->state() == Paused);

if(watch)watch->resume();
}

void
Application::onStopped()
{
Q_ASSERT(watch);
Q_ASSERT(connection);

delete watch;
as->submit();
}
55 changes: 55 additions & 0 deletions app/audioscrobbler/Application.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/***************************************************************************
* Copyright 2005-2009 Last.fm Ltd. *
* *
* 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; if not, write to the *
* Free Software Foundation, Inc., *
* 51 Franklin Steet, Fifth Floor, Boston, MA 02110-1301, USA. *
***************************************************************************/

#include <lastfm/global.h>
#include "lib/unicorn/UnicornApplication.h"
#include <QPointer>
#include <QSystemTrayIcon>
class Scrobbler;
class PlayerMediator;
class PlayerConnection;
class StopWatch;


namespace audioscrobbler
{
class Application : public unicorn::Application
{
Q_OBJECT

QPointer<QSystemTrayIcon> tray;
QPointer<Scrobbler> as;
QPointer<PlayerMediator> mediator;
QPointer<PlayerConnection> connection;
QPointer<StopWatch> watch;

public:
Application(int& argc, char** argv);

private slots:
void onTrayActivated(QSystemTrayIcon::ActivationReason);
void onStopWatchTimedOut();
void setConnection(PlayerConnection*);

void onTrackStarted(const Track&, const Track&);
void onPaused();
void onResumed();
void onStopped();
};
}
6 changes: 6 additions & 0 deletions app/client/StopWatch.cpp → app/audioscrobbler/StopWatch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ StopWatch::StopWatch( ScrobblePoint timeout, uint elapsed ) : m_point( timeout )
connect( m_timer, SIGNAL(timeout()), SLOT(finished()) );
}

StopWatch::~StopWatch()
{
if (!isTimedOut() && (m_point*1000) - elapsed() < 4000)
emit timeout();
}


void
StopWatch::start() //private
Expand Down
6 changes: 3 additions & 3 deletions app/client/StopWatch.h → app/audioscrobbler/StopWatch.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#include <QDateTime>
#include <QObject>
#include <QTimer>

namespace audioscrobbler { class Application; }

namespace mxcl
{
Expand Down Expand Up @@ -54,22 +54,22 @@ class StopWatch : public QObject
Q_OBJECT
Q_DISABLE_COPY( StopWatch )

friend class StateMachine; //for access to timeout() signal
friend class audioscrobbler::Application; //for access to timeout() signal
friend class TestStopWatch; //for testing, duh!

public:
/** The StopWatch starts off paused, call resume() to start.
* The watch will not timeout() if elapsed is greater that the
* scrobble point */
StopWatch( ScrobblePoint timeout_in_seconds, uint elapsed_in_ms = 0 );
~StopWatch();

bool isTimedOut() const { return m_remaining == 0 && !m_timer->isActive(); }

void pause();
void resume();

/** in milliseconds */
uint remaining() const { return m_remaining; }
uint elapsed() const { return ((m_point*1000 - m_remaining) + m_elapsed.elapsed()); }

ScrobblePoint scrobblePoint() const { return m_point; }
Expand Down
9 changes: 9 additions & 0 deletions app/audioscrobbler/audioscrobbler.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
TEMPLATE = app
TARGET = audioscrobbler
CONFIG += unicorn scrobble listener
VERSION = 2.0.0

include( $$ROOT_DIR/admin/include.qmake )
include( _files.qmake )

DEFINES += LASTFM_COLLAPSE_NAMESPACE
51 changes: 51 additions & 0 deletions app/audioscrobbler/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/***************************************************************************
* Copyright 2005-2009 Last.fm Ltd. *
* *
* 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; if not, write to the *
* Free Software Foundation, Inc., *
* 51 Franklin Steet, Fifth Floor, Boston, MA 02110-1301, USA. *
***************************************************************************/

#include "_version.h"
#include "lib/unicorn/UniqueApplication.h"
#include "Application.h"


int main( int argc, char** argv )
{
QCoreApplication::setApplicationName( "Audioscrobbler" );
QCoreApplication::setApplicationVersion( VERSION );

#ifdef NDEBUG
UniqueApplication uapp( moose::id() );
if (uapp.isAlreadyRunning())
return uapp.forward( argc, argv ) ? 0 : 1;
uapp.init1();
#endif

try
{
audioscrobbler::Application app( argc, argv );
#ifdef NDEBUG
uapp.init2( &app );
app.connect( &uapp, SIGNAL(arguments( QStringList )), SLOT(parseArguments( QStringList )) );
#endif
return app.exec();
}
catch (unicorn::Application::StubbornUserException&)
{
// user wouldn't log in
return 0;
}
}
Loading

0 comments on commit d94b904

Please sign in to comment.