From eb9a89f9d88c3c07fb9481821ef7eeb1d195e3f5 Mon Sep 17 00:00:00 2001 From: "Uwe L. Korn" Date: Sat, 13 Jul 2013 18:01:12 +0200 Subject: [PATCH] Move "data mining/aggregation" into its own worker thread --- src/libtomahawk-widgets/CMakeLists.txt | 1 + .../NetworkActivityWidget.cpp | 32 ++----- .../NetworkActivityWidget.h | 5 +- .../NetworkActivityWidget_p.h | 4 + .../NetworkActivityWorker.cpp | 88 +++++++++++++++++++ .../NetworkActivityWorker.h | 57 ++++++++++++ .../NetworkActivityWorker_p.h | 45 ++++++++++ 7 files changed, 208 insertions(+), 24 deletions(-) create mode 100644 src/libtomahawk-widgets/NetworkActivityWorker.cpp create mode 100644 src/libtomahawk-widgets/NetworkActivityWorker.h create mode 100644 src/libtomahawk-widgets/NetworkActivityWorker_p.h diff --git a/src/libtomahawk-widgets/CMakeLists.txt b/src/libtomahawk-widgets/CMakeLists.txt index 6e2b5023a..fe03db118 100644 --- a/src/libtomahawk-widgets/CMakeLists.txt +++ b/src/libtomahawk-widgets/CMakeLists.txt @@ -8,6 +8,7 @@ list(APPEND ${TOMAHAWK_WIDGETS_LIBRARY_TARGET}_SOURCES # ViewPages Dashboard.cpp NetworkActivityWidget.cpp + NetworkActivityWorker.cpp SocialPlaylistWidget.cpp ) diff --git a/src/libtomahawk-widgets/NetworkActivityWidget.cpp b/src/libtomahawk-widgets/NetworkActivityWidget.cpp index 49e4d6ba8..6bd063f62 100644 --- a/src/libtomahawk-widgets/NetworkActivityWidget.cpp +++ b/src/libtomahawk-widgets/NetworkActivityWidget.cpp @@ -18,13 +18,11 @@ #include "NetworkActivityWidget_p.h" -#include "Pipeline.h" #include "audio/AudioEngine.h" #include "database/Database.h" #include "database/DatabaseCommand_NetworkCharts.h" #include "database/DatabaseCommand_TrendingTracks.h" #include "playlist/AlbumItemDelegate.h" -// #include "playlist/PlaylistChartItemDelegate.h" #include "playlist/ViewHeader.h" #include "utils/AnimatedSpinner.h" #include "utils/ImageRegistry.h" @@ -32,15 +30,13 @@ #include "utils/TomahawkStyle.h" #include "utils/TomahawkUtilsGui.h" #include "widgets/OverlayWidget.h" +#include "Pipeline.h" #include #include #include #include -#define NETWORKCHARTS_NUM_TRACKS 20 -#define TRENDING_TRACKS_NUM 3 - using namespace Tomahawk; using namespace Tomahawk::Widgets; @@ -174,15 +170,13 @@ NetworkActivityWidget::NetworkActivityWidget( QWidget* parent ) TomahawkUtils::unmarginLayout( layout ); } - { - // Load trending tracks - qRegisterMetaType< QList< QPair< double,Tomahawk::track_ptr > > >("QList< QPair< double,Tomahawk::track_ptr > >"); - DatabaseCommand_TrendingTracks* dbcmd = new DatabaseCommand_TrendingTracks(); - dbcmd->setLimit( TRENDING_TRACKS_NUM ); - connect( dbcmd, SIGNAL( done( QList< QPair< double,Tomahawk::track_ptr > >) ), - SLOT( trendingTracks( QList< QPair< double,Tomahawk::track_ptr > > ) ) ); - Database::instance()->enqueue( dbcmd_ptr( dbcmd ) ); - } + // Load data in separate thread + d->worker = new NetworkActivityWorker(); + d->worker->moveToThread( d->worker ); + connect( d->worker, SIGNAL( trendingTracks( QList ) ), + SLOT( trendingTracks( QList ) ), Qt::QueuedConnection); + connect( d->worker, SIGNAL( finished() ), d->worker, SLOT( deleteLater() ) ); + d->worker->start(); } @@ -287,19 +281,11 @@ NetworkActivityWidget::overallCharts( const QList& tracks ) void -NetworkActivityWidget::trendingTracks( const QList >& _tracks ) +NetworkActivityWidget::trendingTracks( const QList& tracks ) { Q_D( NetworkActivityWidget ); d->trendingTracksModel->startLoading(); - QList tracks; - QList< QPair< double, track_ptr > >::const_iterator iter = _tracks.constBegin(); - const QList< QPair< double, track_ptr > >::const_iterator end = _tracks.constEnd(); - for(; iter != end; ++iter) - { - tracks << iter->second; - } - d->trendingTracksModel->appendTracks( tracks ); d->trendingTracksModel->finishLoading(); } diff --git a/src/libtomahawk-widgets/NetworkActivityWidget.h b/src/libtomahawk-widgets/NetworkActivityWidget.h index a1f29194d..fa590c3f6 100644 --- a/src/libtomahawk-widgets/NetworkActivityWidget.h +++ b/src/libtomahawk-widgets/NetworkActivityWidget.h @@ -19,6 +19,9 @@ #ifndef NETWORKACTIVITYWIDGET_H #define NETWORKACTIVITYWIDGET_H +#define NETWORKCHARTS_NUM_TRACKS 20 +#define TRENDING_TRACKS_NUM 3 + #include "ViewPage.h" #include "WidgetsDllMacro.h" @@ -66,7 +69,7 @@ private slots: void yearlyCharts( const QList& ); void overallCharts( const QList& ); - void trendingTracks( const QList< QPair< double,Tomahawk::track_ptr > >& tracks ); + void trendingTracks( const QList< Tomahawk::track_ptr >& tracks ); void leftCrumbIndexChanged( const QModelIndex& ); diff --git a/src/libtomahawk-widgets/NetworkActivityWidget_p.h b/src/libtomahawk-widgets/NetworkActivityWidget_p.h index d11720dcb..01262efd0 100644 --- a/src/libtomahawk-widgets/NetworkActivityWidget_p.h +++ b/src/libtomahawk-widgets/NetworkActivityWidget_p.h @@ -22,6 +22,8 @@ #include "NetworkActivityWidget.h" #include "ui_NetworkActivityWidget.h" +#include "NetworkActivityWorker.h" + class NetworkActivityWidgetPrivate { public: @@ -31,6 +33,7 @@ public: , sortedProxy( 0 ) { } + Tomahawk::Widgets::NetworkActivityWidget* q_ptr; Q_DECLARE_PUBLIC ( Tomahawk::Widgets::NetworkActivityWidget ) @@ -48,6 +51,7 @@ private: QPointer trendingTracksModel; Tomahawk::Widgets::NetworkActivityWidget::ViewType activeView; + Tomahawk::NetworkActivityWorker* worker; }; #endif // NETWORKACTIVITYWIDGET_P_H diff --git a/src/libtomahawk-widgets/NetworkActivityWorker.cpp b/src/libtomahawk-widgets/NetworkActivityWorker.cpp new file mode 100644 index 000000000..c00e96208 --- /dev/null +++ b/src/libtomahawk-widgets/NetworkActivityWorker.cpp @@ -0,0 +1,88 @@ +/* === This file is part of Tomahawk Player - === + * + * Copyright 2013, Uwe L. Korn + * + * Tomahawk 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 3 of the License, or + * (at your option) any later version. + * + * Tomahawk 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 Tomahawk. If not, see . + */ + +#include "NetworkActivityWorker_p.h" + +#include "database/Database.h" +#include "database/DatabaseCommand_TrendingTracks.h" +#include "database/DatabaseImpl.h" +#include "utils/Logger.h" +#include "NetworkActivityWidget.h" + +namespace Tomahawk { + +NetworkActivityWorker::NetworkActivityWorker( QObject* parent ) + : QThread( parent ) + , d_ptr( new NetworkActivityWorkerPrivate( this ) ) +{ +} + + +NetworkActivityWorker::~NetworkActivityWorker() +{ +} + + +void +NetworkActivityWorker::run() +{ + tLog() << Q_FUNC_INFO; + { + // Load trending tracks + qRegisterMetaType< QList< QPair< double,Tomahawk::track_ptr > > >("QList< QPair< double,Tomahawk::track_ptr > >"); + DatabaseCommand_TrendingTracks* dbcmd = new DatabaseCommand_TrendingTracks(); + dbcmd->setLimit( TRENDING_TRACKS_NUM ); + connect( dbcmd, SIGNAL( done( QList< QPair< double,Tomahawk::track_ptr > >) ), + SLOT( trendingTracksReceived( QList< QPair< double,Tomahawk::track_ptr > > ) ), Qt::QueuedConnection ); + Database::instance()->enqueue( dbcmd_ptr( dbcmd ) ); + } + + // Start the event loop + exec(); +} + + +void +NetworkActivityWorker::trendingTracksReceived( const QList >& _tracks) +{ + Q_D( NetworkActivityWorker ); + d->trendingTracksDone = true; + + QList tracks; + QList< QPair< double, track_ptr > >::const_iterator iter = _tracks.constBegin(); + const QList< QPair< double, track_ptr > >::const_iterator end = _tracks.constEnd(); + for(; iter != end; ++iter) + { + tracks << iter->second; + } + emit trendingTracks( tracks ); + + checkDone(); +} + +void +NetworkActivityWorker::checkDone() +{ + Q_D( NetworkActivityWorker ); + if ( d->trendingTracksDone ) + { + quit(); + } +} + +} // namespace Tomahawk diff --git a/src/libtomahawk-widgets/NetworkActivityWorker.h b/src/libtomahawk-widgets/NetworkActivityWorker.h new file mode 100644 index 000000000..0f2470ff4 --- /dev/null +++ b/src/libtomahawk-widgets/NetworkActivityWorker.h @@ -0,0 +1,57 @@ +/* === This file is part of Tomahawk Player - === + * + * Copyright 2013, Uwe L. Korn + * + * Tomahawk 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 3 of the License, or + * (at your option) any later version. + * + * Tomahawk 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 Tomahawk. If not, see . + */ + +#pragma once +#ifndef TOMAHAWK_NETWORKACTIVITYWORKER_H +#define TOMAHAWK_NETWORKACTIVITYWORKER_H + +#include "Typedefs.h" + +#include + +namespace Tomahawk { + +class NetworkActivityWorkerPrivate; + +class NetworkActivityWorker : public QThread +{ + Q_OBJECT +public: + explicit NetworkActivityWorker( QObject *parent = 0 ); + virtual ~NetworkActivityWorker(); + + void run(); + +signals: + void trendingTracks( const QList& tracks ); + +protected: + QScopedPointer d_ptr; + +private slots: + void trendingTracksReceived( const QList< QPair< double,Tomahawk::track_ptr > >& tracks ); + +private: + Q_DECLARE_PRIVATE( NetworkActivityWorker ) + + void checkDone(); +}; + +} // namespace Tomahawk + +#endif // TOMAHAWK_NETWORKACTIVITYWORKER_H diff --git a/src/libtomahawk-widgets/NetworkActivityWorker_p.h b/src/libtomahawk-widgets/NetworkActivityWorker_p.h new file mode 100644 index 000000000..d33505fa5 --- /dev/null +++ b/src/libtomahawk-widgets/NetworkActivityWorker_p.h @@ -0,0 +1,45 @@ +/* === This file is part of Tomahawk Player - === + * + * Copyright 2013, Uwe L. Korn + * + * Tomahawk 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 3 of the License, or + * (at your option) any later version. + * + * Tomahawk 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 Tomahawk. If not, see . + */ + +#pragma once +#ifndef NETWORKACTIVITYWORKER_P_H +#define NETWORKACTIVITYWORKER_P_H + +#include "NetworkActivityWorker.h" + +namespace Tomahawk +{ + +class NetworkActivityWorkerPrivate +{ +public: + NetworkActivityWorkerPrivate( NetworkActivityWorker* q ) + : q_ptr( q ) + , trendingTracksDone( false ) + { + } + + NetworkActivityWorker* q_ptr; + Q_DECLARE_PUBLIC( NetworkActivityWorker ) +private: + bool trendingTracksDone; +}; + +} // Tomahawk + +#endif // NETWORKACTIVITYWORKER_P_H