1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-02-24 20:03:07 +01:00

Initial database generators

This commit is contained in:
Leo Franchi 2011-07-25 18:00:14 -04:00
parent 5fca433805
commit 4a8b851ba5
6 changed files with 625 additions and 0 deletions

View File

@ -0,0 +1,101 @@
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
*
* Copyright 2010-2011, Leo Franchi <lfranchi@kde.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 "databasecommand_genericselect.h"
#include "databaseimpl.h"
#include "utils/logger.h"
#include <sourcelist.h>
#include <artist.h>
#include <album.h>
using namespace Tomahawk;
DatabaseCommand_GenericSelect::DatabaseCommand_GenericSelect( const QString& sqlSelect, QObject* parent )
: DatabaseCommand( parent )
, m_sqlSelect( sqlSelect )
{
}
void
DatabaseCommand_GenericSelect::exec( DatabaseImpl* dbi )
{
Q_ASSERT( source()->isLocal() || source()->id() >= 1 );
TomahawkSqlQuery query = dbi->newquery();
query.exec( m_sqlSelect );
QList< query_ptr > queries;
// Expecting
while ( query.next() )
{
Tomahawk::result_ptr result = Tomahawk::result_ptr( new Tomahawk::Result() );
Tomahawk::source_ptr s;
QString artist, track, album;
artist = query.value( 0 ).toString();
album = query.value( 1 ).toString();
track = query.value( 2 ).toString();
Tomahawk::query_ptr qry = Tomahawk::Query::get( artist, track, album, uuid(), (query.value( 7 ).toUInt() != 0) ); // Only auto-resolve non-local results
Tomahawk::artist_ptr artistptr = Tomahawk::Artist::get( query.value( 12 ).toUInt(), artist );
Tomahawk::album_ptr albumptr = Tomahawk::Album::get( query.value( 13 ).toUInt(), album, artistptr );
// If it's a local track, set to the local source and url in the result and we're done. otherwise, we get resolved
if( query.value( 7 ).toUInt() == 0 )
{
s = SourceList::instance()->getLocal();
result->setUrl( query.value( 7 ).toString() );
result->setId( query.value( 9 ).toUInt() );
result->setArtist( artistptr );
result->setAlbum( albumptr );
result->setTrack( query.value( 2 ).toString() );
result->setSize( query.value( 3 ).toUInt() );
result->setDuration( query.value( 4 ).toUInt() );
result->setBitrate( query.value( 5 ).toUInt() );
result->setMimetype( query.value( 8 ).toString() );
result->setScore( 1.0 );
result->setCollection( s->collection() );
TomahawkSqlQuery attrQuery = dbi->newquery();
QVariantMap attr;
attrQuery.prepare( "SELECT k, v FROM track_attributes WHERE id = ?" );
attrQuery.bindValue( 0, result->dbid() );
attrQuery.exec();
while ( attrQuery.next() )
{
attr[ attrQuery.value( 0 ).toString() ] = attrQuery.value( 1 ).toString();
}
result->setAttributes( attr );
qry->addResults( QList<Tomahawk::result_ptr>() << result );
qry->setResolveFinished( true );
}
queries << qry;
}
emit tracks( queries );
}

View File

@ -0,0 +1,56 @@
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
*
* Copyright 2010-2011, Leo Franchi <lfranchi@kde.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 DATABASECOMMAND_GENERICSELECT_H
#define DATABASECOMMAND_GENERICSELECT_H
#include <QVariantMap>
#include "databasecommand.h"
#include "source.h"
#include "typedefs.h"
#include "dllmacro.h"
/**
* This dbcmd takes a generic SELECT command that operates on the database and returns a list of query_ptrs
* that match.
*
* In order for the conversion to query_ptr to work, the SELECT command should select the following items:
* artist.name, album.name, track.name, file.size, file.duration, file.bitrate, file.url, file.source, file.mimetype, track.id
*
*/
class DLLEXPORT DatabaseCommand_GenericSelect : public DatabaseCommand
{
Q_OBJECT
public:
explicit DatabaseCommand_GenericSelect( const QString& sqlSelect, QObject* parent = 0 );
virtual void exec( DatabaseImpl* lib );
virtual bool doesMutates() const { return false; }
virtual QString commandname() const { return "genericselect"; }
signals:
void tracks( const QList< Tomahawk::query_ptr >& tracks );
private:
QString m_sqlSelect;
};
#endif // DATABASECOMMAND_GENERICSELECT_H

View File

