From bf3c5d4952efeee478d3f6acd200f7fdd5433f43 Mon Sep 17 00:00:00 2001 From: Dominik Schmidt Date: Mon, 12 Jan 2015 01:01:28 +0100 Subject: [PATCH] Properly handle online state for database and scriptcollections --- src/libtomahawk/DropJob.cpp | 3 +-- src/libtomahawk/Query.cpp | 4 +-- src/libtomahawk/Result.cpp | 10 +++---- src/libtomahawk/collection/Collection.cpp | 11 ++++++-- src/libtomahawk/collection/Collection.h | 17 +++++++++--- .../database/DatabaseCollection.cpp | 11 ++++++++ src/libtomahawk/database/DatabaseCollection.h | 5 ++++ .../database/DatabaseCommand_AllAlbums.cpp | 4 +-- .../database/DatabaseCommand_AllAlbums.h | 3 ++- .../database/DatabaseCommand_AllArtists.cpp | 2 +- .../database/DatabaseCommand_AllArtists.h | 3 ++- .../database/DatabaseCommand_AllTracks.h | 5 ++-- .../database/DatabaseCommand_Resolve.cpp | 2 +- .../filemetadata/MetadataEditor.cpp | 4 +-- src/libtomahawk/playlist/AlbumModel.cpp | 13 ++-------- src/libtomahawk/playlist/TreeModel.cpp | 4 --- src/libtomahawk/playlist/TreeProxyModel.cpp | 4 +-- .../resolvers/ScriptCollection.cpp | 26 +++++++++++++++++++ src/libtomahawk/resolvers/ScriptCollection.h | 3 +++ .../plugins/ScriptCollectionFactory.cpp | 2 ++ .../viewpages/CollectionViewPage.cpp | 2 +- 21 files changed, 95 insertions(+), 43 deletions(-) diff --git a/src/libtomahawk/DropJob.cpp b/src/libtomahawk/DropJob.cpp index 3df245313..13421b011 100644 --- a/src/libtomahawk/DropJob.cpp +++ b/src/libtomahawk/DropJob.cpp @@ -975,8 +975,7 @@ DropJob::removeRemoteSources() foreach ( const Tomahawk::result_ptr& result, item->results() ) { - if ( !result->resolvedByCollection().isNull() && !result->resolvedByCollection()->source().isNull() - && result->resolvedByCollection()->source()->isLocal() ) + if ( !result->resolvedByCollection().isNull() && !result->resolvedByCollection()->isLocal() ) { list.append( item ); break; diff --git a/src/libtomahawk/Query.cpp b/src/libtomahawk/Query.cpp index 8dd9b4d9d..c3a8e92a1 100644 --- a/src/libtomahawk/Query.cpp +++ b/src/libtomahawk/Query.cpp @@ -409,11 +409,11 @@ Query::resultSorter( const result_ptr& left, const result_ptr& right ) if ( ls == rs ) { -/* if ( left->collection() && left->collection()->source()->isLocal() ) +/* if ( left->collection() && left->collection()->isLocal() ) { return true; }*/ - if ( right->resolvedByCollection() && right->resolvedByCollection()->source()->isLocal() ) + if ( right->resolvedByCollection() && right->resolvedByCollection()->isLocal() ) { return false; } diff --git a/src/libtomahawk/Result.cpp b/src/libtomahawk/Result.cpp index 486b5fc1b..39fd92e6b 100644 --- a/src/libtomahawk/Result.cpp +++ b/src/libtomahawk/Result.cpp @@ -191,7 +191,7 @@ Result::isOnline() const { if ( !resolvedByCollection().isNull() ) { - return resolvedByCollection()->source()->isOnline(); + return resolvedByCollection()->isOnline(); } else { @@ -205,7 +205,7 @@ Result::playable() const { if ( resolvedByCollection() ) { - return resolvedByCollection()->source()->isOnline(); + return resolvedByCollection()->isOnline(); } else { @@ -305,8 +305,8 @@ Result::setResolvedByCollection( const Tomahawk::collection_ptr& collection , bo { Q_ASSERT( !collection.isNull() ); connect( collection.data(), SIGNAL( destroyed( QObject * ) ), SLOT( onOffline() ), Qt::QueuedConnection ); - connect( collection->source().data(), SIGNAL( online() ), SLOT( onOnline() ), Qt::QueuedConnection ); - connect( collection->source().data(), SIGNAL( offline() ), SLOT( onOffline() ), Qt::QueuedConnection ); + connect( collection.data(), SIGNAL( online() ), SLOT( onOnline() ), Qt::QueuedConnection ); + connect( collection.data(), SIGNAL( offline() ), SLOT( onOffline() ), Qt::QueuedConnection ); } } @@ -391,7 +391,7 @@ Result::friendlySource() const return m_friendlySource; } else - return resolvedByCollection()->source()->friendlyName(); + return resolvedByCollection()->prettyName(); } diff --git a/src/libtomahawk/collection/Collection.cpp b/src/libtomahawk/collection/Collection.cpp index 55dfb3759..5537a3c97 100644 --- a/src/libtomahawk/collection/Collection.cpp +++ b/src/libtomahawk/collection/Collection.cpp @@ -119,8 +119,15 @@ Collection::bigIcon() const } -const -source_ptr& Collection::source() const +bool +Collection::isLocal() const +{ + return false; +} + + +const source_ptr +Collection::source() const { return m_source; } diff --git a/src/libtomahawk/collection/Collection.h b/src/libtomahawk/collection/Collection.h index 225ad3097..6c345262c 100644 --- a/src/libtomahawk/collection/Collection.h +++ b/src/libtomahawk/collection/Collection.h @@ -82,6 +82,9 @@ public: virtual const QPixmap icon( const QSize& size ) const override; virtual QPixmap bigIcon() const; //for the ViewPage header + virtual bool isOnline() const = 0; + virtual bool isLocal() const; + virtual void loadPlaylists(); virtual void loadAutoPlaylists(); virtual void loadStations(); @@ -108,7 +111,6 @@ public: virtual Tomahawk::AlbumsRequest* requestAlbums( const Tomahawk::artist_ptr& artist ) = 0; virtual Tomahawk::TracksRequest* requestTracks( const Tomahawk::album_ptr& album ) = 0; - const source_ptr& source() const; unsigned int lastmodified() const { return m_lastmodified; } virtual int trackCount() const; @@ -128,6 +130,9 @@ signals: void changed(); + void online(); + void offline(); + public slots: void setPlaylists( const QList& plists ); void setAutoPlaylists( const QList< Tomahawk::dynplaylist_ptr >& autoplists ); @@ -137,14 +142,18 @@ public slots: void delTracks( const QList& fileids ); protected: - QString m_name; - unsigned int m_lastmodified; // unix time of last change to collection - QSet< BrowseCapability > m_browseCapabilities; + const source_ptr source() const; private slots: void onSynced(); void doLoadPlaylistUpdater( const playlist_ptr& p ); + +protected: + QString m_name; + unsigned int m_lastmodified; // unix time of last change to collection + QSet< BrowseCapability > m_browseCapabilities; + private: bool m_changed; diff --git a/src/libtomahawk/database/DatabaseCollection.cpp b/src/libtomahawk/database/DatabaseCollection.cpp index d60bac1b1..36c59f075 100644 --- a/src/libtomahawk/database/DatabaseCollection.cpp +++ b/src/libtomahawk/database/DatabaseCollection.cpp @@ -43,6 +43,17 @@ DatabaseCollection::DatabaseCollection( const source_ptr& src, QObject* parent ) << CapabilityBrowseArtists << CapabilityBrowseAlbums << CapabilityBrowseTracks; + + + connect( source().data(), SIGNAL( online() ), SIGNAL( online() ) ); + connect( source().data(), SIGNAL( offline() ), SIGNAL( offline() ) ); +} + + +bool +DatabaseCollection::isOnline() const +{ + return source()->isOnline(); } diff --git a/src/libtomahawk/database/DatabaseCollection.h b/src/libtomahawk/database/DatabaseCollection.h index 0d02355f8..61ea9c294 100644 --- a/src/libtomahawk/database/DatabaseCollection.h +++ b/src/libtomahawk/database/DatabaseCollection.h @@ -45,6 +45,11 @@ public: BackendType backendType() const override { return DatabaseCollectionType; } + bool isOnline() const override; + bool isLocal() const override { return source()->isLocal(); } + + using Collection::source; + void loadPlaylists() override; void loadAutoPlaylists() override; void loadStations() override; diff --git a/src/libtomahawk/database/DatabaseCommand_AllAlbums.cpp b/src/libtomahawk/database/DatabaseCommand_AllAlbums.cpp index ee8288b2b..9c7e83e4c 100644 --- a/src/libtomahawk/database/DatabaseCommand_AllAlbums.cpp +++ b/src/libtomahawk/database/DatabaseCommand_AllAlbums.cpp @@ -35,7 +35,7 @@ namespace Tomahawk DatabaseCommand_AllAlbums::DatabaseCommand_AllAlbums( const Tomahawk::collection_ptr& collection, const Tomahawk::artist_ptr& artist, QObject* parent ) : DatabaseCommand( parent ) - , m_collection( collection ) + , m_collection( collection.objectCast< DatabaseCollection >() ) , m_artist( artist ) , m_amount( 0 ) , m_sortOrder( DatabaseCommand_AllAlbums::None ) @@ -74,7 +74,7 @@ DatabaseCommand_AllAlbums::execForArtist( DatabaseImpl* dbi ) } if ( !m_collection.isNull() ) - sourceToken = QString( "AND file.source %1" ).arg( m_collection->source()->isLocal() ? "IS NULL" : QString( "= %1" ).arg( m_collection->source()->id() ) ); + sourceToken = QString( "AND file.source %1" ).arg( m_collection->isLocal() ? "IS NULL" : QString( "= %1" ).arg( m_collection->source()->id() ) ); if ( !m_filter.isEmpty() ) { diff --git a/src/libtomahawk/database/DatabaseCommand_AllAlbums.h b/src/libtomahawk/database/DatabaseCommand_AllAlbums.h index d343b6a68..098cbdce2 100644 --- a/src/libtomahawk/database/DatabaseCommand_AllAlbums.h +++ b/src/libtomahawk/database/DatabaseCommand_AllAlbums.h @@ -30,6 +30,7 @@ #include "Typedefs.h" #include "DatabaseCommand.h" #include "Database.h" +#include "DatabaseCollection.h" #include "DllMacro.h" @@ -74,7 +75,7 @@ signals: void done(); private: - Tomahawk::collection_ptr m_collection; + QSharedPointer< DatabaseCollection > m_collection; Tomahawk::artist_ptr m_artist; unsigned int m_amount; diff --git a/src/libtomahawk/database/DatabaseCommand_AllArtists.cpp b/src/libtomahawk/database/DatabaseCommand_AllArtists.cpp index bc294ae30..b260c3235 100644 --- a/src/libtomahawk/database/DatabaseCommand_AllArtists.cpp +++ b/src/libtomahawk/database/DatabaseCommand_AllArtists.cpp @@ -33,7 +33,7 @@ namespace Tomahawk DatabaseCommand_AllArtists::DatabaseCommand_AllArtists( const Tomahawk::collection_ptr& collection, QObject* parent ) : DatabaseCommand( parent ) - , m_collection( collection ) + , m_collection( collection.objectCast< DatabaseCollection >() ) , m_amount( 0 ) , m_sortOrder( DatabaseCommand_AllArtists::None ) , m_sortDescending( false ) diff --git a/src/libtomahawk/database/DatabaseCommand_AllArtists.h b/src/libtomahawk/database/DatabaseCommand_AllArtists.h index fbeff57a9..6943b0e7e 100644 --- a/src/libtomahawk/database/DatabaseCommand_AllArtists.h +++ b/src/libtomahawk/database/DatabaseCommand_AllArtists.h @@ -29,6 +29,7 @@ #include "Typedefs.h" #include "DatabaseCommand.h" #include "Database.h" +#include "DatabaseCollection.h" #include "DllMacro.h" @@ -65,7 +66,7 @@ signals: void done(); private: - Tomahawk::collection_ptr m_collection; + QSharedPointer< DatabaseCollection > m_collection; unsigned int m_amount; DatabaseCommand_AllArtists::SortOrder m_sortOrder; bool m_sortDescending; diff --git a/src/libtomahawk/database/DatabaseCommand_AllTracks.h b/src/libtomahawk/database/DatabaseCommand_AllTracks.h index 4d1869b30..0053517c5 100644 --- a/src/libtomahawk/database/DatabaseCommand_AllTracks.h +++ b/src/libtomahawk/database/DatabaseCommand_AllTracks.h @@ -24,6 +24,7 @@ #include "DatabaseCommand.h" #include "Database.h" +#include "DatabaseCollection.h" #include "collection/Collection.h" #include "collection/TracksRequest.h" #include "Typedefs.h" @@ -49,7 +50,7 @@ public: explicit DatabaseCommand_AllTracks( const Tomahawk::collection_ptr& collection = Tomahawk::collection_ptr(), QObject* parent = nullptr ) : DatabaseCommand( parent ) - , m_collection( collection ) + , m_collection( collection.objectCast< DatabaseCollection >() ) , m_artist( nullptr ) , m_album( nullptr ) , m_amount( 0 ) @@ -77,7 +78,7 @@ signals: void done( const Tomahawk::collection_ptr& ); private: - Tomahawk::collection_ptr m_collection; + QSharedPointer< DatabaseCollection > m_collection; Tomahawk::artist_ptr m_artist; Tomahawk::album_ptr m_album; diff --git a/src/libtomahawk/database/DatabaseCommand_Resolve.cpp b/src/libtomahawk/database/DatabaseCommand_Resolve.cpp index 89de13781..80ccfd20f 100644 --- a/src/libtomahawk/database/DatabaseCommand_Resolve.cpp +++ b/src/libtomahawk/database/DatabaseCommand_Resolve.cpp @@ -59,7 +59,7 @@ DatabaseCommand_Resolve::exec( DatabaseImpl* lib ) tDebug() << "Using result-hint to speed up resolving:" << m_query->resultHint(); Tomahawk::result_ptr result = lib->resultFromHint( m_query ); - if ( result && ( !result->resolvedByCollection() || result->resolvedByCollection()->source()->isOnline() ) ) + if ( result && ( !result->resolvedByCollection() || result->resolvedByCollection()->isOnline() ) ) { QList res; res << result; diff --git a/src/libtomahawk/filemetadata/MetadataEditor.cpp b/src/libtomahawk/filemetadata/MetadataEditor.cpp index 6ae49d499..c0f1da109 100644 --- a/src/libtomahawk/filemetadata/MetadataEditor.cpp +++ b/src/libtomahawk/filemetadata/MetadataEditor.cpp @@ -240,7 +240,7 @@ MetadataEditor::loadResult( const Tomahawk::result_ptr& result ) return; m_result = result; - setEditable( result->resolvedByCollection() && result->resolvedByCollection()->source()->isLocal() ); + setEditable( result->resolvedByCollection() && result->resolvedByCollection()->isLocal() ); setTitle( result->track()->track() ); setArtist( result->track()->artist() ); @@ -250,7 +250,7 @@ MetadataEditor::loadResult( const Tomahawk::result_ptr& result ) setYear( result->track()->year() ); setBitrate( result->bitrate() ); - if ( result->resolvedByCollection() && result->resolvedByCollection()->source()->isLocal() ) + if ( result->resolvedByCollection() && result->resolvedByCollection()->isLocal() ) { QString furl = m_result->url(); if ( furl.startsWith( "file://" ) ) diff --git a/src/libtomahawk/playlist/AlbumModel.cpp b/src/libtomahawk/playlist/AlbumModel.cpp index a144215de..abc237a96 100644 --- a/src/libtomahawk/playlist/AlbumModel.cpp +++ b/src/libtomahawk/playlist/AlbumModel.cpp @@ -49,10 +49,6 @@ AlbumModel::~AlbumModel() void AlbumModel::addCollection( const collection_ptr& collection, bool overwrite ) { - qDebug() << Q_FUNC_INFO << collection->name() - << collection->source()->id() - << collection->source()->nodeId(); - DatabaseCommand_AllAlbums* cmd = new DatabaseCommand_AllAlbums( collection ); m_overwriteOnAdd = overwrite; m_collection = collection; @@ -62,7 +58,7 @@ AlbumModel::addCollection( const collection_ptr& collection, bool overwrite ) Database::instance()->enqueue( Tomahawk::dbcmd_ptr( cmd ) ); - setTitle( tr( "All albums from %1" ).arg( collection->source()->friendlyName() ) ); + setTitle( tr( "All albums from %1" ).arg( collection->prettyName() ) ); if ( collection.isNull() ) { @@ -86,11 +82,6 @@ AlbumModel::addCollection( const collection_ptr& collection, bool overwrite ) void AlbumModel::addFilteredCollection( const collection_ptr& collection, unsigned int amount, DatabaseCommand_AllAlbums::SortOrder order, bool overwrite ) { -/* qDebug() << Q_FUNC_INFO << collection->name() - << collection->source()->id() - << collection->source()->nodeId() - << amount << order;*/ - DatabaseCommand_AllAlbums* cmd = new DatabaseCommand_AllAlbums( collection ); cmd->setLimit( amount ); cmd->setSortOrder( order ); @@ -104,7 +95,7 @@ AlbumModel::addFilteredCollection( const collection_ptr& collection, unsigned in Database::instance()->enqueue( Tomahawk::dbcmd_ptr( cmd ) ); if ( !collection.isNull() ) - setTitle( tr( "All albums from %1" ).arg( collection->source()->friendlyName() ) ); + setTitle( tr( "All albums from %1" ).arg( collection->prettyName() ) ); else setTitle( tr( "All albums" ) ); diff --git a/src/libtomahawk/playlist/TreeModel.cpp b/src/libtomahawk/playlist/TreeModel.cpp index f5814be59..397af44a9 100644 --- a/src/libtomahawk/playlist/TreeModel.cpp +++ b/src/libtomahawk/playlist/TreeModel.cpp @@ -221,10 +221,6 @@ TreeModel::addTracks( const album_ptr& album, const QModelIndex& parent ) void TreeModel::addCollection( const collection_ptr& collection ) { - qDebug() << Q_FUNC_INFO << collection->name() - << collection->source()->id() - << collection->source()->nodeId(); - startLoading(); m_collection = collection; diff --git a/src/libtomahawk/playlist/TreeProxyModel.cpp b/src/libtomahawk/playlist/TreeProxyModel.cpp index 75541bf63..9f4817668 100644 --- a/src/libtomahawk/playlist/TreeProxyModel.cpp +++ b/src/libtomahawk/playlist/TreeProxyModel.cpp @@ -238,8 +238,8 @@ TreeProxyModel::filterAcceptsRow( int sourceRow, const QModelIndex& sourceParent if ( !item->result()->isOnline() && ti->result()->isOnline() ) return false; - if ( ( item->result()->resolvedByCollection().isNull() || !item->result()->resolvedByCollection()->source()->isLocal() ) && - !ti->result()->resolvedByCollection().isNull() && ti->result()->resolvedByCollection()->source()->isLocal() ) + if ( ( item->result()->resolvedByCollection().isNull() || !item->result()->resolvedByCollection()->isLocal() ) && + !ti->result()->resolvedByCollection().isNull() && ti->result()->resolvedByCollection()->isLocal() ) { return false; } diff --git a/src/libtomahawk/resolvers/ScriptCollection.cpp b/src/libtomahawk/resolvers/ScriptCollection.cpp index 2daf6ce64..54d351d53 100644 --- a/src/libtomahawk/resolvers/ScriptCollection.cpp +++ b/src/libtomahawk/resolvers/ScriptCollection.cpp @@ -44,6 +44,7 @@ ScriptCollection::ScriptCollection( const scriptobject_ptr& scriptObject, , ScriptPlugin( scriptObject ) , m_scriptAccount( scriptAccount ) , m_trackCount( -1 ) //null value + , m_isOnline( true ) { Q_ASSERT( scriptAccount ); qDebug() << Q_FUNC_INFO << scriptAccount->name() << Collection::name(); @@ -87,6 +88,31 @@ ScriptCollection::itemName() const } +bool ScriptCollection::isOnline() const +{ + return m_isOnline; +} + + +void +ScriptCollection::setOnline( bool isOnline ) +{ + m_isOnline = isOnline; + + if ( isOnline ) + { + emit online(); + } + else + { + emit offline(); + } + + + emit changed(); +} + + void ScriptCollection::setIcon( const QPixmap& icon ) { diff --git a/src/libtomahawk/resolvers/ScriptCollection.h b/src/libtomahawk/resolvers/ScriptCollection.h index c70cffca7..588436ff3 100644 --- a/src/libtomahawk/resolvers/ScriptCollection.h +++ b/src/libtomahawk/resolvers/ScriptCollection.h @@ -55,6 +55,8 @@ public: virtual ~ScriptCollection(); ScriptAccount* scriptAccount() const; + bool isOnline() const override; + void setOnline( bool isOnline ); /** * @brief setServiceName sets the name of the service that provides the ScriptCollection. @@ -98,6 +100,7 @@ private: QString m_description; int m_trackCount; QPixmap m_icon; + bool m_isOnline; }; } //ns diff --git a/src/libtomahawk/resolvers/plugins/ScriptCollectionFactory.cpp b/src/libtomahawk/resolvers/plugins/ScriptCollectionFactory.cpp index 69cbfbe86..471fb965c 100644 --- a/src/libtomahawk/resolvers/plugins/ScriptCollectionFactory.cpp +++ b/src/libtomahawk/resolvers/plugins/ScriptCollectionFactory.cpp @@ -27,11 +27,13 @@ void ScriptCollectionFactory::addPlugin( const QSharedPointer& // FIXME: no need for the same javascript call, already done in createPlugin collection->parseMetaData(); + collection->setOnline( true ); SourceList::instance()->addScriptCollection( collection ); } void ScriptCollectionFactory::removePlugin( const QSharedPointer& collection ) const { + collection->setOnline( false ); SourceList::instance()->removeScriptCollection( collection ); } diff --git a/src/libtomahawk/viewpages/CollectionViewPage.cpp b/src/libtomahawk/viewpages/CollectionViewPage.cpp index 7b971499a..69916f607 100644 --- a/src/libtomahawk/viewpages/CollectionViewPage.cpp +++ b/src/libtomahawk/viewpages/CollectionViewPage.cpp @@ -409,7 +409,7 @@ CollectionViewPage::onCollectionChanged() flatModel->appendTracks( m_collection ); albumModel->appendAlbums( m_collection ); - if ( m_collection && m_collection->source() && m_collection->source()->isLocal() ) + if ( m_collection && m_collection->isLocal() ) { setEmptyTip( tr( "After you have scanned your music collection you will find your tracks right here." ) ); }