29
29
#include <kdebug.h>
30
30
#include <k3staticdeleter.h>
31
#include <ktemporaryfile.h>
31
33
#include <kstandarddirs.h>
32
34
#include <kglobal.h>
35
#include <kio/netaccess.h>
34
37
// This is a dictionary to map the track path to their ID. Otherwise we'd have
35
38
// to store this info with each CollectionListItem, which would break the cache
384
387
coverKey CoverManager::addCover(const QPixmap &large, const QString &artist, const QString &album)
389
kDebug() << "Adding new pixmap to cover database.\n";
392
kDebug() << "The pixmap you're trying to add is NULL!\n";
396
KTemporaryFile tempFile;
397
if(!tempFile.open()) {
398
kError() << "Unable to open file for pixmap cover, unable to add cover to DB\n";
402
// Now that file is open, file name will be available, which is where we want
403
// to save the pixmap as a .png.
405
if(!large.save(tempFile.fileName(), "PNG")) {
406
kError() << "Unable to save pixmap to " << tempFile.fileName() << endl;
410
return addCover(KUrl::fromPath(tempFile.fileName()), artist, album);
413
coverKey CoverManager::addCover(const KUrl &path, const QString &artist, const QString &album)
388
415
coverKey id = data()->nextId();
389
416
CoverDataPtr coverData(new CoverData);
392
kDebug() << "The pixmap you're trying to add is NULL!\n";
396
// Save it to file first!
398
QString ext = QString("/coverdb/coverID-%1.png").arg(id);
418
QString fileNameExt = path.fileName();
419
int extPos = fileNameExt.lastIndexOf('.');
421
fileNameExt = fileNameExt.mid(extPos);
425
// Copy it to a local file first.
427
QString ext = QString("/coverdb/coverID-%1%2").arg(id).arg(fileNameExt);
399
428
coverData->path = KGlobal::dirs()->saveLocation("appdata") + ext;
401
430
kDebug() << "Saving pixmap to " << coverData->path;
402
431
data()->createDataDir();
404
if(!large.save(coverData->path, "PNG")) {
405
kError() << "Unable to save pixmap to " << coverData->path << endl;
433
// Can't use NetAccess::download() since if path is already a local file
434
// (which is possible) then that function will return without copying, since
435
// it assumes we merely want the file on the hard disk somewhere.
437
if(!KIO::NetAccess::file_copy(path, KUrl::fromPath(coverData->path))) {
438
kError() << "Failed to download cover from " << path << endl;
422
coverKey CoverManager::addCover(const QString &path, const QString &artist, const QString &album)
424
return addCover(QPixmap(path), artist, album);
427
455
bool CoverManager::hasCover(coverKey id)
429
457
return data()->covers.contains(id);