@ -0,0 +1,147 @@
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
*
* Copyright 2010-2011, Leo Franchi <lfranchi@kde.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 "DatabaseControl.h"
using namespace Tomahawk;
DatabaseControl::DatabaseControl( const QString& selectedType, const QStringList& typeSelectors, QObject* parent )
: DynamicControl ( selectedType.isEmpty() ? "Artist" : selectedType, typeSelectors, parent )
{
setType( "database" );
m_editingTimer.setInterval( 500 ); //timeout to edits
m_editingTimer.setSingleShot( true );
connect( &m_editingTimer, SIGNAL( timeout() ), this, SLOT( editTimerFired() ) );
m_delayedEditTimer.setInterval( 250 ); // additional timer for "just typing" without enter or focus change
m_delayedEditTimer.setSingleShot( true );
connect( &m_delayedEditTimer, SIGNAL( timeout() ), &m_editingTimer, SLOT( start() ) );
}
DatabaseControl::DatabaseControl( const QString& sql, const QString& summary, const QStringList& typeSelectors, QObject* parent )
: DynamicControl ( typeSelectors )
, m_sql( sql )
, m_sqlSummary( summary )
{
setType( "database" );
}
QString DatabaseControl::input() const
{
// TODO
return QString();
}
QWidget* DatabaseControl::inputField()
{
return 0;
}
QString DatabaseControl::match() const
{
return m_matchData;
}
QWidget* DatabaseControl::matchSelector()
{
return 0;
}
QString DatabaseControl::matchString() const
{
return m_matchString;
}
void DatabaseControl::setInput ( const QString& input )
{
// TODO
updateWidgets();
}
void DatabaseControl::setMatch ( const QString& match )
{
m_matchData = match;
updateWidgets();
}
void DatabaseControl::setSelectedType ( const QString& type )
{
if ( type != selectedType() ) {
if ( !m_input.isNull() )
delete m_input.data();
if ( !m_match.isNull() )
delete m_match.data();
Tomahawk::DynamicControl::setSelectedType ( type );
updateWidgets();
updateData();
// qDebug() << "Setting new type, set data to:" << m_data.first << m_data.second;
}
}
void DatabaseControl::editingFinished()
{
}
void DatabaseControl::editTimerFired()
{
}
void DatabaseControl::updateData()
{
}
void DatabaseControl::updateWidgets()
{
}
void DatabaseControl::updateWidgetsFromData()
{
}
void DatabaseControl::calculateSummary()
{
if( !m_sqlSummary.isEmpty() )
return;
}
QString
DatabaseControl::sql() const
{
return m_sql;
}
QString
DatabaseControl::summary() const
{
if( !m_sqlSummary.isEmpty() )
return m_sqlSummary;
return m_summary;
}

View File

@ -0,0 +1,81 @@
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
*
* Copyright 2010-2011, Leo Franchi <lfranchi@kde.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 DATABASE_CONTROL_H
#define DATABASE_CONTROL_H
#include "dynamic/DynamicControl.h"
#include <QTimer>
namespace Tomahawk
{
class DatabaseControl : public DynamicControl
{
Q_OBJECT
public:
virtual QWidget* inputField();
virtual QWidget* matchSelector();
virtual QString input() const;
virtual QString match() const;
virtual QString matchString() const;
virtual QString summary() const;
virtual void setInput(const QString& input);
virtual void setMatch(const QString& match);
/// DO NOT USE IF YOU ARE NOT A DBCMD
DatabaseControl( const QString& type, const QStringList& typeSelectors, QObject* parent = 0 );
DatabaseControl( const QString& sql, const QString& summary, const QStringList& typeSelectors, QObject* parent = 0 );
QString sql() const;
public slots:
virtual void setSelectedType ( const QString& type );
private slots:
void updateData();
void editingFinished();
void editTimerFired();
private:
void updateWidgets();
void updateWidgetsFromData();
// utility
void calculateSummary();
QWeakPointer< QWidget > m_input;
QWeakPointer< QWidget > m_match;
QString m_matchData;
QString m_matchString;
QString m_summary;
QTimer m_editingTimer;
QTimer m_delayedEditTimer;
// SQL control
QString m_sql;
QString m_sqlSummary;
};
};
#endif

View File

@ -0,0 +1,160 @@
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
*
* Copyright 2010-2011, Leo Franchi <lfranchi@kde.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 "DatabaseGenerator.h"
#include "DatabaseControl.h"
#include "utils/logger.h"
#include <database/databasecommand_genericselect.h>
#include <database/database.h>
using namespace Tomahawk;
GeneratorInterface*
DatabaseFactory::create()
{
return new DatabaseGenerator();
}
dyncontrol_ptr
DatabaseFactory::createControl ( const QString& controlType )
{
return dyncontrol_ptr( new DatabaseControl( controlType, typeSelectors() ) );
}
dyncontrol_ptr
DatabaseFactory::createControl ( const QString& sql, const QString& summary )
{
return dyncontrol_ptr( new DatabaseControl( sql, summary, typeSelectors() ) );
}
QStringList
DatabaseFactory::typeSelectors() const
{
return QStringList() << "SQL" << "Artist" << "Album" << "Title";
}
DatabaseGenerator::DatabaseGenerator ( QObject* parent )
: GeneratorInterface ( parent )
{
// m_logo.load( RESPATH "images )
}
DatabaseGenerator::~DatabaseGenerator()
{
}
QPixmap
DatabaseGenerator::logo()
{
return m_logo;
}
void
DatabaseGenerator::dynamicFetched()
{
}
void
DatabaseGenerator::dynamicStarted()
{
}
void
DatabaseGenerator::generate( int number )
{
tLog() << "Generating" << number << "tracks for this database dynamic playlist with" << m_controls.size() << "controls:";
foreach ( const dyncontrol_ptr& ctrl, m_controls )
qDebug() << ctrl->selectedType() << ctrl->match() << ctrl->input();
// TODO for now, we just support the special "SQL" control, not meant to be shown to the user. Just does a raw query.
bool isSql = false;
foreach ( const dyncontrol_ptr& ctrl, m_controls )
{
if( ctrl->selectedType() == "SQL" )
isSql = true;
else if( !isSql )
{
qWarning() << "Cannot mix sql and non-sql controls!";
emit error( "Failed to generate tracks", "Cannot mix sql and non-sql controls" );
}
}
// TODO for now we just support 1 sql query if we're a sql type
if ( isSql )
{
dyncontrol_ptr control = m_controls.first();
DatabaseCommand_GenericSelect* cmd = new DatabaseCommand_GenericSelect( control.dynamicCast< DatabaseControl >()->sql(), this );
connect( cmd, SIGNAL( tracks( QList<Tomahawk::query_ptr> ) ), this, SIGNAL( tracksGenerated( QList<Tomahawk::query_ptr> ) ) );
Database::instance()->enqueue( QSharedPointer< DatabaseCommand >( cmd ) );
return;
}
}
void
DatabaseGenerator::tracksGenerated ( const QList< query_ptr >& tracks )
{
emit generated( tracks );
}
dyncontrol_ptr
DatabaseGenerator::createControl( const QString& type )
{
m_controls << dyncontrol_ptr( new DatabaseControl( type, GeneratorFactory::typeSelectors( m_type ) ) );
return m_controls.last();
}
dyncontrol_ptr
DatabaseGenerator::createControl ( const QString& sql, const QString& summary )
{
m_controls << dyncontrol_ptr( new DatabaseControl( sql, summary, GeneratorFactory::typeSelectors( m_type ) ) );
return m_controls.last();
}
void
DatabaseGenerator::fetchNext( int rating )
{
}
QString
DatabaseGenerator::sentenceSummary()
{
if( m_controls.count() && m_controls.first()->type() == "SQL" )
return m_controls.first()->summary();
// TODO
return QString();
}
void
DatabaseGenerator::startOnDemand()
{
}

View File

@ -0,0 +1,80 @@
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
*
* Copyright 2010-2011, Leo Franchi <lfranchi@kde.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 DATABASE_GENERATOR_H
#define DATABASE_GENERATOR_H
#include <stdexcept>
#include "playlist/dynamic/GeneratorInterface.h"
#include "playlist/dynamic/GeneratorFactory.h"
#include "playlist/dynamic/DynamicControl.h"
#include "dllmacro.h"
namespace Tomahawk
{
class DLLEXPORT DatabaseFactory : public GeneratorFactoryInterface
{
public:
DatabaseFactory() {}
virtual GeneratorInterface* create();
virtual dyncontrol_ptr createControl( const QString& controlType = QString() );
// TO create a special SQL resolver that consists of a pre-baked SQL query and a description of it
virtual dyncontrol_ptr createControl( const QString& sql, const QString& summary );
virtual QStringList typeSelectors() const;
};
/**
* Generator based on the database. Can filter the database based on some user-controllable options,
* or just be the front-facing part of any given SQL query to fake an interesting read-only playlist.
*/
class DatabaseGenerator : public GeneratorInterface
{
Q_OBJECT
public:
explicit DatabaseGenerator( QObject* parent = 0 );
virtual ~DatabaseGenerator();
virtual dyncontrol_ptr createControl( const QString& type = QString() );
virtual dyncontrol_ptr createControl( const QString& sql, const QString& summary );
virtual QPixmap logo();
virtual void generate ( int number = -1 );
virtual void startOnDemand();
virtual void fetchNext( int rating = -1 );
virtual QString sentenceSummary();
virtual bool onDemandSteerable() const { return false; }
virtual QWidget* steeringWidget() { return 0; }
private slots:
void tracksGenerated( const QList< Tomahawk::query_ptr >& tracks );
void dynamicStarted();
void dynamicFetched();
private:
QPixmap m_logo;
};
};
#endif