You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

115 lines
4.0 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2014-2015 CERN
  5. * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
  6. * @author Maciej Suminski <maciej.suminski@cern.ch>
  7. *
  8. * This program is free software: you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation, either version 2 of the License, or (at your
  11. * option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #include <common.h>
  22. #include <pgm_base.h>
  23. #include <paths.h>
  24. #include <systemdirsappend.h>
  25. #include <trace_helpers.h>
  26. #include <wx/arrstr.h>
  27. #include <wx/log.h>
  28. wxString SearchHelpFileFullPath( const wxString& aBaseName )
  29. {
  30. SEARCH_STACK basePaths;
  31. wxString helpFile;
  32. // help files are most likely located in the documentation install path
  33. basePaths.Add( PATHS::GetDocumentationPath() );
  34. #ifndef __WIN32__
  35. // just in case, add all known system directories to the search stack
  36. SystemDirsAppend( &basePaths );
  37. #endif
  38. #if defined( DEBUG )
  39. basePaths.Show( wxString( __func__ ) + wxS( ": basePaths" ) );
  40. #endif
  41. // By default, the documentation from kicad-doc is installed to a folder called "help" with
  42. // subdirectories for all supported languages. Although this can be changed at build-time by
  43. // overwriting ${KICAD_DOC_PATH}, the best guess KiCad can make is that help files are always
  44. // located in a folder named "help". If no translation matching the current locale settings is
  45. // available, the English version will be returned instead.
  46. wxLocale* currentLocale = Pgm().GetLocale();
  47. wxArrayString localeNameDirs;
  48. // canonical form of the current locale (e.g., "fr_FR")
  49. localeNameDirs.Add( currentLocale->GetCanonicalName() );
  50. // short form of the current locale (e.g., "fr")
  51. // wxLocale::GetName() does not always return the short form
  52. localeNameDirs.Add( currentLocale->GetName().BeforeLast( wxS( '_' ) ) );
  53. // plain English (in case a localised version of the help file cannot be found)
  54. localeNameDirs.Add( wxS( "en" ) );
  55. for( wxString& locale : localeNameDirs )
  56. {
  57. SEARCH_STACK docPaths;
  58. for( wxString& base : basePaths )
  59. {
  60. wxFileName path( base, wxEmptyString );
  61. // add <base>/help/<locale>/
  62. path.AppendDir( wxS( "help" ) );
  63. path.AppendDir( locale );
  64. docPaths.AddPaths( path.GetPath() );
  65. // add <base>/doc/help/<locale>/
  66. path.InsertDir( path.GetDirCount() - 2, wxS( "doc" ) );
  67. docPaths.AddPaths( path.GetPath() );
  68. // add <base>/doc/kicad/help/<locale>/
  69. path.InsertDir( path.GetDirCount() - 2, wxS( "kicad" ) );
  70. docPaths.AddPaths( path.GetPath() );
  71. }
  72. #if defined( DEBUG )
  73. docPaths.Show( wxString( __func__ ) + wxS( ": docPaths (" ) + locale + wxS( ")" ) );
  74. #endif
  75. // search HTML first, as it is the preferred format for help files
  76. wxLogTrace( tracePathsAndFiles, wxS( "Checking SEARCH_STACK for file %s.html" ),
  77. aBaseName );
  78. helpFile = docPaths.FindValidPath( aBaseName + wxS( ".html" ) );
  79. if( !helpFile.IsEmpty() )
  80. {
  81. // prepend URI protocol to open the file in a browser
  82. helpFile = wxS( "file://" ) + helpFile;
  83. break;
  84. }
  85. // search PDF only when no corresponding HTML file was found
  86. wxLogTrace( tracePathsAndFiles, wxS( "Checking SEARCH_STACK for file %s.pdf" ), aBaseName );
  87. helpFile = docPaths.FindValidPath( aBaseName + wxS( ".pdf" ) );
  88. if( !helpFile.IsEmpty() )
  89. break;
  90. }
  91. return helpFile;
  92. }