Implemented playlist storage
[someplayer] / src / filestorage.cpp
1 #include "filestorage.h"
2 #include <QDir>
3 #include <QDirIterator>
4 #include <QFileInfo>
5 #include <QTextStream>
6 #include <QRegExp>
7
8 using namespace SomePlayer::Storage;
9 using namespace SomePlayer::DataObjects;
10
11 FileStorage::FileStorage(QString path) {
12         _path_prefix = path;
13         _meta_regexp.setPattern("#META \\[(\\d+)\\].*::(.+)::,::(.+)::,::(.+)::");
14         _path_regexp.setPattern("#PATH (.+)");
15 }
16
17 QList<Playlist> FileStorage::getPlaylists() {
18         QList<Playlist> stub;
19         return stub;
20 }
21
22 Playlist FileStorage::getPlaylist(QString name) {
23         QFile playlistFile (_path_prefix+"/"+name+_PLAYLIST_FILE_EXTENSION_);
24         Playlist playlist;
25         playlist.setName("Bad playlist");
26         if (playlistFile.exists()) {
27                 playlist.setName(name);
28                 QTextStream stream(&playlistFile);
29                 QString buffer = stream.readLine();
30                 int index = 0;
31                 if (buffer.startsWith(_PLAYLIST_SIGNATURE_)) {
32                         while (!stream.atEnd()) {
33                                 buffer = stream.readLine();
34                                 if (_meta_regexp.indexIn(buffer) != -1) {
35                                         int seconds = _meta_regexp.cap(0).toInt();
36                                         QString artist = _meta_regexp.cap(1);
37                                         QString album = _meta_regexp.cap(2);
38                                         QString title = _meta_regexp.cap(3);
39                                         buffer = stream.readLine();
40                                         if (_path_regexp.indexIn(buffer) != -1) {
41                                                 QString source = _path_regexp.cap(0);
42                                                 TrackMetadata meta(title, artist, album, seconds);
43                                                 Track track(index++, meta, source);
44                                                 playlist.addTrack(track);
45                                         }
46                                 }
47                         }
48                 }
49         }
50         return playlist;
51 }
52
53 QStringList FileStorage::getPlaylistsNames() {
54         QDir directory(_path_prefix);
55         QDirIterator iterator(directory, QDirIterator::FollowSymlinks);
56         QStringList playlistNames;
57         while (iterator.hasNext()) {
58                 QString entry = iterator.next();
59                 QFileInfo info(entry);
60                 QString suffix = info.suffix().toLower();
61                 if (suffix == _PLAYLIST_FILE_EXTENSION_) {
62                         playlistNames.append(info.fileName());
63                 }
64         }
65         return playlistNames;
66 }
67
68 void FileStorage::savePlaylist(Playlist playlist) {
69         QString filename = _path_prefix+playlist.name()+_PLAYLIST_FILE_EXTENSION_;
70         QFile playlistFile(filename);
71         if (playlistFile.exists()) {
72                 playlistFile.remove();
73         }
74         QTextStream stream(&playlistFile);
75         stream << _PLAYLIST_SIGNATURE_ << endl;
76         const QList<Track> &tracks = playlist.tracks();
77         foreach (Track track, tracks) {
78                 stream << _PLAYLIST_META_KEYWORD_ << " [" << track.metadata().length() << "],::" << track.metadata().artist()
79                                 << "::,::" << track.metadata().album() << "::,::" << track.metadata().title() << "::" << endl;
80                 stream << _PLAYLIST_PATH_KEYWORD_ << " " << track.source() << endl;
81         }
82 }
83
84 void FileStorage::removePlaylist(Playlist playlist) {
85         QString filename = _path_prefix + "/" + playlist.name() + "." + _PLAYLIST_FILE_EXTENSION_;
86         QFile file(filename);
87         file.remove();
88 }
89
90 void FileStorage::removePlaylist(QString name) {
91         QString filename = _path_prefix + "/" + name + "." + _PLAYLIST_FILE_EXTENSION_;
92         QFile file(filename);
93         file.remove();
94 }
95
96 Playlist FileStorage::getCurrentPlaylist() {
97         return getPlaylist(_CURRENT_PLAYLIST_NAME_);
98 }
99
100 void FileStorage::saveCurrentPlaylist(Playlist playlist) {
101         playlist.setName(_CURRENT_PLAYLIST_NAME_);
102         savePlaylist(playlist);
103 }