KiCad PCB EDA Suite
Loading...
Searching...
No Matches
pgm_base.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) 2004-2015 Jean-Pierre Charras, jp.charras at wanadoo.fr
5 * Copyright (C) 2008 Wayne Stambaugh <[email protected]>
6 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, you may find one here:
20 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21 * or you may search the http://www.gnu.org website for the version 2 license,
22 * or you may write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24 */
25
32
33#include <wx/html/htmlwin.h>
34#include <wx/fs_zip.h>
35#include <wx/dir.h>
36#include <wx/filename.h>
37#include <wx/msgdlg.h>
38#include <wx/propgrid/propgrid.h>
39#include <wx/snglinst.h>
40#include <wx/stdpaths.h>
41#include <wx/sysopt.h>
42#include <wx/filedlg.h>
43#include <wx/ffile.h>
44#include <wx/tooltip.h>
45
46#include <advanced_config.h>
47#include <app_monitor.h>
49#include <bitmaps.h>
50#include <build_version.h>
51#include <common.h>
52#include <confirm.h>
53#include <core/arraydim.h>
54#include <id.h>
56#include <kiplatform/policy.h>
57#include <macros.h>
59#include <paths.h>
60#include <pgm_base.h>
61#include <policy_keys.h>
62#include <python_scripting.h>
65#include <string_utils.h>
66#include <systemdirsappend.h>
67#include <thread_pool.h>
68#include <trace_helpers.h>
69
70#include <widgets/wx_splash.h>
71
72#ifdef KICAD_IPC_API
74#include <api/api_server.h>
75#include <python_manager.h>
76#endif
77
78#ifdef _MSC_VER
79#include <winrt/base.h>
80#endif
90#undef _
91#define _(s) s
93{
94 { wxLANGUAGE_DEFAULT, ID_LANGUAGE_DEFAULT, _( "Default" ), false },
95 { wxLANGUAGE_ARABIC, ID_LANGUAGE_ARABIC, wxT( "العربية" ), true },
96 { wxLANGUAGE_INDONESIAN, ID_LANGUAGE_INDONESIAN, wxT( "Bahasa Indonesia" ), true },
97 { wxLANGUAGE_BULGARIAN, ID_LANGUAGE_BULGARIAN, wxT( "Български" ), true },
98 { wxLANGUAGE_CATALAN, ID_LANGUAGE_CATALAN, wxT( "Català" ), true },
99 { wxLANGUAGE_CZECH, ID_LANGUAGE_CZECH, wxT( "Čeština" ), true },
100 { wxLANGUAGE_DANISH, ID_LANGUAGE_DANISH, wxT( "Dansk" ), true },
101 { wxLANGUAGE_GERMAN, ID_LANGUAGE_GERMAN, wxT( "Deutsch" ), true },
102 { wxLANGUAGE_GREEK, ID_LANGUAGE_GREEK, wxT( "Ελληνικά" ), true },
103 { wxLANGUAGE_ENGLISH, ID_LANGUAGE_ENGLISH, wxT( "English" ), true },
104 { wxLANGUAGE_SPANISH, ID_LANGUAGE_SPANISH, wxT( "Español" ), true },
105 { wxLANGUAGE_SPANISH_MEXICAN, ID_LANGUAGE_SPANISH_MEXICAN,
106 wxT( "Español (Latinoamericano)" ), true },
107 { wxLANGUAGE_FRENCH, ID_LANGUAGE_FRENCH, wxT( "Français" ), true },
108 { wxLANGUAGE_HEBREW, ID_LANGUAGE_HEBREW, wxT( "עברית" ), true },
109 { wxLANGUAGE_KOREAN, ID_LANGUAGE_KOREAN, wxT( "한국어"), true },
110 { wxLANGUAGE_ITALIAN, ID_LANGUAGE_ITALIAN, wxT( "Italiano" ), true },
111 { wxLANGUAGE_LATVIAN, ID_LANGUAGE_LATVIAN, wxT( "Latviešu" ), true },
112 { wxLANGUAGE_LITHUANIAN, ID_LANGUAGE_LITHUANIAN, wxT( "Lietuvių" ), true },
113 { wxLANGUAGE_HUNGARIAN, ID_LANGUAGE_HUNGARIAN, wxT( "Magyar" ), true },
114 { wxLANGUAGE_DUTCH, ID_LANGUAGE_DUTCH, wxT( "Nederlands" ), true },
115 { wxLANGUAGE_NORWEGIAN_BOKMAL, ID_LANGUAGE_NORWEGIAN_BOKMAL, wxT( "Norsk Bokmål" ), true },
116 { wxLANGUAGE_JAPANESE, ID_LANGUAGE_JAPANESE, wxT( "日本語" ), true },
117 { wxLANGUAGE_THAI, ID_LANGUAGE_THAI, wxT( "ภาษาไทย" ), true },
118 { wxLANGUAGE_POLISH, ID_LANGUAGE_POLISH, wxT( "Polski" ), true },
119 { wxLANGUAGE_PORTUGUESE, ID_LANGUAGE_PORTUGUESE, wxT( "Português" ),true },
120 { wxLANGUAGE_PORTUGUESE_BRAZILIAN, ID_LANGUAGE_PORTUGUESE_BRAZILIAN,
121 wxT( "Português (Brasil)" ), true },
122 { wxLANGUAGE_ROMANIAN, ID_LANGUAGE_ROMANIAN, wxT( "Română" ), true },
123 { wxLANGUAGE_RUSSIAN, ID_LANGUAGE_RUSSIAN, wxT( "Русский" ), true },
124 { wxLANGUAGE_SERBIAN, ID_LANGUAGE_SERBIAN, wxT( "Српски" ), true },
125 { wxLANGUAGE_SLOVAK, ID_LANGUAGE_SLOVAK, wxT( "Slovenčina" ), true },
126 { wxLANGUAGE_SLOVENIAN, ID_LANGUAGE_SLOVENIAN, wxT( "Slovenščina" ), true },
127 { wxLANGUAGE_FINNISH, ID_LANGUAGE_FINNISH, wxT( "Suomi" ), true },
128 { wxLANGUAGE_SWEDISH, ID_LANGUAGE_SWEDISH, wxT( "Svenska" ), true },
129 { wxLANGUAGE_VIETNAMESE, ID_LANGUAGE_VIETNAMESE, wxT( "Tiếng Việt" ), true },
130 { wxLANGUAGE_TAMIL, ID_LANGUAGE_TAMIL, wxT( "தமிழ்" ), true },
131 { wxLANGUAGE_TURKISH, ID_LANGUAGE_TURKISH, wxT( "Türkçe" ), true },
132 { wxLANGUAGE_UKRAINIAN, ID_LANGUAGE_UKRAINIAN, wxT( "Українська" ), true },
133 { wxLANGUAGE_CHINESE_SIMPLIFIED, ID_LANGUAGE_CHINESE_SIMPLIFIED,
134 wxT( "简体中文" ), true },
135 { wxLANGUAGE_CHINESE_TRADITIONAL, ID_LANGUAGE_CHINESE_TRADITIONAL,
136 wxT( "繁體中文" ), true },
137 { 0, 0, "", false } // Sentinel
138};
139#undef _
140#define _(s) wxGetTranslation((s))
141
142
144{
145 m_locale = nullptr;
146 m_Printing = false;
147 m_Quitting = false;
148 m_argcUtf8 = 0;
149 m_argvUtf8 = nullptr;
150 m_splash = nullptr;
152
153 setLanguageId( wxLANGUAGE_DEFAULT );
154
155 ForceSystemPdfBrowser( false );
156}
157
158
160{
161 HideSplash();
162 Destroy();
163
164 for( int n = 0; n < m_argcUtf8; n++ )
165 {
166 free( m_argvUtf8[n] );
167 }
168
169 delete[] m_argvUtf8;
170
171 delete m_locale;
172 m_locale = nullptr;
173}
174
175
177{
179
181
182 m_pgm_checker.reset();
183
184#ifdef _MSC_VER
185 winrt::uninit_apartment();
186#endif
187}
188
189
191{
192 wxASSERT( wxTheApp );
193 return *wxTheApp;
194}
195
196
197void PGM_BASE::SetTextEditor( const wxString& aFileName )
198{
199 m_text_editor = aFileName;
201}
202
203
204const wxString& PGM_BASE::GetTextEditor( bool aCanShowFileChooser )
205{
206 wxString editorname = m_text_editor;
207
208 if( !editorname )
209 {
210 if( !wxGetEnv( wxT( "EDITOR" ), &editorname ) )
211 {
212 // If there is no EDITOR variable set, try the desktop default
213#ifdef __WXMAC__
214 editorname = wxT( "/usr/bin/open -e" );
215#elif __WXX11__
216 editorname = wxT( "/usr/bin/xdg-open" );
217#endif
218 }
219 }
220
221 // If we still don't have an editor name show a dialog asking the user to select one
222 if( !editorname && aCanShowFileChooser )
223 {
224 DisplayInfoMessage( nullptr, _( "No default editor found, you must choose one." ) );
225
226 editorname = AskUserForPreferredEditor();
227 }
228
229 // If we finally have a new editor name request it to be copied to m_text_editor and
230 // saved to the preferences file.
231 if( !editorname.IsEmpty() )
232 SetTextEditor( editorname );
233
234 // m_text_editor already has the same value that editorname, or empty if no editor was
235 // found/chosen.
236 return m_text_editor;
237}
238
239
240const wxString PGM_BASE::AskUserForPreferredEditor( const wxString& aDefaultEditor )
241{
242 // Create a mask representing the executable files in the current platform
243#ifdef __WINDOWS__
244 wxString mask( _( "Executable file" ) + wxT( " (*.exe)|*.exe" ) );
245#else
246 wxString mask( _( "Executable file" ) + wxT( " (*)|*" ) );
247#endif
248
249 // Extract the path, name and extension from the default editor (even if the editor's
250 // name was empty, this method will succeed and return empty strings).
251 wxString path, name, ext;
252 wxFileName::SplitPath( aDefaultEditor, &path, &name, &ext );
253
254 // Show the modal editor and return the file chosen (may be empty if the user cancels
255 // the dialog).
256 return wxFileSelector( _( "Select Preferred Editor" ), path, name, wxT( "." ) + ext,
257 mask, wxFD_OPEN | wxFD_FILE_MUST_EXIST, nullptr );
258}
259
260
261#ifdef KICAD_USE_SENTRY
262void PGM_BASE::sentryPrompt()
263{
264 if( !IsGUI() )
265 return;
266
268
271 {
272 wxMessageDialog optIn = wxMessageDialog(
273 nullptr,
274 _( "KiCad can anonymously report crashes and special event data to developers in order to "
275 "aid identifying critical bugs and help profile functionality to guide improvements. \n"
276 "If you choose to voluntarily participate, KiCad will automatically send said reports "
277 "when crashes or events occur. \n"
278 "Your design files such as schematic and PCB are not shared in this process." ),
279 _( "Data Collection Opt In" ), wxYES_NO | wxCENTRE );
280
281 optIn.SetYesNoLabels( _( "Opt In" ), _( "Decline" ) );
282 int result = optIn.ShowModal();
283
284 if( result == wxID_YES )
285 {
287 }
288 else
289 {
291 }
292
293 m_settings_manager->GetCommonSettings()->m_DoNotShowAgain.data_collection_prompt = true;
294 }
295}
296#endif
297
298
300{
301 const wxArrayString& argArray = App().argv.GetArguments();
302 m_argcUtf8 = argArray.size();
303
304 m_argvUtf8 = new char*[m_argcUtf8 + 1];
305 for( int n = 0; n < m_argcUtf8; n++ )
306 {
307 m_argvUtf8[n] = wxStrdup( argArray[n].ToUTF8() );
308 }
309
310 m_argvUtf8[m_argcUtf8] = NULL; // null terminator at end of argv
311}
312
313
315{
316 // Disabling until we change to load each DSO at startup rather than lazy-load when needed.
317 // Note that once the splash screen is re-enabled, there are some remaining bugs to fix:
318 // Any wxWidgets error dialogs that appear during startup are hidden by the splash screen,
319 // so we either need to prevent these from happening (probably not feasible) or else change
320 // the error-handling path to make sure errors go on top of the splash.
321#if 0
322 if( m_splash )
323 return;
324
325 m_splash = new WX_SPLASH( KiBitmap( BITMAPS::splash ), wxSPLASH_CENTRE_ON_SCREEN, 0,
326 NULL, -1, wxDefaultPosition, wxDefaultSize,
327 wxBORDER_NONE | wxSTAY_ON_TOP );
328 wxYield();
329#endif
330}
331
332
334{
335 if( !m_splash )
336 return;
337
338 m_splash->Close( true );
339 m_splash->Destroy();
340 m_splash = nullptr;
341}
342
343
344bool PGM_BASE::InitPgm( bool aHeadless, bool aSkipPyInit, bool aIsUnitTest )
345{
346#if defined( __WXMAC__ )
347 // Set the application locale to the system default
348 wxLogNull noLog;
349 wxLocale loc;
350 loc.Init();
351#endif
352
353 // Just make sure we init precreate any folders early for later code
354 // In particular, the user cache path is the most likely to be hit by startup code
356
358
360
361 // Initialize the singleton instance
362 m_singleton.Init();
363
364 wxString pgm_name;
365
367 if( App().argc == 0 )
368 pgm_name = wxT( "kicad" );
369 else
370 pgm_name = wxFileName( App().argv[0] ).GetName().Lower();
371
372 APP_MONITOR::SENTRY::Instance()->AddTag( "kicad.app", pgm_name );
373
374 wxInitAllImageHandlers();
375
376 // Without this the wxPropertyGridManager segfaults on Windows.
377 if( !wxPGGlobalVars )
378 wxPGInitResourceModule();
379
380#ifndef __WINDOWS__
381 if( wxString( wxGetenv( "HOME" ) ).IsEmpty() )
382 {
383 DisplayErrorMessage( nullptr, _( "Environment variable HOME is empty. "
384 "Unable to continue." ) );
385 return false;
386 }
387#endif
388
389 // Ensure the instance checker directory exists
390 // It should be globally writable because it is shared between all users on Linux, and so on a
391 // multi-user machine, other need to be able to access it to check for the lock files or make
392 // their own lock files.
393 wxString instanceCheckerDir = PATHS::GetInstanceCheckerPath();
394 PATHS::EnsurePathExists( instanceCheckerDir );
395 wxChmod( instanceCheckerDir,
396 wxPOSIX_USER_READ | wxPOSIX_USER_WRITE | wxPOSIX_USER_EXECUTE |
397 wxPOSIX_GROUP_READ | wxPOSIX_GROUP_WRITE | wxPOSIX_GROUP_EXECUTE |
398 wxPOSIX_OTHERS_READ | wxPOSIX_OTHERS_WRITE | wxPOSIX_OTHERS_EXECUTE );
399
400 wxString instanceCheckerName = wxString::Format( wxS( "%s-%s" ), pgm_name,
402
403 m_pgm_checker = std::make_unique<wxSingleInstanceChecker>();
404 m_pgm_checker->Create( instanceCheckerName, instanceCheckerDir );
405
406 // Init KiCad environment
407 // the environment variable KICAD (if exists) gives the kicad path:
408 // something like set KICAD=d:\kicad
409 bool isDefined = wxGetEnv( wxT( "KICAD" ), &m_kicad_env );
410
411 if( isDefined ) // ensure m_kicad_env ends by "/"
412 {
414
415 if( !m_kicad_env.IsEmpty() && m_kicad_env.Last() != '/' )
417 }
418
419 // Init parameters for configuration
420 App().SetVendorName( wxT( "KiCad" ) );
421 App().SetAppName( pgm_name );
422
423 // Install some image handlers, mainly for help
424 if( wxImage::FindHandler( wxBITMAP_TYPE_PNG ) == nullptr )
425 wxImage::AddHandler( new wxPNGHandler );
426
427 if( wxImage::FindHandler( wxBITMAP_TYPE_GIF ) == nullptr )
428 wxImage::AddHandler( new wxGIFHandler );
429
430 if( wxImage::FindHandler( wxBITMAP_TYPE_JPEG ) == nullptr )
431 wxImage::AddHandler( new wxJPEGHandler );
432
433 wxFileSystem::AddHandler( new wxZipFSHandler );
434
435 // Analyze the command line & initialize the binary path
436 wxString tmp;
438 SetDefaultLanguage( tmp );
439
440#ifdef _MSC_VER
441 if( !wxGetEnv( "FONTCONFIG_PATH", NULL ) )
442 {
443 // We need to set this because the internal fontconfig logic
444 // seems to search relative to the dll rather the other logic it
445 // has to look for the /etc folder above the dll
446 // Also don't set it because we need it in QA cli tests to be set by ctest
447 wxSetEnv( "FONTCONFIG_PATH", PATHS::GetWindowsFontConfigDir() );
448 }
449#endif
450
451#ifdef _MSC_VER
452 winrt::init_apartment(winrt::apartment_type::single_threaded);
453#endif
454
455 m_settings_manager = std::make_unique<SETTINGS_MANAGER>( aHeadless );
456 m_background_jobs_monitor = std::make_unique<BACKGROUND_JOBS_MONITOR>();
457 m_notifications_manager = std::make_unique<NOTIFICATIONS_MANAGER>();
458
459#ifdef KICAD_IPC_API
460 m_plugin_manager = std::make_unique<API_PLUGIN_MANAGER>( &App() );
461#endif
462
463 // Our unit test mocks break if we continue
464 // A bug caused InitPgm to terminate early in unit tests and the mocks are...simplistic
465 // TODO fix the unit tests so this can be removed
466 if( aIsUnitTest )
467 return false;
468
469 // Something got in the way of settings load: can't continue
470 if( !m_settings_manager->IsOK() )
471 return false;
472
473 // Set up built-in environment variables (and override them from the system environment if set)
474 COMMON_SETTINGS* commonSettings = GetCommonSettings();
475 commonSettings->InitializeEnvironment();
476
477 // Load color settings after env is initialized
478 m_settings_manager->ReloadColorSettings();
479
480 // Load common settings from disk after setting up env vars
481 GetSettingsManager().Load( commonSettings );
482
483#ifdef KICAD_IPC_API
484 // If user doesn't have a saved Python interpreter, try (potentially again) to find one
485 if( commonSettings->m_Api.python_interpreter.IsEmpty() )
486 commonSettings->m_Api.python_interpreter = PYTHON_MANAGER::FindPythonInterpreter();
487#endif
488
489 // Init user language *before* calling loadSettings, because
490 // env vars could be incorrectly initialized on Linux
491 // (if the value contains some non ASCII7 chars, the env var is not initialized)
492 SetLanguage( tmp, true );
493
494 // Now that translations are available, inform the user if the OS is unsupported
496
498
499#ifdef KICAD_USE_SENTRY
500 sentryPrompt();
501#endif
502
503 ReadPdfBrowserInfos(); // needs GetCommonSettings()
504
506
507 // Create the python scripting stuff
508 // Skip it for applications that do not use it
509 if( !aSkipPyInit )
510 m_python_scripting = std::make_unique<SCRIPTING>();
511
512 // TODO(JE): Remove this if apps are refactored to not assume Prj() always works
513 // Need to create a project early for now (it can have an empty path for the moment)
515
516#ifdef KICAD_IPC_API
517 if( commonSettings->m_Api.enable_server )
518 m_plugin_manager->ReloadPlugins();
519#endif
520
521 // This sets the maximum tooltip display duration to 10s (up from 5) but only affects
522 // Windows as other platforms display tooltips while the mouse is not moving
523 if( !aHeadless )
524 {
525 wxToolTip::Enable( true );
526 wxToolTip::SetAutoPop( 10000 );
527 }
528
529 if( ADVANCED_CFG::GetCfg().m_UpdateUIEventInterval != 0 )
530 wxUpdateUIEvent::SetUpdateInterval( ADVANCED_CFG::GetCfg().m_UpdateUIEventInterval );
531
532 // Now the application can safely start, show the splash screen
533 if( !aHeadless )
534 ShowSplash();
535
536 return true;
537}
538
539
541{
543
544 for( const std::pair<wxString, ENV_VAR_ITEM> it : GetCommonSettings()->m_Env.vars )
545 {
546 wxLogTrace( traceEnvVars, wxT( "PGM_BASE::loadSettings: Found entry %s = %s" ),
547 it.first, it.second.GetValue() );
548
549 // Do not store the env var PROJECT_VAR_NAME ("KIPRJMOD") definition if for some reason
550 // it is found in config. (It is reserved and defined as project path)
551 if( it.first == PROJECT_VAR_NAME )
552 continue;
553
554 // Don't set bogus empty entries in the environment
555 if( it.first.IsEmpty() )
556 continue;
557
558 // Do not overwrite vars set by the system environment with values from the settings file
559 if( it.second.GetDefinedExternally() )
560 continue;
561
562 SetLocalEnvVariable( it.first, it.second.GetValue() );
563 }
564}
565
566
568{
569 // GetCommonSettings() is not initialized until fairly late in the
570 // process startup: InitPgm(), so test before using:
571 if( GetCommonSettings() )
572 GetCommonSettings()->m_System.working_dir = wxGetCwd();
573}
574
575
577{
578 return m_settings_manager ? m_settings_manager->GetCommonSettings() : nullptr;
579}
580
581
582bool PGM_BASE::SetLanguage( wxString& aErrMsg, bool first_time )
583{
584 // Suppress wxWidgets error popups if locale is not found
585 wxLogNull doNotLog;
586
587 if( first_time )
588 {
589 setLanguageId( wxLANGUAGE_DEFAULT );
590
591 // First time SetLanguage is called, the user selected language id is set
592 // from common user config settings
593 wxString languageSel = GetCommonSettings()->m_System.language;
594
595 // Search for the current selection
596 for( unsigned ii = 0; LanguagesList[ii].m_KI_Lang_Identifier != 0; ii++ )
597 {
598 if( LanguagesList[ii].m_Lang_Label == languageSel )
599 {
600 setLanguageId( LanguagesList[ii].m_WX_Lang_Identifier );
601 break;
602 }
603 }
604 }
605
606 // dictionary file name without extend (full name is kicad.mo)
607 wxString dictionaryName( wxT( "kicad" ) );
608
609 delete m_locale;
610 m_locale = new wxLocale;
611
612 // don't use wxLOCALE_LOAD_DEFAULT flag so that Init() doesn't return
613 // false just because it failed to load wxstd catalog
614 if( !m_locale->Init( m_language_id ) )
615 {
616 wxLogTrace( traceLocale, wxT( "This language is not supported by the system." ) );
617
618 setLanguageId( wxLANGUAGE_DEFAULT );
619 delete m_locale;
620
621 m_locale = new wxLocale;
622 m_locale->Init( wxLANGUAGE_DEFAULT );
623
624 aErrMsg = _( "This language is not supported by the operating system." );
625 return false;
626 }
627 else if( !first_time )
628 {
629 wxLogTrace( traceLocale, wxT( "Search for dictionary %s.mo in %s" ) ,
630 dictionaryName, m_locale->GetName() );
631 }
632
633 if( !first_time )
634 {
635 // If we are here, the user has selected another language.
636 // Therefore the new preferred language name is stored in common config.
637 // Do NOT store the wxWidgets language Id, it can change between wxWidgets
638 // versions, for a given language
639 wxString languageSel;
640
641 // Search for the current selection language name
642 for( unsigned ii = 0; LanguagesList[ii].m_KI_Lang_Identifier != 0; ii++ )
643 {
644 if( LanguagesList[ii].m_WX_Lang_Identifier == m_language_id )
645 {
646 languageSel = LanguagesList[ii].m_Lang_Label;
647 break;
648 }
649 }
650
652 cfg->m_System.language = languageSel;
653 cfg->SaveToFile( GetSettingsManager().GetPathForSettingsFile( cfg ) );
654 }
655
656 // Try adding the dictionary if it is not currently loaded
657 if( !m_locale->IsLoaded( dictionaryName ) )
658 m_locale->AddCatalog( dictionaryName );
659
660 // Verify the Kicad dictionary was loaded properly
661 // However, for the English language, the dictionary is not mandatory, as
662 // all messages are already in English, just restricted to ASCII7 chars,
663 // the verification is skipped.
664 if( !m_locale->IsLoaded( dictionaryName ) && m_language_id != wxLANGUAGE_ENGLISH )
665 {
666 wxLogTrace( traceLocale, wxT( "Unable to load dictionary %s.mo in %s" ),
667 dictionaryName, m_locale->GetName() );
668
669 setLanguageId( wxLANGUAGE_DEFAULT );
670 delete m_locale;
671
672 m_locale = new wxLocale;
673 m_locale->Init( wxLANGUAGE_DEFAULT );
674
675 aErrMsg = _( "The KiCad language file for this language is not installed." );
676 return false;
677 }
678
679 return true;
680}
681
682
683bool PGM_BASE::SetDefaultLanguage( wxString& aErrMsg )
684{
685 // Suppress error popups from wxLocale
686 wxLogNull doNotLog;
687
688 setLanguageId( wxLANGUAGE_DEFAULT );
689
690 // dictionary file name without extend (full name is kicad.mo)
691 wxString dictionaryName( wxT( "kicad" ) );
692
693 delete m_locale;
694 m_locale = new wxLocale;
695 m_locale->Init();
696
697 // Try adding the dictionary if it is not currently loaded
698 if( !m_locale->IsLoaded( dictionaryName ) )
699 m_locale->AddCatalog( dictionaryName );
700
701 // Verify the Kicad dictionary was loaded properly
702 // However, for the English language, the dictionary is not mandatory, as
703 // all messages are already in English, just restricted to ASCII7 chars,
704 // the verification is skipped.
705 if( !m_locale->IsLoaded( dictionaryName ) && m_language_id != wxLANGUAGE_ENGLISH )
706 {
707 wxLogTrace( traceLocale, wxT( "Unable to load dictionary %s.mo in %s" ),
708 dictionaryName, m_locale->GetName() );
709
710 setLanguageId( wxLANGUAGE_DEFAULT );
711 delete m_locale;
712
713 m_locale = new wxLocale;
714 m_locale->Init();
715
716 aErrMsg = _( "The KiCad language file for this language is not installed." );
717 return false;
718 }
719
720 return true;
721}
722
723
725{
726 wxLogTrace( traceLocale, wxT( "Select language ID %d from %d possible languages." ),
727 menu_id, (int)arrayDim( LanguagesList )-1 );
728
729 for( unsigned ii = 0; LanguagesList[ii].m_KI_Lang_Identifier != 0; ii++ )
730 {
731 if( menu_id == LanguagesList[ii].m_KI_Lang_Identifier )
732 {
733 setLanguageId( LanguagesList[ii].m_WX_Lang_Identifier );
734 break;
735 }
736 }
737}
738
739
741{
742 const wxLanguageInfo* langInfo = wxLocale::GetLanguageInfo( m_language_id );
743
744 if( !langInfo )
745 {
746 return "";
747 }
748 else
749 {
750 wxString str = langInfo->GetCanonicalWithRegion();
751 str.Replace( "_", "-" );
752
753 return str;
754 }
755}
756
757
759{
760#ifdef _MSC_VER
761 wxLocale::AddCatalogLookupPathPrefix( PATHS::GetWindowsBaseSharePath() + wxT( "locale" ) );
762#endif
763 wxLocale::AddCatalogLookupPathPrefix( PATHS::GetLocaleDataPath() );
764
765 if( wxGetEnv( wxT( "KICAD_RUN_FROM_BUILD_DIR" ), nullptr ) )
766 {
767 wxFileName fn( Pgm().GetExecutablePath() );
768 fn.RemoveLastDir();
769 fn.AppendDir( wxT( "translation" ) );
770 wxLocale::AddCatalogLookupPathPrefix( fn.GetPath() );
771 }
772}
773
774
775bool PGM_BASE::SetLocalEnvVariable( const wxString& aName, const wxString& aValue )
776{
777 wxString env;
778
779 if( aName.IsEmpty() )
780 {
781 wxLogTrace( traceEnvVars,
782 wxT( "PGM_BASE::SetLocalEnvVariable: Attempt to set empty variable to "
783 "value %s" ),
784 aValue );
785 return false;
786 }
787
788 // Check to see if the environment variable is already set.
789 if( wxGetEnv( aName, &env ) )
790 {
791 wxLogTrace( traceEnvVars,
792 wxT( "PGM_BASE::SetLocalEnvVariable: Environment variable %s already set "
793 "to %s" ),
794 aName, env );
795 return env == aValue;
796 }
797
798 wxLogTrace( traceEnvVars,
799 wxT( "PGM_BASE::SetLocalEnvVariable: Setting local environment variable %s to %s" ),
800 aName, aValue );
801
802 return wxSetEnv( aName, aValue );
803}
804
805
807{
808 // Overwrites externally defined environment variable until the next time the application
809 // is run.
810 for( const std::pair<wxString, ENV_VAR_ITEM> m_local_env_var : GetCommonSettings()->m_Env.vars )
811 {
812 wxLogTrace( traceEnvVars,
813 wxT( "PGM_BASE::SetLocalEnvVariables: Setting local environment variable %s "
814 "to %s" ),
815 m_local_env_var.first,
816 m_local_env_var.second.GetValue() );
817 wxSetEnv( m_local_env_var.first, m_local_env_var.second.GetValue() );
818 }
819}
820
821
826
827
829{
830 if( !wxTheApp )
831 return false;
832
833 return wxTheApp->IsGUI();
834}
835
836
837void PGM_BASE::HandleException( std::exception_ptr aPtr )
838{
839 try
840 {
841 if( aPtr )
842 std::rethrow_exception( aPtr );
843 }
844 catch( const IO_ERROR& ioe )
845 {
846 wxLogError( ioe.What() );
847 }
848 catch( const std::exception& e )
849 {
851
852 wxLogError( wxT( "Unhandled exception class: %s what: %s" ),
853 From_UTF8( typeid( e ).name() ), From_UTF8( e.what() ) );
854 }
855 catch( ... )
856 {
857 wxLogError( wxT( "Unhandled exception of unknown type" ) );
858 }
859}
860
861
862void PGM_BASE::HandleAssert( const wxString& aFile, int aLine, const wxString& aFunc,
863 const wxString& aCond, const wxString& aMsg )
864{
865 wxString assertStr;
866
867 // Log the assertion details to standard log
868 if( !aMsg.empty() )
869 {
870 assertStr = wxString::Format( "Assertion failed at %s:%d in %s: %s - %s", aFile, aLine,
871 aFunc, aCond, aMsg );
872 }
873 else
874 {
875 assertStr = wxString::Format( "Assertion failed at %s:%d in %s: %s", aFile, aLine, aFunc,
876 aCond );
877 }
878
879#ifndef NDEBUG
880 wxLogError( assertStr );
881#endif
882
883#ifdef KICAD_USE_SENTRY
884 APP_MONITOR::ASSERT_CACHE_KEY key = { aFile, aLine, aFunc, aCond };
885 APP_MONITOR::SENTRY::Instance()->LogAssert( key, assertStr );
886#endif
887}
888
889
890const wxString& PGM_BASE::GetExecutablePath() const
891{
893}
894
895
901
902
908
909
911
912
914{
915 wxASSERT( process ); // KIFACE_GETTER has already been called.
916 return *process;
917}
918
919
920// Similar to PGM_BASE& Pgm(), but return nullptr when a *.ki_face is run from a python script.
922{
923 return process;
924}
925
926
927void SetPgm( PGM_BASE* pgm )
928{
929 process = pgm;
930}
const char * name
constexpr std::size_t arrayDim(T const (&)[N]) noexcept
Returns # of elements in an array.
Definition arraydim.h:31
wxBitmap KiBitmap(BITMAPS aBitmap, int aHeightTag)
Construct a wxBitmap from an image identifier Returns the image from the active theme if the image ha...
Definition bitmap.cpp:104
wxString GetMajorMinorVersion()
Get only the major and minor version in a string major.minor.
static const ADVANCED_CFG & GetCfg()
Get the singleton instance's config, which is shared by all consumers.
void LogAssert(const ASSERT_CACHE_KEY &aKey, const wxString &aMsg)
void SetSentryOptIn(bool aOptIn)
void LogException(const wxString &aMsg)
void AddTag(const wxString &aKey, const wxString &aValue)
static SENTRY * Instance()
void InitializeEnvironment()
Creates the built-in environment variables and sets their default values.
DO_NOT_SHOW_AGAIN m_DoNotShowAgain
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
virtual const wxString What() const
A composite of Problem() and Where()
virtual bool SaveToFile(const wxString &aDirectory="", bool aForce=false)
Calls Store() and then writes the contents of the JSON document to a file.
static void Cleanup()
Call curl_global_cleanup for the application.
static void Init()
Call curl_global_init for the application.
void Load()
Load notifications stored from disk.
static wxString GetInstanceCheckerPath()
Gets the path used for wxSingleInstanceChecker lock files.
Definition paths.cpp:456
static void EnsureUserPathsExist()
Ensures/creates user default paths.
Definition paths.cpp:504
static bool EnsurePathExists(const wxString &aPath, bool aPathToFile=false)
Attempts to create a given path if it does not exist.
Definition paths.cpp:477
static wxString GetLocaleDataPath()
Gets the locales translation data path.
Definition paths.cpp:317
static const wxString & GetExecutablePath()
Definition paths.cpp:629
Container for data for KiCad programs.
Definition pgm_base.h:103
virtual COMMON_SETTINGS * GetCommonSettings() const
Definition pgm_base.cpp:576
virtual wxApp & App()
Return a bare naked wxApp which may come from wxPython, SINGLE_TOP, or kicad.exe.
Definition pgm_base.cpp:190
std::unique_ptr< NOTIFICATIONS_MANAGER > m_notifications_manager
Definition pgm_base.h:377
int m_argcUtf8
Definition pgm_base.h:414
virtual ENV_VAR_MAP & GetLocalEnvVariables() const
Definition pgm_base.cpp:822
bool m_Printing
wxWidgets on MSW tends to crash if you spool up more than one print job at a time.
Definition pgm_base.h:353
void setLanguageId(int aId)
Trap all changes in here, simplifies debugging.
Definition pgm_base.h:366
std::unique_ptr< SETTINGS_MANAGER > m_settings_manager
Definition pgm_base.h:375
void loadCommonSettings()
Load internal settings from COMMON_SETTINGS.
Definition pgm_base.cpp:540
virtual void SetPdfBrowserName(const wxString &aFileName)
Definition pgm_base.h:178
wxLocale * m_locale
Definition pgm_base.h:391
void Destroy()
Definition pgm_base.cpp:176
wxSplashScreen * m_splash
Definition pgm_base.h:416
bool InitPgm(bool aHeadless=false, bool aSkipPyInit=false, bool aIsUnitTest=false)
Initialize this program.
Definition pgm_base.cpp:344
char ** m_argvUtf8
argv parameters converted to utf8 form because wxWidgets has opinions.
Definition pgm_base.h:412
virtual void ReadPdfBrowserInfos()
Read the PDF browser choice from the common configuration.
Definition pgm_base.cpp:896
bool IsGUI()
Determine if the application is running with a GUI.
Definition pgm_base.cpp:828
bool m_use_system_pdf_browser
Definition pgm_base.h:394
bool m_Quitting
Definition pgm_base.h:357
virtual NOTIFICATIONS_MANAGER & GetNotificationsManager() const
Definition pgm_base.h:134
void BuildArgvUtf8()
Builds the UTF8 based argv variable.
Definition pgm_base.cpp:299
virtual const wxString & GetPdfBrowserName() const
Definition pgm_base.h:176
virtual void SetTextEditor(const wxString &aFileName)
Definition pgm_base.cpp:197
std::unique_ptr< wxSingleInstanceChecker > m_pgm_checker
Check if there is another copy of Kicad running at the same time.
Definition pgm_base.h:382
virtual bool SetLocalEnvVariable(const wxString &aName, const wxString &aValue)
Set the environment variable aName to aValue.
Definition pgm_base.cpp:775
wxString m_kicad_env
The KICAD system environment variable.
Definition pgm_base.h:389
virtual void ForceSystemPdfBrowser(bool aFlg)
Force the use of system PDF browser, even if a preferred PDF browser is set.
Definition pgm_base.h:193
virtual void SetLocalEnvVariables()
Update the local environment with the contents of the current ENV_VAR_MAP stored in the COMMON_SETTIN...
Definition pgm_base.cpp:806
virtual void SetLanguagePath()
Definition pgm_base.cpp:758
bool SetDefaultLanguage(wxString &aErrMsg)
Set the default language without reference to any preferences.
Definition pgm_base.cpp:683
void ShowSplash()
Definition pgm_base.cpp:314
wxString m_text_editor
Definition pgm_base.h:397
virtual ~PGM_BASE()
Definition pgm_base.cpp:159
std::unique_ptr< SCRIPTING > m_python_scripting
Definition pgm_base.h:379
virtual void SetLanguageIdentifier(int menu_id)
Set in .m_language_id member the wxWidgets language identifier ID from the KiCad menu id (internal me...
Definition pgm_base.cpp:724
int m_language_id
Definition pgm_base.h:392
std::unique_ptr< BACKGROUND_JOBS_MONITOR > m_background_jobs_monitor
Definition pgm_base.h:376
void HandleException(std::exception_ptr aPtr)
A exception handler to be used at the top level if exceptions bubble up that for.
Definition pgm_base.cpp:837
void HandleAssert(const wxString &aFile, int aLine, const wxString &aFunc, const wxString &aCond, const wxString &aMsg)
A common assert handler to be used between single_top and kicad.
Definition pgm_base.cpp:862
virtual wxString GetLanguageTag()
Definition pgm_base.cpp:740
virtual const wxString & GetTextEditor(bool aCanShowFileChooser=true)
Return the path to the preferred text editor application.
Definition pgm_base.cpp:204
virtual const wxString & GetExecutablePath() const
Definition pgm_base.cpp:890
virtual SETTINGS_MANAGER & GetSettingsManager() const
Definition pgm_base.h:125
void HideSplash()
Definition pgm_base.cpp:333
virtual void WritePdfBrowserInfos()
Save the PDF browser choice to the common configuration.
Definition pgm_base.cpp:903
void SaveCommonSettings()
Save the program (process) settings subset which are stored .kicad_common.
Definition pgm_base.cpp:567
bool m_PropertyGridInitialized
Definition pgm_base.h:359
KICAD_SINGLETON m_singleton
Definition pgm_base.h:399
virtual const wxString AskUserForPreferredEditor(const wxString &aDefaultEditor=wxEmptyString)
Show a dialog that instructs the user to select a new preferred editor.
Definition pgm_base.cpp:240
virtual bool SetLanguage(wxString &aErrMsg, bool first_time=false)
Set the dictionary file name for internationalization.
Definition pgm_base.cpp:582
COMMON_SETTINGS * GetCommonSettings() const
Retrieve the common settings shared by all applications.
bool LoadProject(const wxString &aFullPath, bool aSetActive=true)
Load a project or sets up a new project with a specified path.
bool WarnUserIfOperatingSystemUnsupported()
Checks if the operating system is explicitly unsupported and displays a disclaimer message box.
Definition common.cpp:703
The common library.
void DisplayInfoMessage(wxWindow *aParent, const wxString &aMessage, const wxString &aExtraInfo)
Display an informational message box with aMessage.
Definition confirm.cpp:222
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Display an error message with aMessage.
Definition confirm.cpp:194
This file is part of the common library.
#define _(s)
#define WIN_STRING_DIR_SEP
Definition gestfich.h:38
#define UNIX_STRING_DIR_SEP
Definition gestfich.h:37
const wxChar *const traceLocale
Flag to enable locale debug output.
const wxChar *const traceEnvVars
Flag to enable debug output of environment variable operations.
@ ID_LANGUAGE_SWEDISH
Definition id.h:101
@ ID_LANGUAGE_CATALAN
Definition id.h:90
@ ID_LANGUAGE_NORWEGIAN_BOKMAL
Definition id.h:108
@ ID_LANGUAGE_CHINESE_SIMPLIFIED
Definition id.h:91
@ ID_LANGUAGE_GERMAN
Definition id.h:75
@ ID_LANGUAGE_DUTCH
Definition id.h:93
@ ID_LANGUAGE_GREEK
Definition id.h:76
@ ID_LANGUAGE_HUNGARIAN
Definition id.h:86
@ ID_LANGUAGE_LITHUANIAN
Definition id.h:97
@ ID_LANGUAGE_TAMIL
Definition id.h:109
@ ID_LANGUAGE_PORTUGUESE_BRAZILIAN
Definition id.h:80
@ ID_LANGUAGE_DEFAULT
Definition id.h:68
@ ID_LANGUAGE_POLISH
Definition id.h:87
@ ID_LANGUAGE_THAI
Definition id.h:100
@ ID_LANGUAGE_JAPANESE
Definition id.h:94
@ ID_LANGUAGE_VIETNAMESE
Definition id.h:98
@ ID_LANGUAGE_ITALIAN
Definition id.h:83
@ ID_LANGUAGE_KOREAN
Definition id.h:89
@ ID_LANGUAGE_SERBIAN
Definition id.h:99
@ ID_LANGUAGE_RUSSIAN
Definition id.h:78
@ ID_LANGUAGE_CZECH
Definition id.h:88
@ ID_LANGUAGE_LATVIAN
Definition id.h:96
@ ID_LANGUAGE_SLOVENIAN
Definition id.h:84
@ ID_LANGUAGE_SPANISH
Definition id.h:73
@ ID_LANGUAGE_PORTUGUESE
Definition id.h:79
@ ID_LANGUAGE_ENGLISH
Definition id.h:69
@ ID_LANGUAGE_SPANISH_MEXICAN
Definition id.h:74
@ ID_LANGUAGE_SLOVAK
Definition id.h:85
@ ID_LANGUAGE_CHINESE_TRADITIONAL
Definition id.h:92
@ ID_LANGUAGE_FINNISH
Definition id.h:71
@ ID_LANGUAGE_BULGARIAN
Definition id.h:95
@ ID_LANGUAGE_ARABIC
Definition id.h:103
@ ID_LANGUAGE_UKRAINIAN
Definition id.h:102
@ ID_LANGUAGE_TURKISH
Definition id.h:81
@ ID_LANGUAGE_DANISH
Definition id.h:67
@ ID_LANGUAGE_FRENCH
Definition id.h:70
@ ID_LANGUAGE_HEBREW
Definition id.h:72
@ ID_LANGUAGE_ROMANIAN
Definition id.h:107
@ ID_LANGUAGE_INDONESIAN
Definition id.h:82
std::map< wxString, ENV_VAR_ITEM > ENV_VAR_MAP
This file contains miscellaneous commonly used macros and functions.
PBOOL GetPolicyBool(const wxString &aKey)
void SetPgm(PGM_BASE *pgm)
Definition pgm_base.cpp:927
static PGM_BASE * process
Definition pgm_base.cpp:910
PGM_BASE & Pgm()
The global program "get" accessor.
Definition pgm_base.cpp:913
PGM_BASE * PgmOrNull()
Return a reference that can be nullptr when running a shared lib from a script, not from a kicad app.
Definition pgm_base.cpp:921
LANGUAGE_DESCR LanguagesList[]
An array containing all the languages that KiCad supports.
Definition pgm_base.cpp:92
#define _(s)
Current list of languages supported by KiCad.
Definition pgm_base.cpp:91
see class PGM_BASE
KICOMMON_API LANGUAGE_DESCR LanguagesList[]
An array containing all the languages that KiCad supports.
Definition pgm_base.cpp:92
KICOMMON_API PGM_BASE & Pgm()
The global program "get" accessor.
Definition pgm_base.cpp:913
#define POLICY_KEY_DATACOLLECTION
Definition policy_keys.h:30
#define PROJECT_VAR_NAME
A variable name whose value holds the current project directory.
Definition project.h:40
APIIMPORT wxPGGlobalVarsClass * wxPGGlobalVars
wxString From_UTF8(const char *cstring)
This struct represents a key being used for the std::set that deduplicates asserts during this runnin...
Definition app_monitor.h:90
A small class to handle the list of existing translations.
Definition pgm_base.h:69
System directories search utilities.
wxString result
Test unit parsing edge cases and error handling.
wxLogTrace helper definitions.