diff --git a/src/tools/CMakeLists.txt b/src/tools/CMakeLists.txt index b847f236d..16eb6fd57 100644 --- a/src/tools/CMakeLists.txt +++ b/src/tools/CMakeLists.txt @@ -1 +1,2 @@ +add_subdirectory( database-reader ) add_subdirectory( tomahawk-test-musicscan ) diff --git a/src/tools/database-reader/CMakeLists.txt b/src/tools/database-reader/CMakeLists.txt new file mode 100644 index 000000000..bd3489057 --- /dev/null +++ b/src/tools/database-reader/CMakeLists.txt @@ -0,0 +1,20 @@ +set( tomahawk_db_list_artists_src + listartists.cpp +) + +include_directories(${CMAKE_CURRENT_BINARY_DIR}) + +add_executable( tomahawk_db_list_artists_bin WIN32 MACOSX_BUNDLE + ${tomahawk_db_list_artists_src} ) +set_target_properties( tomahawk_db_list_artists_bin + PROPERTIES + AUTOMOC TRUE + RUNTIME_OUTPUT_NAME tomahawk-db-list-artists +) +target_link_libraries( tomahawk_db_list_artists_bin + ${TOMAHAWK_LIBRARIES} +) + +qt5_use_modules(tomahawk_db_list_artists_bin Core) +install( TARGETS tomahawk_db_list_artists_bin BUNDLE DESTINATION . RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} ) + diff --git a/src/tools/database-reader/listartists.cpp b/src/tools/database-reader/listartists.cpp new file mode 100644 index 000000000..363cdba4f --- /dev/null +++ b/src/tools/database-reader/listartists.cpp @@ -0,0 +1,83 @@ +#include "database/Database.h" +#include "database/DatabaseCommand_AllArtists.h" +#include "utils/TomahawkUtils.h" +#include "TomahawkVersion.h" +#include "Typedefs.h" + +#include +#include + +#include + + +class Tasks: public QObject +{ +Q_OBJECT +public: + Q_INVOKABLE void startDatabase( QString dbpath ) + { + database = QSharedPointer( new Tomahawk::Database( dbpath ) ); + connect( database.data(), SIGNAL( ready() ), SLOT( runDbCmd() ), Qt::QueuedConnection ); + database->loadIndex(); + } + + Tomahawk::dbcmd_ptr cmd; + QSharedPointer database; + +public slots: + void runDbCmd() + { + // TODO FIX database so that ready is emitted after the index is loaded and at least one worker runs. + database->enqueue( cmd ); + } + + void dbCmdDone( const QList& artists ) + { + std::cout << "=== ARTISTS - START ===" << std::endl; + foreach ( const Tomahawk::artist_ptr& artist, artists ) + { + std::cout << artist->name().toStdString() << std::endl; + } + std::cout << "=== ARTISTS - END ===" << std::endl; + QMetaObject::invokeMethod( thread(), "quit", Qt::QueuedConnection ); + } +}; + +// Include needs to go here as Tasks needs to be defined before. +#include "listartists.moc" + +int main( int argc, char* argv[] ) +{ + QCoreApplication app( argc, argv ); + // TODO: Add an argument to change the path + app.setOrganizationName( QLatin1String( TOMAHAWK_ORGANIZATION_NAME ) ); + + // Helper QObject to connect slots and actions in the correct thread. + Tasks tasks; + + // Start a thread so we can actually block main until the end of the DbCmd + QThread thread( 0 ); + thread.start(); + + // We need to do this or the finished() signal/quit() SLOT is not called. + thread.moveToThread( &thread ); + tasks.moveToThread( &thread ); + + // Load the Database + Tomahawk::DatabaseCommand_AllArtists* cmd = new Tomahawk::DatabaseCommand_AllArtists(); + tasks.cmd = Tomahawk::dbcmd_ptr( cmd ); + tasks.cmd->moveToThread( &thread ); + qRegisterMetaType< QList< Tomahawk::artist_ptr > >(); + QObject::connect( cmd, SIGNAL( artists( QList ) ), + &tasks, SLOT( dbCmdDone( QList ) ), + Qt::QueuedConnection ); + QString dbpath = TomahawkUtils::appDataDir().absoluteFilePath( "tomahawk.db" ); + QMetaObject::invokeMethod( &tasks, "startDatabase", Qt::QueuedConnection, Q_ARG( QString, dbpath ) ); + + // Wait until the dbcmd was executed. + thread.wait(); +} + +#if QT_VERSION < QT_VERSION_CHECK( 5, 0, 0 ) + Q_DECLARE_METATYPE( QList< Tomahawk::artist_ptr > ) +#endif