38
38
ImportSourceClient::ImportSourceClient() :
39
m_statusBar(0), m_requestType(RT_None)
39
m_requestType(RT_None)
41
#if QT_VERSION >= 0x040000
42
m_sock = new QTcpSocket();
43
connect(m_sock, SIGNAL(disconnected()),
44
this, SLOT(slotConnectionClosed()));
45
connect(m_sock, SIGNAL(error(QAbstractSocket::SocketError)),
46
this, SLOT(slotError(QAbstractSocket::SocketError)));
48
m_sock = new QSocket();
49
connect(m_sock, SIGNAL(connectionClosed()),
50
this, SLOT(slotConnectionClosed()));
51
connect(m_sock, SIGNAL(error(int)),
52
this, SLOT(slotError(int)));
54
connect(m_sock, SIGNAL(hostFound()),
55
this, SLOT(slotHostFound()));
56
connect(m_sock, SIGNAL(connected()),
57
this, SLOT(slotConnected()));
58
connect(m_sock, SIGNAL(readyRead()),
59
this, SLOT(slotReadyRead()));
41
connect(this, SIGNAL(bytesReceived(const QByteArray&)),
42
this, SLOT(requestFinished(const QByteArray&)));
65
48
ImportSourceClient::~ImportSourceClient()
74
* Has to be called before use.
76
* @param sb status bar to display progress information.
78
void ImportSourceClient::init(QStatusBar* sb)
81
m_statusBar->QCM_showMessage(i18n("Ready."));
85
* Get string with proxy or destination and port.
86
* If a proxy is set, the proxy is returned, else the real destination.
88
* @param dst real destination
90
* @return "destinationname:port".
92
QString ImportSourceClient::getProxyOrDest(const QString& dst)
95
if (Kid3App::s_miscCfg.m_useProxy) {
96
dest = Kid3App::s_miscCfg.m_proxy;
105
* Extract name and port from string.
107
* @param namePort input string with "name:port"
108
* @param name output string with "name"
109
* @param port output integer with port
111
void ImportSourceClient::splitNamePort(const QString& namePort,
112
QString& name, int& port)
114
int colPos = namePort.QCM_lastIndexOf(':');
117
port = namePort.mid(colPos + 1).toInt(&ok);
119
name = namePort.left(colPos);
133
59
void ImportSourceClient::find(const ImportSourceConfig* cfg,
134
60
const QString& artist, const QString& album)
138
constructFindQuery(cfg, artist, album, dest, destPort);
139
m_sock->connectToHost(dest, destPort);
62
sendFindQuery(cfg, artist, album);
140
63
m_requestType = RT_Find;
142
m_statusBar->QCM_showMessage(i18n("Connecting..."));
146
* Display status if host is found.
148
void ImportSourceClient::slotHostFound()
150
m_statusBar->QCM_showMessage(i18n("Host found..."));
154
* Display status if connection is established.
156
void ImportSourceClient::slotConnected()
158
m_sock->QCM_writeBlock(m_request.QCM_latin1(), m_request.length());
159
m_statusBar->QCM_showMessage(i18n("Request sent..."));
163
* Read received data when the server has closed the connection.
67
* Handle response when request is finished.
164
68
* The data is sent to other objects via signals.
70
* @param rcvStr received data
166
void ImportSourceClient::slotConnectionClosed()
72
void ImportSourceClient::requestFinished(const QByteArray& rcvStr)
168
unsigned long len = m_sock->bytesAvailable();
170
rcvStr.resize(len + 1);
171
m_sock->QCM_readBlock(rcvStr.data(), len);
172
74
switch (m_requestType) {
174
76
emit albumFinished(rcvStr);
180
82
qWarning("Unknown import request type");
183
m_statusBar->QCM_showMessage(i18n("Ready."));
187
* Display information about read progress.
189
void ImportSourceClient::slotReadyRead()
191
m_statusBar->QCM_showMessage(KCM_i18n1("Data received: %1", m_sock->bytesAvailable()));
195
* Display information about socket error.
197
#if QT_VERSION >= 0x040000
198
void ImportSourceClient::slotError(QAbstractSocket::SocketError err)
200
if (err == QAbstractSocket::RemoteHostClosedError)
202
QString msg(i18n("Socket error: "));
204
case QAbstractSocket::ConnectionRefusedError:
205
msg += i18n("Connection refused");
207
case QAbstractSocket::HostNotFoundError:
208
msg += i18n("Host not found");
210
case QAbstractSocket::SocketAccessError:
211
msg += i18n("Read failed");
214
msg += m_sock->errorString();
216
m_statusBar->QCM_showMessage(msg);
219
void ImportSourceClient::slotError(int err)
221
QString msg(i18n("Socket error: "));
223
case QSocket::ErrConnectionRefused:
224
msg += i18n("Connection refused");
226
case QSocket::ErrHostNotFound:
227
msg += i18n("Host not found");
229
case QSocket::ErrSocketRead:
230
msg += i18n("Read failed");
233
msg += QString::number(err);
235
m_statusBar->QCM_showMessage(msg);
240
87
* Request track list from server.
246
93
void ImportSourceClient::getTrackList(const ImportSourceConfig* cfg, QString cat, QString id)
250
constructTrackListQuery(cfg, cat, id, dest, destPort);
251
m_sock->connectToHost(dest, destPort);
95
sendTrackListQuery(cfg, cat, id);
252
96
m_requestType = RT_Album;
253
m_statusBar->QCM_showMessage(i18n("Connecting..."));
100
* Encode a query in an URL.
101
* The query is percent-encoded with spaces collapsed and replaced by '+'.
103
* @param query query to encode
105
* @return encoded query.
107
QString ImportSourceClient::encodeUrlQuery(const QString& query)
109
QString result(query);
110
result.replace(QRegExp(" +"), " "); // collapse spaces
111
QCM_QUrl_encode(result);
112
result.replace("%20", "+"); // replace spaces by '+'