aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/qmldesigner/utils/multifiledownloader.cpp
blob: f0a3201c049af43dc3486f2eaaaca1b1ea0ec81c (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
// Copyright (C) 2023 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0
#include "multifiledownloader.h"
#include "filedownloader.h"

namespace QmlDesigner {

MultiFileDownloader::MultiFileDownloader(QObject *parent)
    : QObject(parent)
{}

MultiFileDownloader::~MultiFileDownloader()
{}

void MultiFileDownloader::setDownloader(FileDownloader *downloader)
{
    m_downloader = downloader;

    QObject::connect(this, &MultiFileDownloader::downloadStarting, [this] {
        m_nextFile = 0;
        if (m_files.length() > 0)
            m_downloader->start();
    });

    QObject::connect(m_downloader, &FileDownloader::progressChanged, this, [this] {
        double curProgress = m_downloader->progress() / 100.0;
        double totalProgress = (m_nextFile + curProgress) / m_files.size();
        m_progress = totalProgress * 100;
        emit progressChanged();
    });

    QObject::connect(m_downloader, &FileDownloader::downloadFailed, this, [this] {
        m_failed = true;
        emit downloadFailed();
    });

    QObject::connect(m_downloader, &FileDownloader::downloadCanceled, this, [this] {
        m_canceled = true;
        emit downloadCanceled();
    });

    QObject::connect(m_downloader, &FileDownloader::finishedChanged, this, [this] {
        switchToNextFile();
    });
}

FileDownloader *MultiFileDownloader::downloader()
{
    return m_downloader;
}

void MultiFileDownloader::start()
{
    m_canceled = false;
    emit downloadStarting();
}

void MultiFileDownloader::cancel()
{
    m_canceled = true;
    m_downloader->cancel();
}

void MultiFileDownloader::setBaseUrl(const QUrl &baseUrl)
{
    if (m_baseUrl != baseUrl) {
        m_baseUrl = baseUrl;
        emit baseUrlChanged();
    }
}

QUrl MultiFileDownloader::baseUrl() const
{
    return m_baseUrl;
}

bool MultiFileDownloader::finished() const
{
    return m_finished;
}

int MultiFileDownloader::progress() const
{
    return m_progress;
}

void MultiFileDownloader::setTargetDirPath(const QString &path)
{
    m_targetDirPath = path;
}

QString MultiFileDownloader::targetDirPath() const
{
    return m_targetDirPath;
}

QString MultiFileDownloader::nextUrl() const
{
    if (m_nextFile >= m_files.length())
        return {};

    return m_baseUrl.toString() + "/" + m_files[m_nextFile];
}

QString MultiFileDownloader::nextTargetPath() const
{
    if (m_nextFile >= m_files.length())
        return {};

    return m_targetDirPath + "/" + m_files[m_nextFile];
}

void MultiFileDownloader::setFiles(const QStringList &files)
{
    m_files = files;
}

QStringList MultiFileDownloader::files() const
{
    return m_files;
}

void MultiFileDownloader::switchToNextFile()
{
    ++m_nextFile;

    if (m_nextFile < m_files.length()) {
        if (m_canceled) {
            emit downloadCanceled();
        } else {
            emit nextUrlChanged();
            emit nextTargetPathChanged();
            m_downloader->start();
        }
    } else {
        m_finished = true;
        emit finishedChanged();
    }
}

} // namespace QmlDesigner