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.

196 lines
6.7 KiB

  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 (C) 2014-2018 KiCad Developers, see CHANGELOG.TXT for contributors.
  6. * @author Maciej Suminski <maciej.suminski@cern.ch>
  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. #include <pgm_base.h>
  26. #include <common.h>
  27. #include <config.h> // to define DEFAULT_INSTALL_PATH
  28. #include <macros.h>
  29. #include <trace_helpers.h>
  30. /**
  31. * Function FindFileInSearchPaths
  32. * looks in "this" for \a aFilename, but first modifies every search
  33. * path by appending a list of path fragments from aSubdirs. That modification
  34. * is not relative.
  35. */
  36. wxString FindFileInSearchPaths( const SEARCH_STACK& aStack,
  37. const wxString& aFilename, const wxArrayString* aSubdirs )
  38. {
  39. wxPathList paths;
  40. for( unsigned i = 0; i < aStack.GetCount(); ++i )
  41. {
  42. wxFileName fn( aStack[i], wxEmptyString );
  43. if( aSubdirs )
  44. {
  45. for( unsigned j = 0; j < aSubdirs->GetCount(); j++ )
  46. fn.AppendDir( (*aSubdirs)[j] );
  47. }
  48. wxLogTrace( tracePathsAndFiles, " %s", fn.GetFullPath() );
  49. if( fn.DirExists() )
  50. {
  51. paths.Add( fn.GetPath() );
  52. }
  53. }
  54. return paths.FindValidPath( aFilename );
  55. }
  56. // See also FindKicadHelpPath.cpp.notused.
  57. wxString SearchHelpFileFullPath( const SEARCH_STACK& aSStack, const wxString& aBaseName )
  58. {
  59. wxArrayString subdirs;
  60. wxArrayString altsubdirs;
  61. SEARCH_STACK ss = aSStack;
  62. // It might already be in aSStack, but why depend on other code
  63. // far away when it's so easy to add it again (to our copy) as the first place to look.
  64. // This is CMAKE_INSTALL_PREFIX unless DEFAULT_INSTALL_PATH was defined during
  65. // build configuration:
  66. ss.AddPaths( wxT( DEFAULT_INSTALL_PATH ), 0 );
  67. #if defined(__WXMAC__)
  68. ss.AddPaths( GetOSXKicadMachineDataDir() );
  69. ss.AddPaths( Pgm().GetExecutablePath(), 0 );
  70. // OS X packages can have the help files in
  71. // /Library/Application\ Support/kicad/help,
  72. // and in Contents/SharedSupport/help inside the
  73. // bundle.
  74. // Below we account for an international subdirectory.
  75. subdirs.Add( "help" );
  76. altsubdirs.Add( "Contents" );
  77. altsubdirs.Add( "SharedSupport" );
  78. altsubdirs.Add( "help" );
  79. #endif
  80. #if ! defined(__WXMAC__) // && defined(__linux__)
  81. // This is the executable path minus the trailing bin directory used on Windows and Linux.
  82. wxFileName tmp( Pgm().GetExecutablePath(), wxEmptyString );
  83. wxArrayString binDirs = tmp.GetDirs();
  84. if( !binDirs.IsEmpty() && binDirs[ binDirs.GetCount() - 1 ].CmpNoCase( wxT( "bin" ) ) == 0 )
  85. tmp.RemoveLastDir();
  86. ss.AddPaths( tmp.GetPath(), 0 );
  87. // Based on kicad-doc.bzr/CMakeLists.txt, line 20, the help files are
  88. // installed into "<CMAKE_INSTALL_PREFIX>/share/doc/kicad/help" for linux.
  89. // This is ${KICAD_HELP} var in that CMakeLists.txt file.
  90. // Below we account for an international subdirectory.
  91. subdirs.Add( "share" );
  92. subdirs.Add( "doc" );
  93. subdirs.Add( "kicad" );
  94. subdirs.Add( "help" );
  95. // Based on kicad-doc.bzr/CMakeLists.txt, line 35, the help files are
  96. // installed into "<CMAKE_INSTALL_PREFIX>/doc/help" for Windows.
  97. // This is ${KICAD_HELP} var in that CMakeLists.txt file.
  98. // Below we account for an international subdirectory.
  99. altsubdirs.Add( "doc" );
  100. altsubdirs.Add( "help" );
  101. #endif
  102. // If there's a KICAD environment variable set, always use that guy's path first.
  103. if( !Pgm().GetKicadEnvVariable().IsEmpty() )
  104. ss.AddPaths( Pgm().GetKicadEnvVariable(), 0 );
  105. /* Search for a help file.
  106. * we *must* find a help file.
  107. * so help is searched in directories in this order:
  108. * help/<canonical name> like help/en_GB
  109. * help/<short name> like help/en
  110. * help/en
  111. */
  112. wxLocale* i18n = Pgm().GetLocale();
  113. // We try to find help file in help/<canonical name>
  114. // If fails, try to find help file in help/<short canonical name>
  115. // If fails, try to find help file in help/en
  116. wxArrayString locale_name_dirs;
  117. locale_name_dirs.Add( i18n->GetCanonicalName() ); // canonical name like fr_FR
  118. // wxLocale::GetName() does not return always the short name
  119. locale_name_dirs.Add( i18n->GetName().BeforeLast( '_' ) ); // short canonical name like fr
  120. locale_name_dirs.Add( "en" ); // default (en)
  121. #if defined(DEBUG) && 1
  122. ss.Show( wxString( __func__ ) );
  123. wxLogTrace( tracePathsAndFiles, "%s: m_help_file:'%s'", __func__, aBaseName );
  124. #endif
  125. wxLogTrace( tracePathsAndFiles, "Checking SEARCH_STACK for file %s", aBaseName );
  126. // Help files can be html (.html ext) or pdf (.pdf ext) files.
  127. // Therefore, <BaseName>.html file is searched and if not found,
  128. // <BaseName>.pdf file is searched in the same paths
  129. wxString fn;
  130. for( unsigned ii = 0; ii < locale_name_dirs.GetCount(); ii++ )
  131. {
  132. subdirs.Add( locale_name_dirs[ii] );
  133. altsubdirs.Add( locale_name_dirs[ii] );
  134. fn = FindFileInSearchPaths( ss, aBaseName + wxT( ".html" ), &altsubdirs );
  135. if( !fn.IsEmpty() )
  136. {
  137. // Prepend URI protocol since we will open in a browser
  138. fn = wxT( "file://" ) + fn;
  139. break;
  140. }
  141. fn = FindFileInSearchPaths( ss, aBaseName + wxT( ".pdf" ), &altsubdirs );
  142. if( !fn.IsEmpty() )
  143. break;
  144. fn = FindFileInSearchPaths( ss, aBaseName + wxT( ".html" ), &subdirs );
  145. if( !fn.IsEmpty() )
  146. {
  147. // Prepend URI protocol since we will open in a browser
  148. fn = wxT( "file://" ) + fn;
  149. break;
  150. }
  151. fn = FindFileInSearchPaths( ss, aBaseName + wxT( ".pdf" ), &subdirs );
  152. if( !fn.IsEmpty() )
  153. break;
  154. subdirs.RemoveAt( subdirs.GetCount() - 1 );
  155. altsubdirs.RemoveAt( altsubdirs.GetCount() - 1 );
  156. }
  157. return fn;
  158. }