KiCad PCB EDA Suite
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>
46#include <common.h>
47#include <config_params.h>
48#include <confirm.h>
49#include <core/arraydim.h>
50#include <eda_base_frame.h>
51#include <eda_draw_frame.h>
52#include <gestfich.h>
53#include <id.h>
54#include <kiplatform/policy.h>
55#include <lockfile.h>
56#include <menus_helpers.h>
57#include <paths.h>
58#include <pgm_base.h>
59#include <policy_keys.h>
60#include <python_scripting.h>
63#include <systemdirsappend.h>
64#include <thread_pool.h>
65#include <trace_helpers.h>
66
67#ifdef KICAD_USE_SENTRY
68#include <boost/uuid/uuid_io.hpp>
69#include <boost/uuid/uuid_generators.hpp>
70#include <sentry.h>
71#include <kicad_build_version.h>
72#endif
73
81#undef _
82#define _(s) s
84{
85 { wxLANGUAGE_DEFAULT, ID_LANGUAGE_DEFAULT, _( "Default" ), false },
86 { wxLANGUAGE_INDONESIAN, ID_LANGUAGE_INDONESIAN, wxT( "Bahasa Indonesia" ), true },
87 { wxLANGUAGE_CZECH, ID_LANGUAGE_CZECH, wxT( "Čeština" ), true },
88 { wxLANGUAGE_DANISH, ID_LANGUAGE_DANISH, wxT( "Dansk" ), true },
89 { wxLANGUAGE_GERMAN, ID_LANGUAGE_GERMAN, wxT( "Deutsch" ), true },
90 { wxLANGUAGE_GREEK, ID_LANGUAGE_GREEK, wxT( "Ελληνικά" ), true },
91 { wxLANGUAGE_ENGLISH, ID_LANGUAGE_ENGLISH, wxT( "English" ), true },
92 { wxLANGUAGE_SPANISH, ID_LANGUAGE_SPANISH, wxT( "Español" ), true },
93 { wxLANGUAGE_SPANISH_MEXICAN, ID_LANGUAGE_SPANISH_MEXICAN, wxT( "Español (Latinoamericano)" ), true },
94 { wxLANGUAGE_FRENCH, ID_LANGUAGE_FRENCH, wxT( "Français" ), true },
95 { wxLANGUAGE_KOREAN, ID_LANGUAGE_KOREAN, wxT( "한국어"), true },
96 { wxLANGUAGE_ITALIAN, ID_LANGUAGE_ITALIAN, wxT( "Italiano" ), true },
97 { wxLANGUAGE_LITHUANIAN, ID_LANGUAGE_LITHUANIAN, wxT( "Lietuvių" ), true },
98 { wxLANGUAGE_HUNGARIAN, ID_LANGUAGE_HUNGARIAN, wxT( "Magyar" ), true },
99 { wxLANGUAGE_JAPANESE, ID_LANGUAGE_JAPANESE, wxT( "日本語" ), true },
100 { wxLANGUAGE_THAI, ID_LANGUAGE_THAI, wxT( "ภาษาไทย" ), true },
101 { wxLANGUAGE_POLISH, ID_LANGUAGE_POLISH, wxT( "Polski" ), true },
102 { wxLANGUAGE_PORTUGUESE, ID_LANGUAGE_PORTUGUESE, wxT( "Português" ),true },
103 { wxLANGUAGE_PORTUGUESE_BRAZILIAN, ID_LANGUAGE_PORTUGUESE_BRAZILIAN, wxT( "Português (Brasil)" ), true },
104 { wxLANGUAGE_RUSSIAN, ID_LANGUAGE_RUSSIAN, wxT( "Русский" ), true },
105 { wxLANGUAGE_SERBIAN, ID_LANGUAGE_SERBIAN, wxT( "Српски" ), true },
106 { wxLANGUAGE_FINNISH, ID_LANGUAGE_FINNISH, wxT( "Suomi" ), true },
107 { wxLANGUAGE_SWEDISH, ID_LANGUAGE_SWEDISH, wxT( "Svenska" ), true },
108 { wxLANGUAGE_VIETNAMESE, ID_LANGUAGE_VIETNAMESE, wxT( "Tiếng Việt" ), true },
109 { wxLANGUAGE_TURKISH, ID_LANGUAGE_TURKISH, wxT( "Türkçe" ), true },
110 { wxLANGUAGE_UKRAINIAN, ID_LANGUAGE_UKRANIAN, wxT( "Українець" ), true },
111 { wxLANGUAGE_CHINESE_SIMPLIFIED, ID_LANGUAGE_CHINESE_SIMPLIFIED,
112 wxT( "简体中文" ), true },
113 { wxLANGUAGE_CHINESE_TRADITIONAL, ID_LANGUAGE_CHINESE_TRADITIONAL,
114 wxT( "繁體中文" ), false },
115 { 0, 0, "", false } // Sentinel
116};
117#undef _
118#define _(s) wxGetTranslation((s))
119
120
122{
123 m_locale = nullptr;
124 m_Printing = false;
125 m_Quitting = false;
127 m_argcUtf8 = 0;
128 m_argvUtf8 = nullptr;
129
130 setLanguageId( wxLANGUAGE_DEFAULT );
131
132 ForceSystemPdfBrowser( false );
133}
134
135
137{
138 Destroy();
139
140 for( int n = 0; n < m_argcUtf8; n++ )
141 {
142 delete m_argvUtf8[n];
143 }
144
145 delete m_argvUtf8;
146}
147
148
150{
151#ifdef KICAD_USE_SENTRY
152 sentry_close();
153#endif
154
155 // unlike a normal destructor, this is designed to be called more than once safely:
156 delete m_locale;
157 m_locale = nullptr;
158}
159
160
162{
163 wxASSERT( wxTheApp );
164 return *wxTheApp;
165}
166
167
168void PGM_BASE::SetTextEditor( const wxString& aFileName )
169{
170 m_text_editor = aFileName;
172}
173
174
175const wxString& PGM_BASE::GetTextEditor( bool aCanShowFileChooser )
176{
177 wxString editorname = m_text_editor;
178
179 if( !editorname )
180 {
181 if( !wxGetEnv( wxT( "EDITOR" ), &editorname ) )
182 {
183 // If there is no EDITOR variable set, try the desktop default
184#ifdef __WXMAC__
185 editorname = wxT( "/usr/bin/open -e" );
186#elif __WXX11__
187 editorname = wxT( "/usr/bin/xdg-open" );
188#endif
189 }
190 }
191
192 // If we still don't have an editor name show a dialog asking the user to select one
193 if( !editorname && aCanShowFileChooser )
194 {
195 DisplayInfoMessage( nullptr, _( "No default editor found, you must choose one." ) );
196
197 editorname = AskUserForPreferredEditor();
198 }
199
200 // If we finally have a new editor name request it to be copied to m_text_editor and
201 // saved to the preferences file.
202 if( !editorname.IsEmpty() )
203 SetTextEditor( editorname );
204
205 // m_text_editor already has the same value that editorname, or empty if no editor was
206 // found/chosen.
207 return m_text_editor;
208}
209
210
211const wxString PGM_BASE::AskUserForPreferredEditor( const wxString& aDefaultEditor )
212{
213 // Create a mask representing the executable files in the current platform
214#ifdef __WINDOWS__
215 wxString mask( _( "Executable file" ) + wxT( " (*.exe)|*.exe" ) );
216#else
217 wxString mask( _( "Executable file" ) + wxT( " (*)|*" ) );
218#endif
219
220 // Extract the path, name and extension from the default editor (even if the editor's
221 // name was empty, this method will succeed and return empty strings).
222 wxString path, name, ext;
223 wxFileName::SplitPath( aDefaultEditor, &path, &name, &ext );
224
225 // Show the modal editor and return the file chosen (may be empty if the user cancels
226 // the dialog).
227 return wxFileSelector( _( "Select Preferred Editor" ), path, name, wxT( "." ) + ext,
228 mask, wxFD_OPEN | wxFD_FILE_MUST_EXIST, nullptr );
229}
230
231
232#ifdef KICAD_USE_SENTRY
233bool PGM_BASE::IsSentryOptedIn()
234{
235 KIPLATFORM::POLICY::STATE policyState =
238 {
239 return policyState == KIPLATFORM::POLICY::STATE::ENABLED;
240 }
241
242 return m_sentry_optin_fn.Exists();
243}
244
245
246void PGM_BASE::SetSentryOptIn( bool aOptIn )
247{
248 if( aOptIn )
249 {
250 if( !m_sentry_uid_fn.Exists() )
251 {
252 sentryCreateUid();
253 }
254
255 if( !m_sentry_optin_fn.Exists() )
256 {
257 wxFFile sentryInitFile( m_sentry_optin_fn.GetFullPath(), "w" );
258 sentryInitFile.Write( "" );
259 sentryInitFile.Close();
260 }
261 }
262 else
263 {
264 if( m_sentry_optin_fn.Exists() )
265 {
266 wxRemoveFile( m_sentry_optin_fn.GetFullPath() );
267 sentry_close();
268 }
269 }
270}
271
272
273wxString PGM_BASE::sentryCreateUid()
274{
275 boost::uuids::uuid uuid = boost::uuids::random_generator()();
276 wxString userGuid = boost::uuids::to_string( uuid );
277
278 wxFFile sentryInitFile( m_sentry_uid_fn.GetFullPath(), "w" );
279 sentryInitFile.Write( userGuid );
280 sentryInitFile.Close();
281
282 return userGuid;
283}
284
285
286void PGM_BASE::ResetSentryId()
287{
288 m_sentryUid = sentryCreateUid();
289}
290
291
292const wxString& PGM_BASE::GetSentryId()
293{
294 return m_sentryUid;
295}
296
297
298void PGM_BASE::sentryInit()
299{
300 m_sentry_optin_fn = wxFileName( PATHS::GetUserCachePath(), "sentry-opt-in" );
301 m_sentry_uid_fn = wxFileName( PATHS::GetUserCachePath(), "sentry-uid" );
302
303 if( IsSentryOptedIn() )
304 {
305 wxFFile sentryInitFile( m_sentry_uid_fn.GetFullPath() );
306 sentryInitFile.ReadAll( &m_sentryUid );
307 sentryInitFile.Close();
308
309 if( m_sentryUid.IsEmpty() || m_sentryUid.length() != 36 )
310 {
311 m_sentryUid = sentryCreateUid();
312 }
313
314 sentry_options_t* options = sentry_options_new();
315
316 sentry_options_set_dsn(
317 options,
318 "https://[email protected]/6266565" );
319
320 wxFileName tmp;
321 tmp.AssignDir( PATHS::GetUserCachePath() );
322 tmp.AppendDir( "sentry" );
323
324 sentry_options_set_database_pathw( options, tmp.GetPathWithSep().wc_str() );
325 sentry_options_set_symbolize_stacktraces( options, true );
326 sentry_options_set_auto_session_tracking( options, false );
327
328#if !KICAD_IS_NIGHTLY
329 sentry_options_set_release( options, KICAD_SEMANTIC_VERSION );
330#else
331 sentry_options_set_release( options, KICAD_COMMIT_HASH );
332#endif
333
334 sentry_init( options );
335
336 sentry_value_t user = sentry_value_new_object();
337 sentry_value_set_by_key( user, "id", sentry_value_new_string( m_sentryUid.c_str() ) );
338 sentry_set_user( user );
339
340 sentry_set_tag( "kicad.version", KICAD_VERSION_FULL );
341 }
342}
343
344
345void PGM_BASE::sentryPrompt()
346{
347 KIPLATFORM::POLICY::STATE policyState =
349
351 && !m_settings_manager->GetCommonSettings()->m_DoNotShowAgain.data_collection_prompt )
352 {
353 wxMessageDialog optIn = wxMessageDialog(
354 nullptr,
355 _( "KiCad can anonymously report crashes and special event "
356 "data to developers in order to aid identifying critical bugs "
357 "across the user base more effectively and help profile "
358 "functionality to guide improvements. \n"
359 "If you choose to voluntarily participate, KiCad will automatically "
360 "handle sending said reports when crashes or events occur. \n"
361 "Your design files such as schematic or PCB are not shared in this process." ),
362 _( "Data collection opt in request" ), wxYES_NO | wxCENTRE );
363
364 int result = optIn.ShowModal();
365
366 if( result == wxID_YES )
367 {
368 SetSentryOptIn( true );
369 sentryInit();
370 }
371 else
372 {
373 SetSentryOptIn( false );
374 }
375
376 m_settings_manager->GetCommonSettings()->m_DoNotShowAgain.data_collection_prompt = true;
377 }
378}
379#endif
380
381
383{
384 const wxArrayString& argArray = App().argv.GetArguments();
385 m_argcUtf8 = argArray.size();
386
387 m_argvUtf8 = new char*[m_argcUtf8 + 1];
388 for( int n = 0; n < m_argcUtf8; n++ )
389 {
390 m_argvUtf8[n] = wxStrdup( argArray[n].ToUTF8() );
391 }
392
393 m_argvUtf8[m_argcUtf8] = NULL; // null terminator at end of argv
394}
395
396
397bool PGM_BASE::InitPgm( bool aHeadless, bool aSkipPyInit, bool aIsUnitTest )
398{
399 // Just make sure we init precreate any folders early for later code
400 // In particular, the user cache path is the most likely to be hit by startup code
402
403#ifdef KICAD_USE_SENTRY
404 sentryInit();
405#endif
406 wxString pgm_name;
407
409 if( App().argc == 0 )
410 pgm_name = wxT( "kicad" );
411 else
412 pgm_name = wxFileName( App().argv[0] ).GetName().Lower();
413
414#ifdef KICAD_USE_SENTRY
415 sentry_set_tag( "kicad.app", pgm_name.c_str() );
416#endif
417
418 wxInitAllImageHandlers();
419
420 // Without this the wxPropertyGridManager segfaults on Windows.
421 if( !wxPGGlobalVars )
422 wxPGInitResourceModule();
423
424#ifndef __WINDOWS__
425 if( wxString( wxGetenv( "HOME" ) ).IsEmpty() )
426 {
427 DisplayErrorMessage( nullptr, _( "Environment variable HOME is empty. "
428 "Unable to continue." ) );
429 return false;
430 }
431#endif
432
433 // Init KiCad environment
434 // the environment variable KICAD (if exists) gives the kicad path:
435 // something like set KICAD=d:\kicad
436 bool isDefined = wxGetEnv( wxT( "KICAD" ), &m_kicad_env );
437
438 if( isDefined ) // ensure m_kicad_env ends by "/"
439 {
441
442 if( !m_kicad_env.IsEmpty() && m_kicad_env.Last() != '/' )
444 }
445
446 // Init parameters for configuration
447 App().SetVendorName( wxT( "KiCad" ) );
448 App().SetAppName( pgm_name );
449
450 // Install some image handlers, mainly for help
451 if( wxImage::FindHandler( wxBITMAP_TYPE_PNG ) == nullptr )
452 wxImage::AddHandler( new wxPNGHandler );
453
454 if( wxImage::FindHandler( wxBITMAP_TYPE_GIF ) == nullptr )
455 wxImage::AddHandler( new wxGIFHandler );
456
457 if( wxImage::FindHandler( wxBITMAP_TYPE_JPEG ) == nullptr )
458 wxImage::AddHandler( new wxJPEGHandler );
459
460 wxFileSystem::AddHandler( new wxZipFSHandler );
461
462 // Analyze the command line & initialize the binary path
463 wxString tmp;
466 SetDefaultLanguage( tmp );
467
468 m_settings_manager = std::make_unique<SETTINGS_MANAGER>( aHeadless );
469
470 // Our unit test mocks break if we continue
471 // A bug caused InitPgm to terminate early in unit tests and the mocks are...simplistic
472 // TODO fix the unit tests so this can be removed
473 if( aIsUnitTest )
474 return false;
475
476 // Something got in the way of settings load: can't continue
477 if( !m_settings_manager->IsOK() )
478 return false;
479
480 // Set up built-in environment variables (and override them from the system environment if set)
482
483 // Load color settings after env is initialized
484 m_settings_manager->ReloadColorSettings();
485
486 // Load common settings from disk after setting up env vars
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
505 // Create the python scripting stuff
506 // Skip it fot applications that do not use it
507 if( !aSkipPyInit )
508 m_python_scripting = std::make_unique<SCRIPTING>();
509
510 // TODO(JE): Remove this if apps are refactored to not assume Prj() always works
511 // Need to create a project early for now (it can have an empty path for the moment)
513
514 // This sets the maximum tooltip display duration to 10s (up from 5) but only affects
515 // Windows as other platforms display tooltips while the mouse is not moving
516 if( !aHeadless )
517 {
518 wxToolTip::Enable( true );
519 wxToolTip::SetAutoPop( 10000 );
520 }
521
522 if( ADVANCED_CFG::GetCfg().m_UpdateUIEventInterval != 0 )
523 wxUpdateUIEvent::SetUpdateInterval( ADVANCED_CFG::GetCfg().m_UpdateUIEventInterval );
524
525 return true;
526}
527
528
530{
531 m_bin_dir = wxStandardPaths::Get().GetExecutablePath();
532
533#ifdef __WXMAC__
534 // On OSX Pgm().GetExecutablePath() will always point to main
535 // bundle directory, e.g., /Applications/kicad.app/
536
537 wxFileName fn( m_bin_dir );
538
539 if( fn.GetName() == wxT( "kicad" ) || fn.GetName() == wxT( "kicad-cli" ) )
540 {
541 // kicad launcher, so just remove the Contents/MacOS part
542 fn.RemoveLastDir();
543 fn.RemoveLastDir();
544 }
545 else
546 {
547 // standalone binaries live in Contents/Applications/<standalone>.app/Contents/MacOS
548 fn.RemoveLastDir();
549 fn.RemoveLastDir();
550 fn.RemoveLastDir();
551 fn.RemoveLastDir();
552 fn.RemoveLastDir();
553 }
554
555 m_bin_dir = fn.GetPath() + wxT( "/" );
556#else
557 // Use unix notation for paths. I am not sure this is a good idea,
558 // but it simplifies compatibility between Windows and Unices.
559 // However it is a potential problem in path handling under Windows.
561
562 // Remove file name form command line:
563 while( m_bin_dir.Last() != '/' && !m_bin_dir.IsEmpty() )
564 m_bin_dir.RemoveLast();
565#endif
566
567 return true;
568}
569
570
572{
574
575 for( const std::pair<wxString, ENV_VAR_ITEM> it : GetCommonSettings()->m_Env.vars )
576 {
577 wxLogTrace( traceEnvVars, wxT( "PGM_BASE::loadSettings: Found entry %s = %s" ),
578 it.first, it.second.GetValue() );
579
580 // Do not store the env var PROJECT_VAR_NAME ("KIPRJMOD") definition if for some reason
581 // it is found in config. (It is reserved and defined as project path)
582 if( it.first == PROJECT_VAR_NAME )
583 continue;
584
585 // Don't set bogus empty entries in the environment
586 if( it.first.IsEmpty() )
587 continue;
588
589 // Do not overwrite vars set by the system environment with values from the settings file
590 if( it.second.GetDefinedExternally() )
591 continue;
592
593 SetLocalEnvVariable( it.first, it.second.GetValue() );
594 }
595}
596
597
599{
600 // GetCommonSettings() is not initialized until fairly late in the
601 // process startup: InitPgm(), so test before using:
602 if( GetCommonSettings() )
603 GetCommonSettings()->m_System.working_dir = wxGetCwd();
604}
605
606
608{
609 return m_settings_manager ? m_settings_manager->GetCommonSettings() : nullptr;
610}
611
612
613bool PGM_BASE::SetLanguage( wxString& aErrMsg, bool first_time )
614{
615 if( first_time )
616 {
617 setLanguageId( wxLANGUAGE_DEFAULT );
618 // First time SetLanguage is called, the user selected language id is set
619 // from common user config settings
620 wxString languageSel = GetCommonSettings()->m_System.language;
621
622 // Search for the current selection
623 for( unsigned ii = 0; LanguagesList[ii].m_KI_Lang_Identifier != 0; ii++ )
624 {
625 if( LanguagesList[ii].m_Lang_Label == languageSel )
626 {
627 setLanguageId( LanguagesList[ii].m_WX_Lang_Identifier );
628 break;
629 }
630 }
631 }
632
633 // dictionary file name without extend (full name is kicad.mo)
634 wxString dictionaryName( wxT( "kicad" ) );
635
636 delete m_locale;
637 m_locale = new wxLocale;
638
639 // don't use wxLOCALE_LOAD_DEFAULT flag so that Init() doesn't return
640 // false just because it failed to load wxstd catalog
641 if( !m_locale->Init( m_language_id, wxLOCALE_DONT_LOAD_DEFAULT ) )
642 {
643 wxLogTrace( traceLocale, wxT( "This language is not supported by the system." ) );
644
645 setLanguageId( wxLANGUAGE_DEFAULT );
646 delete m_locale;
647
648 m_locale = new wxLocale;
649 m_locale->Init( wxLANGUAGE_DEFAULT, wxLOCALE_DONT_LOAD_DEFAULT);
650
651 aErrMsg = _( "This language is not supported by the operating system." );
652 return false;
653 }
654 else if( !first_time )
655 {
656 wxLogTrace( traceLocale, wxT( "Search for dictionary %s.mo in %s" ) ,
657 dictionaryName, m_locale->GetName() );
658 }
659
660 if( !first_time )
661 {
662 // If we are here, the user has selected another language.
663 // Therefore the new preferred language name is stored in common config.
664 // Do NOT store the wxWidgets language Id, it can change between wxWidgets
665 // versions, for a given language
666 wxString languageSel;
667
668 // Search for the current selection language name
669 for( unsigned ii = 0; LanguagesList[ii].m_KI_Lang_Identifier != 0; ii++ )
670 {
671 if( LanguagesList[ii].m_WX_Lang_Identifier == m_language_id )
672 {
673 languageSel = LanguagesList[ii].m_Lang_Label;
674 break;
675 }
676 }
677
679 cfg->m_System.language = languageSel;
680 cfg->SaveToFile( GetSettingsManager().GetPathForSettingsFile( cfg ) );
681 }
682
683 // Try adding the dictionary if it is not currently loaded
684 if( !m_locale->IsLoaded( dictionaryName ) )
685 m_locale->AddCatalog( dictionaryName );
686
687 // Verify the Kicad dictionary was loaded properly
688 // However, for the English language, the dictionary is not mandatory, as
689 // all messages are already in English, just restricted to ASCII7 chars,
690 // the verification is skipped.
691 if( !m_locale->IsLoaded( dictionaryName ) && m_language_id != wxLANGUAGE_ENGLISH )
692 {
693 wxLogTrace( traceLocale, wxT( "Unable to load dictionary %s.mo in %s" ),
694 dictionaryName, m_locale->GetName() );
695
696 setLanguageId( wxLANGUAGE_DEFAULT );
697 delete m_locale;
698
699 m_locale = new wxLocale;
700 m_locale->Init( wxLANGUAGE_DEFAULT, wxLOCALE_DONT_LOAD_DEFAULT);
701
702 aErrMsg = _( "The KiCad language file for this language is not installed." );
703 return false;
704 }
705
706 return true;
707}
708
709
710bool PGM_BASE::SetDefaultLanguage( wxString& aErrMsg )
711{
712 setLanguageId( wxLANGUAGE_DEFAULT );
713
714 // dictionary file name without extend (full name is kicad.mo)
715 wxString dictionaryName( wxT( "kicad" ) );
716
717 delete m_locale;
718 m_locale = new wxLocale;
719 m_locale->Init();
720
721 // Try adding the dictionary if it is not currently loaded
722 if( !m_locale->IsLoaded( dictionaryName ) )
723 m_locale->AddCatalog( dictionaryName );
724
725 // Verify the Kicad dictionary was loaded properly
726 // However, for the English language, the dictionary is not mandatory, as
727 // all messages are already in English, just restricted to ASCII7 chars,
728 // the verification is skipped.
729 if( !m_locale->IsLoaded( dictionaryName ) && m_language_id != wxLANGUAGE_ENGLISH )
730 {
731 wxLogTrace( traceLocale, wxT( "Unable to load dictionary %s.mo in %s" ),
732 dictionaryName, m_locale->GetName() );
733
734 setLanguageId( wxLANGUAGE_DEFAULT );
735 delete m_locale;
736
737 m_locale = new wxLocale;
738 m_locale->Init();
739
740 aErrMsg = _( "The KiCad language file for this language is not installed." );
741 return false;
742 }
743
744 return true;
745}
746
747
749{
750 wxLogTrace( traceLocale, wxT( "Select language ID %d from %d possible languages." ),
751 menu_id, (int)arrayDim( LanguagesList )-1 );
752
753 for( unsigned ii = 0; LanguagesList[ii].m_KI_Lang_Identifier != 0; ii++ )
754 {
755 if( menu_id == LanguagesList[ii].m_KI_Lang_Identifier )
756 {
757 setLanguageId( LanguagesList[ii].m_WX_Lang_Identifier );
758 break;
759 }
760 }
761}
762
763
765{
766 wxLocale::AddCatalogLookupPathPrefix( PATHS::GetLocaleDataPath() );
767
768 if( wxGetEnv( wxT( "KICAD_RUN_FROM_BUILD_DIR" ), nullptr ) )
769 {
770 wxFileName fn( Pgm().GetExecutablePath() );
771 fn.RemoveLastDir();
772 fn.AppendDir( wxT( "translation" ) );
773 wxLocale::AddCatalogLookupPathPrefix( fn.GetPath() );
774 }
775}
776
777
778bool PGM_BASE::SetLocalEnvVariable( const wxString& aName, const wxString& aValue )
779{
780 wxString env;
781
782 if( aName.IsEmpty() )
783 {
784 wxLogTrace( traceEnvVars,
785 wxT( "PGM_BASE::SetLocalEnvVariable: Attempt to set empty variable to value %s" ),
786 aValue );
787 return false;
788 }
789
790 // Check to see if the environment variable is already set.
791 if( wxGetEnv( aName, &env ) )
792 {
793 wxLogTrace( traceEnvVars,
794 wxT( "PGM_BASE::SetLocalEnvVariable: Environment variable %s already set to %s" ),
795 aName, env );
796 return env == aValue;
797 }
798
799 wxLogTrace( traceEnvVars,
800 wxT( "PGM_BASE::SetLocalEnvVariable: Setting local environment variable %s to %s" ),
801 aName, aValue );
802
803 return wxSetEnv( aName, aValue );
804}
805
806
808{
809 // Overwrites externally defined environment variable until the next time the application
810 // is run.
811 for( const std::pair<wxString, ENV_VAR_ITEM> m_local_env_var : GetCommonSettings()->m_Env.vars )
812 {
813 wxLogTrace( traceEnvVars,
814 wxT( "PGM_BASE::SetLocalEnvVariables: Setting local environment variable %s 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
823{
824 return GetCommonSettings()->m_Env.vars;
825}
const char * name
Definition: DXF_plotter.cpp:56
constexpr std::size_t arrayDim(T const (&)[N]) noexcept
Returns # of elements in an array.
Definition: arraydim.h:31
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
virtual bool SaveToFile(const wxString &aDirectory="", bool aForce=false)
static void EnsureUserPathsExist()
Ensures/creates user default paths.
Definition: paths.cpp:369
static wxString GetUserCachePath()
Gets the stock (install) 3d viewer plugins path.
Definition: paths.cpp:321
static wxString GetLocaleDataPath()
Gets the locales translation data path.
Definition: paths.cpp:252
virtual COMMON_SETTINGS * GetCommonSettings() const
Definition: pgm_base.cpp:607
virtual wxApp & App()
Returns a bare naked wxApp which may come from wxPython, SINGLE_TOP, or kicad.exe.
Definition: pgm_base.cpp:161
int m_argcUtf8
argv parameters converted to utf8 form, because wxwidgets has opinions and will return argv as either...
Definition: pgm_base.h:354
virtual ENV_VAR_MAP & GetLocalEnvVariables() const
Definition: pgm_base.cpp:822
bool setExecutablePath()
Find the path to the executable and stores it in PGM_BASE::m_bin_dir.
Definition: pgm_base.cpp:529
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:303
void setLanguageId(int aId)
Trap all changes in here, simplifies debugging.
Definition: pgm_base.h:314
wxString m_bin_dir
Definition: pgm_base.h:334
std::unique_ptr< SETTINGS_MANAGER > m_settings_manager
Definition: pgm_base.h:330
void loadCommonSettings()
Loads internal settings from COMMON_SETTINGS.
Definition: pgm_base.cpp:571
wxLocale * m_locale
The KICAD system environment variable.
Definition: pgm_base.h:337
void Destroy()
Definition: pgm_base.cpp:149
bool InitPgm(bool aHeadless=false, bool aSkipPyInit=false, bool aIsUnitTest=false)
Initialize this program.
Definition: pgm_base.cpp:397
char ** m_argvUtf8
Definition: pgm_base.h:351
virtual void ReadPdfBrowserInfos()
Read the PDF browser choice from the common configuration.
Definition: eda_doc.cpp:37
bool m_Quitting
Definition: pgm_base.h:307
void BuildArgvUtf8()
Builds the UTF8 based argv variable.
Definition: pgm_base.cpp:382
virtual void SetTextEditor(const wxString &aFileName)
Definition: pgm_base.cpp:168
virtual const wxString & GetExecutablePath() const
Definition: pgm_base.h:165
virtual bool SetLocalEnvVariable(const wxString &aName, const wxString &aValue)
Sets the environment variable aName to aValue.
Definition: pgm_base.cpp:778
wxString m_kicad_env
full path to this program
Definition: pgm_base.h:335
virtual void ForceSystemPdfBrowser(bool aFlg)
Force the use of system PDF browser, even if a preferred PDF browser is set.
Definition: pgm_base.h:186
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:807
virtual void SetLanguagePath()
Definition: pgm_base.cpp:764
bool SetDefaultLanguage(wxString &aErrMsg)
Set the default language without reference to any preferences.
Definition: pgm_base.cpp:710
wxString m_text_editor
Filename of the app selected for browsing PDFs.
Definition: pgm_base.h:343
virtual ~PGM_BASE()
Definition: pgm_base.cpp:136
std::unique_ptr< SCRIPTING > m_python_scripting
Definition: pgm_base.h:332
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:748
int m_language_id
Definition: pgm_base.h:338
virtual const wxString & GetTextEditor(bool aCanShowFileChooser=true)
Return the path to the preferred text editor application.
Definition: pgm_base.cpp:175
virtual SETTINGS_MANAGER & GetSettingsManager() const
Definition: pgm_base.h:134
void SaveCommonSettings()
Save the program (process) settings subset which are stored .kicad_common.
Definition: pgm_base.cpp:598
int m_ModalDialogCount
Definition: pgm_base.h:305
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:211
virtual bool SetLanguage(wxString &aErrMsg, bool first_time=false)
Set the dictionary file name for internationalization.
Definition: pgm_base.cpp:613
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:603
The common library.
void DisplayInfoMessage(wxWindow *aParent, const wxString &aMessage, const wxString &aExtraInfo)
Display an informational message box with aMessage.
Definition: confirm.cpp:320
void DisplayErrorMessage(wxWindow *aParent, const wxString &aText, const wxString &aExtraInfo)
Display an error message with aMessage.
Definition: confirm.cpp:299
This file is part of the common library.
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 traceEnvVars
Flag to enable debug output of environment variable operations.
const wxChar *const traceLocale
Flag to enable locale debug output.
@ ID_LANGUAGE_SWEDISH
Definition: id.h:139
@ ID_LANGUAGE_CHINESE_SIMPLIFIED
Definition: id.h:129
@ ID_LANGUAGE_GERMAN
Definition: id.h:113
@ ID_LANGUAGE_GREEK
Definition: id.h:114
@ ID_LANGUAGE_HUNGARIAN
Definition: id.h:124
@ 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_VIETNAMESE
Definition: id.h:136
@ ID_LANGUAGE_ITALIAN
Definition: id.h:121
@ ID_LANGUAGE_KOREAN
Definition: id.h:127
@ ID_LANGUAGE_SERBIAN
Definition: id.h:137
@ 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_TURKISH
Definition: id.h:119
@ ID_LANGUAGE_DANISH
Definition: id.h:106
@ ID_LANGUAGE_FRENCH
Definition: id.h:109
@ ID_LANGUAGE_INDONESIAN
Definition: id.h:120
std::map< wxString, ENV_VAR_ITEM > ENV_VAR_MAP
File locking utilities.
Macros and inline functions to create menus items in menubars or popup menus.
STATE GetPolicyState(const wxString &aKey)
Definition: gtk/policy.cpp:25
LANGUAGE_DESCR LanguagesList[]
An array containing all the languages that KiCad supports.
Definition: pgm_base.cpp:83
#define _(s)
Current list of languages supported by KiCad.
Definition: pgm_base.cpp:118
see class PGM_BASE
#define POLICY_KEY_DATACOLLECTION
Definition: policy_keys.h:28
#define PROJECT_VAR_NAME
A variable name whose value holds the current project directory.
Definition: project.h:39
APIIMPORT wxPGGlobalVarsClass * wxPGGlobalVars
KIWAY Kiway & Pgm(), KFCTL_STANDALONE
The global Program "get" accessor.
Definition: single_top.cpp:111
A small class to handle the list of existing translations.
Definition: pgm_base.h:60
int m_KI_Lang_Identifier
KiCad identifier used in menu selection (See id.h)
Definition: pgm_base.h:65
wxString m_Lang_Label
Labels used in menus.
Definition: pgm_base.h:68
System directories search utilities.
wxLogTrace helper definitions.