1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-08-08 23:26:40 +02:00

Properly handle delegates so that a) we don't crash and b) we actually

show the proper delegates for items when there are more than two in the
queue. This pretty much makes the job view stuff fully work, except
somehow saving got broken.
This commit is contained in:
Jeff Mitchell
2012-06-17 18:52:07 -04:00
parent 10bb33590b
commit 554cb5d7a1
6 changed files with 65 additions and 35 deletions

View File

@@ -103,14 +103,14 @@ ACLRegistry::~ACLRegistry()
ACLRegistry::ACL ACLRegistry::ACL
ACLRegistry::isAuthorizedUser( const QString& dbid, const QString &username, ACLRegistry::ACL globalType, bool skipEmission ) ACLRegistry::isAuthorizedUser( const QString& dbid, const QString &username, ACLRegistry::ACL globalType, bool skipEmission )
{ {
tDebug() << Q_FUNC_INFO; tLog() << Q_FUNC_INFO;
if ( QThread::currentThread() != TOMAHAWK_APPLICATION::instance()->thread() ) if ( QThread::currentThread() != TOMAHAWK_APPLICATION::instance()->thread() )
{ {
if ( !skipEmission ) if ( !skipEmission )
QMetaObject::invokeMethod( this, "isAuthorizedUser", Qt::QueuedConnection, Q_ARG( const QString&, dbid ), Q_ARG( const QString &, username ), Q_ARG( ACLRegistry::ACL, globalType ), Q_ARG( bool, skipEmission ) ); QMetaObject::invokeMethod( this, "isAuthorizedUser", Qt::QueuedConnection, Q_ARG( const QString&, dbid ), Q_ARG( const QString &, username ), Q_ARG( ACLRegistry::ACL, globalType ), Q_ARG( bool, skipEmission ) );
return ACLRegistry::NotFound; return ACLRegistry::NotFound;
} }
tDebug() << Q_FUNC_INFO << "in right thread"; tLog() << Q_FUNC_INFO << "in right thread";
//FIXME: Remove when things are working //FIXME: Remove when things are working
// emit aclResult( dbid, username, ACLRegistry::Stream ); // emit aclResult( dbid, username, ACLRegistry::Stream );
// return ACLRegistry::NotFound; // return ACLRegistry::NotFound;
@@ -176,35 +176,40 @@ ACLRegistry::isAuthorizedUser( const QString& dbid, const QString &username, ACL
void void
ACLRegistry::getUserDecision( ACLRegistry::User user, const QString &username ) ACLRegistry::getUserDecision( ACLRegistry::User user, const QString &username )
{ {
tDebug() << Q_FUNC_INFO; tLog() << Q_FUNC_INFO;
AclJobItem* job = new AclJobItem( user, username ); AclJobItem* job = new AclJobItem( user, username );
m_jobQueue.enqueue( job ); m_jobQueue.enqueue( job );
queueNextJob(); QTimer::singleShot( 0, this, SLOT( queueNextJob() ) );
} }
void void
ACLRegistry::userDecision( ACLRegistry::User user ) ACLRegistry::userDecision( ACLRegistry::User user )
{ {
tDebug() << Q_FUNC_INFO; tLog() << Q_FUNC_INFO;
m_cache.append( user ); m_cache.append( user );
save(); save();
emit aclResult( user.knownDbids.first(), user.knownAccountIds.first(), user.acl ); emit aclResult( user.knownDbids.first(), user.knownAccountIds.first(), user.acl );
m_jobCount--; m_jobCount--;
if ( !m_jobQueue.isEmpty() ) if ( !m_jobQueue.isEmpty() )
queueNextJob(); QTimer::singleShot( 0, this, SLOT( queueNextJob() ) );
} }
void void
ACLRegistry::queueNextJob() ACLRegistry::queueNextJob()
{ {
tDebug() << Q_FUNC_INFO << "jobCount = " << m_jobCount; if ( QThread::currentThread() != TOMAHAWK_APPLICATION::instance()->thread() )
{
QMetaObject::invokeMethod( this, "queueNextJob", Qt::QueuedConnection );
return;
}
tLog() << Q_FUNC_INFO << "jobCount = " << m_jobCount;
tLog() << Q_FUNC_INFO << "jobQueue size = " << m_jobQueue.length();
if ( m_jobCount != 0 ) if ( m_jobCount != 0 )
return; return;
tDebug() << Q_FUNC_INFO << "jobQueue size = " << m_jobQueue.length();
if ( !m_jobQueue.isEmpty() ) if ( !m_jobQueue.isEmpty() )
{ {
AclJobItem* job = m_jobQueue.dequeue(); AclJobItem* job = m_jobQueue.dequeue();
@@ -215,21 +220,21 @@ ACLRegistry::queueNextJob()
ACLRegistry::ACL acl = isAuthorizedUser( dbid, job->username(), ACLRegistry::NotFound, true ); ACLRegistry::ACL acl = isAuthorizedUser( dbid, job->username(), ACLRegistry::NotFound, true );
if ( acl != ACLRegistry::NotFound ) if ( acl != ACLRegistry::NotFound )
{ {
tDebug() << Q_FUNC_INFO << "Found existing acl entry for = " << user.knownAccountIds.first(); tLog() << Q_FUNC_INFO << "Found existing acl entry for = " << user.knownAccountIds.first();
found = true; found = true;
break; break;
} }
} }
if ( found ) if ( found )
{ {
tDebug() << Q_FUNC_INFO << "deleting job, already have ACL for " << user.knownAccountIds.first(); tLog() << Q_FUNC_INFO << "deleting job, already have ACL for " << user.knownAccountIds.first();
delete job; delete job;
QTimer::singleShot( 0, this, SLOT( queueNextJob() ) ); QTimer::singleShot( 0, this, SLOT( queueNextJob() ) );
return; return;
} }
else else
{ {
tDebug() << Q_FUNC_INFO << "activating job for user" << user.knownAccountIds.first(); tLog() << Q_FUNC_INFO << "activating job for user" << user.knownAccountIds.first();
m_jobCount++; m_jobCount++;
JobStatusView::instance()->model()->addJob( job ); JobStatusView::instance()->model()->addJob( job );
connect( job, SIGNAL( userDecision( ACLRegistry::User ) ), this, SLOT( userDecision( ACLRegistry::User ) ) ); connect( job, SIGNAL( userDecision( ACLRegistry::User ) ), this, SLOT( userDecision( ACLRegistry::User ) ) );
@@ -243,13 +248,13 @@ ACLRegistry::queueNextJob()
void void
ACLRegistry::load() ACLRegistry::load()
{ {
tDebug() << Q_FUNC_INFO; tLog() << Q_FUNC_INFO;
QVariantList entryList = TomahawkSettings::instance()->aclEntries(); QVariantList entryList = TomahawkSettings::instance()->aclEntries();
foreach ( QVariant entry, entryList ) foreach ( QVariant entry, entryList )
{ {
if ( !entry.isValid() || !entry.canConvert< ACLRegistry::User >() ) if ( !entry.isValid() || !entry.canConvert< ACLRegistry::User >() )
continue; continue;
tDebug() << Q_FUNC_INFO << "loading entry"; tLog() << Q_FUNC_INFO << "loading entry";
ACLRegistry::User entryUser = entry.value< ACLRegistry::User >(); ACLRegistry::User entryUser = entry.value< ACLRegistry::User >();
if ( entryUser.knownAccountIds.empty() || entryUser.knownDbids.empty() ) if ( entryUser.knownAccountIds.empty() || entryUser.knownDbids.empty() )
continue; continue;
@@ -261,11 +266,11 @@ ACLRegistry::load()
void void
ACLRegistry::save() ACLRegistry::save()
{ {
tDebug() << Q_FUNC_INFO; tLog() << Q_FUNC_INFO;
QVariantList entryList; QVariantList entryList;
foreach ( ACLRegistry::User user, m_cache ) foreach ( ACLRegistry::User user, m_cache )
{ {
tDebug() << Q_FUNC_INFO << "user is " << user.uuid << " with known name " << user.knownAccountIds.first(); tLog() << Q_FUNC_INFO << "user is " << user.uuid << " with known name " << user.knownAccountIds.first();
QVariant val = QVariant::fromValue< ACLRegistry::User >( user ); QVariant val = QVariant::fromValue< ACLRegistry::User >( user );
if ( val.isValid() ) if ( val.isValid() )
entryList.append( val ); entryList.append( val );

View File

@@ -40,6 +40,13 @@
AclJobDelegate::AclJobDelegate( QObject* parent ) AclJobDelegate::AclJobDelegate( QObject* parent )
: QStyledItemDelegate ( parent ) : QStyledItemDelegate ( parent )
{ {
tLog() << Q_FUNC_INFO;
}
AclJobDelegate::~AclJobDelegate()
{
tLog() << Q_FUNC_INFO;
} }
@@ -148,17 +155,22 @@ AclJobItem::AclJobItem( ACLRegistry::User user, const QString &username )
, m_user( user ) , m_user( user )
, m_username( username ) , m_username( username )
{ {
tLog() << Q_FUNC_INFO;
} }
AclJobItem::~AclJobItem() AclJobItem::~AclJobItem()
{ {
tLog() << Q_FUNC_INFO;
if ( m_delegate )
delete m_delegate;
} }
void void
AclJobItem::createDelegate( QObject* parent ) AclJobItem::createDelegate( QObject* parent )
{ {
tLog() << Q_FUNC_INFO;
if ( m_delegate ) if ( m_delegate )
return; return;
@@ -179,15 +191,9 @@ AclJobDelegate::emitSizeHintChanged( const QModelIndex& index )
void void
AclJobItem::aclResult( ACLRegistry::ACL result ) AclJobItem::aclResult( ACLRegistry::ACL result )
{ {
tLog() << Q_FUNC_INFO;
m_user.acl = result; m_user.acl = result;
emit userDecision( m_user ); emit userDecision( m_user );
done();
}
void
AclJobItem::done()
{
emit finished(); emit finished();
} }

View File

@@ -33,7 +33,7 @@ class AclJobDelegate : public QStyledItemDelegate
public: public:
explicit AclJobDelegate ( QObject* parent = 0 ); explicit AclJobDelegate ( QObject* parent = 0 );
virtual ~AclJobDelegate() {} virtual ~AclJobDelegate();
virtual void paint( QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index ) const; virtual void paint( QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index ) const;
virtual QSize sizeHint( const QStyleOptionViewItem& option, const QModelIndex& index ) const; virtual QSize sizeHint( const QStyleOptionViewItem& option, const QModelIndex& index ) const;
@@ -63,8 +63,6 @@ public:
explicit AclJobItem( ACLRegistry::User user, const QString &username ); explicit AclJobItem( ACLRegistry::User user, const QString &username );
virtual ~AclJobItem(); virtual ~AclJobItem();
void done();
virtual QString rightColumnText() const { return QString(); } virtual QString rightColumnText() const { return QString(); }
virtual QString mainText() const { return QString(); } virtual QString mainText() const { return QString(); }
virtual QPixmap icon() const { return QPixmap(); } virtual QPixmap icon() const { return QPixmap(); }

View File

@@ -41,6 +41,7 @@ JobStatusModel::~JobStatusModel()
void void
JobStatusModel::addJob( JobStatusItem* item ) JobStatusModel::addJob( JobStatusItem* item )
{ {
tLog() << Q_FUNC_INFO << "current jobs of item type: " << m_jobTypeCount[ item->type() ] << ", current queue size of item type: " << m_jobQueue[ item->type() ].size();
if ( item->concurrentJobLimit() > 0 ) if ( item->concurrentJobLimit() > 0 )
{ {
if ( m_jobTypeCount[ item->type() ] >= item->concurrentJobLimit() ) if ( m_jobTypeCount[ item->type() ] >= item->concurrentJobLimit() )
@@ -53,6 +54,8 @@ JobStatusModel::addJob( JobStatusItem* item )
m_jobTypeCount[ item->type() ] = currentJobCount; m_jobTypeCount[ item->type() ] = currentJobCount;
} }
tLog() << Q_FUNC_INFO << "new current jobs of item type: " << m_jobTypeCount[ item->type() ];
connect( item, SIGNAL( statusChanged() ), SLOT( itemUpdated() ) ); connect( item, SIGNAL( statusChanged() ), SLOT( itemUpdated() ) );
connect( item, SIGNAL( finished() ), SLOT( itemFinished() ) ); connect( item, SIGNAL( finished() ), SLOT( itemFinished() ) );
@@ -70,7 +73,7 @@ JobStatusModel::addJob( JobStatusItem* item )
} }
} }
qDebug() << "Adding item:" << item; tLog() << Q_FUNC_INFO << "Adding item:" << item;
int currentEndRow = m_items.count(); int currentEndRow = m_items.count();
beginInsertRows( QModelIndex(), currentEndRow, currentEndRow ); beginInsertRows( QModelIndex(), currentEndRow, currentEndRow );
@@ -79,7 +82,7 @@ JobStatusModel::addJob( JobStatusItem* item )
if ( item->hasCustomDelegate() ) if ( item->hasCustomDelegate() )
{ {
tDebug( LOGVERBOSE ) << Q_FUNC_INFO << "job has custom delegate"; tLog() << Q_FUNC_INFO << "job has custom delegate";
emit customDelegateJobInserted( currentEndRow, item ); emit customDelegateJobInserted( currentEndRow, item );
} }
} }
@@ -200,6 +203,8 @@ JobStatusModel::itemFinished()
if ( item->customDelegate() ) if ( item->customDelegate() )
emit customDelegateJobRemoved( idx ); emit customDelegateJobRemoved( idx );
tLog() << Q_FUNC_INFO << "current jobs of item type: " << m_jobTypeCount[ item->type() ] << ", current queue size of item type: " << m_jobQueue[ item->type() ].size();
if ( item->concurrentJobLimit() > 0 ) if ( item->concurrentJobLimit() > 0 )
{ {
int currentJobs = m_jobTypeCount[ item->type() ]; int currentJobs = m_jobTypeCount[ item->type() ];

View File

@@ -100,22 +100,24 @@ JobStatusView::setModel( JobStatusModel* m )
void void
JobStatusView::customDelegateJobInserted( int row, JobStatusItem* item ) JobStatusView::customDelegateJobInserted( int row, JobStatusItem* item )
{ {
tDebug( LOGVERBOSE ) << Q_FUNC_INFO; tLog() << Q_FUNC_INFO << "item is " << item << ", row is " << row;
if ( !item ) if ( !item )
return; return;
tDebug( LOGVERBOSE ) << Q_FUNC_INFO << "telling item to create delegate"; tLog() << Q_FUNC_INFO << "telling item to create delegate";
item->createDelegate( m_view ); item->createDelegate( m_view );
tDebug( LOGVERBOSE ) << Q_FUNC_INFO << "item delegate is " << item->customDelegate(); tLog() << Q_FUNC_INFO << "item delegate is " << item->customDelegate();
m_view->setItemDelegateForRow( row, item->customDelegate() ); m_view->setItemDelegateForRow( row, item->customDelegate() );
m_customDelegateRefCounter[ row ] = m_customDelegateRefCounter[ row ] + 1;
AclJobDelegate* delegate = qobject_cast< AclJobDelegate* >( item->customDelegate() ); AclJobDelegate* delegate = qobject_cast< AclJobDelegate* >( item->customDelegate() );
if ( delegate ) if ( delegate )
{ {
tLog() << Q_FUNC_INFO << "delegate found";
connect( delegate, SIGNAL( update( const QModelIndex& ) ), m_view, SLOT( update( const QModelIndex & ) ) ); connect( delegate, SIGNAL( update( const QModelIndex& ) ), m_view, SLOT( update( const QModelIndex & ) ) );
connect( delegate, SIGNAL( aclResult( ACLRegistry::ACL ) ), item, SLOT( aclResult( ACLRegistry::ACL ) ) ); connect( delegate, SIGNAL( aclResult( ACLRegistry::ACL ) ), item, SLOT( aclResult( ACLRegistry::ACL ) ) );
delegate->emitSizeHintChanged( m_model->index( row ) ); delegate->emitSizeHintChanged( m_model->index( row ) );
} }
else
tLog() << Q_FUNC_INFO << "delegate was not properly found!";
checkCount(); checkCount();
} }
@@ -124,9 +126,24 @@ JobStatusView::customDelegateJobInserted( int row, JobStatusItem* item )
void void
JobStatusView::customDelegateJobRemoved( int row ) JobStatusView::customDelegateJobRemoved( int row )
{ {
if ( m_customDelegateRefCounter[ row ] == 1 ) tLog() << Q_FUNC_INFO << "row is " << row;
m_view->setItemDelegateForRow( row, m_view->itemDelegate() ); int count = m_model->rowCount();
m_customDelegateRefCounter[ row ] = m_customDelegateRefCounter[ row ] - 1; for ( int i = 0; i < count; i++ )
{
tLog() << Q_FUNC_INFO << "checking row " << i;
QModelIndex index = m_model->index( i );
QVariant itemVar = index.data( JobStatusModel::JobDataRole );
if ( !itemVar.canConvert< JobStatusItem* >() || !itemVar.value< JobStatusItem* >() )
{
tLog() << Q_FUNC_INFO << "unable to fetch JobStatusItem* at row " << i;
continue;
}
JobStatusItem* item = itemVar.value< JobStatusItem* >();
if ( item->hasCustomDelegate() )
m_view->setItemDelegateForRow( i, item->customDelegate() );
else
m_view->setItemDelegateForRow( i, m_view->itemDelegate() );
}
checkCount(); checkCount();
} }

View File

@@ -61,7 +61,6 @@ private:
JobStatusModel* m_model; JobStatusModel* m_model;
AnimatedSplitter* m_parent; AnimatedSplitter* m_parent;
mutable int m_cachedHeight; mutable int m_cachedHeight;
QHash< int, int > m_customDelegateRefCounter;
static JobStatusView* s_instance; static JobStatusView* s_instance;
}; };