Add option to preserve "modification date" on file saving for UI

See https://github.com/Martchus/tageditor/issues/67
This commit is contained in:
Martchus 2021-08-21 01:22:29 +02:00
parent fef97aa1db
commit 1e084f8eae
5 changed files with 61 additions and 13 deletions

View File

@ -128,6 +128,8 @@ void restore()
v.tagPocessing.unsupportedFieldHandling = UnsupportedFieldHandling::Ignore; v.tagPocessing.unsupportedFieldHandling = UnsupportedFieldHandling::Ignore;
} }
v.tagPocessing.autoTagManagement = settings.value(QStringLiteral("autotagmanagement"), true).toBool(); v.tagPocessing.autoTagManagement = settings.value(QStringLiteral("autotagmanagement"), true).toBool();
v.tagPocessing.preserveModificationTime
= settings.value(QStringLiteral("preservemodificationtime"), v.tagPocessing.preserveModificationTime).toBool();
settings.beginGroup(QStringLiteral("id3v1")); settings.beginGroup(QStringLiteral("id3v1"));
switch (settings.value(QStringLiteral("usage"), 0).toInt()) { switch (settings.value(QStringLiteral("usage"), 0).toInt()) {
case 1: case 1:
@ -259,6 +261,7 @@ void save()
settings.setValue(QStringLiteral("preferredencoding"), static_cast<int>(v.tagPocessing.preferredEncoding)); settings.setValue(QStringLiteral("preferredencoding"), static_cast<int>(v.tagPocessing.preferredEncoding));
settings.setValue(QStringLiteral("unsupportedfieldhandling"), static_cast<int>(v.tagPocessing.unsupportedFieldHandling)); settings.setValue(QStringLiteral("unsupportedfieldhandling"), static_cast<int>(v.tagPocessing.unsupportedFieldHandling));
settings.setValue(QStringLiteral("autotagmanagement"), v.tagPocessing.autoTagManagement); settings.setValue(QStringLiteral("autotagmanagement"), v.tagPocessing.autoTagManagement);
settings.setValue(QStringLiteral("preservemodificationtime"), v.tagPocessing.preserveModificationTime);
settings.beginGroup(QStringLiteral("id3v1")); settings.beginGroup(QStringLiteral("id3v1"));
settings.setValue(QStringLiteral("usage"), static_cast<int>(v.tagPocessing.creationSettings.id3v1usage)); settings.setValue(QStringLiteral("usage"), static_cast<int>(v.tagPocessing.creationSettings.id3v1usage));
settings.endGroup(); settings.endGroup();

View File

@ -79,6 +79,7 @@ struct TagProcessing {
TagParser::TagTextEncoding preferredEncoding = TagParser::TagTextEncoding::Utf8; TagParser::TagTextEncoding preferredEncoding = TagParser::TagTextEncoding::Utf8;
UnsupportedFieldHandling unsupportedFieldHandling = UnsupportedFieldHandling::Ignore; UnsupportedFieldHandling unsupportedFieldHandling = UnsupportedFieldHandling::Ignore;
bool autoTagManagement = true; bool autoTagManagement = true;
bool preserveModificationTime = false;
TagParser::TagCreationSettings creationSettings; TagParser::TagCreationSettings creationSettings;
FileLayout fileLayout; FileLayout fileLayout;
}; };

View File

@ -357,6 +357,7 @@ bool TagProcessingGeneralOptionPage::apply()
settings.unsupportedFieldHandling = UnsupportedFieldHandling::Discard; settings.unsupportedFieldHandling = UnsupportedFieldHandling::Discard;
} }
settings.autoTagManagement = ui()->autoTagManagementCheckBox->isChecked(); settings.autoTagManagement = ui()->autoTagManagementCheckBox->isChecked();
settings.preserveModificationTime = ui()->preserveModificationTimeCheckBox->isChecked();
} }
return true; return true;
} }
@ -391,6 +392,7 @@ void TagProcessingGeneralOptionPage::reset()
break; break;
} }
ui()->autoTagManagementCheckBox->setChecked(settings.autoTagManagement); ui()->autoTagManagementCheckBox->setChecked(settings.autoTagManagement);
ui()->preserveModificationTimeCheckBox->setChecked(settings.preserveModificationTime);
} }
} }

View File

