]> cloud.milkyroute.net Git - dolphin.git/commitdiff
Viewproperties: prevent loosing view settings
authorMéven Car <meven@kde.org>
Fri, 7 Feb 2025 10:16:05 +0000 (11:16 +0100)
committerMéven Car <meven@kde.org>
Sat, 8 Feb 2025 08:43:09 +0000 (09:43 +0100)
When they match the hardcoded internal settings, when they should be
kept as long as they don't match the currently set default
viewproperties.

Is saved in metadata the diff with the hardcoded internal defaults
still. A stable default reference allows to change defaults without
changing existing saved viewproperties.

CCBUG: 495878

src/tests/viewpropertiestest.cpp
src/views/viewproperties.cpp

index e4d43438320a62641e38c725231b55de54ea5c3c..f23ad4fc859872e8d58ff8dbc9424fa177dd6972 100644 (file)
@@ -29,6 +29,7 @@ private Q_SLOTS:
     void testParamMigrationToFileAttrKeepDirectory();
     void testExtendedAttributeFull();
     void testUseAsDefaultViewSettings();
+    void testUseAsCustomDefaultViewSettings();
 
 private:
     bool m_globalViewProps;
@@ -339,6 +340,69 @@ void ViewPropertiesTest::testUseAsDefaultViewSettings()
     QCOMPARE(testData->viewMode(), defaultData->viewMode());
 }
 
+void ViewPropertiesTest::testUseAsCustomDefaultViewSettings()
+{
+    // Create new test directory for this test to make sure the settings are defaults
+    auto testDir = new TestDir(QDir::homePath() + "/.viewPropertiesTest-");
+    auto cleanupTestDir = qScopeGuard([testDir] {
+        delete testDir;
+    });
+
+    // Create a global viewproperties folder
+    QUrl globalPropertiesPath =
+        QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation).append("/view_properties/").append(QStringLiteral("global")));
+    QVERIFY(QDir().mkpath(globalPropertiesPath.toLocalFile()));
+    auto cleanupGlobalDir = qScopeGuard([globalPropertiesPath] {
+        QDir().rmdir(globalPropertiesPath.toLocalFile());
+    });
+    ViewProperties globalProps(globalPropertiesPath);
+
+    // Check that theres no .directory file and metadata is supported
+    QString dotDirectoryFile = testDir->url().toLocalFile() + "/.directory";
+    QVERIFY(!QFile::exists(dotDirectoryFile));
+    KFileMetaData::UserMetaData testDirMetadata(testDir->url().toLocalFile());
+    KFileMetaData::UserMetaData globalDirMetadata(globalPropertiesPath.toLocalFile());
+    if (!testDirMetadata.isSupported()) {
+        QSKIP("need extended attribute/filesystem metadata to be usefull");
+    }
+
+    // Equivalent of useAsDefault in ViewPropertiesDialog
+    // This sets the DetailsView as default viewMode
+    GeneralSettings::setGlobalViewProps(true);
+    globalProps.setViewMode(DolphinView::Mode::DetailsView);
+    globalProps.setDirProperties(globalProps);
+    globalProps.save();
+    GeneralSettings::setGlobalViewProps(false);
+
+    // Make sure globalDirProperties are not empty, so they will be used
+    auto globalDirPropString = globalDirMetadata.attribute(QStringLiteral("kde.fm.viewproperties#1"));
+    QVERIFY(!globalDirPropString.isEmpty());
+
+    // Load default data
+    QScopedPointer<ViewProperties> globalDirProperties(new ViewProperties(globalPropertiesPath));
+    auto defaultData = globalDirProperties.data();
+    QCOMPARE(defaultData->viewMode(), DolphinView::Mode::DetailsView);
+
+    // Load testdir data, set to icon, i.e default hardcoded, not current user default
+    QScopedPointer<ViewProperties> testDirProperties(new ViewProperties(testDir->url()));
+    testDirProperties->setViewMode(DolphinView::Mode::IconsView);
+    testDirProperties->save();
+
+    // testDirProperties is not default
+    auto testDirPropString = testDirMetadata.attribute(QStringLiteral("kde.fm.viewproperties#1"));
+    QVERIFY(!testDirPropString.isEmpty());
+    QCOMPARE(testDirProperties.data()->viewMode(), DolphinView::Mode::IconsView);
+
+    // testDirProperties is now default
+    testDirProperties->setViewMode(DolphinView::Mode::DetailsView);
+    testDirProperties->save();
+
+    // no more metedata => the defaults settings are in effect for the folder
+    testDirPropString = testDirMetadata.attribute(QStringLiteral("kde.fm.viewproperties#1"));
+    QVERIFY(testDirPropString.isEmpty());
+    QCOMPARE(testDirProperties.data()->viewMode(), DolphinView::Mode::DetailsView);
+}
+
 QTEST_GUILESS_MAIN(ViewPropertiesTest)
 
 #include "viewpropertiestest.moc"
index 7e589019a862d7200fec195ef430d494db7a535f..5dbdd938eb2b796f4f8efea260207af413665af9 100644 (file)
@@ -531,9 +531,27 @@ void ViewProperties::save()
         const auto metaDataKey = QStringLiteral("kde.fm.viewproperties#1");
 
         const auto items = m_node->items();
-        const bool allDefault = std::all_of(items.cbegin(), items.cend(), [this](const KConfigSkeletonItem *item) {
-            return item->name() == "Timestamp" || (item->name() == "Version" && m_node->version() == CurrentViewPropertiesVersion) || item->isDefault();
-        });
+        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;