a91c8e2c42923f55057c63fd173bb16b837974c1
[someplayer] / src / tagresolver.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 "tagresolver.h"
21 #include <QFile>
22 #include <tag.h>
23 #include <fileref.h>
24
25 using namespace SomePlayer::DataObjects;
26
27 TagResolver::TagResolver(QObject *parent) :
28     QObject(parent)
29 {
30 }
31
32 void TagResolver::decode(QStringList files) {
33         foreach (QString filename, files) {
34                 TagLib::FileRef file_ref(QFile::encodeName(filename).data());
35                 if (!file_ref.isNull()) {
36                         TagLib::Tag *tag = file_ref.tag();
37                         if (NULL != tag) {
38                                 TagLib::AudioProperties *properties = file_ref.audioProperties();
39                                 if (NULL != properties) {
40                                         TrackMetadata meta(QString::fromStdWString(tag->title().toWString()),
41                                                            QString::fromStdWString(tag->artist().toWString()),
42                                                            QString::fromStdWString(tag->album().toWString()),
43                                                            properties->length());
44                                         Track track(0, meta, filename);
45                                         emit decoded(track);
46                                 }
47                         }
48                 }
49         }
50         emit done();
51 }
52
53 void TagResolver::updateTags(Track track) {
54         TagLib::FileRef file_ref(QFile::encodeName(track.source()).data());
55         if (!file_ref.isNull()) {
56                 TagLib::Tag *tag = file_ref.tag();
57                 if (NULL != tag) {
58                         tag->setArtist(TagLib::String(track.metadata().artist().toStdWString()));
59                         tag->setAlbum(TagLib::String(track.metadata().album().toStdWString()));
60                         tag->setTitle(TagLib::String(track.metadata().title().toStdWString()));
61                 }
62                 file_ref.save();
63         }
64 }