KiCad PCB EDA Suite
paths.cpp
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2021 KiCad Developers, see AUTHORS.txt for contributors.
5  *
6  * This program is free software: you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by the
8  * Free Software Foundation, either version 3 of the License, or (at your
9  * option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #include <wx/filename.h>
21 #include <wx/stdpaths.h>
22 #include <wx/string.h>
23 #include <wx/utils.h>
24 
25 #include <kiplatform/environment.h>
26 #include <paths.h>
27 #include <pgm_base.h>
29 #include <config.h>
30 
31 // lowercase or pretty case depending on platform
32 #if defined( __WXMAC__ ) || defined( __WXMSW__ )
33 #define KICAD_PATH_STR "KiCad"
34 #else
35 #define KICAD_PATH_STR "kicad"
36 #endif
37 
38 
39 void PATHS::getUserDocumentPath( wxFileName& aPath )
40 {
41  wxString envPath;
42 
43  if( wxGetEnv( wxT( "KICAD_DOCUMENTS_HOME" ), &envPath ) )
44  aPath.AssignDir( envPath );
45  else
46  aPath.AssignDir( KIPLATFORM::ENV::GetDocumentsPath() );
47 
48  aPath.AppendDir( KICAD_PATH_STR );
49  aPath.AppendDir( SETTINGS_MANAGER::GetSettingsVersion() );
50 }
51 
52 
54 {
55  wxFileName tmp;
56  getUserDocumentPath( tmp );
57 
58  tmp.AppendDir( "plugins" );
59 
60  return tmp.GetPath();
61 }
62 
63 
65 {
66  wxFileName tmp;
67 
68  tmp.AssignDir( PATHS::GetUserPluginsPath() );
69  tmp.AppendDir( "3d" );
70 
71  return tmp.GetPath();
72 }
73 
74 
76 {
77  wxFileName tmp;
78  getUserDocumentPath( tmp );
79 
80  tmp.AppendDir( "scripting" );
81 
82  return tmp.GetPath();
83 }
84 
85 
87 {
88  wxFileName tmp;
89  getUserDocumentPath( tmp );
90 
91  tmp.AppendDir( "template" );
92 
93  return tmp.GetPathWithSep();
94 }
95 
96 
98 {
99  wxFileName tmp;
100  getUserDocumentPath( tmp );
101 
102  tmp.AppendDir( "symbols" );
103 
104  return tmp.GetPath();
105 }
106 
107 
109 {
110  wxFileName tmp;
111  getUserDocumentPath( tmp );
112 
113  tmp.AppendDir( "footprints" );
114 
115  return tmp.GetPath();
116 }
117 
118 
120 {
121  wxFileName tmp;
122  getUserDocumentPath( tmp );
123 
124  tmp.AppendDir( "3dmodels" );
125 
126  return tmp.GetPath();
127 }
128 
130 {
131  wxFileName tmp;
132  getUserDocumentPath( tmp );
133 
134  tmp.AppendDir( "3rdparty" );
135 
136  return tmp.GetPath();
137 }
138 
140 {
141  wxFileName tmp;
142  getUserDocumentPath( tmp );
143 
144  tmp.AppendDir( "projects" );
145 
146  return tmp.GetPath();
147 }
148 
149 
150 wxString PATHS::GetStockDataPath( bool aRespectRunFromBuildDir )
151 {
152  wxString path;
153 
154  if( aRespectRunFromBuildDir && wxGetEnv( wxT( "KICAD_RUN_FROM_BUILD_DIR" ), nullptr ) )
155  {
156  // Allow debugging from build dir by placing relevant files/folders in the build root
157 #if defined( __WXMAC__ )
158  wxFileName fn = wxStandardPaths::Get().GetExecutablePath();
159 
160  fn.RemoveLastDir();
161  fn.RemoveLastDir();
162  fn.RemoveLastDir();
163  fn.RemoveLastDir();
164  path = fn.GetPath();
165 #elif defined( __WXMSW__ )
166  path = getWindowsKiCadRoot();
167 #else
168  path = Pgm().GetExecutablePath() + wxT( ".." );
169 #endif
170  }
171  else
172  {
173 #if defined( __WXMAC__ )
174  path = GetOSXKicadDataDir();
175 #elif defined( __WXMSW__ )
176  path = getWindowsKiCadRoot() + wxT( "share/kicad" );
177 #else
178  path = wxString::FromUTF8Unchecked( KICAD_DATA );
179 #endif
180  }
181 
182  return path;
183 }
184 
185 
187 {
188  wxString path;
189 
190 #if defined( __WXMAC__ )
191  path = GetOSXKicadMachineDataDir();
192 #elif defined( __WXMSW__ )
193  path = GetStockDataPath( false );
194 #else
195  path = wxString::FromUTF8Unchecked( KICAD_LIBRARY_DATA );
196 #endif
197 
198  return path;
199 }
200 
201 
203 {
204  wxString path;
205 
206  path = GetStockDataPath() + wxT( "/scripting" );
207 
208  return path;
209 }
210 
211 
213 {
214  wxFileName fn;
215 
216 #if defined( __WXMSW__ )
217  fn.AssignDir( Pgm().GetExecutablePath() );
218  fn.AppendDir( wxT( "scripting" ) );
219 #else
220  fn.AssignDir( PATHS::GetStockDataPath( false ) );
221 #endif
222  fn.AppendDir( wxT( "plugins" ) );
223 
224  return fn.GetPathWithSep();
225 }
226 
227 
229 {
230  wxFileName fn;
231 
232 #ifdef __WXGTK__
233  // KICAD_PLUGINDIR = CMAKE_INSTALL_FULL_LIBDIR path is the absolute path
234  // corresponding to the install path used for constructing KICAD_USER_PLUGIN
235  wxString tfname = wxString::FromUTF8Unchecked( KICAD_PLUGINDIR );
236  fn.Assign( tfname, "" );
237  fn.AppendDir( wxT( "kicad" ) );
238  fn.AppendDir( wxT( "plugins" ) );
239 #elif defined( __WXMAC__ )
240  fn.Assign( wxStandardPaths::Get().GetPluginsDir(), wxEmptyString );
241 #else
242  fn.AssignDir( Pgm().GetExecutablePath() );
243  fn.AppendDir( wxT( "plugins" ) );
244 #endif
245 
246  fn.AppendDir( "3d" );
247 
248  return fn.GetPathWithSep();
249 }
250 
251 
253 {
254  wxFileName fn;
255 
256  fn.AssignDir( PATHS::GetStockDataPath( false ) );
257  fn.AppendDir( wxT( "demos" ) );
258 
259  return fn.GetPathWithSep();
260 }
261 
262 
264 {
265  wxFileName tmp;
266 
267  tmp.AssignDir( KIPLATFORM::ENV::GetUserCachePath() );
268  tmp.AppendDir( KICAD_PATH_STR );
269  tmp.AppendDir( SETTINGS_MANAGER::GetSettingsVersion() );
270 
271  return tmp.GetPathWithSep();
272 }
273 
274 
276 {
277  wxString path;
278 
279 #if defined( __WXMAC__ )
280  path = GetOSXKicadDataDir();
281 #elif defined( __WXMSW__ )
282  path = getWindowsKiCadRoot() + "share/doc/kicad";
283 #else
284  path = wxString::FromUTF8Unchecked( KICAD_DOCS );
285 #endif
286 
287  return path;
288 }
289 
290 
291 bool PATHS::EnsurePathExists( const wxString& aPath )
292 {
293  wxFileName path( aPath );
294  if( !path.Normalize() )
295  {
296  return false;
297  }
298 
299  if( !wxFileName::DirExists( aPath ) )
300  {
301  if( !wxFileName::Mkdir( aPath, wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL ) )
302  {
303  return false;
304  }
305  }
306 
307  return true;
308 }
309 
310 
312 {
322 }
323 
324 
325 #ifdef __WXMAC__
326 wxString PATHS::GetOSXKicadUserDataDir()
327 {
328  // According to wxWidgets documentation for GetUserDataDir:
329  // Mac: ~/Library/Application Support/appname
330  wxFileName udir( wxStandardPaths::Get().GetUserDataDir(), wxEmptyString );
331 
332  // Since appname is different if started via launcher or standalone binary
333  // map all to "kicad" here
334  udir.RemoveLastDir();
335  udir.AppendDir( "kicad" );
336 
337  return udir.GetPath();
338 }
339 
340 
341 wxString PATHS::GetOSXKicadMachineDataDir()
342 {
343  return wxT( "/Library/Application Support/kicad" );
344 }
345 
346 
347 wxString PATHS::GetOSXKicadDataDir()
348 {
349  // According to wxWidgets documentation for GetDataDir:
350  // Mac: appname.app/Contents/SharedSupport bundle subdirectory
351  wxFileName ddir( wxStandardPaths::Get().GetDataDir(), wxEmptyString );
352 
353  // This must be mapped to main bundle for everything but kicad.app
354  const wxArrayString dirs = ddir.GetDirs();
355 
356  // Check if we are the main kicad binary. in this case, the path will be
357  // /path/to/bundlename.app/Contents/SharedSupport
358  // If we are an aux binary, the path will be something like
359  // /path/to/bundlename.app/Contents/Applications/<standalone>.app/Contents/SharedSupport
360  if( dirs.GetCount() >= 6 &&
361  dirs[dirs.GetCount() - 4] == wxT( "Applications" ) &&
362  dirs[dirs.GetCount() - 6].Lower().EndsWith( wxT( "app" ) ) )
363  {
364  ddir.RemoveLastDir();
365  ddir.RemoveLastDir();
366  ddir.RemoveLastDir();
367  ddir.RemoveLastDir();
368  ddir.AppendDir( wxT( "SharedSupport" ) );
369  }
370 
371  return ddir.GetPath();
372 }
373 #endif
374 
375 
376 #ifdef __WXWINDOWS__
377 wxString PATHS::getWindowsKiCadRoot()
378 {
379  wxFileName root( Pgm().GetExecutablePath() + "/../" );
380  root.Normalize();
381 
382  return root.GetPathWithSep();
383 }
384 #endif
static bool EnsurePathExists(const wxString &aPath)
Attempts to create a given path if it does not exist.
Definition: paths.cpp:291
static wxString GetDefaultUser3DModelsPath()
Gets the default path we point users to create projects.
Definition: paths.cpp:119
wxString GetDocumentsPath()
Retrieves the operating system specific path for a user's documents.
static wxString GetUserScriptingPath()
Gets the user path for python scripts.
Definition: paths.cpp:75
static wxString GetStockScriptingPath()
Gets the stock (install) scripting path.
Definition: paths.cpp:202
wxString GetUserCachePath()
Retrieves the operating system specific path for user's application cache.
static wxString GetStockPlugins3DPath()
Gets the stock (install) 3d viewer plugins path.
Definition: paths.cpp:228
static wxString GetUserCachePath()
Gets the stock (install) 3d viewer plugins path.
Definition: paths.cpp:263
static void getUserDocumentPath(wxFileName &aPath)
Gets the user path for the current kicad version which acts as the root for other user paths.
Definition: paths.cpp:39
KIWAY Kiway & Pgm(), KFCTL_STANDALONE
The global Program "get" accessor.
Definition: single_top.cpp:106
static wxString GetDocumentationPath()
Gets the documentation path, which is the base path for help files.
Definition: paths.cpp:275
static wxString GetStockDataPath(bool aRespectRunFromBuildDir=true)
Gets the stock (install) data path, which is the base path for things like scripting,...
Definition: paths.cpp:150
static wxString GetUserPlugins3DPath()
Gets the user path for 3d viewer plugin.
Definition: paths.cpp:64
static wxString GetUserPluginsPath()
Gets the user path for plugins.
Definition: paths.cpp:53
#define KICAD_PATH_STR
Definition: paths.cpp:35
static wxString GetUserTemplatesPath()
Gets the user path for custom templates.
Definition: paths.cpp:86
static void EnsureUserPathsExist()
Ensures/creates user default paths.
Definition: paths.cpp:311
static wxString GetDefaultUserProjectsPath()
Gets the default path we point users to create projects.
Definition: paths.cpp:139
static wxString GetDefaultUserSymbolsPath()
Gets the default path we point users to create projects.
Definition: paths.cpp:97
static wxString GetDefaultUserFootprintsPath()
Gets the default path we point users to create projects.
Definition: paths.cpp:108
see class PGM_BASE
static wxString GetStockDemosPath()
Gets the stock (install) demos path.
Definition: paths.cpp:252
static wxString GetStockEDALibraryPath()
Gets the stock (install) EDA library data path, which is the base path for templates,...
Definition: paths.cpp:186
static std::string GetSettingsVersion()
Parses the current KiCad build version and extracts the major and minor revision to use as the name o...
static wxString GetDefault3rdPartyPath()
Gets the default path for PCM packages.
Definition: paths.cpp:129
static wxString GetStockPluginsPath()
Gets the stock (install) plugins path.
Definition: paths.cpp:212