summaryrefslogtreecommitdiffstats
path: root/src/core/services/qdownloadhelperservice.cpp
blob: 1b0c3eb4fcd159c983ff11d9dc78af0dd65bc023 (plain)
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
/****************************************************************************
**
** Copyright (C) 2017 Klaralvdalens Datakonsult AB (KDAB).
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Qt3D module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:COMM$
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** $QT_END_LICENSE$
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
**
****************************************************************************/

#include "qdownloadhelperservice_p.h"
#include "qdownloadnetworkworker_p.h"
#include <QtCore/QThread>
#include <Qt3DCore/QAspectEngine>
#include <Qt3DCore/private/qabstractserviceprovider_p.h>
#include <Qt3DCore/private/qaspectengine_p.h>
#include <Qt3DCore/private/qaspectmanager_p.h>
#include <Qt3DCore/private/qservicelocator_p.h>

#include <QFile>

QT_BEGIN_NAMESPACE

namespace Qt3DCore {

QDownloadRequest::QDownloadRequest(const QUrl &url)
    : m_url(url)
    , m_succeeded(false)
    , m_cancelled(false)
{

}

QDownloadRequest::~QDownloadRequest()
{

}

void QDownloadRequest::onDownloaded()
{
    // this is called in dl thread. It's an opportunity to do long running tasks
    // like loading the data into a QImage
}


class Q_AUTOTEST_EXPORT QDownloadHelperServicePrivate : public QAbstractServiceProviderPrivate
{
public:
    explicit QDownloadHelperServicePrivate(const QString &description);
    ~QDownloadHelperServicePrivate();

    void init();
    void shutdown();
    void _q_onRequestCompleted(const QDownloadRequestPtr &request);

    Q_DECLARE_PUBLIC(QDownloadHelperService)

    QThread *m_downloadThread;
    QDownloadNetworkWorker *m_downloadWorker;
};


QDownloadHelperServicePrivate::QDownloadHelperServicePrivate(const QString &description)
    : QAbstractServiceProviderPrivate(QServiceLocator::DownloadHelperService, description)
    , m_downloadThread(nullptr)
    , m_downloadWorker(nullptr)
{
}

QDownloadHelperServicePrivate::~QDownloadHelperServicePrivate()
{
}

void QDownloadHelperServicePrivate::init()
{
    Q_Q(QDownloadHelperService);
    m_downloadThread = new QThread(q);
    m_downloadWorker = new QDownloadNetworkWorker;
    m_downloadWorker->moveToThread(m_downloadThread);
    // QueuedConnection
    QObject::connect(m_downloadWorker, SIGNAL(requestDownloaded(const Qt3DCore::QDownloadRequestPtr &)),
                     q, SLOT(_q_onRequestCompleted(const Qt3DCore::QDownloadRequestPtr &)));
    m_downloadThread->start();
}

void QDownloadHelperServicePrivate::shutdown()
{
    emit m_downloadWorker->cancelAllRequests();
    m_downloadThread->exit();
    m_downloadThread->wait();
    m_downloadWorker->deleteLater();
}

// Executed in AspectThread (queued signal connected to download thread)
void QDownloadHelperServicePrivate::_q_onRequestCompleted(const Qt3DCore::QDownloadRequestPtr &request)
{
    request->onCompleted();
}


QDownloadHelperService::QDownloadHelperService(const QString &description)
    : QAbstractServiceProvider(*new QDownloadHelperServicePrivate(description))
{
    Q_D(QDownloadHelperService);
    d->init();
    qRegisterMetaType<Qt3DCore::QDownloadRequestPtr>();
}

QDownloadHelperService::~QDownloadHelperService()
{
    Q_D(QDownloadHelperService);
    d->shutdown();
}

void QDownloadHelperService::submitRequest(const Qt3DCore::QDownloadRequestPtr &request)
{
    Q_D(QDownloadHelperService);

    if (isLocal(request->url())) {
        QFile file(urlToLocalFileOrQrc(request->url()));
        if (file.open(QIODevice::ReadOnly)) {
            request->m_data = file.readAll();
            file.close();
            request->m_succeeded = true;
        } else {
            request->m_succeeded = false;
        }
        request->onCompleted();
    } else {
        emit d->m_downloadWorker->submitRequest(request);
    }
}

void QDownloadHelperService::cancelRequest(const Qt3DCore::QDownloadRequestPtr &request)
{
    Q_D(QDownloadHelperService);
    request->m_cancelled = true;
    emit d->m_downloadWorker->cancelRequest(request);
}

void QDownloadHelperService::cancelAllRequests()
{
    Q_D(QDownloadHelperService);
    emit d->m_downloadWorker->cancelAllRequests();
}

QString QDownloadHelperService::urlToLocalFileOrQrc(const QUrl &url)
{
    const QString scheme(url.scheme().toLower());
    if (scheme == QLatin1String("qrc")) {
        if (url.authority().isEmpty())
            return QLatin1Char(':') + url.path();
        return QString();
    }

#if defined(Q_OS_ANDROID)
    if (scheme == QLatin1String("assets")) {
        if (url.authority().isEmpty())
            return url.toString();
        return QString();
    }
#endif

    return url.toLocalFile();
}

QDownloadHelperService *QDownloadHelperService::getService(QAspectEngine *engine)
{
    auto enginePrivate = Qt3DCore::QAspectEnginePrivate::get(engine);
    return enginePrivate->m_aspectManager->serviceLocator()->downloadHelperService();
}

bool QDownloadHelperService::isLocal(const QUrl &url)
{
    const QString scheme(url.scheme().toLower());
    if (scheme == QLatin1String("file") || scheme == QLatin1String("qrc"))
        return true;
#if defined(Q_OS_ANDROID)
    if (scheme == QLatin1String("assets"))
        return true;
#endif
    return false;
}

} // namespace Qt3DCore

QT_END_NAMESPACE

#include "moc_qdownloadhelperservice_p.cpp"