summaryrefslogtreecommitdiffstats
path: root/qtplaylist.cpp
blob: 65342006738370e60c00ffffe8601895ea46d71a (plain)
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include "qtplaylist.h"

#include "despotify_cpp.h"

QtPlaylist::QtPlaylist(despotify_session *session, QObject *parent)
    : QObject(parent), m_session(session), m_searchResult(0)
{
}

QtPlaylist::~QtPlaylist()
{
    cleanUp();
}

void QtPlaylist::cleanUp()
{
    switch (m_type) {
    case Playlist:
        if (m_playlist != 0) {
            // Can't free playlist, as only the root playlist can have an owner
            // Main window owns root playlist
            m_playlist = 0;
        }
        break;
    case Search:
        if (m_searchResult != 0) {
            despotify_free_search(m_searchResult);
            m_searchResult = 0;
        }
        break;
    case Album:
        if (m_album != 0) {
            despotify_free_album_browse(m_album);
            m_album = 0;
        }
        break;
    case Artist:
        if (m_artist != 0) {
            despotify_free_artist_browse(m_artist);
            m_artist = 0;
        }
        break;
    default:
        break;
    }
}

QList<track *> QtPlaylist::makeList(track *t) const
{
    QList<track *> list;

    while (t != 0) {
        list.append(t);
        t = t->next;
    }

    return list;
}

void QtPlaylist::setPlaylist(playlist *pl)
{
    cleanUp();

    m_type = Playlist;
    m_playlist = pl;
}

void QtPlaylist::setSearchTerm(const QString &searchTerm)
{
    cleanUp();

    m_type = Search;
    m_searchResult = despotify_search(m_session, searchTerm.toUtf8().data(), 50);
}

void QtPlaylist::searchMore()
{
    if (m_type == Search && m_searchResult != 0)
        despotify_search_more(m_session, m_searchResult, m_searchResult->total_tracks, 50);
}

void QtPlaylist::setAlbum(const QByteArray &albumId)
{
    cleanUp();

    m_type = Album;
    m_album = despotify_get_album(m_session, QByteArray(albumId).data());
}

void QtPlaylist::setArtist(const QByteArray &artistId)
{
    cleanUp();

    m_type = Artist;
    m_artist = despotify_get_artist(m_session, QByteArray(artistId).data());
}

QList<track *> QtPlaylist::tracks() const
{
    switch (m_type) {
    case Playlist:
        if (m_playlist != 0)
            return makeList(m_playlist->tracks);
        break;
    case Search:
        if (m_searchResult != 0)
            return makeList(m_searchResult->tracks);
        break;
    case Artist:
        if (m_artist != 0) {
            QList<track *> trackList;

            album_browse *album = m_artist->albums;
            while (album != 0) {
                trackList += makeList(album->tracks);
                album = album->next;
            }

            return trackList;
        }
        break;
    case Album:
        if (m_album != 0)
            return makeList(m_album->tracks);
        break;
    default:
        break;
    }

    return QList<track*>();
}