Commit f222a505 authored by Ragnar Thomsen's avatar Ragnar Thomsen
Browse files

Implement GUI to set compression level for new archives

Three new int's were added to plugin json files specifying minimum,
maximum and default compression level for each readwrite mimetype. A new
KCollapsibleGroupBox with a QSlider was added to CreateDialog to allow
setting the compression level.

FEATURE: 148702
FIXED-IN: 16.08
Differential Revision: D1364
parent b329dcea
......@@ -306,6 +306,9 @@ void MainWindow::newArchive()
qCDebug(ARK) << "CreateDialog returned mime:" << dialog.data()->currentMimeType().name();
m_openArgs.metaData()[QStringLiteral("createNewArchive")] = QStringLiteral("true");
if (dialog.data()->compressionLevel() > -1) {
m_openArgs.metaData()[QStringLiteral("compressionLevel")] = QString::number(dialog.data()->compressionLevel());
}
m_openArgs.metaData()[QStringLiteral("encryptionPassword")] = password;
if (dialog.data()->isHeaderEncryptionEnabled()) {
......@@ -316,6 +319,7 @@ void MainWindow::newArchive()
m_openArgs.metaData().remove(QStringLiteral("showExtractDialog"));
m_openArgs.metaData().remove(QStringLiteral("createNewArchive"));
m_openArgs.metaData().remove(QStringLiteral("compressionLevel"));
m_openArgs.metaData().remove(QStringLiteral("encryptionPassword"));
m_openArgs.metaData().remove(QStringLiteral("encryptHeader"));
}
......
......@@ -33,9 +33,16 @@ ArchiveFormat::ArchiveFormat() :
{
}
ArchiveFormat::ArchiveFormat(const QMimeType& mimeType, Archive::EncryptionType encryptionType) :
ArchiveFormat::ArchiveFormat(const QMimeType& mimeType,
Archive::EncryptionType encryptionType,
int minCompLevel,
int maxCompLevel,
int defaultCompLevel) :
m_mimeType(mimeType),
m_encryptionType(encryptionType)
m_encryptionType(encryptionType),
m_minCompressionLevel(minCompLevel),
m_maxCompressionLevel(maxCompLevel),
m_defaultCompressionLevel(defaultCompLevel)
{
}
......@@ -48,17 +55,19 @@ ArchiveFormat ArchiveFormat::fromMetadata(const QMimeType& mimeType, const KPlug
}
const QJsonObject formatProps = json[mime].toObject();
if (formatProps.isEmpty()) {
return ArchiveFormat(mimeType, Archive::Unencrypted);
}
int minCompLevel = formatProps[QStringLiteral("CompressionLevelMin")].toInt();
int maxCompLevel = formatProps[QStringLiteral("CompressionLevelMax")].toInt();
int defaultCompLevel = formatProps[QStringLiteral("CompressionLevelDefault")].toInt();
Archive::EncryptionType encType = Archive::Unencrypted;
if (formatProps[QStringLiteral("HeaderEncryption")].toBool()) {
return ArchiveFormat(mimeType, Archive::HeaderEncrypted);
encType = Archive::HeaderEncrypted;
} else if (formatProps[QStringLiteral("Encryption")].toBool()) {
encType = Archive::Encrypted;
}
if (formatProps[QStringLiteral("Encryption")].toBool()) {
return ArchiveFormat(mimeType, Archive::Encrypted);
}
return ArchiveFormat(mimeType, encType, minCompLevel, maxCompLevel, defaultCompLevel);
}
return ArchiveFormat();
......@@ -74,4 +83,19 @@ Archive::EncryptionType ArchiveFormat::encryptionType() const
return m_encryptionType;
}
int ArchiveFormat::minCompressionLevel() const
{
return m_minCompressionLevel;
}
int ArchiveFormat::maxCompressionLevel() const
{
return m_maxCompressionLevel;
}
int ArchiveFormat::defaultCompressionLevel() const
{
return m_defaultCompressionLevel;
}
}
......@@ -37,7 +37,11 @@ class KERFUFFLE_EXPORT ArchiveFormat
{
public:
explicit ArchiveFormat();
explicit ArchiveFormat(const QMimeType& mimeType, Kerfuffle::Archive::EncryptionType encryptionType);
explicit ArchiveFormat(const QMimeType& mimeType,
Kerfuffle::Archive::EncryptionType encryptionType,
int minCompLevel,
int maxCompLevel,
int defaultCompLevel);
/**
* @return The archive format of the given @p mimeType, according to the given @p metadata.
......@@ -54,11 +58,18 @@ public:
*/
Kerfuffle::Archive::EncryptionType encryptionType() const;
int minCompressionLevel() const;
int maxCompressionLevel() const;
int defaultCompressionLevel() const;
private:
QMimeType m_mimeType;
Kerfuffle::Archive::EncryptionType m_encryptionType;
int m_minCompressionLevel;
int m_maxCompressionLevel;
int m_defaultCompressionLevel;
};
}
#endif // ARCHIVEFORMAT_H
\ No newline at end of file
#endif // ARCHIVEFORMAT_H
......@@ -252,12 +252,14 @@ bool CliInterface::addFiles(const QStringList & files, const CompressionOptions&
QDir::setCurrent(globalWorkDir);
}
int compLevel = options.value(QStringLiteral("CompressionLevel"), -1).toInt();
const auto args = substituteAddVariables(m_param.value(AddArgs).toStringList(),
files,
workDir,
password(),
isHeaderEncryptionEnabled(),
options.value(QStringLiteral("CompressionLevel")).toInt());
compLevel);
if (!runProcess(m_param.value(AddProgram).toStringList(), args)) {
failOperation();
......@@ -718,11 +720,14 @@ QStringList CliInterface::passwordSwitch(const QString& password) const
QString CliInterface::compressionLevelSwitch(int level) const
{
if (level < 0 || level > 9) {
return QString();
}
Q_ASSERT(m_param.contains(CompressionLevelSwitch));
QString compLevelSwitch = m_param.value(CompressionLevelSwitch).toString();
Q_ASSERT(!compLevelSwitch.isEmpty());
Q_ASSERT(level >= 0 && level <= 9);
compLevelSwitch.replace(QLatin1String("$CompressionLevel"), QString::number(level));
......
......@@ -133,6 +133,16 @@ void CreateDialog::slotUpdateWidgets(int index)
m_ui->collapsibleEncryption->setToolTip(i18n("Protection of the archive with password is not possible with the %1 format.",
mimeType.comment()));
}
if (archiveFormat.maxCompressionLevel() == 0) {
m_ui->collapsibleCompression->setEnabled(false);
} else {
m_ui->collapsibleCompression->setEnabled(true);
m_ui->compLevelSlider->setMinimum(archiveFormat.minCompressionLevel());
m_ui->compLevelSlider->setMaximum(archiveFormat.maxCompressionLevel());
m_ui->compLevelSlider->setValue(archiveFormat.defaultCompressionLevel());
}
slotEncryptionToggled();
}
......@@ -164,6 +174,14 @@ QUrl CreateDialog::selectedUrl() const
return QUrl::fromLocalFile(dir + fileName);
}
int CreateDialog::compressionLevel() const
{
if (m_ui->compLevelSlider->isEnabled()) {
return m_ui->compLevelSlider->value();
}
return -1;
}
QString CreateDialog::password() const
{
return m_ui->pwdWidget->password();
......
......@@ -57,6 +57,7 @@ public:
QString password() const;
QMimeType currentMimeType() const;
bool setMimeType(const QString &mimeTypeName);
int compressionLevel() const;
/**
* @return Whether the user can encrypt the new archive.
......
......@@ -7,7 +7,7 @@
<x>0</x>
<y>0</y>
<width>502</width>
<height>228</height>
<height>369</height>
</rect>
</property>
<property name="minimumSize">
......@@ -70,6 +70,13 @@
</property>
</widget>
</item>
<item row="4" column="0">
<widget class="QLabel" name="label_4">
<property name="text">
<string>Extension:</string>
</property>
</widget>
</item>
<item row="4" column="1">
<widget class="QCheckBox" name="chkAddExtension">
<property name="text">
......@@ -80,13 +87,6 @@
</property>
</widget>
</item>
<item row="4" column="0">
<widget class="QLabel" name="label_4">
<property name="text">
<string>Extension:</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
......@@ -133,6 +133,73 @@
</layout>
</widget>
</item>
<item>
<widget class="KCollapsibleGroupBox" name="collapsibleCompression">
<property name="enabled">
<bool>false</bool>
</property>
<property name="title">
<string>Compression</string>
</property>
<property name="expanded">
<bool>false</bool>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="1">
<widget class="QLabel" name="label_6">
<property name="text">
<string>Min</string>
</property>
</widget>
</item>
<item row="0" column="2">
<widget class="QLabel" name="label_7">
<property name="text">
<string>Max</string>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_5">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>1</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Level:</string>
</property>
</widget>
</item>
<item row="1" column="1" colspan="2">
<widget class="QSlider" name="compLevelSlider">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
<horstretch>3</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="maximum">
<number>9</number>
</property>
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="tickPosition">
<enum>QSlider::TicksBothSides</enum>
</property>
<property name="tickInterval">
<number>1</number>
</property>
</widget>
</item>
</layout>
</widget>
</item>
<item>
<widget class="Line" name="line_2">
<property name="orientation">
......
......@@ -1105,6 +1105,10 @@ void Part::slotAddFiles(const QStringList& filesToAdd, const QString& path)
CompressionOptions options;
options[QStringLiteral("GlobalWorkDir")] = globalWorkDir;
if (arguments().metaData().contains(QStringLiteral("compressionLevel"))) {
options[QStringLiteral("CompressionLevel")] = arguments().metaData()[QStringLiteral("compressionLevel")];
}
AddJob *job = m_model->addFiles(cleanFilesToAdd, options);
if (!job) {
return;
......
......@@ -58,9 +58,15 @@
],
"X-KDE-Priority": 180,
"application/x-7z-compressed": {
"HeaderEncryption": true
"HeaderEncryption": true,
"CompressionLevelMin": 0,
"CompressionLevelMax": 9,
"CompressionLevelDefault": 5
},
"application/zip": {
"Encryption": true
"Encryption": true,
"CompressionLevelMin": 0,
"CompressionLevelMax": 9,
"CompressionLevelDefault": 5
}
}
\ No newline at end of file
}
......@@ -58,6 +58,9 @@
],
"X-KDE-Priority": 120,
"application/x-rar": {
"HeaderEncryption": true
"HeaderEncryption": true,
"CompressionLevelMin": 0,
"CompressionLevelMax": 5,
"CompressionLevelDefault": 3
}
}
\ No newline at end of file
}
......@@ -57,9 +57,15 @@
],
"X-KDE-Priority": 160,
"application/x-java-archive": {
"Encryption": true
"Encryption": true,
"CompressionLevelMin": 0,
"CompressionLevelMax": 9,
"CompressionLevelDefault": 6
},
"application/zip": {
"Encryption": true
"Encryption": true,
"CompressionLevelMin": 0,
"CompressionLevelMax": 9,
"CompressionLevelDefault": 6
}
}
\ No newline at end of file
}
......@@ -57,5 +57,40 @@
},
"X-KDE-Kerfuffle-APIRevision": 1,
"X-KDE-Kerfuffle-ReadWrite": true,
"X-KDE-Priority": 100
}
\ No newline at end of file
"X-KDE-Priority": 100,
"application/x-compressed-tar": {
"CompressionLevelMin": 1,
"CompressionLevelMax": 9,
"CompressionLevelDefault": 6
},
"application/x-bzip-compressed-tar": {
"CompressionLevelMin": 1,
"CompressionLevelMax": 9,
"CompressionLevelDefault": 9
},
"application/x-xz-compressed-tar": {
"CompressionLevelMin": 0,
"CompressionLevelMax": 9,
"CompressionLevelDefault": 6
},
"application/x-lzma-compressed-tar": {
"CompressionLevelMin": 0,
"CompressionLevelMax": 9,
"CompressionLevelDefault": 6
},
"application/x-lzip-compressed-tar": {
"CompressionLevelMin": 0,
"CompressionLevelMax": 9,
"CompressionLevelDefault": 6
},
"application/x-tzo": {
"CompressionLevelMin": 1,
"CompressionLevelMax": 9,
"CompressionLevelDefault": 5
},
"application/x-lrzip-compressed-tar": {
"CompressionLevelMin": 1,
"CompressionLevelMax": 9,
"CompressionLevelDefault": 1
}
}
......@@ -144,6 +144,19 @@ bool ReadWriteLibarchivePlugin::addFiles(const QStringList& files, const Compres
QLatin1String(archive_error_string(arch_writer.data()))));
return false;
}
// Set compression level if passed in CompressionOptions.
if (options.contains(QStringLiteral("CompressionLevel"))) {
qCDebug(ARK) << "Using compression level:" << options.value(QStringLiteral("CompressionLevel")).toString();
ret = archive_write_set_filter_option(arch_writer.data(), NULL, "compression-level", options.value(QStringLiteral("CompressionLevel")).toString().toUtf8());
if (ret != ARCHIVE_OK) {
qCWarning(ARK) << "Failed to set compression level";
emit error(xi18nc("@info", "Setting the compression level failed with the following error:<nl/><message>%1</message>",
QLatin1String(archive_error_string(arch_writer.data()))));
return false;
}
}
} else {
switch (archive_filter_code(arch_reader.data(), 0)) {
case ARCHIVE_FILTER_GZIP:
......@@ -184,18 +197,6 @@ bool ReadWriteLibarchivePlugin::addFiles(const QStringList& files, const Compres
QLatin1String(archive_error_string(arch_writer.data()))));
return false;
}
// Set compression level if passed in CompressionOptions.
if (options.contains(QStringLiteral("CompressionLevel"))) {
qCDebug(ARK) << "Using compression level:" << options.value(QStringLiteral("CompressionLevel")).toString();
ret = archive_write_set_filter_option(arch_writer.data(), NULL, "compression-level", options.value(QStringLiteral("CompressionLevel")).toString().toUtf8());
if (ret != ARCHIVE_OK) {
qCWarning(ARK) << "Failed to set compression level";
emit error(xi18nc("@info", "Setting the compression level failed with the following error:<nl/><message>%1</message>",
QLatin1String(archive_error_string(arch_writer.data()))));
return false;
}
}
}
ret = archive_write_open_fd(arch_writer.data(), tempFile.handle());
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment