| 1 |
/* |
| 2 |
Copyright (C) 2004 Max Howell <[email protected]> |
| 3 |
Copyright (C) 2005-2014 Mario Stephan <[email protected]> |
| 4 |
|
| 5 |
This library is free software; you can redistribute it and/or modify |
| 6 |
it under the terms of the GNU Lesser General Public License as published |
| 7 |
by the Free Software Foundation; either version 2.1 of the License, or |
| 8 |
(at your option) any later version. |
| 9 |
|
| 10 |
This program is distributed in the hope that it will be useful, |
| 11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
GNU General Public License for more details. |
| 14 |
|
| 15 |
You should have received a copy of the GNU Lesser General Public License |
| 16 |
along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
*/ |
| 18 |
|
| 19 |
#include "track.h" |
| 20 |
#include "playlistitem.h" |
| 21 |
|
| 22 |
#include <QFileInfo> |
| 23 |
#include <QPainter> |
| 24 |
#include <qdebug.h> |
| 25 |
|
| 26 |
#include <taglib/audioproperties.h> |
| 27 |
#include <taglib/fileref.h> |
| 28 |
#include <taglib/tag.h> |
| 29 |
#include <taglib/tstring.h> |
| 30 |
|
| 31 |
#include <taglib/attachedpictureframe.h> |
| 32 |
#include <taglib/id3v2frame.h> |
| 33 |
#include <taglib/id3v2framefactory.h> |
| 34 |
#include <taglib/id3v2tag.h> |
| 35 |
#include <taglib/mpegfile.h> |
| 36 |
#include <taglib/tbytevector.h> |
| 37 |
|
| 38 |
QStringList Track::tagNameList = QStringList() << "location" |
| 39 |
<< "creator" |
| 40 |
<< "title" |
| 41 |
<< "album" |
| 42 |
<< "year" |
| 43 |
<< "genre" |
| 44 |
<< "trackNum" |
| 45 |
<< "duration" |
| 46 |
<< "counter" |
| 47 |
<< "rate"; |
| 48 |
|
| 49 |
struct TrackPrivate { |
| 50 |
QUrl url; |
| 51 |
QString title; |
| 52 |
QString artist; |
| 53 |
QString album; |
| 54 |
QString year; |
| 55 |
QString comment; |
| 56 |
QString genre; |
| 57 |
QString tracknumber; |
| 58 |
int counter; |
| 59 |
int length; |
| 60 |
int rate; |
| 61 |
Track::Options flags; |
| 62 |
}; |
| 63 |
|
| 64 |
Track::Track() |
| 65 |
: p(new TrackPrivate) |
| 66 |
{ |
| 67 |
p->counter = -1; |
| 68 |
p->rate = 0; |
| 69 |
} |
| 70 |
|
| 71 |
Track::~Track() |
| 72 |
{ |
| 73 |
delete p; |
| 74 |
} |
| 75 |
|
| 76 |
Track::Track(const QUrl& u) |
| 77 |
: p(new TrackPrivate) |
| 78 |
{ |
| 79 |
p->url = u; |
| 80 |
readTags(); |
| 81 |
} |
| 82 |
|
| 83 |
Track::Track(const QStringList& list) |
| 84 |
: p(new TrackPrivate) |
| 85 |
{ |
| 86 |
if (list.count() > 9) { |
| 87 |
p->url = QUrl::fromLocalFile(list.at(0)); |
| 88 |
p->artist = list.at(1); |
| 89 |
p->title = list.at(2); |
| 90 |
p->album = list.at(3); |
| 91 |
p->year = list.at(4); |
| 92 |
p->genre = list.at(5); |
| 93 |
p->tracknumber = list.at(6); |
| 94 |
p->length = QString(list.at(7)).toInt(); |
| 95 |
p->counter = QString(list.at(8)).toInt(); |
| 96 |
p->rate = QString(list.at(9)).toInt(); |
| 97 |
} |
| 98 |
if (list.count() > 10) |
| 99 |
p->flags = QFlag(list.at(10).toInt()); |
| 100 |
} |
| 101 |
|
| 102 |
Track::Track(const PlaylistItem* item) |
| 103 |
: p(new TrackPrivate) |
| 104 |
{ |
| 105 |
p->url = QUrl::fromLocalFile(item->urlString()); |
| 106 |
p->title = item->title(); |
| 107 |
p->artist = item->exactText(2); |
| 108 |
p->album = item->exactText(4); |
| 109 |
p->year = item->exactText(5); |
| 110 |
p->comment = item->exactText(6); |
| 111 |
p->genre = item->exactText(7); |
| 112 |
p->tracknumber = item->exactText(8); |
| 113 |
p->counter = item->exactText(9).toInt(); |
| 114 |
// p->rate = item->rate(); |
| 115 |
} |
| 116 |
|
| 117 |
void Track::readTags() |
| 118 |
{ |
| 119 |
QString fileName = p->url.toLocalFile(); |
| 120 |
|
| 121 |
#ifdef Q_OS_WIN32 |
| 122 |
TagLib::FileRef fileref = TagLib::FileRef(fileName.toStdWString().c_str(), true, TagLib::AudioProperties::Fast); |
| 123 |
#else |
| 124 |
TagLib::FileRef fileref = TagLib::FileRef(QFile::encodeName(fileName).constData(), true, TagLib::AudioProperties::Fast); |
| 125 |
|
| 126 |
#endif |
| 127 |
|
| 128 |
if (!fileref.isNull()) { |
| 129 |
if (fileref.tag()) { |
| 130 |
TagLib::Tag* tag = fileref.tag(); |
| 131 |
|
| •132 |
p->title = !tag->title().isEmpty() ? TStringToQString(tag->title()).trimmed() : QObject::tr("Unknown"); |
| 133 |
p->artist = !tag->artist().isEmpty() ? TStringToQString(tag->artist()).trimmed() : QObject::tr("Unknown"); |
| 134 |
p->album = !tag->album().isEmpty() ? TStringToQString(tag->album()).trimmed() : QObject::tr("Unknown"); |
| 135 |
p->comment = TStringToQString(tag->comment()).trimmed(); |
| •136 |
p->genre = !tag->genre().isEmpty() ? TStringToQString(tag->genre()).trimmed() : QObject::tr("Unknown"); |
| 137 |
p->year = tag->year() ? QString::number(tag->year()) : QString::null; |
| 138 |
p->tracknumber = tag->track() ? QString::number(tag->track()) : QString::null; |
| 139 |
p->length = fileref.audioProperties()->length(); |
| 140 |
p->counter = 0; |
| 141 |
p->rate = 0; |
| 142 |
|
| 143 |
//polish up empty tags |
| 144 |
if (p->title == QObject::tr("Unknown")) { |
| 145 |
QFileInfo fileInfo(p->url.toLocalFile()); |
| 146 |
p->title = fileInfo.fileName().replace('_', ' ').replace('.' + fileInfo.suffix(), ""); |
| 147 |
} |
| 148 |
} |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
bool Track::operator==(Track* track) |
| 153 |
{ |
| 154 |
|
| 155 |
if (!isValid()) |
| 156 |
return false; |
| 157 |
return p->artist == track->artist() |
| 158 |
&& p->title == track->title(); |
| 159 |
} |
| 160 |
|
| 161 |
bool Track::containIn(QList<Track*> list) |
| 162 |
{ |
| 163 |
foreach (Track* i, list) { |
| 164 |
if (*i == this) { |
| 165 |
return true; |
| 166 |
} |
| 167 |
} |
| 168 |
return false; |
| 169 |
} |
| 170 |
|
| 171 |
QImage Track::coverImage() |
| 172 |
{ |
| 173 |
if (!p->url.isValid()) |
| 174 |
return QImage(); |
| 175 |
|
| 176 |
qDebug() << "image url:" << p->url; |
| 177 |
if (p->url.path() == "") |
| 178 |
return QImage(); |
| 179 |
|
| 180 |
QByteArray fileName = QFile::encodeName(p->url.toLocalFile()); |
| 181 |
const char* encodedName = fileName.constData(); |
| 182 |
TagLib::MPEG::File fileref(encodedName); |
| 183 |
|
| 184 |
if (!fileref.isOpen()) |
| 185 |
return QImage(); |
| 186 |
|
| 187 |
TagLib::ID3v2::Tag* tag = fileref.ID3v2Tag(); |
| 188 |
|
| 189 |
if (tag) { |
| 190 |
TagLib::ID3v2::FrameList l = fileref.ID3v2Tag()->frameListMap()["APIC"]; |
| 191 |
if (!l.isEmpty()) { |
| 192 |
|
| 193 |
//qDebug() << "Found APIC frame(s)" << endl; |
| 194 |
TagLib::ID3v2::Frame* f = l.front(); |
| 195 |
TagLib::ID3v2::AttachedPictureFrame* ap = (TagLib::ID3v2::AttachedPictureFrame*)f; |
| 196 |
|
| 197 |
const TagLib::ByteVector bytes = ap->picture(); |
| 198 |
QImage image = QImage::fromData(reinterpret_cast<const uchar*>(bytes.data()), bytes.size()); |
| 199 |
|
| 200 |
if (!image.isNull()) { |
| 201 |
return image; |
| 202 |
} |
| 203 |
} |
| 204 |
} |
| 205 |
return defaultImage(); |
| 206 |
} |
| 207 |
|
| 208 |
/* |
| 209 |
* Create a default cover image: a gray record with diagonal record name as text |
| 210 |
*/ |
| 211 |
QImage Track::defaultImage() |
| 212 |
{ |
| 213 |
QImage img(120, 120, QImage::Format_RGB888); |
| 214 |
img.fill(QColor(Qt::white)); |
| 215 |
|
| 216 |
QPainter painter(&img); |
| 217 |
|
| 218 |
painter.setPen(QPen(Qt::lightGray)); |
| 219 |
painter.setBrush(Qt::lightGray); |
| 220 |
painter.setRenderHint(QPainter::Antialiasing, true); |
| 221 |
painter.drawEllipse(QPoint(60, 60), 45, 45); |
| 222 |
|
| 223 |
painter.setBrush(Qt::white); |
| 224 |
painter.drawEllipse(QPoint(60, 60), 15, 15); |
| 225 |
|
| 226 |
painter.setPen(QPen(QColor(60, 60, 60))); |
| 227 |
painter.setFont(QFont("Monospace")); |
| 228 |
painter.translate(QPoint(-26, 62)); |
| 229 |
painter.rotate(-45); |
| 230 |
painter.drawText(img.rect(), Qt::AlignCenter, p->artist + '\n' + p->title); |
| 231 |
|
| 232 |
return img; |
| 233 |
} |
| 234 |
|
| 235 |
QString Track::prettyTitle() const |
| 236 |
{ |
| 237 |
|
| 238 |
QString s = QString::null; |
| 239 |
if (p->artist != QObject::tr("Unknown")) |
| 240 |
s += p->artist + " - "; |
| 241 |
s += p->title; |
| 242 |
|
| 243 |
return s; |
| 244 |
} |
| 245 |
|
| 246 |
QString Track::prettyTitle(QString filename) //static |
| 247 |
{ |
| 248 |
QString& s = filename; |
| 249 |
|
| 250 |
s = s.left(s.indexOf('.')); |
| 251 |
|
| 252 |
return s; |
| 253 |
} |
| 254 |
|
| 255 |
QString Track::prettyArtist(int maxlen) const |
| 256 |
{ |
| 257 |
return this->rsqueeze(p->artist, maxlen); |
| 258 |
} |
| 259 |
|
| 260 |
QString Track::prettyTitle(int maxlen) const |
| 261 |
{ |
| 262 |
return this->rsqueeze(p->title, maxlen); |
| 263 |
} |
| 264 |
|
| 265 |
QString Track::rsqueeze(const QString& str, int maxlen) const |
| 266 |
{ |
| 267 |
if (str.length() > maxlen) { |
| 268 |
int part = maxlen - 3; |
| 269 |
return QString(str.left(part) + "..."); |
| 270 |
} else |
| 271 |
return str; |
| 272 |
} |
| 273 |
|
| 274 |
void Track::setLengthFromPretty(QString s) |
| 275 |
{ |
| 276 |
int i = -1; |
| 277 |
if (s.contains(':')) { |
| 278 |
QStringList list = s.split(':'); |
| 279 |
i = list.at(0).toInt() * 60; |
| 280 |
i += list.at(1).toInt(); |
| 281 |
} |
| 282 |
p->length = i; |
| 283 |
} |
| 284 |
|
| 285 |
QString Track::prettyLength(int seconds) |
| 286 |
{ |
| 287 |
QString s; |
| 288 |
|
| 289 |
if (seconds > 0) |
| 290 |
s = prettyTime(seconds, false); |
| 291 |
else if (seconds == -1) |
| 292 |
s = '?'; |
| 293 |
|
| 294 |
return s; |
| 295 |
} |
| 296 |
|
| 297 |
QString Track::prettyTime(int seconds, bool showHours) |
| 298 |
{ |
| 299 |
QString s = QChar(':'); |
| 300 |
s.append(zeroPad(seconds % 60)); |
| 301 |
seconds /= 60; |
| 302 |
|
| 303 |
if (showHours) { |
| 304 |
s.clear(); |
| 305 |
s.prepend(zeroPad(seconds % 60)); |
| 306 |
s.prepend(':'); |
| 307 |
seconds /= 60; |
| 308 |
} |
| 309 |
|
| 310 |
s.prepend(QString::number(seconds)); |
| 311 |
return s; |
| 312 |
} |
| 313 |
|
| 314 |
bool Track::isValid() |
| 315 |
{ |
| 316 |
if (p->url.isEmpty() || !p->url.isValid()) |
| 317 |
return false; |
| 318 |
|
| 319 |
if (p->url.toString().contains(".mp3", Qt::CaseInsensitive) |
| 320 |
|| p->url.toString().contains(".ogg", Qt::CaseInsensitive) |
| 321 |
|| p->url.toString().contains(".wav", Qt::CaseInsensitive) |
| 322 |
|| p->url.toString().contains(".m4a", Qt::CaseInsensitive) |
| 323 |
|| p->url.toString().contains(".m4p", Qt::CaseInsensitive) |
| 324 |
|| p->url.toString().contains(".flac", Qt::CaseInsensitive) |
| 325 |
|| p->url.toString().contains(".aiff", Qt::CaseInsensitive) |
| 326 |
|| p->url.toString().contains(".oga", Qt::CaseInsensitive) |
| 327 |
|| p->url.toString().contains(".wma", Qt::CaseInsensitive) |
| 328 |
|| p->url.toString().contains(".au", Qt::CaseInsensitive) |
| 329 |
|| p->url.toString().contains(".tta", Qt::CaseInsensitive) |
| 330 |
|| p->url.toString().contains(".raw", Qt::CaseInsensitive)) |
| 331 |
return true; |
| 332 |
else |
| 333 |
return false; |
| 334 |
} |
| 335 |
|
| 336 |
QString Track::dirPath() |
| 337 |
{ |
| 338 |
QString localPath = p->url.toLocalFile(); |
| 339 |
QFileInfo fileInfo(localPath); |
| 340 |
return fileInfo.absolutePath(); |
| 341 |
} |
| 342 |
|
| 343 |
QStringList Track::tagList() { return (QStringList() << p->url.toLocalFile() |
| 344 |
<< p->artist |
| 345 |
<< p->title |
| 346 |
<< p->album |
| 347 |
<< p->year |
| 348 |
<< p->genre |
| 349 |
<< p->tracknumber |
| 350 |
<< QString().setNum(p->length) |
| 351 |
<< QString().setNum(p->counter) |
| 352 |
<< QString().setNum(p->rate)); } |
| 353 |
|
| 354 |
int Track::length() { return p->length > 0 ? p->length : 0; } |
| 355 |
QUrl Track::url() { return p->url; } |
| 356 |
QString Track::title() { return p->title; } |
| 357 |
QString Track::artist() { return p->artist; } |
| 358 |
QString Track::album() { return p->album; } |
| 359 |
int Track::rate() { return p->rate; } |
| 360 |
QString Track::year() { return p->year; } |
| 361 |
QString Track::comment() { return p->comment; } |
| 362 |
QString Track::genre() { return p->genre; } |
| 363 |
QString Track::tracknumber() { return p->tracknumber > 0 ? p->tracknumber : "0"; } |
| 364 |
int Track::counter() { return p->counter; } |
| 365 |
QString Track::prettyLength() { return prettyLength(p->length); } |
| 366 |
Track::Options Track::flags() { return p->flags; } |
| 367 |
|
| 368 |
void Track::setUrl(QUrl url) { p->url = url; } |
| 369 |
void Track::setTitle(QString s) { p->title = s; } |
| 370 |
void Track::setArtist(QString s) { p->artist = s; } |
| 371 |
void Track::setAlbum(QString s) { p->album = s; } |
| 372 |
void Track::setRate(int s) { p->rate = s; } |
| 373 |
void Track::setYear(QString s) { p->year = s; } |
| 374 |
void Track::setComment(QString s) { p->comment = s; } |
| 375 |
void Track::setGenre(QString s) { p->genre = s; } |
| 376 |
void Track::setTracknumber(QString s) { p->tracknumber = s; } |
| 377 |
void Track::setLength(QString s) { p->length = s.toInt(); } |
| 378 |
void Track::setCounter(QString s) { p->counter = s.toInt(); } |
| 379 |
void Track::setFlags(Track::Options flags) { p->flags = flags; } |