filehandle.cpp 6.72 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/***************************************************************************
    begin                : Sun Feb 29 2004
    copyright            : (C) 2004 by Scott Wheeler
    email                : wheeler@kde.org
***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

16
#include "filehandle.h"
17

18 19
#include <kdebug.h>

20 21 22 23
#include <QFileInfo>

#include <limits.h>
#include <stdlib.h>
24

25
#include "filehandleproperties.h"
26
#include "tag.h"
27
#include "cache.h"
28
#include "coverinfo.h"
29

Dirk Mueller's avatar
Dirk Mueller committed
30 31 32 33 34 35 36 37 38 39 40
AddProperty(Title, tag()->title())
AddProperty(Artist, tag()->artist())
AddProperty(Album, tag()->album())
AddProperty(Genre, tag()->genre())
AddNumberProperty(Track, tag()->track())
AddNumberProperty(Year, tag()->year())
AddProperty(Comment, tag()->comment())
AddNumberProperty(Seconds, tag()->seconds())
AddNumberProperty(Bitrate, tag()->bitrate())
AddProperty(Path, absFilePath())
AddNumberProperty(Size, fileInfo().size())
41
AddProperty(Extension, fileInfo().suffix())
42

43 44 45 46
static QString resolveSymLinks(const QFileInfo &file) // static
{
    char real[PATH_MAX];

47
    if(file.exists() && realpath(QFile::encodeName(file.absoluteFilePath()).data(), real))
48
        return QFile::decodeName(real);
49
    else
50
        return file.filePath();
51 52
}

53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
/**
 * A simple reference counter -- pasted from TagLib.
 */

class RefCounter
{
public:
    RefCounter() : refCount(1) {}
    void ref() { refCount++; }
    bool deref() { return ! --refCount ; }
    int count() const { return refCount; }
private:
    uint refCount;
};

class FileHandle::FileHandlePrivate : public RefCounter
{
public:
    FileHandlePrivate() :
72
        tag(0),
73
        coverInfo(0) {}
74

Scott Wheeler's avatar
Scott Wheeler committed
75 76 77
    ~FileHandlePrivate()
    {
        delete tag;
78
        delete coverInfo;
Scott Wheeler's avatar
Scott Wheeler committed
79 80
    }

81
    mutable Tag *tag;
82
    mutable CoverInfo *coverInfo;
83
    mutable QString absFilePath;
84 85 86
    QFileInfo fileInfo;
    QDateTime modificationTime;
    QDateTime lastModified;
87 88 89 90 91 92 93 94
};

////////////////////////////////////////////////////////////////////////////////
// public methods
////////////////////////////////////////////////////////////////////////////////

FileHandle::FileHandle()
{
95 96
    static FileHandlePrivate nullPrivate;
    d = &nullPrivate;
97
    d->ref();
98 99
}

100 101
FileHandle::FileHandle(const FileHandle &f) :
    d(f.d)
102
{
103
    if(!d) {
104 105
        kDebug(65432) << "The source FileHandle was not initialized." << endl;
        d = null().d;
106
    }
107 108 109
    d->ref();
}

110 111
FileHandle::FileHandle(const QFileInfo &info, const QString &path) :
    d(0)
112
{
113
    setup(info, path);
114 115
}

116 117
FileHandle::FileHandle(const QString &path) :
    d(0)
118
{
119
    setup(QFileInfo(path), path);
120 121
}

122 123 124 125
FileHandle::FileHandle(const QString &path, CacheDataStream &s)
{
    d = new FileHandlePrivate;
    d->fileInfo = QFileInfo(path);
126
    d->absFilePath = path;
127 128 129 130
    read(s);
    Cache::instance()->insert(*this);
}

131 132 133 134 135 136
FileHandle::~FileHandle()
{
    if(d->deref())
        delete d;
}

137 138 139 140
void FileHandle::refresh()
{
    d->fileInfo.refresh();
    delete d->tag;
141
    d->tag = new Tag(d->absFilePath);
142 143
}

