~ubuntu-branches/ubuntu/karmic/kid3/karmic

« back to all changes in this revision

Viewing changes to kid3/importsourceclient.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Patrick Matthäi
  • Date: 2009-05-20 16:12:30 UTC
  • mfrom: (1.2.3 upstream)
  • mto: This revision was merged to the branch mainline in revision 23.
  • Revision ID: james.westby@ubuntu.com-20090520161230-qetp532r8ydujkz2
Tags: upstream-1.2
Import upstream version 1.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
6
6
 * \author Urs Fleisch
7
7
 * \date 09 Oct 2006
8
8
 *
9
 
 * Copyright (C) 2006-2007  Urs Fleisch
 
9
 * Copyright (C) 2006-2009  Urs Fleisch
10
10
 *
11
11
 * This file is part of Kid3.
12
12
 *
36
36
 * Constructor.
37
37
 */
38
38
ImportSourceClient::ImportSourceClient() :
39
 
 m_statusBar(0), m_requestType(RT_None)
 
39
 m_requestType(RT_None)
40
40
{
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)));
47
 
#else
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)));
53
 
#endif
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&)));
60
43
}
61
44
 
62
45
/**
64
47
 */
65
48
ImportSourceClient::~ImportSourceClient()
66
49
{
67
 
        m_sock->close();
68
 
        m_sock->disconnect();
69
 
        delete m_sock;
70
 
}
71
 
 
72
 
/**
73
 
 * Initialize object.
74
 
 * Has to be called before use.
75
 
 *
76
 
 * @param sb status bar to display progress information.
77
 
 */
78
 
void ImportSourceClient::init(QStatusBar* sb)
79
 
{
80
 
        m_statusBar = sb;
81
 
        m_statusBar->QCM_showMessage(i18n("Ready."));
82
 
}
83
 
 
84
 
/**
85
 
 * Get string with proxy or destination and port.
86
 
 * If a proxy is set, the proxy is returned, else the real destination.
87
 
 *
88
 
 * @param dst real destination
89
 
 *
90
 
 * @return "destinationname:port".
91
 
 */
92
 
QString ImportSourceClient::getProxyOrDest(const QString& dst)
93
 
{
94
 
        QString dest;
95
 
        if (Kid3App::s_miscCfg.m_useProxy) {
96
 
                dest = Kid3App::s_miscCfg.m_proxy;
97
 
        }
98
 
        if (dest.isEmpty()) {
99
 
                dest = dst;
100
 
        }
101
 
        return dest;
102
 
}
103
 
 
104
 
/**
105
 
 * Extract name and port from string.
106
 
 *
107
 
 * @param namePort input string with "name:port"
108
 
 * @param name     output string with "name"
109
 
 * @param port     output integer with port
110
 
 */
111
 
void ImportSourceClient::splitNamePort(const QString& namePort,
112
 
                                                                                                                                 QString& name, int& port)
113
 
{
114
 
        int colPos = namePort.QCM_lastIndexOf(':');
115
 
        if (colPos >= 0) {
116
 
                bool ok;
117
 
                port = namePort.mid(colPos + 1).toInt(&ok);
118
 
                if (!ok) port = 80;
119
 
                name = namePort.left(colPos);
120
 
        } else {
121
 
                name = namePort;
122
 
                port = 80;
123
 
        }
124
50
}
125
51
 
126
52
/**
133
59
void ImportSourceClient::find(const ImportSourceConfig* cfg,
134
60
                                                                                                                        const QString& artist, const QString& album)
135
61
{
136
 
        QString dest;
137
 
        int destPort;
138
 
        constructFindQuery(cfg, artist, album, dest, destPort);
139
 
        m_sock->connectToHost(dest, destPort);
 
62
        sendFindQuery(cfg, artist, album);
140
63
        m_requestType = RT_Find;
141
 
 
142
 
        m_statusBar->QCM_showMessage(i18n("Connecting..."));
143
 
}
144
 
 
145
 
/**
146
 
 * Display status if host is found.
147
 
 */
148
 
void ImportSourceClient::slotHostFound()
149
 
{
150
 
        m_statusBar->QCM_showMessage(i18n("Host found..."));
151
 
}
152
 
 
153
 
/**
154
 
 * Display status if connection is established.
155
 
 */
156
 
