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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
|
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2014 KiCad Developers, see CHANGELOG.TXT for contributors.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, you may find one here:
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* or you may search the http://www.gnu.org website for the version 2 license,
* or you may write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include <wx/stdpaths.h>
#include <fctsys.h>
#include <macros.h>
#include <pgm_base.h>
#include <project.h>
#include <common.h> // NAMELESS_PROJECT
#include <confirm.h>
#include <kicad_string.h>
#include <config_params.h>
#include <wildcards_and_files_ext.h>
PROJECT::PROJECT()
{
memset( m_elems, 0, sizeof(m_elems) );
}
void PROJECT::ElemsClear()
{
DBG( printf( "%s: clearing all _ELEMS for project %s\n", __func__, TO_UTF8( GetProjectFullName() ) );)
// careful here, this should work, but the virtual destructor may not
// be in the same link image as PROJECT.
for( unsigned i = 0; i < DIM( m_elems ); ++i )
{
SetElem( ELEM_T( i ), NULL );
}
}
PROJECT::~PROJECT()
{
ElemsClear();
}
void PROJECT::SetProjectFullName( const wxString& aFullPathAndName )
{
// Compare paths, rather than inodes, to be less surprising to the user.
// Create a temporary wxFileName to normalize the path
wxFileName candidate_path( aFullPathAndName );
// Edge transitions only. This is what clears the project
// data using the Clear() function.
if( m_project_name.GetFullPath() != candidate_path.GetFullPath() )
{
Clear(); // clear the data when the project changes.
DBG(printf( "%s: old:'%s' new:'%s'\n", __func__, TO_UTF8( GetProjectFullName() ), TO_UTF8( aFullPathAndName ) );)
m_project_name = aFullPathAndName;
wxASSERT( m_project_name.IsAbsolute() );
wxASSERT( m_project_name.GetExt() == ProjectFileExtension );
// until multiple projects are in play, set an environment variable for the
// the project pointer.
{
wxString path = m_project_name.GetPath();
wxSetEnv( PROJECT_VAR_NAME, path );
}
}
}
const wxString PROJECT::GetProjectFullName() const
{
return m_project_name.GetFullPath();
}
const wxString PROJECT::GetProjectPath() const
{
return m_project_name.GetPathWithSep();
}
const wxString PROJECT::GetProjectName() const
{
return m_project_name.GetName();
}
const wxString PROJECT::FootprintLibTblName() const
{
wxFileName fn = GetProjectFullName();
wxString path = fn.GetPath();
// DBG(printf( "path:'%s' fn:'%s'\n", TO_UTF8(path), TO_UTF8(fn.GetFullPath()) );)
// if there's no path to the project name, or the name as a whole is bogus or its not
// write-able then use a template file.
if( !fn.GetDirCount() || !fn.IsOk() || !wxFileName::IsDirWritable( path ) )
{
// return a template filename now.
// this next line is likely a problem now, since it relies on an
// application title which is no longer constant or known. This next line needs
// to be re-thought out.
#ifndef __WXMAC__
fn.AssignDir( wxStandardPaths::Get().GetUserConfigDir() );
#else
// don't pollute home folder, temp folder seems to be more appropriate
fn.AssignDir( wxStandardPaths::Get().GetTempDir() );
#endif
#if defined( __WINDOWS__ )
fn.AppendDir( wxT( "kicad" ) );
#endif
/*
The footprint library table name used when no project file is passed
to Pcbnew or CvPcb. This is used temporarily to store the project
specific library table until the project file being edited is saved.
It is then moved to the file fp-lib-table in the folder where the
project file is saved.
*/
fn.SetName( wxT( "prj-fp-lib-table" ) );
}
else // normal path.
{
fn.SetName( wxT( "fp-lib-table" ) );
}
fn.ClearExt();
return fn.GetFullPath();
}
void PROJECT::SetRString( RSTRING_T aIndex, const wxString& aString )
{
unsigned ndx = unsigned( aIndex );
if( ndx < DIM( m_rstrings ) )
{
m_rstrings[ndx] = aString;
}
else
{
wxASSERT( 0 ); // bad index
}
}
const wxString& PROJECT::GetRString( RSTRING_T aIndex )
{
unsigned ndx = unsigned( aIndex );
if( ndx < DIM( m_rstrings ) )
{
return m_rstrings[ndx];
}
else
{
static wxString no_cookie_for_you;
wxASSERT( 0 ); // bad index
return no_cookie_for_you;
}
}
PROJECT::_ELEM* PROJECT::GetElem( ELEM_T aIndex )
{
// This is virtual, so implement it out of line
if( unsigned( aIndex ) < DIM( m_elems ) )
{
return m_elems[aIndex];
}
return NULL;
}
void PROJECT::SetElem( ELEM_T aIndex, _ELEM* aElem )
{
// This is virtual, so implement it out of line
if( unsigned( aIndex ) < DIM( m_elems ) )
{
#if defined(DEBUG) && 0
if( aIndex == ELEM_SCH_PART_LIBS )
{
printf( "%s: &m_elems[%i]:%p aElem:%p\n", __func__, aIndex, &m_elems[aIndex], aElem );
}
#endif
delete m_elems[aIndex];
m_elems[aIndex] = aElem;
}
}
static bool copy_pro_file_template( const SEARCH_STACK& aSearchS, const wxString& aDestination )
{
if( aDestination.IsEmpty() )
{
DBG( printf( "%s: destination is empty.\n", __func__ );)
return false;
}
wxString templateFile = wxT( "kicad." ) + ProjectFileExtension;
wxString kicad_pro_template = aSearchS.FindValidPath( templateFile );
if( !kicad_pro_template )
{
DBG( printf( "%s: template file '%s' not found using search paths.\n", __func__, TO_UTF8( templateFile ) );)
wxFileName templ( wxStandardPaths::Get().GetDocumentsDir(),
wxT( "kicad" ), ProjectFileExtension );
if( !templ.IsFileReadable() )
{
wxString msg = wxString::Format( _(
"Unable to find '%s' template config file." ),
GetChars( templateFile ) );
DisplayError( NULL, msg );
return false;
}
kicad_pro_template = templ.GetFullPath();
}
DBG( printf( "%s: using template file '%s' as project file.\n", __func__, TO_UTF8( kicad_pro_template ) );)
// Verify aDestination can be created. if this is not the case, wxCopyFile
// will generate a crappy log error message, and we *do not want* this kind
// of stupid message
wxFileName fn( aDestination );
bool success = true;
if( fn.IsOk() && fn.IsDirWritable() )
success = wxCopyFile( kicad_pro_template, aDestination );
else
{
wxLogMessage( _( "Cannot create prj file '%s' (Directory not writable)" ),
GetChars( aDestination) );
success = false;
}
return success;
}
wxConfigBase* PROJECT::configCreate( const SEARCH_STACK& aSList,
const wxString& aGroupName, const wxString& aProjectFileName )
{
wxConfigBase* cfg = 0;
wxString cur_pro_fn = !aProjectFileName ? GetProjectFullName() : aProjectFileName;
if( wxFileName( cur_pro_fn ).IsFileReadable() )
{
// Note: currently, aGroupName is not used.
// Previoulsy, the version of aGroupName was tested, but it
// was useless, and if the version is important,
// this is not the right place here, because configCreate does know anything
// about info stored in this config file.
cfg = new wxFileConfig( wxEmptyString, wxEmptyString, cur_pro_fn, wxEmptyString );
return cfg;
}
// No suitable pro file was found, either does not exist, or not readable.
// Use the template kicad.pro file. Find it by using caller's SEARCH_STACK.
copy_pro_file_template( aSList, cur_pro_fn );
cfg = new wxFileConfig( wxEmptyString, wxEmptyString, cur_pro_fn, wxEmptyString );
return cfg;
}
void PROJECT::ConfigSave( const SEARCH_STACK& aSList, const wxString& aGroupName,
const PARAM_CFG_ARRAY& aParams, const wxString& aFileName )
{
std::auto_ptr<wxConfigBase> cfg( configCreate( aSList, aGroupName, aFileName ) );
if( !cfg.get() )
{
// could not find template
return;
}
cfg->SetPath( wxT( "/" ) );
cfg->Write( wxT( "update" ), DateAndTime() );
// @todo: pass in aLastClient wxString:
cfg->Write( wxT( "last_client" ), Pgm().App().GetAppName() );
// Save parameters
cfg->DeleteGroup( aGroupName ); // Erase all data
cfg->Flush();
cfg->SetPath( aGroupName );
cfg->Write( wxT( "version" ), CONFIG_VERSION );
cfg->SetPath( wxT( "/" ) );
wxConfigSaveParams( cfg.get(), aParams, aGroupName );
cfg->SetPath( wxT( "/" ) );
// cfg is deleted here by std::auto_ptr, that saves the *.pro file to disk
}
bool PROJECT::ConfigLoad( const SEARCH_STACK& aSList, const wxString& aGroupName,
const PARAM_CFG_ARRAY& aParams, const wxString& aForeignProjectFileName )
{
std::auto_ptr<wxConfigBase> cfg( configCreate( aSList, aGroupName, aForeignProjectFileName ) );
if( !cfg.get() )
{
// could not find template
return false;
}
cfg->SetPath( wxCONFIG_PATH_SEPARATOR );
wxString timestamp = cfg->Read( wxT( "update" ) );
m_pro_date_and_time = timestamp;
// We do not want expansion of env var values when reading our project config file
bool state = cfg.get()->IsExpandingEnvVars();
cfg.get()->SetExpandEnvVars( false );
wxConfigLoadParams( cfg.get(), aParams, aGroupName );
cfg.get()->SetExpandEnvVars( state );
return true;
}
const wxString PROJECT::AbsolutePath( const wxString& aFileName ) const
{
wxFileName fn = aFileName;
if( !fn.IsAbsolute() )
{
wxString pro_dir = wxPathOnly( GetProjectFullName() );
fn.Normalize( wxPATH_NORM_ALL, pro_dir );
}
return fn.GetFullPath();
}
|