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 
129 
131 {
132  wxFileName tmp;
133  getUserDocumentPath( tmp );
134 
135  tmp.AppendDir( "projects" );
136 
137  return tmp.GetPath();
138 }
139 
140 
141 wxString PATHS::GetStockDataPath( bool aRespectRunFromBuildDir )
142 {
143  wxString path;
144 
145  if( aRespectRunFromBuildDir && wxGetEnv( wxT( "KICAD_RUN_FROM_BUILD_DIR" ), nullptr ) )
146  {
147  // Allow debugging from build dir by placing relevant files/folders in the build root
148  path = Pgm().GetExecutablePath() + wxT( ".." );
149  }
150  else
151  {
152 #if defined( __WXMAC__ )
153  path = GetOSXKicadDataDir();
154 #elif defined( __WXMSW__ )
155  path = Pgm().GetExecutablePath() + wxT( "../share/kicad" );
156 #else
157  path = wxString::FromUTF8Unchecked( KICAD_DATA );
158 #endif
159  }
160 
161  return path;
162 }
163 
164 
166 {
167  wxString path;
168 
169 #if defined( __WXMAC__ )
170  path = GetOSXKicadMachineDataDir();
171 #elif defined( __WXMSW__ )
172  path = GetStockDataPath( false );
173 #else
174  path = wxString::FromUTF8Unchecked( KICAD_LIBRARY_DATA );
175 #endif
176 
177  return path;
178 }
179 
180 
182 {
183  wxString path;
184 
185  path = GetStockDataPath() + wxT( "/scripting" );
186 
187  return path;
188 }
189 
190 
192 {
193  wxFileName fn;
194 
195 #if defined( __WXMSW__ )
196  fn.AssignDir( Pgm().GetExecutablePath() );
197  fn.AppendDir( wxT( "scripting" ) );
198 #else
199  fn.AssignDir( PATHS::GetStockDataPath( false ) );
200 #endif
201  fn.AppendDir( wxT( "plugins" ) );
202 
203  return fn.GetPathWithSep();
204 }
205 
206 
208 {
209  wxFileName fn;
210 
211 #ifdef __WXGTK__
212  // KICAD_PLUGINDIR = CMAKE_INSTALL_FULL_LIBDIR path is the absolute path
213  // corresponding to the install path used for constructing KICAD_USER_PLUGIN
214  wxString tfname = wxString::FromUTF8Unchecked( KICAD_PLUGINDIR );
215  fn.Assign( tfname, "" );
216  fn.AppendDir( wxT( "kicad" ) );
217  fn.AppendDir( wxT( "plugins" ) );
218 #elif defined( __WXMAC__ )
219  fn.Assign( wxStandardPaths::Get().GetPluginsDir(), wxEmptyString );
220 #else
221  fn.AssignDir( Pgm().GetExecutablePath() );
222  fn.AppendDir( wxT( "plugins" ) );
223 #endif
224 
225  fn.AppendDir( "3d" );
226 
227  return fn.GetPathWithSep();
228 }
229 
230 
232 {
233  wxFileName tmp;
234 
235  tmp.AssignDir( KIPLATFORM::ENV::GetUserCachePath() );
236  tmp.AppendDir( KICAD_PATH_STR );
237  tmp.AppendDir( SETTINGS_MANAGER::GetSettingsVersion() );
238 
239  return tmp.GetPathWithSep();
240 }
241 
242 
244 {
245  wxString path;
246 
247 #if defined( __WXMAC__ )
248  path = GetOSXKicadDataDir();
249 #elif defined( __WXMSW__ )
250  path = Pgm().GetExecutablePath() + "../share/doc/kicad";
251 #else
252  path = wxString::FromUTF8Unchecked( KICAD_DOCS );
253 #endif
254 
255  return path;
256 }
257 
258 
259 bool PATHS::EnsurePathExists( const wxString& aPath )
260 {
261  wxFileName path( aPath );
262  if( !path.Normalize() )
263  {
264  return false;
265  }
266 
267  if( !wxFileName::DirExists( aPath ) )
268  {
269  if( !wxFileName::Mkdir( aPath, wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL ) )
270  {
271  return false;
272  }
273  }
274 
275  return true;
276 }
277 
278 
280 {
289 }
290 
291 
292 #ifdef __WXMAC__
293 wxString PATHS::GetOSXKicadUserDataDir()
294 {
295  // According to wxWidgets documentation for GetUserDataDir:
296  // Mac: ~/Library/Application Support/appname
297  wxFileName udir( wxStandardPaths::Get().GetUserDataDir(), wxEmptyString );
298 
299  // Since appname is different if started via launcher or standalone binary
300  // map all to "kicad" here
301  udir.RemoveLastDir();
302  udir.AppendDir( "kicad" );
303 
304  return udir.GetPath();
305 }
306 
307 
308 wxString PATHS::GetOSXKicadMachineDataDir()
309 {
310  return wxT( "/Library/Application Support/kicad" );
311 }
312 
313 
314 wxString PATHS::GetOSXKicadDataDir()
315 {
316  // According to wxWidgets documentation for GetDataDir:
317  // Mac: appname.app/Contents/SharedSupport bundle subdirectory
318  wxFileName ddir( wxStandardPaths::Get().GetDataDir(), wxEmptyString );
319 
320  // This must be mapped to main bundle for everything but kicad.app
321  const wxArrayString dirs = ddir.GetDirs();
322  if( dirs[dirs.GetCount() - 3].Lower() != wxT( "kicad.app" ) )
323  {
324  // Bundle structure resp. current path is
325  // kicad.app/Contents/Applications/<standalone>.app/Contents/SharedSupport
326  // and will be mapped to
327  // kicad.app/Contents/SharedSupprt
328  ddir.RemoveLastDir();
329  ddir.RemoveLastDir();
330  ddir.RemoveLastDir();
331  ddir.RemoveLastDir();
332  ddir.AppendDir( wxT( "SharedSupport" ) );
333  }
334 
335  return ddir.GetPath();
336 }
337 #endif
static bool EnsurePathExists(const wxString &aPath)
Attempts to create a given path if it does not exist.
Definition: paths.cpp:259
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:181
wxString GetUserCachePath()
Retrieves the operating system specific path for user's application cache.
static wxString GetStockPlugins3DPath()
Gets the stock (install) 3d viewer pluginspath.
Definition: paths.cpp:207
static wxString GetUserCachePath()
Gets the stock (install) 3d viewer pluginspath.
Definition: paths.cpp:231
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:243
static wxString GetStockDataPath(bool aRespectRunFromBuildDir=true)
Gets the stock (install) data path, which is the base path for things like scripting,...
Definition: paths.cpp:141
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:279
static wxString GetDefaultUserProjectsPath()
Gets the default path we point users to create projects.
Definition: paths.cpp:130
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 GetStockEDALibraryPath()
Gets the stock (install) EDA library data path, which is the base path for templates,...
Definition: paths.cpp:165
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 GetStockPluginsPath()
Gets the stock (install) plugins path.
Definition: paths.cpp:191