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 (C) 1992-2021 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
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/propgrid/propgrid.h>
38#include <wx/snglinst.h>
39#include <wx/stdpaths.h>
40#include <wx/sysopt.h>
41#include <wx/filedlg.h>
42#include <wx/ffile.h>
43#include <wx/tooltip.h>
44
45#include <advanced_config.h>
47#include <bitmaps.h>
48#include <common.h>
49#include <confirm.h>
50#include <core/arraydim.h>
51#include <eda_base_frame.h>
52#include <eda_draw_frame.h>
53#include <gestfich.h>
54#include <id.h>
56#include <kiplatform/policy.h>
57#include <macros.h>
58#include <menus_helpers.h>
60#include <paths.h>
61#include <pgm_base.h>
62#include <policy_keys.h>
63#include <python_scripting.h>
66#include <string_utils.h>
67#include <systemdirsappend.h>
68#include <core/thread_pool.h>
69#include <trace_helpers.h>
70
71#include <widgets/wx_splash.h>
72
73#ifdef KICAD_USE_SENTRY
74#include <boost/uuid/uuid_io.hpp>
75#include <boost/uuid/uuid_generators.hpp>
76#include <sentry.h>
77#include <build_version.h>
78#endif
79
89#undef _
90#define _(s) s
92{
93 { wxLANGUAGE_DEFAULT, ID_LANGUAGE_DEFAULT, _( "Default" ), false },
94// { wxLANGUAGE_INDONESIAN, ID_LANGUAGE_INDONESIAN, wxT( "Bahasa Indonesia" ), true },
95 { wxLANGUAGE_CZECH, ID_LANGUAGE_CZECH, wxT( "Čeština" ), true },
96 { wxLANGUAGE_DANISH, ID_LANGUAGE_DANISH, wxT( "Dansk" ), true },
97 { wxLANGUAGE_GERMAN, ID_LANGUAGE_GERMAN, wxT( "Deutsch" ), true },
98 { wxLANGUAGE_GREEK, ID_LANGUAGE_GREEK, wxT( "Ελληνικά" ), true },
99 { wxLANGUAGE_ENGLISH, ID_LANGUAGE_ENGLISH, wxT( "English" ), true },
100 { wxLANGUAGE_SPANISH, ID_LANGUAGE_SPANISH, wxT( "Español" ), true },
101 { wxLANGUAGE_SPANISH_MEXICAN, ID_LANGUAGE_SPANISH_MEXICAN, wxT( "Español (Latinoamericano)" ), true },
102 { wxLANGUAGE_FRENCH, ID_LANGUAGE_FRENCH, wxT( "Français" ), true },
103 { wxLANGUAGE_KOREAN, ID_LANGUAGE_KOREAN, wxT( "한국어"), true },
104 { wxLANGUAGE_ITALIAN, ID_LANGUAGE_ITALIAN, wxT( "Italiano" ), true },
105 { wxLANGUAGE_LITHUANIAN, ID_LANGUAGE_LITHUANIAN, wxT( "Lietuvių" ), true },
106// { wxLANGUAGE_HUNGARIAN, ID_LANGUAGE_HUNGARIAN, wxT( "Magyar" ), true },
107 { wxLANGUAGE_DUTCH, ID_LANGUAGE_DUTCH, wxT( "Nederlandse" ), true },
108 { wxLANGUAGE_JAPANESE, ID_LANGUAGE_JAPANESE, wxT( "日本語" ), true },
109 { wxLANGUAGE_THAI, ID_LANGUAGE_THAI, wxT( "ภาษาไทย" ), true },
110 { wxLANGUAGE_POLISH, ID_LANGUAGE_POLISH, wxT( "Polski" ), true },
111 { wxLANGUAGE_PORTUGUESE, ID_LANGUAGE_PORTUGUESE, wxT( "Português" ),true },
112 { wxLANGUAGE_PORTUGUESE_BRAZILIAN, ID_LANGUAGE_PORTUGUESE_BRAZILIAN, wxT( "Português (Brasil)" ), true },
113 { wxLANGUAGE_RUSSIAN, ID_LANGUAGE_RUSSIAN, wxT( "Русский" ), true },
114// { wxLANGUAGE_SERBIAN, ID_LANGUAGE_SERBIAN, wxT( "Српски" ), true },
115 { wxLANGUAGE_FINNISH, ID_LANGUAGE_FINNISH, wxT( "Suomi" ), true },
116 { wxLANGUAGE_SWEDISH, ID_LANGUAGE_SWEDISH, wxT( "Svenska" ), true },
117// { wxLANGUAGE_VIETNAMESE, ID_LANGUAGE_VIETNAMESE, wxT( "Tiếng Việt" ), true },
118// { wxLANGUAGE_TURKISH, ID_LANGUAGE_TURKISH, wxT( "Türkçe" ), true },
119 { wxLANGUAGE_UKRAINIAN, ID_LANGUAGE_UKRANIAN, wxT( "Українська" ), true },
120 { wxLANGUAGE_CHINESE_SIMPLIFIED, ID_LANGUAGE_CHINESE_SIMPLIFIED,
121 wxT( "简体中文" ), true },
122 { wxLANGUAGE_CHINESE_TRADITIONAL, ID_LANGUAGE_CHINESE_TRADITIONAL,
123 wxT( "繁體中文" ), false },
124 { 0, 0, "", false } // Sentinel
125};
126#undef _
127#define _(s) wxGetTranslation((s))
128
129
131{
132 m_locale = nullptr;
133 m_Printing = false;
134 m_Quitting = false;
135 m_argcUtf8 = 0;
136 m_argvUtf8 = nullptr;
137 m_splash = nullptr;
138
139 setLanguageId( wxLANGUAGE_DEFAULT );
140
141 ForceSystemPdfBrowser( false );
142}
143
144
146{
147 HideSplash();
148 Destroy();
149
150 for( int n = 0; n < m_argcUtf8; n++ )
151 {
152 free( m_argvUtf8[n] );
153 }
154
155 delete[] m_argvUtf8;
156}
157
158
160{
162
163#ifdef KICAD_USE_SENTRY
164 sentry_close();
165#endif
166
167 // unlike a normal destructor, this is designed to be called more than once safely:
168 delete m_locale;
169 m_locale = nullptr;
170
171 m_pgm_checker.reset();
172}
173
174
176{
177 wxASSERT( wxTheApp );
178 return *wxTheApp;
179}
180
181
182void PGM_BASE::SetTextEditor( const wxString& aFileName )
183{
184 m_text_editor = aFileName;
186}
187
188
189const wxString& PGM_BASE::GetTextEditor( bool aCanShowFileChooser )
190{
191 wxString editorname = m_text_editor;
192
193 if( !editorname )
194 {
195 if( !wxGetEnv( wxT( "EDITOR" ), &editorname ) )
196 {
197 // If there is no EDITOR variable set, try the desktop default
198#ifdef __WXMAC__
199 editorname = wxT( "/usr/bin/open -e" );
200#elif __WXX11__
201 editorname = wxT( "/usr/bin/xdg-open" );
202#endif
203 }
204 }
205
206 // If we still don't have an editor name show a dialog asking the user to select one
207 if( !editorname && aCanShowFileChooser )
208 {
209 DisplayInfoMessage( nullptr, _( "No default editor found, you must choose one." ) );
210
211 editorname = AskUserForPreferredEditor();
212 }
213
214 // If we finally have a new editor name request it to be copied to m_text_editor and
215 // saved to the preferences file.
216 if( !editorname.IsEmpty() )
217 SetTextEditor( editorname );
218
219 // m_text_editor already has the same value that editorname, or empty if no editor was
220 // found/chosen.
221 return m_text_editor;
222}
223
224
225const wxString PGM_BASE::AskUserForPreferredEditor( const wxString& aDefaultEditor )
226{
227 // Create a mask representing the executable files in the current platform
228#ifdef __WINDOWS__
229 wxString mask( _( "Executable file" ) + wxT( " (*.exe)|*.exe" ) );
230#else
231 wxString mask( _( "Executable file" ) + wxT( " (*)|*" ) );
232#endif
233
234 // Extract the path, name and extension from the default editor (even if the editor's
235 // name was empty, this method will succeed and return empty strings).
236 wxString path, name, ext;
237 wxFileName::SplitPath( aDefaultEditor, &path, &name, &ext );
238
239 // Show the modal editor and return the file chosen (may be empty if the user cancels
240 // the dialog).
241 return wxFileSelector( _( "Select Preferred Editor" ), path, name, wxT( "." ) + ext,
242 mask, wxFD_OPEN | wxFD_FILE_MUST_EXIST, nullptr );
243}
244
245
246#ifdef KICAD_USE_SENTRY
247bool PGM_BASE::IsSentryOptedIn()
248{
249 KIPLATFORM::POLICY::PBOOL policyState =
252 {
253 return policyState == KIPLATFORM::POLICY::PBOOL::ENABLED;
254 }
255
256 return m_sentry_optin_fn.Exists();
257}
258
259
260void PGM_BASE::SetSentryOptIn( bool aOptIn )
261{
262 if( aOptIn )
263 {
264 if( !m_sentry_uid_fn.Exists() )
265 {
266 sentryCreateUid();
267 }
268
269 if( !m_sentry_optin_fn.Exists() )
270 {
271 wxFFile sentryInitFile( m_sentry_optin_fn.GetFullPath(), "w" );
272 sentryInitFile.Write( "" );
273 sentryInitFile.Close();
274 }
275 }
276 else
277 {
278 if( m_sentry_optin_fn.Exists() )
279 {
280 wxRemoveFile( m_sentry_optin_fn.GetFullPath() );
281 sentry_close();
282 }
283 }
284}
285
286
287wxString PGM_BASE::sentryCreateUid()
288{
289 boost::uuids::uuid uuid = boost::uuids::random_generator()();
290 wxString userGuid = boost::uuids::to_string( uuid );
291
292 wxFFile sentryInitFile( m_sentry_uid_fn.GetFullPath(), "w" );
293 sentryInitFile.Write( userGuid );
294 sentryInitFile.Close();
295
296 return userGuid;
297}
298
299
300void PGM_BASE::ResetSentryId()
301{
302 m_sentryUid = sentryCreateUid();
303}
304
305
306const wxString& PGM_BASE::GetSentryId()
307{
308 return m_sentryUid;
309}
310
311
312void PGM_BASE::sentryInit()
313{
314 m_sentry_optin_fn = wxFileName( PATHS::GetUserCachePath(), "sentry-opt-in" );
315 m_sentry_uid_fn = wxFileName( PATHS::GetUserCachePath(), "sentry-uid" );
316
317 if( IsSentryOptedIn() )
318 {
319 wxFFile sentryInitFile( m_sentry_uid_fn.GetFullPath() );
320 sentryInitFile.ReadAll( &m_sentryUid );
321 sentryInitFile.Close();
322
323 if( m_sentryUid.IsEmpty() || m_sentryUid.length() != 36 )
324 {
325 m_sentryUid = sentryCreateUid();
326 }
327
328 sentry_options_t* options = sentry_options_new();
329
330 #ifndef KICAD_SENTRY_DSN
331 # error "Project configuration error, missing KICAD_SENTRY_DSN"
332 #endif
333
334 sentry_options_set_dsn( options, KICAD_SENTRY_DSN );
335
336 wxFileName tmp;
337 tmp.AssignDir( PATHS::GetUserCachePath() );
338 tmp.AppendDir( "sentry" );
339
340#ifdef __WINDOWS__
341 sentry_options_set_database_pathw( options, tmp.GetPathWithSep().wc_str() );
342#else
343 sentry_options_set_database_path( options, tmp.GetPathWithSep().c_str() );
344#endif
345 sentry_options_set_symbolize_stacktraces( options, true );
346 sentry_options_set_auto_session_tracking( options, false );
347
348 sentry_options_set_release( options, GetCommitHash().ToStdString().c_str() );
349
350 // This just gives us more filtering within sentry, issues still get grouped across environments
351 sentry_options_set_environment( options, GetMajorMinorVersion().c_str() );
352
353 sentry_init( options );
354
355 sentry_value_t user = sentry_value_new_object();
356 sentry_value_set_by_key( user, "id", sentry_value_new_string( m_sentryUid.c_str() ) );
357 sentry_set_user( user );
358
359 sentry_set_tag( "kicad.version", GetBuildVersion().ToStdString().c_str() );
360 }
361}
362
363
364void PGM_BASE::sentryPrompt()
365{
366 if( !IsGUI() )
367 return;
368
369 KIPLATFORM::POLICY::PBOOL policyState =
371
373 && !m_settings_manager->GetCommonSettings()->m_DoNotShowAgain.data_collection_prompt )
374 {
375 wxMessageDialog optIn = wxMessageDialog(
376 nullptr,
377 _( "KiCad can anonymously report crashes and special event "
378 "data to developers in order to aid identifying critical bugs "
379 "across the user base more effectively and help profile "
380 "functionality to guide improvements. \n"
381 "If you choose to voluntarily participate, KiCad will automatically "
382 "handle sending said reports when crashes or events occur. \n"
383 "Your design files such as schematic or PCB are not shared in this process." ),
384 _( "Data collection opt in request" ), wxYES_NO | wxCENTRE );
385
386 int result = optIn.ShowModal();
387
388 if( result == wxID_YES )
389 {
390 SetSentryOptIn( true );
391 sentryInit();
392 }
393 else
394 {
395 SetSentryOptIn( false );
396 }
397
398 m_settings_manager->GetCommonSettings()->m_DoNotShowAgain.data_collection_prompt = true;
399 }
400}
401#endif
402
403
405{
406 const wxArrayString& argArray = App().argv.GetArguments();
407 m_argcUtf8 = argArray.size();
408
409 m_argvUtf8 = new char*[m_argcUtf8 + 1];
410 for( int n = 0; n < m_argcUtf8; n++ )
411 {
412 m_argvUtf8[n] = wxStrdup( argArray[n].ToUTF8() );
413 }
414
415 m_argvUtf8[m_argcUtf8] = NULL; // null terminator at end of argv
416}
417
418
420{
421 if( m_splash )
422 return;
423
424 m_splash = new WX_SPLASH( KiBitmap( BITMAPS::splash ), wxSPLASH_CENTRE_ON_SCREEN, 0,
425 NULL, -1, wxDefaultPosition, wxDefaultSize, wxBORDER_NONE | wxSTAY_ON_TOP );
426 wxYield();
427}
428
429
431{
432 if( !m_splash )
433 return;
434
435 m_splash->Close( true );
436 m_splash->Destroy();
437 m_splash = nullptr;
438}
439
440
441bool PGM_BASE::InitPgm( bool aHeadless, bool aSkipPyInit, bool aIsUnitTest )
442{
443#if defined( __WXMAC__ )
444 // Set the application locale to the system default
445 wxLogNull noLog;
446 wxLocale loc;
447 loc.Init();
448#endif
449
450 // Just make sure we init precreate any folders early for later code
451 // In particular, the user cache path is the most likely to be hit by startup code
453
455
456#ifdef KICAD_USE_SENTRY
457 sentryInit();
458#endif
459 wxString pgm_name;
460
462 if( App().argc == 0 )
463 pgm_name = wxT( "kicad" );
464 else
465 pgm_name = wxFileName( App().argv[0] ).GetName().Lower();
466
467#ifdef KICAD_USE_SENTRY
468 sentry_set_tag( "kicad.app", pgm_name.c_str() );
469#endif
470
471 wxInitAllImageHandlers();
472
473 // Without this the wxPropertyGridManager segfaults on Windows.
474 if( !wxPGGlobalVars )
475 wxPGInitResourceModule();
476
477#ifndef __WINDOWS__
478 if( wxString( wxGetenv( "HOME" ) ).IsEmpty() )
479 {
480 DisplayErrorMessage( nullptr, _( "Environment variable HOME is empty. "
481 "Unable to continue." ) );
482 return false;
483 }
484#endif
485 m_pgm_checker = std::make_unique<wxSingleInstanceChecker>();
486 m_pgm_checker->Create( pgm_name, wxStandardPaths::Get().GetTempDir() );
487
488 // Init KiCad environment
489 // the environment variable KICAD (if exists) gives the kicad path:
490 // something like set KICAD=d:\kicad
491 bool isDefined = wxGetEnv( wxT( "KICAD" ), &m_kicad_env );
492
493 if( isDefined ) // ensure m_kicad_env ends by "/"
494 {
496
497 if( !m_kicad_env.IsEmpty() && m_kicad_env.Last() != '/' )
499 }
500
501 // Init parameters for configuration
502 App().SetVendorName( wxT( "KiCad" ) );
503 App().SetAppName( pgm_name );
504
505 // Install some image handlers, mainly for help
506 if( wxImage::FindHandler( wxBITMAP_TYPE_PNG ) == nullptr )
507 wxImage::AddHandler( new wxPNGHandler );
508
509 if( wxImage::FindHandler( wxBITMAP_TYPE_GIF ) == nullptr )
510 wxImage::AddHandler( new wxGIFHandler );
511
512 if( wxImage::FindHandler( wxBITMAP_TYPE_JPEG ) == nullptr )
513 wxImage::AddHandler( new wxJPEGHandler );
514
515 wxFileSystem::AddHandler( new wxZipFSHandler );
516
517 // Analyze the command line & initialize the binary path
518 wxString tmp;
520 SetDefaultLanguage( tmp );
521
522#ifdef _MSC_VER
523 // We need to set this because the internal fontconfig logic
524 // seems to search relative to the dll rather the other logic it
525 // has to look for the /etc folder above the dll
526 wxSetEnv( "FONTCONFIG_PATH", PATHS::GetWindowsFontConfigDir() );
527#endif
528
529 m_settings_manager = std::make_unique<SETTINGS_MANAGER>( aHeadless );
530 m_background_jobs_monitor = std::make_unique<BACKGROUND_JOBS_MONITOR>();
531 m_notifications_manager = std::make_unique<NOTIFICATIONS_MANAGER>();
532
533 // Our unit test mocks break if we continue
534 // A bug caused InitPgm to terminate early in unit tests and the mocks are...simplistic
535 // TODO fix the unit tests so this can be removed
536 if( aIsUnitTest )
537 return false;
538
539 // Something got in the way of settings load: can't continue
540 if( !m_settings_manager->IsOK() )
541 return false;
542
543 // Set up built-in environment variables (and override them from the system environment if set)
545
546 // Load color settings after env is initialized
547 m_settings_manager->ReloadColorSettings();
548
549 // Load common settings from disk after setting up env vars
551
552 // Init user language *before* calling loadSettings, because
553 // env vars could be incorrectly initialized on Linux
554 // (if the value contains some non ASCII7 chars, the env var is not initialized)
555 SetLanguage( tmp, true );
556
557 // Now that translations are available, inform the user if the OS is unsupported
559
561
562#ifdef KICAD_USE_SENTRY
563 sentryPrompt();
564#endif
565
566 ReadPdfBrowserInfos(); // needs GetCommonSettings()
567
569
570 // Create the python scripting stuff
571 // Skip it fot applications that do not use it
572 if( !aSkipPyInit )
573 m_python_scripting = std::make_unique<SCRIPTING>();
574
575 // TODO(JE): Remove this if apps are refactored to not assume Prj() always works
576 // Need to create a project early for now (it can have an empty path for the moment)
578
579 // This sets the maximum tooltip display duration to 10s (up from 5) but only affects
580 // Windows as other platforms display tooltips while the mouse is not moving
581 if( !aHeadless )
582 {
583 wxToolTip::Enable( true );
584 wxToolTip::SetAutoPop( 10000 );
585 }
586
587 if( ADVANCED_CFG::GetCfg().m_UpdateUIEventInterval != 0 )
588 wxUpdateUIEvent::SetUpdateInterval( ADVANCED_CFG::GetCfg().m_UpdateUIEventInterval );
589
590 // Now the application can safely start, show the splash screen
591 if( !aHeadless )
592 ShowSplash();
593
594 return true;
595}
596
597
599{
601
602 for( const std::pair<wxString, ENV_VAR_ITEM> it : GetCommonSettings()->m_Env.vars )
603 {
604 wxLogTrace( traceEnvVars, wxT( "PGM_BASE::loadSettings: Found entry %s = %s" ),
605 it.first, it.second.GetValue() );
606
607 // Do not store the env var PROJECT_VAR_NAME ("KIPRJMOD") definition if for some reason
608 // it is found in config. (It is reserved and defined as project path)
609 if( it.first == PROJECT_VAR_NAME )
610 continue;
611
612 // Don't set bogus empty entries in the environment
613 if( it.first.IsEmpty() )
614 continue;
615
616 // Do not overwrite vars set by the system environment with values from the settings file
617 if( it.second.GetDefinedExternally() )
618 continue;
619
620 SetLocalEnvVariable( it.first, it.second.GetValue() );
621 }
622}
623
624
626{
627 // GetCommonSettings() is not initialized until fairly late in the
628 // process startup: InitPgm(), so test before using:
629 if( GetCommonSettings() )
630 GetCommonSettings()->m_System.working_dir = wxGetCwd();
631}
632
633
635{
636 return m_settings_manager ? m_settings_manager->GetCommonSettings() : nullptr;
637}
638
639
640bool PGM_BASE::SetLanguage( wxString& aErrMsg, bool first_time )
641{
642 // Suppress wxWidgets error popups if locale is not found
643 wxLogNull doNotLog;
644
645 if( first_time )
646 {
647 setLanguageId( wxLANGUAGE_DEFAULT );
648 // First time SetLanguage is called, the user selected language id is set
649 // from common user config settings
650 wxString languageSel = GetCommonSettings()->m_System.language;
651
652 // Search for the current selection
653 for( unsigned ii = 0; LanguagesList[ii].m_KI_Lang_Identifier != 0; ii++ )
654 {
655 if( LanguagesList[ii].m_Lang_Label == languageSel )
656 {
657 setLanguageId( LanguagesList[ii].m_WX_Lang_Identifier );
658 break;
659 }
660 }
661 }
662
663 // dictionary file name without extend (full name is kicad.mo)
664 wxString dictionaryName( wxT( "kicad" ) );
665
666 delete m_locale;
667 m_locale = new wxLocale;
668
669 // don't use wxLOCALE_LOAD_DEFAULT flag so that Init() doesn't return
670 // false just because it failed to load wxstd catalog
671 if( !m_locale->Init( m_language_id ) )
672 {
673 wxLogTrace( traceLocale, wxT( "This language is not supported by the system." ) );
674
675 setLanguageId( wxLANGUAGE_DEFAULT );
676 delete m_locale;
677
678 m_locale = new wxLocale;
679 m_locale->Init( wxLANGUAGE_DEFAULT );
680
681 aErrMsg = _( "This language is not supported by the operating system." );
682 return false;
683 }
684 else if( !first_time )
685 {
686 wxLogTrace( traceLocale, wxT( "Search for dictionary %s.mo in %s" ) ,
687 dictionaryName, m_locale->GetName() );
688 }
689
690 if( !first_time )
691 {
692 // If we are here, the user has selected another language.
693 // Therefore the new preferred language name is stored in common config.
694 // Do NOT store the wxWidgets language Id, it can change between wxWidgets
695 // versions, for a given language
696 wxString languageSel;
697
698 // Search for the current selection language name
699 for( unsigned ii = 0; LanguagesList[ii].m_KI_Lang_Identifier != 0; ii++ )
700 {
701 if( LanguagesList[ii].m_WX_Lang_Identifier == m_language_id )
702 {
703 languageSel = LanguagesList[ii].m_Lang_Label;
704 break;
705 }
706 }
707
709 cfg->m_System.language = languageSel;
710 cfg->SaveToFile( GetSettingsManager().GetPathForSettingsFile( cfg ) );
711 }
712
713 // Try adding the dictionary if it is not currently loaded
714 if( !m_locale->IsLoaded( dictionaryName ) )
715 m_locale->AddCatalog( dictionaryName );
716
717 // Verify the Kicad dictionary was loaded properly
718 // However, for the English language, the dictionary is not mandatory, as
719 // all messages are already in English, just restricted to ASCII7 chars,
720 // the verification is skipped.
721 if( !m_locale->IsLoaded( dictionaryName ) && m_language_id != wxLANGUAGE_ENGLISH )
722 {
723 wxLogTrace( traceLocale, wxT( "Unable to load dictionary %s.mo in %s" ),
724 dictionaryName, m_locale->GetName() );
725
726 setLanguageId( wxLANGUAGE_DEFAULT );
727 delete m_locale;
728
729 m_locale = new wxLocale;
730 m_locale->Init( wxLANGUAGE_DEFAULT );
731
732 aErrMsg = _( "The KiCad language file for this language is not installed." );
733 return false;
734 }
735
736 return true;
737}
738
739
740bool PGM_BASE::SetDefaultLanguage( wxString& aErrMsg )
741{
742 // Suppress error popups from wxLocale
743 wxLogNull doNotLog;
744
745 setLanguageId( wxLANGUAGE_DEFAULT );
746
747 // dictionary file name without extend (full name is kicad.mo)
748 wxString dictionaryName( wxT( "kicad" ) );
749
750 delete m_locale;
751 m_locale = new wxLocale;
752 m_locale->Init();
753
754 // Try adding the dictionary if it is not currently loaded
755 if( !m_locale->IsLoaded( dictionaryName ) )
756 m_locale->AddCatalog( dictionaryName );
757
758 // Verify the Kicad dictionary was loaded properly
759 // However, for the English language, the dictionary is not mandatory, as
760 // all messages are already in English, just restricted to ASCII7 chars,
761 // the verification is skipped.
762 if( !m_locale->IsLoaded( dictionaryName ) && m_language_id != wxLANGUAGE_ENGLISH )
763 {
764 wxLogTrace( traceLocale, wxT( "Unable to load dictionary %s.mo in %s" ),
765 dictionaryName, m_locale->GetName() );
766
767 setLanguageId( wxLANGUAGE_DEFAULT );
768 delete m_locale;
769
770 m_locale = new wxLocale;
771 m_locale->Init();
772
773 aErrMsg = _( "The KiCad language file for this language is not installed." );
774 return false;
775 }
776
777 return true;
778}
779
780
782{
783 wxLogTrace( traceLocale, wxT( "Select language ID %d from %d possible languages." ),
784 menu_id, (int)arrayDim( LanguagesList )-1 );
785
786 for( unsigned ii = 0; LanguagesList[ii].m_KI_Lang_Identifier != 0; ii++ )
787 {
788 if( menu_id == LanguagesList[ii].m_KI_Lang_Identifier )
789 {
790 setLanguageId( LanguagesList[ii].m_WX_Lang_Identifier );
791 break;
792 }
793 }
794}
795
796
798{
799 const wxLanguageInfo* langInfo = wxLocale::GetLanguageInfo( m_language_id );
800
801 if( !langInfo )
802 return "";
803 else
804 {
805 wxString str = langInfo->GetCanonicalWithRegion();
806 str.Replace( "_", "-" );
807
808 return str;
809 }
810}
811
812
814{
815 wxLocale::AddCatalogLookupPathPrefix( PATHS::GetLocaleDataPath() );
816
817 if( wxGetEnv( wxT( "KICAD_RUN_FROM_BUILD_DIR" ), nullptr ) )
818 {
819 wxFileName fn( Pgm().GetExecutablePath() );
820 fn.RemoveLastDir();
821 fn.AppendDir( wxT( "translation" ) );
822 wxLocale::AddCatalogLookupPathPrefix( fn.GetPath() );
823 }
824}
825
826
827bool PGM_BASE::SetLocalEnvVariable( const wxString& aName, const wxString& aValue )
828{
829 wxString env;
830
831 if( aName.IsEmpty() )
832 {
833 wxLogTrace( traceEnvVars,
834 wxT( "PGM_BASE::SetLocalEnvVariable: Attempt to set empty variable to value %s" ),
835 aValue );
836 return false;
837 }
838
839 // Check to see if the environment variable is already set.
840 if( wxGetEnv( aName, &env ) )
841 {
842 wxLogTrace( traceEnvVars,
843 wxT( "PGM_BASE::SetLocalEnvVariable: Environment variable %s already set to %s" ),
844 aName, env );
845 return env == aValue;
846 }
847
848 wxLogTrace( traceEnvVars,
849 wxT( "PGM_BASE::SetLocalEnvVariable: Setting local environment variable %s to %s" ),
850 aName, aValue );
851
852 return wxSetEnv( aName, aValue );
853}
854
855
857{
858 // Overwrites externally defined environment variable until the next time the application
859 // is run.
860 for( const std::pair<wxString, ENV_VAR_ITEM> m_local_env_var : GetCommonSettings()->m_Env.vars )
861 {
862 wxLogTrace( traceEnvVars,
863 wxT( "PGM_BASE::SetLocalEnvVariables: Setting local environment variable %s to %s" ),
864 m_local_env_var.first,
865 m_local_env_var.second.GetValue() );
866 wxSetEnv( m_local_env_var.first, m_local_env_var.second.GetValue() );
867 }
868}
869
870
872{
873 return GetCommonSettings()->m_Env.vars;
874}
875
876
878{
879 if( !wxTheApp )
880 return false;
881
882 return wxTheApp->IsGUI();
883}
884
885
886void PGM_BASE::HandleException( std::exception_ptr aPtr )
887{
888 try
889 {
890 if( aPtr )
891 std::rethrow_exception( aPtr );
892 }
893 catch( const IO_ERROR& ioe )
894 {
895 wxLogError( ioe.What() );
896 }
897 catch( const std::exception& e )
898 {
899#ifdef KICAD_USE_SENTRY
900 if( IsSentryOptedIn() )
901 {
902 sentry_value_t exc = sentry_value_new_exception( "exception", e.what() );
903 sentry_value_set_stacktrace( exc, NULL, 0 );
904
905 sentry_value_t sentryEvent = sentry_value_new_event();
906 sentry_event_add_exception( sentryEvent, exc );
907 sentry_capture_event( sentryEvent );
908 }
909#endif
910
911 wxLogError( wxT( "Unhandled exception class: %s what: %s" ),
912 From_UTF8( typeid( e ).name() ), From_UTF8( e.what() ) );
913 }
914 catch( ... )
915 {
916 wxLogError( wxT( "Unhandled exception of unknown type" ) );
917 }
918}
919
920
921void PGM_BASE::HandleAssert( const wxString& aFile, int aLine, const wxString& aFunc,
922 const wxString& aCond, const wxString& aMsg )
923{
924 wxString assertStr;
925 // Log the assertion details to standard log
926 if( !aMsg.empty() )
927 {
928 assertStr = wxString::Format( "Assertion failed at %s:%d in %s: %s - %s", aFile, aLine,
929 aFunc, aCond, aMsg );
930 }
931 else
932 {
933 assertStr = wxString::Format( "Assertion failed at %s:%d in %s: %s", aFile, aLine, aFunc,
934 aCond );
935 }
936
937#ifndef NDEBUG
938 wxLogError( assertStr );
939#endif
940
941#ifdef KICAD_USE_SENTRY
942 if( IsSentryOptedIn() )
943 {
944 sentry_value_t exc = sentry_value_new_exception( "assert", assertStr );
945 sentry_value_set_stacktrace( exc, NULL, 0 );
946
947 sentry_value_t sentryEvent = sentry_value_new_event();
948 sentry_event_add_exception( sentryEvent, exc );
949 sentry_capture_event( sentryEvent );
950 }
951#endif
952}
953
954
955const wxString& PGM_BASE::GetExecutablePath() const
956{
958}
const char * name
Definition: DXF_plotter.cpp:57
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:106
wxString GetMajorMinorVersion()
Get only the major and minor version in a string major.minor.
wxString GetCommitHash()
Get the commit hash as a string.
wxString GetBuildVersion()
Get the full KiCad version string.
static const ADVANCED_CFG & GetCfg()
Get the singleton instance's config, which is shared by all consumers.
void InitializeEnvironment()
Creates the built-in environment variables and sets their default values.
ENVIRONMENT m_Env
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
Definition: ki_exception.h:76
virtual const wxString What() const
A composite of Problem() and Where()
Definition: exceptions.cpp:30
virtual bool SaveToFile(const wxString &aDirectory="", bool aForce=false)
static void Cleanup()
Call curl_global_cleanup for the application.
Definition: kicad_curl.cpp:42
static void Init()
Call curl_global_init for the application.
Definition: kicad_curl.cpp:33
void Load()
Loads notifications stored from disk.
static void EnsureUserPathsExist()
Ensures/creates user default paths.
Definition: paths.cpp:396
static wxString GetUserCachePath()
Gets the stock (install) 3d viewer plugins path.
Definition: paths.cpp:339
static wxString GetLocaleDataPath()
Gets the locales translation data path.
Definition: paths.cpp:252
static const wxString & GetExecutablePath()
Definition: paths.cpp:522
virtual COMMON_SETTINGS * GetCommonSettings() const
Definition: pgm_base.cpp:634
virtual wxApp & App()
Returns a bare naked wxApp which may come from wxPython, SINGLE_TOP, or kicad.exe.
Definition: pgm_base.cpp:175
std::unique_ptr< NOTIFICATIONS_MANAGER > m_notifications_manager
Definition: pgm_base.h:399
int m_argcUtf8
argv parameters converted to utf8 form, because wxwidgets has opinions and will return argv as either...
Definition: pgm_base.h:426
virtual ENV_VAR_MAP & GetLocalEnvVariables() const
Definition: pgm_base.cpp:871
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:377
void setLanguageId(int aId)
Trap all changes in here, simplifies debugging.
Definition: pgm_base.h:388
std::unique_ptr< SETTINGS_MANAGER > m_settings_manager
Definition: pgm_base.h:397
void loadCommonSettings()
Loads internal settings from COMMON_SETTINGS.
Definition: pgm_base.cpp:598
wxLocale * m_locale
The KICAD system environment variable.
Definition: pgm_base.h:409
void Destroy()
Definition: pgm_base.cpp:159
wxSplashScreen * m_splash
Definition: pgm_base.h:428
bool InitPgm(bool aHeadless=false, bool aSkipPyInit=false, bool aIsUnitTest=false)
Initialize this program.
Definition: pgm_base.cpp:441
char ** m_argvUtf8
Definition: pgm_base.h:423
virtual void ReadPdfBrowserInfos()
Read the PDF browser choice from the common configuration.
Definition: eda_doc.cpp:37
bool IsGUI()
Determine if the application is running with a GUI.
Definition: pgm_base.cpp:877
bool m_Quitting
Definition: pgm_base.h:381
virtual NOTIFICATIONS_MANAGER & GetNotificationsManager() const
Definition: pgm_base.h:145
void BuildArgvUtf8()
Builds the UTF8 based argv variable.
Definition: pgm_base.cpp:404
virtual void SetTextEditor(const wxString &aFileName)
Definition: pgm_base.cpp:182
std::unique_ptr< wxSingleInstanceChecker > m_pgm_checker
Checks if there is another copy of Kicad running at the same time.
Definition: pgm_base.h:404
virtual bool SetLocalEnvVariable(const wxString &aName, const wxString &aValue)
Sets the environment variable aName to aValue.
Definition: pgm_base.cpp:827
wxString m_kicad_env
Definition: pgm_base.h:407
virtual void ForceSystemPdfBrowser(bool aFlg)
Force the use of system PDF browser, even if a preferred PDF browser is set.
Definition: pgm_base.h:195
virtual void SetLocalEnvVariables()
Updates the local environment with the contents of the current ENV_VAR_MAP stored in the COMMON_SETTI...
Definition: pgm_base.cpp:856
virtual void SetLanguagePath()
Definition: pgm_base.cpp:813
bool SetDefaultLanguage(wxString &aErrMsg)
Set the default language without reference to any preferences.
Definition: pgm_base.cpp:740
void ShowSplash()
Definition: pgm_base.cpp:419
wxString m_text_editor
Filename of the app selected for browsing PDFs.
Definition: pgm_base.h:415
virtual ~PGM_BASE()
Definition: pgm_base.cpp:145
std::unique_ptr< SCRIPTING > m_python_scripting
Definition: pgm_base.h:401
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:781
int m_language_id
Definition: pgm_base.h:410
std::unique_ptr< BACKGROUND_JOBS_MONITOR > m_background_jobs_monitor
Definition: pgm_base.h:398
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:886
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:921
virtual wxString GetLanguageTag()
Definition: pgm_base.cpp:797
virtual const wxString & GetTextEditor(bool aCanShowFileChooser=true)
Return the path to the preferred text editor application.
Definition: pgm_base.cpp:189
virtual const wxString & GetExecutablePath() const
Definition: pgm_base.cpp:955
virtual SETTINGS_MANAGER & GetSettingsManager() const
Definition: pgm_base.h:139
void HideSplash()
Definition: pgm_base.cpp:430
void SaveCommonSettings()
Save the program (process) settings subset which are stored .kicad_common.
Definition: pgm_base.cpp:625
virtual const wxString AskUserForPreferredEditor(const wxString &aDefaultEditor=wxEmptyString)
Shows a dialog that instructs the user to select a new preferred editor.
Definition: pgm_base.cpp:225
virtual bool SetLanguage(wxString &aErrMsg, bool first_time=false)
Set the dictionary file name for internationalization.
Definition: pgm_base.cpp:640
bool LoadProject(const wxString &aFullPath, bool aSetActive=true)
Loads 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:672
The common library.
void DisplayInfoMessage(wxWindow *aParent, const wxString &aMessage, const wxString &aExtraInfo)
Display an informational message box with aMessage.
Definition: confirm.cpp:332
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Display an error message with aMessage.
Definition: confirm.cpp:305
This file is part of the common library.
#define _(s)
Base window classes and related definitions.
#define WIN_STRING_DIR_SEP
Definition: gestfich.h:36
#define UNIX_STRING_DIR_SEP
Definition: gestfich.h:35
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:139
@ ID_LANGUAGE_CHINESE_SIMPLIFIED
Definition: id.h:129
@ ID_LANGUAGE_GERMAN
Definition: id.h:113
@ ID_LANGUAGE_DUTCH
Definition: id.h:131
@ ID_LANGUAGE_GREEK
Definition: id.h:114
@ ID_LANGUAGE_LITHUANIAN
Definition: id.h:135
@ ID_LANGUAGE_PORTUGUESE_BRAZILIAN
Definition: id.h:118
@ ID_LANGUAGE_DEFAULT
Definition: id.h:107
@ ID_LANGUAGE_POLISH
Definition: id.h:125
@ ID_LANGUAGE_THAI
Definition: id.h:138
@ ID_LANGUAGE_JAPANESE
Definition: id.h:132
@ ID_LANGUAGE_ITALIAN
Definition: id.h:121
@ ID_LANGUAGE_KOREAN
Definition: id.h:127
@ ID_LANGUAGE_RUSSIAN
Definition: id.h:116
@ ID_LANGUAGE_CZECH
Definition: id.h:126
@ ID_LANGUAGE_UKRANIAN
Definition: id.h:140
@ ID_LANGUAGE_SPANISH
Definition: id.h:111
@ ID_LANGUAGE_PORTUGUESE
Definition: id.h:117
@ ID_LANGUAGE_ENGLISH
Definition: id.h:108
@ ID_LANGUAGE_SPANISH_MEXICAN
Definition: id.h:112
@ ID_LANGUAGE_CHINESE_TRADITIONAL
Definition: id.h:130
@ ID_LANGUAGE_FINNISH
Definition: id.h:110
@ ID_LANGUAGE_DANISH
Definition: id.h:106
@ ID_LANGUAGE_FRENCH
Definition: id.h:109
std::map< wxString, ENV_VAR_ITEM > ENV_VAR_MAP
This file contains miscellaneous commonly used macros and functions.
Macros and inline functions to create menus items in menubars or popup menus.
PBOOL GetPolicyBool(const wxString &aKey)
Definition: gtk/policy.cpp:26
LANGUAGE_DESCR LanguagesList[]
An array containing all the languages that KiCad supports.
Definition: pgm_base.cpp:91
#define _(s)
Current list of languages supported by KiCad.
Definition: pgm_base.cpp:127
see class PGM_BASE
PGM_BASE & Pgm()
The global Program "get" accessor.
Definition: single_top.cpp:115
LANGUAGE_DESCR LanguagesList[]
An array containing all the languages that KiCad supports.
Definition: pgm_base.cpp:91
#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:39
APIIMPORT wxPGGlobalVarsClass * wxPGGlobalVars
wxString From_UTF8(const char *cstring)
A small class to handle the list of existing translations.
Definition: pgm_base.h:65
int m_KI_Lang_Identifier
KiCad identifier used in menu selection (See id.h)
Definition: pgm_base.h:70
wxString m_Lang_Label
Labels used in menus.
Definition: pgm_base.h:73
System directories search utilities.
wxLogTrace helper definitions.