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.

208 lines
6.9KB

  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. {
  104. list.push_back ({ m.getID(), path });
  105. return true;
  106. }
  107. return false;
  108. }
  109. static void addAllModulesInSubfoldersRecursively (const File& path, int depth, ModuleIDAndFolderList& list)
  110. {
  111. if (depth > 0)
  112. {
  113. for (const auto& iter : RangedDirectoryIterator (path, false, "*", File::findDirectories))
  114. {
  115. if (auto* job = ThreadPoolJob::getCurrentThreadPoolJob())
  116. if (job->shouldExit())
  117. return;
  118. auto childPath = iter.getFile();
  119. if (! tryToAddModuleFromFolder (childPath, list))
  120. addAllModulesInSubfoldersRecursively (childPath, depth - 1, list);
  121. }
  122. }
  123. }
  124. static void addAllModulesInFolder (const File& path, ModuleIDAndFolderList& list)
  125. {
  126. if (! tryToAddModuleFromFolder (path, list))
  127. {
  128. constexpr int subfolders = 3;
  129. addAllModulesInSubfoldersRecursively (path, subfolders, list);
  130. }
  131. }
  132. Array<File> pathsToScan;
  133. std::function<void (const ModuleIDAndFolderList&)> completionCallback;
  134. };
  135. //==============================================================================
  136. void handleAsyncUpdate() override
  137. {
  138. listeners.call ([this] (Listener& l) { l.availableModulesChanged (this); });
  139. }
  140. std::unique_ptr<ThreadPoolJob> createScannerJob (const Array<File>& paths)
  141. {
  142. return std::make_unique<ModuleScannerJob> (paths, [this] (ModuleIDAndFolderList scannedModulesList)
  143. {
  144. {
  145. const ScopedLock swapLock (lock);
  146. modulesList.swap (scannedModulesList);
  147. }
  148. triggerAsyncUpdate();
  149. });
  150. }
  151. void removePendingAndAddJob (std::unique_ptr<ThreadPoolJob> jobToAdd)
  152. {
  153. scanPool.removeAllJobs (false, 100);
  154. scanPool.addJob (jobToAdd.release(), true);
  155. }
  156. //==============================================================================
  157. ThreadPool scanPool { 1 };
  158. ModuleIDAndFolderList modulesList;
  159. ListenerList<Listener> listeners;
  160. CriticalSection lock;
  161. //==============================================================================
  162. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (AvailableModulesList)
  163. };