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#if defined( __WINDOWS__ )
300 m_params.emplace_back( new PARAM<wxString>( "system.file_explorer",
301 &m_System.file_explorer, wxS( "explorer.exe /n,/select,%F" ) ) );
302#else
303 m_params.emplace_back( new PARAM<wxString>( "system.file_explorer",
304 &m_System.file_explorer, wxS( "" ) ) );
305#endif
306
307 m_params.emplace_back( new PARAM<int>( "system.file_history_size",
308 &m_System.file_history_size, 9 ) );
309
310 m_params.emplace_back( new PARAM<wxString>( "system.language",
311 &m_System.language, wxS( "Default" ) ) );
312
313 m_params.emplace_back( new PARAM<wxString>( "system.pdf_viewer_name",
314 &m_System.pdf_viewer_name, wxS( "" ) ) );
315
316 m_params.emplace_back( new PARAM<bool>( "system.use_system_pdf_viewer",
317 &m_System.use_system_pdf_viewer, true ) );
318
319 m_params.emplace_back( new PARAM<wxString>( "system.working_dir",
320 &m_System.working_dir, wxS( "" ) ) );
321
322 m_params.emplace_back( new PARAM<int>( "system.clear_3d_cache_interval",
323 &m_System.clear_3d_cache_interval, 30 ) );
324
325 m_params.emplace_back( new PARAM<bool>( "do_not_show_again.zone_fill_warning",
326 &m_DoNotShowAgain.zone_fill_warning, false ) );
327
328 m_params.emplace_back( new PARAM<bool>( "do_not_show_again.env_var_overwrite_warning",
329 &m_DoNotShowAgain.env_var_overwrite_warning, false ) );
330
331 m_params.emplace_back( new PARAM<bool>( "do_not_show_again.scaled_3d_models_warning",
332 &m_DoNotShowAgain.scaled_3d_models_warning, false ) );
333
334 m_params.emplace_back( new PARAM<bool>( "do_not_show_again.data_collection_prompt",
335 &m_DoNotShowAgain.data_collection_prompt, false ) );
336
337 m_params.emplace_back( new PARAM<bool>( "do_not_show_again.update_check_prompt",
338 &m_DoNotShowAgain.update_check_prompt, false ) );
339
340 m_params.emplace_back( new PARAM<bool>( "session.remember_open_files",
341 &m_Session.remember_open_files, false ) );
342
343 m_params.emplace_back( new PARAM_LIST<wxString>( "session.pinned_symbol_libs",
344 &m_Session.pinned_symbol_libs, {} ) );
345
346 m_params.emplace_back( new PARAM_LIST<wxString>( "session.pinned_fp_libs",
347 &m_Session.pinned_fp_libs, {} ) );
348
349 m_params.emplace_back( new PARAM<int>( "netclass_panel.sash_pos",
350 &m_NetclassPanel.sash_pos, 160 ) );
351
352 m_params.emplace_back( new PARAM<wxString>( "netclass_panel.eeschema_shown_columns",
353 &m_NetclassPanel.eeschema_visible_columns, "0 10 11 12 13" ) );
354
355 m_params.emplace_back( new PARAM<wxString>( "netclass_panel.pcbnew_shown_columns",
356 &m_NetclassPanel.pcbnew_visible_columns, "0 1 2 3 4 5 6 7 8 9" ) );
357
358 m_params.emplace_back( new PARAM<int>( "package_manager.sash_pos",
359 &m_PackageManager.sash_pos, 380 ) );
360
361 m_params.emplace_back( new PARAM_LAMBDA<nlohmann::json>( "git.repositories",
362 [&]() -> nlohmann::json
363 {
364 nlohmann::json ret = {};
365
366 for( const GIT_REPOSITORY& repo : m_Git.repositories )
367 {
368 nlohmann::json repoJson = {};
369
370 repoJson["name"] = repo.name;
371 repoJson["path"] = repo.path;
372 repoJson["authType"] = repo.authType;
373 repoJson["username"] = repo.username;
374 repoJson["ssh_path"] = repo.ssh_path;
375 repoJson["active"] = repo.active;
376
377 ret.push_back( repoJson );
378 }
379
380 return ret;
381 },
382 [&]( const nlohmann::json& aJson )
383 {
384 if( !aJson.is_array() )
385 return;
386
387 m_Git.repositories.clear();
388
389 for( const auto& repoJson : aJson )
390 {
391 GIT_REPOSITORY repo;
392
393 repo.name = repoJson["name"].get<wxString>();
394 repo.path = repoJson["path"].get<wxString>();
395 repo.authType = repoJson["authType"].get<wxString>();
396 repo.username = repoJson["username"].get<wxString>();
397 repo.ssh_path = repoJson["ssh_path"].get<wxString>();
398 repo.active = repoJson["active"].get<bool>();
399 repo.checkValid = true;
400
401 m_Git.repositories.push_back( repo );
402 }
403 },
404 {} ) );
405
406 m_params.emplace_back( new PARAM<wxString>( "git.authorName",
407 &m_Git.authorName, wxS( "" ) ) );
408
409 m_params.emplace_back( new PARAM<wxString>( "git.authorEmail",
410 &m_Git.authorEmail, wxS( "" ) ) );
411
412 m_params.emplace_back( new PARAM<bool>( "git.useDefaultAuthor",
413 &m_Git.useDefaultAuthor, true ) );
414
415 m_params.emplace_back( new PARAM<wxString>( "api.interpreter_path",
416 &m_Api.python_interpreter, wxS( "" ) ) );
417
418 m_params.emplace_back( new PARAM<bool>( "api.enable_server",
419 &m_Api.enable_server, false ) );
420
421 registerMigration( 0, 1, std::bind( &COMMON_SETTINGS::migrateSchema0to1, this ) );
422 registerMigration( 1, 2, std::bind( &COMMON_SETTINGS::migrateSchema1to2, this ) );
423 registerMigration( 2, 3, std::bind( &COMMON_SETTINGS::migrateSchema2to3, this ) );
424}
425
426
428{
435 nlohmann::json::json_pointer mwp_pointer( "/input/mousewheel_pan"_json_pointer );
436
437 bool mwp = false;
438
439 try
440 {
441 mwp = m_internals->at( mwp_pointer );
442 m_internals->At( "input" ).erase( "mousewheel_pan" );
443 }
444 catch( ... )
445 {
446 wxLogTrace( traceSettings, wxT( "COMMON_SETTINGS::Migrate 0->1: mousewheel_pan not found" ) );
447 }
448
449 if( mwp )
450 {
451 ( *m_internals )[nlohmann::json::json_pointer( "/input/horizontal_pan" )] = true;
452
453 ( *m_internals )[nlohmann::json::json_pointer( "/input/scroll_modifier_pan_h" )] = WXK_SHIFT;
454 ( *m_internals )[nlohmann::json::json_pointer( "/input/scroll_modifier_pan_v" )] = 0;
455 ( *m_internals )[nlohmann::json::json_pointer( "/input/scroll_modifier_zoom" )] = WXK_CONTROL;
456 }
457 else
458 {
459 ( *m_internals )[nlohmann::json::json_pointer( "/input/horizontal_pan" )] = false;
460
461 ( *m_internals )[nlohmann::json::json_pointer( "/input/scroll_modifier_pan_h" )] = WXK_CONTROL;
462 ( *m_internals )[nlohmann::json::json_pointer( "/input/scroll_modifier_pan_v" )] = WXK_SHIFT;
463 ( *m_internals )[nlohmann::json::json_pointer( "/input/scroll_modifier_zoom" )] = 0;
464 }
465
466 return true;
467}
468
469
471{
472 nlohmann::json::json_pointer v1_pointer( "/input/prefer_select_to_drag"_json_pointer );
473
474 bool prefer_selection = false;
475
476 try
477 {
478 prefer_selection = m_internals->at( v1_pointer );
479 m_internals->at( nlohmann::json::json_pointer( "/input"_json_pointer ) ).erase( "prefer_select_to_drag" );
480 }
481 catch( ... )
482 {
483 wxLogTrace( traceSettings, wxT( "COMMON_SETTINGS::Migrate 1->2: prefer_select_to_drag not found" ) );
484 }
485
486 if( prefer_selection )
487 ( *m_internals )[nlohmann::json::json_pointer( "/input/mouse_left" )] = MOUSE_DRAG_ACTION::SELECT;
488 else
489 ( *m_internals )[nlohmann::json::json_pointer( "/input/mouse_left" )] = MOUSE_DRAG_ACTION::DRAG_ANY;
490
491 return true;
492}
493
494
496{
497 wxFileName cfgpath;
498 cfgpath.AssignDir( PATHS::GetUserSettingsPath() );
499 cfgpath.AppendDir( wxT( "3d" ) );
500 cfgpath.SetFullName( wxS( "3Dresolver.cfg" ) );
501 cfgpath.MakeAbsolute();
502
503 std::vector<LEGACY_3D_SEARCH_PATH> legacyPaths;
504 readLegacy3DResolverCfg( cfgpath.GetFullPath(), legacyPaths );
505
506 // env variables have a limited allowed character set for names
507 wxRegEx nonValidCharsRegex( wxS( "[^A-Z0-9_]+" ), wxRE_ADVANCED );
508
509 for( const LEGACY_3D_SEARCH_PATH& path : legacyPaths )
510 {
511 wxString key = path.m_Alias;
512 const wxString& val = path.m_Pathvar;
513
514 // The 3d alias config didnt use the same naming restrictions as real env variables
515 // We need to sanitize them
516
517 // upper case only
518 key.MakeUpper();
519 // logically swap - with _
520 key.Replace( wxS( "-" ), wxS( "_" ) );
521
522 // remove any other chars
523 nonValidCharsRegex.Replace( &key, wxEmptyString );
524
525 if( !m_Env.vars.count( key ) )
526 {
527 wxLogTrace( traceEnvVars, wxS( "COMMON_SETTINGS: Loaded new var: %s = %s" ), key, val );
528 m_Env.vars[key] = ENV_VAR_ITEM( key, val );
529 }
530 }
531
532 if( cfgpath.FileExists() )
533 {
534 wxRemoveFile( cfgpath.GetFullPath() );
535 }
536
537 return true;
538}
539
540
541bool COMMON_SETTINGS::MigrateFromLegacy( wxConfigBase* aCfg )
542{
543 bool ret = true;
544
545 ret &= fromLegacy<double>( aCfg, "CanvasScale", "appearance.canvas_scale" );
546 ret &= fromLegacy<int>( aCfg, "IconScale", "appearance.icon_scale" );
547 ret &= fromLegacy<bool>( aCfg, "UseIconsInMenus", "appearance.use_icons_in_menus" );
548 ret &= fromLegacy<bool>( aCfg, "ShowEnvVarWarningDialog", "environment.show_warning_dialog" );
549
550 auto load_env_vars =
551 [&]()
552 {
553 wxString key, value;
554 long index = 0;
555 nlohmann::json::json_pointer ptr = m_internals->PointerFromString( "environment.vars" );
556
557 aCfg->SetPath( "EnvironmentVariables" );
558 ( *m_internals )[ptr] = nlohmann::json( {} );
559
560 while( aCfg->GetNextEntry( key, index ) )
561 {
562 if( versionedEnvVarRegex.Matches( key ) )
563 {
564 wxLogTrace( traceSettings,
565 wxT( "Migrate Env: %s is blacklisted; skipping." ), key );
566 continue;
567 }
568
569 value = aCfg->Read( key, wxEmptyString );
570
571 if( !value.IsEmpty() )
572 {
573 ptr.push_back( key.ToStdString() );
574
575 wxLogTrace( traceSettings, wxT( "Migrate Env: %s=%s" ),
576 ptr.to_string(), value );
577 ( *m_internals )[ptr] = value.ToUTF8();
578
579 ptr.pop_back();
580 }
581 }
582
583 aCfg->SetPath( ".." );
584 };
585
586 load_env_vars();
587
588 bool mousewheel_pan = false;
589
590 if( aCfg->Read( "MousewheelPAN", &mousewheel_pan ) && mousewheel_pan )
591 {
592 Set( "input.horizontal_pan", true );
593 Set( "input.scroll_modifier_pan_h", static_cast<int>( WXK_SHIFT ) );
594 Set( "input.scroll_modifier_pan_v", 0 );
595 Set( "input.scroll_modifier_zoom", static_cast<int>( WXK_CONTROL ) );
596 }
597
598 ret &= fromLegacy<bool>( aCfg, "AutoPAN", "input.auto_pan" );
599 ret &= fromLegacy<bool>( aCfg, "ImmediateActions", "input.immediate_actions" );
600 ret &= fromLegacy<bool>( aCfg, "PreferSelectionToDragging", "input.prefer_select_to_drag" );
601 ret &= fromLegacy<bool>( aCfg, "MoveWarpsCursor", "input.warp_mouse_on_move" );
602 ret &= fromLegacy<bool>( aCfg, "ZoomNoCenter", "input.center_on_zoom" );
603
604 // This was stored inverted in legacy config
605 if( std::optional<bool> value = Get<bool>( "input.center_on_zoom" ) )
606 Set( "input.center_on_zoom", !( *value ) );
607
608 ret &= fromLegacy<int>( aCfg, "OpenGLAntialiasingMode", "graphics.opengl_antialiasing_mode" );
609 ret &= fromLegacy<int>( aCfg, "CairoAntialiasingMode", "graphics.cairo_antialiasing_mode" );
610
611 ret &= fromLegacy<int>( aCfg, "AutoSaveInterval", "system.autosave_interval" );
612 ret &= fromLegacyString( aCfg, "Editor", "system.editor_name" );
613 ret &= fromLegacy<int>( aCfg, "FileHistorySize", "system.file_history_size" );
614 ret &= fromLegacyString( aCfg, "LanguageID", "system.language" );
615 ret &= fromLegacyString( aCfg, "PdfBrowserName", "system.pdf_viewer_name" );
616 ret &= fromLegacy<bool>( aCfg, "UseSystemBrowser", "system.use_system_pdf_viewer" );
617 ret &= fromLegacyString( aCfg, "WorkingDir", "system.working_dir" );
618
619 return ret;
620}
621
622
624{
625 auto addVar =
626 [&]( const wxString& aKey, const wxString& aDefault )
627 {
628 m_Env.vars[aKey] = ENV_VAR_ITEM( aKey, aDefault, aDefault );
629
630 wxString envValue;
631
632 if( wxGetEnv( aKey, &envValue ) == true && !envValue.IsEmpty() )
633 {
634 m_Env.vars[aKey].SetValue( envValue );
635 m_Env.vars[aKey].SetDefinedExternally();
636 wxLogTrace( traceEnvVars,
637 wxS( "InitializeEnvironment: Entry %s defined externally as %s" ), aKey,
638 envValue );
639 }
640 else
641 {
642 wxLogTrace( traceEnvVars, wxS( "InitializeEnvironment: Setting entry %s to "
643 "default %s" ),
644 aKey, aDefault );
645 }
646 };
647
648 wxFileName basePath( PATHS::GetStockEDALibraryPath(), wxEmptyString );
649
650 wxFileName path( basePath );
651 path.AppendDir( wxT( "footprints" ) );
652 addVar( ENV_VAR::GetVersionedEnvVarName( wxS( "FOOTPRINT_DIR" ) ), path.GetFullPath() );
653
654 path = basePath;
655 path.AppendDir( wxT( "3dmodels" ) );
656 addVar( ENV_VAR::GetVersionedEnvVarName( wxS( "3DMODEL_DIR" ) ), path.GetFullPath() );
657
658 addVar( ENV_VAR::GetVersionedEnvVarName( wxS( "TEMPLATE_DIR" ) ),
660
661 addVar( wxT( "KICAD_USER_TEMPLATE_DIR" ), PATHS::GetUserTemplatesPath() );
662
663 addVar( ENV_VAR::GetVersionedEnvVarName( wxS( "3RD_PARTY" ) ),
665
666 path = basePath;
667 path.AppendDir( wxT( "symbols" ) );
668 addVar( ENV_VAR::GetVersionedEnvVarName( wxS( "SYMBOL_DIR" ) ), path.GetFullPath() );
669}
670
671
673 std::vector<LEGACY_3D_SEARCH_PATH>& aSearchPaths )
674{
675 wxFileName cfgpath( path );
676
677 // This should be the same as wxWidgets 3.0 wxPATH_NORM_ALL which is deprecated in 3.1.
678 // There are known issues with environment variable expansion so maybe we should be using
679 // our own ExpandEnvVarSubstitutions() here instead.
680 cfgpath.Normalize( FN_NORMALIZE_FLAGS | wxPATH_NORM_ENV_VARS );
681 wxString cfgname = cfgpath.GetFullPath();
682
683 std::ifstream cfgFile;
684 std::string cfgLine;
685
686 if( !wxFileName::Exists( cfgname ) )
687 {
688 std::ostringstream ostr;
689 ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
690 wxString errmsg = "no 3D configuration file";
691 ostr << " * " << errmsg.ToUTF8() << " '";
692 ostr << cfgname.ToUTF8() << "'";
693 wxLogTrace( traceSettings, "%s\n", ostr.str().c_str() );
694 return false;
695 }
696
697 cfgFile.open( cfgname.ToUTF8() );
698
699 if( !cfgFile.is_open() )
700 {
701 std::ostringstream ostr;
702 ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
703 wxString errmsg = wxS( "Could not open configuration file" );
704 ostr << " * " << errmsg.ToUTF8() << " '" << cfgname.ToUTF8() << "'";
705 wxLogTrace( traceSettings, wxS( "%s\n" ), ostr.str().c_str() );
706 return false;
707 }
708
709 int lineno = 0;
711 size_t idx;
712 int vnum = 0; // version number
713
714 while( cfgFile.good() )
715 {
716 cfgLine.clear();
717 std::getline( cfgFile, cfgLine );
718 ++lineno;
719
720 if( cfgLine.empty() )
721 {
722 if( cfgFile.eof() )
723 break;
724
725 continue;
726 }
727
728 if( 1 == lineno && cfgLine.compare( 0, 2, "#V" ) == 0 )
729 {
730 // extract the version number and parse accordingly
731 if( cfgLine.size() > 2 )
732 {
733 std::istringstream istr;
734 istr.str( cfgLine.substr( 2 ) );
735 istr >> vnum;
736 }
737
738 continue;
739 }
740
741 idx = 0;
742
743 if( !getLegacy3DHollerith( cfgLine, idx, al.m_Alias ) )
744 continue;
745
746 // Don't add KICADn_3DMODEL_DIR, one of its legacy equivalents, or KIPRJMOD from a
747 // config file. They're system variables which are defined at runtime.
748 wxString versionedPath = wxString::Format( wxS( "${%s}" ),
749 ENV_VAR::GetVersionedEnvVarName( wxS( "3DMODEL_DIR" ) ) );
750
751 if( al.m_Alias == versionedPath || al.m_Alias == wxS( "${KIPRJMOD}" )
752 || al.m_Alias == wxS( "$(KIPRJMOD)" ) || al.m_Alias == wxS( "${KISYS3DMOD}" )
753 || al.m_Alias == wxS( "$(KISYS3DMOD)" ) )
754 {
755 continue;
756 }
757
758 if( !getLegacy3DHollerith( cfgLine, idx, al.m_Pathvar ) )
759 continue;
760
761 if( !getLegacy3DHollerith( cfgLine, idx, al.m_Description ) )
762 continue;
763
764 aSearchPaths.push_back( al );
765 }
766
767 cfgFile.close();
768
769 return true;
770}
771
772
773bool COMMON_SETTINGS::getLegacy3DHollerith( const std::string& aString, size_t& aIndex,
774 wxString& aResult )
775{
776 aResult.clear();
777
778 if( aIndex >= aString.size() )
779 {
780 std::ostringstream ostr;
781 ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
782 wxString errmsg = wxS( "bad Hollerith string on line" );
783 ostr << " * " << errmsg.ToUTF8() << "\n'" << aString << "'";
784 wxLogTrace( traceSettings, wxS( "%s\n" ), ostr.str().c_str() );
785
786 return false;
787 }
788
789 size_t i2 = aString.find( '"', aIndex );
790
791 if( std::string::npos == i2 )
792 {
793 std::ostringstream ostr;
794 ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
795 wxString errmsg = wxS( "missing opening quote mark in config file" );
796 ostr << " * " << errmsg.ToUTF8() << "\n'" << aString << "'";
797 wxLogTrace( traceSettings, wxS( "%s\n" ), ostr.str().c_str() );
798
799 return false;
800 }
801
802 ++i2;
803
804 if( i2 >= aString.size() )
805 {
806 std::ostringstream ostr;
807 ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
808 wxString errmsg = wxS( "invalid entry (unexpected end of line)" );
809 ostr << " * " << errmsg.ToUTF8() << "\n'" << aString << "'";
810 wxLogTrace( traceSettings, wxS( "%s\n" ), ostr.str().c_str() );
811
812 return false;
813 }
814
815 std::string tnum;
816
817 while( aString[i2] >= '0' && aString[i2] <= '9' )
818 tnum.append( 1, aString[i2++] );
819
820 if( tnum.empty() || aString[i2++] != ':' )
821 {
822 std::ostringstream ostr;
823 ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
824 wxString errmsg = wxS( "bad Hollerith string on line" );
825 ostr << " * " << errmsg.ToUTF8() << "\n'" << aString << "'";
826 wxLogTrace( traceSettings, wxS( "%s\n" ), ostr.str().c_str() );
827
828 return false;
829 }
830
831 std::istringstream istr;
832 istr.str( tnum );
833 size_t nchars;
834 istr >> nchars;
835
836 if( ( i2 + nchars ) >= aString.size() )
837 {
838 std::ostringstream ostr;
839 ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
840 wxString errmsg = wxS( "invalid entry (unexpected end of line)" );
841 ostr << " * " << errmsg.ToUTF8() << "\n'" << aString << "'";
842 wxLogTrace( traceSettings, wxS( "%s\n" ), ostr.str().c_str() );
843
844 return false;
845 }
846
847 if( nchars > 0 )
848 {
849 aResult = wxString::FromUTF8( aString.substr( i2, nchars ).c_str() );
850 i2 += nchars;
851 }
852
853 if( i2 >= aString.size() || aString[i2] != '"' )
854 {
855 std::ostringstream ostr;
856 ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
857 wxString errmsg = wxS( "missing closing quote mark in config file" );
858 ostr << " * " << errmsg.ToUTF8() << "\n'" << aString << "'";
859 wxLogTrace( traceSettings, wxS( "%s\n" ), ostr.str().c_str() );
860
861 return false;
862 }
863
864 aIndex = i2 + 1;
865 return true;
866}
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