File indexing completed on 2024-05-19 04:55:59

0001 /**
0002  * \file importclient.cpp
0003  * Client to connect to server with import data.
0004  *
0005  * \b Project: Kid3
0006  * \author Urs Fleisch
0007  * \date 09 Oct 2006
0008  *
0009  * Copyright (C) 2006-2018  Urs Fleisch
0010  *
0011  * This file is part of Kid3.
0012  *
0013  * Kid3 is free software; you can redistribute it and/or modify
0014  * it under the terms of the GNU General Public License as published by
0015  * the Free Software Foundation; either version 2 of the License, or
0016  * (at your option) any later version.
0017  *
0018  * Kid3 is distributed in the hope that it will be useful,
0019  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0020  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0021  * GNU General Public License for more details.
0022  *
0023  * You should have received a copy of the GNU General Public License
0024  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
0025  */
0026 
0027 #include "importclient.h"
0028 #include <QRegularExpression>
0029 #include <QUrl>
0030 
0031 #include "serverimporterconfig.h"
0032 
0033 /**
0034  * Constructor.
0035  *
0036  * @param netMgr  network access manager
0037  */
0038 ImportClient::ImportClient(QNetworkAccessManager* netMgr)
0039   : HttpClient(netMgr), m_requestType(RT_None)
0040 {
0041   setObjectName(QLatin1String("ImportClient"));
0042   connect(this, &HttpClient::bytesReceived,
0043           this, &ImportClient::requestFinished);
0044 }
0045 
0046 /**
0047  * Find keyword on server.
0048  *
0049  * @param cfg    import source configuration
0050  * @param artist artist to search
0051  * @param album  album to search
0052  */
0053 void ImportClient::find(const ServerImporterConfig* cfg,
0054                               const QString& artist, const QString& album)
0055 {
0056   sendFindQuery(cfg, artist, album);
0057   m_requestType = RT_Find;
0058 }
0059 
0060 /**
0061  * Handle response when request is finished.
0062  * The data is sent to other objects via signals.
0063  *
0064  * @param rcvStr received data
0065  */
0066 void ImportClient::requestFinished(const QByteArray& rcvStr)
0067 {
0068   switch (m_requestType) {
0069     case RT_Album:
0070       emit albumFinished(rcvStr);
0071       break;
0072     case RT_Find:
0073       emit findFinished(rcvStr);
0074       break;
0075     default:
0076       qWarning("Unknown import request type");
0077   }
0078 }
0079 
0080 /**
0081  * Request track list from server.
0082  *
0083  * @param cfg import source configuration
0084  * @param cat category
0085  * @param id  ID
0086  */
0087 void ImportClient::getTrackList(const ServerImporterConfig* cfg,
0088                                 const QString& cat, const QString& id)
0089 {
0090   sendTrackListQuery(cfg, cat, id);
0091   m_requestType = RT_Album;
0092 }
0093 
0094 /**
0095  * Encode a query in an URL.
0096  * The query is percent-encoded with spaces collapsed and replaced by '+'.
0097  *
0098  * @param query query to encode
0099  *
0100  * @return encoded query.
0101  */
0102 QString ImportClient::encodeUrlQuery(const QString& query)
0103 {
0104   QString result(query);
0105   result.replace(QRegularExpression(QLatin1String(" +")), QLatin1String(" ")); // collapse spaces
0106   result = QString::fromLatin1(QUrl::toPercentEncoding(result));
0107   result.replace(QLatin1String("%20"), QLatin1String("+")); // replace spaces by '+'
0108   return result;
0109 }