void ImportSourceClient::slotConnected()
157
 
{
158
 
        m_sock->QCM_writeBlock(m_request.QCM_latin1(), m_request.length());
159
 
        m_statusBar->QCM_showMessage(i18n("Request sent..."));
160
 
}
161
 
 
162
 
/**
163
 
 * Read received data when the server has closed the connection.
 
64
}
 
65
 
 
66
/**
 
67
 * Handle response when request is finished.
164
68
 * The data is sent to other objects via signals.
 
69
 *
 
70
 * @param rcvStr received data
165
71
 */
166
 
void ImportSourceClient::slotConnectionClosed()
 
72
void ImportSourceClient::requestFinished(const QByteArray& rcvStr)
167
73
{
168
 
        unsigned long len = m_sock->bytesAvailable();
169
 
        QByteArray rcvStr;
170
 
        rcvStr.resize(len + 1);
171
 
        m_sock->QCM_readBlock(rcvStr.data(), len);
172
74
        switch (m_requestType) {
173
75
                case RT_Album:
174
76
                        emit albumFinished(rcvStr);
179
81
                default:
180
82
                        qWarning("Unknown import request type");
181
83
        }
182
 
        m_sock->close();
183
 
        m_statusBar->QCM_showMessage(i18n("Ready."));
184
 
}
185
 
 
186
 
/**
187
 
 * Display information about read progress.
188
 
 */
189
 
void ImportSourceClient::slotReadyRead()
190
 
{
191
 
        m_statusBar->QCM_showMessage(KCM_i18n1("Data received: %1", m_sock->bytesAvailable()));
192
 
}
193
 
 
194
 
/**
195
 
 * Display information about socket error.
196
 
 */
197
 
#if QT_VERSION >= 0x040000
198
 
void ImportSourceClient::slotError(QAbstractSocket::SocketError err)
199
 
{
200
 
        if (err == QAbstractSocket::RemoteHostClosedError)
201
 
                return;
202
 
        QString msg(i18n("Socket error: "));
203
 
        switch (err) {
204
 
                case QAbstractSocket::ConnectionRefusedError:
205
 
                        msg += i18n("Connection refused");
206
 
                        break;
207
 
                case QAbstractSocket::HostNotFoundError:
208
 
                        msg += i18n("Host not found");
209
 
                        break;
210
 
                case QAbstractSocket::SocketAccessError:
211
 
                        msg += i18n("Read failed");
212
 
                        break;
213
 
                default:
214
 
                        msg += m_sock->errorString();
215
 
        }
216
 
        m_statusBar->QCM_showMessage(msg);
217
 
}
218
 
#else
219
 
void ImportSourceClient::slotError(int err)
220
 
{
221
 
        QString msg(i18n("Socket error: "));
222
 
        switch (err) {
223
 
                case QSocket::ErrConnectionRefused:
224
 
                        msg += i18n("Connection refused");
225
 
                        break;
226
 
                case QSocket::ErrHostNotFound:
227
 
                        msg += i18n("Host not found");
228
 
                        break;
229
 
                case QSocket::ErrSocketRead:
230
 
                        msg += i18n("Read failed");
231
 
                        break;
232
 
                default:
233
 
                        msg += QString::number(err);
234
 
        }
235
 
        m_statusBar->QCM_showMessage(msg);
236
 
}
237
 
#endif
 
84
}
238
85
 
239
86
/**
240
87
 * Request track list from server.
245
92
 */
246
93
void ImportSourceClient::getTrackList(const ImportSourceConfig* cfg, QString cat, QString id)
247
94
{
248
 
        QString dest;
249
 
        int destPort;
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..."));
 
97
}
 
98
 
 
99
/**
 
100
 * Encode a query in an URL.
 
101
 * The query is percent-encoded with spaces collapsed and replaced by '+'.
 
102
 *
 
103
 * @param query query to encode
 
104
 *
 
105
 * @return encoded query.
 
106
 */
 
107
QString ImportSourceClient::encodeUrlQuery(const QString& query)
 
108
{
 
109
        QString result(query);
 
110
        result.replace(QRegExp(" +"), " "); // collapse spaces
 
111
        QCM_QUrl_encode(result);
 
112
        result.replace("%20", "+"); // replace spaces by '+'
 
113
        return result;
254
114
}