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