-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsoundstorage.cpp
86 lines (66 loc) · 2.72 KB
/
soundstorage.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# include "soundstorage.h"
# include "soundcloudapi.h"
SoundStorage::SoundStorage(QObject *parent) : QObject(parent) {
connect(&SoundCloudApi::getInstance(), SIGNAL(likesReceived(QList<Sound>)),
this, SLOT(updateLikes(QList<Sound>)));
connect(&SoundCloudApi::getInstance(), SIGNAL(playlistsReceived(QList<Sound>,QList<Playlist>)),
this, SLOT(updatePlaylists(QList<Sound>,QList<Playlist>)));
connect(&SoundCloudApi::getInstance(), SIGNAL(activitiesReceived(QList<QPair<int,QString> >,QList<Sound>,QList<Playlist>)),
this, SLOT(updateActivities(QList<QPair<int,QString> >,QList<Sound>,QList<Playlist>)));
}
Sound SoundStorage::getSoundById(int id) const {
return sounds.value(id);
}
Playlist SoundStorage::getPlaylistById(int id) const {
return playlists.value(id);
}
// --- public slots
void SoundStorage::fill() {
SoundCloudApi* api = &SoundCloudApi::getInstance();
api->requestLikes();
api->requestPlaylists();
api->requestActivities();
}
// --- private slots
void SoundStorage::updatePlaylists(QList<Sound> containedSongs, QList<Playlist> newPlaylists) {
// merge the sounds into the sound database
for (QList<Sound>::const_iterator it = containedSongs.begin(); it != containedSongs.end(); ++it) {
int id = it->getId();
if (!sounds.contains(id)) {
sounds.insert(id, *it);
}
}
// replace the hashed playlists with the new ones
QList<int> playlistIds;
playlists.clear();
for (QList<Playlist>::const_iterator it = newPlaylists.begin(); it != newPlaylists.end(); ++it) {
playlists.insert(it->getId(), *it);
playlistIds.append(it->getId());
}
emit playlistsUpdated(playlistIds);
}
void SoundStorage::updateLikes(QList<Sound> newLikes) {
QList<int> ids;
for (QList<Sound>::const_iterator it = newLikes.begin(); it != newLikes.end(); ++it) {
int id = it->getId();
ids.append(it->getId());
// save the sound if it is not yet present in our sound database
if (!sounds.contains(id)) {
sounds.insert(id, *it);
}
}
emit likesUpdated(ids);
}
void SoundStorage::updateActivities(QList<QPair<int, QString> > idsAndTypes, QList<Sound> sounds, QList<Playlist> playlists) {
// integrate sounds into sound db
for (QList<Sound>::const_iterator it = sounds.begin(); it != sounds.end(); ++it) {
if (!this->sounds.contains(it->getId())) {
this->sounds.insert(it->getId(), *it);
}
}
// integrate playlists into playlist db
for (QList<Playlist>::const_iterator it = playlists.begin(); it != playlists.end(); ++it) {
this->playlists.insert(it->getId(), *it);
}
emit activitiesUpdated(idsAndTypes);
}