KiCad PCB EDA Suite
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-2022 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 <paths.h>
26#include <search_stack.h>
30#include <settings/parameters.h>
31#include <systemdirsappend.h>
32#include <trace_helpers.h>
33#include <wx/config.h>
34#include <wx/log.h>
35#include <wx/regex.h>
36
37
39const std::set<wxString> envVarBlacklist =
40 {
41 wxT( "KICAD7_SYMBOL_DIR" ),
42 wxT( "KICAD7_FOOTPRINT_DIR" ),
43 wxT( "KICAD7_TEMPLATES_DIR" ),
44 wxT( "KICAD7_3DMODEL_DIR" )
45 };
46
47
49const int commonSchemaVersion = 3;
50
53 m_Appearance(),
54 m_Backup(),
55 m_Env(),
56 m_Input(),
57 m_Graphics(),
58 m_Session(),
59 m_System(),
60 m_DoNotShowAgain(),
61 m_NetclassPanel(),
62 m_PackageManager()
63{
64 /*
65 * Automatic dark mode detection works fine on Mac.
66 */
67#if defined( __WXGTK__ ) || defined( __WXMSW__ )
68 m_params.emplace_back( new PARAM_ENUM<ICON_THEME>( "appearance.icon_theme",
70#else
72#endif
73
74 /*
75 * Automatic icon scaling works fine on Mac. It works mostly fine on MSW, but perhaps not
76 * uniformly enough to exclude the explicit controls there.
77 */
78#if defined( __WXGTK__ ) || defined( __WXMSW__ )
79 m_params.emplace_back( new PARAM<int>( "appearance.icon_scale",
81#else
83#endif
84
85 /*
86 * Automatic canvas scaling works fine on all supported platforms, so it's no longer exposed as
87 * a configuration option.
88 */
90
91 /*
92 * Menu icons are off by default on OSX and on for all other platforms.
93 */
94#ifdef __WXMAC__
95 m_params.emplace_back( new PARAM<bool>( "appearance.use_icons_in_menus",
97#else
98 m_params.emplace_back( new PARAM<bool>( "appearance.use_icons_in_menus",
100#endif
101
102 /*
103 * Font scaling hacks are only needed on GTK under wxWidgets 3.0.
104 */
105#if defined( __WXGTK__ ) && !wxCHECK_VERSION( 3, 1, 0 )
106 m_params.emplace_back( new PARAM<bool>( "appearance.apply_icon_scale_to_fonts",
108#else
110#endif
111
112 m_params.emplace_back( new PARAM<bool>( "appearance.show_scrollbars",
113 &m_Appearance.show_scrollbars, true ) );
114
115 m_params.emplace_back( new PARAM<double>( "appearance.hicontrast_dimming_factor",
117
118 m_params.emplace_back( new PARAM<int>( "appearance.text_editor_zoom",
120
121 m_params.emplace_back( new PARAM<bool>( "auto_backup.enabled", &m_Backup.enabled, true ) );
122
123 m_params.emplace_back( new PARAM<bool>( "auto_backup.backup_on_autosave",
124 &m_Backup.backup_on_autosave, false ) );
125
126 m_params.emplace_back( new PARAM<int>( "auto_backup.limit_total_files",
128
129 m_params.emplace_back( new PARAM<unsigned long long>( "auto_backup.limit_total_size",
130 &m_Backup.limit_total_size, 104857600 ) );
131
132 m_params.emplace_back( new PARAM<int>( "auto_backup.limit_daily_files",
134
135 m_params.emplace_back( new PARAM<int>( "auto_backup.min_interval",
136 &m_Backup.min_interval, 300 ) );
137
138 m_params.emplace_back( new PARAM_LAMBDA<nlohmann::json>( "environment.vars",
139 [&]() -> nlohmann::json
140 {
141 nlohmann::json ret = {};
142
143 for( const std::pair<wxString, ENV_VAR_ITEM> entry : m_Env.vars )
144 {
145 const ENV_VAR_ITEM& var = entry.second;
146
147 wxASSERT( entry.first == var.GetKey() );
148
149 // Default values are never persisted
150 if( var.IsDefault() )
151 {
152 wxLogTrace( traceEnvVars,
153 wxS( "COMMON_SETTINGS: Env var %s skipping save (default)" ),
154 var.GetKey() );
155 continue;
156 }
157
158 wxString value = var.GetValue();
159
160 // Vars that existed in JSON are persisted, but if they were overridden
161 // externally, we persist the old value (i.e. the one that was loaded from JSON)
162 if( var.GetDefinedExternally() )
163 {
164 if( var.GetDefinedInSettings() )
165 {
166 wxLogTrace( traceEnvVars,
167 wxS( "COMMON_SETTINGS: Env var %s was overridden externally, " )
168 "saving previously-loaded value %s",
169 var.GetKey(), var.GetSettingsValue() );
170 value = var.GetSettingsValue();
171 }
172 else
173 {
174 wxLogTrace( traceEnvVars,
175 wxS( "COMMON_SETTINGS: Env var %s skipping save (external)" ),
176 var.GetKey() );
177 continue;
178 }
179 }
180
181 wxLogTrace( traceEnvVars,
182 wxS( "COMMON_SETTINGS: Saving env var %s = %s" ),
183 var.GetKey(), value);
184
185 std::string key( var.GetKey().ToUTF8() );
186 ret[key] = value;
187 }
188
189 return ret;
190 },
191 [&]( const nlohmann::json& aJson )
192 {
193 if( !aJson.is_object() )
194 return;
195
196 for( const auto& entry : aJson.items() )
197 {
198 wxString key = wxString( entry.key().c_str(), wxConvUTF8 );
199 wxString val = entry.value().get<wxString>();
200
201 if( m_Env.vars.count( key ) )
202 {
203 if( m_Env.vars[key].GetDefinedExternally() )
204 {
205 wxLogTrace( traceEnvVars, wxS( "COMMON_SETTINGS: %s is defined externally" ),
206 key );
207 m_Env.vars[key].SetDefinedInSettings();
208 m_Env.vars[key].SetSettingsValue( val );
209 continue;
210 }
211 else
212 {
213 wxLogTrace( traceEnvVars, wxS( "COMMON_SETTINGS: Updating %s: %s -> %s"),
214 key, m_Env.vars[key].GetValue(), val );
215 m_Env.vars[key].SetValue( val );
216 }
217 }
218 else
219 {
220 wxLogTrace( traceEnvVars, wxS( "COMMON_SETTINGS: Loaded new var: %s = %s" ),
221 key, val );
222 m_Env.vars[key] = ENV_VAR_ITEM( key, val );
223 }
224
225 m_Env.vars[key].SetDefinedInSettings();
226 m_Env.vars[key].SetSettingsValue( val );
227 }
228 },
229 {} ) );
230
231 m_params.emplace_back( new PARAM<bool>( "input.focus_follow_sch_pcb",
232 &m_Input.focus_follow_sch_pcb, false ) );
233
234 m_params.emplace_back( new PARAM<bool>( "input.auto_pan", &m_Input.auto_pan, false ) );
235
236 m_params.emplace_back( new PARAM<int>( "input.auto_pan_acceleration",
237 &m_Input.auto_pan_acceleration, 5 ) );
238
239 m_params.emplace_back( new PARAM<bool>( "input.center_on_zoom",
240 &m_Input.center_on_zoom, true ) );
241
242 m_params.emplace_back( new PARAM<bool>( "input.immediate_actions",
243 &m_Input.immediate_actions, true ) );
244
245 m_params.emplace_back( new PARAM<bool>( "input.warp_mouse_on_move",
246 &m_Input.warp_mouse_on_move, true ) );
247
248 m_params.emplace_back( new PARAM<bool>( "input.horizontal_pan",
249 &m_Input.horizontal_pan, false ) );
250
251 m_params.emplace_back( new PARAM<bool>( "input.zoom_acceleration",
252 &m_Input.zoom_acceleration, false ) );
253
254#ifdef __WXMAC__
255 int default_zoom_speed = 5;
256#else
257 int default_zoom_speed = 1;
258#endif
259
260 m_params.emplace_back( new PARAM<int>( "input.zoom_speed",
261 &m_Input.zoom_speed, default_zoom_speed ) );
262
263 m_params.emplace_back( new PARAM<bool>( "input.zoom_speed_auto",
264 &m_Input.zoom_speed_auto, true ) );
265
266 m_params.emplace_back( new PARAM<int>( "input.scroll_modifier_zoom",
267 &m_Input.scroll_modifier_zoom, 0 ) );
268
269 m_params.emplace_back( new PARAM<int>( "input.scroll_modifier_pan_h",
270 &m_Input.scroll_modifier_pan_h, WXK_CONTROL ) );
271
272 m_params.emplace_back( new PARAM<int>( "input.scroll_modifier_pan_v",
273 &m_Input.scroll_modifier_pan_v, WXK_SHIFT ) );
274
275 m_params.emplace_back( new PARAM_ENUM<MOUSE_DRAG_ACTION>( "input.mouse_left",
278
279 m_params.emplace_back( new PARAM_ENUM<MOUSE_DRAG_ACTION>( "input.mouse_middle",
282
283 m_params.emplace_back( new PARAM_ENUM<MOUSE_DRAG_ACTION>( "input.mouse_right",
286
287 m_params.emplace_back( new PARAM<int>( "graphics.opengl_antialiasing_mode",
288 &m_Graphics.opengl_aa_mode, 1, 0, 2 ) );
289
290 m_params.emplace_back( new PARAM<int>( "graphics.cairo_antialiasing_mode",
291 &m_Graphics.cairo_aa_mode, 0, 0, 2 ) );
292
293 m_params.emplace_back( new PARAM<int>( "system.autosave_interval",
294 &m_System.autosave_interval, 600 ) );
295
296#ifdef __WXMAC__
297 m_params.emplace_back( new PARAM<wxString>( "system.text_editor",
298 &m_System.text_editor, wxS( "/usr/bin/open -e" ) ) );
299#else
300 m_params.emplace_back( new PARAM<wxString>( "system.text_editor",
301 &m_System.text_editor, wxS( "" ) ) );
302#endif
303
304 m_params.emplace_back( new PARAM<int>( "system.file_history_size",
305 &m_System.file_history_size, 9 ) );
306
307 m_params.emplace_back( new PARAM<wxString>( "system.language",
308 &m_System.language, wxS( "Default" ) ) );
309
310 m_params.emplace_back( new PARAM<wxString>( "system.pdf_viewer_name",
311 &m_System.pdf_viewer_name, wxS( "" ) ) );
312
313 m_params.emplace_back( new PARAM<bool>( "system.use_system_pdf_viewer",
314 &m_System.use_system_pdf_viewer, true ) );
315
316 m_params.emplace_back( new PARAM<wxString>( "system.working_dir",
317 &m_System.working_dir, wxS( "" ) ) );
318
319 m_params.emplace_back( new PARAM<int>( "system.clear_3d_cache_interval",
320 &m_System.clear_3d_cache_interval, 30 ) );
321
322 m_params.emplace_back( new PARAM<bool>( "do_not_show_again.zone_fill_warning",
323 &m_DoNotShowAgain.zone_fill_warning, false ) );
324
325 m_params.emplace_back( new PARAM<bool>( "do_not_show_again.env_var_overwrite_warning",
326 &m_DoNotShowAgain.env_var_overwrite_warning, false ) );
327
328 m_params.emplace_back( new PARAM<bool>( "do_not_show_again.scaled_3d_models_warning",
329 &m_DoNotShowAgain.scaled_3d_models_warning, false ) );
330
331 m_params.emplace_back( new PARAM<bool>( "do_not_show_again.data_collection_prompt",
332 &m_DoNotShowAgain.data_collection_prompt, false ) );
333
334 m_params.emplace_back( new PARAM<bool>( "session.remember_open_files",
335 &m_Session.remember_open_files, false ) );
336
337 m_params.emplace_back( new PARAM_LIST<wxString>( "session.pinned_symbol_libs",
338 &m_Session.pinned_symbol_libs, {} ) );
339
340 m_params.emplace_back( new PARAM_LIST<wxString>( "session.pinned_fp_libs",
341 &m_Session.pinned_fp_libs, {} ) );
342
343 m_params.emplace_back( new PARAM<int>( "netclass_panel.sash_pos",
344 &m_NetclassPanel.sash_pos, 160 ) );
345
346 m_params.emplace_back( new PARAM<int>( "package_manager.sash_pos",
347 &m_PackageManager.sash_pos, 380 ) );
348
349 registerMigration( 0, 1, std::bind( &COMMON_SETTINGS::migrateSchema0to1, this ) );
350 registerMigration( 1, 2, std::bind( &COMMON_SETTINGS::migrateSchema1to2, this ) );
351 registerMigration( 2, 3, std::bind( &COMMON_SETTINGS::migrateSchema2to3, this ) );
352}
353
354
356{
363 nlohmann::json::json_pointer mwp_pointer( "/input/mousewheel_pan"_json_pointer );
364
365 bool mwp = false;
366
367 try
368 {
369 mwp = m_internals->at( mwp_pointer );
370 m_internals->At( "input" ).erase( "mousewheel_pan" );
371 }
372 catch( ... )
373 {
374 wxLogTrace( traceSettings, wxT( "COMMON_SETTINGS::Migrate 0->1: mousewheel_pan not found" ) );
375 }
376
377 if( mwp )
378 {
379 ( *m_internals )[nlohmann::json::json_pointer( "/input/horizontal_pan" )] = true;
380
381 ( *m_internals )[nlohmann::json::json_pointer( "/input/scroll_modifier_pan_h" )] = WXK_SHIFT;
382 ( *m_internals )[nlohmann::json::json_pointer( "/input/scroll_modifier_pan_v" )] = 0;
383 ( *m_internals )[nlohmann::json::json_pointer( "/input/scroll_modifier_zoom" )] = WXK_CONTROL;
384 }
385 else
386 {
387 ( *m_internals )[nlohmann::json::json_pointer( "/input/horizontal_pan" )] = false;
388
389 ( *m_internals )[nlohmann::json::json_pointer( "/input/scroll_modifier_pan_h" )] = WXK_CONTROL;
390 ( *m_internals )[nlohmann::json::json_pointer( "/input/scroll_modifier_pan_v" )] = WXK_SHIFT;
391 ( *m_internals )[nlohmann::json::json_pointer( "/input/scroll_modifier_zoom" )] = 0;
392 }
393
394 return true;
395}
396
397
399{
400 nlohmann::json::json_pointer v1_pointer( "/input/prefer_select_to_drag"_json_pointer );
401
402 bool prefer_selection = false;
403
404 try
405 {
406 prefer_selection = m_internals->at( v1_pointer );
407 m_internals->at( nlohmann::json::json_pointer( "/input"_json_pointer ) ).erase( "prefer_select_to_drag" );
408 }
409 catch( ... )
410 {
411 wxLogTrace( traceSettings, wxT( "COMMON_SETTINGS::Migrate 1->2: prefer_select_to_drag not found" ) );
412 }
413
414 if( prefer_selection )
415 ( *m_internals )[nlohmann::json::json_pointer( "/input/mouse_left" )] = MOUSE_DRAG_ACTION::SELECT;
416 else
417 ( *m_internals )[nlohmann::json::json_pointer( "/input/mouse_left" )] = MOUSE_DRAG_ACTION::DRAG_ANY;
418
419 return true;
420}
421
422
424{
425 wxFileName cfgpath;
426 cfgpath.AssignDir( SETTINGS_MANAGER::GetUserSettingsPath() );
427 cfgpath.AppendDir( wxT( "3d" ) );
428 cfgpath.SetFullName( wxS( "3Dresolver.cfg" ) );
429 cfgpath.MakeAbsolute();
430
431 std::vector<LEGACY_3D_SEARCH_PATH> legacyPaths;
432 readLegacy3DResolverCfg( cfgpath.GetFullPath(), legacyPaths );
433
434 // env variables have a limited allowed character set for names
435 wxRegEx nonValidCharsRegex( wxS( "[^A-Z0-9_]+" ), wxRE_ADVANCED );
436
437 for( const LEGACY_3D_SEARCH_PATH& path : legacyPaths )
438 {
439 wxString key = path.m_Alias;
440 const wxString& val = path.m_Pathvar;
441
442 // The 3d alias config didnt use the same naming restrictions as real env variables
443 // We need to sanitize them
444
445 // upper case only
446 key.MakeUpper();
447 // logically swap - with _
448 key.Replace( wxS( "-" ), wxS( "_" ) );
449
450 // remove any other chars
451 nonValidCharsRegex.Replace( &key, wxEmptyString );
452
453 if( !m_Env.vars.count( key ) )
454 {
455 wxLogTrace( traceEnvVars, wxS( "COMMON_SETTINGS: Loaded new var: %s = %s" ), key, val );
456 m_Env.vars[key] = ENV_VAR_ITEM( key, val );
457 }
458 }
459
460 if( cfgpath.FileExists() )
461 {
462 wxRemoveFile( cfgpath.GetFullPath() );
463 }
464
465 return true;
466}
467
468
469bool COMMON_SETTINGS::MigrateFromLegacy( wxConfigBase* aCfg )
470{
471 bool ret = true;
472
473 ret &= fromLegacy<double>( aCfg, "CanvasScale", "appearance.canvas_scale" );
474 ret &= fromLegacy<int>( aCfg, "IconScale", "appearance.icon_scale" );
475 ret &= fromLegacy<bool>( aCfg, "UseIconsInMenus", "appearance.use_icons_in_menus" );
476 ret &= fromLegacy<bool>( aCfg, "ShowEnvVarWarningDialog", "environment.show_warning_dialog" );
477
478 auto load_env_vars =
479 [&]()
480 {
481 wxString key, value;
482 long index = 0;
483 nlohmann::json::json_pointer ptr = m_internals->PointerFromString( "environment.vars" );
484
485 aCfg->SetPath( "EnvironmentVariables" );
486 ( *m_internals )[ptr] = nlohmann::json( {} );
487
488 while( aCfg->GetNextEntry( key, index ) )
489 {
490 if( envVarBlacklist.count( key ) )
491 {
492 wxLogTrace( traceSettings, wxT( "Migrate Env: %s is blacklisted; skipping." ), key );
493 continue;
494 }
495
496 value = aCfg->Read( key, wxEmptyString );
497
498 if( !value.IsEmpty() )
499 {
500 ptr.push_back( key.ToStdString() );
501
502 wxLogTrace( traceSettings, wxT( "Migrate Env: %s=%s" ), ptr.to_string(), value );
503 ( *m_internals )[ptr] = value.ToUTF8();
504
505 ptr.pop_back();
506 }
507 }
508
509 aCfg->SetPath( ".." );
510 };
511
512 load_env_vars();
513
514 bool mousewheel_pan = false;
515
516 if( aCfg->Read( "MousewheelPAN", &mousewheel_pan ) && mousewheel_pan )
517 {
518 Set( "input.horizontal_pan", true );
519 Set( "input.scroll_modifier_pan_h", static_cast<int>( WXK_SHIFT ) );
520 Set( "input.scroll_modifier_pan_v", 0 );
521 Set( "input.scroll_modifier_zoom", static_cast<int>( WXK_CONTROL ) );
522 }
523
524 ret &= fromLegacy<bool>( aCfg, "AutoPAN", "input.auto_pan" );
525 ret &= fromLegacy<bool>( aCfg, "ImmediateActions", "input.immediate_actions" );
526 ret &= fromLegacy<bool>( aCfg, "PreferSelectionToDragging", "input.prefer_select_to_drag" );
527 ret &= fromLegacy<bool>( aCfg, "MoveWarpsCursor", "input.warp_mouse_on_move" );
528 ret &= fromLegacy<bool>( aCfg, "ZoomNoCenter", "input.center_on_zoom" );
529
530 // This was stored inverted in legacy config
531 if( std::optional<bool> value = Get<bool>( "input.center_on_zoom" ) )
532 Set( "input.center_on_zoom", !( *value ) );
533
534 ret &= fromLegacy<int>( aCfg, "OpenGLAntialiasingMode", "graphics.opengl_antialiasing_mode" );
535 ret &= fromLegacy<int>( aCfg, "CairoAntialiasingMode", "graphics.cairo_antialiasing_mode" );
536
537 ret &= fromLegacy<int>( aCfg, "AutoSaveInterval", "system.autosave_interval" );
538 ret &= fromLegacyString( aCfg, "Editor", "system.editor_name" );
539 ret &= fromLegacy<int>( aCfg, "FileHistorySize", "system.file_history_size" );
540 ret &= fromLegacyString( aCfg, "LanguageID", "system.language" );
541 ret &= fromLegacyString( aCfg, "PdfBrowserName", "system.pdf_viewer_name" );
542 ret &= fromLegacy<bool>( aCfg, "UseSystemBrowser", "system.use_system_pdf_viewer" );
543 ret &= fromLegacyString( aCfg, "WorkingDir", "system.working_dir" );
544
545 return ret;
546}
547
548
550{
551 auto addVar =
552 [&]( const wxString& aKey, const wxString& aDefault )
553 {
554 m_Env.vars[aKey] = ENV_VAR_ITEM( aKey, aDefault, aDefault );
555
556 wxString envValue;
557
558 if( wxGetEnv( aKey, &envValue ) == true && !envValue.IsEmpty() )
559 {
560 m_Env.vars[aKey].SetValue( envValue );
561 m_Env.vars[aKey].SetDefinedExternally();
562 wxLogTrace( traceEnvVars,
563 wxS( "InitializeEnvironment: Entry %s defined externally as %s" ), aKey,
564 envValue );
565 }
566 else
567 {
568 wxLogTrace( traceEnvVars, wxS( "InitializeEnvironment: Setting entry %s to default %s" ),
569 aKey, aDefault );
570 }
571 };
572
573 wxFileName basePath( PATHS::GetStockEDALibraryPath(), wxEmptyString );
574
575 wxFileName path( basePath );
576 path.AppendDir( wxT( "footprints" ) );
577 addVar( wxT( "KICAD7_FOOTPRINT_DIR" ), path.GetFullPath() );
578
579 path = basePath;
580 path.AppendDir( wxT( "3dmodels" ) );
581 addVar( wxT( "KICAD7_3DMODEL_DIR" ), path.GetFullPath() );
582
583 addVar( wxT( "KICAD7_TEMPLATE_DIR" ), PATHS::GetStockTemplatesPath() );
584
585 addVar( wxT( "KICAD_USER_TEMPLATE_DIR" ), PATHS::GetUserTemplatesPath() );
586
587 addVar( wxT( "KICAD7_3RD_PARTY" ), PATHS::GetDefault3rdPartyPath() );
588
589 path = basePath;
590 path.AppendDir( wxT( "symbols" ) );
591 addVar( wxT( "KICAD7_SYMBOL_DIR" ), path.GetFullPath() );
592}
593
594
596 std::vector<LEGACY_3D_SEARCH_PATH>& aSearchPaths )
597{
598 wxFileName cfgpath( path );
599
600 // This should be the same as wxWidgets 3.0 wxPATH_NORM_ALL which is deprecated in 3.1.
601 // There are known issues with environment variable expansion so maybe we should be using
602 // our own ExpandEnvVarSubstitutions() here instead.
603 cfgpath.Normalize( FN_NORMALIZE_FLAGS | wxPATH_NORM_ENV_VARS );
604 wxString cfgname = cfgpath.GetFullPath();
605
606 std::ifstream cfgFile;
607 std::string cfgLine;
608
609 if( !wxFileName::Exists( cfgname ) )
610 {
611 std::ostringstream ostr;
612 ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
613 wxString errmsg = "no 3D configuration file";
614 ostr << " * " << errmsg.ToUTF8() << " '";
615 ostr << cfgname.ToUTF8() << "'";
616 wxLogTrace( traceSettings, "%s\n", ostr.str().c_str() );
617 return false;
618 }
619
620 cfgFile.open( cfgname.ToUTF8() );
621
622 if( !cfgFile.is_open() )
623 {
624 std::ostringstream ostr;
625 ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
626 wxString errmsg = wxS( "Could not open configuration file" );
627 ostr << " * " << errmsg.ToUTF8() << " '" << cfgname.ToUTF8() << "'";
628 wxLogTrace( traceSettings, wxS( "%s\n" ), ostr.str().c_str() );
629 return false;
630 }
631
632 int lineno = 0;
634 size_t idx;
635 int vnum = 0; // version number
636
637 while( cfgFile.good() )
638 {
639 cfgLine.clear();
640 std::getline( cfgFile, cfgLine );
641 ++lineno;
642
643 if( cfgLine.empty() )
644 {
645 if( cfgFile.eof() )
646 break;
647
648 continue;
649 }
650
651 if( 1 == lineno && cfgLine.compare( 0, 2, "#V" ) == 0 )
652 {
653 // extract the version number and parse accordingly
654 if( cfgLine.size() > 2 )
655 {
656 std::istringstream istr;
657 istr.str( cfgLine.substr( 2 ) );
658 istr >> vnum;
659 }
660
661 continue;
662 }
663
664 idx = 0;
665
666 if( !getLegacy3DHollerith( cfgLine, idx, al.m_Alias ) )
667 continue;
668
669 // Don't add KICAD7_3DMODEL_DIR, one of its legacy equivalents, or KIPRJMOD from a
670 // config file. They're system variables are are defined at runtime.
671 if( al.m_Alias == wxS( "${KICAD7_3DMODEL_DIR}" ) || al.m_Alias == wxS( "${KIPRJMOD}" )
672 || al.m_Alias == wxS( "$(KIPRJMOD)" ) || al.m_Alias == wxS( "${KISYS3DMOD}" )
673 || al.m_Alias == wxS( "$(KISYS3DMOD)" ) )
674 {
675 continue;
676 }
677
678 if( !getLegacy3DHollerith( cfgLine, idx, al.m_Pathvar ) )
679 continue;
680
681 if( !getLegacy3DHollerith( cfgLine, idx, al.m_Description ) )
682 continue;
683
684 aSearchPaths.push_back( al );
685 }
686
687 cfgFile.close();
688
689 return true;
690}
691
692
693bool COMMON_SETTINGS::getLegacy3DHollerith( const std::string& aString, size_t& aIndex,
694 wxString& aResult )
695{
696 aResult.clear();
697
698 if( aIndex >= aString.size() )
699 {
700 std::ostringstream ostr;
701 ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
702 wxString errmsg = wxS( "bad Hollerith string on line" );
703 ostr << " * " << errmsg.ToUTF8() << "\n'" << aString << "'";
704 wxLogTrace( traceSettings, wxS( "%s\n" ), ostr.str().c_str() );
705
706 return false;
707 }
708
709 size_t i2 = aString.find( '"', aIndex );
710
711 if( std::string::npos == i2 )
712 {
713 std::ostringstream ostr;
714 ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
715 wxString errmsg = wxS( "missing opening quote mark in config file" );
716 ostr << " * " << errmsg.ToUTF8() << "\n'" << aString << "'";
717 wxLogTrace( traceSettings, wxS( "%s\n" ), ostr.str().c_str() );
718
719 return false;
720 }
721
722 ++i2;
723
724 if( i2 >= aString.size() )
725 {
726 std::ostringstream ostr;
727 ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
728 wxString errmsg = wxS( "invalid entry (unexpected end of line)" );
729 ostr << " * " << errmsg.ToUTF8() << "\n'" << aString << "'";
730 wxLogTrace( traceSettings, wxS( "%s\n" ), ostr.str().c_str() );
731
732 return false;
733 }
734
735 std::string tnum;
736
737 while( aString[i2] >= '0' && aString[i2] <= '9' )
738 tnum.append( 1, aString[i2++] );
739
740 if( tnum.empty() || aString[i2++] != ':' )
741 {
742 std::ostringstream ostr;
743 ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
744 wxString errmsg = wxS( "bad Hollerith string on line" );
745 ostr << " * " << errmsg.ToUTF8() << "\n'" << aString << "'";
746 wxLogTrace( traceSettings, wxS( "%s\n" ), ostr.str().c_str() );
747
748 return false;
749 }
750
751 std::istringstream istr;
752 istr.str( tnum );
753 size_t nchars;
754 istr >> nchars;
755
756 if( ( i2 + nchars ) >= aString.size() )
757 {
758 std::ostringstream ostr;
759 ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
760 wxString errmsg = wxS( "invalid entry (unexpected end of line)" );
761 ostr << " * " << errmsg.ToUTF8() << "\n'" << aString << "'";
762 wxLogTrace( traceSettings, wxS( "%s\n" ), ostr.str().c_str() );
763
764 return false;
765 }
766
767 if( nchars > 0 )
768 {
769 aResult = wxString::FromUTF8( aString.substr( i2, nchars ).c_str() );
770 i2 += nchars;
771 }
772
773 if( i2 >= aString.size() || aString[i2] != '"' )
774 {
775 std::ostringstream ostr;
776 ostr << __FILE__ << ": " << __FUNCTION__ << ": " << __LINE__ << "\n";
777 wxString errmsg = wxS( "missing closing quote mark in config file" );
778 ostr << " * " << errmsg.ToUTF8() << "\n'" << aString << "'";
779 wxLogTrace( traceSettings, wxS( "%s\n" ), ostr.str().c_str() );
780
781 return false;
782 }
783
784 aIndex = i2 + 1;
785 return true;
786}
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.
std::vector< PARAM_BASE * > m_params
The list of parameters (owned by this object)
std::unique_ptr< JSON_SETTINGS_INTERNALS > m_internals
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...
Stores an enum as an integer.
Definition: parameters.h:217
static wxString GetUserTemplatesPath()
Gets the user path for custom templates.
Definition: paths.cpp:86
static wxString GetStockEDALibraryPath()
Gets the stock (install) EDA library data path, which is the base path for templates,...
Definition: paths.cpp:186
static wxString GetDefault3rdPartyPath()
Gets the default path for PCM packages.
Definition: paths.cpp:129
static wxString GetStockTemplatesPath()
Gets the stock (install) templates path.
Definition: paths.cpp:242
static wxString GetUserSettingsPath()
Return the user configuration path used to store KiCad's configuration files.
const std::set< wxString > envVarBlacklist
! The following environment variables will never be migrated from a previous version
const int commonSchemaVersion
! Update the schema version whenever a migration is required
nlohmann::json json
Definition: gerbview.cpp:44
const wxChar *const traceEnvVars
Flag to enable debug output of environment variable operations.
const wxChar *const traceSettings
Flag to enable debug output of settings operations and management.
SETTINGS_LOC
Definition: json_settings.h:47
@ USER
The main config directory (e.g. ~/.config/kicad/)
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:38