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.

264 lines
8.9KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. FileBasedDocument::FileBasedDocument (const String& fileExtension_,
  19. const String& fileWildcard_,
  20. const String& openFileDialogTitle_,
  21. const String& saveFileDialogTitle_)
  22. : changedSinceSave (false),
  23. fileExtension (fileExtension_),
  24. fileWildcard (fileWildcard_),
  25. openFileDialogTitle (openFileDialogTitle_),
  26. saveFileDialogTitle (saveFileDialogTitle_)
  27. {
  28. }
  29. FileBasedDocument::~FileBasedDocument()
  30. {
  31. }
  32. //==============================================================================
  33. void FileBasedDocument::setChangedFlag (const bool hasChanged)
  34. {
  35. if (changedSinceSave != hasChanged)
  36. {
  37. changedSinceSave = hasChanged;
  38. sendChangeMessage();
  39. }
  40. }
  41. void FileBasedDocument::changed()
  42. {
  43. changedSinceSave = true;
  44. sendChangeMessage();
  45. }
  46. //==============================================================================
  47. void FileBasedDocument::setFile (const File& newFile)
  48. {
  49. if (documentFile != newFile)
  50. {
  51. documentFile = newFile;
  52. changed();
  53. }
  54. }
  55. //==============================================================================
  56. #if JUCE_MODAL_LOOPS_PERMITTED
  57. bool FileBasedDocument::loadFrom (const File& newFile,
  58. const bool showMessageOnFailure)
  59. {
  60. MouseCursor::showWaitCursor();
  61. const File oldFile (documentFile);
  62. documentFile = newFile;
  63. Result result (Result::fail ("The file doesn't exist"));
  64. if (newFile.existsAsFile())
  65. {
  66. result = loadDocument (newFile);
  67. if (result.wasOk())
  68. {
  69. setChangedFlag (false);
  70. MouseCursor::hideWaitCursor();
  71. setLastDocumentOpened (newFile);
  72. return true;
  73. }
  74. }
  75. documentFile = oldFile;
  76. MouseCursor::hideWaitCursor();
  77. if (showMessageOnFailure)
  78. {
  79. AlertWindow::showMessageBox (AlertWindow::WarningIcon,
  80. TRANS("Failed to open file..."),
  81. TRANS("There was an error while trying to load the file: FLNM")
  82. .replace ("FLNM", "\n" + newFile.getFullPathName())
  83. + "\n\n"
  84. + result.getErrorMessage());
  85. }
  86. return false;
  87. }
  88. bool FileBasedDocument::loadFromUserSpecifiedFile (const bool showMessageOnFailure)
  89. {
  90. FileChooser fc (openFileDialogTitle,
  91. getLastDocumentOpened(),
  92. fileWildcard);
  93. if (fc.browseForFileToOpen())
  94. return loadFrom (fc.getResult(), showMessageOnFailure);
  95. return false;
  96. }
  97. static bool askToOverwriteFile (const File& newFile)
  98. {
  99. return AlertWindow::showOkCancelBox (AlertWindow::WarningIcon,
  100. TRANS("File already exists"),
  101. TRANS("There's already a file called: FLMN")
  102. .replace ("FLNM", newFile.getFullPathName())
  103. + "\n\n"
  104. + TRANS("Are you sure you want to overwrite it?"),
  105. TRANS("Overwrite"),
  106. TRANS("Cancel"));
  107. }
  108. //==============================================================================
  109. FileBasedDocument::SaveResult FileBasedDocument::save (const bool askUserForFileIfNotSpecified,
  110. const bool showMessageOnFailure)
  111. {
  112. return saveAs (documentFile,
  113. false,
  114. askUserForFileIfNotSpecified,
  115. showMessageOnFailure);
  116. }
  117. FileBasedDocument::SaveResult FileBasedDocument::saveAs (const File& newFile,
  118. const bool warnAboutOverwritingExistingFiles,
  119. const bool askUserForFileIfNotSpecified,
  120. const bool showMessageOnFailure)
  121. {
  122. if (newFile == File::nonexistent)
  123. {
  124. if (askUserForFileIfNotSpecified)
  125. return saveAsInteractive (true);
  126. // can't save to an unspecified file
  127. jassertfalse;
  128. return failedToWriteToFile;
  129. }
  130. if (warnAboutOverwritingExistingFiles
  131. && newFile.exists()
  132. && ! askToOverwriteFile (newFile))
  133. return userCancelledSave;
  134. MouseCursor::showWaitCursor();
  135. const File oldFile (documentFile);
  136. documentFile = newFile;
  137. const Result result (saveDocument (newFile));
  138. if (result.wasOk())
  139. {
  140. setChangedFlag (false);
  141. MouseCursor::hideWaitCursor();
  142. return savedOk;
  143. }
  144. documentFile = oldFile;
  145. MouseCursor::hideWaitCursor();
  146. if (showMessageOnFailure)
  147. {
  148. AlertWindow::showMessageBox (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. }
  156. return failedToWriteToFile;
  157. }
  158. FileBasedDocument::SaveResult FileBasedDocument::saveIfNeededAndUserAgrees()
  159. {
  160. if (! hasChangedSinceSaved())
  161. return savedOk;
  162. const int 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 (const bool warnAboutOverwritingExistingFiles)
  180. {
  181. File f;
  182. if (documentFile.existsAsFile())
  183. f = documentFile;
  184. else
  185. f = getLastDocumentOpened();
  186. String legalFilename (File::createLegalFileName (getDocumentTitle()));
  187. if (legalFilename.isEmpty())
  188. legalFilename = "unnamed";
  189. if (f.existsAsFile() || f.getParentDirectory().isDirectory())
  190. f = f.getSiblingFile (legalFilename);
  191. else
  192. f = File::getSpecialLocation (File::userDocumentsDirectory).getChildFile (legalFilename);
  193. f = getSuggestedSaveAsFile (f);
  194. FileChooser fc (saveFileDialogTitle, f, fileWildcard);
  195. if (fc.browseForFileToSave (warnAboutOverwritingExistingFiles))
  196. {
  197. File chosen (fc.getResult());
  198. if (chosen.getFileExtension().isEmpty())
  199. {
  200. chosen = chosen.withFileExtension (fileExtension);
  201. if (chosen.exists() && ! askToOverwriteFile (chosen))
  202. return userCancelledSave;
  203. }
  204. setLastDocumentOpened (chosen);
  205. return saveAs (chosen, false, false, true);
  206. }
  207. return userCancelledSave;
  208. }
  209. #endif