File: network.cpp

package info (click to toggle)
cb2bib 1.9.2-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 3,368 kB
  • sloc: cpp: 24,179; sh: 481; makefile: 16
file content (377 lines) | stat: -rw-r--r-- 12,877 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/***************************************************************************
 *   Copyright (C) 2004-2015 by Pere Constans
 *   constans@molspaces.com
 *   cb2Bib version 1.9.2. Licensed under the GNU GPL version 3.
 *   See the LICENSE file that comes with this distribution.
 ***************************************************************************/
#include "network.h"

#include "cb2bib_utilities.h"
#include "settings.h"

#include <QNetworkCookie>
#include <QNetworkCookieJar>
#include <QNetworkProxy>
#include <QNetworkReply>
#include <QTimer>


network::network(QObject* parento) : QObject(parento), _max_redirections(15)
{
    _is_fetching = false;
    _fetcher = new QNetworkAccessManager(this);
    connect(_fetcher, SIGNAL(proxyAuthenticationRequired(const QNetworkProxy&, QAuthenticator*)), this,
            SIGNAL(proxyAuthenticationRequired(const QNetworkProxy&, QAuthenticator*)));
    loadSettings();
    connect(settings::instance(), SIGNAL(newSettings()), this, SLOT(loadSettings()));
    // Set predefined cookies
    QNetworkCookieJar* ncj = _fetcher->cookieJar();
    QNetworkCookie nc("GSP", "ID=d093ce1ea042ad2b:IN=54afcd58e3b38df9:HIN=ff7e3a3ab3fbae0a+7e6cc990821af63:CF=4");
    ncj->setCookiesFromUrl(QList<QNetworkCookie>() << nc, QUrl("https://scholar.google.com"));
}


/****************************************************************************

  PUBLIC PART

*****************************************************************************/

void network::getFile(const QString& source, const QString& destination, const Action action,
                      QObject* receiver, const char* callback, const bool overwrite)
{
    if (_is_fetching)
    {
        c2bUtils::warn(tr("network::getFile: Requesting network while still fetching previous request. Returned"));
        return;
    }
    setup(source, destination);
    disconnect(this, SIGNAL(requestFinished(bool)), 0, 0);
    if (receiver)
        connect(this, SIGNAL(requestFinished(bool)), receiver, callback);
    if (overwrite)
        if (QFileInfo(destination).exists())
            QFile::remove(destination);
    getFilePrivate(action);
}

void network::headFile(const QString& source, QObject* receiver, const char* callback)
{
    if (_is_fetching)
    {
        c2bUtils::warn(tr("network::headFile: Requesting network while still fetching previous request. Returned"));
        return;
    }
    setup(source);
    disconnect(this, SIGNAL(requestFinished(bool)), 0, 0);
    if (receiver)
        connect(this, SIGNAL(requestFinished(bool)), receiver, callback);
    headFilePrivate();
}

void network::cancelDownload()
{
    if (_is_fetching)
        _current_reply->abort();
}


/****************************************************************************

  PRIVATE PART

*****************************************************************************/

void network::getFilePrivate(const Action action)
{
    if (!checkDestination())
    {
        _emit_request_finished(false);
        return;
    }
    if (_source_filename.startsWith("<<post>>")) // cb2Bib keyword to use post http method
    {
        _source_filename.remove(QRegExp("^<<post>>"));
        _fetch_c2b(action, QNetworkAccessManager::PostOperation);
        return;
    }
    if (FmClient)
        if ((action == Copy && !FmClientCopyBin.isEmpty()) || (action == Move && !FmClientMoveBin.isEmpty()))
        {
            _fetch_client(action);
            return;
        }
    _fetch_c2b(action);
}

void network::headFilePrivate()
{
    const QUrl u(_source_filename, QUrl::TolerantMode);
    if (u.scheme() == "file" || QFileInfo(_source_filename).exists())
    {
        // Local File
        const QString fn(u.scheme() == "file" ? u.toLocalFile() : _source_filename);
        const bool succeeded(QFileInfo(fn).exists());
        if (!succeeded)
            _request_error_string = tr("File does not exist.");
        _emit_request_finished(succeeded);
    }
    else
    {
        // Network File
        _head(u);
    }
}

