aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/qmldesigner/utils/fileextractor.cpp
blob: 7c32381b69cc66c76fa633334f72fab706a40294 (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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
// 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 "fileextractor.h"

#include <QQmlEngine>
#include <QStorageInfo>

#include <extensionsystem/pluginmanager.h>
#include <extensionsystem/pluginspec.h>

#include <utils/algorithm.h>
#include <utils/archive.h>
#include <utils/fileutils.h>
#include <utils/qtcassert.h>

namespace QmlDesigner {

FileExtractor::FileExtractor(QObject *parent)
    : QObject(parent)
{
    m_timer.setInterval(100);
    m_timer.setSingleShot(false);

    QObject::connect(this, &FileExtractor::targetFolderExistsChanged, this, [this]() {
        if (targetFolderExists())
            m_birthTime = QFileInfo(m_targetPath.toString() + "/" + m_archiveName).birthTime();
        else
            m_birthTime = QDateTime();

        emit birthTimeChanged();
    });

    QObject::connect(
        &m_timer, &QTimer::timeout, this, [this]() {
            static QHash<QString, int> hash;
            QDirIterator it(m_targetFolder, {"*.*"}, QDir::Files, QDirIterator::Subdirectories);

            int count = 0;
            while (it.hasNext()) {
                if (!hash.contains(it.fileName())) {
                    m_currentFile = it.fileName();
                    hash.insert(m_currentFile, 0);
                    emit currentFileChanged();
                }
                it.next();
                count++;
            }

            qint64 currentSize = m_bytesBefore
                                 - QStorageInfo(m_targetPath.toFileInfo().dir()).bytesAvailable();

            // We can not get the uncompressed size of the archive yet, that is why we use an
            // approximation. We assume a 50% compression rate.

            int progress = 0;
            if (m_compressedSize > 0)
                progress = std::min(100ll, currentSize * 100 / m_compressedSize * 2);

            if (progress >= 0) {
                m_progress = progress;
                emit progressChanged();
            } else {
                qWarning() << "FileExtractor has got negative progress. Likely due to QStorageInfo.";
            }

            m_size = QString::number(currentSize);
            m_count = QString::number(count);
            emit sizeChanged();
        });
}

FileExtractor::~FileExtractor() {}

void FileExtractor::changeTargetPath(const QString &path)
{
    m_targetPath = Utils::FilePath::fromString(path);
    emit targetPathChanged();
    emit targetFolderExistsChanged();
}

QString FileExtractor::targetPath() const
{
    return m_targetPath.toUserOutput();
}

void FileExtractor::setTargetPath(const QString &path)
{
    m_targetPath = Utils::FilePath::fromString(path);

    QDir dir(m_targetPath.toString());

    if (!path.isEmpty() && !dir.exists()) {
        // Even though m_targetPath will be created eventually, we need to make sure the path exists
        // before m_bytesBefore is being calculated.
        dir.mkpath(m_targetPath.toString());
    }
}

void FileExtractor::browse()
{
    const Utils::FilePath path =
        Utils::FileUtils::getExistingDirectory(nullptr, tr("Choose Directory"), m_targetPath);

    if (!path.isEmpty())
        m_targetPath = path;

    emit targetPathChanged();
    emit targetFolderExistsChanged();
}

void FileExtractor::setSourceFile(const QString &sourceFilePath)
{
    m_sourceFile = Utils::FilePath::fromString(sourceFilePath);
    emit targetFolderExistsChanged();
}

void FileExtractor::setArchiveName(const QString &filePath)
{
    m_archiveName = filePath;
    emit targetFolderExistsChanged();
}

const QString FileExtractor::detailedText() const
{
    return m_detailedText;
}

void FileExtractor::setClearTargetPathContents(bool value)
{
    if (m_clearTargetPathContents != value) {
        m_clearTargetPathContents = value;
        emit clearTargetPathContentsChanged();
    }
}

bool FileExtractor::clearTargetPathContents() const
{
    return m_clearTargetPathContents;
}

void FileExtractor::setAlwaysCreateDir(bool value)
{
    if (m_alwaysCreateDir != value) {
        m_alwaysCreateDir = value;
        emit alwaysCreateDirChanged();
    }
}

bool FileExtractor::alwaysCreateDir() const
{
    return m_alwaysCreateDir;
}

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

QString FileExtractor::currentFile() const
{
    return m_currentFile;
}

QString FileExtractor::size() const
{
    return m_size;
}

QString FileExtractor::count() const
{
    return m_count;
}

bool FileExtractor::targetFolderExists() const
{
    return QFileInfo::exists(m_targetPath.toString() + "/" + m_archiveName);
}

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

QDateTime FileExtractor::birthTime() const
{
    return m_birthTime;
}

QString FileExtractor::archiveName() const
{
    return m_archiveName;
}

QString FileExtractor::sourceFile() const
{
    return m_sourceFile.toString();
}

void FileExtractor::extract()
{
    m_targetFolder = m_targetPath.toString() + "/" + m_archiveName;

    // If the target directory already exists, remove it and its content
    QDir targetDir(m_targetFolder);
    if (targetDir.exists() && m_clearTargetPathContents)
        targetDir.removeRecursively();

    if (m_alwaysCreateDir) {
        // Create a new directory to generate a proper creation date
        targetDir.mkdir(m_targetFolder);
    }

    Utils::Archive *archive = new Utils::Archive(m_sourceFile, m_targetPath);
    QTC_ASSERT(archive->isValid(), delete archive; return);

    m_timer.start();
    m_bytesBefore = QStorageInfo(m_targetPath.toFileInfo().dir()).bytesAvailable();
    m_compressedSize = QFileInfo(m_sourceFile.toString()).size();
    if (m_compressedSize <= 0)
        qWarning() << "Compressed size for file '" << m_sourceFile << "' is zero or invalid: " << m_compressedSize;

    QObject::connect(archive, &Utils::Archive::outputReceived, this, [this](const QString &output) {
        m_detailedText += output;
        emit detailedTextChanged();
    });

    QObject::connect(archive, &Utils::Archive::finished, this, [this, archive](bool ret) {
        archive->deleteLater();
        m_finished = ret;
        m_timer.stop();

        m_progress = 100;
        emit progressChanged();

        emit targetFolderExistsChanged();
        emit finishedChanged();
        QTC_CHECK(ret);
    });
    archive->unarchive();
}

} // namespace QmlDesigner