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

Pluginify link generation

This commit is contained in:
Dominik Schmidt 2014-11-30 01:40:45 +01:00 committed by Dominik Schmidt
parent 33d225233b
commit cd7ca26e78
9 changed files with 416 additions and 138 deletions

View File

@ -91,6 +91,7 @@ set( libGuiSources
resolvers/ScriptEngine.cpp
resolvers/JSPlugin.cpp
resolvers/ScriptJob.cpp
resolvers/SyncScriptJob.cpp
utils/DpiScaler.cpp
utils/ImageRegistry.cpp
@ -119,6 +120,8 @@ set( libGuiSources
utils/NetworkAccessManager.cpp
utils/ShortLinkHelper.cpp
utils/LinkGenerator.cpp
utils/LinkGeneratorPlugin.cpp
utils/TomaHkLinkGeneratorPlugin.cpp
viewpages/SearchViewPage.cpp
viewpages/SourceViewPage.cpp

View File

@ -0,0 +1,31 @@
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
*
* Copyright (C) 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 2 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 "SyncScriptJob.h"
Tomahawk::SyncScriptJob::SyncScriptJob( const QVariantMap& resultData )
: ScriptJob( nullptr, "nomethod")
{
m_data = resultData;
}
void Tomahawk::SyncScriptJob::start()
{
QMetaObject::invokeMethod( this, "reportResults", Qt::QueuedConnection, Q_ARG( QVariantMap, m_data ) );
}

View File

@ -0,0 +1,42 @@
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
*
* Copyright (C) 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 2 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/>.
*/
#pragma once
#ifndef TOMAHAWK_SYNCSCRIPTJOB_H
#define TOMAHAWK_SYNCSCRIPTJOB_H
#include "ScriptJob.h"
#include "../DllMacro.h"
#include "../Typedefs.h"
namespace Tomahawk {
class SyncScriptJob : public ScriptJob
{
Q_OBJECT
public:
SyncScriptJob( const QVariantMap& resultData );
void start() override;
};
} // namespace Tomahawk
#endif // TOMAHAWK_SYNCSCRIPTJOB_H

View File

@ -27,42 +27,19 @@
#include "Logger.h"
#include "ShortLinkHelper.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 "TomaHkLinkGeneratorPlugin.h"
#include <echonest/Playlist.h>
#include "../resolvers/SyncScriptJob.h"
#include <QClipboard>
#include <QApplication>
#include <QMetaObject>
#include <memory>
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;
@ -79,6 +56,8 @@ LinkGenerator::instance()
LinkGenerator::LinkGenerator( QObject* parent )
: QObject( parent )
{
m_defaultPlugin.reset( new TomaHkLinkGeneratorPlugin );
m_plugins.append( m_defaultPlugin.get() );
}
@ -86,112 +65,6 @@ LinkGenerator::~LinkGenerator()
{
}
QString
LinkGenerator::hostname() const
{
return QString( "http://toma.hk" );
}
ScriptJob*
LinkGenerator::openLink( const query_ptr& query ) const
{
QString title = query->track()->track();
QString artist = query->track()->artist();
QString album = query->track()->album();
return openLink( title, artist, album );
}
ScriptJob*
LinkGenerator::openLink( const QString& title, const QString& artist, const QString& album ) const
{
QUrl link( QString( "%1/open/track/" ).arg( hostname() ) );
if ( !artist.isEmpty() )
TomahawkUtils::urlAddQueryItem( link, "artist", artist );
if ( !title.isEmpty() )
TomahawkUtils::urlAddQueryItem( link, "title", title );
if ( !album.isEmpty() )
TomahawkUtils::urlAddQueryItem( link, "album", album );
QVariantMap data;
data[ "url" ] = link;
return new SyncScriptJob( data );
}
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 nullptr;
}
TomahawkUtils::urlAddQueryItem( link, "type", "echonest" );
TomahawkUtils::urlAddQueryItem( link, "title", playlist->title() );
QList< dyncontrol_ptr > controls = playlist->generator()->controls();
foreach ( const dyncontrol_ptr& c, controls )
{
if ( c->selectedType() == "Artist" )
{
if ( c->match().toInt() == Echonest::DynamicPlaylist::ArtistType )
TomahawkUtils::urlAddQueryItem( link, "artist_limitto", c->input() );
else
TomahawkUtils::urlAddQueryItem( link, "artist", c->input() );
}
else if ( c->selectedType() == "Artist Description" )
{
TomahawkUtils::urlAddQueryItem( link, "description", c->input() );
}
else
{
QString name = c->selectedType().toLower().replace( " ", "_" );
Echonest::DynamicPlaylist::PlaylistParam p = static_cast< Echonest::DynamicPlaylist::PlaylistParam >( c->match().toInt() );
// if it is a max, set that too
if ( p == Echonest::DynamicPlaylist::MaxTempo || p == Echonest::DynamicPlaylist::MaxDuration || p == Echonest::DynamicPlaylist::MaxLoudness
|| p == Echonest::DynamicPlaylist::MaxDanceability || p == Echonest::DynamicPlaylist::MaxEnergy || p == Echonest::DynamicPlaylist::ArtistMaxFamiliarity
|| p == Echonest::DynamicPlaylist::ArtistMaxHotttnesss || p == Echonest::DynamicPlaylist::SongMaxHotttnesss || p == Echonest::DynamicPlaylist::ArtistMaxLatitude
|| p == Echonest::DynamicPlaylist::ArtistMaxLongitude )
name += "_max";
TomahawkUtils::urlAddQueryItem( link, name, c->input() );
}
}
QVariantMap data;
data[ "url" ] = link;
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::copyScriptJobResultToClipboard( const QVariantMap& data )
{

View File

@ -22,10 +22,13 @@
#define TOMAHAWK_UTILS_LINKGENERATOR_H
#include "../resolvers/ScriptJob.h"
#include "LinkGeneratorPlugin.h"
#include "../DllMacro.h"
#include "../Typedefs.h"
#include <memory>
namespace Tomahawk {
namespace Utils {
@ -37,11 +40,45 @@ public:
static LinkGenerator* instance();
virtual ~LinkGenerator();
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;
// TODO: openLink(QString, QString, QString) is a rather annoying special case. Can we get rid of it?
ScriptJob* openLink( const QString& title, const QString& artist, const QString& album ) const
{
ScriptJob* job;
QList< LinkGeneratorPlugin* >::const_iterator i = m_plugins.constEnd();
while ( i != m_plugins.constBegin() )
{
--i;
job = (*i)->openLink( title, artist, album );
if ( job )
{
break;
}
}
// No suitable link generator plugin found
Q_ASSERT( job );
return job;
}
template <typename T> ScriptJob* openLink( const T& item ) const
{
ScriptJob* job;
QList< LinkGeneratorPlugin* >::const_iterator i = m_plugins.constEnd();
while ( i != m_plugins.constBegin() )
{
--i;
job = (*i)->openLink( item );
if ( job )
{
break;
}
}
// No suitable link generator plugin found
Q_ASSERT( job );
return job;
}
// Fire and forget
@ -68,10 +105,13 @@ private slots:
private:
explicit LinkGenerator( QObject* parent = 0 );
QString hostname() const;
QUrl m_clipboardLongUrl;
std::unique_ptr< LinkGeneratorPlugin > m_defaultPlugin;
QList< LinkGeneratorPlugin* > m_plugins;
static LinkGenerator* s_instance;
};

View File

@ -0,0 +1,65 @@
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
*
* Copyright (C) 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 2 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 "LinkGeneratorPlugin.h"
#include "../Query.h"
#include "../Track.h"
#include "../Artist.h"
#include "../Album.h"
Tomahawk::Utils::LinkGeneratorPlugin::~LinkGeneratorPlugin()
{
}
Tomahawk::ScriptJob*
Tomahawk::Utils::LinkGeneratorPlugin::openLink(const QString&, const QString&, const QString&) const
{
return nullptr;
}
Tomahawk::ScriptJob* Tomahawk::Utils::LinkGeneratorPlugin::openLink( const Tomahawk::query_ptr& query ) const
{
QString title = query->track()->track();
QString artist = query->track()->artist();
QString album = query->track()->album();
return openLink( title, artist, album );
}
Tomahawk::ScriptJob*
Tomahawk::Utils::LinkGeneratorPlugin::openLink(const Tomahawk::artist_ptr&) const
{
return nullptr;
}
Tomahawk::ScriptJob*
Tomahawk::Utils::LinkGeneratorPlugin::openLink(const Tomahawk::album_ptr&) const
{
return nullptr;
}
Tomahawk::ScriptJob*
Tomahawk::Utils::LinkGeneratorPlugin::openLink(const Tomahawk::dynplaylist_ptr&) const
{
return nullptr;
}

View File

@ -0,0 +1,47 @@
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
*
* Copyright (C) 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 2 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/>.
*/
#pragma once
#ifndef TOMAHAWK_UTILS_LINKGENERATORPLUGIN_H
#define TOMAHAWK_UTILS_LINKGENERATORPLUGIN_H
#include "../DllMacro.h"
#include "../Typedefs.h"
namespace Tomahawk {
class ScriptJob;
namespace Utils {
class DLLEXPORT LinkGeneratorPlugin
{
public:
virtual ~LinkGeneratorPlugin();
virtual ScriptJob* openLink( const QString& title, const QString& artist, const QString& album ) const;
virtual ScriptJob* openLink( const Tomahawk::query_ptr& query ) const;
virtual ScriptJob* openLink( const Tomahawk::artist_ptr& artist ) const;
virtual ScriptJob* openLink( const Tomahawk::album_ptr& album ) const;
virtual ScriptJob* openLink( const Tomahawk::dynplaylist_ptr& playlist ) const;
};
} // namespace Utils
} // namespace Tomahawk
#endif // TOMAHAWK_UTILS_LINKGENERATORPLUGIN_H

View File

@ -0,0 +1,125 @@
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
*
* Copyright (C) 2011 Leo Franchi <lfranchi@kde.org>
* Copyright (C) 2011, Jeff Mitchell <jeff@tomahawk-player.org>
* Copyright (C) 2011-2014, Christian Muehlhaeuser <muesli@tomahawk-player.org>
* Copyright (C) 2013, Uwe L. Korn <uwelk@xhochy.com>
* Copyright (C) 2013, Teo Mrnjavac <teo@kde.org>
* Copyright (C) 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 2 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 "TomaHkLinkGeneratorPlugin.h"
#include "../playlist/dynamic/GeneratorInterface.h"
#include "../playlist/dynamic/DynamicPlaylist.h"
#include "../Track.h"
#include "../Artist.h"
#include "../Album.h"
#include "../resolvers/SyncScriptJob.h"
#include "../utils/Logger.h"
QString
Tomahawk::Utils::TomaHkLinkGeneratorPlugin::hostname() const
{
return QString( "http://toma.hk" );
}
Tomahawk::ScriptJob*
Tomahawk::Utils::TomaHkLinkGeneratorPlugin::openLink( const QString& title, const QString& artist, const QString& album ) const
{
QUrl link( QString( "%1/open/track/" ).arg( hostname() ) );
if ( !artist.isEmpty() )
TomahawkUtils::urlAddQueryItem( link, "artist", artist );
if ( !title.isEmpty() )
TomahawkUtils::urlAddQueryItem( link, "title", title );
if ( !album.isEmpty() )
TomahawkUtils::urlAddQueryItem( link, "album", album );
QVariantMap data;
data[ "url" ] = link;
return new SyncScriptJob( data );
}
Tomahawk::ScriptJob*
Tomahawk::Utils::TomaHkLinkGeneratorPlugin::openLink( const Tomahawk::artist_ptr& artist ) const
{
QVariantMap data;
data[ "url" ] = QString( "%1/artist/%2" ).arg( hostname() ).arg( artist->name() );
return new SyncScriptJob( data );
}
Tomahawk::ScriptJob*
Tomahawk::Utils::TomaHkLinkGeneratorPlugin::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 );
}
Tomahawk::ScriptJob*
Tomahawk::Utils::TomaHkLinkGeneratorPlugin::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 nullptr;
}
TomahawkUtils::urlAddQueryItem( link, "type", "echonest" );
TomahawkUtils::urlAddQueryItem( link, "title", playlist->title() );
QList< dyncontrol_ptr > controls = playlist->generator()->controls();
foreach ( const dyncontrol_ptr& c, controls )
{
if ( c->selectedType() == "Artist" )
{
if ( c->match().toInt() == Echonest::DynamicPlaylist::ArtistType )
TomahawkUtils::urlAddQueryItem( link, "artist_limitto", c->input() );
else
TomahawkUtils::urlAddQueryItem( link, "artist", c->input() );
}
else if ( c->selectedType() == "Artist Description" )
{
TomahawkUtils::urlAddQueryItem( link, "description", c->input() );
}
else
{
QString name = c->selectedType().toLower().replace( " ", "_" );
Echonest::DynamicPlaylist::PlaylistParam p = static_cast< Echonest::DynamicPlaylist::PlaylistParam >( c->match().toInt() );
// if it is a max, set that too
if ( p == Echonest::DynamicPlaylist::MaxTempo || p == Echonest::DynamicPlaylist::MaxDuration || p == Echonest::DynamicPlaylist::MaxLoudness
|| p == Echonest::DynamicPlaylist::MaxDanceability || p == Echonest::DynamicPlaylist::MaxEnergy || p == Echonest::DynamicPlaylist::ArtistMaxFamiliarity
|| p == Echonest::DynamicPlaylist::ArtistMaxHotttnesss || p == Echonest::DynamicPlaylist::SongMaxHotttnesss || p == Echonest::DynamicPlaylist::ArtistMaxLatitude
|| p == Echonest::DynamicPlaylist::ArtistMaxLongitude )
name += "_max";
TomahawkUtils::urlAddQueryItem( link, name, c->input() );
}
}
QVariantMap data;
data[ "url" ] = link;
return new SyncScriptJob( data );
}

View File

@ -0,0 +1,52 @@
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
*
* Copyright (C) 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 2 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/>.
*/
#pragma once
#ifndef TOMAHAWK_UTILS_TOMAHKLINKGENERATORPLUGIN_H
#define TOMAHAWK_UTILS_TOMAHKLINKGENERATORPLUGIN_H
#include "LinkGeneratorPlugin.h"
#include "../DllMacro.h"
#include "../Typedefs.h"
#include <echonest/Playlist.h>
namespace Tomahawk {
class ScriptJob;
namespace Utils {
class DLLEXPORT TomaHkLinkGeneratorPlugin : public LinkGeneratorPlugin
{
public:
ScriptJob* openLink( const QString& title, const QString& artist, const QString& album ) const override;
ScriptJob* openLink( const Tomahawk::artist_ptr& artist ) const override;
ScriptJob* openLink( const Tomahawk::album_ptr& album ) const override;
ScriptJob* openLink( const Tomahawk::dynplaylist_ptr& playlist ) const override;
private:
QString hostname() const;
};
} // namespace Utils
} // namespace Tomahawk
#endif // TOMAHAWK_UTILS_TOMAHKLINKGENERATORPLUGIN_H