File: PathList.cpp

package info (click to toggle)
audacity 3.7.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 125,252 kB
  • sloc: cpp: 358,238; ansic: 75,458; lisp: 7,761; sh: 3,410; python: 1,503; xml: 1,385; perl: 854; makefile: 122
file content (198 lines) | stat: -rw-r--r-- 7,052 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/**********************************************************************

  Audacity: A Digital Audio Editor

  @file PathList.cpp

  Paul Licameli split from AudacityApp.cpp

**********************************************************************/

#include "PathList.h"

#include "PlatformCompatibility.h"
#include "FileNames.h"
#include "TempDirectory.h"
#include <wx/stdpaths.h>
#include <wx/utils.h>

#if HAVE_DLFCN_H && !defined(DISABLE_DLADDR)
#  if __linux__ && !defined(_GNU_SOURCE)
#    define _GNU_SOURCE
#  endif
#  include <dlfcn.h>
#  define HAVE_GET_LIBRARY_PATH 1
namespace
{
wxString GetLibraryPath()
{
   Dl_info info;
   // This is a GNU extension, but it's also supported on FreeBSD, OpenBSD, macOS and Solaris.
   if (dladdr(reinterpret_cast<const void*>(GetLibraryPath), &info))
      return info.dli_fname;
   return {};
}
}
#endif

