+
+ auto cleanDotDirectoryFile = [this]() {
+ const QString settingsFile = m_filePath + QDir::separator() + ViewPropertiesFileName;
+ if (QFile::exists(settingsFile)) {
+ qCDebug(DolphinDebug) << "cleaning .directory" << settingsFile;
+ KConfig cfg(settingsFile, KConfig::OpenFlag::SimpleConfig);
+ const auto groupList = cfg.groupList();
+ for (const auto &group : groupList) {
+ if (group == QStringLiteral("Dolphin") || group == QStringLiteral("Settings")) {
+ cfg.deleteGroup(group);
+ }
+ }
+ if (cfg.groupList().isEmpty()) {
+ QFile::remove(settingsFile);
+ } else if (cfg.isDirty()) {
+ cfg.sync();
+ }
+ }
+ };
+
+ // ensures the destination dir exists, in case we don't write metadata directly on the folder
+ QDir destinationDir(m_filePath);
+ if (!destinationDir.exists() && !destinationDir.mkpath(m_filePath)) {
+ qCWarning(DolphinDebug) << "Could not create fake directory to store metadata";
+ }
+
+ KFileMetaData::UserMetaData metaData(m_filePath);
+ if (metaData.isSupported()) {
+ const auto metaDataKey = QStringLiteral("kde.fm.viewproperties#1");
+
+ const auto items = m_node->items();
+ const auto defaultConfig = defaultProperties();
+ bool allDefault = true;
+ for (const auto item : items) {
+ if (item->name() == "Timestamp") {
+ continue;
+ }
+ if (item->name() == "Version") {
+ if (m_node->version() != CurrentViewPropertiesVersion) {
+ allDefault = false;
+ break;
+ } else {
+ continue;
+ }
+ }
+ auto defaultItem = defaultConfig->findItem(item->name());
+ if (!defaultItem || defaultItem->property() != item->property()) {
+ allDefault = false;
+ break;
+ }
+ }
+
+ if (allDefault) {
+ if (metaData.hasAttribute(metaDataKey)) {
+ qCDebug(DolphinDebug) << "clearing extended attributes for " << m_filePath;
+ const auto result = metaData.setAttribute(metaDataKey, QString());
+ if (result != KFileMetaData::UserMetaData::NoError) {
+ qCWarning(DolphinDebug) << "could not clear extended attributes for " << m_filePath << "error:" << result;
+ }
+ }
+ cleanDotDirectoryFile();
+ return;
+ }
+
+ // save config to disk
+ if (!m_node->save()) {
+ qCWarning(DolphinDebug) << "could not save viewproperties" << m_node->config()->name();
+ return;
+ }
+
+ QFile configFile(m_node->config()->name());
+ if (!configFile.open(QIODevice::ReadOnly)) {
+ qCWarning(DolphinDebug) << "Could not open readonly config file" << m_node->config()->name();
+ } else {
+ // load config from disk
+ const QString viewPropertiesString = configFile.readAll();
+
+ // save to xattr
+ const auto result = metaData.setAttribute(metaDataKey, viewPropertiesString);
+ if (result != KFileMetaData::UserMetaData::NoError) {
+ if (result == KFileMetaData::UserMetaData::NoSpace) {
+ // copy settings to dotDirectory file as fallback
+ if (!configFile.copy(m_filePath + QDir::separator() + ViewPropertiesFileName)) {
+ qCWarning(DolphinDebug) << "could not write viewproperties to .directory for dir " << m_filePath;
+ }
+ // free the space used by viewproperties from the file metadata
+ metaData.setAttribute(metaDataKey, "");
+ } else {
+ qCWarning(DolphinDebug) << "could not save viewproperties to extended attributes for dir " << m_filePath << "error:" << result;
+ }
+ // keep .directory file
+ return;
+ }
+ cleanDotDirectoryFile();
+ }
+
+ m_changedProps = false;
+ return;
+ }
+