7f8165b265de93909185c9dd439831a77670c43a
[someplayer] / src / library.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 "library.h"
21 #include <QDebug>
22
23 using namespace SomePlayer::DataObjects;
24 using namespace SomePlayer::Storage;
25
26 #include "mediascanner.h"
27 #include <QDir>
28
29 Library::Library(QString databasePath, QString playlistsPath) : QObject(0) {
30         _library_storage = new DbStorage(databasePath);
31         _playlist_storage = new FileStorage(playlistsPath);
32         _scanner = new MediaScanner();
33         _resolver = new TagResolver(this);
34         connect(_scanner, SIGNAL(scanFinish(QStringList)), this, SLOT(_scanned(QStringList)));
35         connect(_resolver, SIGNAL(done()), this, SIGNAL(done()));
36         connect(_resolver, SIGNAL(decoded(Track)), this, SLOT(_decoded(Track)));
37 }
38
39 Library::~Library() {
40         delete _library_storage;
41         delete _playlist_storage;
42 }
43
44 void Library::addDirectory(QString path) {
45         _scanner->init(path);
46         _scanner->start(QThread::LowestPriority);
47 }
48
49 void Library::addFile(QString path) {
50         QStringList files(path);
51         _resolver->decode(files);
52 }
53
54 QList<QString> Library::getArtists() {
55         return _library_storage->getArtists();
56 }
57
58 QList<QString> Library::getAlbumsForArtist(QString artist) {
59         return _library_storage->getAlbumsForArtist(artist);
60 }
61
62 QList<Track> Library::getTracksForAlbum(QString album, QString artist) {
63         return _library_storage->getTracksForAlbum(album, artist);
64 }
65
66
67 // dynamic:
68
69 Playlist Library::getFavorites() {
70         return _library_storage->getFavorites();
71 }
72
73 Playlist Library::getMostPlayed() {
74         return _library_storage->getMostPlayed();
75 }
76
77 Playlist Library::getNeverPlayed() {
78         return _library_storage->getNeverPlayed();
79 }
80
81 Playlist Library::getRecentlyAdded() {
82         return _library_storage->getRecentlyAdded();
83 }
84
85
86 void Library::removeTrack(Track track) {
87         _library_storage->removeTrack(track);
88 }
89
90 void Library::addTrack(Track track) {
91         _library_storage->addTrack(track);
92 }
93
94 void Library::addToFavorites(Track track) {
95         _library_storage->addToFavorites(track);
96 }
97
98 void Library::updateTrackCount(Track track) {
99         _library_storage->updateTrackCount(track);
100 }
101
102
103 // playlists:
104
105 QList<Playlist> Library::getPlaylists() {
106         return _playlist_storage->getPlaylists();
107 }
108
109 QStringList Library::getPlaylistsNames() {
110         return _playlist_storage->getPlaylistsNames();
111 }
112
113 Playlist Library::getPlaylist(QString name) {
114         return _playlist_storage->getPlaylist(name);
115 }
116
117 void Library::savePlaylist(const Playlist &playlist) {
118         _playlist_storage->savePlaylist(playlist);
119 }
120
121 void Library::removePlaylist(const Playlist &playlist) {
122         _playlist_storage->removePlaylist(playlist);
123 }
124
125 void Library::removePlaylist(QString name) {
126         _playlist_storage->removePlaylist(name);
127 }
128
129 Playlist Library::getCurrentPlaylist() {
130         return _playlist_storage->getCurrentPlaylist();
131 }
132
133 void Library::saveCurrentPlaylist(const Playlist &playlist) {
134         _playlist_storage->saveCurrentPlaylist(playlist);
135 }
136
137 void Library::_decoded(Track track) {
138         emit trackAdded();
139         addTrack(track);
140 }
141
142 void Library::_scanned(QStringList files) {
143         emit addingTracks(files.count());
144         _resolver->decode(files);
145 }
146
147 void Library::updateTrackMetadata(Track track) {
148         Track ntrack = track;
149         if (track.id() > 0) {
150                 ntrack = _library_storage->updateTrack(track);
151         }
152         // update all playlists
153         QList<QString> playlists = getPlaylistsNames();
154         foreach (QString name, playlists) {
155                 Playlist pl = getPlaylist(name);
156                 QList<Track> tracks = pl.tracks();
157                 int pos = tracks.indexOf(ntrack); // comparing using source
158                 tracks.removeOne(ntrack); // comparing using source
159                 tracks.insert(pos, ntrack);
160                 pl.setTracks(tracks);
161                 savePlaylist(pl);
162         }
163         _resolver->updateTags(ntrack);
164 }