144 145
void FileHandle::setFile(const QString &path)
{
146 147 148
    if(!d || isNull())
        setup(QFileInfo(path), path);
    else {
149
        d->absFilePath = resolveSymLinks(path);
150
        d->fileInfo.setFile(path);
151
        d->tag->setFileName(d->absFilePath);
152
    }
153 154
}

155 156
Tag *FileHandle::tag() const
{
157
    if(!d->tag)
158
        d->tag = new Tag(d->absFilePath);
159

160 161 162
    return d->tag;
}

163 164 165
CoverInfo *FileHandle::coverInfo() const
{
    if(!d->coverInfo)
166
        d->coverInfo = new CoverInfo(*this);
167 168 169 170

    return d->coverInfo;
}

171 172
QString FileHandle::absFilePath() const
{
173
    if(d->absFilePath.isEmpty())
174
        d->absFilePath = resolveSymLinks(d->fileInfo.absoluteFilePath());
175 176 177 178 179 180 181 182
    return d->absFilePath;
}

const QFileInfo &FileHandle::fileInfo() const
{
    return d->fileInfo;
}

183 184 185 186 187
bool FileHandle::isNull() const
{
    return *this == null();
}

188 189
bool FileHandle::current() const
{
190 191 192
    return (d->modificationTime.isValid() &&
            lastModified().isValid() &&
            d->modificationTime >= lastModified());
193 194 195 196
}

const QDateTime &FileHandle::lastModified() const
{
197 198 199 200 201 202 203 204 205 206 207 208
    if(d->lastModified.isNull())
        d->lastModified = d->fileInfo.lastModified();

    return d->lastModified;
}

void FileHandle::read(CacheDataStream &s)
{
    switch(s.cacheVersion()) {
    case 1:
    default:
        if(!d->tag)
209
            d->tag = new Tag(d->absFilePath, true);
210

211 212 213 214
        s >> *(d->tag);
        s >> d->modificationTime;
        break;
    }
215 216
}

217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
FileHandle &FileHandle::operator=(const FileHandle &f)
{
    if(&f == this)
        return *this;

    if(d->deref())
        delete d;

    d = f.d;
    d->ref();

    return *this;
}

bool FileHandle::operator==(const FileHandle &f) const
{
    return d == f.d;
}
235 236 237 238 239 240

bool FileHandle::operator!=(const FileHandle &f) const
{
    return d != f.d;
}

241 242 243 244 245 246 247
QStringList FileHandle::properties() // static
{
    return FileHandleProperties::properties();
}

QString FileHandle::property(const QString &name) const
{
Dirk Mueller's avatar
Dirk Mueller committed
248
    return FileHandleProperties::property(*this, name.toAscii());
249 250
}

251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
const FileHandle &FileHandle::null() // static
{
    static FileHandle f;
    return f;
}

////////////////////////////////////////////////////////////////////////////////
// private methods
////////////////////////////////////////////////////////////////////////////////

void FileHandle::setup(const QFileInfo &info, const QString &path)
{
    if(d && !isNull())
        return;

266
    QString fileName = path.isEmpty() ? info.absoluteFilePath() : path;
267

268
    FileHandle cached = Cache::instance()->value(resolveSymLinks(fileName));
269 270 271 272 273 274 275 276

    if(cached != null()) {
        d = cached.d;
        d->ref();
    }
    else {
        d = new FileHandlePrivate;
        d->fileInfo = info;
277
        d->absFilePath = resolveSymLinks(fileName);
278
        d->modificationTime = info.lastModified();
279
        Cache::instance()->insert(*this);
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
    }
}

////////////////////////////////////////////////////////////////////////////////
// related functions
////////////////////////////////////////////////////////////////////////////////

QDataStream &operator<<(QDataStream &s, const FileHandle &f)
{
    s << *(f.tag())
      << f.lastModified();

    return s;
}

CacheDataStream &operator>>(CacheDataStream &s, FileHandle &f)
{
    f.read(s);
    return s;
}
300 301

// vim: set et sw=4 tw=0 sta: