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.

216 lines
7.1KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 6 End-User License
  8. Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
  9. End User License Agreement: www.juce.com/juce-6-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. #pragma once
  19. #include "jucer_ModuleDescription.h"
  20. //==============================================================================
  21. class AvailableModulesList : private AsyncUpdater
  22. {
  23. public:
  24. using ModuleIDAndFolder = std::pair<String, File>;
  25. using ModuleIDAndFolderList = std::vector<ModuleIDAndFolder>;
  26. AvailableModulesList() = default;
  27. //==============================================================================
  28. void scanPaths (const Array<File>& paths)
  29. {
  30. auto job = createScannerJob (paths);
  31. auto& ref = *job;
  32. removePendingAndAddJob (std::move (job));
  33. scanPool.waitForJobToFinish (&ref, -1);
  34. }
  35. void scanPathsAsync (const Array<File>& paths)
  36. {
  37. removePendingAndAddJob (createScannerJob (paths));
  38. }
  39. //==============================================================================
  40. ModuleIDAndFolderList getAllModules() const
  41. {
  42. const ScopedLock readLock (lock);
  43. return modulesList;
  44. }
  45. ModuleIDAndFolder getModuleWithID (const String& id) const
  46. {
  47. const ScopedLock readLock (lock);
  48. for (auto& mod : modulesList)
  49. if (mod.first == id)
  50. return mod;
  51. return {};
  52. }
  53. //==============================================================================
  54. void removeDuplicates (const ModuleIDAndFolderList& other)
  55. {
  56. const ScopedLock readLock (lock);
  57. const auto predicate = [&] (const ModuleIDAndFolder& entry)
  58. {
  59. return std::find (other.begin(), other.end(), entry) != other.end();
  60. };
  61. modulesList.erase (std::remove_if (modulesList.begin(), modulesList.end(), predicate),
  62. modulesList.end());
  63. }
  64. //==============================================================================
  65. struct Listener
  66. {
  67. virtual ~Listener() = default;
  68. virtual void availableModulesChanged (AvailableModulesList* listThatHasChanged) = 0;
  69. };
  70. void addListener (Listener* listenerToAdd) { listeners.add (listenerToAdd); }
  71. void removeListener (Listener* listenerToRemove) { listeners.remove (listenerToRemove); }
  72. private:
  73. //==============================================================================
  74. struct ModuleScannerJob : public ThreadPoolJob
  75. {
  76. ModuleScannerJob (const Array<File>& paths,
  77. std::function<void (const ModuleIDAndFolderList&)>&& callback)
  78. : ThreadPoolJob ("ModuleScannerJob"),
  79. pathsToScan (paths),
  80. completionCallback (std::move (callback))
  81. {
  82. }
  83. JobStatus runJob() override
  84. {
  85. ModuleIDAndFolderList list;
  86. for (auto& p : pathsToScan)
  87. addAllModulesInFolder (p, list);
  88. if (! shouldExit())
  89. {
  90. std::sort (list.begin(), list.end(), [] (const ModuleIDAndFolder& m1,
  91. const ModuleIDAndFolder& m2)
  92. {
  93. return m1.first.compareIgnoreCase (m2.first) < 0;
  94. });
  95. completionCallback (list);
  96. }
  97. return jobHasFinished;
  98. }
  99. static bool tryToAddModuleFromFolder (const File& path, ModuleIDAndFolderList& list)
  100. {
  101. ModuleDescription m (path);
  102. if (m.isValid()
  103. && std::find_if (list.begin(), list.end(),
  104. [&m] (const ModuleIDAndFolder& element) { return element.first == m.getID(); }) == std::end (list))
  105. {
  106. list.push_back ({ m.getID(), path });
  107. return true;
  108. }
  109. return false;
  110. }
  111. static void addAllModulesInFolder (const File& topLevelPath, ModuleIDAndFolderList& list)
  112. {
  113. struct FileAndDepth
  114. {
  115. File file;
  116. int depth;
  117. };
  118. std::queue<FileAndDepth> pathsToCheck;
  119. pathsToCheck.push ({ topLevelPath, 0 });
  120. while (! pathsToCheck.empty())
  121. {
  122. const auto path = pathsToCheck.front();
  123. pathsToCheck.pop();
  124. if (tryToAddModuleFromFolder (path.file, list) || path.depth == 3)
  125. continue;
  126. for (const auto& iter : RangedDirectoryIterator (path.file, false, "*", File::findDirectories))
  127. {
  128. if (auto* job = ThreadPoolJob::getCurrentThreadPoolJob())
  129. if (job->shouldExit())
  130. return;
  131. pathsToCheck.push({ iter.getFile(), path.depth + 1 });
  132. }
  133. }
  134. }
  135. Array<File> pathsToScan;
  136. std::function<void (const ModuleIDAndFolderList&)> completionCallback;
  137. };
  138. //==============================================================================
  139. void handleAsyncUpdate() override
  140. {
  141. listeners.call ([this] (Listener& l) { l.availableModulesChanged (this); });
  142. }
  143. std::unique_ptr<ThreadPoolJob> createScannerJob (const Array<File>& paths)
  144. {
  145. return std::make_unique<ModuleScannerJob> (paths, [this] (ModuleIDAndFolderList scannedModulesList)
  146. {
  147. if (scannedModulesList == modulesList)
  148. return;
  149. {
  150. const ScopedLock swapLock (lock);
  151. modulesList.swap (scannedModulesList);
  152. }
  153. triggerAsyncUpdate();
  154. });
  155. }
  156. void removePendingAndAddJob (std::unique_ptr<ThreadPoolJob> jobToAdd)
  157. {
  158. scanPool.removeAllJobs (false, 100);
  159. scanPool.addJob (jobToAdd.release(), true);
  160. }
  161. //==============================================================================
  162. ThreadPool scanPool { 1 };
  163. ModuleIDAndFolderList modulesList;
  164. ListenerList<Listener> listeners;
  165. CriticalSection lock;
  166. //==============================================================================
  167. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (AvailableModulesList)
  168. };