~neon/juk/master

« back to all changes in this revision

Viewing changes to covermanager.cpp

  • Committer: Michael Pyne
  • Date: 2008-05-10 02:32:39 UTC
  • Revision ID: git-v1:0551ea2cfc1d6ef8355b20510b90617a7cdd8caf
Fix bug 157987 (JuK unnecessarily converts all cover art to PNG) in trunk.  Now when JuK downloads cover
art it will simply copy it in place instead of converting it to PNG en route.

CCBUG:157987

svn path=/trunk/KDE/kdemultimedia/juk/; revision=806109

Show diffs side-by-side

added added

removed removed

Lines of Context:
28
28
 
29
29
#include <kdebug.h>
30
30
#include <k3staticdeleter.h>
 
31
#include <ktemporaryfile.h>
 
32
#include <kurl.h>
31
33
#include <kstandarddirs.h>
32
34
#include <kglobal.h>
 
35
#include <kio/netaccess.h>
33
36
 
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
383
386
 
384
387
coverKey CoverManager::addCover(const QPixmap &large, const QString &artist, const QString &album)
385
388
{
386
 
    kDebug() ;
387
 
 
 
389
    kDebug() << "Adding new pixmap to cover database.\n";
 
390
 
 
391
    if(large.isNull()) {
 
392
        kDebug() << "The pixmap you're trying to add is NULL!\n";
 
393
        return NoMatch;
 
394
    }
 
395
 
 
396
    KTemporaryFile tempFile;
 
397
    if(!tempFile.open()) {
 
398
        kError() << "Unable to open file for pixmap cover, unable to add cover to DB\n";
 
399
        return NoMatch;
 
400
    }
 
401
 
 
402
    // Now that file is open, file name will be available, which is where we want
 
403
    // to save the pixmap as a .png.
 
404
 
 
405
    if(!large.save(tempFile.fileName(), "PNG")) {
 
406
        kError() << "Unable to save pixmap to " << tempFile.fileName() << endl;
 
407
        return NoMatch;
 
408
    }
 
409
 
 
410
    return addCover(KUrl::fromPath(tempFile.fileName()), artist, album);
 
411
}
 
412
 
 
413
coverKey CoverManager::addCover(const KUrl &path, const QString &artist, const QString &album)
 
414
{
388
415
    coverKey id = data()->nextId();
389
416
    CoverDataPtr coverData(new CoverData);
390
417
 
391
 
    if(large.isNull()) {
392
 
        kDebug() << "The pixmap you're trying to add is NULL!\n";
393
 
        return NoMatch;
394
 
    }
395
 
 
396
 
    // Save it to file first!
397
 
 
398
 
    QString ext = QString("/coverdb/coverID-%1.png").arg(id);
 
418
    QString fileNameExt = path.fileName();
 
419
    int extPos = fileNameExt.lastIndexOf('.');
 
420
 
 
421
    fileNameExt = fileNameExt.mid(extPos);
 
422
    if(extPos == -1)
 
423
        fileNameExt = "";
 
424
 
 
425
    // Copy it to a local file first.
 
426
 
 
427
    QString ext = QString("/coverdb/coverID-%1%2").arg(id).arg(fileNameExt);
399
428
    coverData->path = KGlobal::dirs()->saveLocation("appdata") + ext;
400
429
 
401
430
    kDebug() << "Saving pixmap to " << coverData->path;
402
431
    data()->createDataDir();
403
432
 
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.
 
436
 
 
437
    if(!KIO::NetAccess::file_copy(path, KUrl::fromPath(coverData->path))) {
 
438
        kError() << "Failed to download cover from " << path << endl;
406
439
        return NoMatch;
407
440
    }
408
441
 
419
452
    return id;
420
453
}
421
454
 
422
 
coverKey CoverManager::addCover(const QString &path, const QString &artist, const QString &album)
423
 
{
424
 
    return addCover(QPixmap(path), artist, album);
425
 
}
426
 
 
427
455
bool CoverManager::hasCover(coverKey id)
428
456
{
429
457
    return data()->covers.contains(id);