From be707eaa3c9b086bcb1341dd8607c19f1dc790d1 Mon Sep 17 00:00:00 2001 From: "craig.p.drummond@gmail.com" Date: Mon, 20 Aug 2012 11:36:51 +0000 Subject: [PATCH] Fix track order when adding newly added album, via folders page, to playqueue. --- ChangeLog | 2 ++ gui/folderpage.cpp | 24 ++++++------------ gui/folderpage.h | 4 +-- models/dirviewmodel.cpp | 49 ++++++++++++++++++++++++++---------- models/dirviewmodel.h | 4 +-- models/musiclibrarymodel.cpp | 7 +++++- mpd/mpdconnection.cpp | 2 +- mpd/mpdconnection.h | 2 ++ 8 files changed, 58 insertions(+), 36 deletions(-) diff --git a/ChangeLog b/ChangeLog index d82a97bb2..0201115b4 100644 --- a/ChangeLog +++ b/ChangeLog @@ -34,6 +34,8 @@ 22. Allow 'showPage' dbus command to also show playqueue (if this has been placed in the sidebar) 23. Fix handling of filename's with quotes. +24. Fix track order when adding newly added album, via folders page, to + playqueue. 0.8.2 ----- diff --git a/gui/folderpage.cpp b/gui/folderpage.cpp index d31d48875..14d86224a 100644 --- a/gui/folderpage.cpp +++ b/gui/folderpage.cpp @@ -197,41 +197,31 @@ void FolderPage::openFileManager() } #endif -QList FolderPage::selectedSongs() const +QList FolderPage::selectedSongs(bool allowPlaylists) const { - const QModelIndexList selected = view->selectedIndexes(); - - if (0==selected.size()) { - return QList(); - } - - QModelIndexList mapped; - foreach (const QModelIndex &idx, selected) { - mapped.append(proxy.mapToSource(idx)); - } - - return MusicLibraryModel::self()->songs(DirViewModel::self()->filenames(mapped)); + return MusicLibraryModel::self()->songs(selectedFiles(allowPlaylists)); } -QStringList FolderPage::selectedFiles() const +QStringList FolderPage::selectedFiles(bool allowPlaylists) const { - const QModelIndexList selected = view->selectedIndexes(); + QModelIndexList selected = view->selectedIndexes(); if (0==selected.size()) { return QStringList(); } + qSort(selected); QModelIndexList mapped; foreach (const QModelIndex &idx, selected) { mapped.append(proxy.mapToSource(idx)); } - return DirViewModel::self()->filenames(mapped); + return DirViewModel::self()->filenames(mapped, allowPlaylists); } void FolderPage::addSelectionToPlaylist(const QString &name, bool replace, quint8 priorty) { - QStringList files=selectedFiles(); + QStringList files=selectedFiles(true); if (!files.isEmpty()) { if (name.isEmpty()) { diff --git a/gui/folderpage.h b/gui/folderpage.h index a5b77c7b2..af2923675 100644 --- a/gui/folderpage.h +++ b/gui/folderpage.h @@ -40,8 +40,8 @@ public: bool isEnabled() const { return DirViewModel::self()->isEnabled(); } void refresh(); void clear(); - QStringList selectedFiles() const; - QList selectedSongs() const; + QStringList selectedFiles(bool allowPlaylists=false) const; + QList selectedSongs(bool allowPlaylists=false) const; void addSelectionToPlaylist(const QString &name=QString(), bool replace=false, quint8 priorty=0); #ifdef ENABLE_DEVICES_SUPPORT void addSelectionToDevice(const QString &udi); diff --git a/models/dirviewmodel.cpp b/models/dirviewmodel.cpp index 4ed1adfd8..b7bef9ce7 100644 --- a/models/dirviewmodel.cpp +++ b/models/dirviewmodel.cpp @@ -314,13 +314,12 @@ Qt::ItemFlags DirViewModel::flags(const QModelIndex &index) const } } -QStringList DirViewModel::filenames(const QModelIndexList &indexes) const +QStringList DirViewModel::filenames(const QModelIndexList &indexes, bool allowPlaylists) const { QStringList fnames; foreach(QModelIndex index, indexes) { - DirViewItem *item = static_cast(index.internalPointer()); - recurseDirItems(*item, fnames); + getFiles(static_cast(index.internalPointer()), fnames, allowPlaylists); } return fnames; } @@ -328,7 +327,7 @@ QStringList DirViewModel::filenames(const QModelIndexList &indexes) const QMimeData *DirViewModel::mimeData(const QModelIndexList &indexes) const { QMimeData *mimeData = new QMimeData(); - QStringList files=filenames(indexes); + QStringList files=filenames(indexes, true); PlayQueueModel::encode(*mimeData, PlayQueueModel::constFileNameMimeType, files); if (!MPDConnection::self()->getDetails().dir.isEmpty()) { QStringList paths; @@ -340,15 +339,39 @@ QMimeData *DirViewModel::mimeData(const QModelIndexList &indexes) const return mimeData; } -void DirViewModel::recurseDirItems(DirViewItem &parent, QStringList &filenames) const +static inline void addFile(DirViewItem *item, QStringList &insertInto, QStringList &checkAgainst, bool allowPlaylists) { - if (parent.childCount() == 0) { - if (!filenames.contains(parent.fullName())) { - filenames << parent.fullName(); - } - } else { - for (int i = 0; i < parent.childCount(); i++) { - recurseDirItems(*parent.child(i), filenames); - } + QString path=item->fullName(); + if ((allowPlaylists || !MPDConnection::isPlaylist(path)) && !checkAgainst.contains(path)) { + insertInto << path; + } +} + +void DirViewModel::getFiles(DirViewItem *item, QStringList &filenames, bool allowPlaylists) const +{ + if (!item) { + return; + } + + switch (item->type()) { + case DirViewItem::Type_File: + addFile(item, filenames, filenames, allowPlaylists); + break; + case DirViewItem::Type_Dir: { + QStringList dirFiles; + for (int c=0; cchildCount(); c++) { + DirViewItem *child=item->child(c); + if (DirViewItem::Type_Dir==child->type()) { + getFiles(child, filenames, allowPlaylists); + } else if (DirViewItem::Type_File==child->type()) { + addFile(child, dirFiles, filenames, allowPlaylists); + } + } + + qSort(dirFiles); + filenames+=dirFiles; + } + default: + break; } } diff --git a/models/dirviewmodel.h b/models/dirviewmodel.h index 5264c4fe3..15e17c84e 100644 --- a/models/dirviewmodel.h +++ b/models/dirviewmodel.h @@ -47,7 +47,7 @@ public: int columnCount(const QModelIndex &) const; QVariant data(const QModelIndex &, int) const; Qt::ItemFlags flags(const QModelIndex &index) const; - QStringList filenames(const QModelIndexList &indexes) const; + QStringList filenames(const QModelIndexList &indexes, bool allowPlaylists) const; QMimeData *mimeData(const QModelIndexList &indexes) const; void clear(); void addFileToList(const QString &file); @@ -61,7 +61,7 @@ public Q_SLOTS: private: void addFileToList(const QStringList &parts, const QModelIndex &parent, DirViewItemDir *dir); void removeFileFromList(const QStringList &parts, const QModelIndex &parent, DirViewItemDir *dir); - void recurseDirItems(DirViewItem &parent, QStringList &filenames) const; + void getFiles(DirViewItem *item, QStringList &filenames, bool allowPlaylists) const; private: DirViewItemRoot *rootItem; diff --git a/models/musiclibrarymodel.cpp b/models/musiclibrarymodel.cpp index d67ac8f0f..da34ec4f5 100644 --- a/models/musiclibrarymodel.cpp +++ b/models/musiclibrarymodel.cpp @@ -719,9 +719,14 @@ QList MusicLibraryModel::songs(const QModelIndexList &indexes, bool allowP QList MusicLibraryModel::songs(const QStringList &filenames) const { - QSet files=filenames.toSet(); QList songs; + if (filenames.isEmpty()) { + return songs; + } + + QSet files=filenames.toSet(); + foreach (const MusicLibraryItem *artist, static_cast(rootItem)->childItems()) { foreach (const MusicLibraryItem *album, static_cast(artist)->childItems()) { foreach (const MusicLibraryItem *song, static_cast(album)->childItems()) { diff --git a/mpd/mpdconnection.cpp b/mpd/mpdconnection.cpp index 4cb5215a2..bac9cda44 100644 --- a/mpd/mpdconnection.cpp +++ b/mpd/mpdconnection.cpp @@ -502,7 +502,7 @@ MPDConnection::Response MPDConnection::sendCommand(const QByteArray &command, bo /* * Playlist commands */ -static bool isPlaylist(const QString &file) +bool MPDConnection::isPlaylist(const QString &file) { return file.endsWith("asx", Qt::CaseInsensitive) || file.endsWith("cue", Qt::CaseInsensitive) || file.endsWith("m3u", Qt::CaseInsensitive) || file.endsWith("pls", Qt::CaseInsensitive) || diff --git a/mpd/mpdconnection.h b/mpd/mpdconnection.h index a83213f81..be1479547 100644 --- a/mpd/mpdconnection.h +++ b/mpd/mpdconnection.h @@ -177,6 +177,8 @@ public: bool isConnected() const { return State_Connected==state; } bool canUsePriority() const { return ver>=MPD_MAKE_VERSION(0, 17, 0); } + static bool isPlaylist(const QString &file); + public Q_SLOTS: void reconnect(); void setDetails(const MPDConnectionDetails &det);