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