summaryrefslogtreecommitdiffstats
path: root/src/libs/installer/lib7zarchive.cpp
blob: d7b0c0dc976d39a62f812dd40a5865eb7925700d (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) 2021 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Qt Installer Framework.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
** 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.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
**************************************************************************/

#include "lib7zarchive.h"

#include "errors.h"
#include "lib7z_facade.h"
#include "lib7z_create.h"
#include "lib7z_list.h"

#include <QCoreApplication>

namespace QInstaller {

/*!
    \inmodule QtInstallerFramework
    \class QInstaller::Lib7zArchive
    \brief The Lib7zArchive class represents an archive file
           handled with the LZMA software development kit.
*/

/*!
    \inmodule QtInstallerFramework
    \class QInstaller::Lib7zArchive::ExtractCallbackWrapper
    \internal
*/

/*!
    Constructs an archive object representing an archive file
    specified by \a filename with \a parent as parent object.
*/
Lib7zArchive::Lib7zArchive(const QString &filename, QObject *parent)
    : AbstractArchive(parent)
    , m_extractCallback(new ExtractCallbackWrapper())
{
    Lib7zArchive::setFilename(filename);
    listenExtractCallback();
}

/*!
    Constructs an archive object with the given \a parent.
*/
Lib7zArchive::Lib7zArchive(QObject *parent)
    : AbstractArchive(parent)
    , m_extractCallback(new ExtractCallbackWrapper())
{
    listenExtractCallback();
}

/*!
    Destroys the instance and releases resources.
*/
Lib7zArchive::~Lib7zArchive()
{
    delete m_extractCallback;
}

/*!
    \reimp

    Opens the underlying file device using \a mode. Returns \c true if
    succesfull; otherwise \c false.
*/
bool Lib7zArchive::open(QIODevice::OpenMode mode)
{
    if (!m_file.open(mode)) {
        setErrorString(m_file.errorString());
        return false;
    }
    return true;
}

/*!
    \reimp

    Closes the underlying file device.
*/
void Lib7zArchive::close()
{
    m_file.close();
}

/*!
    \reimp

    Sets the \a filename of the underlying file device.
*/
void Lib7zArchive::setFilename(const QString &filename)
{
    m_file.setFileName(filename);
}

/*!
    \reimp

    Extracts the contents of this archive to \a dirPath.
    Returns \c true on success; \c false otherwise.
*/
bool Lib7zArchive::extract(const QString &dirPath)
{
    m_extractCallback->setState(S_OK);
    try {
        Lib7z::extractArchive(&m_file, dirPath, m_extractCallback);
    } catch (const Lib7z::SevenZipException &e) {
        setErrorString(e.message());
        return false;
    }
    return true;
}

/*!
    \reimp

    Extracts the contents of this archive to \a dirPath. The \a totalFiles
    parameter is unused. Returns \c true on success; \c false otherwise.
*/
bool Lib7zArchive::extract(const QString &dirPath, const quint64 totalFiles)
{
    Q_UNUSED(totalFiles)
    return extract(dirPath);
}

/*!
    \reimp

    Packages the given \a data into the archive and creates the file on disk.
*/
bool Lib7zArchive::create(const QStringList &data)
{
    try {
        // No support for callback yet.
        Lib7z::createArchive(&m_file, data, compressionLevel());
    } catch (const Lib7z::SevenZipException &e) {
        setErrorString(e.message());
        return false;
    }
    return true;
}

/*!
    \reimp

    Returns the contents of this archive as an array of \c ArchiveEntry objects.
    On failure, returns an empty array.
*/
QVector<ArchiveEntry> Lib7zArchive::list()
{
    try {
        return Lib7z::listArchive(&m_file);
    } catch (const Lib7z::SevenZipException &e) {
        setErrorString(e.message());
        return QVector<ArchiveEntry>();
    }
}

/*!
    \reimp

    Returns \c true if the current archive is of supported format;
    \c false otherwise.
*/
bool Lib7zArchive::isSupported()
{
    try {
        return Lib7z::isSupportedArchive(&m_file);
    } catch (const Lib7z::SevenZipException &e) {
        setErrorString(e.message());
        return false;
    }
}

/*!
    \reimp

    Cancels the extract operation in progress.
*/
void Lib7zArchive::cancel()
{
    m_extractCallback->setState(E_ABORT);
}

/*!
    \internal
*/
void Lib7zArchive::listenExtractCallback()
{
    connect(m_extractCallback, &ExtractCallbackWrapper::currentEntryChanged,
            this, &Lib7zArchive::currentEntryChanged);
    connect(m_extractCallback, &ExtractCallbackWrapper::completedChanged,
            this, &Lib7zArchive::completedChanged);
}


Lib7zArchive::ExtractCallbackWrapper::ExtractCallbackWrapper()
    : m_state(S_OK)
{
}

void Lib7zArchive::ExtractCallbackWrapper::setState(HRESULT state)
{
    m_state = state;
}

void Lib7zArchive::ExtractCallbackWrapper::setCurrentFile(const QString &filename)
{
    emit currentEntryChanged(filename);
}

HRESULT Lib7zArchive::ExtractCallbackWrapper::setCompleted(quint64 completed, quint64 total)
{
    qApp->processEvents();

    emit completedChanged(completed, total);
    return m_state;
}

} // namespace QInstaller