1 /**
2  * \file importclient.cpp
3  * Client to connect to server with import data.
4  *
5  * \b Project: Kid3
6  * \author Urs Fleisch
7  * \date 09 Oct 2006
8  *
9  * Copyright (C) 2006-2018  Urs Fleisch
10  *
11  * This file is part of Kid3.
12  *
13  * Kid3 is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 2 of the License, or
16  * (at your option) any later version.
17  *
18  * Kid3 is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25  */
26 
27 #include "importclient.h"
28 #include <QRegularExpression>
29 #include <QUrl>
30 
31 #include "serverimporterconfig.h"
32 
33 /**
34  * Constructor.
35  *
36  * @param netMgr  network access manager
37  */
ImportClient(QNetworkAccessManager * netMgr)38 ImportClient::ImportClient(QNetworkAccessManager* netMgr)
39   : HttpClient(netMgr), m_requestType(RT_None)
40 {
41   setObjectName(QLatin1String("ImportClient"));
42   connect(this, &HttpClient::bytesReceived,
43           this, &ImportClient::requestFinished);
44 }
45 
46 /**
47  * Find keyword on server.
48  *
49  * @param cfg    import source configuration
50  * @param artist artist to search
51  * @param album  album to search
52  */
find(const ServerImporterConfig * cfg,const QString & artist,const QString & album)53 void ImportClient::find(const ServerImporterConfig* cfg,
54                               const QString& artist, const QString& album)
55 {
56   sendFindQuery(cfg, artist, album);
57   m_requestType = RT_Find;
58 }
59 
60 /**
61  * Handle response when request is finished.
62  * The data is sent to other objects via signals.
63  *
64  * @param rcvStr received data
65  */
requestFinished(const QByteArray & rcvStr)66 void ImportClient::requestFinished(const QByteArray& rcvStr)
67 {
68   switch (m_requestType) {
69     case RT_Album:
70       emit albumFinished(rcvStr);
71       break;
72     case RT_Find:
73       emit findFinished(rcvStr);
74       break;
75     default:
76       qWarning("Unknown import request type");
77   }
78 }
79 
80 /**
81  * Request track list from server.
82  *
83  * @param cfg import source configuration
84  * @param cat category
85  * @param id  ID
86  */
getTrackList(const ServerImporterConfig * cfg,const QString & cat,const QString & id)87 void ImportClient::getTrackList(const ServerImporterConfig* cfg,
88                                 const QString& cat, const QString& id)
89 {
90   sendTrackListQuery(cfg, cat, id);
91   m_requestType = RT_Album;
92 }
93 
94 /**
95  * Encode a query in an URL.
96  * The query is percent-encoded with spaces collapsed and replaced by '+'.
97  *
98  * @param query query to encode
99  *
100  * @return encoded query.
101  */
encodeUrlQuery(const QString & query)102 QString ImportClient::encodeUrlQuery(const QString& query)
103 {
104   QString result(query);
105   result.replace(QRegularExpression(QLatin1String(" +")), QLatin1String(" ")); // collapse spaces
106   result = QString::fromLatin1(QUrl::toPercentEncoding(result));
107   result.replace(QLatin1String("%20"), QLatin1String("+")); // replace spaces by '+'
108   return result;
109 }
110