@ -53,6 +53,7 @@
#include <QtConcurrent> #include <QtConcurrent>
#include <algorithm> #include <algorithm>
#include <filesystem>
#include <functional> #include <functional>
#include <iostream> #include <iostream>
@ -1161,7 +1162,7 @@ bool TagEditorWidget::startSaving()
m_fileInfo.setMaxPadding(fileLayoutSettings.maxPadding); m_fileInfo.setMaxPadding(fileLayoutSettings.maxPadding);
m_fileInfo.setPreferredPadding(fileLayoutSettings.preferredPadding); m_fileInfo.setPreferredPadding(fileLayoutSettings.preferredPadding);
m_fileInfo.setBackupDirectory(settings.editor.backupDirectory); m_fileInfo.setBackupDirectory(settings.editor.backupDirectory);
const auto startThread = [this] { const auto startThread = [this, preserveModificationTime = settings.tagPocessing.preserveModificationTime] {
// define functions to show the saving progress and to actually applying the changes // define functions to show the saving progress and to actually applying the changes
auto showPercentage([this](AbortableProgressFeedback &progress) { auto showPercentage([this](AbortableProgressFeedback &progress) {
if (m_abortClicked) { if (m_abortClicked) {
@ -1183,6 +1184,13 @@ bool TagEditorWidget::startSaving()
auto ioError = QString(); auto ioError = QString();
auto processingError = false, canceled = false; auto processingError = false, canceled = false;
try { try {
auto modificationDateError = std::error_code();
auto modificationDate = std::filesystem::file_time_type();
auto modifiedFilePath = std::filesystem::path();
if (preserveModificationTime) {
modifiedFilePath = m_fileInfo.saveFilePath().empty() ? m_fileInfo.path() : m_fileInfo.saveFilePath();
modificationDate = std::filesystem::last_write_time(modifiedFilePath, modificationDateError);
}
try { try {
m_fileInfo.applyChanges(m_diag, progress); m_fileInfo.applyChanges(m_diag, progress);
} catch (const OperationAbortedException &) { } catch (const OperationAbortedException &) {
@ -1194,8 +1202,18 @@ bool TagEditorWidget::startSaving()
ioError = tr("unknown error"); ioError = tr("unknown error");
} }
} }
if (preserveModificationTime) {
if (!modificationDateError) {
std::filesystem::last_write_time(modifiedFilePath, modificationDate, modificationDateError);
}
if (modificationDateError) {
m_diag.emplace_back(
DiagLevel::Critical, "Unable to preserve modification time: " + modificationDateError.message(), "applying changes");
}
}
} catch (const exception &e) { } catch (const exception &e) {
m_diag.emplace_back(TagParser::DiagLevel::Critical, argsToString("Something completely unexpected happened: ", e.what()), "making"); m_diag.emplace_back(
TagParser::DiagLevel::Critical, argsToString("Something completely unexpected happened: ", e.what()), "applying changes");
processingError = true; processingError = true;
} }
QMetaObject::invokeMethod( QMetaObject::invokeMethod(

View File

@ -98,21 +98,45 @@
</widget> </widget>
</item> </item>
<item> <item>
<widget class="QLabel" name="label"> <widget class="QGroupBox" name="timestampsGroupBox_2">
<property name="text"> <property name="title">
<string>&lt;span style=&quot;font-weight: bold;&quot;&gt;Automatic tag management&lt;/span&gt; <string>Automatic tag management</string>
&lt;br&gt;If enabled, appropriate tags will be created and removed according to the settings automatically when opening a file. Otherwise you have to do this manually (eg. adding an ID3 tag if none is present yet) and settings like ID3 usage have no effect.</string>
</property>
<property name="wordWrap">
<bool>true</bool>
</property> </property>
<layout class="QVBoxLayout" name="verticalLayout_5">
<item>
<widget class="QLabel" name="label">
<property name="text">
<string>If enabled, appropriate tags will be created and removed according to the settings automatically when opening a file. Otherwise you have to do this manually (eg. adding an ID3 tag if none is present yet) and settings like ID3 usage have no effect.</string>
</property>
<property name="wordWrap">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QCheckBox" name="autoTagManagementCheckBox">
<property name="text">
<string>Enable automatic tag management (default)</string>
</property>
</widget>
</item>
</layout>
</widget> </widget>
</item> </item>
<item> <item>
<widget class="QCheckBox" name="autoTagManagementCheckBox"> <widget class="QGroupBox" name="timestampsGroupBox">
<property name="text"> <property name="title">
<string>Enable automatic tag management (default)</string> <string>Timestamps</string>
</property> </property>
<layout class="QVBoxLayout" name="verticalLayout_4">
<item>
<widget class="QCheckBox" name="preserveModificationTimeCheckBox">
<property name="text">
<string>Preserve modification time</string>
</property>
</widget>
</item>
</layout>
</widget> </widget>
</item> </item>
<item> <item>
@ -133,7 +157,7 @@
<resources/> <resources/>
<connections/> <connections/>
<buttongroups> <buttongroups>
<buttongroup name="preferredTextEncodingButtonGroup"/>
<buttongroup name="unsupportedButtonGroup"/> <buttongroup name="unsupportedButtonGroup"/>
<buttongroup name="preferredTextEncodingButtonGroup"/>
</buttongroups> </buttongroups>
</ui> </ui>