Commit fce1c72a authored by Michael Pyne's avatar Michael Pyne

Remove a KSharedPtr.

parent f00e55c1
......@@ -97,24 +97,14 @@ void CoverDialog::show()
QWidget::show();
}
// Here we try to keep the GUI from freezing for too long while we load the
// covers.
// TODO: Make this concurrent on a non-GUI thread
void CoverDialog::loadCovers()
{
CoverDataMapIterator it, end;
int i = 0;
it = CoverManager::begin();
end = CoverManager::end();
auto it = CoverManager::begin();
const auto &end = CoverManager::end();
for(; it != end; ++it) {
(void) new CoverIconViewItem(it.key(), m_covers);
// TODO: Threading!
if(++i == 10) {
i = 0;
kapp->processEvents();
}
(void) new CoverIconViewItem(it->first, m_covers);
}
}
......@@ -138,8 +128,8 @@ void CoverDialog::slotArtistClicked(QListWidgetItem *item)
end = CoverManager::end();
for(; it != end; ++it) {
if(it.value()->artist == artist)
(void) new CoverIconViewItem(it.key(), m_covers);
if(it->second.artist == artist)
(void) new CoverIconViewItem(it->first, m_covers);
}
}
}
......@@ -177,32 +167,26 @@ void CoverDialog::slotSearchPatternChanged(const QString& pattern)
QRegExp filter(pattern, Qt::CaseInsensitive, QRegExp::Wildcard);
QString artist = item->text().toLower();
CoverDataMapIterator it, end;
it = CoverManager::begin();
end = CoverManager::end();
CoverDataMapIterator it = CoverManager::begin();
const CoverDataMapIterator end = CoverManager::end();
// Here, only show cover that match the search pattern.
if (dynamic_cast<AllArtistsListViewItem *>(item)) {
for(; it != end; ++it) {
if (filter.indexIn(it.value()->artist) != -1) {
(void) new CoverIconViewItem(it.key(), m_covers);
if (filter.indexIn(it->second.artist) != -1) {
(void) new CoverIconViewItem(it->first, m_covers);
}
}
}
// Here, only show the covers that match the search pattern and
// that have the same artist as the currently selected one.
else {
for(; it != end; ++it) {
if (it.value()->artist == artist
&& ((filter.indexIn(it.value()->artist) != -1)
|| (filter.indexIn(it.value()->album) != -1))) {
(void) new CoverIconViewItem(it.key(), m_covers);
if (it->second.artist == artist
&& ((filter.indexIn(it->second.artist) != -1)
|| (filter.indexIn(it->second.album) != -1)))
{
(void) new CoverIconViewItem(it->first, m_covers);
}
}
}
......
......@@ -23,9 +23,9 @@ using CoverUtility::CoverIconViewItem;
CoverIconViewItem::CoverIconViewItem(coverKey id, KListWidget *parent) :
QListWidgetItem(parent), m_id(id)
{
CoverDataPtr data = CoverManager::coverInfo(id);
setText(QString("%1 - %2").arg(data->artist, data->album));
setIcon(data->thumbnail());
const auto &data = CoverManager::coverInfo(id);
setText(QString("%1 - %2").arg(data.artist, data.album));
setIcon(data.thumbnail());
setSizeHint(QSize(140, 150));
}
......@@ -44,17 +44,4 @@ CoverIconViewItem *CoverIconView::currentItem() const
return static_cast<CoverIconViewItem *>(KListWidget::currentItem());
}
// TODO: port to Qt4
#if 0
Q3DragObject *CoverIconView::dragObject()
{
// Temporarily disabled pending conversion of the cover manager icon view
// to Qt 4 ish stuff.
CoverIconViewItem *item = currentItem();
if(item)
return new CoverDrag(item->id(), this);
return 0;
}
#endif
// vim: set et sw=4 tw=0 sta:
......@@ -247,7 +247,7 @@ QPixmap CoverInfo::pixmap(CoverSize size) const
QString CoverInfo::localPathToCover(const QString &fallbackFileName) const
{
if(m_coverKey != CoverManager::NoMatch) {
QString path = CoverManager::coverInfo(m_coverKey)->path;
QString path = CoverManager::coverInfo(m_coverKey).path;
if(!path.isEmpty())
return path;
}
......
......@@ -105,7 +105,7 @@ class CoverManagerPrivate
{
public:
/// Maps coverKey id's to CoverDataPtrs
/// Maps coverKey id's to CoverData
CoverDataMap covers;
/// Maps file names to coverKey id's.
......@@ -204,14 +204,14 @@ void CoverManagerPrivate::saveCovers() const
QDataStream out(&file);
// Write out the version and count
out << quint32(0) << quint32(covers.count());
out << quint32(0) << quint32(covers.size());
kDebug() << "Writing out" << covers.count() << "covers.";
kDebug() << "Writing out" << covers.size() << "covers.";
// Write out the data
for(CoverDataMap::const_iterator it = covers.begin(); it != covers.end(); ++it) {
out << quint32(it.key());
out << *it.value();
for(const auto &it : covers) {
out << quint32(it.first);
out << it.second;
}
// Now write out the track mapping.
......@@ -255,11 +255,11 @@ void CoverManagerPrivate::loadCovers()
for(quint32 i = 0; i < count; ++i) {
// Read the id, and 3 QStrings for every 1 of the count.
quint32 id;
CoverDataPtr data(new CoverData);
CoverData data;
in >> id;
in >> *data;
data->refCount = 0;
in >> data;
data.refCount = 0;
covers[(coverKey) id] = data;
}
......@@ -277,7 +277,7 @@ void CoverManagerPrivate::loadCovers()
// development.
if(KDE_ISLIKELY(!tracks.contains(path))) {
++covers[(coverKey) id]->refCount; // Another track using this.
++covers[(coverKey) id].refCount; // Another track using this.
tracks.insert(path, id);
}
}
......@@ -290,15 +290,12 @@ QString CoverManagerPrivate::coverLocation() const
return KGlobal::dirs()->saveLocation("appdata") + "coverdb/covers";
}
// XXX: This could probably use some improvement, I don't like the linear
// search for ID idea. Linear search is used instead of covers.size() since we want to
// re-use old IDs if possible.
coverKey CoverManagerPrivate::nextId() const
{
// Start from 1...
coverKey key = 1;
while(covers.contains(key))
while(covers.find(key) != covers.end())
++key;
return key;
......@@ -344,14 +341,12 @@ const char *CoverDrag::mimetype()
//
coverKey CoverManager::idFromMetadata(const QString &artist, const QString &album)
{
// Search for the string, yay! It might make sense to use a cache here,
// if so it's not hard to add a QCache.
CoverDataMap::const_iterator it = begin();
CoverDataMap::const_iterator endIt = end();
CoverDataMap::const_iterator it = begin();
const CoverDataMap::const_iterator endIt = end();
for(; it != endIt; ++it) {
if(it.value()->album == album.toLower() && it.value()->artist == artist.toLower())
return it.key();
if(it->second.album == album.toLower() && it->second.artist == artist.toLower())
return it->first;
}
return NoMatch;
......@@ -359,15 +354,14 @@ coverKey CoverManager::idFromMetadata(const QString &artist, const QString &albu
QPixmap CoverManager::coverFromId(coverKey id, Size size)
{
CoverDataPtr info = coverInfo(id);
if(!info)
const auto &info = data()->covers.find(id);
if(info == data()->covers.end())
return QPixmap();
if(size == Thumbnail)
return info->thumbnail();
return info->second.thumbnail();
return info->pixmap();
return info->second.pixmap();
}
QPixmap CoverManager::coverFromData(const CoverData &coverData, Size size)
......@@ -437,7 +431,7 @@ coverKey CoverManager::addCover(const QPixmap &large, const QString &artist, con
coverKey CoverManager::addCover(const KUrl &path, const QString &artist, const QString &album)
{
coverKey id = data()->nextId();
CoverDataPtr coverData(new CoverData);
CoverData coverData;
QString fileNameExt = path.fileName();
int extPos = fileNameExt.lastIndexOf('.');
......@@ -449,23 +443,23 @@ coverKey CoverManager::addCover(const KUrl &path, const QString &artist, const Q
// Copy it to a local file first.
QString ext = QString("/coverdb/coverID-%1%2").arg(id).arg(fileNameExt);
coverData->path = KGlobal::dirs()->saveLocation("appdata") + ext;
coverData.path = KGlobal::dirs()->saveLocation("appdata") + ext;
kDebug() << "Saving pixmap to " << coverData->path;
kDebug() << "Saving pixmap to " << coverData.path;
data()->createDataDir();
coverData->artist = artist.toLower();
coverData->album = album.toLower();
coverData->refCount = 0;
coverData.artist = artist.toLower();
coverData.album = album.toLower();
coverData.refCount = 0;
data()->covers[id] = coverData;
data()->covers.emplace(id, coverData);
// Can't use NetAccess::download() since if path is already a local file
// (which is possible) then that function will return without copying, since
// it assumes we merely want the file on the hard disk somewhere.
KIO::FileCopyJob *job = KIO::file_copy(
path, KUrl::fromPath(coverData->path),
path, KUrl::fromPath(coverData.path),
-1 /* perms */,KIO::HideProgressInfo | KIO::Overwrite
);
QObject::connect(job, SIGNAL(result(KJob*)),
......@@ -506,18 +500,18 @@ void CoverManager::jobComplete(KJob *job, bool completedSatisfactory)
return;
}
CoverDataPtr coverData = data()->covers[id];
CoverData coverData = data()->covers[id];
// Make sure the new cover isn't inadvertently cached.
QPixmapCache::remove(QString("f%1").arg(coverData->path));
QPixmapCache::remove(QString("t%1").arg(coverData->path));
QPixmapCache::remove(QString("f%1").arg(coverData.path));
QPixmapCache::remove(QString("t%1").arg(coverData.path));
JuK::JuKInstance()->coverDownloaded(coverFromData(*coverData, CoverManager::Thumbnail));
JuK::JuKInstance()->coverDownloaded(coverFromData(coverData, CoverManager::Thumbnail));
}
bool CoverManager::hasCover(coverKey id)
{
return data()->covers.contains(id);
return data()->covers.find(id) != data()->covers.end();
}
bool CoverManager::removeCover(coverKey id)
......@@ -526,9 +520,9 @@ bool CoverManager::removeCover(coverKey id)
return false;
// Remove cover from cache.
CoverDataPtr coverData = coverInfo(id);
QPixmapCache::remove(QString("f%1").arg(coverData->path));
QPixmapCache::remove(QString("t%1").arg(coverData->path));
CoverData coverData = coverInfo(id);
QPixmapCache::remove(QString("f%1").arg(coverData.path));
QPixmapCache::remove(QString("t%1").arg(coverData.path));
// Remove references to files that had that track ID.
QList<QString> affectedFiles = data()->tracks.keys(id);
......@@ -537,10 +531,10 @@ bool CoverManager::removeCover(coverKey id)
}
// Remove covers from disk.
QFile::remove(coverData->path);
QFile::remove(coverData.path);
// Finally, forget that we ever knew about this cover.
data()->covers.remove(id);
data()->covers.erase(id);
data()->requestSave();
return true;
......@@ -551,13 +545,13 @@ bool CoverManager::replaceCover(coverKey id, const QPixmap &large)
if(!hasCover(id))
return false;
CoverDataPtr coverData = coverInfo(id);
CoverData coverData = coverInfo(id);
// Empty old pixmaps from cache.
QPixmapCache::remove(QString("t%1").arg(coverData->path));
QPixmapCache::remove(QString("f%1").arg(coverData->path));
QPixmapCache::remove(QString("t%1").arg(coverData.path));
QPixmapCache::remove(QString("f%1").arg(coverData.path));
large.save(coverData->path, "PNG");
large.save(coverData.path, "PNG");
// No save is needed, as all that has changed is the on-disk cover data,
// not the list of tracks or covers.
......@@ -582,17 +576,12 @@ void CoverManager::shutdown()
CoverDataMapIterator CoverManager::begin()
{
return data()->covers.constBegin();
return data()->covers.begin();
}
CoverDataMapIterator CoverManager::end()
{
return data()->covers.constEnd();
}
CoverList CoverManager::keys()
{
return data()->covers.keys();
return data()->covers.end();
}
void CoverManager::setIdForTrack(const QString &path, coverKey id)
......@@ -602,17 +591,17 @@ void CoverManager::setIdForTrack(const QString &path, coverKey id)
return; // We're already done.
if(oldId != NoMatch) {
data()->covers[oldId]->refCount--;
data()->covers[oldId].refCount--;
data()->tracks.remove(path);
if(data()->covers[oldId]->refCount == 0) {
if(data()->covers[oldId].refCount == 0) {
kDebug() << "Cover " << oldId << " is unused, removing.\n";
removeCover(oldId);
}
}
if(id != NoMatch) {
data()->covers[id]->refCount++;
data()->covers[id].refCount++;
data()->tracks.insert(path, id);
}
......@@ -624,12 +613,13 @@ coverKey CoverManager::idForTrack(const QString &path)
return data()->tracks.value(path, NoMatch);
}
CoverDataPtr CoverManager::coverInfo(coverKey id)
CoverData CoverManager::coverInfo(coverKey id)
{
if(data()->covers.contains(id))
if(hasCover(id))
return data()->covers[id];
return CoverDataPtr(0);
// TODO throw new something or other
return CoverData{};
}
/**
......
......@@ -23,15 +23,14 @@
#include <QMimeData>
#include <QString>
#include <map>
class CoverManagerPrivate;
class CoverProxy;
class QPixmap;
class QTimer;
class KJob;
template<class Key, class Value>
class QMap;
template<class T>
class QList;
......@@ -68,7 +67,7 @@ private:
* @author Michael Pyne <mpyne@kde.org>
* @see CoverManager
*/
class CoverData : public KShared
class CoverData
{
public:
QPixmap pixmap() const;
......@@ -81,13 +80,10 @@ public:
unsigned refCount; // Refers to number of tracks using this.
};
typedef KSharedPtr<CoverData> CoverDataPtr;
typedef unsigned long coverKey; ///< Type of the id for a cover.
typedef QMap<coverKey, CoverDataPtr> CoverDataMap;
// I can't believe this actually works...
typedef CoverDataMap::const_iterator CoverDataMapIterator;
using CoverDataMap = std::map<coverKey, CoverData>;
using CoverDataMapIterator = typename CoverDataMap::const_iterator;
typedef QList<coverKey> CoverList;
......@@ -162,8 +158,7 @@ public:
* Returns the cover art for @p ptr. This function is intended for use
* by CoverData.
*
* @param ptr The CoverData to get the cover of. Note that it is a
* CoverData, not CoverDataPtr.
* @param ptr The CoverData to get the cover of.
* @param size The size to return it as.
* @see CoverData
*/
......@@ -176,7 +171,7 @@ public:
* @param id the id of the cover to retrieve info on.
* @return 0 if there is no info on @p id, otherwise its information.
*/
static CoverDataPtr coverInfo(coverKey id);
static CoverData coverInfo(coverKey id);
/**
* Adds @p large to the cover database, associating with it @p artist and
......@@ -251,11 +246,6 @@ public:
*/
static CoverDataMapIterator end();
/**
* @return A list of all of the id's listed in the database.
*/
static CoverList keys();
/**
* Associates @p path with the cover identified by @id. No comparison of
* metadata is performed to enforce this matching.
......
......@@ -116,8 +116,8 @@ QString DBusCollectionProxy::trackCover(const QString &track)
{
coverKey id = CoverManager::idForTrack(track);
if(id != CoverManager::NoMatch) {
CoverDataPtr coverData = CoverManager::coverInfo(id);
return coverData->path;
CoverData coverData = CoverManager::coverInfo(id);
return coverData.path;
}
// No cover, let's see if one is embedded.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment