The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

173 lines
5.5KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 7 technical preview.
  4. Copyright (c) 2022 - Raw Material Software Limited
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For the technical preview this file cannot be licensed commercially.
  8. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  9. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  10. DISCLAIMED.
  11. ==============================================================================
  12. */
  13. #pragma once
  14. #include "jucer_ModuleDescription.h"
  15. #include <future>
  16. //==============================================================================
  17. class AvailableModulesList : private AsyncUpdater
  18. {
  19. public:
  20. using ModuleIDAndFolder = std::pair<String, File>;
  21. using ModuleIDAndFolderList = std::vector<ModuleIDAndFolder>;
  22. AvailableModulesList() = default;
  23. //==============================================================================
  24. void scanPaths (const Array<File>& paths)
  25. {
  26. scanPathsAsync (paths);
  27. scanner = {};
  28. }
  29. void scanPathsAsync (const Array<File>& paths)
  30. {
  31. scanner = std::async (std::launch::async, [this, paths]
  32. {
  33. ModuleIDAndFolderList list;
  34. for (auto& p : paths)
  35. addAllModulesInFolder (p, list);
  36. std::sort (list.begin(), list.end(), [] (const ModuleIDAndFolder& m1,
  37. const ModuleIDAndFolder& m2)
  38. {
  39. return m1.first.compareIgnoreCase (m2.first) < 0;
  40. });
  41. {
  42. const ScopedLock swapLock (lock);
  43. if (list == modulesList)
  44. return;
  45. modulesList.swap (list);
  46. }
  47. triggerAsyncUpdate();
  48. });
  49. }
  50. //==============================================================================
  51. ModuleIDAndFolderList getAllModules() const
  52. {
  53. const ScopedLock readLock (lock);
  54. return modulesList;
  55. }
  56. ModuleIDAndFolder getModuleWithID (const String& id) const
  57. {
  58. const ScopedLock readLock (lock);
  59. for (auto& mod : modulesList)
  60. if (mod.first == id)
  61. return mod;
  62. return {};
  63. }
  64. //==============================================================================
  65. void removeDuplicates (const ModuleIDAndFolderList& other)
  66. {
  67. const ScopedLock readLock (lock);
  68. const auto predicate = [&] (const ModuleIDAndFolder& entry)
  69. {
  70. return std::find (other.begin(), other.end(), entry) != other.end();
  71. };
  72. modulesList.erase (std::remove_if (modulesList.begin(), modulesList.end(), predicate),
  73. modulesList.end());
  74. }
  75. //==============================================================================
  76. struct Listener
  77. {
  78. virtual ~Listener() = default;
  79. virtual void availableModulesChanged (AvailableModulesList* listThatHasChanged) = 0;
  80. };
  81. void addListener (Listener* listenerToAdd) { listeners.add (listenerToAdd); }
  82. void removeListener (Listener* listenerToRemove) { listeners.remove (listenerToRemove); }
  83. private:
  84. //==============================================================================
  85. static bool tryToAddModuleFromFolder (const File& path, ModuleIDAndFolderList& list)
  86. {
  87. ModuleDescription m (path);
  88. if (m.isValid()
  89. && std::none_of (list.begin(), list.end(),
  90. [&m] (const ModuleIDAndFolder& element) { return element.first == m.getID(); }))
  91. {
  92. list.push_back ({ m.getID(), path });
  93. return true;
  94. }
  95. return false;
  96. }
  97. static void addAllModulesInFolder (const File& topLevelPath, ModuleIDAndFolderList& list)
  98. {
  99. struct FileAndDepth
  100. {
  101. File file;
  102. int depth;
  103. };
  104. std::queue<FileAndDepth> pathsToCheck;
  105. pathsToCheck.push ({ topLevelPath, 0 });
  106. while (! pathsToCheck.empty())
  107. {
  108. const auto path = pathsToCheck.front();
  109. pathsToCheck.pop();
  110. if (tryToAddModuleFromFolder (path.file, list) || path.depth == 3)
  111. continue;
  112. for (const auto& iter : RangedDirectoryIterator (path.file, false, "*", File::findDirectories))
  113. {
  114. if (auto* job = ThreadPoolJob::getCurrentThreadPoolJob())
  115. if (job->shouldExit())
  116. return;
  117. pathsToCheck.push({ iter.getFile(), path.depth + 1 });
  118. }
  119. }
  120. }
  121. //==============================================================================
  122. void handleAsyncUpdate() override
  123. {
  124. listeners.call ([this] (Listener& l) { l.availableModulesChanged (this); });
  125. }
  126. //==============================================================================
  127. ModuleIDAndFolderList modulesList;
  128. ListenerList<Listener> listeners;
  129. CriticalSection lock;
  130. std::future<void> scanner;
  131. //==============================================================================
  132. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (AvailableModulesList)
  133. };