[PATCH] Improve specialDirectory resolution


#1

Hello Jules,

Most modern Linux desktop systems configure their special directories through a config file “~/.config/user-dirs.dirs”.

I also took the liberty of suggesting a userPicturesDirectory, although it’s incomplete since I haven’t touched Mac/Win etc.

[code]diff --git a/modules/juce_core/files/juce_File.h b/modules/juce_core/files/juce_File.h
index 1aa13c6…682930b 100644
— a/modules/juce_core/files/juce_File.h
+++ b/modules/juce_core/files/juce_File.h
@@ -834,6 +834,9 @@ public:

     /** The most likely place where a user might store their movie files. */
     userMoviesDirectory,
  •    /** The most likely place where a user might store their picture files. */
    
  •    userPicturesDirectory,
    

    };

    /** Finds the location of a special type of file or directory, such as a home folder or
    diff --git a/modules/juce_core/native/juce_linux_Files.cpp b/modules/juce_core/native/juce_linux_Files.cpp
    index 55b0434…0c4f8ab 100644
    — a/modules/juce_core/native/juce_linux_Files.cpp
    +++ b/modules/juce_core/native/juce_linux_Files.cpp
    @@ -132,6 +132,26 @@ File File::getLinkedTarget() const
    const char* const* juce_argv = nullptr;
    int juce_argc = 0;

+static File resolveXdgSpecialDirectory (const String& xdgToken, const String& fallBackDirectory)
+{

  • StringArray xdgConf;
  • File("~/.config/user-dirs.dirs").readLines(xdgConf);
  • for (int i = 0; i < xdgConf.size(); ++i)
  • {
  •    String line = xdgConf[i];
    
  •    if (line.trim().startsWith (xdgToken))
    
  •    {
    
  •        // eg. resolve XDG_MUSIC_DIR="$HOME/Music" to /home/user/Music
    
  •        File xdgDir (line.replace ("$HOME", File ("~").getFullPathName()).fromFirstOccurrenceOf ("=", false, false).removeCharacters ("\"").trim());
    
  •        if (xdgDir.isDirectory())
    
  •            return xdgDir;
    
  •    }
    
  • }
  • return File (fallBackDirectory);
    +}

File File::getSpecialLocation (const SpecialLocationType type)
{
switch (type)
@@ -151,13 +171,22 @@ File File::getSpecialLocation (const SpecialLocationType type)
}

 case userDocumentsDirectory:
  •    return resolveXdgSpecialDirectory ("XDG_DOCUMENTS_DIR", "~");
    
  • case userMusicDirectory:

  •    return resolveXdgSpecialDirectory ("XDG_MUSIC_DIR", "~");
    
  • case userMoviesDirectory:

  •    return resolveXdgSpecialDirectory ("XDG_VIDEOS_DIR", "~");
    
  • case userPicturesDirectory:

  •    return resolveXdgSpecialDirectory ("XDG_PICTURES_DIR", "~");
    
  • case userApplicationDataDirectory:
    return File ("~");

    case userDesktopDirectory:

  •    return File ("~/Desktop");
    
  •    return resolveXdgSpecialDirectory ("XDG_DESKTOP_DIR", "~/Desktop");
    

    case commonApplicationDataDirectory:
    return File ("/var");
    [/code]


#2

Excellent stuff, thanks! I’ll have a look at that today…


#3

Brilliant! Thank you for reacting so quickly. It’s instructive to see the changes you make, every time I learn something new about Juce or C++. :slight_smile: