utility.cpp Example File
declarative-music-browser/utility.cpp
#include "utility.h"
#include <QtCore>
#define QT_GALLERY_MEDIA_ART_ILLEGAL_CHARACTERS \
"\\(.*\\)|\\{.*\\}|\\[.*\\]|<.*>|[\\(\\)_\\{\\}\\[\\]\\!@#$\\^&\\*\\+\\=\\|\\\\/\"'\\?~`]"
Utility::Utility(QObject *parent)
: QObject(parent)
#if defined(Q_OS_UNIX) && !(defined(Q_OS_SYMBIAN) || defined(Q_OS_MAC))
, illegalCharacters(QLatin1String(QT_GALLERY_MEDIA_ART_ILLEGAL_CHARACTERS))
, whitespace(QCryptographicHash::hash(" ", QCryptographicHash::Md5).toHex())
#endif
{
}
Utility::~Utility()
{
}
QString Utility::formatDuration(int duration) const
{
const int seconds = duration % 60;
const int minutes = (duration /= 60) % 60;
const int hours = duration / 60;
QTime time(hours, minutes, seconds);
return time.hour() > 0
? time.toString(tr("h:mm:ss"))
: time.toString(tr("mm:ss"));
}
#if defined(Q_OS_UNIX) && !(defined(Q_OS_SYMBIAN) || defined(Q_OS_MAC))
QUrl Utility::getAlbumArtUrl(const QString &artist, const QString &title) const
{
QString fileName = QDir::homePath()
+ QLatin1String("/.cache/media-art/album-")
+ hash(artist)
+ QLatin1Char('-')
+ hash(title)
+ QLatin1String(".jpeg");
if (QFile::exists(fileName))
return QUrl::fromLocalFile(fileName);
return QUrl();
}
QUrl Utility::getAlbumArtThumbnailUrl(const QString &artist, const QString &title) const
{
QUrl albumUrl = getAlbumArtUrl(artist, title);
if (albumUrl.isValid()) {
#if defined(Q_WS_MAEMO_5)
QString thumbnailPath = QDir::homePath()
+ QLatin1String("/.thumbnails/cropped/")
+ QCryptographicHash::hash(
albumUrl.toString().toUtf8(), QCryptographicHash::Md5).toHex()
+ QLatin1String(".jpeg");
#elif defined(Q_WS_MAEMO_6)
QString thumbnailPath = QDir::homePath()
+ QLatin1String("/.thumbnails/grid/")
+ QCryptographicHash::hash(
albumUrl.toString().toUtf8(), QCryptographicHash::Md5).toHex()
+ QLatin1String(".jpeg");
#else
QString thumbnailPath = QDir::homePath()
+ QLatin1String("/.thumbnails/normal/")
+ QCryptographicHash::hash(
albumUrl.toEncoded(), QCryptographicHash::Md5).toHex()
+ QLatin1String(".png");
#endif
if (QFile::exists(thumbnailPath))
return QUrl::fromLocalFile(thumbnailPath);
}
return albumUrl;
}
QString Utility::hash(const QString &identifier) const
{
if (identifier.isEmpty()) {
return whitespace;
} else {
return QCryptographicHash::hash(
identifier.toLower().remove(illegalCharacters).simplified().toUtf8(),
QCryptographicHash::Md5).toHex();
}
}
#else
QUrl Utility::getAlbumArtUrl(const QString &, const QString &) const
{
return QUrl();
}
QUrl Utility::getAlbumArtThumbnailUrl(const QString &, const QString &) const
{
return QUrl();
}
#endif
[+] Documentation Feedback