File indexing completed on 2024-05-19 04:48:27

0001 /*
0002    Babe - tiny music player
0003    Copyright (C) 2017  Camilo Higuita
0004    This program is free software; you can redistribute it and/or modify
0005    it under the terms of the GNU General Public License as published by
0006    the Free Software Foundation; either version 3 of the License, or
0007    (at your option) any later version.
0008    This program is distributed in the hope that it will be useful,
0009    but WITHOUT ANY WARRANTY; without even the implied warranty of
0010    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0011    GNU General Public License for more details.
0012    You should have received a copy of the GNU General Public License
0013    along with this program; if not, write to the Free Software Foundation,
0014    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
0015 
0016    */
0017 
0018 #include "pulpo.h"
0019 #include "services/lastfmService.h"
0020 #include "services/spotifyService.h"
0021 //#include "services/lyricwikiaService.h"
0022 //#include "services/geniusService.h"
0023 //#include "services/musicbrainzService.h"
0024 //#include "services/deezerService.h"
0025 
0026 //#include "qgumbodocument.h"p
0027 //#include "qgumbonode.h"
0028 
0029 Pulpo::Pulpo(QObject *parent)
0030     : QObject(parent)
0031 {
0032 }
0033 
0034 Pulpo::~Pulpo()
0035 {
0036     qDebug() << "DELETING PULPO INSTANCE";
0037 }
0038 
0039 void Pulpo::request(const PULPO::REQUEST &request)
0040 {
0041     this->req = request;
0042 
0043     if (this->req.track.isEmpty()) {
0044         Q_EMIT this->error();
0045         return;
0046     }
0047 
0048     if (this->req.services.isEmpty()) {
0049         qWarning() << "Please register at least one Pulpo Service";
0050         Q_EMIT this->error();
0051         return;
0052     }
0053 
0054     this->start();
0055 }
0056 
0057 void Pulpo::start()
0058 {
0059     this->send(this->req.services.first());
0060 }
0061 
0062 void Pulpo::passSignal(const REQUEST &request, const RESPONSES &responses)
0063 {
0064     if (request.callback)
0065         request.callback(request, responses);
0066     else
0067         Q_EMIT this->infoReady(request, responses);
0068     Q_EMIT this->finished();
0069 }
0070 
0071 void Pulpo::send(const SERVICES &service)
0072 {
0073     switch (service) {
0074     case SERVICES::LastFm: {
0075         auto lastfm = new class lastfm();
0076         connect(lastfm, &lastfm::responseReady, [this, lastfm](PULPO::REQUEST request, PULPO::RESPONSES responses) {
0077             this->passSignal(request, responses);
0078             lastfm->deleteLater();
0079         });
0080 
0081         connect(lastfm, &lastfm::error, [this, service, lastfm](PULPO::REQUEST request) {
0082             if (!request.services.isEmpty()) {
0083                 request.services.removeOne(service);
0084                 this->request(request);
0085             } else {
0086                 Q_EMIT this->error();
0087             }
0088 
0089             lastfm->deleteLater();
0090         });
0091 
0092         lastfm->set(this->req);
0093         break;
0094     }
0095 
0096     case SERVICES::Spotify: {
0097         auto spotify = new class spotify();
0098         connect(spotify, &lastfm::responseReady, [this, spotify](PULPO::REQUEST request, PULPO::RESPONSES responses) {
0099             this->passSignal(request, responses);
0100             spotify->deleteLater();
0101         });
0102 
0103         connect(spotify, &lastfm::error, [this, service, spotify](PULPO::REQUEST request) {
0104             if (!request.services.isEmpty()) {
0105                 request.services.removeOne(service);
0106                 this->request(request);
0107             } else {
0108                 Q_EMIT this->error();
0109             }
0110 
0111             spotify->deleteLater();
0112         });
0113 
0114         spotify->set(this->req);
0115         break;
0116     }
0117 
0118     default: {
0119         if (!this->req.services.isEmpty()) {
0120             this->req.services.removeOne(service);
0121             this->request(req);
0122         } else {
0123             Q_EMIT this->error();
0124         }
0125     }
0126     }
0127 }