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.

542 lines
17KB

  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. #include "../Application/jucer_Headers.h"
  14. #include "jucer_OpenDocumentManager.h"
  15. #include "../CodeEditor/jucer_ItemPreviewComponent.h"
  16. #include "../Application/jucer_Application.h"
  17. //==============================================================================
  18. class UnknownDocument : public OpenDocumentManager::Document
  19. {
  20. public:
  21. UnknownDocument (Project* p, const File& f)
  22. : project (p), file (f)
  23. {
  24. reloadFromFile();
  25. }
  26. //==============================================================================
  27. struct Type : public OpenDocumentManager::DocumentType
  28. {
  29. bool canOpenFile (const File&) override { return true; }
  30. Document* openFile (Project* p, const File& f) override { return new UnknownDocument (p, f); }
  31. };
  32. //==============================================================================
  33. bool loadedOk() const override { return true; }
  34. bool isForFile (const File& f) const override { return file == f; }
  35. bool isForNode (const ValueTree&) const override { return false; }
  36. bool refersToProject (Project& p) const override { return project == &p; }
  37. Project* getProject() const override { return project; }
  38. bool needsSaving() const override { return false; }
  39. bool saveSyncWithoutAsking() override { return true; }
  40. void saveAsync (std::function<void (bool)>) override {}
  41. void saveAsAsync (std::function<void (bool)>) override {}
  42. bool hasFileBeenModifiedExternally() override { return fileModificationTime != file.getLastModificationTime(); }
  43. void reloadFromFile() override { fileModificationTime = file.getLastModificationTime(); }
  44. String getName() const override { return file.getFileName(); }
  45. File getFile() const override { return file; }
  46. std::unique_ptr<Component> createEditor() override { return std::make_unique<ItemPreviewComponent> (file); }
  47. std::unique_ptr<Component> createViewer() override { return createEditor(); }
  48. void fileHasBeenRenamed (const File& newFile) override { file = newFile; }
  49. String getState() const override { return {}; }
  50. void restoreState (const String&) override {}
  51. String getType() const override
  52. {
  53. if (file.getFileExtension().isNotEmpty())
  54. return file.getFileExtension() + " file";
  55. jassertfalse;
  56. return "Unknown";
  57. }
  58. private:
  59. Project* const project;
  60. File file;
  61. Time fileModificationTime;
  62. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (UnknownDocument)
  63. };
  64. //==============================================================================
  65. OpenDocumentManager::DocumentType* createGUIDocumentType();
  66. OpenDocumentManager::OpenDocumentManager()
  67. {
  68. registerType (new UnknownDocument::Type());
  69. registerType (new SourceCodeDocument::Type());
  70. registerType (createGUIDocumentType());
  71. }
  72. OpenDocumentManager::~OpenDocumentManager()
  73. {
  74. }
  75. void OpenDocumentManager::clear()
  76. {
  77. documents.clear();
  78. types.clear();
  79. }
  80. //==============================================================================
  81. void OpenDocumentManager::registerType (DocumentType* type, int index)
  82. {
  83. types.insert (index, type);
  84. }
  85. //==============================================================================
  86. void OpenDocumentManager::addListener (DocumentCloseListener* listener)
  87. {
  88. listeners.addIfNotAlreadyThere (listener);
  89. }
  90. void OpenDocumentManager::removeListener (DocumentCloseListener* listener)
  91. {
  92. listeners.removeFirstMatchingValue (listener);
  93. }
  94. //==============================================================================
  95. bool OpenDocumentManager::canOpenFile (const File& file)
  96. {
  97. for (int i = types.size(); --i >= 0;)
  98. if (types.getUnchecked(i)->canOpenFile (file))
  99. return true;
  100. return false;
  101. }
  102. OpenDocumentManager::Document* OpenDocumentManager::openFile (Project* project, const File& file)
  103. {
  104. for (int i = documents.size(); --i >= 0;)
  105. if (documents.getUnchecked(i)->isForFile (file))
  106. return documents.getUnchecked(i);
  107. Document* d = nullptr;
  108. for (int i = types.size(); --i >= 0 && d == nullptr;)
  109. {
  110. if (types.getUnchecked(i)->canOpenFile (file))
  111. {
  112. d = types.getUnchecked(i)->openFile (project, file);
  113. jassert (d != nullptr);
  114. }
  115. }
  116. jassert (d != nullptr); // should always at least have been picked up by UnknownDocument
  117. documents.add (d);
  118. ProjucerApplication::getCommandManager().commandStatusChanged();
  119. return d;
  120. }
  121. int OpenDocumentManager::getNumOpenDocuments() const
  122. {
  123. return documents.size();
  124. }
  125. OpenDocumentManager::Document* OpenDocumentManager::getOpenDocument (int index) const
  126. {
  127. return documents.getUnchecked (index);
  128. }
  129. void OpenDocumentManager::saveIfNeededAndUserAgrees (OpenDocumentManager::Document* doc,
  130. std::function<void (FileBasedDocument::SaveResult)> callback)
  131. {
  132. if (! doc->needsSaving())
  133. {
  134. if (callback != nullptr)
  135. callback (FileBasedDocument::savedOk);
  136. return;
  137. }
  138. AlertWindow::showYesNoCancelBox (MessageBoxIconType::QuestionIcon,
  139. TRANS("Closing document..."),
  140. TRANS("Do you want to save the changes to \"")
  141. + doc->getName() + "\"?",
  142. TRANS("Save"),
  143. TRANS("Discard changes"),
  144. TRANS("Cancel"),
  145. nullptr,
  146. ModalCallbackFunction::create ([parent = WeakReference<OpenDocumentManager> { this }, doc, callback] (int r)
  147. {
  148. if (parent == nullptr)
  149. return;
  150. if (r == 1)
  151. {
  152. doc->saveAsync ([parent, callback] (bool hasSaved)
  153. {
  154. if (parent == nullptr)
  155. return;
  156. if (callback != nullptr)
  157. callback (hasSaved ? FileBasedDocument::savedOk : FileBasedDocument::failedToWriteToFile);
  158. });
  159. return;
  160. }
  161. if (callback != nullptr)
  162. callback (r == 2 ? FileBasedDocument::savedOk : FileBasedDocument::userCancelledSave);
  163. }));
  164. }
  165. bool OpenDocumentManager::closeDocumentWithoutSaving (Document* doc)
  166. {
  167. if (documents.contains (doc))
  168. {
  169. bool canClose = true;
  170. for (int i = listeners.size(); --i >= 0;)
  171. if (auto* l = listeners[i])
  172. if (! l->documentAboutToClose (doc))
  173. canClose = false;
  174. if (! canClose)
  175. return false;
  176. documents.removeObject (doc);
  177. ProjucerApplication::getCommandManager().commandStatusChanged();
  178. }
  179. return true;
  180. }
  181. void OpenDocumentManager::closeDocumentAsync (Document* doc, SaveIfNeeded saveIfNeeded, std::function<void (bool)> callback)
  182. {
  183. if (! documents.contains (doc))
  184. {
  185. if (callback != nullptr)
  186. callback (true);
  187. return;
  188. }
  189. if (saveIfNeeded == SaveIfNeeded::yes)
  190. {
  191. saveIfNeededAndUserAgrees (doc,
  192. [parent = WeakReference<OpenDocumentManager> { this }, doc, callback] (FileBasedDocument::SaveResult result)
  193. {
  194. if (parent == nullptr)
  195. return;
  196. if (result != FileBasedDocument::savedOk)
  197. {
  198. if (callback != nullptr)
  199. callback (false);
  200. return;
  201. }
  202. auto closed = parent->closeDocumentWithoutSaving (doc);
  203. if (callback != nullptr)
  204. callback (closed);
  205. });
  206. return;
  207. }
  208. auto closed = closeDocumentWithoutSaving (doc);
  209. if (callback != nullptr)
  210. callback (closed);
  211. }
  212. void OpenDocumentManager::closeFileWithoutSaving (const File& f)
  213. {
  214. for (int i = documents.size(); --i >= 0;)
  215. if (auto* d = documents[i])
  216. if (d->isForFile (f))
  217. closeDocumentWithoutSaving (d);
  218. }
  219. static void closeLastAsyncRecusrsive (WeakReference<OpenDocumentManager> parent,
  220. OpenDocumentManager::SaveIfNeeded askUserToSave,
  221. std::function<void (bool)> callback)
  222. {
  223. auto lastIndex = parent->getNumOpenDocuments() - 1;
  224. if (lastIndex < 0)
  225. {
  226. if (callback != nullptr)
  227. callback (true);
  228. return;
  229. }
  230. parent->closeDocumentAsync (parent->getOpenDocument (lastIndex),
  231. askUserToSave,
  232. [parent, askUserToSave, callback] (bool closedSuccessfully)
  233. {
  234. if (parent == nullptr)
  235. return;
  236. if (! closedSuccessfully)
  237. {
  238. if (callback != nullptr)
  239. callback (false);
  240. return;
  241. }
  242. closeLastAsyncRecusrsive (parent, askUserToSave, std::move (callback));
  243. });
  244. }
  245. void OpenDocumentManager::closeAllAsync (SaveIfNeeded askUserToSave, std::function<void (bool)> callback)
  246. {
  247. closeLastAsyncRecusrsive (this, askUserToSave, std::move (callback));
  248. }
  249. void OpenDocumentManager::closeLastDocumentUsingProjectRecursive (WeakReference<OpenDocumentManager> parent,
  250. Project* project,
  251. SaveIfNeeded askUserToSave,
  252. std::function<void (bool)> callback)
  253. {
  254. for (int i = documents.size(); --i >= 0;)
  255. {
  256. if (auto* d = documents[i])
  257. {
  258. if (d->getProject() == project)
  259. {
  260. closeDocumentAsync (d, askUserToSave, [parent, project, askUserToSave, callback] (bool closedSuccessfully)
  261. {
  262. if (parent == nullptr)
  263. return;
  264. if (! closedSuccessfully)
  265. {
  266. if (callback != nullptr)
  267. callback (false);
  268. return;
  269. }
  270. parent->closeLastDocumentUsingProjectRecursive (parent, project, askUserToSave, std::move (callback));
  271. });
  272. return;
  273. }
  274. }
  275. }
  276. if (callback != nullptr)
  277. callback (true);
  278. }
  279. void OpenDocumentManager::closeAllDocumentsUsingProjectAsync (Project& project, SaveIfNeeded askUserToSave, std::function<void (bool)> callback)
  280. {
  281. WeakReference<OpenDocumentManager> parent { this };
  282. closeLastDocumentUsingProjectRecursive (parent, &project, askUserToSave, std::move (callback));
  283. }
  284. void OpenDocumentManager::closeAllDocumentsUsingProjectWithoutSaving (Project& project)
  285. {
  286. for (int i = documents.size(); --i >= 0;)
  287. if (Document* d = documents[i])
  288. if (d->refersToProject (project))
  289. closeDocumentWithoutSaving (d);
  290. }
  291. bool OpenDocumentManager::anyFilesNeedSaving() const
  292. {
  293. for (int i = documents.size(); --i >= 0;)
  294. if (documents.getUnchecked (i)->needsSaving())
  295. return true;
  296. return false;
  297. }
  298. void OpenDocumentManager::saveAllSyncWithoutAsking()
  299. {
  300. for (int i = documents.size(); --i >= 0;)
  301. {
  302. if (documents.getUnchecked (i)->saveSyncWithoutAsking())
  303. ProjucerApplication::getCommandManager().commandStatusChanged();
  304. }
  305. }
  306. void OpenDocumentManager::reloadModifiedFiles()
  307. {
  308. for (int i = documents.size(); --i >= 0;)
  309. {
  310. Document* d = documents.getUnchecked (i);
  311. if (d->hasFileBeenModifiedExternally())
  312. d->reloadFromFile();
  313. }
  314. }
  315. void OpenDocumentManager::fileHasBeenRenamed (const File& oldFile, const File& newFile)
  316. {
  317. for (int i = documents.size(); --i >= 0;)
  318. {
  319. Document* d = documents.getUnchecked (i);
  320. if (d->isForFile (oldFile))
  321. d->fileHasBeenRenamed (newFile);
  322. }
  323. }
  324. //==============================================================================
  325. RecentDocumentList::RecentDocumentList()
  326. {
  327. ProjucerApplication::getApp().openDocumentManager.addListener (this);
  328. }
  329. RecentDocumentList::~RecentDocumentList()
  330. {
  331. ProjucerApplication::getApp().openDocumentManager.removeListener (this);
  332. }
  333. void RecentDocumentList::clear()
  334. {
  335. previousDocs.clear();
  336. nextDocs.clear();
  337. }
  338. void RecentDocumentList::newDocumentOpened (OpenDocumentManager::Document* document)
  339. {
  340. if (document != nullptr && document != getCurrentDocument())
  341. {
  342. nextDocs.clear();
  343. previousDocs.add (document);
  344. }
  345. }
  346. bool RecentDocumentList::canGoToPrevious() const
  347. {
  348. return previousDocs.size() > 1;
  349. }
  350. bool RecentDocumentList::canGoToNext() const
  351. {
  352. return nextDocs.size() > 0;
  353. }
  354. OpenDocumentManager::Document* RecentDocumentList::getPrevious()
  355. {
  356. if (! canGoToPrevious())
  357. return nullptr;
  358. nextDocs.insert (0, previousDocs.removeAndReturn (previousDocs.size() - 1));
  359. return previousDocs.getLast();
  360. }
  361. OpenDocumentManager::Document* RecentDocumentList::getNext()
  362. {
  363. if (! canGoToNext())
  364. return nullptr;
  365. OpenDocumentManager::Document* d = nextDocs.removeAndReturn (0);
  366. previousDocs.add (d);
  367. return d;
  368. }
  369. bool RecentDocumentList::contains (const File& f) const
  370. {
  371. for (int i = previousDocs.size(); --i >= 0;)
  372. if (previousDocs.getUnchecked(i)->getFile() == f)
  373. return true;
  374. return false;
  375. }
  376. OpenDocumentManager::Document* RecentDocumentList::getClosestPreviousDocOtherThan (OpenDocumentManager::Document* oneToAvoid) const
  377. {
  378. for (int i = previousDocs.size(); --i >= 0;)
  379. if (previousDocs.getUnchecked(i) != oneToAvoid)
  380. return previousDocs.getUnchecked(i);
  381. return nullptr;
  382. }
  383. bool RecentDocumentList::documentAboutToClose (OpenDocumentManager::Document* document)
  384. {
  385. previousDocs.removeAllInstancesOf (document);
  386. nextDocs.removeAllInstancesOf (document);
  387. jassert (! previousDocs.contains (document));
  388. jassert (! nextDocs.contains (document));
  389. return true;
  390. }
  391. static void restoreDocList (Project& project, Array <OpenDocumentManager::Document*>& list, const XmlElement* xml)
  392. {
  393. if (xml != nullptr)
  394. {
  395. OpenDocumentManager& odm = ProjucerApplication::getApp().openDocumentManager;
  396. for (auto* e : xml->getChildWithTagNameIterator ("DOC"))
  397. {
  398. const File file (e->getStringAttribute ("file"));
  399. if (file.exists())
  400. {
  401. if (OpenDocumentManager::Document* doc = odm.openFile (&project, file))
  402. {
  403. doc->restoreState (e->getStringAttribute ("state"));
  404. list.add (doc);
  405. }
  406. }
  407. }
  408. }
  409. }
  410. void RecentDocumentList::restoreFromXML (Project& project, const XmlElement& xml)
  411. {
  412. clear();
  413. if (xml.hasTagName ("RECENT_DOCUMENTS"))
  414. {
  415. restoreDocList (project, previousDocs, xml.getChildByName ("PREVIOUS"));
  416. restoreDocList (project, nextDocs, xml.getChildByName ("NEXT"));
  417. }
  418. }
  419. static void saveDocList (const Array <OpenDocumentManager::Document*>& list, XmlElement& xml)
  420. {
  421. for (int i = 0; i < list.size(); ++i)
  422. {
  423. const OpenDocumentManager::Document& doc = *list.getUnchecked(i);
  424. XmlElement* e = xml.createNewChildElement ("DOC");
  425. e->setAttribute ("file", doc.getFile().getFullPathName());
  426. e->setAttribute ("state", doc.getState());
  427. }
  428. }
  429. std::unique_ptr<XmlElement> RecentDocumentList::createXML() const
  430. {
  431. auto xml = std::make_unique<XmlElement> ("RECENT_DOCUMENTS");
  432. saveDocList (previousDocs, *xml->createNewChildElement ("PREVIOUS"));
  433. saveDocList (nextDocs, *xml->createNewChildElement ("NEXT"));
  434. return xml;
  435. }