Commit b9cf1056 authored by Scott Wheeler's avatar Scott Wheeler

Here's something that Michael Pyne and I came up with to allow properties

to be easily added and queried for the FileHandle class.  This will be
useful in the DCOP interface.

It contains a couple of macros for registering properties in a property
map and functions to get that information.  I've tested it with kdDebug()
here and I'll let Michael add the DCOP bits.

CCMAIL:Michael Pyne <pynm0001@comcast.net>

svn path=/trunk/kdemultimedia/juk/; revision=313141
parent a05d01fc
......@@ -23,20 +23,29 @@
#include <qfileinfo.h>
#include "filehandle.h"
#include "filehandleproperties.h"
#include "tag.h"
#include "cache.h"
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());
AddProperty(Path, absFilePath());
static QString resolveSymLinks(const QFileInfo &file) // static
{
char real[PATH_MAX];
if(file.exists() && realpath(QFile::encodeName(file.absFilePath()).data(), real))
return QFile::decodeName(real);
return QFile::decodeName(real);
else
return file.filePath();
return file.filePath();
}
/**
* A simple reference counter -- pasted from TagLib.
*/
......@@ -195,6 +204,16 @@ bool FileHandle::operator!=(const FileHandle &f) const
return d != f.d;
}
QStringList FileHandle::properties() // static
{
return FileHandleProperties::properties();
}
QString FileHandle::property(const QString &name) const
{
return FileHandleProperties::property(*this, name.latin1());
}
const FileHandle &FileHandle::null() // static
{
static FileHandle f;
......@@ -222,7 +241,7 @@ void FileHandle::setup(const QFileInfo &info, const QString &path)
d = new FileHandlePrivate;
d->fileInfo = info;
d->absFilePath = fileName;
Cache::instance()->insert(*this);
Cache::instance()->insert(*this);
}
}
......
......@@ -18,7 +18,7 @@
#ifndef JUK_FILEHANDLE_H
#define JUK_FILEHANDLE_H
#include <qvaluelist.h>
#include <qstringlist.h>
class QFileInfo;
class QDateTime;
......@@ -61,6 +61,9 @@ public:
bool operator==(const FileHandle &f) const;
bool operator!=(const FileHandle &f) const;
static QStringList properties();
QString property(const QString &name) const;
static const FileHandle &null();
private:
......
/***************************************************************************
Copyright (C) 2004 by Scott Wheeler <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. *
* *
***************************************************************************/
#ifndef FILEHANDLEPROPERTIES_H
#define FILEHANDLEPROPERTIES_H
#include <qmap.h>
#include <qcstring.h>
/*
* These ugly macros make possible a property registration system that makes it
* easy to add properties to the FileHandle that can be accessed via the DCOP
* interface.
*
* Properties should actually be added to the filehandle.cpp file. This file
* is just here to separate out some of the macro-related ugliness.
*/
#define AddProperty(name, method) \
namespace FileHandleProperties \
{ \
struct name##Property : public Property \
{ \
virtual QString value(const FileHandle &f) const \
{ \
return f.##method; \
} \
static const int dummy; \
}; \
static name##Property name##Instance; \
const int name##Property::dummy = addToPropertyMap(#name, &name##Instance); \
}
#define AddNumberProperty(name, method) \
namespace FileHandleProperties \
{ \
struct name##Property : public Property \
{ \
virtual QString value(const FileHandle &f) const \
{ \
return QString::number(f.##method); \
} \
static const int dummy; \
}; \
static name##Property name##Instance; \
const int name##Property::dummy = addToPropertyMap(#name, &name##Instance); \
}
namespace FileHandleProperties
{
struct Property
{
virtual QString value(const FileHandle &) const
{
return QString::null;
}
};
static QMap<QCString, const Property *> propertyMap;
static int addToPropertyMap(const QCString &name, Property *property)
{
propertyMap[name] = property;
return 0;
}
static QString property(const FileHandle &file, const QCString &key)
{
return propertyMap.contains(key) ? propertyMap[key]->value(file) : QString::null;
}
static QStringList properties()
{
static QStringList l;
if(l.isEmpty()) {
QMap<QCString, const Property *>::ConstIterator it = propertyMap.begin();
for(; it != propertyMap.end(); ++it)
l.append(QString(it.key()));
}
return l;
}
}
#endif
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