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.

201 lines
6.6KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 6 technical preview.
  4. Copyright (c) 2020 - 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 this technical preview, this file is not subject to commercial licensing.
  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. //==============================================================================
  16. class AvailableModulesList : private AsyncUpdater
  17. {
  18. public:
  19. using ModuleIDAndFolder = std::pair<String, File>;
  20. using ModuleIDAndFolderList = std::vector<ModuleIDAndFolder>;
  21. AvailableModulesList() = default;
  22. //==============================================================================
  23. void scanPaths (const Array<File>& paths)
  24. {
  25. auto job = createScannerJob (paths);
  26. auto& ref = *job;
  27. removePendingAndAddJob (std::move (job));
  28. scanPool.waitForJobToFinish (&ref, -1);
  29. }
  30. void scanPathsAsync (const Array<File>& paths)
  31. {
  32. removePendingAndAddJob (createScannerJob (paths));
  33. }
  34. //==============================================================================
  35. ModuleIDAndFolderList getAllModules() const
  36. {
  37. const ScopedLock readLock (lock);
  38. return modulesList;
  39. }
  40. ModuleIDAndFolder getModuleWithID (const String& id) const
  41. {
  42. const ScopedLock readLock (lock);
  43. for (auto& mod : modulesList)
  44. if (mod.first == id)
  45. return mod;
  46. return {};
  47. }
  48. //==============================================================================
  49. void removeDuplicates (const ModuleIDAndFolderList& other)
  50. {
  51. const ScopedLock readLock (lock);
  52. const auto predicate = [&] (const ModuleIDAndFolder& entry)
  53. {
  54. return std::find (other.begin(), other.end(), entry) != other.end();
  55. };
  56. modulesList.erase (std::remove_if (modulesList.begin(), modulesList.end(), predicate),
  57. modulesList.end());
  58. }
  59. //==============================================================================
  60. struct Listener
  61. {
  62. virtual ~Listener() = default;
  63. virtual void availableModulesChanged (AvailableModulesList* listThatHasChanged) = 0;
  64. };
  65. void addListener (Listener* listenerToAdd) { listeners.add (listenerToAdd); }
  66. void removeListener (Listener* listenerToRemove) { listeners.remove (listenerToRemove); }
  67. private:
  68. //==============================================================================
  69. struct ModuleScannerJob : public ThreadPoolJob
  70. {
  71. ModuleScannerJob (const Array<File>& paths,
  72. std::function<void (const ModuleIDAndFolderList&)>&& callback)
  73. : ThreadPoolJob ("ModuleScannerJob"),
  74. pathsToScan (paths),
  75. completionCallback (std::move (callback))
  76. {
  77. }
  78. JobStatus runJob() override
  79. {
  80. ModuleIDAndFolderList list;
  81. for (auto& p : pathsToScan)
  82. addAllModulesInFolder (p, list);
  83. if (! shouldExit())
  84. {
  85. std::sort (list.begin(), list.end(), [] (const ModuleIDAndFolder& m1,
  86. const ModuleIDAndFolder& m2)
  87. {
  88. return m1.first.compareIgnoreCase (m2.first) < 0;
  89. });
  90. completionCallback (list);
  91. }
  92. return jobHasFinished;
  93. }
  94. static bool tryToAddModuleFromFolder (const File& path, ModuleIDAndFolderList& list)
  95. {
  96. ModuleDescription m (path);
  97. if (m.isValid())
  98. {
  99. list.push_back ({ m.getID(), path });
  100. return true;
  101. }
  102. return false;
  103. }
  104. static void addAllModulesInSubfoldersRecursively (const File& path, int depth, ModuleIDAndFolderList& list)
  105. {
  106. if (depth > 0)
  107. {
  108. for (const auto& iter : RangedDirectoryIterator (path, false, "*", File::findDirectories))
  109. {
  110. if (auto* job = ThreadPoolJob::getCurrentThreadPoolJob())
  111. if (job->shouldExit())
  112. return;
  113. auto childPath = iter.getFile();
  114. if (! tryToAddModuleFromFolder (childPath, list))
  115. addAllModulesInSubfoldersRecursively (childPath, depth - 1, list);
  116. }
  117. }
  118. }
  119. static void addAllModulesInFolder (const File& path, ModuleIDAndFolderList& list)
  120. {
  121. if (! tryToAddModuleFromFolder (path, list))
  122. {
  123. constexpr int subfolders = 3;
  124. addAllModulesInSubfoldersRecursively (path, subfolders, list);
  125. }
  126. }
  127. Array<File> pathsToScan;
  128. std::function<void (const ModuleIDAndFolderList&)> completionCallback;
  129. };
  130. //==============================================================================
  131. void handleAsyncUpdate() override
  132. {
  133. listeners.call ([this] (Listener& l) { l.availableModulesChanged (this); });
  134. }
  135. std::unique_ptr<ThreadPoolJob> createScannerJob (const Array<File>& paths)
  136. {
  137. return std::make_unique<ModuleScannerJob> (paths, [this] (ModuleIDAndFolderList scannedModulesList)
  138. {
  139. {
  140. const ScopedLock swapLock (lock);
  141. modulesList.swap (scannedModulesList);
  142. }
  143. triggerAsyncUpdate();
  144. });
  145. }
  146. void removePendingAndAddJob (std::unique_ptr<ThreadPoolJob> jobToAdd)
  147. {
  148. scanPool.removeAllJobs (false, 100);
  149. scanPool.addJob (jobToAdd.release(), true);
  150. }
  151. //==============================================================================
  152. ThreadPool scanPool { 1 };
  153. ModuleIDAndFolderList modulesList;
  154. ListenerList<Listener> listeners;
  155. CriticalSection lock;
  156. //==============================================================================
  157. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (AvailableModulesList)
  158. };