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
|
/*
SPDX-FileCopyrightText: 2016 Ragnar Thomsen <rthomsen6@gmail.com>
SPDX-License-Identifier: BSD-2-Clause
*/
#include "compressionoptionswidget.h"
#include "ark_debug.h"
#include "pluginmanager.h"
#include "settings.h"
#include <KColorScheme>
#include <KPluginMetaData>
#include <QMimeDatabase>
namespace Kerfuffle
{
CompressionOptionsWidget::CompressionOptionsWidget(QWidget *parent, const CompressionOptions &opts)
: QWidget(parent)
, m_opts(opts)
{
setupUi(this);
KColorScheme colorScheme(QPalette::Active, KColorScheme::View);
pwdWidget->setBackgroundWarningColor(colorScheme.background(KColorScheme::NegativeBackground).color());
pwdWidget->setPasswordStrengthMeterVisible(false);
#if QT_VERSION < QT_VERSION_CHECK(6, 7, 0)
connect(multiVolumeCheckbox, &QCheckBox::stateChanged, this, &CompressionOptionsWidget::slotMultiVolumeChecked);
#else
connect(multiVolumeCheckbox, &QCheckBox::checkStateChanged, this, &CompressionOptionsWidget::slotMultiVolumeChecked);
#endif
connect(compMethodComboBox, &QComboBox::currentTextChanged, this, &CompressionOptionsWidget::slotCompMethodChanged);
connect(encMethodComboBox, &QComboBox::currentTextChanged, this, &CompressionOptionsWidget::slotEncryptionMethodChanged);
if (m_opts.isVolumeSizeSet()) {
multiVolumeCheckbox->setChecked(true);
// Convert from kilobytes.
volumeSizeSpinbox->setValue(static_cast<double>(m_opts.volumeSize()) / 1024);
}
warningMsgWidget->setWordWrap(true);
}
CompressionOptions CompressionOptionsWidget::commpressionOptions() const
{
CompressionOptions opts;
opts.setCompressionLevel(compLevelSlider->value());
if (multiVolumeCheckbox->isChecked()) {
opts.setVolumeSize(volumeSize());
}
if (!compMethodComboBox->currentText().isEmpty()) {
opts.setCompressionMethod(compMethodComboBox->currentText());
}
return opts;
}
int CompressionOptionsWidget::compressionLevel() const
{
if (compLevelSlider->isEnabled()) {
return compLevelSlider->value();
} else {
return -1;
}
}
QString CompressionOptionsWidget::compressionMethod() const
{
return compMethodComboBox->currentText();
}
ulong CompressionOptionsWidget::volumeSize() const
{
if (collapsibleMultiVolume->isEnabled() && multiVolumeCheckbox->isChecked()) {
// Convert to kilobytes.
return static_cast<ulong>(volumeSizeSpinbox->value() * 1024);
} else {
return 0;
}
}
void CompressionOptionsWidget::setEncryptionVisible(bool visible)
{
collapsibleEncryption->setVisible(visible);
}
QString CompressionOptionsWidget::password() const
{
return pwdWidget->password();
}
ArchiveFormat CompressionOptionsWidget::archiveFormat() const
{
const KPluginMetaData metadata = PluginManager().preferredPluginFor(m_mimetype)->metaData();
return ArchiveFormat::fromMetadata(m_mimetype, metadata);
}
void CompressionOptionsWidget::updateWidgets()
{
const ArchiveFormat archiveFormat = this->archiveFormat();
Q_ASSERT(archiveFormat.isValid());
if (archiveFormat.encryptionType() != Archive::Unencrypted) {
collapsibleEncryption->setEnabled(true);
collapsibleEncryption->setToolTip(QString());
encMethodComboBox->clear();
encMethodComboBox->insertItems(0, archiveFormat.encryptionMethods());
if (!m_opts.encryptionMethod().isEmpty() && encMethodComboBox->findText(m_opts.encryptionMethod()) > -1) {
encMethodComboBox->setCurrentText(m_opts.encryptionMethod());
} else {
encMethodComboBox->setCurrentText(archiveFormat.defaultEncryptionMethod());
}
if (!archiveFormat.encryptionMethods().isEmpty()) {
lblEncMethod->setEnabled(true);
encMethodComboBox->setEnabled(true);
}
pwdWidget->setEnabled(true);
if (archiveFormat.encryptionType() == Archive::HeaderEncrypted) {
encryptHeaderCheckBox->setEnabled(true);
encryptHeaderCheckBox->setToolTip(QString());
} else {
encryptHeaderCheckBox->setEnabled(false);
// Show the tooltip only if the encryption is still enabled.
// This is needed because if the new filter is e.g. tar, the whole encryption group gets disabled.
if (collapsibleEncryption->isEnabled() && collapsibleEncryption->isExpanded()) {
encryptHeaderCheckBox->setToolTip(i18n("Protection of the list of files is not possible with the %1 format.", m_mimetype.comment()));
} else {
encryptHeaderCheckBox->setToolTip(QString());
}
}
} else {
collapsibleEncryption->setEnabled(false);
collapsibleEncryption->setToolTip(i18n("Protection of the archive with password is not possible with the %1 format.", m_mimetype.comment()));
lblEncMethod->setEnabled(false);
encMethodComboBox->setEnabled(false);
encMethodComboBox->clear();
pwdWidget->setEnabled(false);
encryptHeaderCheckBox->setToolTip(QString());
}
collapsibleCompression->setEnabled(true);
if (archiveFormat.maxCompressionLevel() == 0) {
compLevelSlider->setEnabled(false);
lblCompLevel1->setEnabled(false);
lblCompLevel2->setEnabled(false);
lblCompLevel3->setEnabled(false);
compLevelSlider->setToolTip(i18n("It is not possible to set compression level for the %1 format.", m_mimetype.comment()));
} else {
compLevelSlider->setEnabled(true);
lblCompLevel1->setEnabled(true);
lblCompLevel2->setEnabled(true);
lblCompLevel3->setEnabled(true);
compLevelSlider->setToolTip(QString());
compLevelSlider->setMinimum(archiveFormat.minCompressionLevel());
compLevelSlider->setMaximum(archiveFormat.maxCompressionLevel());
if (m_opts.isCompressionLevelSet()) {
compLevelSlider->setValue(m_opts.compressionLevel());
} else {
compLevelSlider->setValue(archiveFormat.defaultCompressionLevel());
}
}
if (archiveFormat.compressionMethods().isEmpty()) {
lblCompMethod->setEnabled(false);
compMethodComboBox->setEnabled(false);
compMethodComboBox->setToolTip(i18n("It is not possible to set compression method for the %1 format.", m_mimetype.comment()));
compMethodComboBox->clear();
} else {
lblCompMethod->setEnabled(true);
compMethodComboBox->setEnabled(true);
compMethodComboBox->setToolTip(QString());
compMethodComboBox->clear();
compMethodComboBox->insertItems(0, archiveFormat.compressionMethods().keys());
if (!m_opts.compressionMethod().isEmpty() && compMethodComboBox->findText(m_opts.compressionMethod()) > -1) {
compMethodComboBox->setCurrentText(m_opts.compressionMethod());
} else {
compMethodComboBox->setCurrentText(archiveFormat.defaultCompressionMethod());
}
}
collapsibleCompression->setEnabled(compLevelSlider->isEnabled() || compMethodComboBox->isEnabled());
if (archiveFormat.supportsMultiVolume()) {
collapsibleMultiVolume->setEnabled(true);
collapsibleMultiVolume->setToolTip(QString());
} else {
collapsibleMultiVolume->setEnabled(false);
collapsibleMultiVolume->setToolTip(i18n("The %1 format does not support multi-volume archives.", m_mimetype.comment()));
}
}
void CompressionOptionsWidget::setMimeType(const QMimeType &mimeType)
{
m_mimetype = mimeType;
updateWidgets();
}
bool CompressionOptionsWidget::isEncryptionAvailable() const
{
return collapsibleEncryption->isEnabled();
}
bool CompressionOptionsWidget::isEncryptionEnabled() const
{
return isEncryptionAvailable() && collapsibleEncryption->isExpanded();
}
bool CompressionOptionsWidget::isHeaderEncryptionAvailable() const
{
return isEncryptionEnabled() && encryptHeaderCheckBox->isEnabled();
}
bool CompressionOptionsWidget::isHeaderEncryptionEnabled() const
{
return isHeaderEncryptionAvailable() && encryptHeaderCheckBox->isChecked();
}
KNewPasswordWidget::PasswordStatus CompressionOptionsWidget::passwordStatus() const
{
return pwdWidget->passwordStatus();
}
QString CompressionOptionsWidget::encryptionMethod() const
{
if (encMethodComboBox->isEnabled() && encMethodComboBox->count() > 1 && !password().isEmpty()) {
return encMethodComboBox->currentText();
}
return QString();
}
void CompressionOptionsWidget::slotMultiVolumeChecked(int state)
{
if (state == Qt::Checked) {
lblVolumeSize->setEnabled(true);
volumeSizeSpinbox->setEnabled(true);
} else {
lblVolumeSize->setEnabled(false);
volumeSizeSpinbox->setEnabled(false);
}
}
void CompressionOptionsWidget::slotCompMethodChanged(const QString &value)
{
const ArchiveFormat archiveFormat = this->archiveFormat();
Q_ASSERT(archiveFormat.isValid());
if (m_mimetype == QMimeDatabase().mimeTypeForName(QStringLiteral("application/zip"))) {
if (value == QLatin1String("Zstd")) {
compLevelSlider->setMaximum(22);
} else {
compLevelSlider->setMaximum(archiveFormat.maxCompressionLevel());
}
}
}
void CompressionOptionsWidget::slotEncryptionMethodChanged(const QString &value)
{
if (value.isEmpty() || m_mimetype != QMimeDatabase().mimeTypeForName(QStringLiteral("application/zip"))) {
warningMsgWidget->hide();
return;
}
// AES encryption is not supported by unzip, warn the users if they are creating a zip.
warningMsgWidget->setVisible(value != QLatin1String("ZipCrypto") && ArkSettings::showEncryptionWarning());
}
}
#include "moc_compressionoptionswidget.cpp"
|