From f2d28ac958a7b2c71e65f9f9e86bbb0a0bb30cde Mon Sep 17 00:00:00 2001 From: Dominik Schmidt Date: Fri, 5 Jul 2013 23:45:00 +0200 Subject: [PATCH] Add API to access DBCommand factories so you can observe automatically created instances --- src/libtomahawk/database/Database.cpp | 38 ++++++++++++++++++-- src/libtomahawk/database/Database.h | 28 ++++++++++++--- src/libtomahawk/database/DatabaseCommand.cpp | 2 +- src/libtomahawk/database/DatabaseCommand.h | 11 ++++-- 4 files changed, 69 insertions(+), 10 deletions(-) diff --git a/src/libtomahawk/database/Database.cpp b/src/libtomahawk/database/Database.cpp index 4e72a1145..7d4773097 100644 --- a/src/libtomahawk/database/Database.cpp +++ b/src/libtomahawk/database/Database.cpp @@ -52,6 +52,18 @@ #define DEFAULT_WORKER_THREADS 4 #define MAX_WORKER_THREADS 16 + +DatabaseCommand* +DatabaseCommandFactory::newInstance() +{ + DatabaseCommand* command = create(); + emit created( command ); + return command; +} + + + + Database* Database::s_instance = 0; @@ -248,9 +260,12 @@ void Database::registerCommand( DatabaseCommandFactory* commandFactory ) { // this is ugly, but we don't have virtual static methods in C++ :( - QScopedPointer command( commandFactory->create() ); + QScopedPointer command( commandFactory->newInstance() ); const QString commandName = command->commandname(); + const QString className = command->metaObject()->className(); + + tDebug() << "Registering command" << commandName << "from class" << className; if( m_commandFactories.keys().contains( commandName ) ) { @@ -258,14 +273,31 @@ Database::registerCommand( DatabaseCommandFactory* commandFactory ) } Q_ASSERT( !m_commandFactories.keys().contains( commandName ) ); + m_commandNameClassNameMapping.insert( commandName, className ); m_commandFactories.insert( commandName, commandFactory ); } +DatabaseCommandFactory* +Database::commandFactoryByClassName(const QString& className) +{ + const QString commandName = m_commandNameClassNameMapping.key( className ); + return commandFactoryByCommandName( commandName ); +} + + +DatabaseCommandFactory* +Database::commandFactoryByCommandName(const QString& commandName ) +{ + return m_commandFactories.value( commandName ); +} + + + DatabaseCommand* Database::createCommandInstance( const QString& commandName ) { - DatabaseCommandFactory* factory = m_commandFactories.value( commandName ); + DatabaseCommandFactory* factory = commandFactoryByCommandName( commandName ); if( !factory ) { @@ -273,7 +305,7 @@ Database::createCommandInstance( const QString& commandName ) return 0; } - return factory->create(); + return factory->newInstance(); } diff --git a/src/libtomahawk/database/Database.h b/src/libtomahawk/database/Database.h index 1fc1b77be..97a4a728b 100644 --- a/src/libtomahawk/database/Database.h +++ b/src/libtomahawk/database/Database.h @@ -36,14 +36,25 @@ class DatabaseWorker; class IdThreadWorker; -struct DLLEXPORT DatabaseCommandFactory { - virtual ~DatabaseCommandFactory() {}; - virtual DatabaseCommand* create() const = 0; +class DLLEXPORT DatabaseCommandFactory : public QObject +{ +Q_OBJECT + +public: + virtual ~DatabaseCommandFactory() {}; + DatabaseCommand* newInstance(); + +signals: + void created( DatabaseCommand* command ); + +protected: + virtual DatabaseCommand* create() const = 0; }; template -struct DatabaseCommandFactoryImplementation : public DatabaseCommandFactory +class DatabaseCommandFactoryImplementation : public DatabaseCommandFactory { +protected: virtual COMMAND* create() const { return new COMMAND(); }; }; @@ -80,6 +91,11 @@ public: registerCommand( new DatabaseCommandFactoryImplementation() ); } + template DatabaseCommandFactory* commandFactory() + { + return commandFactoryByClassName( T::staticMetaObject.className() ); + } + signals: void indexReady(); // search index void ready(); @@ -96,6 +112,8 @@ private slots: private: void registerCommand( DatabaseCommandFactory* commandFactory ); + DatabaseCommandFactory* commandFactoryByClassName( const QString& className ); + DatabaseCommandFactory* commandFactoryByCommandName( const QString& commandName ); DatabaseCommand* createCommandInstance( const QString& commandName ); bool m_ready; @@ -105,7 +123,9 @@ private: QList< QPointer< DatabaseWorkerThread > > m_workerThreads; IdThreadWorker* m_idWorker; int m_maxConcurrentThreads; + QHash< QString, DatabaseCommandFactory* > m_commandFactories; + QHash< QString, QString> m_commandNameClassNameMapping; QHash< QThread*, DatabaseImpl* > m_implHash; QMutex m_mutex; diff --git a/src/libtomahawk/database/DatabaseCommand.cpp b/src/libtomahawk/database/DatabaseCommand.cpp index c3ec13213..773c7cf9a 100644 --- a/src/libtomahawk/database/DatabaseCommand.cpp +++ b/src/libtomahawk/database/DatabaseCommand.cpp @@ -55,7 +55,7 @@ DatabaseCommand::_exec( DatabaseImpl* lib ) { //qDebug() << "RUNNING" << thread(); m_state = RUNNING; - emit running(); + emitRunning(); exec( lib ); m_state = FINISHED; //qDebug() << "FINISHED" << thread(); diff --git a/src/libtomahawk/database/DatabaseCommand.h b/src/libtomahawk/database/DatabaseCommand.h index 0e2accfdc..c3236da63 100644 --- a/src/libtomahawk/database/DatabaseCommand.h +++ b/src/libtomahawk/database/DatabaseCommand.h @@ -64,7 +64,7 @@ public: // stuff to do once transaction applied ok. // Don't change the database from in here, duh. - void postCommit() { postCommitHook(); emit committed(); } + void postCommit() { postCommitHook(); emitCommitted(); } virtual void postCommitHook(){}; void setSource( const Tomahawk::source_ptr& s ); @@ -87,12 +87,19 @@ public: } void setGuid( const QString& g ) { m_guid = g; } - void emitFinished() { emit finished(); } + void emitFinished() { emit finished(this); emit finished(); } + void emitCommitted() { emit committed(this); emit committed(); } + void emitRunning() { emit running(this); emit running(); } signals: void running(); + void running(DatabaseCommand*); + void finished(); + void finished(DatabaseCommand*); + void committed(); + void committed(DatabaseCommand*); private: State m_state;