From fb3d5ad4762854c1dbd633d15bca9fc23505c360 Mon Sep 17 00:00:00 2001
From: Alexey Sokolov <sokolov@google.com>
Date: Fri, 10 Nov 2023 22:49:38 +0000
Subject: [PATCH] Read system projectm presets from subdirs

Fix #7151

Note that this requires C++17 patches from #7272 because I don't want to
bother backporting the fix to syntax available in C++11
---
 src/visualisations/projectmpresetmodel.cpp   | 20 ++++++++++++--------
 src/visualisations/projectmvisualisation.cpp |  2 +-
 2 files changed, 13 insertions(+), 9 deletions(-)

diff --git a/src/visualisations/projectmpresetmodel.cpp b/src/visualisations/projectmpresetmodel.cpp
index 67b0499350..9fcff21b2f 100644
--- a/src/visualisations/projectmpresetmodel.cpp
+++ b/src/visualisations/projectmpresetmodel.cpp
@@ -27,22 +27,26 @@
 #endif
 
 #include <QDir>
+#include <QDirIterator>
 #include <QtDebug>
+#include <set>
 
 ProjectMPresetModel::ProjectMPresetModel(ProjectMVisualisation* vis,
                                          QObject* parent)
     : QAbstractItemModel(parent), vis_(vis) {
   // Find presets
-  QDir preset_dir(vis_->preset_url());
-  QStringList presets(
-      preset_dir.entryList(QStringList() << "*.milk"
-                                         << "*.prjm",
+  QDirIterator it(vis_->preset_url(),
+                  QStringList() << "*.milk" << "*.prjm",
                            QDir::Files | QDir::NoDotAndDotDot | QDir::Readable,
-                           QDir::Name | QDir::IgnoreCase));
+                           QDirIterator::Subdirectories);
+  std::set<std::pair<QString, QString>> files;
+  while (it.hasNext()) {
+    it.next();
+    files.insert({it.filePath(), it.fileName()});
+  }
 
-  for (const QString& filename : presets) {
-    all_presets_ << Preset(preset_dir.absoluteFilePath(filename), filename,
-                           false);
+  for (const auto& [filePath, fileName] : files) {
+    all_presets_ << Preset(filePath, fileName, false);
   }
 }
 
diff --git a/src/visualisations/projectmvisualisation.cpp b/src/visualisations/projectmvisualisation.cpp
index abc94289ec..ba5fff6777 100644
--- a/src/visualisations/projectmvisualisation.cpp
+++ b/src/visualisations/projectmvisualisation.cpp
@@ -93,7 +93,7 @@ void ProjectMVisualisation::InitProjectM() {
     if (!QFile::exists(path)) continue;
 
     // Don't use empty directories
-    if (QDir(path).entryList(QDir::Files | QDir::NoDotAndDotDot).isEmpty())
+    if (QDir(path).entryList(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot).isEmpty())
       continue;
 
     preset_path = path;