void network::_emit_request_finished(bool succeeded)
{
    _request_succeeded = succeeded;
    // Give some time to cleanup events and to return all network functions
    // before passing the control to the callback routine
    QTimer::singleShot(50, this, SLOT(_emit_request_finished()));
}

void network::_emit_request_finished()
{
    _is_fetching = false;
    // Assumed events are clean, all functions returned, then make the callback
    emit requestFinished(_request_succeeded);
}

bool network::checkDestination()
{
    // Checks whether or not writing to destination is safe
    // Returns false if file exists
    if (QFileInfo(_destination_filename).exists())
    {
        _request_error_string = tr("Destination file '%1' already exists.").arg(_destination_filename);
        return false;
    }
    else
        return true;
}

void network::loadSettings()
{
    settings* s(settings::instance());
    FmClient = s->value("cb2Bib/FmClient").toBool();
    FmClientCopyBin = s->fileName("cb2Bib/FmClientCopyBin");
    FmClientMoveBin = s->fileName("cb2Bib/FmClientMoveBin");
    FmClientCopyArg = s->value("cb2Bib/FmClientCopyArg").toString();
    FmClientMoveArg = s->value("cb2Bib/FmClientMoveArg").toString();
    QNetworkProxy proxy;
    if (s->value("cb2Bib/UseProxy").toBool())
    {
        const QString hn(s->value("cb2Bib/ProxyHostName", QString()).toString());
        if (!hn.isEmpty())
        {
            if (s->value("cb2Bib/ProxyType").toInt() == 0)
                proxy = QNetworkProxy::HttpProxy;
            else
                proxy = QNetworkProxy::Socks5Proxy;
            proxy.setHostName(hn);
            proxy.setPort(quint16(s->value("cb2Bib/ProxyPort").toInt()));
        }
    }
    _fetcher->setProxy(proxy);
}


/****************************************************************************

  PRIVATE PART: FILEMANAGER CLIENT

*****************************************************************************/

void network::_fetch_client(const Action action)
{
    // Getting NetworkFile through kfmclient
    Action act(action);
    // Only move local files
    QUrl u(_source_filename);
    if (!(u.scheme() == "file" || QFileInfo(_source_filename).exists()))
        if (action == Move)
            act = Copy; // Copy network files

    QStringList arglist;
    QString fmclient_bin;
    if (act == Copy)
    {
        fmclient_bin = FmClientCopyBin;
        arglist = FmClientCopyArg.split(' ', QString::SkipEmptyParts);
    }
    else if (act == Move)
    {
        fmclient_bin = FmClientMoveBin;
        arglist = FmClientMoveArg.split(' ', QString::SkipEmptyParts);
    }
    arglist.append(_source_filename);
    arglist.append(_destination_filename);
    _fetcher_client = new QProcess(this);
    connect(_fetcher_client, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(_client_finished(int, QProcess::ExitStatus)));
    _fetcher_client->start(fmclient_bin, arglist);
    if (!_fetcher_client->waitForStarted())
    {
        delete _fetcher_client;
        _request_error_string = tr("FM Client '%1' could not be launched.").arg(fmclient_bin);
        _emit_request_finished(false);
    }
}

void network::_client_finished(int exitCode, QProcess::ExitStatus exitStatus)
{
    bool succeeded(false);
    if (exitStatus == QProcess::CrashExit)
        _request_error_string = tr("FM Client crashed.");
    else
    {
        if (QFileInfo(_destination_filename).exists())
            succeeded = true;
        else
            _request_error_string = tr("File '%1' has not been written. Exit code '%2'.").arg(_source_filename).arg(exitCode);
    }
    delete _fetcher_client;
    _emit_request_finished(succeeded);
}


/****************************************************************************

  PRIVATE PART: C2B FETCHER

*****************************************************************************/

void network::_head(const QUrl& url)
{
    QNetworkRequest request;
    request.setUrl(url);
    request.setRawHeader("User-Agent", QString("cb2Bib/" + C2B_VERSION + " (Bibliographic Browser Tool)").toLatin1());
    _current_reply = _fetcher->head(request);
    connect(_current_reply, SIGNAL(finished()), SLOT(_head_finished()));
}

void network::_head_finished()
{
    if (_redirection_count++ < _max_redirections)
    {
        const QUrl redirection(_current_reply->attribute(QNetworkRequest::RedirectionTargetAttribute).toUrl());
        if (redirection.isValid())
        {
            const QUrl ru(_current_reply->url().resolved(redirection));
            _source_filename = ru.toString();
            _current_reply->deleteLater();
            _head(ru);
            return;
        }
    }
    const bool succeeded(_current_reply->error() == QNetworkReply::NoError);
    if (succeeded)
        _file_mimetype_string = _current_reply->header(QNetworkRequest::ContentTypeHeader).toString();
    else
        _request_error_string = _current_reply->errorString() + '.';
    _current_reply->deleteLater();
    _emit_request_finished(succeeded);
}

void network::_fetch_c2b(const Action action, const QNetworkAccessManager::Operation operation)
{
    _fetch_operation = operation;
    _fetch_url_query.clear();
    QString url_str;
    if (_fetch_operation == QNetworkAccessManager::PostOperation)
    {
        const int qmark(_source_filename.indexOf('?'));
        url_str = _source_filename.mid(0, qmark);
        if (qmark > -1)
        {
            url_str += '/';
            _fetch_url_query = _source_filename.mid(qmark + 1).toUtf8();
        }
    }
    else
        url_str = _source_filename;

    QUrl u(url_str, QUrl::TolerantMode);
    if (u.scheme() == "file" || QFileInfo(_source_filename).exists())
    {
        // Local File
        QFile source(u.scheme() == "file" ? u.toLocalFile() : _source_filename);
        bool succeeded(false);
        if (action == Copy)
            succeeded = source.copy(_destination_filename);
        else if (action == Move)
            succeeded = source.rename(_destination_filename);
        if (!succeeded)
            _request_error_string = source.errorString();
        _emit_request_finished(succeeded);
    }
    else
    {
        // Network File
        _fetch(u);
    }
}

void network::_fetch(const QUrl& url)
{
    _destination_file.setFileName(_destination_filename);
    if (!_destination_file.open(QIODevice::WriteOnly))
    {
        _request_error_string = tr("File '%1' cannot be written. %2").arg(_destination_filename).arg(_destination_file.errorString());
        _emit_request_finished(false);
        return;
    }
    QNetworkRequest request;
    request.setUrl(url);
    request.setRawHeader("User-Agent", QString("cb2Bib/" + C2B_VERSION + " (Bibliographic Browser Tool)").toLatin1());
    if (_fetch_operation == QNetworkAccessManager::PostOperation)
    {
        request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded");
        _current_reply = _fetcher->post(request, _fetch_url_query);
    }
    else
        _current_reply = _fetcher->get(request);
    connect(_current_reply, SIGNAL(readyRead()), this, SLOT(_fetch_ready_read()));
    connect(_current_reply, SIGNAL(finished()), SLOT(_fetch_finished()));
    connect(_current_reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(logError()));
    connect(_current_reply, SIGNAL(downloadProgress(qint64, qint64)), this, SIGNAL(downloadProgress(qint64, qint64)));
}

void network::_fetch_finished()
{
    _destination_file.close();
    if (_current_reply->error() == QNetworkReply::OperationCanceledError)
        _destination_file.remove(); // Delete file
    else if (_redirection_count++ < _max_redirections)
    {
        const QUrl redirection(_current_reply->attribute(QNetworkRequest::RedirectionTargetAttribute).toUrl());
        if (redirection.isValid())
        {
            const QUrl ru(_current_reply->url().resolved(redirection));
            _source_filename = ru.toString();
            const int status(_current_reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt());
            if ((status >= 301 && status <= 303) || status == 307)
                _fetch_operation = QNetworkAccessManager::GetOperation;
            _current_reply->deleteLater();
            _fetch(ru);
            return;
        }
    }
    const bool succeeded(_current_reply->error() == QNetworkReply::NoError);
    if (succeeded)
        _file_mimetype_string = _current_reply->header(QNetworkRequest::ContentTypeHeader).toString();
    else
        _request_error_string = _current_reply->errorString() + '.';
    _current_reply->deleteLater();
    _emit_request_finished(succeeded);
}

void network::_fetch_ready_read()
{
    _destination_file.write(_current_reply->readAll());
}

void network::logError()
{
    c2bUtils::warn(tr("network::QNetworkReply log: %1").arg(_current_reply->errorString()));
}