1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-03-18 23:09:42 +01:00

Streamline link generation api and make it async

getShortUrl or rather its result "shorturl" was only ever used in
XmppInfo and even there was a fallback for creating the shorturl.
As I could not trigger getShortUrl in the InfoSystem and as it was
barely used, I removed it to reduce complexity.
This commit is contained in:
Dominik Schmidt 2014-11-30 00:24:38 +01:00 committed by Dominik Schmidt
parent 9542aaffb4
commit 33d225233b
16 changed files with 261 additions and 153 deletions

View File

@ -118,13 +118,17 @@ Tomahawk::InfoSystem::XmppInfoPlugin::audioStarted( const Tomahawk::InfoSystem::
Tomahawk::InfoSystem::InfoStringHash info = map[ "trackinfo" ].value< Tomahawk::InfoSystem::InfoStringHash >();
QUrl url;
if ( pushInfoPair.first.contains( "shorturl" ) )
url = pushInfoPair.first[ "shorturl" ].toUrl();
else
url = Utils::LinkGenerator::instance()->openLink( info.value( "title" ), info.value( "artist" ), info.value( "album" ) );
ScriptJob* job = Utils::LinkGenerator::instance()->openLink( info.value( "title" ), info.value( "artist" ), info.value( "album" ) );
connect( job, SIGNAL( done( QVariantMap ) ), SLOT( onQueryLinkReady( QVariantMap ) ) );
job->setProperty( "infoStringHash", QVariant::fromValue( info ) );
job->start();
}
emit publishTune( url, info );
void
Tomahawk::InfoSystem::XmppInfoPlugin::onQueryLinkReady( const QVariantMap& data )
{
emit publishTune( data[ "url" ].toUrl(), sender()->property("infoStringHash").value< Tomahawk::InfoSystem::InfoStringHash >() );
}

View File

