Members of the KDE Community are recommended to subscribe to the kde-community mailing list at https://mail.kde.org/mailman/listinfo/kde-community to allow them to participate in important discussions and receive other important announcements

Commit 58dcb72b authored by Michael Pyne's avatar Michael Pyne

Hmm, I seem to have lost my covers, I was playing around with other changes to the code that are

uncommitted but until I can verify that this change didn't cause it I'm reverting for now, and
will check tomorrow.

svn path=/trunk/KDE/kdemultimedia/juk/; revision=831201
parent b0f92177
/***************************************************************************
begin : Sun May 15 2005
copyright : (C) 2005, 2008 by Michael Pyne
copyright : (C) 2005 by Michael Pyne
email : michael.pyne@kdemail.net
***************************************************************************/
......@@ -21,14 +21,14 @@
#include <QImage>
#include <QDir>
#include <QDataStream>
#include <QHash>
#include <Q3Dict>
#include <Q3Cache>
#include <QMimeSource>
#include <QBuffer>
#include <kdebug.h>
#include <k3staticdeleter.h>
#include <ktemporaryfile.h>
#include <kdemacros.h>
#include <kurl.h>
#include <kstandarddirs.h>
#include <kglobal.h>
......@@ -37,14 +37,19 @@
// This is a dictionary to map the track path to their ID. Otherwise we'd have
// to store this info with each CollectionListItem, which would break the cache
// of users who upgrade, and would just generally be a big mess.
typedef QHash<QString, coverKey> TrackLookupMap;
typedef Q3Dict<coverKey> TrackLookupMap;
static const char dragMimetype[] = "application/x-juk-coverid";
// This is responsible for making sure that the CoverManagerPrivate class
// gets properly destructed on shutdown.
static K3StaticDeleter<CoverManagerPrivate> sd;
const char *CoverDrag::mimetype = "application/x-juk-coverid";
// Caches the QPixmaps for the covers so that the covers are not all kept in
// memory for no reason.
typedef Q3Cache<QPixmap> CoverPixmapCache;
CoverManagerPrivate *CoverManager::m_data = 0;
// Used to save and load CoverData from a QDataStream
QDataStream &operator<<(QDataStream &out, const CoverData &data);
QDataStream &operator>>(QDataStream &in, CoverData &data);
......@@ -86,7 +91,7 @@ public:
/// 't' followed by the pathname for Thumbnail covers.
CoverPixmapCache pixmapCache;
CoverManagerPrivate() : pixmapCache(2 * 1024 * 768)
CoverManagerPrivate() : tracks(1301), pixmapCache(2 * 1024 * 768)
{
loadCovers();
pixmapCache.setAutoDelete(true);
......@@ -123,10 +128,6 @@ public:
QString coverLocation() const;
};
// This is responsible for making sure that the CoverManagerPrivate class
// gets properly destructed on shutdown.
K_GLOBAL_STATIC(CoverManagerPrivate, sd);
//
// Implementation of CoverManagerPrivate methods.
//
......@@ -168,9 +169,9 @@ void CoverManagerPrivate::saveCovers() const
// Now write out the track mapping.
out << quint32(tracks.count());
TrackLookupMap::ConstIterator trackMapIt = tracks.constBegin();
while(trackMapIt != tracks.constEnd()) {
out << trackMapIt.key() << quint32(trackMapIt.value());
Q3DictIterator<coverKey> trackMapIt(tracks);
while(trackMapIt.current()) {
out << trackMapIt.currentKey() << quint32(*trackMapIt.current());
++trackMapIt;
}
}
......@@ -224,9 +225,9 @@ void CoverManagerPrivate::loadCovers()
// don't do so again. Possible due to a coding error during 3.5
// development.
if(KDE_ISLIKELY(!tracks.contains(path))) {
if(!tracks.find(path)) {
++covers[(coverKey) id]->refCount; // Another track using this.
tracks.insert(path, id);
tracks.insert(path, new coverKey(id));
}
}
}
......@@ -263,7 +264,7 @@ CoverDrag::CoverDrag(coverKey id, QWidget *src) : Q3DragObject(src, "coverDrag")
const char *CoverDrag::format(int i) const
{
if(i == 0)
return dragMimetype;
return mimetype;
if(i == 1)
return "image/png";
......@@ -272,14 +273,14 @@ const char *CoverDrag::format(int i) const
QByteArray CoverDrag::encodedData(const char *mimetype) const
{
if(qstrcmp(dragMimetype, mimetype) == 0) {
if(qstrcmp(CoverDrag::mimetype, mimetype) == 0) {
QByteArray data;
QDataStream ds(&data, QIODevice::WriteOnly);
ds << quint32(m_id);
return data;
}
else if(qstrcmp(dragMimetype, "image/png") == 0) {
else if(qstrcmp(mimetype, "image/png") == 0) {
QPixmap large = CoverManager::coverFromId(m_id, CoverManager::FullSize);
QImage img = large.toImage();
QByteArray data;
......@@ -296,7 +297,7 @@ QByteArray CoverDrag::encodedData(const char *mimetype) const
bool CoverDrag::canDecode(const QMimeSource *e)
{
return e->provides(dragMimetype);
return e->provides(mimetype);
}
bool CoverDrag::decode(const QMimeSource *e, coverKey &id)
......@@ -304,7 +305,7 @@ bool CoverDrag::decode(const QMimeSource *e, coverKey &id)
if(!canDecode(e))
return false;
QByteArray data = e->encodedData(dragMimetype);
QByteArray data = e->encodedData(mimetype);
QDataStream ds(&data, QIODevice::ReadOnly);
quint32 i;
......@@ -314,11 +315,6 @@ bool CoverDrag::decode(const QMimeSource *e, coverKey &id)
return true;
}
const char *CoverDrag::mimetype()
{
return dragMimetype;
}
//
// Implementation of CoverManager methods.
//
......@@ -472,10 +468,10 @@ bool CoverManager::removeCover(coverKey id)
data()->pixmapCache.remove(QString("t%1").arg(coverData->path));
// Remove references to files that had that track ID.
QList<QString> affectedFiles = data()->tracks.keys(id);
foreach (const QString &file, affectedFiles) {
data()->tracks.remove(file);
}
Q3DictIterator<coverKey> it(data()->tracks);
for(; it.current(); ++it)
if(*it.current() == id)
data()->tracks.remove(it.currentKey());
// Remove covers from disk.
QFile::remove(coverData->path);
......@@ -494,8 +490,8 @@ bool CoverManager::replaceCover(coverKey id, const QPixmap &large)
CoverDataPtr coverData = coverInfo(id);
// Empty old pixmaps from cache.
data()->pixmapCache.remove(QString("t%1").arg(coverData->path));
data()->pixmapCache.remove(QString("f%1").arg(coverData->path));
data()->pixmapCache.remove(QString("%1%2").arg("t", coverData->path));
data()->pixmapCache.remove(QString("%1%2").arg("f", coverData->path));
large.save(coverData->path, "PNG");
return true;
......@@ -503,7 +499,10 @@ bool CoverManager::replaceCover(coverKey id, const QPixmap &large)
CoverManagerPrivate *CoverManager::data()
{
return sd;
if(!m_data)
sd.setObject(m_data, new CoverManagerPrivate);
return m_data;
}
void CoverManager::saveCovers()
......@@ -513,7 +512,7 @@ void CoverManager::saveCovers()
void CoverManager::shutdown()
{
sd.destroy();
sd.destructObject();
}
CoverDataMapIterator CoverManager::begin()
......@@ -533,29 +532,34 @@ CoverList CoverManager::keys()
void CoverManager::setIdForTrack(const QString &path, coverKey id)
{
coverKey oldId = data()->tracks.value(path, NoMatch);
if(data()->tracks.contains(path) && (id == oldId))
coverKey *oldId = data()->tracks.find(path);
if(oldId && (id == *oldId))
return; // We're already done.
if(oldId != NoMatch) {
data()->covers[oldId]->refCount--;
if(oldId && *oldId != NoMatch) {
data()->covers[*oldId]->refCount--;
data()->tracks.remove(path);
if(data()->covers[oldId]->refCount == 0) {
kDebug(65432) << "Cover " << oldId << " is unused, removing.\n";
removeCover(oldId);
if(data()->covers[*oldId]->refCount == 0) {
kDebug(65432) << "Cover " << *oldId << " is unused, removing.\n";
removeCover(*oldId);
}
}
if(id != NoMatch) {
data()->covers[id]->refCount++;
data()->tracks.insert(path, id);
data()->tracks.insert(path, new coverKey(id));
}
}
coverKey CoverManager::idForTrack(const QString &path)
{
return data()->tracks.value(path, NoMatch);
coverKey *coverPtr = data()->tracks.find(path);
if(!coverPtr)
return NoMatch;
return *coverPtr;
}
CoverDataPtr CoverManager::coverInfo(coverKey id)
......
......@@ -92,7 +92,7 @@ public:
static bool canDecode(const QMimeSource *e);
static bool decode(const QMimeSource *e, coverKey &id);
static const char* mimetype();
static const char* mimetype;
private:
coverKey m_id;
......@@ -263,6 +263,8 @@ public:
static const coverKey NoMatch = 0;
private:
static CoverManagerPrivate *m_data;
static CoverManagerPrivate *data();
static QPixmap createThumbnail(const QPixmap &base);
};
......
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