Audacity 3.2.0
PathList.cpp
Go to the documentation of this file.
1/**********************************************************************
2
3 Audacity: A Digital Audio Editor
4
5 @file PathList.cpp
6
7 Paul Licameli split from AudacityApp.cpp
8
9**********************************************************************/
10
11#include "PathList.h"
12
14#include "FileNames.h"
15#include "TempDirectory.h"
16#include <wx/stdpaths.h>
17#include <wx/utils.h>
18
19#if HAVE_DLFCN_H && !defined(DISABLE_DLADDR)
20# if __linux__ && !defined(_GNU_SOURCE)
21# define _GNU_SOURCE
22# endif
23# include <dlfcn.h>
24# define HAVE_GET_LIBRARY_PATH 1
25namespace
26{
27wxString GetLibraryPath()
28{
29 Dl_info info;
30 // This is a GNU extension, but it's also supported on FreeBSD, OpenBSD, macOS and Solaris.
31 if (dladdr(reinterpret_cast<const void*>(GetLibraryPath), &info))
32 return info.dli_fname;
33 return {};
34}
35}
36#endif
37
39{
40 const auto programPath = PlatformCompatibility::GetExecutablePath();
41
42 //
43 // Paths: set search path and temp dir path
44 //
45 FilePaths audacityPathList;
46 auto &standardPaths = wxStandardPaths::Get();
47
48#ifdef __WXGTK__
49 const auto portablePrefix = wxPathOnly(wxPathOnly(programPath));
50
51 // Make sure install prefix is set so wxStandardPath resolves paths properly
52 if (wxDirExists(portablePrefix + L"/share/audacity")) {
53 // use prefix relative to executable location to make Audacity portable
54 standardPaths.SetInstallPrefix(portablePrefix);
55 } else {
56 // fallback to hard-coded prefix set during configuration
57 standardPaths.SetInstallPrefix(wxT(INSTALL_PREFIX));
58 }
59 wxString installPrefix = standardPaths.GetInstallPrefix();
60
61 /* Search path (for plug-ins, translations etc) is (in this order):
62 * The AUDACITY_PATH environment variable
63 * The current directory
64 * The user's "~/.audacity-data" or "Portable Settings" directory
65 * The user's "~/.audacity-files" directory
66 * The "share" and "share/doc" directories in their install path */
67 wxString home = wxGetHomeDir();
68
69 wxString envTempDir = wxGetenv(wxT("TMPDIR"));
70 if (!envTempDir.empty()) {
71 /* On Unix systems, the environment variable TMPDIR may point to
72 an unusual path when /tmp and /var/tmp are not desirable. */
73 TempDirectory::SetDefaultTempDir( wxString::Format(
74 wxT("%s/audacity-%s"), envTempDir, wxGetUserId() ) );
75 } else {
76 /* On Unix systems, the default temp dir is in /var/tmp. */
77 TempDirectory::SetDefaultTempDir( wxString::Format(
78 wxT("/var/tmp/audacity-%s"), wxGetUserId() ) );
79 }
80
81 wxString pathVar = wxGetenv(wxT("AUDACITY_PATH"));
82
83 if (!pathVar.empty())
84 FileNames::AddMultiPathsToPathList(pathVar, audacityPathList);
85 FileNames::AddUniquePathToPathList(::wxGetCwd(), audacityPathList);
86
87 const auto progPath = wxPathOnly(programPath);
88
89 FileNames::AddUniquePathToPathList(progPath, audacityPathList);
90 // Add the path to modules:
91 FileNames::AddUniquePathToPathList(progPath + L"/lib/audacity", audacityPathList);
92
93#if !defined(__WXMSW__)
94 // On Unix systems, the common directory structure is
95 // .../bin
96 // .../lib
97 const wxString progParentPath = wxPathOnly(progPath);
98
99 if (!progParentPath.IsEmpty())
100 {
101 FileNames::AddUniquePathToPathList(progParentPath + L"/lib/audacity", audacityPathList);
102 FileNames::AddUniquePathToPathList(progParentPath + L"/lib", audacityPathList);
103 }
104
105#if HAVE_GET_LIBRARY_PATH
106 const wxString thisLibPath = GetLibraryPath();
107 if (!thisLibPath.IsEmpty())
108 FileNames::AddUniquePathToPathList(wxPathOnly(thisLibPath), audacityPathList);
109#endif
110#endif
111
113
114#ifdef AUDACITY_NAME
115 FileNames::AddUniquePathToPathList(wxString::Format(wxT("%s/.%s-files"),
116 home, wxT(AUDACITY_NAME)),
117 audacityPathList);
119 audacityPathList);
120 FileNames::AddUniquePathToPathList(wxString::Format(installPrefix + L"/share/%s", wxT(AUDACITY_NAME)),
121 audacityPathList);
122 FileNames::AddUniquePathToPathList(wxString::Format(installPrefix + L"/share/doc/%s", wxT(AUDACITY_NAME)),
123 audacityPathList);
124#else //AUDACITY_NAME
125 FileNames::AddUniquePathToPathList(wxString::Format(wxT("%s/.audacity-files"),
126 home),
127 audacityPathList);
129 audacityPathList);
130 FileNames::AddUniquePathToPathList(installPrefix + L"/share/audacity",
131 audacityPathList);
132 FileNames::AddUniquePathToPathList(installPrefix + L"/share/doc/audacity",
133 audacityPathList);
134#endif //AUDACITY_NAME
135
136 FileNames::AddUniquePathToPathList(installPrefix + L"/share/locale",
137 audacityPathList);
138
139 FileNames::AddUniquePathToPathList(wxString::Format(wxT("./locale")),
140 audacityPathList);
141
142#endif //__WXGTK__
143
144// JKC Bug 1220: Use path based on home directory on WXMAC
145#ifdef __WXMAC__
146 wxFileName tmpFile;
147 tmpFile.AssignHomeDir();
148 wxString tmpDirLoc = tmpFile.GetPath(wxPATH_GET_VOLUME);
149#else
150 wxFileName tmpFile;
151 tmpFile.AssignTempFileName(wxT("nn"));
152 wxString tmpDirLoc = tmpFile.GetPath(wxPATH_GET_VOLUME);
153 ::wxRemoveFile(tmpFile.GetFullPath());
154#endif
155
156
157
158 // On Mac and Windows systems, use the directory which contains Audacity.
159#ifdef __WXMSW__
160 // On Windows, the path to the Audacity program is programPath
161 const auto progPath = wxPathOnly(programPath);
162 FileNames::AddUniquePathToPathList(progPath, audacityPathList);
163 FileNames::AddUniquePathToPathList(progPath + wxT("\\Languages"), audacityPathList);
164
165 // See bug #1271 for explanation of location
167 TempDirectory::SetDefaultTempDir( wxString::Format(
168 wxT("%s\\SessionData"), tmpDirLoc ) );
169#endif //__WXWSW__
170
171#ifdef __WXMAC__
172 // On Mac OS X, the path to the Audacity program is programPath
173 const auto progPath = wxPathOnly(programPath);
174
175 FileNames::AddUniquePathToPathList(progPath, audacityPathList);
176 // If Audacity is a "bundle" package, then the root directory is
177 // the great-great-grandparent of the directory containing the executable.
178 //FileNames::AddUniquePathToPathList(progPath + wxT("/../../../"), audacityPathList);
179
180 // These allow for searching the "bundle"
182 progPath + wxT("/../"), audacityPathList);
184 progPath + wxT("/../Resources"), audacityPathList);
185
186 // JKC Bug 1220: Using an actual temp directory for session data on Mac was
187 // wrong because it would get cleared out on a reboot.
188 TempDirectory::SetDefaultTempDir( wxString::Format(
189 wxT("%s/Library/Application Support/audacity/SessionData"), tmpDirLoc) );
190
191 //TempDirectory::SetDefaultTempDir( wxString::Format(
192 // wxT("%s/audacity-%s"),
193 // tmpDirLoc,
194 // wxGetUserId() ) );
195#endif //__WXMAC__
196
197 FileNames::SetAudacityPathList( std::move( audacityPathList ) );
198}
wxT("CloseDown"))
Extend wxArrayString with move operations and construction and insertion fromstd::initializer_list.
Services * Get()
Fetch the global instance, or nullptr if none is yet installed.
Definition: BasicUI.cpp:202
FILES_API wxString MkDir(const wxString &Str)
FILES_API void AddUniquePathToPathList(const FilePath &path, FilePaths &pathList)
FILES_API void InitializePathList()
FILES_API void AddMultiPathsToPathList(const wxString &multiPathString, FilePaths &pathList)
FILES_API FilePath DataDir()
Audacity user data directory.
FILES_API FilePath ModulesDir()
FILES_API void SetAudacityPathList(FilePaths list)
std::string FILES_API GetExecutablePath()
std::string FILES_API GetUserLocalDataDir()
FILES_API void SetDefaultTempDir(const FilePath &tempDir)