diff options
| author | Akseli Lahtinen <[email protected]> | 2024-12-16 19:00:03 +0000 |
|---|---|---|
| committer | Méven Car <[email protected]> | 2024-12-16 19:00:03 +0000 |
| commit | 8d155558e980197b4dc2180660ea9bc61a23f3d3 (patch) | |
| tree | 4b166b514e51a1da1dbc51ec2a9485f2ee1edd1d /src/views/viewproperties.cpp | |
| parent | 37b081331e19b8b10bc80b8aecc4ff43de2474c0 (diff) | |
ViewProperties: Return nullptr if viewPropertiesString is empty
If viewPropertiesString is empty, return a nullptr.
This will later used in the stack by the defaultProperties call.
In defaultProperties, if we can't find the global directory,
create new one with a tempfile. If tempfiles can't be created,
use default instead.
This will ensure that view settings are saved and loaded correctly
if user has separate view properties per folder.
This will also add an unit test, where we create a global directory,
modify it and make sure the changes are reflected in the unmodified
folder.
BUG:495878
Diffstat (limited to 'src/views/viewproperties.cpp')
| -rw-r--r-- | src/views/viewproperties.cpp | 59 |
1 files changed, 39 insertions, 20 deletions
diff --git a/src/views/viewproperties.cpp b/src/views/viewproperties.cpp index 0536e028d..7e589019a 100644 --- a/src/views/viewproperties.cpp +++ b/src/views/viewproperties.cpp @@ -42,19 +42,26 @@ ViewPropertySettings *ViewProperties::loadProperties(const QString &folderPath) return new ViewPropertySettings(KSharedConfig::openConfig(settingsFile, KConfig::SimpleConfig)); } - QTemporaryFile tempFile; - tempFile.setAutoRemove(false); - if (!tempFile.open()) { - qCWarning(DolphinDebug) << "Could not open temp file"; - return nullptr; - } + auto createTempFile = []() -> QTemporaryFile * { + QTemporaryFile *tempFile = new QTemporaryFile; + tempFile->setAutoRemove(false); + if (!tempFile->open()) { + qCWarning(DolphinDebug) << "Could not open temp file"; + return nullptr; + } + return tempFile; + }; if (QFile::exists(settingsFile)) { // copy settings to tempfile to load them separately - QFile::remove(tempFile.fileName()); - QFile::copy(settingsFile, tempFile.fileName()); + const QTemporaryFile *tempFile = createTempFile(); + if (!tempFile) { + return nullptr; + } + QFile::remove(tempFile->fileName()); + QFile::copy(settingsFile, tempFile->fileName()); - auto config = KConfig(tempFile.fileName(), KConfig::SimpleConfig); + auto config = KConfig(tempFile->fileName(), KConfig::SimpleConfig); // ignore settings that are outside of dolphin scope if (config.hasGroup("Dolphin") || config.hasGroup("Settings")) { const auto groupList = config.groupList(); @@ -63,25 +70,30 @@ ViewPropertySettings *ViewProperties::loadProperties(const QString &folderPath) config.deleteGroup(group); } } - return new ViewPropertySettings(KSharedConfig::openConfig(tempFile.fileName(), KConfig::SimpleConfig)); + return new ViewPropertySettings(KSharedConfig::openConfig(tempFile->fileName(), KConfig::SimpleConfig)); } else if (!config.groupList().isEmpty()) { // clear temp file content - QFile::remove(tempFile.fileName()); + QFile::remove(tempFile->fileName()); } } // load from metadata const QString viewPropertiesString = metadata.attribute(QStringLiteral("kde.fm.viewproperties#1")); - if (!viewPropertiesString.isEmpty()) { - // load view properties from xattr to temp file then loads into ViewPropertySettings - // clear the temp file - QFile outputFile(tempFile.fileName()); - outputFile.open(QIODevice::WriteOnly); - outputFile.write(viewPropertiesString.toUtf8()); - outputFile.close(); + if (viewPropertiesString.isEmpty()) { + return nullptr; + } + // load view properties from xattr to temp file then loads into ViewPropertySettings + // clear the temp file + const QTemporaryFile *tempFile = createTempFile(); + if (!tempFile) { + return nullptr; } - return new ViewPropertySettings(KSharedConfig::openConfig(tempFile.fileName(), KConfig::SimpleConfig)); + QFile outputFile(tempFile->fileName()); + outputFile.open(QIODevice::WriteOnly); + outputFile.write(viewPropertiesString.toUtf8()); + outputFile.close(); + return new ViewPropertySettings(KSharedConfig::openConfig(tempFile->fileName(), KConfig::SimpleConfig)); } ViewPropertySettings *ViewProperties::defaultProperties() const @@ -89,7 +101,14 @@ ViewPropertySettings *ViewProperties::defaultProperties() const auto props = loadProperties(destinationDir(QStringLiteral("global"))); if (props == nullptr) { qCWarning(DolphinDebug) << "Could not load default global viewproperties"; - props = new ViewPropertySettings; + QTemporaryFile tempFile; + tempFile.setAutoRemove(false); + if (!tempFile.open()) { + qCWarning(DolphinDebug) << "Could not open temp file"; + props = new ViewPropertySettings; + } else { + props = new ViewPropertySettings(KSharedConfig::openConfig(tempFile.fileName(), KConfig::SimpleConfig)); + } } return props; |
