diff --git a/src/libtomahawk/CMakeLists.txt b/src/libtomahawk/CMakeLists.txt index dba6721d1..47d503e98 100644 --- a/src/libtomahawk/CMakeLists.txt +++ b/src/libtomahawk/CMakeLists.txt @@ -337,6 +337,7 @@ list(APPEND libSources sip/SipInfo.cpp sip/PeerInfo.cpp sip/SipStatusMessage.cpp + sip/WeakPeerHash.cpp utils/TomahawkUtils.cpp utils/Logger.cpp diff --git a/src/libtomahawk/sip/PeerInfo.cpp b/src/libtomahawk/sip/PeerInfo.cpp index 405174e4d..f122ba2ae 100644 --- a/src/libtomahawk/sip/PeerInfo.cpp +++ b/src/libtomahawk/sip/PeerInfo.cpp @@ -20,6 +20,7 @@ #include "PeerInfo_p.h" #include "SipPlugin.h" +#include "WeakPeerHash.h" #include "utils/TomahawkCache.h" #include "utils/TomahawkUtilsGui.h" #include "network/ControlConnection.h" @@ -32,7 +33,7 @@ namespace Tomahawk { -QHash< QString, peerinfo_wptr > PeerInfo::s_peersByCacheKey = QHash< QString, peerinfo_wptr >(); +WeakPeerHash PeerInfo::s_peersByCacheKey = WeakPeerHash(); QHash< SipPlugin*, peerinfo_ptr > PeerInfo::s_selfPeersBySipPlugin = QHash< SipPlugin*, peerinfo_ptr >(); @@ -79,9 +80,9 @@ Tomahawk::peerinfo_ptr PeerInfo::get( SipPlugin* parent, const QString& id, GetOptions options ) { const QString key = peerCacheKey( parent, id ); - if ( s_peersByCacheKey.contains( key ) && !s_peersByCacheKey.value( key ).isNull() ) + if ( s_peersByCacheKey.hash().contains( key ) && !s_peersByCacheKey.hash().value( key ).isNull() ) { - return s_peersByCacheKey.value( key ).toStrongRef(); + return s_peersByCacheKey.hash().value( key ).toStrongRef(); } // if AutoCreate isn't enabled nothing to do here @@ -92,7 +93,7 @@ PeerInfo::get( SipPlugin* parent, const QString& id, GetOptions options ) peerinfo_ptr peerInfo( new PeerInfo( parent, id ) ); peerInfo->setWeakRef( peerInfo.toWeakRef() ); - s_peersByCacheKey.insert( key, peerInfo.toWeakRef() ); + s_peersByCacheKey.insert( key, peerInfo ); return peerInfo; } @@ -102,7 +103,7 @@ QList< Tomahawk::peerinfo_ptr > PeerInfo::getAll() { QList< Tomahawk::peerinfo_ptr > strongRefs; - foreach ( Tomahawk::peerinfo_wptr wptr, s_peersByCacheKey.values() ) + foreach ( Tomahawk::peerinfo_wptr wptr, s_peersByCacheKey.hash().values() ) { if ( !wptr.isNull() ) strongRefs << wptr.toStrongRef(); @@ -122,8 +123,6 @@ PeerInfo::PeerInfo( SipPlugin* parent, const QString& id ) PeerInfo::~PeerInfo() { -// tDebug() << Q_FUNC_INFO; - s_peersByCacheKey.remove( s_peersByCacheKey.key( weakRef() ) ); delete m_avatar; delete m_fancyAvatar; delete d_ptr; diff --git a/src/libtomahawk/sip/PeerInfo.h b/src/libtomahawk/sip/PeerInfo.h index 12ffbf703..44be6847d 100644 --- a/src/libtomahawk/sip/PeerInfo.h +++ b/src/libtomahawk/sip/PeerInfo.h @@ -30,6 +30,7 @@ class ControlConnection; class SipPlugin; class SipInfo; +class WeakPeerHash; namespace Tomahawk { @@ -129,7 +130,7 @@ private: Q_DECLARE_PRIVATE( Tomahawk::PeerInfo ) Tomahawk::PeerInfoPrivate* d_ptr; - static QHash< QString, peerinfo_wptr > s_peersByCacheKey; + static WeakPeerHash s_peersByCacheKey; static QHash< SipPlugin*, peerinfo_ptr > s_selfPeersBySipPlugin; mutable QPixmap* m_avatar; diff --git a/src/libtomahawk/sip/WeakPeerHash.cpp b/src/libtomahawk/sip/WeakPeerHash.cpp new file mode 100644 index 000000000..a6e451845 --- /dev/null +++ b/src/libtomahawk/sip/WeakPeerHash.cpp @@ -0,0 +1,57 @@ +/* === This file is part of Tomahawk Player - === + * + * Copyright 2013, Uwe L. Korn + * + * 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 . + */ + +#include "WeakPeerHash_p.h" + +#include "PeerInfo.h" + +#define WEAKPEERHASH_KEY "WeakPeerHashKey" + +WeakPeerHash::WeakPeerHash(QObject *parent) + : QObject(parent) + , d_ptr( new WeakPeerHashPrivate( this ) ) +{ +} + +WeakPeerHash::WeakPeerHash( const WeakPeerHash &hash ) + : QObject( hash.parent() ) + , d_ptr( new WeakPeerHashPrivate( this ) ) +{ + d_func()->hash = hash.hash(); +} + +void +WeakPeerHash::insert(const QString &key, const Tomahawk::peerinfo_ptr &value) +{ + value->setProperty( WEAKPEERHASH_KEY, key ); + connect( value.data(), SIGNAL( destroyed( QObject* ) ), SLOT( remove( QObject* ) ) ); + d_func()->hash.insert( key, value.toWeakRef() ); +} + +const QHash& +WeakPeerHash::hash() +{ + return d_func()->hash; +} + +void +WeakPeerHash::remove( QObject *value ) +{ + const QString key = value->property( WEAKPEERHASH_KEY ).toString(); + d_func()->hash.remove( key ); +} diff --git a/src/libtomahawk/sip/WeakPeerHash.h b/src/libtomahawk/sip/WeakPeerHash.h new file mode 100644 index 000000000..f9f724f23 --- /dev/null +++ b/src/libtomahawk/sip/WeakPeerHash.h @@ -0,0 +1,47 @@ +/* === This file is part of Tomahawk Player - === + * + * Copyright 2013, Uwe L. Korn + * + * 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 . + */ + +#ifndef WEAKPEERHASH_H +#define WEAKPEERHASH_H + +#include "Typedefs.h" + +#include + +class WeakPeerHashPrivate; + +class WeakPeerHash : public QObject +{ + Q_OBJECT +public: + WeakPeerHash( QObject *parent = 0 ); + WeakPeerHash( const WeakPeerHash& hash ); + void insert( const QString& key, const Tomahawk::peerinfo_ptr& value ); + const QHash< QString, Tomahawk::peerinfo_wptr>& hash(); + +signals: + +private slots: + void remove( QObject* value ); +private: + Q_DECLARE_PRIVATE( WeakPeerHash ) + WeakPeerHashPrivate* d_ptr; + +}; + +#endif // WEAKPEERHASH_H diff --git a/src/libtomahawk/sip/WeakPeerHash_p.h b/src/libtomahawk/sip/WeakPeerHash_p.h new file mode 100644 index 000000000..ee4e80e07 --- /dev/null +++ b/src/libtomahawk/sip/WeakPeerHash_p.h @@ -0,0 +1,39 @@ +/* === This file is part of Tomahawk Player - === + * + * Copyright 2013, Uwe L. Korn + * + * 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 . + */ + +#ifndef WEAKPEERHASH_P_H +#define WEAKPEERHASH_P_H + +#include "WeakPeerHash.h" + +class WeakPeerHashPrivate +{ +public: + WeakPeerHashPrivate( WeakPeerHash* q ) + : q_ptr ( q ) + { + } + WeakPeerHash* q_ptr; + Q_DECLARE_PUBLIC ( WeakPeerHash ) + +private: + QHash< QString, Tomahawk::peerinfo_wptr > hash; +}; + + +#endif // WEAKPEERHASH_P_H