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.

427 lines
13KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 6 technical preview.
  4. Copyright (c) 2017 - ROLI Ltd.
  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. #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 save() override { return true; }
  40. bool saveAs() override { return false; }
  41. bool hasFileBeenModifiedExternally() override { return fileModificationTime != file.getLastModificationTime(); }
  42. void reloadFromFile() override { fileModificationTime = file.getLastModificationTime(); }
  43. String getName() const override { return file.getFileName(); }
  44. File getFile() const override { return file; }
  45. Component* createEditor() override { return new ItemPreviewComponent (file); }
  46. Component* createViewer() override { return createEditor(); }
  47. void fileHasBeenRenamed (const File& newFile) override { file = newFile; }
  48. String getState() const override { return {}; }
  49. void restoreState (const String&) override {}
  50. String getType() const override
  51. {
  52. if (file.getFileExtension().isNotEmpty())
  53. return file.getFileExtension() + " file";
  54. jassertfalse;
  55. return "Unknown";
  56. }
  57. private:
  58. Project* const project;
  59. File file;
  60. Time fileModificationTime;
  61. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (UnknownDocument)
  62. };
  63. //==============================================================================
  64. OpenDocumentManager::DocumentType* createGUIDocumentType();
  65. OpenDocumentManager::OpenDocumentManager()
  66. {
  67. registerType (new UnknownDocument::Type());
  68. registerType (new SourceCodeDocument::Type());
  69. registerType (createGUIDocumentType());
  70. }
  71. OpenDocumentManager::~OpenDocumentManager()
  72. {
  73. }
  74. void OpenDocumentManager::clear()
  75. {
  76. documents.clear();
  77. types.clear();
  78. }
  79. //==============================================================================
  80. void OpenDocumentManager::registerType (DocumentType* type, int index)
  81. {
  82. types.insert (index, type);
  83. }
  84. //==============================================================================
  85. void OpenDocumentManager::addListener (DocumentCloseListener* listener)
  86. {
  87. listeners.addIfNotAlreadyThere (listener);
  88. }
  89. void OpenDocumentManager::removeListener (DocumentCloseListener* listener)
  90. {
  91. listeners.removeFirstMatchingValue (listener);
  92. }
  93. //==============================================================================
  94. bool OpenDocumentManager::canOpenFile (const File& file)
  95. {
  96. for (int i = types.size(); --i >= 0;)
  97. if (types.getUnchecked(i)->canOpenFile (file))
  98. return true;
  99. return false;
  100. }
  101. OpenDocumentManager::Document* OpenDocumentManager::openFile (Project* project, const File& file)
  102. {
  103. for (int i = documents.size(); --i >= 0;)
  104. if (documents.getUnchecked(i)->isForFile (file))
  105. return documents.getUnchecked(i);
  106. Document* d = nullptr;
  107. for (int i = types.size(); --i >= 0 && d == nullptr;)
  108. {
  109. if (types.getUnchecked(i)->canOpenFile (file))
  110. {
  111. d = types.getUnchecked(i)->openFile (project, file);
  112. jassert (d != nullptr);
  113. }
  114. }
  115. jassert (d != nullptr); // should always at least have been picked up by UnknownDocument
  116. documents.add (d);
  117. ProjucerApplication::getCommandManager().commandStatusChanged();
  118. return d;
  119. }
  120. int OpenDocumentManager::getNumOpenDocuments() const
  121. {
  122. return documents.size();
  123. }
  124. OpenDocumentManager::Document* OpenDocumentManager::getOpenDocument (int index) const
  125. {
  126. return documents.getUnchecked (index);
  127. }
  128. FileBasedDocument::SaveResult OpenDocumentManager::saveIfNeededAndUserAgrees (OpenDocumentManager::Document* doc)
  129. {
  130. if (! doc->needsSaving())
  131. return FileBasedDocument::savedOk;
  132. const int r = AlertWindow::showYesNoCancelBox (AlertWindow::QuestionIcon,
  133. TRANS("Closing document..."),
  134. TRANS("Do you want to save the changes to \"")
  135. + doc->getName() + "\"?",
  136. TRANS("Save"),
  137. TRANS("Discard changes"),
  138. TRANS("Cancel"));
  139. if (r == 1) // save changes
  140. return doc->save() ? FileBasedDocument::savedOk
  141. : FileBasedDocument::failedToWriteToFile;
  142. if (r == 2) // discard changes
  143. return FileBasedDocument::savedOk;
  144. return FileBasedDocument::userCancelledSave;
  145. }
  146. bool OpenDocumentManager::closeDocument (int index, bool saveIfNeeded)
  147. {
  148. if (Document* doc = documents [index])
  149. {
  150. if (saveIfNeeded)
  151. if (saveIfNeededAndUserAgrees (doc) != FileBasedDocument::savedOk)
  152. return false;
  153. bool canClose = true;
  154. for (int i = listeners.size(); --i >= 0;)
  155. if (DocumentCloseListener* l = listeners[i])
  156. if (! l->documentAboutToClose (doc))
  157. canClose = false;
  158. if (! canClose)
  159. return false;
  160. documents.remove (index);
  161. ProjucerApplication::getCommandManager().commandStatusChanged();
  162. }
  163. return true;
  164. }
  165. bool OpenDocumentManager::closeDocument (Document* document, bool saveIfNeeded)
  166. {
  167. return closeDocument (documents.indexOf (document), saveIfNeeded);
  168. }
  169. void OpenDocumentManager::closeFile (const File& f, bool saveIfNeeded)
  170. {
  171. for (int i = documents.size(); --i >= 0;)
  172. if (Document* d = documents[i])
  173. if (d->isForFile (f))
  174. closeDocument (i, saveIfNeeded);
  175. }
  176. bool OpenDocumentManager::closeAll (bool askUserToSave)
  177. {
  178. for (int i = getNumOpenDocuments(); --i >= 0;)
  179. if (! closeDocument (i, askUserToSave))
  180. return false;
  181. return true;
  182. }
  183. bool OpenDocumentManager::closeAllDocumentsUsingProject (Project& project, bool saveIfNeeded)
  184. {
  185. for (int i = documents.size(); --i >= 0;)
  186. if (Document* d = documents[i])
  187. if (d->refersToProject (project))
  188. if (! closeDocument (i, saveIfNeeded))
  189. return false;
  190. return true;
  191. }
  192. bool OpenDocumentManager::anyFilesNeedSaving() const
  193. {
  194. for (int i = documents.size(); --i >= 0;)
  195. if (documents.getUnchecked (i)->needsSaving())
  196. return true;
  197. return false;
  198. }
  199. bool OpenDocumentManager::saveAll()
  200. {
  201. for (int i = documents.size(); --i >= 0;)
  202. {
  203. if (! documents.getUnchecked (i)->save())
  204. return false;
  205. ProjucerApplication::getCommandManager().commandStatusChanged();
  206. }
  207. return true;
  208. }
  209. void OpenDocumentManager::reloadModifiedFiles()
  210. {
  211. for (int i = documents.size(); --i >= 0;)
  212. {
  213. Document* d = documents.getUnchecked (i);
  214. if (d->hasFileBeenModifiedExternally())
  215. d->reloadFromFile();
  216. }
  217. }
  218. void OpenDocumentManager::fileHasBeenRenamed (const File& oldFile, const File& newFile)
  219. {
  220. for (int i = documents.size(); --i >= 0;)
  221. {
  222. Document* d = documents.getUnchecked (i);
  223. if (d->isForFile (oldFile))
  224. d->fileHasBeenRenamed (newFile);
  225. }
  226. }
  227. //==============================================================================
  228. RecentDocumentList::RecentDocumentList()
  229. {
  230. ProjucerApplication::getApp().openDocumentManager.addListener (this);
  231. }
  232. RecentDocumentList::~RecentDocumentList()
  233. {
  234. ProjucerApplication::getApp().openDocumentManager.removeListener (this);
  235. }
  236. void RecentDocumentList::clear()
  237. {
  238. previousDocs.clear();
  239. nextDocs.clear();
  240. }
  241. void RecentDocumentList::newDocumentOpened (OpenDocumentManager::Document* document)
  242. {
  243. if (document != nullptr && document != getCurrentDocument())
  244. {
  245. nextDocs.clear();
  246. previousDocs.add (document);
  247. }
  248. }
  249. bool RecentDocumentList::canGoToPrevious() const
  250. {
  251. return previousDocs.size() > 1;
  252. }
  253. bool RecentDocumentList::canGoToNext() const
  254. {
  255. return nextDocs.size() > 0;
  256. }
  257. OpenDocumentManager::Document* RecentDocumentList::getPrevious()
  258. {
  259. if (! canGoToPrevious())
  260. return nullptr;
  261. nextDocs.insert (0, previousDocs.removeAndReturn (previousDocs.size() - 1));
  262. return previousDocs.getLast();
  263. }
  264. OpenDocumentManager::Document* RecentDocumentList::getNext()
  265. {
  266. if (! canGoToNext())
  267. return nullptr;
  268. OpenDocumentManager::Document* d = nextDocs.removeAndReturn (0);
  269. previousDocs.add (d);
  270. return d;
  271. }
  272. bool RecentDocumentList::contains (const File& f) const
  273. {
  274. for (int i = previousDocs.size(); --i >= 0;)
  275. if (previousDocs.getUnchecked(i)->getFile() == f)
  276. return true;
  277. return false;
  278. }
  279. OpenDocumentManager::Document* RecentDocumentList::getClosestPreviousDocOtherThan (OpenDocumentManager::Document* oneToAvoid) const
  280. {
  281. for (int i = previousDocs.size(); --i >= 0;)
  282. if (previousDocs.getUnchecked(i) != oneToAvoid)
  283. return previousDocs.getUnchecked(i);
  284. return nullptr;
  285. }
  286. bool RecentDocumentList::documentAboutToClose (OpenDocumentManager::Document* document)
  287. {
  288. previousDocs.removeAllInstancesOf (document);
  289. nextDocs.removeAllInstancesOf (document);
  290. jassert (! previousDocs.contains (document));
  291. jassert (! nextDocs.contains (document));
  292. return true;
  293. }
  294. static void restoreDocList (Project& project, Array <OpenDocumentManager::Document*>& list, const XmlElement* xml)
  295. {
  296. if (xml != nullptr)
  297. {
  298. OpenDocumentManager& odm = ProjucerApplication::getApp().openDocumentManager;
  299. forEachXmlChildElementWithTagName (*xml, e, "DOC")
  300. {
  301. const File file (e->getStringAttribute ("file"));
  302. if (file.exists())
  303. {
  304. if (OpenDocumentManager::Document* doc = odm.openFile (&project, file))
  305. {
  306. doc->restoreState (e->getStringAttribute ("state"));
  307. list.add (doc);
  308. }
  309. }
  310. }
  311. }
  312. }
  313. void RecentDocumentList::restoreFromXML (Project& project, const XmlElement& xml)
  314. {
  315. clear();
  316. if (xml.hasTagName ("RECENT_DOCUMENTS"))
  317. {
  318. restoreDocList (project, previousDocs, xml.getChildByName ("PREVIOUS"));
  319. restoreDocList (project, nextDocs, xml.getChildByName ("NEXT"));
  320. }
  321. }
  322. static void saveDocList (const Array <OpenDocumentManager::Document*>& list, XmlElement& xml)
  323. {
  324. for (int i = 0; i < list.size(); ++i)
  325. {
  326. const OpenDocumentManager::Document& doc = *list.getUnchecked(i);
  327. XmlElement* e = xml.createNewChildElement ("DOC");
  328. e->setAttribute ("file", doc.getFile().getFullPathName());
  329. e->setAttribute ("state", doc.getState());
  330. }
  331. }
  332. std::unique_ptr<XmlElement> RecentDocumentList::createXML() const
  333. {
  334. auto xml = std::make_unique<XmlElement> ("RECENT_DOCUMENTS");
  335. saveDocList (previousDocs, *xml->createNewChildElement ("PREVIOUS"));
  336. saveDocList (nextDocs, *xml->createNewChildElement ("NEXT"));
  337. return xml;
  338. }