@ -53,6 +53,7 @@ namespace Tomahawk {
void audioStarted( const Tomahawk::InfoSystem::PushInfoPair& pushInfoPair );
void audioStopped();
void audioPaused();
void onQueryLinkReady( const QVariantMap& data );
private:
QPointer< XmppSipPlugin > m_sipPlugin;

View File

@ -90,6 +90,7 @@ set( libGuiSources
resolvers/JSResolverHelper.cpp
resolvers/ScriptEngine.cpp
resolvers/JSPlugin.cpp
resolvers/ScriptJob.cpp
utils/DpiScaler.cpp
utils/ImageRegistry.cpp

View File

@ -421,7 +421,7 @@ ContextMenu::copyLink()
{
if ( m_queries.count() )
{
Utils::LinkGenerator::instance()->copyToClipboard( m_queries.first() );
Utils::LinkGenerator::instance()->copyOpenLink( m_queries.first() );
}
else if ( m_albums.count() )
{

View File

@ -291,19 +291,7 @@ InfoSystemWorker::getInfo( Tomahawk::InfoSystem::InfoRequestData requestData )
void
InfoSystemWorker::pushInfo( Tomahawk::InfoSystem::InfoPushData pushData )
{
tDebug( LOGVERBOSE ) << Q_FUNC_INFO << "type is " << pushData.type;
if ( pushData.pushFlags != PushNoFlag )
{
if ( pushData.pushFlags & PushShortUrlFlag )
{
pushData.pushFlags = Tomahawk::InfoSystem::PushInfoFlags( pushData.pushFlags & ~PushShortUrlFlag );
QMetaObject::invokeMethod( this, "getShortUrl", Qt::QueuedConnection, Q_ARG( Tomahawk::InfoSystem::InfoPushData, pushData ) );
return;
}
}
tDebug( LOGVERBOSE ) << Q_FUNC_INFO << "number of matching plugins: " << m_infoPushMap[ pushData.type ].size();
tDebug( LOGVERBOSE ) << Q_FUNC_INFO << "type is " << pushData.type << "number of matching plugins: " << m_infoPushMap[ pushData.type ].size();
Q_FOREACH( InfoPluginPtr ptr, m_infoPushMap[ pushData.type ] )
{
@ -313,72 +301,6 @@ InfoSystemWorker::pushInfo( Tomahawk::InfoSystem::InfoPushData pushData )
}
void
InfoSystemWorker::getShortUrl( Tomahawk::InfoSystem::InfoPushData pushData )
{
tDebug( LOGVERBOSE ) << Q_FUNC_INFO << "type is " << pushData.type;
if ( !pushData.infoPair.second.canConvert< Tomahawk::InfoSystem::InfoStringHash >() )
{
QMetaObject::invokeMethod( this, "pushInfo", Qt::QueuedConnection, Q_ARG( Tomahawk::InfoSystem::InfoPushData, pushData ) );
return;
}
Tomahawk::InfoSystem::InfoStringHash hash = pushData.infoPair.second.value< Tomahawk::InfoSystem::InfoStringHash >();
if ( hash.isEmpty() || !hash.contains( "title" ) || !hash.contains( "artist" ) )
{
QMetaObject::invokeMethod( this, "pushInfo", Qt::QueuedConnection, Q_ARG( Tomahawk::InfoSystem::InfoPushData, pushData ) );
return;
}
QString title, artist, album;
title = hash[ "title" ];
artist = hash[ "artist" ];
if( hash.contains( "album" ) )
album = hash[ "album" ];
QUrl longUrl = Utils::LinkGenerator::instance()->openLink( title, artist, album );
Tomahawk::Utils::ShortLinkHelper* slh = new Tomahawk::Utils::ShortLinkHelper();
connect( slh, SIGNAL( shortLinkReady( QUrl, QUrl, QVariant ) ),
SLOT( shortLinkReady( QUrl, QUrl, QVariant ) ) );
connect( slh, SIGNAL( done() ),
slh, SLOT( deleteLater() ),
Qt::QueuedConnection );
slh->shortenLink( longUrl, QVariant::fromValue< Tomahawk::InfoSystem::InfoPushData >( pushData ) );
m_shortLinksWaiting++;
}
void
InfoSystemWorker::shortLinkReady( QUrl longUrl, QUrl shortUrl, QVariant callbackObj )
{
tDebug( LOGVERBOSE ) << Q_FUNC_INFO << "long url = " << longUrl << ", shortUrl = " << shortUrl;
m_shortLinksWaiting--;
if ( !m_shortLinksWaiting )
disconnect( Utils::LinkGenerator::instance(), SIGNAL( shortLinkReady( QUrl, QUrl, QVariant ) ) );
if ( !callbackObj.isValid() )
{
tDebug() << Q_FUNC_INFO << "callback object was not valid, cannot continue";
return;
}
Tomahawk::InfoSystem::InfoPushData pushData = callbackObj.value< Tomahawk::InfoSystem::InfoPushData >();
if ( !shortUrl.isEmpty() && longUrl != shortUrl )
{
QVariantMap flagProps = pushData.infoPair.first;
flagProps[ "shorturl" ] = shortUrl;
pushData.infoPair.first = flagProps;
}
tDebug( LOGVERBOSE ) << Q_FUNC_INFO << "pushInfoPair first is: " << pushData.infoPair.first.keys();
QMetaObject::invokeMethod( this, "pushInfo", Qt::QueuedConnection, Q_ARG( Tomahawk::InfoSystem::InfoPushData, pushData ) );
}
void
InfoSystemWorker::infoSlot( Tomahawk::InfoSystem::InfoRequestData requestData, QVariant output )
{

View File

@ -69,9 +69,6 @@ public slots:
void addInfoPlugin( Tomahawk::InfoSystem::InfoPluginPtr plugin );
void removeInfoPlugin( Tomahawk::InfoSystem::InfoPluginPtr plugin );
void loadInfoPlugins();
void getShortUrl( Tomahawk::InfoSystem::InfoPushData data );
void shortLinkReady( QUrl longUrl, QUrl shortUrl, QVariant callbackObj );
private slots:
void checkTimeoutsTimerFired();

View File

@ -26,4 +26,4 @@ void
JSPlugin::addToJavaScriptWindowObject( const QString& name, QObject* object )
{
m_engine->mainFrame()->addToJavaScriptWindowObject( name, object );
};
}

View File

@ -0,0 +1,55 @@
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
*
* Copyright 2014, Dominik Schmidt <domme@tomahawk-player.org>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#include "ScriptJob.h"
#include <QMetaObject>
using namespace Tomahawk;
ScriptJob::ScriptJob( ScriptObject* scriptObject, const QString& method, const QVariantMap& parameters )
: QObject()
{
}
ScriptJob::~ScriptJob()
{
}
void
ScriptJob::start()
{
}
void
ScriptJob::reportResults( const QVariantMap& data )
{
m_data = data;
emit done( data );
}
void
ScriptJob::reportFailure()
{
}

View File

@ -0,0 +1,60 @@
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
*
* Copyright 2010-2011, Christian Muehlhaeuser <muesli@tomahawk-player.org>
* Copyright 2010-2012, Jeff Mitchell <jeff@tomahawk-player.org>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#ifndef TOMAHAWK_SCRIPTJOB_H
#define TOMAHAWK_SCRIPTJOB_H
#include <QVariantMap>
#include <QObject>
#include "DllMacro.h"
namespace Tomahawk
{
class ScriptObject;
class DLLEXPORT ScriptJob : public QObject
{
friend class ScriptObject;
Q_OBJECT
public:
ScriptJob( ScriptObject* scriptObject, const QString& method, const QVariantMap& parameters = QVariantMap() );
virtual ~ScriptJob();
virtual void start();
protected slots:
void reportResults( const QVariantMap& data );
void reportFailure();
signals:
void done( const QVariantMap& result );
void failed( const QVariantMap& reason );
protected:
// TODO: pimple
QVariantMap m_data;
};
}
#endif // TOMAHAWK_SCRIPTJOB_H

View File

@ -27,21 +27,42 @@
#include "Logger.h"
#include "ShortLinkHelper.h"
#include "playlist/dynamic/GeneratorInterface.h"
#include "playlist/dynamic/DynamicPlaylist.h"
#include "../playlist/dynamic/GeneratorInterface.h"
#include "../playlist/dynamic/DynamicPlaylist.h"
#include "../Track.h"
#include "../Artist.h"
#include "../Album.h"
#include "../resolvers/ScriptJob.h"
#include <echonest/Playlist.h>
#include <QClipboard>
#include <QApplication>
#include <QMetaObject>
using namespace Tomahawk;
using namespace Tomahawk::Utils;
// HACK: to prepare proper scripting
class SyncScriptJob : public ScriptJob
{
Q_OBJECT
public:
SyncScriptJob( const QVariantMap& resultData ) : ScriptJob( nullptr, "nomethod")
{
m_data = resultData;
}
void start() override
{
QMetaObject::invokeMethod( this, "reportResults", Qt::QueuedConnection, Q_ARG( QVariantMap, m_data ) );
}
};
#include "LinkGenerator.moc"
LinkGenerator* LinkGenerator::s_instance = 0;
@ -73,8 +94,8 @@ LinkGenerator::hostname() const
}
QUrl
LinkGenerator::openLinkFromQuery( const query_ptr& query ) const
ScriptJob*
LinkGenerator::openLink( const query_ptr& query ) const
{
QString title = query->track()->track();
QString artist = query->track()->artist();
@ -84,34 +105,7 @@ LinkGenerator::openLinkFromQuery( const query_ptr& query ) const
}
QUrl
LinkGenerator::copyOpenLink( const artist_ptr& artist ) const
{
const QUrl link( QString( "%1/artist/%2" ).arg( hostname() ).arg( artist->name() ) );
QClipboard* cb = QApplication::clipboard();
QByteArray data = TomahawkUtils::percentEncode( link );
cb->setText( data );
return link;
}
QUrl
LinkGenerator::copyOpenLink( const album_ptr& album ) const
{
const QUrl link = QUrl::fromUserInput( QString( "%1/album/%2/%3" ).arg( hostname() ).arg( album->artist().isNull() ? QString() : album->artist()->name() ).arg( album->name() ) );
QClipboard* cb = QApplication::clipboard();
QByteArray data = TomahawkUtils::percentEncode( link );
cb->setText( data );
return link;
}
QUrl
ScriptJob*
LinkGenerator::openLink( const QString& title, const QString& artist, const QString& album ) const
{
QUrl link( QString( "%1/open/track/" ).arg( hostname() ) );
@ -123,19 +117,22 @@ LinkGenerator::openLink( const QString& title, const QString& artist, const QStr
if ( !album.isEmpty() )
TomahawkUtils::urlAddQueryItem( link, "album", album );
return link;
QVariantMap data;
data[ "url" ] = link;
return new SyncScriptJob( data );
}
QString
LinkGenerator::copyPlaylistToClipboard( const dynplaylist_ptr& playlist )
ScriptJob*
LinkGenerator::openLink( const Tomahawk::dynplaylist_ptr& playlist ) const
{
QUrl link( QString( "%1/%2/create/" ).arg( hostname() ).arg( playlist->mode() == OnDemand ? "station" : "autoplaylist" ) );
if ( playlist->generator()->type() != "echonest" )
{
tLog() << "Only echonest generators are supported";
return QString();
return nullptr;
}
TomahawkUtils::urlAddQueryItem( link, "type", "echonest" );
@ -170,18 +167,46 @@ LinkGenerator::copyPlaylistToClipboard( const dynplaylist_ptr& playlist )
}
}
QClipboard* cb = QApplication::clipboard();
QByteArray data = TomahawkUtils::percentEncode( link );
cb->setText( data );
QVariantMap data;
data[ "url" ] = link;
return link.toString();
return new SyncScriptJob( data );
}
ScriptJob*
LinkGenerator::openLink( const Tomahawk::artist_ptr& artist ) const
{
QVariantMap data;
data[ "url" ] = QString( "%1/artist/%2" ).arg( hostname() ).arg( artist->name() );
return new SyncScriptJob( data );
}
ScriptJob*
LinkGenerator::openLink( const Tomahawk::album_ptr& album ) const
{
QVariantMap data;
data[ "url" ] = QUrl::fromUserInput( QString( "%1/album/%2/%3" ).arg( hostname() ).arg( album->artist().isNull() ? QString() : album->artist()->name() ).arg( album->name() ) );;
return new SyncScriptJob( data );
}
void
LinkGenerator::copyToClipboard( const query_ptr& query )
LinkGenerator::copyScriptJobResultToClipboard( const QVariantMap& data )
{
m_clipboardLongUrl = openLinkFromQuery( query );
m_clipboardLongUrl = data[ "url" ].toUrl();
copyToClipboardReady( m_clipboardLongUrl, m_clipboardLongUrl );
sender()->deleteLater();
}
void
LinkGenerator::copyScriptJobResultToClipboardShortened( const QVariantMap& data )
{
m_clipboardLongUrl = data[ "url" ].toUrl();
Tomahawk::Utils::ShortLinkHelper* slh = new Tomahawk::Utils::ShortLinkHelper();
connect( slh, SIGNAL( shortLinkReady( QUrl, QUrl, QVariant ) ),
SLOT( copyToClipboardReady( QUrl, QUrl, QVariant ) ) );
@ -189,6 +214,8 @@ LinkGenerator::copyToClipboard( const query_ptr& query )
slh, SLOT( deleteLater() ),
Qt::QueuedConnection );
slh->shortenLink( m_clipboardLongUrl );
sender()->deleteLater();
}

View File

@ -21,13 +21,14 @@
#ifndef TOMAHAWK_UTILS_LINKGENERATOR_H
#define TOMAHAWK_UTILS_LINKGENERATOR_H
#include "DllMacro.h"
#include "Typedefs.h"
#include "../resolvers/ScriptJob.h"
#include "../DllMacro.h"
#include "../Typedefs.h"
namespace Tomahawk {
namespace Utils {
class ShortLinkHelperPrivate;
namespace Utils {
class DLLEXPORT LinkGenerator : public QObject
{
@ -36,22 +37,34 @@ public:
static LinkGenerator* instance();
virtual ~LinkGenerator();
QUrl openLinkFromQuery( const Tomahawk::query_ptr& query ) const;
ScriptJob* openLink( const QString& title, const QString& artist, const QString& album ) const;
ScriptJob* openLink( const Tomahawk::query_ptr& query ) const;
ScriptJob* openLink( const Tomahawk::artist_ptr& artist ) const;
ScriptJob* openLink( const Tomahawk::album_ptr& album ) const;
ScriptJob* openLink( const Tomahawk::dynplaylist_ptr& playlist ) const;
QUrl copyOpenLink( const Tomahawk::artist_ptr& artist ) const;
QUrl copyOpenLink( const Tomahawk::album_ptr& album ) const;
// Fire and forget
QUrl openLink( const QString& title, const QString& artist, const QString& album ) const;
// the query link is shortened automatically
void copyOpenLink( const query_ptr& query ) const
{
ScriptJob* job = openLink( query );
connect( job, SIGNAL( done( QVariantMap ) ), SLOT( copyScriptJobResultToClipboardShortened( QVariantMap ) ), Qt::QueuedConnection );
job->start();
}
QString copyPlaylistToClipboard( const Tomahawk::dynplaylist_ptr& playlist );
public slots:
/// Creates a link from the requested data and copies it to the clipboard
void copyToClipboard( const Tomahawk::query_ptr& query );
// all others are not
template <typename T> void copyOpenLink( const T& item ) const
{
ScriptJob* job = openLink( item );
connect( job, SIGNAL( done( QVariantMap ) ), SLOT( copyScriptJobResultToClipboard( QVariantMap ) ), Qt::QueuedConnection );
job->start();
}
private slots:
void copyToClipboardReady( const QUrl& longUrl, const QUrl& shortUrl, const QVariant& callbackObj );
void copyToClipboardReady( const QUrl& longUrl, const QUrl& shortUrl, const QVariant& callbackObj = QVariant() );
void copyScriptJobResultToClipboard( const QVariantMap& data );
void copyScriptJobResultToClipboardShortened( const QVariantMap& data );
private:
explicit LinkGenerator( QObject* parent = 0 );

View File

@ -45,6 +45,7 @@
#include "utils/TomahawkUtilsGui.h"
#include "widgets/SourceTreePopupDialog.h"
#include "PlaylistEntry.h"
#include "resolvers/ScriptJob.h"
#include "../../viewpages/dashboard/Dashboard.h"
#include "../../viewpages/whatsnew_0_8/WhatsNew_0_8.h"
@ -503,7 +504,7 @@ SourceTreeView::copyPlaylistLink()
{
DynamicPlaylistItem* item = itemFromIndex< DynamicPlaylistItem >( m_contextMenuIndex );
dynplaylist_ptr playlist = item->dynPlaylist();
Utils::LinkGenerator::instance()->copyPlaylistToClipboard( playlist );
Utils::LinkGenerator::instance()->copyOpenLink( playlist );
}
else if ( type == SourcesModel::StaticPlaylist )
{
@ -563,8 +564,14 @@ SourceTreeView::addToLocal()
// copy to a link and then generate a new playlist from that
// this way we cheaply regenerate the needed controls
QString link = Utils::LinkGenerator::instance()->copyPlaylistToClipboard( playlist );
GlobalActionManager::instance()->parseTomahawkLink( link );
ScriptJob* job = Utils::LinkGenerator::instance()->openLink( playlist );
if( !job )
{
// No supported generator
return;
}
connect( job, SIGNAL( done( QVariantMap ) ), SLOT( onPlaylistLinkReady( QVariantMap ) ) );
job->start();
}
else if ( type == SourcesModel::StaticPlaylist )
{
@ -581,6 +588,15 @@ SourceTreeView::addToLocal()
}
void
SourceTreeView::onPlaylistLinkReady(const QVariantMap& data)
{
GlobalActionManager::instance()->parseTomahawkLink( data[ "url" ].toString() );
sender()->deleteLater();
}
void
SourceTreeView::latchOnOrCatchUp()
{

View File

@ -89,6 +89,7 @@ private slots:
void onDeletePlaylistResult( bool result );
void shortLinkReady( const Tomahawk::playlist_ptr& playlist, const QUrl& shortUrl );
void onPlaylistLinkReady( const QVariantMap& data );
protected:
void drawRow( QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index ) const;

View File

@ -155,7 +155,7 @@ TemporaryPageItem::linkActionTriggered( QAction* action )
{
TrackInfoWidget* tPage = dynamic_cast< TrackInfoWidget* >( m_page );
Q_ASSERT( tPage );
Utils::LinkGenerator::instance()->copyToClipboard( tPage->query() );
Utils::LinkGenerator::instance()->copyOpenLink( tPage->query() );
break;
}

View File

@ -180,8 +180,18 @@ SocialWidget::setQuery( const Tomahawk::query_ptr& query )
onShortLinkReady( QString(), QString(), QVariant() );
onChanged();
QUrl longUrl = Tomahawk::Utils::LinkGenerator::instance()->openLinkFromQuery( query );
m_slh.shortenLink( longUrl );
Tomahawk::ScriptJob* job = Tomahawk::Utils::LinkGenerator::instance()->openLink( query );
connect( job, SIGNAL( done( QVariantMap ) ), SLOT( onQueryLinkReady( QVariantMap ) ) );
job->start();
}
void
SocialWidget::onQueryLinkReady( const QVariantMap& data )
{
m_slh.shortenLink( data[ "url" ].toUrl() );
sender()->deleteLater();
}

View File

@ -65,6 +65,7 @@ protected:
private slots:
void accept();
void onChanged();
void onQueryLinkReady( const QVariantMap& data );
void onShortLinkReady( const QUrl& longUrl, const QUrl& shortUrl, const QVariant& callbackObj );
void onGeometryUpdate();