Skip to content

Commit

Permalink
DownloadButton: Share click handling code between primitive and widge…
Browse files Browse the repository at this point in the history
…t mode
  • Loading branch information
dschmidt committed Apr 15, 2016
1 parent 4408ba2 commit 34448b1
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 17 deletions.
68 changes: 51 additions & 17 deletions src/libtomahawk/widgets/DownloadButton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ DownloadButton::addDownloadJob()
if ( result.isNull() )
return;

if ( handleClickPreDownload( m_query ) )
return;

if ( !result->downloadFormats().isEmpty() )
{
if ( m_view && m_index.isValid() )
Expand All @@ -124,6 +127,10 @@ DownloadButton::addDownloadJob()

DownloadManager::instance()->addJob( result->toDownloadJob( result->downloadFormats().at( currentIndex() ) ) );
}
else
{
handleClickPostDownload( m_query );
}
}


Expand Down Expand Up @@ -202,43 +209,70 @@ DownloadButton::handleEditorEvent(QEvent* event , QAbstractItemView* view, Playa
if ( event->type() == QEvent::MouseButtonRelease )
{
PlayableItem* item = model->sourceModel()->itemFromIndex( model->mapToSource( index ) );
if ( !item )
if ( !item && ! item->query() )
return false;

if ( item->query() && item->query()->numResults( true ) && !item->query()->results().first()->downloadFormats().isEmpty() )
if ( handleClickPreDownload( item->query() ) )
return true;

if( item->query()->numResults( true ) && !item->query()->results().first()->downloadFormats().isEmpty() )
{
model->sourceModel()->setAllColumnsEditable( true );
view->edit( index );
model->sourceModel()->setAllColumnsEditable( false );
return true;
}
else
{
WebPopup* popup = new WebPopup( item->query()->results().first()->purchaseUrl(), QSize( 400, 800 ) );
connect( item->query()->results().first().data(), SIGNAL( destroyed() ), popup, SLOT( close() ) );
}

return handleClickPostDownload( item->query() );
}

return false;
}


QWidget*
DownloadButton::handleCreateEditor( QWidget* parent, const query_ptr& query, QAbstractItemView* view, const QModelIndex& index )
bool
DownloadButton::handleClickPreDownload( const Tomahawk::query_ptr& query )
{
Tomahawk::result_ptr result = query->numResults( true ) ? query->results().first() : Tomahawk::result_ptr();
// view in folder
if ( !DownloadManager::instance()->localUrlForDownload( query ).isEmpty() )
{
QDesktopServices::openUrl( DownloadManager::instance()->localUrlForDownload( query ) );
return true;
}

if ( result && !result->downloadFormats().isEmpty() &&
!DownloadManager::instance()->localFileForDownload( result->downloadFormats().first().url.toString() ).isEmpty() )
// download in progress
Tomahawk::result_ptr result = query->numResults( true ) ? query->results().first() : Tomahawk::result_ptr();
if ( result && result->downloadJob() && result->downloadJob()->state() != DownloadJob::Finished )
{
QDesktopServices::openUrl( QUrl::fromLocalFile( QFileInfo( DownloadManager::instance()->localFileForDownload( result->downloadFormats().first().url.toString() ) ).absolutePath() ) );
// do nothing, handled
return true;
}
else if ( result && result->downloadJob() && result->downloadJob()->state() == DownloadJob::Finished )

return false;
}


bool
DownloadButton::handleClickPostDownload( const Tomahawk::query_ptr& query )
{
// handle buy click
Tomahawk::result_ptr result = query->numResults( true ) ? query->results().first() : Tomahawk::result_ptr();
if ( result && !result->purchaseUrl().isEmpty() )
{
QDesktopServices::openUrl( QUrl::fromLocalFile( QFileInfo( result->downloadJob()->localFile() ).absolutePath() ) );
WebPopup* popup = new WebPopup( result->purchaseUrl(), QSize( 400, 800 ) );
connect( result.data(), SIGNAL( destroyed() ), popup, SLOT( close() ) );
return true;
}
else if ( result &&
!result->downloadFormats().isEmpty() && !result->downloadJob() )

return false;
}


QWidget*
DownloadButton::handleCreateEditor( QWidget* parent, const query_ptr& query, QAbstractItemView* view, const QModelIndex& index )
{
Tomahawk::result_ptr result = query->numResults( true ) ? query->results().first() : Tomahawk::result_ptr();
if ( result && !result->downloadFormats().isEmpty() && !result->downloadJob() )
{
return new DownloadButton( query, parent, view, index );
}
Expand Down
2 changes: 2 additions & 0 deletions src/libtomahawk/widgets/DownloadButton.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ private slots:
void addDownloadJob();

private:
static bool handleClickPreDownload( const Tomahawk::query_ptr& query );
static bool handleClickPostDownload( const Tomahawk::query_ptr& query );
void init();

Tomahawk::query_ptr m_query;
Expand Down

0 comments on commit 34448b1

Please sign in to comment.