Fixes in desktop file
[someplayer] / src / filestorage.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 "filestorage.h"
21 #include <QDir>
22 #include <QDirIterator>
23 #include <QFileInfo>
24 #include <QTextStream>
25 #include <QRegExp>
26 #include <QDebug>
27
28 using namespace SomePlayer::Storage;
29 using namespace SomePlayer::DataObjects;
30
31 FileStorage::FileStorage(QString path) {
32         _path_prefix = path;
33         _meta_regexp.setPattern("#META\\ +\\[(\\d+)\\]\\[(\\d+)\\].*::(.+)::,::(.+)::,::(.+)::");
34         _path_regexp.setPattern("#PATH (.+)");
35
36         Playlist current = getCurrentPlaylist();
37         if (current.name() == PLAYLIST_BAD_NAME) {
38                 current.setName(_CURRENT_PLAYLIST_NAME_);
39                 saveCurrentPlaylist(current);
40         }
41 }
42
43 QList<Playlist> FileStorage::getPlaylists() {
44         QList<Playlist> stub;
45         return stub;
46 }
47
48 Playlist FileStorage::getPlaylist(QString name) {
49         if (name == _CURRENT_PLAYLIST_SUBST_)
50                 name = _CURRENT_PLAYLIST_NAME_;
51         QFile playlistFile (_path_prefix+"/"+name+"."+_PLAYLIST_FILE_EXTENSION_);
52         Playlist playlist;
53         playlist.setName(PLAYLIST_BAD_NAME);
54         if (playlistFile.exists()) {
55                 playlist.setName(name);
56                 playlistFile.open(QFile::ReadOnly);
57                 QTextStream stream(&playlistFile);
58                 QString buffer = stream.readLine();
59                 if (buffer.startsWith(_PLAYLIST_SIGNATURE_)) {
60                         while (!stream.atEnd()) {
61                                 buffer = stream.readLine();
62                                 if (_meta_regexp.indexIn(buffer) != -1) {
63                                         int id = _meta_regexp.cap(1).toInt();
64                                         int seconds = _meta_regexp.cap(2).toInt();
65                                         QString artist = _meta_regexp.cap(3);
66                                         QString album = _meta_regexp.cap(4);
67                                         QString title = _meta_regexp.cap(5);
68                                         buffer = stream.readLine();
69                                         if (_path_regexp.indexIn(buffer) != -1) {
70                                                 QString source = _path_regexp.cap(1);
71                                                 TrackMetadata meta(title, artist, album, seconds);
72                                                 Track track(id, meta, source);
73                                                 playlist.addTrack(track);
74                                         }
75                                 }
76                         }
77                 }
78         }
79         return playlist;
80 }
81
82 QStringList FileStorage::getPlaylistsNames() {
83         QDir directory(_path_prefix);
84         QDirIterator iterator(directory, QDirIterator::FollowSymlinks);
85         QStringList playlistNames;
86         while (iterator.hasNext()) {
87                 QString entry = iterator.next();
88                 QFileInfo info(entry);
89                 QString suffix = info.suffix().toLower();
90                 if (suffix == _PLAYLIST_FILE_EXTENSION_) {
91                         QString name = info.fileName().replace(QString(".%1").arg(_PLAYLIST_FILE_EXTENSION_), "", Qt::CaseInsensitive);
92                         if (name == _CURRENT_PLAYLIST_NAME_)
93                             name = _CURRENT_PLAYLIST_SUBST_;
94                         playlistNames.append(name);
95                 }
96         }
97         return playlistNames;
98 }
99
100 void FileStorage::savePlaylist(Playlist playlist) {
101         QString name = playlist.name();
102         if (playlist.name() == _CURRENT_PLAYLIST_SUBST_)
103                 name = _CURRENT_PLAYLIST_NAME_;
104         QString filename = _path_prefix + "/" +name+"."_PLAYLIST_FILE_EXTENSION_;
105         QFile playlistFile(filename);
106         if (playlistFile.exists()) {
107                 playlistFile.remove();
108         }
109         playlistFile.open(QFile::WriteOnly);
110         QTextStream stream(&playlistFile);
111         stream << _PLAYLIST_SIGNATURE_ << endl;
112         const QList<Track> &tracks = playlist.tracks();
113         foreach (Track track, tracks) {
114                 stream << _PLAYLIST_META_KEYWORD_ << " [" << track.id() << "]" << "[" << track.metadata().length() << "],::"
115                                 << track.metadata().artist() << "::,::" << track.metadata().album() << "::,::"
116                                 << track.metadata().title() << "::" << endl;
117                 stream << _PLAYLIST_PATH_KEYWORD_ << " " << track.source() << endl;
118         }
119 }
120
121 void FileStorage::removePlaylist(Playlist playlist) {
122         QString filename = _path_prefix + "/" + playlist.name() + "." + _PLAYLIST_FILE_EXTENSION_;
123         QFile file(filename);
124         file.remove();
125 }
126
127 void FileStorage::removePlaylist(QString name) {
128         QString filename = _path_prefix + "/" + (name == _CURRENT_PLAYLIST_SUBST_ ? _CURRENT_PLAYLIST_NAME_ : name)
129                            + "." + _PLAYLIST_FILE_EXTENSION_;
130         QFile file(filename);
131         file.remove();
132 }
133
134 Playlist FileStorage::getCurrentPlaylist() {
135         return getPlaylist(_CURRENT_PLAYLIST_NAME_);
136 }
137
138 void FileStorage::saveCurrentPlaylist(Playlist playlist) {
139         playlist.setName(_CURRENT_PLAYLIST_NAME_);
140         savePlaylist(playlist);
141 }