2 * SomePlayer - An alternate music player for Maemo 5
3 * Copyright (C) 2010 Nikolay (somebody) Tischenko <niktischenko@gmail.com>
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.
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.
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.
20 #include "filestorage.h"
22 #include <QDirIterator>
24 #include <QTextStream>
25 #include <QtXml/QtXml>
27 using namespace SomePlayer::Storage;
28 using namespace SomePlayer::DataObjects;
30 FileStorage::FileStorage(QString path) {
33 Playlist current = getCurrentPlaylist();
34 if (current.name() == PLAYLIST_BAD_NAME) {
35 current.setName(_CURRENT_PLAYLIST_NAME_);
36 saveCurrentPlaylist(current);
40 QList<Playlist> FileStorage::getPlaylists() {
45 Playlist FileStorage::getPlaylist(QString name) {
46 if (name == _CURRENT_PLAYLIST_SUBST_)
47 name = _CURRENT_PLAYLIST_NAME_;
48 QFile playlistFile (_path_prefix+"/"+name+"."+_PLAYLIST_FILE_EXTENSION_);
50 playlist.setName(PLAYLIST_BAD_NAME);
51 if (playlistFile.exists()) {
52 playlist.setName(name);
54 playlistFile.open(QFile::ReadOnly);
55 doc.setContent(&playlistFile);
57 QDomElement eplaylist = doc.documentElement();
58 if (eplaylist.tagName() == "playlist") {
59 QDomElement etracklist = eplaylist.firstChildElement("trackList");
60 if (!etracklist.isNull()) {
61 QDomElement etrack = etracklist.firstChildElement("track");
62 while (!etrack.isNull()) {
63 QDomElement elocation = etrack.firstChildElement("location");
64 QDomElement eextension = etrack.firstChildElement("extension");
65 if (!eextension.isNull()) {
66 QDomElement ecl_clip = eextension.firstChildElement("cl:clip");
67 if (!ecl_clip.isNull()) {
68 QString artist = ecl_clip.attribute("artist");
69 QString album = ecl_clip.attribute("album");
70 QString title = ecl_clip.attribute("title");
71 QDomElement eduration = etrack.firstChildElement("duration");
72 if (!eduration.isNull()) {
73 QVariant duration = eduration.text();
75 basource.append(elocation.text());
76 QString source = QUrl::fromEncoded(basource).toLocalFile();
77 TrackMetadata meta(title, artist, album, duration.toInt()/1000);
78 Track track(meta, source);
79 playlist.addTrack(track);
83 etrack = etrack.nextSiblingElement("track");
91 QStringList FileStorage::getPlaylistsNames() {
92 QDir directory(_path_prefix);
93 QDirIterator iterator(directory, QDirIterator::FollowSymlinks);
94 QStringList playlistNames;
95 while (iterator.hasNext()) {
96 QString entry = iterator.next();
97 QFileInfo info(entry);
98 QString suffix = info.suffix().toLower();
99 if (suffix == _PLAYLIST_FILE_EXTENSION_) {
100 QString name = info.fileName().replace(QString(".%1").arg(_PLAYLIST_FILE_EXTENSION_), "", Qt::CaseInsensitive);
101 if (name == _CURRENT_PLAYLIST_NAME_)
102 name = _CURRENT_PLAYLIST_SUBST_;
103 playlistNames.append(name);
106 return playlistNames;
109 void FileStorage::savePlaylist(Playlist playlist) {
110 QString name = playlist.name();
111 if (playlist.name() == _CURRENT_PLAYLIST_SUBST_)
112 name = _CURRENT_PLAYLIST_NAME_;
113 QString filename = _path_prefix + "/" +name+"."_PLAYLIST_FILE_EXTENSION_;
114 QFile playlistFile(filename);
115 if (playlistFile.exists()) {
116 playlistFile.remove();
118 playlistFile.open(QFile::WriteOnly);
119 QTextStream stream(&playlistFile);
121 QDomElement root = doc.createElement("playlist");
122 root.setAttribute("version", "1");
123 root.setAttribute("xmlns", "http://xspf.org/ns/0/");
124 root.setAttribute("xmlns:cl", "http://example.com");
125 QDomElement tracklist = doc.createElement("trackList");
126 foreach (Track track, playlist.tracks()) {
127 QDomElement etrack = doc.createElement("track");
128 QDomElement elocation = doc.createElement("location");
129 QDomElement eextension = doc.createElement("extension");
130 QDomElement ecl_clip = doc.createElement("cl:clip");
131 QDomElement etitle = doc.createElement("title");
132 QDomElement eduration = doc.createElement("duration");
133 etitle.appendChild(doc.createTextNode(QString("%1 - %2").arg(track.metadata().artist()).arg(track.metadata().title())));
134 ecl_clip.setAttribute("title", track.metadata().title());
135 ecl_clip.setAttribute("artist", track.metadata().artist());
136 ecl_clip.setAttribute("album", track.metadata().album());
137 eextension.appendChild(ecl_clip);
138 eextension.setAttribute("application", "http://example.com");
139 elocation.appendChild(doc.createTextNode(QString("%1").arg(QUrl::fromLocalFile(track.source()).toEncoded().constData())));
140 eduration.appendChild(doc.createTextNode(QString("%1").arg(track.metadata().length()*1000)));
141 etrack.appendChild(elocation);
142 etrack.appendChild(eextension);
143 etrack.appendChild(etitle);
144 etrack.appendChild(eduration);
145 tracklist.appendChild(etrack);
147 root.appendChild(tracklist);
148 doc.appendChild(root);
149 stream << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
150 stream << doc.toString();
153 void FileStorage::removePlaylist(Playlist playlist) {
154 QString filename = _path_prefix + "/" + playlist.name() + "." + _PLAYLIST_FILE_EXTENSION_;
155 QFile file(filename);
159 void FileStorage::removePlaylist(QString name) {
160 QString filename = _path_prefix + "/" + (name == _CURRENT_PLAYLIST_SUBST_ ? _CURRENT_PLAYLIST_NAME_ : name)
161 + "." + _PLAYLIST_FILE_EXTENSION_;
162 QFile file(filename);
166 Playlist FileStorage::getCurrentPlaylist() {
167 return getPlaylist(_CURRENT_PLAYLIST_NAME_);
170 void FileStorage::saveCurrentPlaylist(Playlist playlist) {
171 playlist.setName(_CURRENT_PLAYLIST_NAME_);
172 savePlaylist(playlist);