From 06c8a8978cfe6da9f2e3e1cd2eda2833e0ebceeb Mon Sep 17 00:00:00 2001 From: Craig Drummond Date: Mon, 8 Jun 2015 20:26:13 +0100 Subject: [PATCH] - DirViewModel should handle MPD updates - When loose MPD connection, should clear view but not DB contents --- gui/folderpage.cpp | 24 +++--------------------- gui/folderpage.h | 5 ++--- gui/librarypage.cpp | 7 +------ gui/librarypage.h | 1 - gui/mainwindow.cpp | 19 +------------------ gui/mainwindow.h | 1 - gui/playlistspage.cpp | 6 ------ gui/playlistspage.h | 1 - models/dirviewmodel.cpp | 18 ++++++++++++++++++ models/dirviewmodel.h | 3 +++ models/sqllibrarymodel.cpp | 8 ++++++++ models/sqllibrarymodel.h | 1 + 12 files changed, 37 insertions(+), 57 deletions(-) diff --git a/gui/folderpage.cpp b/gui/folderpage.cpp index 6845688fc..2bb1abd05 100644 --- a/gui/folderpage.cpp +++ b/gui/folderpage.cpp @@ -90,32 +90,14 @@ void FolderPage::setEnabled(bool e) } DirViewModel::self()->setEnabled(e); - if (e) { - refresh(); + if (isVisible()) { + emit loadFolders(); + loaded=true; } else { loaded=false; } } -void FolderPage::refresh() -{ - view->goToTop(); - if (DirViewModel::self()->isEnabled()) { - if (!isVisible()) { - loaded=false; // Refresh called for, but we are not currently visible... - } else { - emit loadFolders(); - loaded=true; - } - } -} - -void FolderPage::clear() -{ - DirViewModel::self()->clear(); - loaded=false; -} - void FolderPage::showEvent(QShowEvent *e) { view->focusView(); diff --git a/gui/folderpage.h b/gui/folderpage.h index c3927b384..b202f4169 100644 --- a/gui/folderpage.h +++ b/gui/folderpage.h @@ -47,9 +47,8 @@ public: void setEnabled(bool e); bool isEnabled() const { return DirViewModel::self()->isEnabled(); } - void load() { if (!loaded) refresh(); } - void refresh(); - void clear(); + void load() { DirViewModel::self()->load(); } + void clear() { DirViewModel::self()->clear(); } QStringList selectedFiles(bool allowPlaylists=false) const; QList selectedSongs(EmptySongMod esMod, bool allowPlaylists=false) const; QList selectedSongs(bool allowPlaylists=false) const { return selectedSongs(ES_None, allowPlaylists); } diff --git a/gui/librarypage.cpp b/gui/librarypage.cpp index 786ced922..4356eee2a 100644 --- a/gui/librarypage.cpp +++ b/gui/librarypage.cpp @@ -84,14 +84,9 @@ void LibraryPage::showEvent(QShowEvent *e) QWidget::showEvent(e); } -void LibraryPage::refresh() -{ - view->goToTop(); - // TODO: Is this used?? -} - void LibraryPage::clear() { + MpdLibraryModel::self()->clear(); view->goToTop(); } diff --git a/gui/librarypage.h b/gui/librarypage.h index c9a06d692..4e7a6541b 100644 --- a/gui/librarypage.h +++ b/gui/librarypage.h @@ -37,7 +37,6 @@ public: LibraryPage(QWidget *p); virtual ~LibraryPage(); - void refresh(); void clear(); QStringList selectedFiles(bool allowPlaylists=false) const; QList selectedSongs(bool allowPlaylists=false) const; diff --git a/gui/mainwindow.cpp b/gui/mainwindow.cpp index 07db85f8c..5b4221a13 100644 --- a/gui/mainwindow.cpp +++ b/gui/mainwindow.cpp @@ -821,7 +821,6 @@ MainWindow::MainWindow(QWidget *parent) connect(&playQueueModel, SIGNAL(streamFetchStatus(QString)), playQueue, SLOT(streamFetchStatus(QString))); connect(playQueue, SIGNAL(cancelStreamFetch()), &playQueueModel, SLOT(cancelStreamFetch())); connect(playQueue, SIGNAL(itemsSelected(bool)), SLOT(playQueueItemsSelected(bool))); - connect(MPDStats::self(), SIGNAL(updated()), this, SLOT(updateStats())); connect(MPDStatus::self(), SIGNAL(updated()), this, SLOT(updateStatus())); connect(MPDConnection::self(), SIGNAL(playlistUpdated(const QList &, bool)), this, SLOT(updatePlayQueue(const QList &, bool))); connect(MPDConnection::self(), SIGNAL(currentSongUpdated(Song)), this, SLOT(updateCurrentSong(Song))); @@ -931,7 +930,6 @@ MainWindow::MainWindow(QWidget *parent) if (Settings::self()->firstRun() && MPDConnection::self()->isConnected()) { mpdConnectionStateChanged(true); - updateStats(); } #ifndef ENABLE_KDE_SUPPORT MediaKeys::self()->start(); @@ -1180,7 +1178,7 @@ void MainWindow::refreshDbPromp() void MainWindow::fullDbRefresh() { - MpdLibraryModel::self()->clear(); + MpdLibraryModel::self()->clearDb(); } #ifdef ENABLE_KDE_SUPPORT @@ -1784,21 +1782,6 @@ void MainWindow::scrollPlayQueue(bool wasEmpty) } } -void MainWindow::updateStats() -{ - // Check if remote db is more recent than local one -// if (0==MusicLibraryModel::self()->lastUpdate() || MPDStats::self()->dbUpdate() > MusicLibraryModel::self()->lastUpdate()) { -// if (0==MusicLibraryModel::self()->lastUpdate()) { -// libraryPage->clear(); -// folderPage->clear(); -// playlistsPage->clear(); -// } -// libraryPage->refresh(); -// folderPage->refresh(); -// } - // TODO???? -} - void MainWindow::updateStatus() { updateStatus(MPDStatus::self()); diff --git a/gui/mainwindow.h b/gui/mainwindow.h index a2c465b9e..9ff5b2742 100644 --- a/gui/mainwindow.h +++ b/gui/mainwindow.h @@ -199,7 +199,6 @@ public Q_SLOTS: void updatePlayQueue(const QList &songs, bool isComplete); void updateCurrentSong(Song song, bool wasEmpty=false); void scrollPlayQueue(bool wasEmpty=false); - void updateStats(); void updateStatus(); void playQueueItemActivated(const QModelIndex &); void promptClearPlayQueue(); diff --git a/gui/playlistspage.cpp b/gui/playlistspage.cpp index b66534071..008009b16 100644 --- a/gui/playlistspage.cpp +++ b/gui/playlistspage.cpp @@ -139,12 +139,6 @@ void PlaylistsPage::updateRows() view->updateRows(); } -void PlaylistsPage::refresh() -{ - view->goToTop(); - PlaylistsModel::self()->getPlaylists(); -} - void PlaylistsPage::clear() { PlaylistsModel::self()->clear(); diff --git a/gui/playlistspage.h b/gui/playlistspage.h index bd1fe037d..dd912a33f 100644 --- a/gui/playlistspage.h +++ b/gui/playlistspage.h @@ -41,7 +41,6 @@ public: void setStartClosed(bool sc); bool isStartClosed(); void updateRows(); - void refresh(); void clear(); //QStringList selectedFiles() const; void addSelectionToPlaylist(const QString &name=QString(), bool replace=false, quint8 priorty=0); diff --git a/models/dirviewmodel.cpp b/models/dirviewmodel.cpp index a9e67f333..b307e62a1 100644 --- a/models/dirviewmodel.cpp +++ b/models/dirviewmodel.cpp @@ -41,6 +41,7 @@ #include "roles.h" #include "gui/settings.h" #include "mpd-interface/mpdconnection.h" +#include "mpd-interface/mpdstats.h" #include "support/icon.h" #include "widgets/icons.h" #include "config.h" @@ -62,6 +63,7 @@ DirViewModel::DirViewModel(QObject *parent) #if defined ENABLE_MODEL_TEST new ModelTest(this, this); #endif + connect(this, SIGNAL(loadFolers()), MPDConnection::self(), SLOT(loadFolders())); } DirViewModel::~DirViewModel() @@ -78,9 +80,11 @@ void DirViewModel::setEnabled(bool e) if (enabled) { connect(MPDConnection::self(), SIGNAL(dirViewUpdated(DirViewItemRoot *, time_t)), this, SLOT(updateDirView(DirViewItemRoot *, time_t))); + connect(MPDStats::self(), SIGNAL(updated()), this, SLOT(mpdStatsUpdated())); } else { clear(); disconnect(MPDConnection::self(), SIGNAL(dirViewUpdated(DirViewItemRoot *, time_t)), this, SLOT(updateDirView(DirViewItemRoot *, time_t))); + disconnect(MPDStats::self(), SIGNAL(updated()), this, SLOT(mpdStatsUpdated())); } } @@ -214,6 +218,13 @@ QVariant DirViewModel::data(const QModelIndex &index, int role) const return QVariant(); } +void DirViewModel::load() +{ + if (enabled && (!rootItem || 0==rootItem->childCount())) { + emit loadFolers(); + } +} + void DirViewModel::clear() { if (!rootItem || 0==rootItem->childCount()) { @@ -266,6 +277,13 @@ void DirViewModel::updateDirView(DirViewItemRoot *newroot, time_t dbUpdate) #endif } +void DirViewModel::mpdStatsUpdated() +{ + if (0==databaseTime || MPDStats::self()->dbUpdate() > databaseTime) { + emit loadFolers(); + } +} + void DirViewModel::addFileToList(const QString &file, const QString &mopidyPath) { if (!enabled) { diff --git a/models/dirviewmodel.h b/models/dirviewmodel.h index 2da400abc..a79dd2d29 100644 --- a/models/dirviewmodel.h +++ b/models/dirviewmodel.h @@ -53,6 +53,7 @@ public: #ifndef ENABLE_UBUNTU QMimeData *mimeData(const QModelIndexList &indexes) const; #endif + void load(); void clear(); void addFileToList(const QString &file, const QString &mopidyPath); void removeFileFromList(const QString &file); @@ -61,9 +62,11 @@ public: public Q_SLOTS: void updateDirView(DirViewItemRoot *newroot, time_t dbUpdate=0); + void mpdStatsUpdated(); Q_SIGNALS: void updated(); + void loadFolers(); private: void toXML(const DirViewItem *item, QXmlStreamWriter &writer); diff --git a/models/sqllibrarymodel.cpp b/models/sqllibrarymodel.cpp index b4f40831f..3b4a8d275 100644 --- a/models/sqllibrarymodel.cpp +++ b/models/sqllibrarymodel.cpp @@ -70,6 +70,14 @@ SqlLibraryModel::SqlLibraryModel(LibraryDb *d, QObject *p) } void SqlLibraryModel::clear() +{ + beginResetModel(); + delete root; + root=0; + endResetModel(); +} + +void SqlLibraryModel::clearDb() { db->clear(); } diff --git a/models/sqllibrarymodel.h b/models/sqllibrarymodel.h index 71781eb62..06b24d87b 100644 --- a/models/sqllibrarymodel.h +++ b/models/sqllibrarymodel.h @@ -133,6 +133,7 @@ public: SqlLibraryModel(LibraryDb *d, QObject *p); void clear(); + void clearDb(); void settings(const QString &top, const QString &lib, const QString &al); Type topLevel() const { return tl; } void search(const QString &str);