1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-08-13 17:43:59 +02:00

Pimple DatabaseCommand

This commit is contained in:
Uwe L. Korn
2013-07-10 19:37:33 +02:00
parent a92cc59851
commit f87bf7921d
3 changed files with 169 additions and 39 deletions

View File

@@ -1,6 +1,7 @@
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> === /* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
* *
* Copyright 2010-2011, Christian Muehlhaeuser <muesli@tomahawk-player.org> * Copyright 2010-2011, Christian Muehlhaeuser <muesli@tomahawk-player.org>
* Copyright 2013, Uwe L. Korn <uwelk@xhochy.com>
* *
* Tomahawk is free software: you can redistribute it and/or modify * Tomahawk is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@@ -16,7 +17,7 @@
* along with Tomahawk. If not, see <http://www.gnu.org/licenses/>. * along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include "DatabaseCommand.h" #include "DatabaseCommand_p.h"
#include "utils/Logger.h" #include "utils/Logger.h"
@@ -28,70 +29,146 @@ namespace Tomahawk
DatabaseCommand::DatabaseCommand( QObject* parent ) DatabaseCommand::DatabaseCommand( QObject* parent )
: QObject( parent ) : QObject( parent )
, m_state( PENDING ) , d_ptr( new DatabaseCommandPrivate( this ) )
{ {
//qDebug() << Q_FUNC_INFO;
} }
DatabaseCommand::DatabaseCommand( const Tomahawk::source_ptr& src, QObject* parent ) DatabaseCommand::DatabaseCommand( const Tomahawk::source_ptr& src, QObject* parent )
: QObject( parent ) : QObject( parent )
, m_state( PENDING ) , d_ptr( new DatabaseCommandPrivate( this, src ) )
, m_source( src )
{ {
//qDebug() << Q_FUNC_INFO;
} }
DatabaseCommand::DatabaseCommand( const DatabaseCommand& other ) DatabaseCommand::DatabaseCommand( const DatabaseCommand& other )
: QObject( other.parent() ) : QObject( other.parent() )
, d_ptr( new DatabaseCommandPrivate( this ) )
{ {
} }
DatabaseCommand::~DatabaseCommand() DatabaseCommand::~DatabaseCommand()
{ {
// qDebug() << Q_FUNC_INFO; }
DatabaseCommand::State
DatabaseCommand::state() const
{
Q_D( const DatabaseCommand );
return d->state;
} }
void void
DatabaseCommand::_exec( DatabaseImpl* lib ) DatabaseCommand::_exec( DatabaseImpl* lib )
{ {
//qDebug() << "RUNNING" << thread(); Q_D( DatabaseCommand );
m_state = RUNNING; d->state = RUNNING;
emitRunning(); emitRunning();
exec( lib ); exec( lib );
m_state = FINISHED; d->state = FINISHED;
//qDebug() << "FINISHED" << thread();
} }
void void
DatabaseCommand::setSource( const Tomahawk::source_ptr& s ) DatabaseCommand::setSource( const Tomahawk::source_ptr& s )
{ {
m_source = s; Q_D( DatabaseCommand );
d->source = s;
} }
const Tomahawk::source_ptr& const Tomahawk::source_ptr&
DatabaseCommand::source() const DatabaseCommand::source() const
{ {
return m_source; Q_D( const DatabaseCommand );
return d->source;
}
QVariant
DatabaseCommand::data() const
{
Q_D( const DatabaseCommand );
return d->data;
}
void
DatabaseCommand::setData( const QVariant& data )
{
Q_D( DatabaseCommand );
d->data = data;
}
QString
DatabaseCommand::guid() const
{
Q_D( const DatabaseCommand );
if( d->guid.isEmpty() )
d->guid = uuid();
return d->guid;
}
void
DatabaseCommand::setGuid( const QString& g)
{
Q_D( DatabaseCommand );
d->guid = g;
}
void
DatabaseCommand::emitFinished()
{
Q_D( DatabaseCommand );
emit finished( d->ownRef.toStrongRef() );
emit finished();
}
void
DatabaseCommand::emitCommitted()
{
Q_D( DatabaseCommand );
emit committed( d->ownRef.toStrongRef() );
emit committed();
}
void
DatabaseCommand::emitRunning()
{
Q_D( DatabaseCommand );
emit running( d->ownRef.toStrongRef() );
emit running();
} }
QWeakPointer< DatabaseCommand > QWeakPointer< DatabaseCommand >
DatabaseCommand::weakRef() DatabaseCommand::weakRef() const
{ {
return m_ownRef; Q_D( const DatabaseCommand );
return d->ownRef;
} }
void void
DatabaseCommand::setWeakRef( QWeakPointer< DatabaseCommand > weakRef ) DatabaseCommand::setWeakRef( QWeakPointer< DatabaseCommand > weakRef )
{ {
m_ownRef = weakRef; Q_D( DatabaseCommand );
d->ownRef = weakRef;
}
DatabaseCommand::DatabaseCommand( QObject* parent, DatabaseCommandPrivate* d)
: QObject( parent )
, d_ptr( d )
{
} }
} }

View File

@@ -2,6 +2,7 @@
* *
* Copyright 2010-2011, Christian Muehlhaeuser <muesli@tomahawk-player.org> * Copyright 2010-2011, Christian Muehlhaeuser <muesli@tomahawk-player.org>
* Copyright 2010-2011, Jeff Mitchell <jeff@tomahawk-player.org> * Copyright 2010-2011, Jeff Mitchell <jeff@tomahawk-player.org>
* Copyright 2013, Uwe L. Korn <uwelk@xhochy.com>
* *
* Tomahawk is free software: you can redistribute it and/or modify * Tomahawk is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
@@ -27,13 +28,14 @@
#include <QVariant> #include <QVariant>
#include "Typedefs.h" #include "Typedefs.h"
#include "database/Op.h" // #include "database/Op.h"
#include "DllMacro.h" #include "DllMacro.h"
namespace Tomahawk namespace Tomahawk
{ {
class DatabaseCommandPrivate;
class DatabaseImpl; class DatabaseImpl;
class DLLEXPORT DatabaseCommand : public QObject class DLLEXPORT DatabaseCommand : public QObject
@@ -57,7 +59,7 @@ public:
virtual QString commandname() const { return "DatabaseCommand"; } virtual QString commandname() const { return "DatabaseCommand"; }
virtual bool doesMutates() const { return true; } virtual bool doesMutates() const { return true; }
State state() const { return m_state; } State state() const;
// if i make this pure virtual, i get compile errors in qmetatype.h. // if i make this pure virtual, i get compile errors in qmetatype.h.
// we need Q_DECLARE_METATYPE to use in queued sig/slot connections. // we need Q_DECLARE_METATYPE to use in queued sig/slot connections.
@@ -78,23 +80,17 @@ public:
virtual bool singletonCmd() const { return false; } virtual bool singletonCmd() const { return false; }
virtual bool localOnly() const { return false; } virtual bool localOnly() const { return false; }
virtual QVariant data() const { return m_data; } virtual QVariant data() const;
virtual void setData( const QVariant& data ) { m_data = data; } virtual void setData( const QVariant& data );
QString guid() const QString guid() const;
{ void setGuid( const QString& g );
if( m_guid.isEmpty() )
m_guid = uuid();
return m_guid; void emitFinished();
} void emitCommitted();
void setGuid( const QString& g ) { m_guid = g; } void emitRunning();
void emitFinished() { emit finished(m_ownRef.toStrongRef()); emit finished(); } QWeakPointer< Tomahawk::DatabaseCommand > weakRef() const;
void emitCommitted() { emit committed(m_ownRef.toStrongRef()); emit committed(); }
void emitRunning() { emit running(m_ownRef.toStrongRef()); emit running(); }
QWeakPointer< Tomahawk::DatabaseCommand > weakRef();
void setWeakRef( QWeakPointer< Tomahawk::DatabaseCommand > weakRef ); void setWeakRef( QWeakPointer< Tomahawk::DatabaseCommand > weakRef );
signals: signals:
@@ -106,15 +102,13 @@ signals:
void committed(); void committed();
void committed( const Tomahawk::dbcmd_ptr& ); void committed( const Tomahawk::dbcmd_ptr& );
protected:
explicit DatabaseCommand( QObject* parent, DatabaseCommandPrivate* d );
QScopedPointer<DatabaseCommandPrivate> d_ptr;
private: private:
State m_state; Q_DECLARE_PRIVATE( DatabaseCommand )
Tomahawk::source_ptr m_source;
mutable QString m_guid;
QVariant m_data;
QWeakPointer< Tomahawk::DatabaseCommand > m_ownRef;
}; };
} }

View File

@@ -0,0 +1,59 @@
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
*
* Copyright 2010-2011, Christian Muehlhaeuser <muesli@tomahawk-player.org>
* Copyright 2010-2011, Jeff Mitchell <jeff@tomahawk-player.org>
* Copyright 2013, Uwe L. Korn <uwelk@xhochy.com>
*
* 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_P_H
#define DATABASECOMMAND_P_H
#include "DatabaseCommand.h"
namespace Tomahawk
{
class DatabaseCommandPrivate
{
public:
explicit DatabaseCommandPrivate( DatabaseCommand* q )
: q_ptr( q )
, state( DatabaseCommand::PENDING )
{
}
explicit DatabaseCommandPrivate( DatabaseCommand* q, const Tomahawk::source_ptr& src )
: q_ptr( q )
, source( src )
, state( DatabaseCommand::PENDING )
{
}
Q_DECLARE_PUBLIC( DatabaseCommand )
DatabaseCommand* q_ptr;
private:
Tomahawk::source_ptr source;
DatabaseCommand::State state;
mutable QString guid;
QVariant data;
QWeakPointer< Tomahawk::DatabaseCommand > ownRef;
};
} // Tomahawk
#endif // DATABASECOMMAND_P_H