/* * Cantata Web * * Copyright (c) 2015 Craig Drummond * * ---- * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; see the file COPYING. If not, write to * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301, USA. */ #include "playqueueapi.h" #include "db/mpdlibrarydb.h" #include "http/httprequest.h" #include "http/httpresponse.h" #include "httpserver.h" #include "mpd-interface/mpdconnection.h" #include "mpd-interface/mpdparseutils.h" #include "streams/streamfetcher.h" #include "support/localize.h" #include #define DBUG if (ApiHandler::debugEnabled()) qWarning() << metaObject()->className() << (void *)this << __FUNCTION__ static QString streamText(const Song &song, const QString &trackTitle, bool useName=true) { if (song.album.isEmpty() && song.albumArtist().isEmpty()) { QString songName=song.name(); return song.title.isEmpty() && songName.isEmpty() ? song.file : !useName || songName.isEmpty() ? song.title : song.title.isEmpty() ? songName : (song.title + " - " + songName); } else if (!song.title.isEmpty() && !song.artist.isEmpty()) { QString name=song.name(); return song.artist + " - " + (!useName || name.isEmpty() ? song.title : song.title.isEmpty() ? name : (song.title + " - " + name)); } else { return trackTitle; } } static QVariant toVariant(const QList &songs) { QVariantList list; int lastKey=Song::Null_Key; int lastAlbumIndex=0; int albumDuration=0; int totalDuration=0; for (int i=0; i,bool)), this, SLOT(playlistUpdated(QList,bool))); } HttpRequestHandler::HandleStatus PlayQueueApi::handle(HttpRequest *request, HttpResponse *response) { const QByteArray &path=request->path(); DBUG << path; if (HttpRequest::Method_Get==request->method() && path=="/api/v1/playqueue") { emit playListInfo(); awaitResponse(response); return Status_Handling; } else if (HttpRequest::Method_Delete==request->method() && path=="/api/v1/playqueue") { emit clear(); } else if (HttpRequest::Method_Post==request->method() && path=="/api/v1/playqueue") { QString url=request->parameter("url"); bool play="true"==request->parameter("play"); QStringList files; DBUG << "params" << url; if (url.isEmpty()) { QString genre=request->parameter("genre"); QString artistId=request->parameter("artistId"); QString albumId=request->parameter("albumId"); QString sort=request->parameter("sort"); DBUG << "other params" << artistId << albumId << genre << sort; QList songs=MpdLibraryDb::self()->getTracks(artistId, albumId, genre, sort); foreach (const Song &s, songs) { files.append(s.file); } } else { files << url; } bool isStream=!url.isEmpty() && request->parameter("stream")=="true"; bool isPlaylist=!url.isEmpty() && request->parameter("playlist")=="true"; if (isStream) { if (!fetcher) { fetcher=new StreamFetcher(this); connect(fetcher, SIGNAL(result(QStringList,int,bool,quint8)), this, SLOT(streamResult(QStringList,int,bool,quint8))); fetcher->get(files, 0, true, 0); } awaitResponse(response); return Status_Handling; } else if (isPlaylist) { if (!files.isEmpty()) { emit loadPlaylist(url, play); } setResponse(response, !files.isEmpty()); } else { if (!files.isEmpty()) { emit add(files, play, 0); } setResponse(response, !files.isEmpty()); } } else if (HttpRequest::Method_Post==request->method() && path=="/api/v1/playqueue/control") { QString cmd=request->parameter("cmd"); if ("prev"==cmd) { emit prev(); } else if ("play"==cmd) { emit play(); } else if("pause"==cmd) { emit pause(true); } else if ("next"==cmd) { emit next(); } else { return Status_BadRequest; } } else { return Status_BadRequest; } return Status_Handled; } void PlayQueueApi::streamResult(const QStringList &items, int insertRow, bool replace, quint8 priority) { Q_UNUSED(insertRow) Q_UNUSED(replace) Q_UNUSED(priority) DBUG << awaitingResponse(); if (awaitingResponse()) { emit add(items, true, 0); responseReceived(true); } } void PlayQueueApi::playlistUpdated(const QList &songs, bool isComplete) { DBUG << isComplete << awaitingResponse(); if (isComplete && awaitingResponse()) { responseReceived(toVariant(songs)); } }