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>
27 using namespace SomePlayer::Storage;
28 using namespace SomePlayer::DataObjects;
30 FileStorage::FileStorage(QString path) {
32 _meta_regexp.setPattern("#META\\ +\\[(\\d+)\\]\\[(\\d+)\\].*::(.+)::,::(.+)::,::(.+)::");
33 _path_regexp.setPattern("#PATH (.+)");
35 Playlist current = getCurrentPlaylist();
36 if (current.name() == PLAYLIST_BAD_NAME) {
37 current.setName(_CURRENT_PLAYLIST_NAME_);
38 saveCurrentPlaylist(current);
42 QList<Playlist> FileStorage::getPlaylists() {
47 Playlist FileStorage::getPlaylist(QString name) {
48 if (name == _CURRENT_PLAYLIST_SUBST_)
49 name = _CURRENT_PLAYLIST_NAME_;
50 QFile playlistFile (_path_prefix+"/"+name+"."+_PLAYLIST_FILE_EXTENSION_);
52 playlist.setName(PLAYLIST_BAD_NAME);
53 if (playlistFile.exists()) {
54 playlist.setName(name);
55 playlistFile.open(QFile::ReadOnly);
56 QTextStream stream(&playlistFile);
57 QString buffer = stream.readLine();
58 if (buffer.startsWith(_PLAYLIST_SIGNATURE_)) {
59 while (!stream.atEnd()) {
60 buffer = stream.readLine();
61 if (_meta_regexp.indexIn(buffer) != -1) {
62 int id = _meta_regexp.cap(1).toInt();
63 int seconds = _meta_regexp.cap(2).toInt();
64 QString artist = _meta_regexp.cap(3);
65 QString album = _meta_regexp.cap(4);
66 QString title = _meta_regexp.cap(5);
67 buffer = stream.readLine();
68 if (_path_regexp.indexIn(buffer) != -1) {
69 QString source = _path_regexp.cap(1);
70 TrackMetadata meta(title, artist, album, seconds);
71 Track track(id, meta, source);
72 playlist.addTrack(track);
81 QStringList FileStorage::getPlaylistsNames() {
82 QDir directory(_path_prefix);
83 QDirIterator iterator(directory, QDirIterator::FollowSymlinks);
84 QStringList playlistNames;
85 while (iterator.hasNext()) {
86 QString entry = iterator.next();
87 QFileInfo info(entry);
88 QString suffix = info.suffix().toLower();
89 if (suffix == _PLAYLIST_FILE_EXTENSION_) {
90 QString name = info.fileName().replace(QString(".%1").arg(_PLAYLIST_FILE_EXTENSION_), "", Qt::CaseInsensitive);
91 if (name == _CURRENT_PLAYLIST_NAME_)
92 name = _CURRENT_PLAYLIST_SUBST_;
93 playlistNames.append(name);
99 void FileStorage::savePlaylist(Playlist playlist) {
100 QString name = playlist.name();
101 if (playlist.name() == _CURRENT_PLAYLIST_SUBST_)
102 name = _CURRENT_PLAYLIST_NAME_;
103 QString filename = _path_prefix + "/" +name+"."_PLAYLIST_FILE_EXTENSION_;
104 QFile playlistFile(filename);
105 if (playlistFile.exists()) {
106 playlistFile.remove();
108 playlistFile.open(QFile::WriteOnly);
109 QTextStream stream(&playlistFile);
110 stream << _PLAYLIST_SIGNATURE_ << endl;
111 const QList<Track> &tracks = playlist.tracks();
112 foreach (Track track, tracks) {
113 stream << _PLAYLIST_META_KEYWORD_ << " [" << track.id() << "]" << "[" << track.metadata().length() << "],::"
114 << track.metadata().artist() << "::,::" << track.metadata().album() << "::,::"
115 << track.metadata().title() << "::" << endl;
116 stream << _PLAYLIST_PATH_KEYWORD_ << " " << track.source() << endl;
120 void FileStorage::removePlaylist(Playlist playlist) {
121 QString filename = _path_prefix + "/" + playlist.name() + "." + _PLAYLIST_FILE_EXTENSION_;
122 QFile file(filename);
126 void FileStorage::removePlaylist(QString name) {
127 QString filename = _path_prefix + "/" + (name == _CURRENT_PLAYLIST_SUBST_ ? _CURRENT_PLAYLIST_NAME_ : name)
128 + "." + _PLAYLIST_FILE_EXTENSION_;
129 QFile file(filename);
133 Playlist FileStorage::getCurrentPlaylist() {
134 return getPlaylist(_CURRENT_PLAYLIST_NAME_);
137 void FileStorage::saveCurrentPlaylist(Playlist playlist) {
138 playlist.setName(_CURRENT_PLAYLIST_NAME_);
139 savePlaylist(playlist);