Audio plugin host https://kx.studio/carla
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.

265 lines
8.7KB

  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. namespace juce
  14. {
  15. FileBasedDocument::FileBasedDocument (const String& fileExtension_,
  16. const String& fileWildcard_,
  17. const String& openFileDialogTitle_,
  18. const String& saveFileDialogTitle_)
  19. : fileExtension (fileExtension_),
  20. fileWildcard (fileWildcard_),
  21. openFileDialogTitle (openFileDialogTitle_),
  22. saveFileDialogTitle (saveFileDialogTitle_)
  23. {
  24. }
  25. FileBasedDocument::~FileBasedDocument()
  26. {
  27. }
  28. //==============================================================================
  29. void FileBasedDocument::setChangedFlag (bool hasChanged)
  30. {
  31. if (changedSinceSave != hasChanged)
  32. {
  33. changedSinceSave = hasChanged;
  34. sendChangeMessage();
  35. }
  36. }
  37. void FileBasedDocument::changed()
  38. {
  39. changedSinceSave = true;
  40. sendChangeMessage();
  41. }
  42. //==============================================================================
  43. void FileBasedDocument::setFile (const File& newFile)
  44. {
  45. if (documentFile != newFile)
  46. {
  47. documentFile = newFile;
  48. changed();
  49. }
  50. }
  51. //==============================================================================
  52. Result FileBasedDocument::loadFrom (const File& newFile, bool showMessageOnFailure, bool showWaitCursor)
  53. {
  54. if (showWaitCursor)
  55. MouseCursor::showWaitCursor();
  56. auto oldFile = documentFile;
  57. documentFile = newFile;
  58. auto result = Result::fail (TRANS("The file doesn't exist"));
  59. if (newFile.existsAsFile())
  60. {
  61. result = loadDocument (newFile);
  62. if (result.wasOk())
  63. {
  64. setChangedFlag (false);
  65. if (showWaitCursor)
  66. MouseCursor::hideWaitCursor();
  67. setLastDocumentOpened (newFile);
  68. return result;
  69. }
  70. }
  71. documentFile = oldFile;
  72. if (showWaitCursor)
  73. MouseCursor::hideWaitCursor();
  74. if (showMessageOnFailure)
  75. AlertWindow::showMessageBoxAsync (AlertWindow::WarningIcon,
  76. TRANS("Failed to open file..."),
  77. TRANS("There was an error while trying to load the file: FLNM")
  78. .replace ("FLNM", "\n" + newFile.getFullPathName())
  79. + "\n\n"
  80. + result.getErrorMessage());
  81. return result;
  82. }
  83. #if JUCE_MODAL_LOOPS_PERMITTED
  84. Result FileBasedDocument::loadFromUserSpecifiedFile (const bool showMessageOnFailure)
  85. {
  86. FileChooser fc (openFileDialogTitle,
  87. getLastDocumentOpened(),
  88. fileWildcard);
  89. if (fc.browseForFileToOpen())
  90. return loadFrom (fc.getResult(), showMessageOnFailure);
  91. return Result::fail (TRANS("User cancelled"));
  92. }
  93. static bool askToOverwriteFile (const File& newFile)
  94. {
  95. return AlertWindow::showOkCancelBox (AlertWindow::WarningIcon,
  96. TRANS("File already exists"),
  97. TRANS("There's already a file called: FLNM")
  98. .replace ("FLNM", newFile.getFullPathName())
  99. + "\n\n"
  100. + TRANS("Are you sure you want to overwrite it?"),
  101. TRANS("Overwrite"),
  102. TRANS("Cancel"));
  103. }
  104. //==============================================================================
  105. FileBasedDocument::SaveResult FileBasedDocument::save (bool askUserForFileIfNotSpecified,
  106. bool showMessageOnFailure)
  107. {
  108. return saveAs (documentFile,
  109. false,
  110. askUserForFileIfNotSpecified,
  111. showMessageOnFailure);
  112. }
  113. FileBasedDocument::SaveResult FileBasedDocument::saveAs (const File& newFile,
  114. bool warnAboutOverwritingExistingFiles,
  115. bool askUserForFileIfNotSpecified,
  116. bool showMessageOnFailure,
  117. bool showWaitCursor)
  118. {
  119. if (newFile == File())
  120. {
  121. if (askUserForFileIfNotSpecified)
  122. return saveAsInteractive (true);
  123. // can't save to an unspecified file
  124. jassertfalse;
  125. return failedToWriteToFile;
  126. }
  127. if (warnAboutOverwritingExistingFiles
  128. && newFile.exists()
  129. && ! askToOverwriteFile (newFile))
  130. return userCancelledSave;
  131. if (showWaitCursor)
  132. MouseCursor::showWaitCursor();
  133. auto oldFile = documentFile;
  134. documentFile = newFile;
  135. auto result = saveDocument (newFile);
  136. if (result.wasOk())
  137. {
  138. setChangedFlag (false);
  139. if (showWaitCursor)
  140. MouseCursor::hideWaitCursor();
  141. sendChangeMessage(); // because the filename may have changed
  142. return savedOk;
  143. }
  144. documentFile = oldFile;
  145. if (showWaitCursor)
  146. MouseCursor::hideWaitCursor();
  147. if (showMessageOnFailure)
  148. AlertWindow::showMessageBoxAsync (AlertWindow::WarningIcon,
  149. TRANS("Error writing to file..."),
  150. TRANS("An error occurred while trying to save \"DCNM\" to the file: FLNM")
  151. .replace ("DCNM", getDocumentTitle())
  152. .replace ("FLNM", "\n" + newFile.getFullPathName())
  153. + "\n\n"
  154. + result.getErrorMessage());
  155. sendChangeMessage(); // because the filename may have changed
  156. return failedToWriteToFile;
  157. }
  158. FileBasedDocument::SaveResult FileBasedDocument::saveIfNeededAndUserAgrees()
  159. {
  160. if (! hasChangedSinceSaved())
  161. return savedOk;
  162. auto r = AlertWindow::showYesNoCancelBox (AlertWindow::QuestionIcon,
  163. TRANS("Closing document..."),
  164. TRANS("Do you want to save the changes to \"DCNM\"?")
  165. .replace ("DCNM", getDocumentTitle()),
  166. TRANS("Save"),
  167. TRANS("Discard changes"),
  168. TRANS("Cancel"));
  169. if (r == 1) // save changes
  170. return save (true, true);
  171. if (r == 2) // discard changes
  172. return savedOk;
  173. return userCancelledSave;
  174. }
  175. File FileBasedDocument::getSuggestedSaveAsFile (const File& defaultFile)
  176. {
  177. return defaultFile.withFileExtension (fileExtension).getNonexistentSibling (true);
  178. }
  179. FileBasedDocument::SaveResult FileBasedDocument::saveAsInteractive (bool warnAboutOverwritingExistingFiles)
  180. {
  181. auto f = documentFile.existsAsFile() ? documentFile : getLastDocumentOpened();
  182. auto legalFilename = File::createLegalFileName (getDocumentTitle());
  183. if (legalFilename.isEmpty())
  184. legalFilename = "unnamed";
  185. if (f.existsAsFile() || f.getParentDirectory().isDirectory())
  186. f = f.getSiblingFile (legalFilename);
  187. else
  188. f = File::getSpecialLocation (File::userDocumentsDirectory).getChildFile (legalFilename);
  189. f = getSuggestedSaveAsFile (f);
  190. FileChooser fc (saveFileDialogTitle, f, fileWildcard);
  191. if (fc.browseForFileToSave (warnAboutOverwritingExistingFiles))
  192. {
  193. auto chosen = fc.getResult();
  194. if (chosen.getFileExtension().isEmpty())
  195. {
  196. chosen = chosen.withFileExtension (fileExtension);
  197. if (chosen.exists() && ! askToOverwriteFile (chosen))
  198. return userCancelledSave;
  199. }
  200. setLastDocumentOpened (chosen);
  201. return saveAs (chosen, false, false, true);
  202. }
  203. return userCancelledSave;
  204. }
  205. #endif
  206. } // namespace juce