From 82e135167a5d24f600f006480b78a59511ae5cb3 Mon Sep 17 00:00:00 2001 From: Yoann Lopes Date: Thu, 8 Oct 2015 18:26:39 +0200 Subject: Change the way a playlist is bound to a media object. The previous behavior was to simply switch from the internal control to the service's control, discarding anything that was added to the playlist before binding. We now carry over the changes made to the playlist when switching controls. This means the switch is now transparent to the user. When the service's control is read-only, we cannot transfer the items, which means the user must be notified of the items that might have been "lost" during the switch. Auto-test modified to reflect this change. Change-Id: Ibf80b650b06425ddbaeb320b72ac5d3082a25960 Reviewed-by: Jim Hodapp Reviewed-by: Yoann Lopes --- src/multimedia/playback/qmediaplaylist.cpp | 72 ++++++++++++++++++++++++++---- 1 file changed, 64 insertions(+), 8 deletions(-) (limited to 'src/multimedia/playback/qmediaplaylist.cpp') diff --git a/src/multimedia/playback/qmediaplaylist.cpp b/src/multimedia/playback/qmediaplaylist.cpp index 06813592e..c63340637 100644 --- a/src/multimedia/playback/qmediaplaylist.cpp +++ b/src/multimedia/playback/qmediaplaylist.cpp @@ -168,10 +168,13 @@ bool QMediaPlaylist::setMediaObject(QMediaObject *mediaObject) newControl = d->networkPlaylistControl; if (d->control != newControl) { - int oldSize = 0; + int removedStart = -1; + int removedEnd = -1; + int insertedStart = -1; + int insertedEnd = -1; + if (d->control) { QMediaPlaylistProvider *playlist = d->control->playlistProvider(); - oldSize = playlist->mediaCount(); disconnect(playlist, SIGNAL(loadFailed(QMediaPlaylist::Error,QString)), this, SLOT(_q_loadFailed(QMediaPlaylist::Error,QString))); @@ -190,6 +193,12 @@ bool QMediaPlaylist::setMediaObject(QMediaObject *mediaObject) disconnect(d->control, SIGNAL(currentMediaChanged(QMediaContent)), this, SIGNAL(currentMediaChanged(QMediaContent))); + // Copy playlist items, sync playback mode and sync current index between + // old control and new control + d->syncControls(d->control, newControl, + &removedStart, &removedEnd, + &insertedStart, &insertedEnd); + if (d->mediaObject) d->mediaObject->service()->releaseControl(d->control); } @@ -214,14 +223,14 @@ bool QMediaPlaylist::setMediaObject(QMediaObject *mediaObject) connect(d->control, SIGNAL(currentMediaChanged(QMediaContent)), this, SIGNAL(currentMediaChanged(QMediaContent))); - if (oldSize) { - emit mediaAboutToBeRemoved(0, oldSize-1); - emit mediaRemoved(0, oldSize-1); + if (removedStart != -1 && removedEnd != -1) { + emit mediaAboutToBeRemoved(removedStart, removedEnd); + emit mediaRemoved(removedStart, removedEnd); } - if (playlist->mediaCount()) { - emit mediaAboutToBeInserted(0,playlist->mediaCount()-1); - emit mediaInserted(0,playlist->mediaCount()-1); + if (insertedStart != -1 && insertedEnd != -1) { + emit mediaAboutToBeInserted(insertedStart, insertedEnd); + emit mediaInserted(insertedStart, insertedEnd); } } @@ -435,6 +444,53 @@ bool QMediaPlaylistPrivate::writeItems(QMediaPlaylistWriter *writer) return true; } +/*! + * \internal + * Copy playlist items, sync playback mode and sync current index between old control and new control +*/ +void QMediaPlaylistPrivate::syncControls(QMediaPlaylistControl *oldControl, QMediaPlaylistControl *newControl, + int *removedStart, int *removedEnd, + int *insertedStart, int *insertedEnd) +{ + Q_ASSERT(oldControl != NULL && newControl != NULL); + Q_ASSERT(removedStart != NULL && removedEnd != NULL + && insertedStart != NULL && insertedEnd != NULL); + + QMediaPlaylistProvider *oldPlaylist = oldControl->playlistProvider(); + QMediaPlaylistProvider *newPlaylist = newControl->playlistProvider(); + + Q_ASSERT(oldPlaylist != NULL && newPlaylist != NULL); + + *removedStart = -1; + *removedEnd = -1; + *insertedStart = -1; + *insertedEnd = -1; + + if (newPlaylist->isReadOnly()) { + // we can't transfer the items from the old control. + // Report these items as removed. + if (oldPlaylist->mediaCount() > 0) { + *removedStart = 0; + *removedEnd = oldPlaylist->mediaCount() - 1; + } + // The new control might have some items that can't be cleared. + // Report these as inserted. + if (newPlaylist->mediaCount() > 0) { + *insertedStart = 0; + *insertedEnd = newPlaylist->mediaCount() - 1; + } + } else { + const int oldPlaylistSize = oldPlaylist->mediaCount(); + + newPlaylist->clear(); + for (int i = 0; i < oldPlaylistSize; ++i) + newPlaylist->addMedia(oldPlaylist->media(i)); + } + + newControl->setPlaybackMode(oldControl->playbackMode()); + newControl->setCurrentIndex(oldControl->currentIndex()); +} + /*! Load playlist using network \a request. If \a format is specified, it is used, otherwise format is guessed from playlist name and data. -- cgit v1.2.3