Permalink
Browse files

Qt audioscrobbler app, no menu no icon, but it's there!

  • Loading branch information...
1 parent 0801487 commit 513755f16f8ae4fe1583a89e697106193e54c794 @mxcl mxcl committed Apr 23, 2009
View
@@ -7,6 +7,7 @@ SUBDIRS = lib/lastfm/core/libcore.pro \
lib/lastfm/scrobble/libscrobble.pro \
lib/unicorn/libunicorn.pro \
lib/listener/liblistener.pro \
+ app/audioscrobbler \
app/radio \
app/twiddly
@@ -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();
+}
@@ -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();
+ };
+}
@@ -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
@@ -24,7 +24,7 @@
#include <QDateTime>
#include <QObject>
#include <QTimer>
-
+namespace audioscrobbler { class Application; }
namespace mxcl
{
@@ -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; }
@@ -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
@@ -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;
+ }
+}

0 comments on commit 513755f

Please sign in to comment.