FlmExportDialog.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #include "ui/FlmExportDialog.h"
  2. #include "engine/SafeWrite.h"
  3. #include "ui/Notices.h" // round 190qx
  4. #include <map>
  5. #include "engine/Mp3Format.h"
  6. #include "ui/FileChoosers.h"
  7. #include "ui/HintBar.h"
  8. #include "ui/I18n.h"
  9. namespace lynx
  10. {
  11. namespace
  12. {
  13. juce::String fromUtf8 (const std::string& s) { return juce::String::fromUTF8 (s.c_str(), (int) s.size()); }
  14. FlmExportOptions optionsFor (const juce::File& target, std::shared_ptr<juce::AudioFormatManager> formats)
  15. {
  16. FlmExportOptions o;
  17. o.title = target.getFileNameWithoutExtension().toStdString();
  18. o.targetFolder = target.getParentDirectory().getFullPathName().toStdString();
  19. o.nowSeconds = (double) juce::Time::currentTimeMillis() / 1000.0;
  20. o.resourceRoots = flmFactoryRoots();
  21. auto seen = std::make_shared<std::map<std::string, double>>();
  22. o.sampleSeconds = [formats, seen] (const std::string& path) {
  23. if (const auto it = seen->find (path); it != seen->end())
  24. return it->second;
  25. double seconds = 0.0;
  26. if (std::unique_ptr<juce::AudioFormatReader> reader { formats->createReaderFor (juce::File (fromUtf8 (path))) }; reader != nullptr && reader->sampleRate > 0)
  27. seconds = (double) reader->lengthInSamples / reader->sampleRate;
  28. seen->emplace (path, seconds);
  29. return seconds;
  30. };
  31. return o;
  32. }
  33. std::shared_ptr<juce::AudioFormatManager> audioFormats()
  34. {
  35. auto formats = std::make_shared<juce::AudioFormatManager>();
  36. registerAudioFormats (*formats);
  37. return formats;
  38. }
  39. } // namespace
  40. juce::String flmExportLossText (const FlmExportResult& result)
  41. {
  42. juce::String sound, other;
  43. for (const auto& l : result.lost)
  44. (l.sound ? sound : other) << "- " << tr (l.text.c_str(), { fromUtf8 (l.detail) }).trimEnd() << "\n";
  45. juce::String text;
  46. if (sound.isNotEmpty())
  47. text << tr ("May sound different there:") << "\n" << sound << "\n";
  48. if (other.isNotEmpty())
  49. text << tr ("Left out (the sound stays):") << "\n" << other;
  50. if (text.isEmpty())
  51. text = tr ("Nothing is lost: the copy holds the whole project.");
  52. return text.trimEnd();
  53. }
  54. juce::AlertWindow* buildFlmExportWindow (const FlmExportResult& result, std::shared_ptr<juce::TextEditor> list)
  55. {
  56. auto* w = new juce::AlertWindow (tr ("Save a copy as FL Studio Mobile project"),
  57. tr ("The copy opens in FL Studio Mobile, with its samples in a folder beside it. The open project stays as it is."),
  58. juce::MessageBoxIconType::NoIcon);
  59. list->setMultiLine (true);
  60. list->setReadOnly (true);
  61. list->setScrollbarsShown (true);
  62. list->setCaretVisible (false);
  63. list->setSize (560, 240);
  64. list->setText (flmExportLossText (result), false);
  65. w->addCustomComponent (list.get());
  66. w->addButton (tr ("Save copy..."), 1, juce::KeyPress (juce::KeyPress::returnKey));
  67. w->addButton (tr ("Cancel"), 0, juce::KeyPress (juce::KeyPress::escapeKey));
  68. return w;
  69. }
  70. bool writeFlmCopy (const Song& song, const juce::File& target, FlmExportResult& result, juce::String& error)
  71. {
  72. result = exportFlm (song, optionsFor (target, audioFormats()));
  73. if (! result.ok)
  74. {
  75. error = tr ("The FL Studio Mobile project could not be written");
  76. return false;
  77. }
  78. // the samples first (a copy that already holds the same file is left as it is), then the project
  79. int missing = 0;
  80. for (const auto& s : result.samples)
  81. {
  82. const juce::File from (fromUtf8 (s.source));
  83. const auto to = target.getParentDirectory().getChildFile (fromUtf8 (s.relative));
  84. if (! from.existsAsFile())
  85. {
  86. ++missing;
  87. continue;
  88. }
  89. if (to.existsAsFile() && to.getSize() == from.getSize() && to.hasIdenticalContentTo (from))
  90. continue;
  91. to.getParentDirectory().createDirectory();
  92. if (juce::String why; ! safewrite::copyFile (from, to, why)) // (round 190rm: checked)
  93. {
  94. error = tr ("Not written: {0}. {1} was not changed.", { tr (why), to.getFileName() });
  95. return false;
  96. }
  97. }
  98. if (missing > 0)
  99. result.lost.push_back ({ "Samples not found on this computer ({0}): their notes stay, FL Studio Mobile plays nothing for them", std::to_string (missing), true });
  100. if (juce::String why; ! safewrite::writeData (target, result.bytes.data(), result.bytes.size(), why)) // (round 190rm: checked)
  101. {
  102. error = tr ("Not written: {0}. {1} was not changed.", { tr (why), target.getFileName() });
  103. return false;
  104. }
  105. return true;
  106. }
  107. void showFlmExportDialog (AudioEngine& engine, juce::Component* parent, const juce::File& suggestedFolder, const juce::String& projectName)
  108. {
  109. engine.capturePluginStates();
  110. const auto stem = juce::File::createLegalFileName (projectName.isNotEmpty() ? projectName : juce::String ("LYNX SHARE"));
  111. // what the copy loses, worked out before anything is saved (the names of the copy's samples don't change it)
  112. const auto dryRun = exportFlm (engine.getSong(), optionsFor (suggestedFolder.getChildFile (stem + ".flm"), audioFormats()));
  113. auto list = std::make_shared<juce::TextEditor>();
  114. auto* w = buildFlmExportWindow (dryRun, list);
  115. juce::Component::SafePointer<juce::Component> safe (parent);
  116. w->enterModalState (true, juce::ModalCallbackFunction::create ([w, list, &engine, safe, suggestedFolder, stem] (int choice) {
  117. juce::ignoreUnused (w, list);
  118. if (choice != 1)
  119. return;
  120. auto chooser = std::shared_ptr<juce::FileChooser> (
  121. ui::makeFileChooser (tr ("Save a copy as FL Studio Mobile project"), suggestedFolder.getChildFile (stem + ".flm"), "*.flm").release());
  122. ui::launchChooser (chooser, juce::FileBrowserComponent::saveMode | juce::FileBrowserComponent::warnAboutOverwriting,
  123. // (round 190sk) the callback keeps the chooser alive (it died here before: nothing happened)
  124. [&engine, safe, chooser] (const juce::FileChooser& c) {
  125. auto target = c.getResult();
  126. if (target == juce::File())
  127. return;
  128. if (! target.hasFileExtension ("flm"))
  129. target = target.withFileExtension ("flm");
  130. engine.capturePluginStates();
  131. FlmExportResult result;
  132. juce::String error;
  133. if (! writeFlmCopy (engine.getSong(), target, result, error))
  134. {
  135. juce::AlertWindow::showMessageBoxAsync (juce::MessageBoxIconType::WarningIcon, tr ("Could not save"), error, {},
  136. safe.getComponent());
  137. return;
  138. }
  139. ui::savedFile (tr ("Saved a copy for FL Studio Mobile"), target); // (round 190qx)
  140. });
  141. }), true);
  142. }
  143. } // namespace lynx