forked from Elleo/libQtSpotify
-
Notifications
You must be signed in to change notification settings - Fork 3
/
qspotifycachemanager.cpp
114 lines (93 loc) · 2.7 KB
/
qspotifycachemanager.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include "qspotifycachemanager.h"
#include <QtCore/QObject>
#include <QtCore/QEvent>
#include <QtCore/QCoreApplication>
#include <QtCore/QTime>
#include <QtCore/QHash>
#include <QtCore/QMutex>
#include <QtCore/QMutexLocker>
#include <QtCore/QThread>
#include <QtCore/QDebug>
#include <libspotify/api.h>
#include "qspotifytrack.h"
#include "qspotifyplaylist.h"
#include "qspotifyartist.h"
#include "qspotifyalbum.h"
QSpotifyCacheManager &QSpotifyCacheManager::instance() {
static QSpotifyCacheManager inst;
return inst;
}
void QSpotifyCacheManager::removeObject(QSpotifyObject *obj)
{
if (auto track = dynamic_cast<QSpotifyTrack*>(obj)) {
auto it = m_tracks.find(track->sptrack());
if (it != m_tracks.end()) m_tracks.erase(it);
} else if (auto album = dynamic_cast<QSpotifyAlbum*>(obj)) {
auto it = m_albums.find(album->spalbum());
if (it != m_albums.end()) m_albums.erase(it);
} else if (auto artist = dynamic_cast<QSpotifyArtist*>(obj)) {
auto it = m_artists.find(artist->spartist());
if (it != m_artists.end()) m_artists.erase(it);
} else {
qDebug() << "Try to remove unknown object from cache";
}
}
QSpotifyTrack *QSpotifyCacheManager::getTrack(sp_track *t, QSpotifyPlaylist *playlist)
{
Q_ASSERT(t);
auto iter = m_tracks.find(t);
if(iter != m_tracks.end()) {
if(auto ptr = iter.value()) {
ptr->addRef();
return ptr;
}
}
auto qtrack = new QSpotifyTrack(t, playlist);
qtrack->init();
// XXX: possibility to not display unavalaible tracks: if (!qtrack->isAvailable()) return nullptr;
m_tracks.insert(t, qtrack);
if(playlist) {
playlist->registerTrackType(qtrack);
}
return qtrack;
}
QSpotifyArtist *QSpotifyCacheManager::getArtist(sp_artist *a)
{
Q_ASSERT(a);
if (!a) {
return nullptr;
}
auto iter = m_artists.find(a);
if(iter != m_artists.end()) {
if(auto ptr = iter.value()) {
ptr->addRef();
return ptr;
}
}
auto artPtr = new QSpotifyArtist(a);
artPtr->init();
m_artists.insert(a, artPtr);
return artPtr;
}
QSpotifyAlbum *QSpotifyCacheManager::getAlbum(sp_album *a)
{
Q_ASSERT(a);
if (!a) {
return nullptr;
}
auto iter = m_albums.find(a);
if(iter != m_albums.end()) {
if(auto ptr = iter.value()) {
ptr->addRef();
return ptr;
}
}
auto albPtr = new QSpotifyAlbum(a);
albPtr->init();
m_albums.insert(a, albPtr);
return albPtr;
}
void QSpotifyCacheManager::cacheInfo()
{
qDebug() << "#Cache Info: Tracks" << m_tracks.size() << "Artists" << m_artists.size() << "Ablums" << m_albums.size();
}