diff --git a/Source/PluginEditor.cpp b/Source/PluginEditor.cpp index 1abc22d..4c29cd2 100644 --- a/Source/PluginEditor.cpp +++ b/Source/PluginEditor.cpp @@ -1,6 +1,30 @@ #include "PluginEditor.h" #include +namespace +{ + // Returns a sensible default directory for preset files. On Linux, + // File::getSpecialLocation (userHomeDirectory) can resolve to "/" when the + // home directory can't be determined, so fall back through $HOME, the user + // documents directory, and finally the current working directory. + juce::File getDefaultPresetDirectory() + { + auto test = [] (const juce::File& f) { return f.isDirectory() && f.createDirectory().wasOk(); }; + + if (auto* homeEnv = std::getenv ("HOME")) + if (test (juce::File (homeEnv))) + return juce::File (homeEnv); + + if (test (juce::File::getSpecialLocation (juce::File::userHomeDirectory))) + return juce::File::getSpecialLocation (juce::File::userHomeDirectory); + + if (test (juce::File::getSpecialLocation (juce::File::userDocumentsDirectory))) + return juce::File::getSpecialLocation (juce::File::userDocumentsDirectory); + + return juce::File::getCurrentWorkingDirectory(); + } +} + KnobLookAndFeel::KnobLookAndFeel() { knobOverlay = juce::ImageCache::getFromMemory(BinaryData::metalknob_png, BinaryData::metalknob_pngSize); @@ -907,7 +931,7 @@ juce::String MainContentComponent::currentPresetName() const { void MainContentComponent::savePresetFile() { auto* chooser = new juce::FileChooser("Save ChromaFlock Preset", - juce::File::getSpecialLocation(juce::File::userHomeDirectory) + getDefaultPresetDirectory() .getChildFile("chromaflock-" + currentPresetName().replaceCharacter(' ', '-') + ".cfl"), "*.cfl"); @@ -945,7 +969,7 @@ void MainContentComponent::savePresetFile() { void MainContentComponent::loadPresetFile() { auto* chooser = new juce::FileChooser("Load ChromaFlock Preset", - juce::File::getSpecialLocation(juce::File::userHomeDirectory), + getDefaultPresetDirectory(), "*.cfl"); chooser->launchAsync(juce::FileBrowserComponent::openMode