1 /*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright (C) 2014-2021 KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, you may find one here:
18 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19 * or you may search the http://www.gnu.org website for the version 2 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
24 #include <wx/log.h>
25 #include <wx/stdpaths.h>
26
27 #include <confirm.h>
28 #include <core/arraydim.h>
29 #include <fp_lib_table.h>
30 #include <string_utils.h>
31 #include <kiface_ids.h>
32 #include <kiway.h>
33 #include <macros.h>
34 #include <project.h>
35 #include <project/project_file.h>
36 #include <trace_helpers.h>
37 #include <wildcards_and_files_ext.h>
38
39
PROJECT()40 PROJECT::PROJECT() :
41 m_readOnly( false ),
42 m_projectFile( nullptr ),
43 m_localSettings( nullptr )
44 {
45 memset( m_elems, 0, sizeof( m_elems ) );
46 }
47
48
ElemsClear()49 void PROJECT::ElemsClear()
50 {
51 // careful here, this should work, but the virtual destructor may not
52 // be in the same link image as PROJECT.
53 for( unsigned i = 0; i < arrayDim( m_elems ); ++i )
54 {
55 SetElem( ELEM_T( i ), nullptr );
56 }
57 }
58
59
~PROJECT()60 PROJECT::~PROJECT()
61 {
62 ElemsClear();
63 }
64
65
TextVarResolver(wxString * aToken) const66 bool PROJECT::TextVarResolver( wxString* aToken ) const
67 {
68 if( GetTextVars().count( *aToken ) > 0 )
69 {
70 *aToken = GetTextVars().at( *aToken );
71 return true;
72 }
73
74 return false;
75 }
76
77
GetTextVars() const78 std::map<wxString, wxString>& PROJECT::GetTextVars() const
79 {
80 return GetProjectFile().m_TextVars;
81 }
82
83
setProjectFullName(const wxString & aFullPathAndName)84 void PROJECT::setProjectFullName( const wxString& aFullPathAndName )
85 {
86 // Compare paths, rather than inodes, to be less surprising to the user.
87 // Create a temporary wxFileName to normalize the path
88 wxFileName candidate_path( aFullPathAndName );
89
90 // Edge transitions only. This is what clears the project
91 // data using the Clear() function.
92 if( m_project_name.GetFullPath() != candidate_path.GetFullPath() )
93 {
94 Clear(); // clear the data when the project changes.
95
96 wxLogTrace( tracePathsAndFiles, "%s: old:'%s' new:'%s'", __func__,
97 TO_UTF8( GetProjectFullName() ), TO_UTF8( aFullPathAndName ) );
98
99 m_project_name = aFullPathAndName;
100
101 wxASSERT( m_project_name.IsAbsolute() );
102
103 wxASSERT( m_project_name.GetExt() == ProjectFileExtension );
104
105 // until multiple projects are in play, set an environment variable for the
106 // the project pointer.
107 {
108 wxString path = m_project_name.GetPath();
109
110 wxSetEnv( PROJECT_VAR_NAME, path );
111 }
112 }
113 }
114
115
GetProjectFullName() const116 const wxString PROJECT::GetProjectFullName() const
117 {
118 return m_project_name.GetFullPath();
119 }
120
121
GetProjectPath() const122 const wxString PROJECT::GetProjectPath() const
123 {
124 return m_project_name.GetPathWithSep();
125 }
126
127
GetProjectName() const128 const wxString PROJECT::GetProjectName() const
129 {
130 return m_project_name.GetName();
131 }
132
133
IsNullProject() const134 bool PROJECT::IsNullProject() const
135 {
136 return m_project_name.GetName().IsEmpty();
137 }
138
139
SymbolLibTableName() const140 const wxString PROJECT::SymbolLibTableName() const
141 {
142 return libTableName( "sym-lib-table" );
143 }
144
145
FootprintLibTblName() const146 const wxString PROJECT::FootprintLibTblName() const
147 {
148 return libTableName( "fp-lib-table" );
149 }
150
151
libTableName(const wxString & aLibTableName) const152 const wxString PROJECT::libTableName( const wxString& aLibTableName ) const
153 {
154 wxFileName fn = GetProjectFullName();
155 wxString path = fn.GetPath();
156
157 // if there's no path to the project name, or the name as a whole is bogus or its not
158 // write-able then use a template file.
159 if( !fn.GetDirCount() || !fn.IsOk() || !wxFileName::IsDirWritable( path ) )
160 {
161 // return a template filename now.
162
163 // this next line is likely a problem now, since it relies on an
164 // application title which is no longer constant or known. This next line needs
165 // to be re-thought out.
166
167 #ifndef __WXMAC__
168 fn.AssignDir( wxStandardPaths::Get().GetUserConfigDir() );
169 #else
170 // don't pollute home folder, temp folder seems to be more appropriate
171 fn.AssignDir( wxStandardPaths::Get().GetTempDir() );
172 #endif
173
174 #if defined( __WINDOWS__ )
175 fn.AppendDir( wxT( "kicad" ) );
176 #endif
177
178 /*
179 * The library table name used when no project file is passed to the appropriate
180 * code. This is used temporarily to store the project specific library table
181 * until the project file being edited is saved. It is then moved to the correct
182 * file in the folder where the project file is saved.
183 */
184 fn.SetName( "prj-" + aLibTableName );
185 }
186 else // normal path.
187 {
188 fn.SetName( aLibTableName );
189 }
190
191 fn.ClearExt();
192
193 return fn.GetFullPath();
194 }
195
196
GetSheetName(const KIID & aSheetID)197 const wxString PROJECT::GetSheetName( const KIID& aSheetID )
198 {
199 if( m_sheetNames.empty() )
200 {
201 for( auto pair : GetProjectFile().GetSheets() )
202 m_sheetNames[pair.first] = pair.second;
203 }
204
205 if( m_sheetNames.count( aSheetID ) )
206 return m_sheetNames.at( aSheetID );
207 else
208 return aSheetID.AsString();
209 }
210
211
SetRString(RSTRING_T aIndex,const wxString & aString)212 void PROJECT::SetRString( RSTRING_T aIndex, const wxString& aString )
213 {
214 unsigned ndx = unsigned( aIndex );
215
216 if( ndx < arrayDim( m_rstrings ) )
217 {
218 m_rstrings[ndx] = aString;
219 }
220 else
221 {
222 wxASSERT( 0 ); // bad index
223 }
224 }
225
226
GetRString(RSTRING_T aIndex)227 const wxString& PROJECT::GetRString( RSTRING_T aIndex )
228 {
229 unsigned ndx = unsigned( aIndex );
230
231 if( ndx < arrayDim( m_rstrings ) )
232 {
233 return m_rstrings[ndx];
234 }
235 else
236 {
237 static wxString no_cookie_for_you;
238
239 wxASSERT( 0 ); // bad index
240
241 return no_cookie_for_you;
242 }
243 }
244
245
GetElem(ELEM_T aIndex)246 PROJECT::_ELEM* PROJECT::GetElem( ELEM_T aIndex )
247 {
248 // This is virtual, so implement it out of line
249 if( unsigned( aIndex ) < arrayDim( m_elems ) )
250 {
251 return m_elems[aIndex];
252 }
253
254 return nullptr;
255 }
256
257
SetElem(ELEM_T aIndex,_ELEM * aElem)258 void PROJECT::SetElem( ELEM_T aIndex, _ELEM* aElem )
259 {
260 // This is virtual, so implement it out of line
261 if( unsigned( aIndex ) < arrayDim( m_elems ) )
262 {
263 delete m_elems[aIndex];
264 m_elems[aIndex] = aElem;
265 }
266 }
267
268
AbsolutePath(const wxString & aFileName) const269 const wxString PROJECT::AbsolutePath( const wxString& aFileName ) const
270 {
271 wxFileName fn = aFileName;
272
273 if( !fn.IsAbsolute() )
274 {
275 wxString pro_dir = wxPathOnly( GetProjectFullName() );
276 fn.Normalize( wxPATH_NORM_ALL, pro_dir );
277 }
278
279 return fn.GetFullPath();
280 }
281
282
PcbFootprintLibs(KIWAY & aKiway)283 FP_LIB_TABLE* PROJECT::PcbFootprintLibs( KIWAY& aKiway )
284 {
285 // This is a lazy loading function, it loads the project specific table when
286 // that table is asked for, not before.
287
288 FP_LIB_TABLE* tbl = (FP_LIB_TABLE*) GetElem( ELEM_FPTBL );
289
290 if( tbl )
291 {
292 wxASSERT( tbl->Type() == FP_LIB_TABLE_T );
293 }
294 else
295 {
296 try
297 {
298 // Build a new project specific FP_LIB_TABLE with the global table as a fallback.
299 // ~FP_LIB_TABLE() will not touch the fallback table, so multiple projects may
300 // stack this way, all using the same global fallback table.
301 KIFACE* kiface = aKiway.KiFACE( KIWAY::FACE_PCB );
302
303 tbl = (FP_LIB_TABLE*) kiface->IfaceOrAddress( KIFACE_NEW_FOOTPRINT_TABLE );
304 tbl->Load( FootprintLibTblName() );
305
306 SetElem( ELEM_FPTBL, tbl );
307 }
308 catch( const IO_ERROR& ioe )
309 {
310 DisplayErrorMessage( nullptr, _( "Error loading project footprint library table." ),
311 ioe.What() );
312 }
313 catch( ... )
314 {
315 DisplayErrorMessage( nullptr, _( "Error loading project footprint library table." ) );
316 }
317 }
318
319 return tbl;
320 }
321