void FileNames::InitializePathList()
{
   const auto programPath = PlatformCompatibility::GetExecutablePath();

   //
   // Paths: set search path and temp dir path
   //
   FilePaths audacityPathList;
   auto &standardPaths = wxStandardPaths::Get();

#ifdef __WXGTK__
   const auto portablePrefix = wxPathOnly(wxPathOnly(programPath));

   // Make sure install prefix is set so wxStandardPath resolves paths properly
   if (wxDirExists(portablePrefix + L"/share/audacity")) {
      // use prefix relative to executable location to make Audacity portable
      standardPaths.SetInstallPrefix(portablePrefix);
   } else {
      // fallback to hard-coded prefix set during configuration
      standardPaths.SetInstallPrefix(wxT(INSTALL_PREFIX));
   }
   wxString installPrefix = standardPaths.GetInstallPrefix();

   /* Search path (for plug-ins, translations etc) is (in this order):
      * The AUDACITY_PATH environment variable
      * The current directory
      * The user's "~/.audacity-data" or "Portable Settings" directory
      * The user's "~/.audacity-files" directory
      * The "share" and "share/doc" directories in their install path */
   wxString home = wxGetHomeDir();

   wxString envTempDir = wxGetenv(wxT("TMPDIR"));
   if (!envTempDir.empty()) {
      /* On Unix systems, the environment variable TMPDIR may point to
         an unusual path when /tmp and /var/tmp are not desirable. */
      TempDirectory::SetDefaultTempDir( wxString::Format(
         wxT("%s/audacity-%s"), envTempDir, wxGetUserId() ) );
   } else {
      /* On Unix systems, the default temp dir is in /var/tmp. */
      TempDirectory::SetDefaultTempDir( wxString::Format(
         wxT("/var/tmp/audacity-%s"), wxGetUserId() ) );
   }

   wxString pathVar = wxGetenv(wxT("AUDACITY_PATH"));

   if (!pathVar.empty())
      FileNames::AddMultiPathsToPathList(pathVar, audacityPathList);
   FileNames::AddUniquePathToPathList(::wxGetCwd(), audacityPathList);

   const auto progPath = wxPathOnly(programPath);

   FileNames::AddUniquePathToPathList(progPath, audacityPathList);
   // Add the path to modules:
   FileNames::AddUniquePathToPathList(progPath + L"/lib/audacity", audacityPathList);

#if !defined(__WXMSW__)
   // On Unix systems, the common directory structure is
   // .../bin
   // .../lib
   const wxString progParentPath = wxPathOnly(progPath);

   if (!progParentPath.IsEmpty())
   {
      FileNames::AddUniquePathToPathList(progParentPath + L"/lib/audacity", audacityPathList);
      FileNames::AddUniquePathToPathList(progParentPath + L"/lib", audacityPathList);
   }

#if HAVE_GET_LIBRARY_PATH
   const wxString thisLibPath = GetLibraryPath();
   if (!thisLibPath.IsEmpty())
      FileNames::AddUniquePathToPathList(wxPathOnly(thisLibPath), audacityPathList);
#endif
#endif

   FileNames::AddUniquePathToPathList(FileNames::DataDir(), audacityPathList);

#ifdef AUDACITY_NAME
   FileNames::AddUniquePathToPathList(wxString::Format(wxT("%s/.%s-files"),
      home, wxT(AUDACITY_NAME)),
      audacityPathList);
   FileNames::AddUniquePathToPathList(FileNames::ModulesDir(),
      audacityPathList);
   FileNames::AddUniquePathToPathList(wxString::Format(installPrefix + L"/share/%s", wxT(AUDACITY_NAME)),
      audacityPathList);
   FileNames::AddUniquePathToPathList(wxString::Format(installPrefix + L"/share/doc/%s", wxT(AUDACITY_NAME)),
      audacityPathList);
#else //AUDACITY_NAME
   FileNames::AddUniquePathToPathList(wxString::Format(wxT("%s/.audacity-files"),
      home),
      audacityPathList);
   FileNames::AddUniquePathToPathList(FileNames::ModulesDir(),
      audacityPathList);
   FileNames::AddUniquePathToPathList(installPrefix + L"/share/audacity",
      audacityPathList);
   FileNames::AddUniquePathToPathList(installPrefix + L"/share/doc/audacity",
      audacityPathList);
#endif //AUDACITY_NAME

   FileNames::AddUniquePathToPathList(installPrefix + L"/share/locale",
      audacityPathList);

   FileNames::AddUniquePathToPathList(wxString::Format(wxT("./locale")),
      audacityPathList);

#endif //__WXGTK__

// JKC Bug 1220: Use path based on home directory on WXMAC
#ifdef __WXMAC__
   wxFileName tmpFile;
   tmpFile.AssignHomeDir();
   wxString tmpDirLoc = tmpFile.GetPath(wxPATH_GET_VOLUME);
#else
   wxFileName tmpFile;
   tmpFile.AssignTempFileName(wxT("nn"));
   wxString tmpDirLoc = tmpFile.GetPath(wxPATH_GET_VOLUME);
   ::wxRemoveFile(tmpFile.GetFullPath());
#endif



   // On Mac and Windows systems, use the directory which contains Audacity.
#ifdef __WXMSW__
   // On Windows, the path to the Audacity program is programPath
   const auto progPath = wxPathOnly(programPath);
   FileNames::AddUniquePathToPathList(progPath, audacityPathList);
   FileNames::AddUniquePathToPathList(progPath + wxT("\\Languages"), audacityPathList);

   // See bug #1271 for explanation of location
   tmpDirLoc = FileNames::MkDir(wxStandardPaths::Get().GetUserLocalDataDir());
   TempDirectory::SetDefaultTempDir( wxString::Format(
      wxT("%s\\SessionData"), tmpDirLoc ) );
#endif //__WXWSW__

#ifdef __WXMAC__
   // On Mac OS X, the path to the Audacity program is programPath
   const auto progPath = wxPathOnly(programPath);

   FileNames::AddUniquePathToPathList(progPath, audacityPathList);
   // If Audacity is a "bundle" package, then the root directory is
   // the great-great-grandparent of the directory containing the executable.
   //FileNames::AddUniquePathToPathList(progPath + wxT("/../../../"), audacityPathList);

   // These allow for searching the "bundle"
   FileNames::AddUniquePathToPathList(
      progPath + wxT("/../"), audacityPathList);
   FileNames::AddUniquePathToPathList(
      progPath + wxT("/../Resources"), audacityPathList);

   // JKC Bug 1220: Using an actual temp directory for session data on Mac was
   // wrong because it would get cleared out on a reboot.
   TempDirectory::SetDefaultTempDir( wxString::Format(
      wxT("%s/Library/Application Support/audacity/SessionData"), tmpDirLoc) );

   //TempDirectory::SetDefaultTempDir( wxString::Format(
   //   wxT("%s/audacity-%s"),
   //   tmpDirLoc,
   //   wxGetUserId() ) );
#endif //__WXMAC__

   FileNames::SetAudacityPathList( std::move( audacityPathList ) );
}