We are no longer offering accounts on this server. Consider https://gitlab.freedesktop.org/ as a place to host projects.

servercomm.cpp 8 KB
Newer Older
Mike Sheldon's avatar
Mike Sheldon committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#include <QDebug>
#include <QCryptographicHash>
#include <QUrl>
#include <QtNetwork/QNetworkRequest>
#include <QtNetwork/QNetworkReply>
#include <QDateTime>
#include <QtXml/QDomDocument>
#include "servercomm.h"

ServerComm::ServerComm(QObject *parent) :
    QObject(parent)
{
    hs_url = "http://turtle.libre.fm/";
    ws_url = "http://alpha.libre.fm/2.0/";
Mike Sheldon's avatar
Mike Sheldon committed
15
    playlist = new QList<Track>();
16
    settings = new QSettings("Libre.fm", "Libre.fm");
17
    currentSong = -1;
18 19 20 21 22 23 24 25

    // Check login details
    qDebug() << "Checking settings...";
    if (settings->contains("Auth/username") && settings->contains("Auth/password")) {
        qDebug() << "Logging in...";
        login(settings->value("Auth/username").toString(), settings->value("Auth/password").toString());
        qDebug() << "Username:" << settings->value("Auth/username").toString();
    }
Mike Sheldon's avatar
Mike Sheldon committed
26 27 28 29
}

void ServerComm::login(const QString &username, const QString &password) {
    qDebug() << "Logging in...";
30 31 32 33
    if(username.isEmpty() || password.isEmpty()) {
        loginFailed();
        return;
    }
Mike Sheldon's avatar
Mike Sheldon committed
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
    QString passMD5 = QCryptographicHash::hash(QByteArray(password.toAscii()), QCryptographicHash::Md5).toHex();
    long timestamp = QDateTime::currentDateTime().toTime_t();
    QString token = QCryptographicHash::hash(QByteArray(QString(QString(passMD5) + QString::number(timestamp)).toAscii()), QCryptographicHash::Md5).toHex();
    QString wstoken = QCryptographicHash::hash(QByteArray(QString(username.toLower() + QString(passMD5)).toAscii()), QCryptographicHash::Md5).toHex();
    // Scrobble login
    QNetworkAccessManager *s_netman = new QNetworkAccessManager(this);
    connect(s_netman, SIGNAL(finished(QNetworkReply*)), this, SLOT(scrobbleLoginReply(QNetworkReply*)));
    QUrl url = QUrl(hs_url);
    url.addQueryItem("hs", "true");
    url.addQueryItem("p", "1.2");
    url.addQueryItem("c", "mee");
    url.addQueryItem("t", QString::number(timestamp));
    url.addQueryItem("u", username);
    url.addQueryItem("a", token);
    s_netman->get(QNetworkRequest(url));

    QNetworkAccessManager *ws_netman = new QNetworkAccessManager(this);
    connect(ws_netman, SIGNAL(finished(QNetworkReply*)), this, SLOT(wsLoginReply(QNetworkReply*)));
    // Webservice login
    url = QUrl(ws_url);
    url.addQueryItem("method", "auth.getmobilesession");
    url.addQueryItem("username", username);
    url.addQueryItem("authToken", wstoken);
    qDebug() << "AuthToken:" << wstoken;
    ws_netman->get(QNetworkRequest(url));
59 60 61 62

    // Save authentication details
    settings->setValue("Auth/username", username);
    settings->setValue("Auth/password", password);
Mike Sheldon's avatar
Mike Sheldon committed
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
}

void ServerComm::scrobbleLoginReply(QNetworkReply *reply) {
    char line[256];
    reply->readLine(line, 256);
    if (QString(line).contains("BADAUTH")) {
        qDebug() << "Scrobble login failed.";
        loginFailed();
    } else if(QString(line).contains("OK")) {
            reply->readLine(line, 256);
            scrobble_sk = QString(line).trimmed();
            reply->readLine(line, 256);
            np_url = QString(line).trimmed();
            reply->readLine(line, 256);
            scr_url = QString(line).trimmed();
            qDebug() << "Scrobble login complete, scrobble session key is" << scrobble_sk << "Now playing URL:" << np_url << "Scrobble URL:" << scr_url;
            if (!ws_sk.isEmpty()) {
                loginSuccessful();
            }
    }

}

void ServerComm::wsLoginReply(QNetworkReply *reply) {
    QDomDocument xml("wsresponse");
    xml.setContent(reply);
    QDomElement root = xml.documentElement();
    for(QDomNode n = root.firstChild(); !n.isNull(); n = n.nextSibling()) {
        QDomElement e = n.toElement();
        if(!e.isNull()) {
            if(e.tagName() == "error") {
                loginFailed();
                return;
            }

            if(e.tagName() == "session") {
                for(QDomNode c = n.firstChild(); !c.isNull(); c = c.nextSibling()) {
                    QDomElement ce = c.toElement();
                    if(ce.tagName() == "key") {
                        ws_sk = ce.text();
                        qDebug() << "Webservice key:" << ws_sk;
                        if (!scrobble_sk.isEmpty()) {
                            loginSuccessful();
                        }
                    }
                }
            }
        }
    }
}

114 115
void ServerComm::tuneStation(const QString &station) {
    qDebug() << "Tuning to station: " << station;
116 117
    // Clear the playlist
    playlist = new QList<Track>();
118 119 120
    QNetworkAccessManager *tune_netman = new QNetworkAccessManager(this);
    connect(tune_netman, SIGNAL(finished(QNetworkReply*)), this, SLOT(tuneReply(QNetworkReply*)));
    QUrl url = QUrl(ws_url);
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
    QByteArray params;
    params.append("method=radio.tune");
    params.append("&sk="); params.append(ws_sk);
    params.append("&station="); params.append(station);
    tune_netman->post(QNetworkRequest(url), params);
}

void ServerComm::tuneReply(QNetworkReply *reply) {
    QDomDocument xml("tuneresponse");
    xml.setContent(reply);
    QDomElement root = xml.documentElement();
    for(QDomNode n = root.firstChild(); !n.isNull(); n = n.nextSibling()) {
        QDomElement e = n.toElement();
        if(!e.isNull()) {
            if(e.tagName() == "error") {
                qWarning() << "Station tuning failed.";
            }

            if(e.tagName() == "station") {
                for(QDomNode c = n.firstChild(); !c.isNull(); c = c.nextSibling()) {
                    QDomElement ce = c.toElement();
                    if(ce.tagName() == "name") {
                        QString stationName = ce.text().remove(0, 9); // Remove 'Libre.fm' from the start of station names
                        qDebug() << "Tuned to:" << stationName;
                        tuned(stationName);
Mike Sheldon's avatar
Mike Sheldon committed
146
                        getPlaylist();
147 148 149 150 151
                    }
                }
            }
        }
    }
Mike Sheldon's avatar
Mike Sheldon committed
152
}
Mike Sheldon's avatar
Mike Sheldon committed
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182

void ServerComm::getPlaylist() {
    qDebug() << "Fetching playlist";

    QNetworkAccessManager *playlist_netman = new QNetworkAccessManager(this);
    connect(playlist_netman, SIGNAL(finished(QNetworkReply*)), this, SLOT(playlistReply(QNetworkReply*)));
    QUrl url = QUrl(ws_url);
    url.addQueryItem("method", "radio.getPlaylist");
    url.addQueryItem("sk", ws_sk);
    playlist_netman->get(QNetworkRequest(url));
}

void ServerComm::playlistReply(QNetworkReply *reply) {
    qDebug() << "Playlist retrieved";
    QDomDocument xml("playlist");
    xml.setContent(reply);
    QDomElement root = xml.documentElement();
    for(QDomNode n = root.firstChild(); !n.isNull(); n = n.nextSibling()) {
        QDomElement e = n.toElement();
        if(!e.isNull()) {
            if(e.tagName() == "trackList") {
                for(QDomNode c = n.firstChild(); !c.isNull(); c = c.nextSibling()) {
                    QDomElement ce = c.toElement();
                    if(ce.tagName() == "track") {
                        parseTrack(c);
                    }
                }
            }
        }
    }
183 184 185
    if(currentSong == -1) {
        play(0);
    }
Mike Sheldon's avatar
Mike Sheldon committed
186 187 188 189 190 191 192
}

void ServerComm::parseTrack(QDomNode trackNode) {
    Track *t = new Track();
    for(QDomNode n = trackNode.firstChild(); !n.isNull(); n = n.nextSibling()) {
        QDomElement e = n.toElement();
        if(!e.isNull()) {
193
            if(e.tagName() == "creator") {
Mike Sheldon's avatar
Mike Sheldon committed
194 195 196 197 198 199 200 201 202 203 204 205
                t->artist = e.text();
            } else if(e.tagName() == "album") {
                t->album = e.text();
            } else if(e.tagName() == "title") {
                t->title = e.text();
            } else if(e.tagName() == "location") {
                t->location = e.text();
            } else if(e.tagName() == "image") {
                t->image = e.text();
            }
        }
    }
206
    qDebug() << "Artist: " << t->artist;
Mike Sheldon's avatar
Mike Sheldon committed
207 208 209 210
    playlist->append(*t);
}

void ServerComm::play(int song) {
211 212
    Track t = playlist->at(song);
    playing(t.artist, t.album, t.title, t.image);
Mike Sheldon's avatar
Mike Sheldon committed
213

214 215 216 217 218 219 220 221 222 223 224 225 226
    if (song >= playlist->length() - 3) {
        getPlaylist();
    }
}

void ServerComm::next() {
    play(currentSong++);
}

void ServerComm::prev() {
    if(currentSong > 1) {
        play(currentSong--);
    }
Mike Sheldon's avatar
Mike Sheldon committed
227
}