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.

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