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.

464 lines
16KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. class ModulesPanel : public Component,
  18. private TableListBoxModel,
  19. private ValueTree::Listener,
  20. private Button::Listener
  21. {
  22. public:
  23. ModulesPanel (Project& p)
  24. : project (p),
  25. modulesValueTree (p.getModules().state),
  26. addWebModuleButton ("Download and add a module..."),
  27. updateModuleButton ("Install updates to modules...")
  28. {
  29. table.getHeader().addColumn ("Module", nameCol, 180, 100, 400, TableHeaderComponent::notSortable);
  30. table.getHeader().addColumn ("Installed Version", versionCol, 100, 100, 100, TableHeaderComponent::notSortable);
  31. table.getHeader().addColumn ("Available Version", updateCol, 100, 100, 100, TableHeaderComponent::notSortable);
  32. table.getHeader().addColumn ("Make Local Copy", copyCol, 100, 100, 100, TableHeaderComponent::notSortable);
  33. table.getHeader().addColumn ("Paths", pathCol, 250, 100, 600, TableHeaderComponent::notSortable);
  34. table.setModel (this);
  35. table.setColour (TableListBox::backgroundColourId, Colours::transparentBlack);
  36. addAndMakeVisible (&table);
  37. table.updateContent();
  38. table.setRowHeight (20);
  39. addAndMakeVisible (&addWebModuleButton);
  40. addAndMakeVisible (&updateModuleButton);
  41. addWebModuleButton.addListener (this);
  42. updateModuleButton.addListener (this);
  43. updateModuleButton.setEnabled (false);
  44. modulesValueTree.addListener (this);
  45. lookAndFeelChanged();
  46. }
  47. void paint (Graphics& g) override
  48. {
  49. if (webUpdateThread == nullptr)
  50. webUpdateThread = new WebsiteUpdateFetchThread (*this);
  51. IntrojucerLookAndFeel::fillWithBackgroundTexture (*this, g);
  52. }
  53. void resized() override
  54. {
  55. Rectangle<int> r (getLocalBounds().reduced (5, 4));
  56. table.setBounds (r.removeFromTop (table.getRowPosition (getNumRows() - 1, true).getBottom() + 20));
  57. Rectangle<int> buttonRow (r.removeFromTop (32).removeFromBottom (28));
  58. addWebModuleButton.setBounds (buttonRow.removeFromLeft (jmin (260, r.getWidth() / 2)));
  59. buttonRow.removeFromLeft (8);
  60. updateModuleButton.setBounds (buttonRow.removeFromLeft (jmin (260, r.getWidth() / 2)));
  61. }
  62. int getNumRows() override
  63. {
  64. return project.getModules().getNumModules();
  65. }
  66. void paintRowBackground (Graphics& g, int /*rowNumber*/, int width, int height, bool rowIsSelected) override
  67. {
  68. g.setColour (rowIsSelected ? Colours::lightblue.withAlpha (0.4f)
  69. : Colours::white.withAlpha (0.4f));
  70. g.fillRect (0, 0, width, height - 1);
  71. }
  72. void paintCell (Graphics& g, int rowNumber, int columnId, int width, int height, bool rowIsSelected) override
  73. {
  74. String text;
  75. const String moduleID (project.getModules().getModuleID (rowNumber));
  76. if (columnId == nameCol)
  77. {
  78. text = moduleID;
  79. }
  80. else if (columnId == versionCol)
  81. {
  82. text = project.getModules().getModuleInfo (moduleID).getVersion();
  83. if (text.isEmpty())
  84. text = "?";
  85. }
  86. else if (columnId == updateCol)
  87. {
  88. if (listFromWebsite != nullptr)
  89. {
  90. if (const ModuleDescription* m = listFromWebsite->getModuleWithID (moduleID))
  91. {
  92. if (m->getVersion() != project.getModules().getModuleInfo (moduleID).getVersion())
  93. text = m->getVersion() + " available";
  94. else
  95. text = "Up-to-date";
  96. }
  97. else
  98. text = "?";
  99. }
  100. else
  101. {
  102. text = "-";
  103. }
  104. }
  105. else if (columnId == copyCol)
  106. {
  107. text = project.getModules().shouldCopyModuleFilesLocally (moduleID).getValue()
  108. ? "Yes" : "No";
  109. }
  110. else if (columnId == pathCol)
  111. {
  112. StringArray paths;
  113. for (Project::ExporterIterator exporter (project); exporter.next();)
  114. paths.addIfNotAlreadyThere (exporter->getPathForModuleString (moduleID).trim());
  115. text = paths.joinIntoString (", ");
  116. }
  117. g.setColour (Colours::black);
  118. g.setFont (height * 0.65f);
  119. g.drawText (text, Rectangle<int> (width, height).reduced (4, 0), Justification::centredLeft, true);
  120. }
  121. void cellDoubleClicked (int rowNumber, int, const MouseEvent&) override
  122. {
  123. const String moduleID (project.getModules().getModuleID (rowNumber));
  124. if (moduleID.isNotEmpty())
  125. if (ProjectContentComponent* pcc = findParentComponentOfClass<ProjectContentComponent>())
  126. pcc->showModule (moduleID);
  127. }
  128. void deleteKeyPressed (int row) override
  129. {
  130. project.getModules().removeModule (project.getModules().getModuleID (row));
  131. }
  132. void webUpdateFinished (const ModuleList& newList)
  133. {
  134. listFromWebsite = new ModuleList (newList);
  135. webUpdateThread = nullptr;
  136. table.updateContent();
  137. table.repaint();
  138. updateModuleButton.setEnabled (getUpdatableModules().size() != 0);
  139. }
  140. void buttonClicked (Button* b)
  141. {
  142. if (b == &addWebModuleButton)
  143. showAddModuleMenu();
  144. else if (b == &updateModuleButton)
  145. showUpdateModulesMenu();
  146. }
  147. private:
  148. enum
  149. {
  150. nameCol = 1,
  151. versionCol,
  152. updateCol,
  153. copyCol,
  154. pathCol
  155. };
  156. Project& project;
  157. ValueTree modulesValueTree;
  158. TableListBox table;
  159. TextButton addWebModuleButton, updateModuleButton;
  160. ScopedPointer<ModuleList> listFromWebsite;
  161. void valueTreePropertyChanged (ValueTree&, const Identifier&) override { itemChanged(); }
  162. void valueTreeChildAdded (ValueTree&, ValueTree&) override { itemChanged(); }
  163. void valueTreeChildRemoved (ValueTree&, ValueTree&) override { itemChanged(); }
  164. void valueTreeChildOrderChanged (ValueTree&) override { itemChanged(); }
  165. void valueTreeParentChanged (ValueTree&) override { itemChanged(); }
  166. void itemChanged()
  167. {
  168. table.updateContent();
  169. resized();
  170. repaint();
  171. }
  172. StringArray getUpdatableModules() const
  173. {
  174. StringArray result;
  175. if (listFromWebsite != nullptr)
  176. {
  177. for (int i = 0; i < listFromWebsite->modules.size(); ++i)
  178. {
  179. const ModuleDescription* m = listFromWebsite->modules.getUnchecked(i);
  180. const String v1 (m->getVersion());
  181. const String v2 (project.getModules().getModuleInfo (m->getID()).getVersion());
  182. if (v1 != v2 && v1.isNotEmpty() && v2.isNotEmpty())
  183. result.add (m->getID());
  184. }
  185. }
  186. return result;
  187. }
  188. StringArray getAddableModules() const
  189. {
  190. StringArray result;
  191. if (listFromWebsite != nullptr)
  192. {
  193. for (int i = 0; i < listFromWebsite->modules.size(); ++i)
  194. {
  195. const ModuleDescription* m = listFromWebsite->modules.getUnchecked(i);
  196. if (! project.getModules().isModuleEnabled (m->getID()))
  197. result.add (m->getID());
  198. }
  199. }
  200. return result;
  201. }
  202. void showUpdateModulesMenu()
  203. {
  204. StringArray mods (getUpdatableModules());
  205. PopupMenu m;
  206. m.addItem (1000, "Update all modules");
  207. m.addSeparator();
  208. for (int i = 0; i < mods.size(); ++i)
  209. m.addItem (1 + i, "Update " + mods[i]);
  210. int res = m.showAt (&updateModuleButton);
  211. if (res > 0 && listFromWebsite != nullptr)
  212. {
  213. if (res != 1000)
  214. mods = StringArray (mods[res - 1]);
  215. Array<ModuleDescription> modsToUpdate;
  216. for (int i = 0; i < mods.size(); ++i)
  217. {
  218. if (const ModuleDescription* md = listFromWebsite->getModuleWithID (mods[i]))
  219. {
  220. ModuleDescription modToUpdate (*md);
  221. modToUpdate.manifestFile = project.getModules().getModuleInfo (modToUpdate.getID()).manifestFile;
  222. modsToUpdate.add (modToUpdate);
  223. }
  224. }
  225. DownloadAndInstallThread::updateModulesFromWeb (project, modsToUpdate);
  226. }
  227. }
  228. void showAddModuleMenu()
  229. {
  230. if (listFromWebsite == nullptr)
  231. {
  232. AlertWindow::showMessageBoxAsync (AlertWindow::WarningIcon,
  233. "Couldn't contact the website!",
  234. "Failed to get the latest module list from juce.com - "
  235. "maybe network or server problems - try again soon!");
  236. return;
  237. }
  238. StringArray mods (getAddableModules());
  239. if (mods.size() == 0)
  240. {
  241. AlertWindow::showMessageBoxAsync (AlertWindow::WarningIcon,
  242. "No modules to add!",
  243. "Couldn't find any new modules that aren't already in your project!");
  244. return;
  245. }
  246. PopupMenu m;
  247. for (int i = 0; i < mods.size(); ++i)
  248. m.addItem (i + 1, "Install " + mods[i]);
  249. int res = m.showAt (&addWebModuleButton);
  250. if (res > 0 && listFromWebsite != nullptr)
  251. if (const ModuleDescription* md = listFromWebsite->getModuleWithID (mods[res - 1]))
  252. DownloadAndInstallThread::addModuleFromWebsite (project, *md);
  253. }
  254. struct WebsiteUpdateFetchThread : private Thread,
  255. private AsyncUpdater
  256. {
  257. WebsiteUpdateFetchThread (ModulesPanel& p) : Thread ("Web Updater"), panel (p)
  258. {
  259. startThread (3);
  260. }
  261. ~WebsiteUpdateFetchThread()
  262. {
  263. stopThread (15000);
  264. }
  265. void run() override
  266. {
  267. if (list.loadFromWebsite() && ! threadShouldExit())
  268. triggerAsyncUpdate();
  269. }
  270. void handleAsyncUpdate() override
  271. {
  272. panel.webUpdateFinished (list);
  273. }
  274. private:
  275. ModuleList list;
  276. ModulesPanel& panel;
  277. };
  278. ScopedPointer<WebsiteUpdateFetchThread> webUpdateThread;
  279. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ModulesPanel)
  280. };
  281. //==============================================================================
  282. class DownloadAndInstallThread : public ThreadWithProgressWindow
  283. {
  284. public:
  285. DownloadAndInstallThread (const Array<ModuleDescription>& modulesToInstall)
  286. : ThreadWithProgressWindow ("Installing New Modules", true, true),
  287. result (Result::ok()),
  288. modules (modulesToInstall)
  289. {
  290. }
  291. static void updateModulesFromWeb (Project& project, const Array<ModuleDescription>& mods)
  292. {
  293. DownloadAndInstallThread d (mods);
  294. if (d.runThread())
  295. {
  296. if (d.result.failed())
  297. {
  298. AlertWindow::showMessageBoxAsync (AlertWindow::WarningIcon,
  299. "Module Install Failed",
  300. d.result.getErrorMessage());
  301. }
  302. else
  303. {
  304. for (int i = 0; i < d.modules.size(); ++i)
  305. project.getModules().addModule (d.modules.getReference(i).manifestFile,
  306. project.getModules().areMostModulesCopiedLocally());
  307. }
  308. }
  309. }
  310. static void addModuleFromWebsite (Project& project, const ModuleDescription& module)
  311. {
  312. Array<ModuleDescription> mods;
  313. mods.add (module);
  314. static File lastLocation (EnabledModuleList::findDefaultModulesFolder (project));
  315. FileChooser fc ("Select the parent folder for the new module...", lastLocation, String::empty, false);
  316. if (fc.browseForDirectory())
  317. {
  318. lastLocation = fc.getResult();
  319. if (lastLocation.getChildFile (ModuleDescription::getManifestFileName()).exists())
  320. {
  321. AlertWindow::showMessageBoxAsync (AlertWindow::WarningIcon,
  322. "Adding Module",
  323. "You chose a folder that appears to be a module.\n\n"
  324. "You need to select the *parent* folder inside which the new modules will be created.");
  325. return;
  326. }
  327. for (int i = 0; i < mods.size(); ++i)
  328. mods.getReference(i).manifestFile = lastLocation.getChildFile (mods.getReference(i).getID())
  329. .getChildFile (ModuleDescription::getManifestFileName());
  330. updateModulesFromWeb (project, mods);
  331. }
  332. }
  333. void run() override
  334. {
  335. for (int i = 0; i < modules.size(); ++i)
  336. {
  337. const ModuleDescription& m = modules.getReference(i);
  338. setProgress (i / (double) modules.size());
  339. MemoryBlock downloaded;
  340. result = download (m, downloaded);
  341. if (result.failed() || threadShouldExit())
  342. break;
  343. result = unzip (m, downloaded);
  344. if (result.failed() || threadShouldExit())
  345. break;
  346. }
  347. }
  348. Result download (const ModuleDescription& m, MemoryBlock& dest)
  349. {
  350. setStatusMessage ("Downloading " + m.getID() + "...");
  351. const ScopedPointer<InputStream> in (m.url.createInputStream (false, nullptr, nullptr, String::empty, 10000));
  352. if (in != nullptr && in->readIntoMemoryBlock (dest))
  353. return Result::ok();
  354. return Result::fail ("Failed to download from: " + m.url.toString (false));
  355. }
  356. Result unzip (const ModuleDescription& m, const MemoryBlock& data)
  357. {
  358. setStatusMessage ("Installing " + m.getID() + "...");
  359. MemoryInputStream input (data, false);
  360. ZipFile zip (input);
  361. if (zip.getNumEntries() == 0)
  362. return Result::fail ("The downloaded file wasn't a valid module file!");
  363. if (! m.getFolder().deleteRecursively())
  364. return Result::fail ("Couldn't delete the existing folder:\n" + m.getFolder().getFullPathName());
  365. return zip.uncompressTo (m.getFolder().getParentDirectory(), true);
  366. }
  367. Result result;
  368. Array<ModuleDescription> modules;
  369. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (DownloadAndInstallThread)
  370. };