Version bump
[someplayer] / src / track.cpp
1 /*
2  * SomePlayer - An alternate music player for Maemo 5
3  * Copyright (C) 2010 Nikolay (somebody) Tischenko <niktischenko@gmail.com>
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (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 General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18  */
19
20 #include "track.h"
21 #include "tagresolver.h"
22 #include <QFileInfo>
23 #include <QCryptographicHash>
24 #include <QDebug>
25
26 using namespace SomePlayer::DataObjects;
27
28 Track::Track() : QObject() {
29 }
30
31 Track::Track(TrackMetadata metadata, QString source) : QObject() {
32         _metadata = metadata;
33         if (_metadata.title() == _UNKNOWN_TRACK_) {
34                 QFileInfo info(source);
35                 _metadata.setTitle(info.baseName());
36         }
37         _source = source;
38         _count = 0;
39 }
40
41 Track::Track(const Track &track) : QObject() {
42         this->_metadata = track.metadata();
43         this->_source = track.source();
44         this->_count = track._count;
45 }
46
47 /// deprecated
48 Track::Track(QString source) :QObject() {
49         _resolver = new TagResolver(this);
50         connect(_resolver, SIGNAL(decoded(Track)), this, SLOT(decoded(Track)));
51         QStringList foo;
52         foo << source;
53         _resolver->decode(foo);
54         _count = 0;
55 }
56
57 TrackMetadata Track::metadata() const {
58         return _metadata;
59 }
60
61 void Track::setMetadata(TrackMetadata meta) {
62         _metadata = meta;
63 }
64
65 QString Track::source() const {
66         return _source;
67 }
68
69 void Track::setSource(QString source) {
70         _source = source;
71 }
72
73 int Track::count() const{
74         return _count;
75 }
76
77 void Track::setCount(int count) {
78         _count = count;
79 }
80
81 void Track::decoded(Track track) {
82         _source = track.source();
83         _metadata = track.metadata();
84         delete _resolver;
85 }
86
87 Track &Track::operator =(const Track &track) {
88         _source = track.source();
89         _metadata = track.metadata();
90         _count = track._count;
91         return *this;
92 }
93
94 Track::~Track() {}
95
96 bool Track::operator ==(const Track &track) const {
97         return _source == track._source;
98 }
99
100 bool Track::operator >= (const Track &track) const {
101         return (metadata().title().compare(track.metadata().title()) >= 0);
102 }
103
104 bool Track::operator < (const Track &track) const {
105         return (metadata().title().compare(track.metadata().title()) < 0);
106 }
107
108 QString Track::mediaArtLocal() {
109         QString album = _metadata.album();
110         if (album == _UNKNOWN_ALBUM_) {
111                 return QString("no_mediaartlocal");
112         }
113         QRegExp rsb("\\[.*\\]");
114         QRegExp rfb("{.*}");
115         QRegExp rrb("\\(.*\\)");
116         QRegExp stripB("^[()_{}[]!@#$^&*+=|\\\\/\"'?<>~`\\s\\t]*");
117         QRegExp stripE("[()_{}[]!@#$^&*+=|\\\\/\"'?<>~`\\s\\t]*$");
118         album = album.replace(rsb, "");
119         album = album.replace(rfb, "");
120         album = album.replace(rrb, "");
121         album = album.replace(stripB, "");
122         album = album.replace(stripE, "");
123         album = album.replace("  ", " ");
124         album = album.replace("\t", " ");
125         album = album.toLower();
126         QByteArray first_hash = QCryptographicHash::hash(QString(" ").toUtf8(), QCryptographicHash::Md5).toHex();
127         QByteArray second_hash = QCryptographicHash::hash(QString(album).toUtf8(), QCryptographicHash::Md5).toHex();
128         return QString("album-%1-%2.jpeg").arg(first_hash.constData()).arg(second_hash.constData());
129 }