KiCad PCB EDA Suite
Loading...
Searching...
No Matches
common_settings.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) 2020 Jon Evans <[email protected]>
5 * Copyright (C) 2020-2023 KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software: you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation, either version 3 of the License, or (at your
10 * option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21#include <set>
22#include <fstream>
23#include <sstream>
24
25#include <env_vars.h>
26#include <paths.h>
27#include <search_stack.h>
31#include <settings/parameters.h>
32#include <systemdirsappend.h>
33#include <trace_helpers.h>
34#include <wx/config.h>
35#include <wx/log.h>
36#include <wx/regex.h>
37
38
40const wxRegEx versionedEnvVarRegex( wxS( "KICAD[0-9]+_[A-Z0-9_]+(_DIR)?" ) );
41
43const int commonSchemaVersion = 3;
44
47 m_Appearance(),
48 m_Backup(),
49 m_Env(),
50 m_Input(),
51 m_Graphics(),
52 m_Session(),
53 m_System(),
54 m_DoNotShowAgain(),
55 m_NetclassPanel(),
56 m_PackageManager(),
57 m_Api()
58{
59 /*
60 * Automatic dark mode detection works fine on Mac.
61 */
62#if defined( __WXGTK__ ) || defined( __WXMSW__ )
63 m_params.emplace_back( new PARAM_ENUM<ICON_THEME>( "appearance.icon_theme",
64 &m_Appearance.icon_theme, ICON_THEME::AUTO, ICON_THEME::LIGHT, ICON_THEME::AUTO ) );
65#else
66 m_Appearance.icon_theme = ICON_THEME::AUTO;
67#endif
68
69 /*
70 * Automatic canvas scaling works fine on all supported platforms, so it's no longer exposed as
71 * a configuration option.
72 */
74
75 /*
76 * Menu icons are off by default on OSX and on for all other platforms.
77 */
78#ifdef __WXMAC__
79 m_params.emplace_back( new PARAM<bool>( "appearance.use_icons_in_menus",
81#else
82 m_params.emplace_back( new PARAM<bool>( "appearance.use_icons_in_menus",
84#endif
85
86 /*
87 * Font scaling hacks are only needed on GTK under wxWidgets 3.0.
88 */
90
91 m_params.emplace_back( new PARAM<bool>( "appearance.show_scrollbars",
92 &m_Appearance.show_scrollbars, false ) );
93
94 m_params.emplace_back( new PARAM<double>( "appearance.hicontrast_dimming_factor",
96
97 m_params.emplace_back( new PARAM<int>( "appearance.text_editor_zoom",
99
100 m_params.emplace_back( new PARAM<int>( "appearance.toolbar_icon_size",
101 &m_Appearance.toolbar_icon_size, 24, 16, 64 ) );
102
103 m_params.emplace_back( new PARAM<bool>( "appearance.grid_striping",
104 &m_Appearance.grid_striping, false ) );
105
106 m_params.emplace_back( new PARAM<bool>( "auto_backup.enabled", &m_Backup.enabled, true ) );
107
108 m_params.emplace_back( new PARAM<bool>( "auto_backup.backup_on_autosave",
109 &m_Backup.backup_on_autosave, false ) );
110
111 m_params.emplace_back( new PARAM<int>( "auto_backup.limit_total_files",
113
114 m_params.emplace_back( new PARAM<unsigned long long>( "auto_backup.limit_total_size",
115 &m_Backup.limit_total_size, 104857600 ) );
116
117 m_params.emplace_back( new PARAM<int>( "auto_backup.limit_daily_files",
119
120 m_params.emplace_back( new PARAM<int>( "auto_backup.min_interval",
121 &m_Backup.min_interval, 300 ) );
122
123 m_params.emplace_back( new PARAM_LAMBDA<nlohmann::json>( "environment.vars",
124 [&]() -> nlohmann::json
125 {
126 nlohmann::json ret = {};
127
128 for( const std::pair<wxString, ENV_VAR_ITEM> entry : m_Env.vars )
129 {
130 const ENV_VAR_ITEM& var = entry.second;
131
132 wxASSERT( entry.first == var.GetKey() );
133
134 // Default values are never persisted
135 if( var.IsDefault() )
136 {
137 wxLogTrace( traceEnvVars,
138 wxS( "COMMON_SETTINGS: Env var %s skipping save (default)" ),
139 var.GetKey() );
140 continue;
141 }
142
143 wxString value = var.GetValue();
144
145 // Vars that existed in JSON are persisted, but if they were overridden
146 // externally, we persist the old value (i.e. the one that was loaded from JSON)
147 if( var.GetDefinedExternally() )
148 {
149 if( var.GetDefinedInSettings() )
150 {
151 wxLogTrace( traceEnvVars,
152 wxS( "COMMON_SETTINGS: Env var %s was overridden "
153 "externally, saving previously-loaded value %s" ),
154 var.GetKey(), var.GetSettingsValue() );
155 value = var.GetSettingsValue();
156 }
157 else
158 {
159 wxLogTrace( traceEnvVars,
160 wxS( "COMMON_SETTINGS: Env var %s skipping save "
161 "(external)" ),
162 var.GetKey() );
163 continue;
164 }
165 }
166
167 wxLogTrace( traceEnvVars,
168 wxS( "COMMON_SETTINGS: Saving env var %s = %s" ),
169 var.GetKey(), value);
170
171 std::string key( var.GetKey().ToUTF8() );
172 ret[ std::move( key ) ] = value;
173 }
174
175 return ret;
176 },
177 [&]( const nlohmann::json& aJson )
178 {
179 if( !aJson.is_object() )
180 return;
181
182 for( const auto& entry : aJson.items() )
183 {
184 wxString key = wxString( entry.key().c_str(), wxConvUTF8 );
185 wxString val = entry.value().get<wxString>();
186
187 if( m_Env.vars.count( key ) )
188 {
189 if( m_Env.vars[key].GetDefinedExternally() )
190 {
191 wxLogTrace( traceEnvVars,
192 wxS( "COMMON_SETTINGS: %s is defined externally" ),
193 key );
194 m_Env.vars[key].SetDefinedInSettings();
195 m_Env.vars[key].SetSettingsValue( val );
196 continue;
197 }
198 else
199 {
200 wxLogTrace( traceEnvVars,
201 wxS( "COMMON_SETTINGS: Updating %s: %s -> %s"),
202 key, m_Env.vars[key].GetValue(), val );
203 m_Env.vars[key].SetValue( val );
204 }
205 }
206 else
207 {
208 wxLogTrace( traceEnvVars,
209 wxS( "COMMON_SETTINGS: Loaded new var: %s = %s" ),
210 key, val );
211 m_Env.vars[key] = ENV_VAR_ITEM( key, val );
212 }
213
214 m_Env.vars[key].SetDefinedInSettings();
215 m_Env.vars[key].SetSettingsValue( val );
216 }
217 },
218 {} ) );
219
220 m_params.emplace_back( new PARAM<bool>( "input.focus_follow_sch_pcb",
221 &m_Input.focus_follow_sch_pcb, false ) );
222
223 m_params.emplace_back( new PARAM<bool>( "input.auto_pan", &m_Input.auto_pan, false ) );
224
225 m_params.emplace_back( new PARAM<int>( "input.auto_pan_acceleration",
226 &m_Input.auto_pan_acceleration, 5 ) );
227
228 m_params.emplace_back( new PARAM<bool>( "input.center_on_zoom",
229 &m_Input.center_on_zoom, true ) );
230
231 m_params.emplace_back( new PARAM<bool>( "input.immediate_actions",
232 &m_Input.immediate_actions, true ) );
233
234 m_params.emplace_back( new PARAM<bool>( "input.warp_mouse_on_move",
235 &m_Input.warp_mouse_on_move, true ) );
236
237 m_params.emplace_back( new PARAM<bool>( "input.horizontal_pan",
238 &m_Input.horizontal_pan, false ) );
239
240 m_params.emplace_back( new PARAM<bool>( "input.hotkey_feedback",
241 &m_Input.hotkey_feedback, true ) );
242
243 m_params.emplace_back( new PARAM<bool>( "input.zoom_acceleration",
244 &m_Input.zoom_acceleration, false ) );
245
246#ifdef __WXMAC__
247 int default_zoom_speed = 5;
248#else
249 int default_zoom_speed = 1;
250#endif
251
252 m_params.emplace_back( new PARAM<int>( "input.zoom_speed",
253 &m_Input.zoom_speed, default_zoom_speed ) );
254
255 m_params.emplace_back( new PARAM<bool>( "input.zoom_speed_auto",
256 &m_Input.zoom_speed_auto, true ) );
257
258 m_params.emplace_back( new PARAM<int>( "input.scroll_modifier_zoom",
259 &m_Input.scroll_modifier_zoom, 0 ) );
260
261 m_params.emplace_back( new PARAM<int>( "input.scroll_modifier_pan_h",
262 &m_Input.scroll_modifier_pan_h, WXK_CONTROL ) );
263
264 m_params.emplace_back( new PARAM<int>( "input.scroll_modifier_pan_v",
265 &m_Input.scroll_modifier_pan_v, WXK_SHIFT ) );
266
267 m_params.emplace_back( new PARAM<bool>( "input.reverse_scroll_pan_h",
268 &m_Input.reverse_scroll_pan_h, false ) );
269
270 m_params.emplace_back( new PARAM_ENUM<MOUSE_DRAG_ACTION>( "input.mouse_left",
273
274 m_params.emplace_back( new PARAM_ENUM<MOUSE_DRAG_ACTION>( "input.mouse_middle",
277
278 m_params.emplace_back( new PARAM_ENUM<MOUSE_DRAG_ACTION>( "input.mouse_right",
281
282 m_params.emplace_back( new PARAM<int>( "graphics.opengl_antialiasing_mode",
283 &m_Graphics.opengl_aa_mode, 1, 0, 2 ) );
284
285 m_params.emplace_back( new PARAM<int>( "graphics.cairo_antialiasing_mode",
286 &m_Graphics.cairo_aa_mode, 0, 0, 2 ) );
287
288 m_params.emplace_back( new PARAM<int>( "system.autosave_interval",
289 &m_System.autosave_interval, 600 ) );
290
291#ifdef __WXMAC__
292 m_params.emplace_back( new PARAM<wxString>( "system.text_editor",
293 &m_System.text_editor, wxS( "/usr/bin/open -e" ) ) );
294#else
295 m_params.emplace_back( new PARAM<wxString>( "system.text_editor",
296 &m_System.text_editor, wxS( "" ) ) );
297#endif
298
299 m_params.emplace_back( new PARAM<int>( "system.file_history_size",
300 &m_System.file_history_size, 9 ) );
301
302 m_params.emplace_back( new PARAM<wxString>( "system.language",
303 &m_System.language, wxS( "Default" ) ) );
304
305 m_params.emplace_back( new PARAM<wxString>( "system.pdf_viewer_name",
306 &m_System.pdf_viewer_name, wxS( "" ) ) );
307
308 m_params.emplace_back( new PARAM<bool>( "system.use_system_pdf_viewer",
309 &m_System.use_system_pdf_viewer, true ) );
310
311 m_params.emplace_back( new PARAM<wxString>( "system.working_dir",
312 &m_System.working_dir, wxS( "" ) ) );
313
314 m_params.emplace_back( new PARAM<int>( "system.clear_3d_cache_interval",
315 &m_System.clear_3d_cache_interval, 30 ) );
316
317 m_params.emplace_back( new PARAM<bool>( "do_not_show_again.zone_fill_warning",
318 &m_DoNotShowAgain.zone_fill_warning, false ) );
319
320 m_params.emplace_back( new PARAM<bool>( "do_not_show_again.env_var_overwrite_warning",
321 &m_DoNotShowAgain.env_var_overwrite_warning, false ) );
322
323 m_params.emplace_back( new PARAM<bool>( "do_not_show_again.scaled_3d_models_warning",
324 &m_DoNotShowAgain.scaled_3d_models_warning, false ) );
325
326 m_params.emplace_back( new PARAM<bool>( "do_not_show_again.data_collection_prompt",
327 &m_DoNotShowAgain.data_collection_prompt, false ) );
328
329 m_params.emplace_back( new PARAM<bool>( "do_not_show_again.update_check_prompt",
330 &m_DoNotShowAgain.update_check_prompt, false ) );
331
332 m_params.emplace_back( new PARAM<bool>( "session.remember_open_files",
333 &m_Session.remember_open_files, false ) );
334
335 m_params.emplace_back( new PARAM_LIST<wxString>( "session.pinned_symbol_libs",
336 &m_Session.pinned_symbol_libs, {} ) );
337
338 m_params.emplace_back( new PARAM_LIST<wxString>( "session.pinned_fp_libs",
339 &m_Session.pinned_fp_libs, {} ) );
340
341 m_params.emplace_back( new PARAM<int>( "netclass_panel.sash_pos",
342 &m_NetclassPanel.sash_pos, 160 ) );
343
344 m_params.emplace_back( new PARAM<wxString>( "netclass_panel.eeschema_shown_columns",
345 &m_NetclassPanel.eeschema_visible_columns, "0 10 11 12 13" ) );
346
347 m_params.emplace_back( new PARAM<wxString>( "netclass_panel.pcbnew_shown_columns",
348 &m_NetclassPanel.pcbnew_visible_columns, "0 1 2 3 4 5 6 7 8 9" ) );
349
350 m_params.emplace_back( new PARAM<int>( "package_manager.sash_pos",
351 &m_PackageManager.sash_pos, 380 ) );
352
353 m_params.emplace_back( new PARAM_LAMBDA<nlohmann::json>( "git.repositories",
354 [&]() -> nlohmann::json
355 {
356 nlohmann::json ret = {};
357
358 for( const GIT_REPOSITORY& repo : m_Git.repositories )
359 {
360 nlohmann::json repoJson = {};
361
362 repoJson["name"] = repo.name;
363 repoJson["path"] = repo.path;
364 repoJson["authType"] = repo.authType;
365 repoJson["username"] = repo.username;
366 repoJson["ssh_path"] = repo.ssh_path;
367 repoJson["active"] = repo.active;
368
369 ret.push_back( repoJson );
370 }
371
372 return ret;
373 },
374 [&]( const nlohmann::json& aJson )
375 {
376 if( !aJson.is_array() )
377 return;
378
379 m_Git.repositories.clear();
380
381 for( const auto& repoJson : aJson )
382 {
383 GIT_REPOSITORY repo;
384
385 repo.name = repoJson["name"].get<wxString>();
386 repo.path = repoJson["path"].get<wxString>();
387 repo.authType = repoJson["authType"].get<wxString>();
388 repo.username = repoJson["username"].get<wxString>();
389 repo.ssh_path = repoJson["ssh_path"].get<wxString>();
390 repo.active = repoJson["active"].get<bool>();
391 repo.checkValid = true;
392
393 m_Git.repositories.push_back( repo );
394 }
395 },
396 {} ) );
397
398 m_params.emplace_back( new PARAM<wxString>( "git.authorName",
399 &m_Git.authorName, wxS( "" ) ) );
400
401 m_params.emplace_back( new PARAM<wxString>( "git.authorEmail",
402 &m_Git.authorEmail, wxS( "" ) ) );
403
404 m_params.emplace_back( new PARAM<bool>( "git.useDefaultAuthor",
405 &m_Git.useDefaultAuthor, true ) );
406
407 m_params.emplace_back( new PARAM<wxString>( "api.interpreter_path",
408 &m_Api.python_interpreter, wxS( "" ) ) );
409
410 m_params.emplace_back( new PARAM<bool>( "api.enable_server",
411 &m_Api.enable_server, false ) );
412
413 registerMigration( 0, 1, std::bind( &COMMON_SETTINGS::migrateSchema0to1, this ) );
414 registerMigration( 1, 2, std::bind( &COMMON_SETTINGS::migrateSchema1to2, this ) );
415 registerMigration( 2, 3, std::bind( &COMMON_SETTINGS::migrateSchema2to3, this ) );
416}
417
418
420{
427 nlohmann::json::json_pointer mwp_pointer( "/input/mousewheel_pan"_json_pointer );
428
429 bool mwp = false;
430
431 try
432 {
433 mwp = m_internals->at( mwp_pointer );
434 m_internals->At( "input" ).erase( "mousewheel_pan" );
435 }
436 catch( ... )
437 {
438 wxLogTrace( traceSettings, wxT( "COMMON_SETTINGS::Migrate 0->1: mousewheel_pan not found" ) );
439 }
440
441 if( mwp )
442 {
443 ( *m_internals )[nlohmann::json::json_pointer( "/input/horizontal_pan" )] = true;
444
445 ( *m_internals )[nlohmann::json::json_pointer( "/input/scroll_modifier_pan_h" )] = WXK_SHIFT;
446 ( *m_internals )[nlohmann::json::json_pointer( "/input/scroll_modifier_pan_v" )] = 0;
447 ( *m_internals )[nlohmann::json::json_pointer( "/input/scroll_modifier_zoom" )] = WXK_CONTROL;
448 }
449 else
450 {
451 ( *m_internals )[nlohmann::json::json_pointer( "/input/horizontal_pan" )] = false;
452
453 ( *m_internals )[nlohmann::json::json_pointer( "/input/scroll_modifier_pan_h" )] = WXK_CONTROL;
454 ( *m_internals )[nlohmann::json::json_pointer( "/input/scroll_modifier_pan_v" )] = WXK_SHIFT;
455 ( *m_internals )[nlohmann::json::json_pointer( "/input/scroll_modifier_zoom" )] = 0;
456 }
457
458 return true;
459}
460
461
463{
464 nlohmann::json::json_pointer v1_pointer( "/input/prefer_select_to_drag"_json_pointer );
465
466 bool prefer_selection = false;
467
468 try
469 {
470 prefer_selection = m_internals->at( v1_pointer );
471 m_internals->at( nlohmann::json::json_pointer( "/input"_json_pointer ) ).erase( "prefer_select_to_drag" );
472 }
473 catch( ... )
474 {
475 wxLogTrace( traceSettings, wxT( "COMMON_SETTINGS::Migrate 1->2: prefer_select_to_drag not found" ) );
476 }
477
478 if( prefer_selection )
479 ( *m_internals )[nlohmann::json::json_pointer( "/input/mouse_left" )] = MOUSE_DRAG_ACTION::SELECT;
480 else
481 ( *m_internals )[nlohmann::json::json_pointer( "/input/mouse_left" )] = MOUSE_DRAG_ACTION::DRAG_ANY;
482
483 return true;
484}
485
486
488{
489 wxFileName cfgpath;
490 cfgpath.AssignDir( PATHS::GetUserSettingsPath() );
491 cfgpath.AppendDir( wxT( "3d" ) );
492 cfgpath.SetFullName( wxS( "3Dresolver.cfg" ) );
493 cfgpath.MakeAbsolute();
494
495 std::vector<LEGACY_3D_SEARCH_PATH> legacyPaths;
496 readLegacy3DResolverCfg( cfgpath.GetFullPath(), legacyPaths );
497
498 // env variables have a limited allowed character set for names
499 wxRegEx nonValidCharsRegex( wxS( "[^A-Z0-9_]+" ), wxRE_ADVANCED );
500
501 for( const LEGACY_3D_SEARCH_PATH& path : legacyPaths )
502 {
503 wxString key = path.m_Alias;
504 const wxString& val = path.m_Pathvar;
505
506 // The 3d alias config didnt use the same naming restrictions as real env variables
507 // We need to sanitize them
508
509 // upper case only
510 key.MakeUpper();
511 // logically swap - with _
512 key.Replace( wxS( "-" ), wxS( "_" ) );
513
514 // remove any other chars
515 nonValidCharsRegex.Replace( &key, wxEmptyString );
516
517 if( !m_Env.vars.count( key ) )
518 {
519 wxLogTrace( traceEnvVars, wxS( "COMMON_SETTINGS: Loaded new var: %s = %s" ), key, val );
520 m_Env.vars[key] = ENV_VAR_ITEM( key, val );
521 }
522 }
523
524 if( cfgpath.FileExists() )
525 {
526 wxRemoveFile( cfgpath.GetFullPath() );
527 }
528
529 return true;
530}
531
532
533bool COMMON_SETTINGS::MigrateFromLegacy( wxConfigBase* aCfg )
534{
535 bool ret = true;
536
537 ret &= fromLegacy<double>( aCfg, "CanvasScale", "appearance.canvas_scale" );
538 ret &= fromLegacy<int>( aCfg, "IconScale", "appearance.icon_scale" );
539 ret &= fromLegacy<bool>( aCfg, "UseIconsInMenus", "appearance.use_icons_in_menus" );
540 ret &= fromLegacy<bool>( aCfg, "ShowEnvVarWarningDialog", "environment.show_warning_dialog" );
541
542 auto load_env_vars =
543 [&]()
544 {
545 wxString key, value;
546 long index = 0;
547 nlohmann::json::json_pointer ptr = m_internals->PointerFromString( "environment.vars" );
548
549 aCfg->SetPath( "EnvironmentVariables" );
550 ( *m_internals )[ptr] = nlohmann::json( {} );
551
552 while( aCfg->GetNextEntry( key, index ) )
553 {
554 if( versionedEnvVarRegex.Matches( key ) )
555 {
556 wxLogTrace( traceSettings,
557 wxT( "Migrate Env: %s is blacklisted; skipping." ), key );
558 continue;
559 }
560
561 value = aCfg->Read( key, wxEmptyString );
562
563 if( !value.IsEmpty() )
564 {
565 ptr.push_back( key.ToStdString() );
566
567 wxLogTrace( traceSettings, wxT( "Migrate Env: %s=%s" ),
568 ptr.to_string(), value );
569 ( *m_internals )[ptr] = value.ToUTF8();
570
571 ptr.pop_back();
572 }
573 }
574
575 aCfg->SetPath( ".." );
576 };
577
578 load_env_vars();
579
580 bool mousewheel_pan = false;
581
582 if( aCfg->Read( "MousewheelPAN", &mousewheel_pan ) && mousewheel_pan )
583 {
584 Set( "input.horizontal_pan", true );
585 Set( "input.scroll_modifier_pan_h", static_cast<int>( WXK_SHIFT ) );
586 Set( "input.scroll_modifier_pan_v", 0 );
587 Set( "input.scroll_modifier_zoom", static_cast<int>( WXK_CONTROL ) );
588 }
589
590 ret &= fromLegacy<bool>( aCfg, "AutoPAN", "input.auto_pan" );
591 ret &= fromLegacy<bool>( aCfg, "ImmediateActions", "input.immediate_actions" );
592 ret &= fromLegacy<bool>( aCfg, "PreferSelectionToDragging", "input.prefer_select_to_drag" );
593 ret &= fromLegacy<bool>( aCfg, "MoveWarpsCursor", "input.warp_mouse_on_move" );
594 ret &= fromLegacy<bool>( aCfg, "ZoomNoCenter", "input.center_on_zoom" );
595
596 // This was stored inverted in legacy config
597 if( std::optional<bool> value = Get<bool>( "input.center_on_zoom" ) )
598 Set( "input.center_on_zoom", !( *value ) );
599
600 ret &= fromLegacy<int>( aCfg, "OpenGLAntialiasingMode", "graphics.opengl_antialiasing_mode" );
601 ret &= fromLegacy<int>( aCfg, "CairoAntialiasingMode", "graphics.cairo_antialiasing_mode" );
602
603 ret &= fromLegacy<int>( aCfg, "AutoSaveInterval", "system.autosave_interval" );
604 ret &= fromLegacyString( aCfg, "Editor", "system.editor_name" );
605 ret &= fromLegacy<int>( aCfg, "FileHistorySize", "system.file_history_size" );
606 ret &= fromLegacyString( aCfg, "LanguageID", "system.language" );
607 ret &= fromLegacyString( aCfg, "PdfBrowserName", "system.pdf_viewer_name" );
608 ret &= fromLegacy<bool>( aCfg, "UseSystemBrowser", "system.use_system_pdf_viewer" );
609 ret &= fromLegacyString( aCfg, "WorkingDir", "system.working_dir" );
610
611 return ret;
612}
613
614
616{
617 auto addVar =
618 [&]( const wxString& aKey, const wxString& aDefault )
619 {
620 m_Env.vars[aKey] = ENV_VAR_ITEM( aKey, aDefault, aDefault );
621
622 wxString envValue;
623
624 if( wxGetEnv( aKey, &envValue ) == true && !envValue.IsEmpty() )
625 {
626 m_Env.vars[aKey].SetValue( envValue );
627 m_Env.vars[aKey].SetDefinedExternally();
628 wxLogTrace( traceEnvVars,
629 wxS( "InitializeEnvironment: Entry %s defined externally as %s" ), aKey,
630 envValue );
631 }
632 else
633 {
634 wxLogTrace( traceEnvVars, wxS( "InitializeEnvironment: Setting entry %s to "
635 "default %s" ),
636 aKey, aDefault );
637 }
638 };
639
640 wxFileName basePath( PATHS::GetStockEDALibraryPath(), wxEmptyString );
641
642 wxFileName path( basePath );
643 path.AppendDir( wxT( "footprints" ) );
644 addVar( ENV_VAR::GetVersionedEnvVarName( wxS( "FOOTPRINT_DIR" ) ), path.GetFullPath() );
645
646 path = basePath;
647 path.AppendDir( wxT( "3dmodels" ) );
648 addVar( ENV_VAR::GetVersionedEnvVarName( wxS( "3DMODEL_DIR" ) ), path.GetFullPath() );
649
650 addVar( ENV_VAR::GetVersionedEnvVarName( wxS( "TEMPLATE_DIR" ) ),
652
653 addVar( wxT( "KICAD_USER_TEMPLATE_DIR" ), PATHS::GetUserTemplatesPath() );
654
655 addVar( ENV_VAR::GetVersionedEnvVarName( wxS( "3RD_PARTY" ) ),
657
658 path = basePath;
659 path.AppendDir( wxT( "symbols" ) );
660 addVar( ENV_VAR::GetVersionedEnvVarName( wxS( "SYMBOL_DIR" ) ), path.GetFullPath() );
661}
662
663
665 std::vector<LEGACY_3D_SEARCH_PATH>& aSearchPaths )
666{
667 wxFileName cfgpath( path );
668
669 // This should be the same as wxWidgets 3.0 wxPATH_NORM_ALL which is deprecated in 3.1.
670 // There are known issues with environment variable expansion so maybe we should be using
671 // our own ExpandEnvVarSubstitutions() here instead.
672 cfgpath.Normalize( FN_NORMALIZE_FLAGS | wxPATH_NORM_ENV_VARS );
673 wxString cfgname = cfgpath.GetFullPath();
674
675 std::ifstream cfgFile;
676 std::string cfgLine;
677
678 if( !wxFileName::Exists( cfgname ) )
679 {
680 std::ostringstream ostr;
681 ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
682 wxString errmsg = "no 3D configuration file";
683 ostr << " * " << errmsg.ToUTF8() << " '";
684 ostr << cfgname.ToUTF8() << "'";
685 wxLogTrace( traceSettings, "%s\n", ostr.str().c_str() );
686 return false;
687 }
688
689 cfgFile.open( cfgname.ToUTF8() );
690
691 if( !cfgFile.is_open() )
692 {
693 std::ostringstream ostr;
694 ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
695 wxString errmsg = wxS( "Could not open configuration file" );
696 ostr << " * " << errmsg.ToUTF8() << " '" << cfgname.ToUTF8() << "'";
697 wxLogTrace( traceSettings, wxS( "%s\n" ), ostr.str().c_str() );
698 return false;
699 }
700
701 int lineno = 0;
703 size_t idx;
704 int vnum = 0; // version number
705
706 while( cfgFile.good() )
707 {
708 cfgLine.clear();
709 std::getline( cfgFile, cfgLine );
710 ++lineno;
711
712 if( cfgLine.empty() )
713 {
714 if( cfgFile.eof() )
715 break;
716
717 continue;
718 }
719
720 if( 1 == lineno && cfgLine.compare( 0, 2, "#V" ) == 0 )
721 {
722 // extract the version number and parse accordingly
723 if( cfgLine.size() > 2 )
724 {
725 std::istringstream istr;
726 istr.str( cfgLine.substr( 2 ) );
727 istr >> vnum;
728 }
729
730 continue;
731 }
732
733 idx = 0;
734
735 if( !getLegacy3DHollerith( cfgLine, idx, al.m_Alias ) )
736 continue;
737
738 // Don't add KICADn_3DMODEL_DIR, one of its legacy equivalents, or KIPRJMOD from a
739 // config file. They're system variables which are defined at runtime.
740 wxString versionedPath = wxString::Format( wxS( "${%s}" ),
741 ENV_VAR::GetVersionedEnvVarName( wxS( "3DMODEL_DIR" ) ) );
742
743 if( al.m_Alias == versionedPath || al.m_Alias == wxS( "${KIPRJMOD}" )
744 || al.m_Alias == wxS( "$(KIPRJMOD)" ) || al.m_Alias == wxS( "${KISYS3DMOD}" )
745 || al.m_Alias == wxS( "$(KISYS3DMOD)" ) )
746 {
747 continue;
748 }
749
750 if( !getLegacy3DHollerith( cfgLine, idx, al.m_Pathvar ) )
751 continue;
752
753 if( !getLegacy3DHollerith( cfgLine, idx, al.m_Description ) )
754 continue;
755
756 aSearchPaths.push_back( al );
757 }
758
759 cfgFile.close();
760
761 return true;
762}
763
764
765bool COMMON_SETTINGS::getLegacy3DHollerith( const std::string& aString, size_t& aIndex,
766 wxString& aResult )
767{
768 aResult.clear();
769
770 if( aIndex >= aString.size() )
771 {
772 std::ostringstream ostr;
773 ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
774 wxString errmsg = wxS( "bad Hollerith string on line" );
775 ostr << " * " << errmsg.ToUTF8() << "\n'" << aString << "'";
776 wxLogTrace( traceSettings, wxS( "%s\n" ), ostr.str().c_str() );
777
778 return false;
779 }
780
781 size_t i2 = aString.find( '"', aIndex );
782
783 if( std::string::npos == i2 )
784 {
785 std::ostringstream ostr;
786 ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
787 wxString errmsg = wxS( "missing opening quote mark in config file" );
788 ostr << " * " << errmsg.ToUTF8() << "\n'" << aString << "'";
789 wxLogTrace( traceSettings, wxS( "%s\n" ), ostr.str().c_str() );
790
791 return false;
792 }
793
794 ++i2;
795
796 if( i2 >= aString.size() )
797 {
798 std::ostringstream ostr;
799 ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
800 wxString errmsg = wxS( "invalid entry (unexpected end of line)" );
801 ostr << " * " << errmsg.ToUTF8() << "\n'" << aString << "'";
802 wxLogTrace( traceSettings, wxS( "%s\n" ), ostr.str().c_str() );
803
804 return false;
805 }
806
807 std::string tnum;
808
809 while( aString[i2] >= '0' && aString[i2] <= '9' )
810 tnum.append( 1, aString[i2++] );
811
812 if( tnum.empty() || aString[i2++] != ':' )
813 {
814 std::ostringstream ostr;
815 ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
816 wxString errmsg = wxS( "bad Hollerith string on line" );
817 ostr << " * " << errmsg.ToUTF8() << "\n'" << aString << "'";
818 wxLogTrace( traceSettings, wxS( "%s\n" ), ostr.str().c_str() );
819
820 return false;
821 }
822
823 std::istringstream istr;
824 istr.str( tnum );
825 size_t nchars;
826 istr >> nchars;
827
828 if( ( i2 + nchars ) >= aString.size() )
829 {
830 std::ostringstream ostr;
831 ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
832 wxString errmsg = wxS( "invalid entry (unexpected end of line)" );
833 ostr << " * " << errmsg.ToUTF8() << "\n'" << aString << "'";
834 wxLogTrace( traceSettings, wxS( "%s\n" ), ostr.str().c_str() );
835
836 return false;
837 }
838
839 if( nchars > 0 )
840 {
841 aResult = wxString::FromUTF8( aString.substr( i2, nchars ).c_str() );
842 i2 += nchars;
843 }
844
845 if( i2 >= aString.size() || aString[i2] != '"' )
846 {
847 std::ostringstream ostr;
848 ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
849 wxString errmsg = wxS( "missing closing quote mark in config file" );
850 ostr << " * " << errmsg.ToUTF8() << "\n'" << aString << "'";
851 wxLogTrace( traceSettings, wxS( "%s\n" ), ostr.str().c_str() );
852
853 return false;
854 }
855
856 aIndex = i2 + 1;
857 return true;
858}
APPEARANCE m_Appearance
static bool getLegacy3DHollerith(const std::string &aString, size_t &aIndex, wxString &aResult)
bool readLegacy3DResolverCfg(const wxString &aPath, std::vector< LEGACY_3D_SEARCH_PATH > &aSearchPaths)
void InitializeEnvironment()
Creates the built-in environment variables and sets their default values.
AUTO_BACKUP m_Backup
virtual bool MigrateFromLegacy(wxConfigBase *aLegacyConfig) override
Migrates from wxConfig to JSON-based configuration.
ENVIRONMENT m_Env
KiCad uses environment variables internally for determining the base paths for libraries,...
bool fromLegacyString(wxConfigBase *aConfig, const std::string &aKey, const std::string &aDest)
Translates a legacy wxConfig string value to a given JSON pointer value.
void Set(const std::string &aPath, ValueType aVal)
Stores a value into the JSON document Will throw an exception if ValueType isn't something that the l...
std::vector< PARAM_BASE * > m_params
The list of parameters (owned by this object)
std::unique_ptr< JSON_SETTINGS_INTERNALS > m_internals
Stores an enum as an integer.
Definition: parameters.h:226
Like a normal param, but with custom getter and setter functions.
Definition: parameters.h:293
static wxString GetUserTemplatesPath()
Gets the user path for custom templates.
Definition: paths.cpp:76
static wxString GetStockEDALibraryPath()
Gets the stock (install) EDA library data path, which is the base path for templates,...
Definition: paths.cpp:191
static wxString GetDefault3rdPartyPath()
Gets the default path for PCM packages.
Definition: paths.cpp:119
static wxString GetStockTemplatesPath()
Gets the stock (install) templates path.
Definition: paths.cpp:247
static wxString GetUserSettingsPath()
Return the user configuration path used to store KiCad's configuration files.
Definition: paths.cpp:510
const int commonSchemaVersion
! Update the schema version whenever a migration is required
const wxRegEx versionedEnvVarRegex(wxS("KICAD[0-9]+_[A-Z0-9_]+(_DIR)?"))
! The following environment variables will never be migrated from a previous version
const wxRegEx versionedEnvVarRegex(wxS("KICAD[0-9]+_[A-Z0-9_]+(_DIR)?"))
Functions related to environment variables, including help functions.
const wxChar *const traceEnvVars
Flag to enable debug output of environment variable operations.
SETTINGS_LOC
Definition: json_settings.h:54
@ USER
The main config directory (e.g. ~/.config/kicad/)
#define traceSettings
Definition: json_settings.h:52
KICOMMON_API wxString GetVersionedEnvVarName(const wxString &aBaseName)
Constructs a versioned environment variable based on this KiCad major version.
Definition: env_vars.cpp:74
int min_interval
Minimum time, in seconds, between subsequent backups.
bool backup_on_autosave
Trigger a backup on autosave.
unsigned long long limit_total_size
Maximum total size of backups (bytes), 0 for unlimited.
int limit_total_files
Maximum number of backup archives to retain.
int limit_daily_files
Maximum files to keep per day, 0 for unlimited.
bool enabled
Automatically back up the project when files are saved.
System directories search utilities.
wxLogTrace helper definitions.
#define FN_NORMALIZE_FLAGS
Default flags to pass to wxFileName::Normalize().
Definition: wx_filename.h:39