From eb265df04e7bd0bdefbce8354326c552eb6ffb10 Mon Sep 17 00:00:00 2001 From: RJ Ryan Date: Tue, 21 Oct 2014 15:58:06 -0400 Subject: Some NULL-safety changes. --- src/dlgcoverartfullsize.cpp | 4 ++++ src/dlgtrackinfo.cpp | 19 +++++++++++++++---- src/library/coverartcache.cpp | 4 ++-- src/library/coverartdelegate.cpp | 23 ++++++++++++++++------- src/library/coverartdelegate.h | 7 +++---- src/mixxx.cpp | 10 ++++++---- src/widget/wcoverart.cpp | 33 +++++++++++++++++++++++---------- src/widget/wcoverart.h | 1 - src/widget/wcoverartmenu.cpp | 25 +++++++++++++++++++------ src/widget/wtracktableview.cpp | 7 +++++-- 10 files changed, 93 insertions(+), 40 deletions(-) (limited to 'src') diff --git a/src/dlgcoverartfullsize.cpp b/src/dlgcoverartfullsize.cpp index bb3a05743d..debd0bc480 100644 --- a/src/dlgcoverartfullsize.cpp +++ b/src/dlgcoverartfullsize.cpp @@ -20,6 +20,10 @@ void DlgCoverArtFullSize::init(CoverInfo info) { } CoverArtCache* cache = CoverArtCache::instance(); + if (cache == NULL) { + return; + } + if (info.coverLocation.isEmpty()) { info.coverLocation = cache->trackInDBHash(info.trackId); } diff --git a/src/dlgtrackinfo.cpp b/src/dlgtrackinfo.cpp index 29cc411d10..d8dd78bff9 100644 --- a/src/dlgtrackinfo.cpp +++ b/src/dlgtrackinfo.cpp @@ -80,8 +80,11 @@ void DlgTrackInfo::init(){ m_bpmTapFilter[i] = 0.0f; } - connect(CoverArtCache::instance(), SIGNAL(pixmapFound(int, QPixmap)), - this, SLOT(slotPixmapFound(int, QPixmap))); + CoverArtCache* pCache = CoverArtCache::instance(); + if (pCache != NULL) { + connect(pCache, SIGNAL(pixmapFound(int, QPixmap)), + this, SLOT(slotPixmapFound(int, QPixmap))); + } connect(m_pWCoverArtLabel, SIGNAL(coverLocationUpdated(const QString&, const QString&, QPixmap)), this, @@ -187,7 +190,10 @@ void DlgTrackInfo::loadTrack(TrackPointer pTrack, CoverInfo info) { connect(pTrack.data(), SIGNAL(changed(TrackInfoObject*)), this, SLOT(updateTrackMetadata())); - CoverArtCache::instance()->requestPixmap(info); + CoverArtCache* pCache = CoverArtCache::instance(); + if (pCache != NULL) { + pCache->requestPixmap(info); + } } void DlgTrackInfo::slotPixmapFound(int trackId, QPixmap pixmap) { @@ -365,7 +371,12 @@ void DlgTrackInfo::saveTrack() { m_pLoadedTrack->removeCue(pCue); } - bool res = CoverArtCache::instance()->changeCoverArt( + CoverArtCache* pCache = CoverArtCache::instance(); + if (pCache == NULL) { + return; + } + + bool res = pCache->changeCoverArt( m_pLoadedTrack->getId(), m_loadedCover.coverLocation); if (!res) { diff --git a/src/library/coverartcache.cpp b/src/library/coverartcache.cpp index c039729e96..cddea8327f 100644 --- a/src/library/coverartcache.cpp +++ b/src/library/coverartcache.cpp @@ -51,7 +51,7 @@ QString CoverArtCache::trackInDBHash(int trackId) { bool CoverArtCache::changeCoverArt(int trackId, const QString& newCoverLocation) { - if (trackId < 1) { + if (trackId < 1 || m_pCoverArtDAO == NULL || m_pTrackDAO == NULL) { return false; } @@ -99,7 +99,7 @@ QPixmap CoverArtCache::requestPixmap(CoverInfo info, const QSize& croppedSize, const bool onlyCached, const bool issueRepaint) { - if (info.trackId < 1) { + if (info.trackId < 1 || m_pCoverArtDAO == NULL) { return QPixmap(); } diff --git a/src/library/coverartdelegate.cpp b/src/library/coverartdelegate.cpp index a619b06681..3b9104a514 100644 --- a/src/library/coverartdelegate.cpp +++ b/src/library/coverartdelegate.cpp @@ -1,16 +1,19 @@ -#include - #include "library/coverartdelegate.h" +#include "library/coverartcache.h" #include "library/dao/trackdao.h" CoverArtDelegate::CoverArtDelegate(QObject *parent) : QStyledItemDelegate(parent), - m_pCoverCache(CoverArtCache::instance()), m_bOnlyCachedCover(false), - m_sDefaultCover(m_pCoverCache->getDefaultCoverLocation()), m_iCoverLocationColumn(-1), m_iCoverHashColumn(-1), + m_iTrackLocationColumn(-1), m_iIdColumn(-1) { + CoverArtCache* pCache = CoverArtCache::instance(); + if (pCache != NULL) { + m_sDefaultCover = pCache->getDefaultCoverLocation(); + } + // This assumes that the parent is wtracktableview connect(parent, SIGNAL(onlyCachedCoverArt(bool)), this, SLOT(slotOnlyCachedCoverArt(bool))); @@ -63,6 +66,11 @@ void CoverArtDelegate::paint(QPainter *painter, QString coverLocation = index.sibling( index.row(), m_iCoverLocationColumn).data().toString(); + CoverArtCache* pCache = CoverArtCache::instance(); + if (pCache == NULL) { + return; + } + // drawing only an existing cover_art, // otherwise leave it blank... if (coverLocation != m_sDefaultCover) { @@ -74,11 +82,12 @@ void CoverArtDelegate::paint(QPainter *painter, info.trackLocation = index.sibling( index.row(), m_iTrackLocationColumn).data().toString(); QSize coverSize(100, option.rect.height()); - QPixmap pixmap = m_pCoverCache->requestPixmap(info, coverSize, - m_bOnlyCachedCover, true); + + QPixmap pixmap = pCache->requestPixmap(info, coverSize, + m_bOnlyCachedCover, true); if (info.coverLocation.isEmpty()) { - info.coverLocation = m_pCoverCache->trackInDBHash(info.trackId); + info.coverLocation = pCache->trackInDBHash(info.trackId); } if (!pixmap.isNull() && info.coverLocation != m_sDefaultCover) { diff --git a/src/library/coverartdelegate.h b/src/library/coverartdelegate.h index 5a5d85c680..cfca4b7f7d 100644 --- a/src/library/coverartdelegate.h +++ b/src/library/coverartdelegate.h @@ -1,15 +1,15 @@ #ifndef COVERARTDELEGATE_H #define COVERARTDELEGATE_H +#include +#include #include #include -#include "library/coverartcache.h" #include "library/trackmodel.h" class CoverArtDelegate : public QStyledItemDelegate { - Q_OBJECT - + Q_OBJECT public: explicit CoverArtDelegate(QObject* parent = NULL); virtual ~CoverArtDelegate(); @@ -32,7 +32,6 @@ class CoverArtDelegate : public QStyledItemDelegate { void slotOnlyCachedCoverArt(bool b); private: - CoverArtCache* m_pCoverCache; bool m_bOnlyCachedCover; QString m_sDefaultCover; int m_iCoverLocationColumn; diff --git a/src/mixxx.cpp b/src/mixxx.cpp index 63f601fe5c..a780d11e39 100644 --- a/src/mixxx.cpp +++ b/src/mixxx.cpp @@ -297,10 +297,12 @@ MixxxMainWindow::MixxxMainWindow(QApplication* pApp, const CmdlineArgs& args) } } - CoverArtCache::instance()->setCoverArtDAO( - &m_pLibrary->getTrackCollection()->getCoverArtDAO()); - CoverArtCache::instance()->setTrackDAO( - &m_pLibrary->getTrackCollection()->getTrackDAO()); + CoverArtCache* pCache = CoverArtCache::instance(); + if (pCache != NULL) { + pCache->setCoverArtDAO( + &m_pLibrary->getTrackCollection()->getCoverArtDAO()); + pCache->setTrackDAO(&m_pLibrary->getTrackCollection()->getTrackDAO()); + } // Call inits to invoke all other construction parts diff --git a/src/widget/wcoverart.cpp b/src/widget/wcoverart.cpp index 082746b258..599f888501 100644 --- a/src/widget/wcoverart.cpp +++ b/src/widget/wcoverart.cpp @@ -13,17 +13,19 @@ WCoverArt::WCoverArt(QWidget* parent, TrackCollection* pTrackCollection) : QWidget(parent), WBaseWidget(this), - m_pCoverCache(CoverArtCache::instance()), m_bEnableWidget(true), m_pMenu(new WCoverArtMenu(this)), - m_loadedCover(m_pCoverCache->getDefaultCoverArt()), - m_loadedCoverScaled(scaledCoverArt(m_loadedCover)), m_trackDAO(pTrackCollection->getTrackDAO()), m_pDlgFullSize(new DlgCoverArtFullSize()) { setSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding); - connect(m_pCoverCache, SIGNAL(pixmapFound(int, QPixmap)), - this, SLOT(slotPixmapFound(int, QPixmap)), Qt::DirectConnection); + CoverArtCache* pCache = CoverArtCache::instance(); + if (pCache != NULL) { + connect(pCache, SIGNAL(pixmapFound(int, QPixmap)), + this, SLOT(slotPixmapFound(int, QPixmap)), Qt::DirectConnection); + m_loadedCover = pCache->getDefaultCoverArt(); + m_loadedCoverScaled = scaledCoverArt(m_loadedCover); + } connect(m_pMenu, SIGNAL(coverLocationUpdated(const QString&, const QString&, QPixmap)), this, @@ -64,7 +66,9 @@ void WCoverArt::slotCoverLocationUpdated(const QString& newLoc, QPixmap px) { Q_UNUSED(oldLoc); Q_UNUSED(px); - if (!m_pCoverCache->changeCoverArt(m_lastRequestedCover.trackId, newLoc)) { + CoverArtCache* pCache = CoverArtCache::instance(); + if (pCache != NULL && !pCache->changeCoverArt( + m_lastRequestedCover.trackId, newLoc)) { // parent must be NULL - it ensures the use of the default style. QMessageBox::warning(NULL, tr("Change Cover Art"), tr("Could not change the cover art.")); @@ -82,8 +86,11 @@ void WCoverArt::slotEnableWidget(bool enable) { void WCoverArt::slotResetWidget() { m_lastRequestedCover = CoverInfo(); - m_loadedCover = m_pCoverCache->getDefaultCoverArt(); - m_loadedCoverScaled = scaledCoverArt(m_loadedCover); + CoverArtCache* pCache = CoverArtCache::instance(); + if (pCache != NULL) { + m_loadedCover = pCache->getDefaultCoverArt(); + m_loadedCoverScaled = scaledCoverArt(m_loadedCover); + } update(); } @@ -103,7 +110,10 @@ void WCoverArt::slotLoadCoverArt(CoverInfo info, bool cachedOnly) { return; } m_lastRequestedCover = info; - m_pCoverCache->requestPixmap(info, QSize(0,0), cachedOnly); + CoverArtCache* pCache = CoverArtCache::instance(); + if (pCache != NULL) { + pCache->requestPixmap(info, QSize(0,0), cachedOnly); + } } QPixmap WCoverArt::scaledCoverArt(QPixmap normal) { @@ -135,7 +145,10 @@ void WCoverArt::resizeEvent(QResizeEvent*) { setMaximumHeight(h); } if (m_lastRequestedCover.trackId < 1) { - m_loadedCover = m_pCoverCache->getDefaultCoverArt(); + CoverArtCache* pCache = CoverArtCache::instance(); + if (pCache != NULL) { + m_loadedCover = pCache->getDefaultCoverArt(); + } } m_loadedCoverScaled = scaledCoverArt(m_loadedCover); } diff --git a/src/widget/wcoverart.h b/src/widget/wcoverart.h index 6e0fa12f42..98ef4b3f7b 100644 --- a/src/widget/wcoverart.h +++ b/src/widget/wcoverart.h @@ -42,7 +42,6 @@ class WCoverArt : public QWidget, public WBaseWidget { private: QPixmap scaledCoverArt(QPixmap normal); - CoverArtCache* m_pCoverCache; bool m_bEnableWidget; WCoverArtMenu* m_pMenu; QPixmap m_loadedCover; diff --git a/src/widget/wcoverartmenu.cpp b/src/widget/wcoverartmenu.cpp index 6cab7e5acd..dc8be5eb94 100644 --- a/src/widget/wcoverartmenu.cpp +++ b/src/widget/wcoverartmenu.cpp @@ -51,12 +51,16 @@ void WCoverArtMenu::slotChange() { return; } + CoverArtCache* pCache = CoverArtCache::instance(); + if (pCache == NULL) { + return; + } + // get initial directory (trackdir or coverdir) QString initialDir; QString trackPath = m_pTrack->getDirectory(); if (m_coverInfo.coverLocation.isEmpty() || - m_coverInfo.coverLocation == CoverArtCache::instance() - ->getDefaultCoverLocation()) { + m_coverInfo.coverLocation == pCache->getDefaultCoverLocation()) { initialDir = trackPath; } else { initialDir = m_coverInfo.coverLocation; @@ -111,14 +115,19 @@ void WCoverArtMenu::slotReload() { if (m_coverInfo.trackId < 1) { return; } + CoverArtCache* pCache = CoverArtCache::instance(); + if (pCache == NULL) { + return; + } + CoverArtDAO::CoverArtInfo info; info.trackId = m_pTrack->getId(); info.album = m_pTrack->getAlbum(); info.trackDirectory = m_pTrack->getDirectory(); info.trackLocation = m_pTrack->getLocation(); info.trackBaseName = QFileInfo(m_pTrack->getFilename()).baseName(); - CoverArtCache::FutureResult res = - CoverArtCache::instance()->searchImage(info, QSize(0,0), false); + CoverArtCache::FutureResult res = pCache->searchImage( + info, QSize(0,0), false); QPixmap px; px.convertFromImage(res.img); emit(coverLocationUpdated(res.coverLocation, m_coverInfo.coverLocation, px)); @@ -128,7 +137,11 @@ void WCoverArtMenu::slotUnset() { if (m_coverInfo.trackId < 1) { return; } - QString newLoc = CoverArtCache::instance()->getDefaultCoverLocation(); - QPixmap px = CoverArtCache::instance()->getDefaultCoverArt(); + CoverArtCache* pCache = CoverArtCache::instance(); + if (pCache == NULL) { + return; + } + QString newLoc = pCache->getDefaultCoverLocation(); + QPixmap px = pCache->getDefaultCoverArt(); emit(coverLocationUpdated(newLoc, m_coverInfo.coverLocation, px)); } diff --git a/src/widget/wtracktableview.cpp b/src/widget/wtracktableview.cpp index 100c5518f2..d3a759db18 100644 --- a/src/widget/wtracktableview.cpp +++ b/src/widget/wtracktableview.cpp @@ -96,8 +96,11 @@ WTrackTableView::WTrackTableView(QWidget * parent, connect(this, SIGNAL(scrollValueChanged(int)), this, SLOT(slotScrollValueChanged(int))); - connect(CoverArtCache::instance(), SIGNAL(requestRepaint()), - this, SLOT(update())); + CoverArtCache* pCache = CoverArtCache::instance(); + if (pCache != NULL) { + connect(pCache, SIGNAL(requestRepaint()), + this, SLOT(update())); + } } WTrackTableView::~WTrackTableView() { -- cgit v1.2.3