2014-10-19 20:20:16 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
2015-02-06 21:13:35 +00:00
|
|
|
* Copyright (C) 2014-2015 CERN
|
2018-10-17 19:03:33 +00:00
|
|
|
* Copyright (C) 2014-2018 KiCad Developers, see CHANGELOG.TXT for contributors.
|
2014-10-19 20:20:16 +00:00
|
|
|
* @author Maciej Suminski <maciej.suminski@cern.ch>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, you may find one here:
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 2 license,
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
2014-04-14 18:49:52 +00:00
|
|
|
|
|
|
|
#include <pgm_base.h>
|
|
|
|
#include <common.h>
|
2015-05-15 12:49:11 +00:00
|
|
|
#include <config.h> // to define DEFAULT_INSTALL_PATH
|
2015-09-20 18:23:17 +00:00
|
|
|
#include <macros.h>
|
2020-10-01 15:15:15 +00:00
|
|
|
#include <wx/log.h>
|
2018-10-17 19:03:33 +00:00
|
|
|
#include <trace_helpers.h>
|
2014-04-14 18:49:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function FindFileInSearchPaths
|
|
|
|
* looks in "this" for \a aFilename, but first modifies every search
|
|
|
|
* path by appending a list of path fragments from aSubdirs. That modification
|
2015-02-06 21:13:35 +00:00
|
|
|
* is not relative.
|
2014-04-14 18:49:52 +00:00
|
|
|
*/
|
|
|
|
wxString FindFileInSearchPaths( const SEARCH_STACK& aStack,
|
|
|
|
const wxString& aFilename, const wxArrayString* aSubdirs )
|
|
|
|
{
|
|
|
|
wxPathList paths;
|
|
|
|
|
|
|
|
for( unsigned i = 0; i < aStack.GetCount(); ++i )
|
|
|
|
{
|
|
|
|
wxFileName fn( aStack[i], wxEmptyString );
|
|
|
|
|
|
|
|
if( aSubdirs )
|
|
|
|
{
|
|
|
|
for( unsigned j = 0; j < aSubdirs->GetCount(); j++ )
|
|
|
|
fn.AppendDir( (*aSubdirs)[j] );
|
|
|
|
}
|
|
|
|
|
2018-10-17 19:03:33 +00:00
|
|
|
wxLogTrace( tracePathsAndFiles, " %s", fn.GetFullPath() );
|
2015-09-20 18:23:17 +00:00
|
|
|
|
2014-04-14 18:49:52 +00:00
|
|
|
if( fn.DirExists() )
|
|
|
|
{
|
|
|
|
paths.Add( fn.GetPath() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return paths.FindValidPath( aFilename );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// See also FindKicadHelpPath.cpp.notused.
|
|
|
|
wxString SearchHelpFileFullPath( const SEARCH_STACK& aSStack, const wxString& aBaseName )
|
|
|
|
{
|
|
|
|
wxArrayString subdirs;
|
|
|
|
wxArrayString altsubdirs;
|
|
|
|
SEARCH_STACK ss = aSStack;
|
|
|
|
|
|
|
|
// It might already be in aSStack, but why depend on other code
|
|
|
|
// far away when it's so easy to add it again (to our copy) as the first place to look.
|
|
|
|
|
2015-09-20 18:23:17 +00:00
|
|
|
// This is CMAKE_INSTALL_PREFIX unless DEFAULT_INSTALL_PATH was defined during
|
|
|
|
// build configuration:
|
|
|
|
ss.AddPaths( wxT( DEFAULT_INSTALL_PATH ), 0 );
|
2014-04-14 18:49:52 +00:00
|
|
|
|
2015-02-06 21:13:35 +00:00
|
|
|
#if defined(__WXMAC__)
|
|
|
|
ss.AddPaths( GetOSXKicadMachineDataDir() );
|
|
|
|
ss.AddPaths( Pgm().GetExecutablePath(), 0 );
|
2015-09-20 18:23:17 +00:00
|
|
|
|
|
|
|
// OS X packages can have the help files in
|
|
|
|
// /Library/Application\ Support/kicad/help,
|
|
|
|
// and in Contents/SharedSupport/help inside the
|
|
|
|
// bundle.
|
|
|
|
// Below we account for an international subdirectory.
|
2018-10-17 19:03:33 +00:00
|
|
|
subdirs.Add( "help" );
|
|
|
|
altsubdirs.Add( "Contents" );
|
|
|
|
altsubdirs.Add( "SharedSupport" );
|
|
|
|
altsubdirs.Add( "help" );
|
2015-02-06 21:13:35 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if ! defined(__WXMAC__) // && defined(__linux__)
|
2015-09-20 18:23:17 +00:00
|
|
|
// This is the executable path minus the trailing bin directory used on Windows and Linux.
|
|
|
|
wxFileName tmp( Pgm().GetExecutablePath(), wxEmptyString );
|
|
|
|
wxArrayString binDirs = tmp.GetDirs();
|
|
|
|
|
|
|
|
if( !binDirs.IsEmpty() && binDirs[ binDirs.GetCount() - 1 ].CmpNoCase( wxT( "bin" ) ) == 0 )
|
|
|
|
tmp.RemoveLastDir();
|
|
|
|
|
|
|
|
ss.AddPaths( tmp.GetPath(), 0 );
|
|
|
|
|
2014-04-14 18:49:52 +00:00
|
|
|
// Based on kicad-doc.bzr/CMakeLists.txt, line 20, the help files are
|
|
|
|
// installed into "<CMAKE_INSTALL_PREFIX>/share/doc/kicad/help" for linux.
|
|
|
|
// This is ${KICAD_HELP} var in that CMakeLists.txt file.
|
|
|
|
// Below we account for an international subdirectory.
|
2018-10-17 19:03:33 +00:00
|
|
|
subdirs.Add( "share" );
|
|
|
|
subdirs.Add( "doc" );
|
|
|
|
subdirs.Add( "kicad" );
|
|
|
|
subdirs.Add( "help" );
|
2014-04-14 18:49:52 +00:00
|
|
|
|
|
|
|
// Based on kicad-doc.bzr/CMakeLists.txt, line 35, the help files are
|
|
|
|
// installed into "<CMAKE_INSTALL_PREFIX>/doc/help" for Windows.
|
|
|
|
// This is ${KICAD_HELP} var in that CMakeLists.txt file.
|
|
|
|
// Below we account for an international subdirectory.
|
2018-10-17 19:03:33 +00:00
|
|
|
altsubdirs.Add( "doc" );
|
|
|
|
altsubdirs.Add( "help" );
|
2014-04-14 18:49:52 +00:00
|
|
|
#endif
|
|
|
|
|
2015-09-20 18:23:17 +00:00
|
|
|
// If there's a KICAD environment variable set, always use that guy's path first.
|
|
|
|
if( !Pgm().GetKicadEnvVariable().IsEmpty() )
|
|
|
|
ss.AddPaths( Pgm().GetKicadEnvVariable(), 0 );
|
2015-02-06 21:13:35 +00:00
|
|
|
|
2014-04-14 18:49:52 +00:00
|
|
|
/* Search for a help file.
|
|
|
|
* we *must* find a help file.
|
|
|
|
* so help is searched in directories in this order:
|
|
|
|
* help/<canonical name> like help/en_GB
|
|
|
|
* help/<short name> like help/en
|
|
|
|
* help/en
|
|
|
|
*/
|
|
|
|
|
|
|
|
wxLocale* i18n = Pgm().GetLocale();
|
|
|
|
|
2014-10-17 18:30:56 +00:00
|
|
|
// We try to find help file in help/<canonical name>
|
|
|
|
// If fails, try to find help file in help/<short canonical name>
|
|
|
|
// If fails, try to find help file in help/en
|
|
|
|
wxArrayString locale_name_dirs;
|
|
|
|
locale_name_dirs.Add( i18n->GetCanonicalName() ); // canonical name like fr_FR
|
2015-09-20 18:23:17 +00:00
|
|
|
|
2014-10-17 18:30:56 +00:00
|
|
|
// wxLocale::GetName() does not return always the short name
|
|
|
|
locale_name_dirs.Add( i18n->GetName().BeforeLast( '_' ) ); // short canonical name like fr
|
2018-10-17 19:03:33 +00:00
|
|
|
locale_name_dirs.Add( "en" ); // default (en)
|
2014-04-14 18:49:52 +00:00
|
|
|
|
2015-09-20 18:23:17 +00:00
|
|
|
#if defined(DEBUG) && 1
|
|
|
|
ss.Show( wxString( __func__ ) );
|
2018-10-17 19:03:33 +00:00
|
|
|
wxLogTrace( tracePathsAndFiles, "%s: m_help_file:'%s'", __func__, aBaseName );
|
2014-04-14 18:49:52 +00:00
|
|
|
#endif
|
|
|
|
|
2018-10-17 19:03:33 +00:00
|
|
|
wxLogTrace( tracePathsAndFiles, "Checking SEARCH_STACK for file %s", aBaseName );
|
2015-09-20 18:23:17 +00:00
|
|
|
|
2014-09-02 16:44:53 +00:00
|
|
|
// Help files can be html (.html ext) or pdf (.pdf ext) files.
|
|
|
|
// Therefore, <BaseName>.html file is searched and if not found,
|
|
|
|
// <BaseName>.pdf file is searched in the same paths
|
2014-10-17 18:30:56 +00:00
|
|
|
wxString fn;
|
2014-09-02 16:44:53 +00:00
|
|
|
|
2014-10-17 18:30:56 +00:00
|
|
|
for( unsigned ii = 0; ii < locale_name_dirs.GetCount(); ii++ )
|
2014-04-14 18:49:52 +00:00
|
|
|
{
|
2014-10-17 18:30:56 +00:00
|
|
|
subdirs.Add( locale_name_dirs[ii] );
|
|
|
|
altsubdirs.Add( locale_name_dirs[ii] );
|
2014-04-14 18:49:52 +00:00
|
|
|
|
2015-02-06 21:13:35 +00:00
|
|
|
fn = FindFileInSearchPaths( ss, aBaseName + wxT( ".html" ), &altsubdirs );
|
2014-04-14 18:49:52 +00:00
|
|
|
|
2014-10-17 18:30:56 +00:00
|
|
|
if( !fn.IsEmpty() )
|
2018-03-28 21:18:03 +00:00
|
|
|
{
|
|
|
|
// Prepend URI protocol since we will open in a browser
|
|
|
|
fn = wxT( "file://" ) + fn;
|
2014-10-17 18:30:56 +00:00
|
|
|
break;
|
2018-03-28 21:18:03 +00:00
|
|
|
}
|
2014-10-15 13:14:14 +00:00
|
|
|
|
2015-02-06 21:13:35 +00:00
|
|
|
fn = FindFileInSearchPaths( ss, aBaseName + wxT( ".pdf" ), &altsubdirs );
|
2014-10-15 13:14:14 +00:00
|
|
|
|
2014-10-17 18:30:56 +00:00
|
|
|
if( !fn.IsEmpty() )
|
|
|
|
break;
|
2014-04-14 18:49:52 +00:00
|
|
|
|
2015-02-06 21:13:35 +00:00
|
|
|
fn = FindFileInSearchPaths( ss, aBaseName + wxT( ".html" ), &subdirs );
|
2014-04-14 18:49:52 +00:00
|
|
|
|
2014-10-17 18:30:56 +00:00
|
|
|
if( !fn.IsEmpty() )
|
2018-03-28 21:18:03 +00:00
|
|
|
{
|
|
|
|
// Prepend URI protocol since we will open in a browser
|
|
|
|
fn = wxT( "file://" ) + fn;
|
2014-10-17 18:30:56 +00:00
|
|
|
break;
|
2018-03-28 21:18:03 +00:00
|
|
|
}
|
2014-04-14 18:49:52 +00:00
|
|
|
|
2015-02-06 21:13:35 +00:00
|
|
|
fn = FindFileInSearchPaths( ss, aBaseName + wxT( ".pdf" ), &subdirs );
|
2014-09-02 16:44:53 +00:00
|
|
|
|
2014-10-17 18:30:56 +00:00
|
|
|
if( !fn.IsEmpty() )
|
|
|
|
break;
|
2014-09-02 16:44:53 +00:00
|
|
|
|
2014-10-17 18:30:56 +00:00
|
|
|
subdirs.RemoveAt( subdirs.GetCount() - 1 );
|
|
|
|
altsubdirs.RemoveAt( altsubdirs.GetCount() - 1 );
|
2014-04-14 18:49:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return fn;
|
|
|
|
}
|