KiCad PCB EDA Suite
eda_doc.cpp File Reference
#include <pgm_base.h>
#include <common.h>
#include <confirm.h>
#include <gestfich.h>
#include <settings/common_settings.h>
#include <wx/mimetype.h>
#include <wx/filename.h>
#include <wx/uri.h>
#include <wx/filedlg.h>

Go to the source code of this file.

Functions

bool GetAssociatedDocument (wxWindow *aParent, const wxString &aDocName, PROJECT *aProject, SEARCH_STACK *aPaths)
 Open a document (file) with the suitable browser. More...
 

Variables

static wxMimeTypesManager * mimeDatabase
 
static const wxFileTypeInfo EDAfallbacks []
 

Function Documentation

◆ GetAssociatedDocument()

bool GetAssociatedDocument ( wxWindow *  aParent,
const wxString &  aDocName,
PROJECT aProject,
SEARCH_STACK aPaths = nullptr 
)

Open a document (file) with the suitable browser.

Environmental variables are substituted before the document name is resolved for either browser or file. If aDocName begins with http: or ftp: or www. the default internet browser is launched.

Parameters
aParentmain frame.
aDocNamefilename of file to open (Full filename or short filename).
aPathsAdditional paths to search for local disk datasheet files

Definition at line 74 of file eda_doc.cpp.

75 {
76  wxString docname;
77  wxString fullfilename;
78  wxString msg;
79  wxString command;
80  bool success = false;
81 
82  // Is an internet url
83  static const std::vector<wxString> url_header =
84  {
85  wxT( "http:" ),
86  wxT( "https:" ),
87  wxT( "ftp:" ),
88  wxT( "www." ),
89  wxT( "file:" )
90  };
91 
92  // Replace before resolving as we might have a URL in a variable
93  docname = ResolveUriByEnvVars( aDocName, aProject );
94 
95  for( const wxString& proc : url_header)
96  {
97  if( docname.StartsWith( proc ) ) // looks like an internet url
98  {
99  wxURI uri( docname );
100  wxLaunchDefaultBrowser( uri.BuildURI() );
101  return true;
102  }
103  }
104 
105 #ifdef __WINDOWS__
106  docname.Replace( UNIX_STRING_DIR_SEP, WIN_STRING_DIR_SEP );
107 #else
108  docname.Replace( WIN_STRING_DIR_SEP, UNIX_STRING_DIR_SEP );
109 #endif
110 
111  /* Compute the full file name */
112  if( wxIsAbsolutePath( docname ) || aPaths == nullptr )
113  fullfilename = docname;
114  /* If the file exists, this is a trivial case: return the filename "as this". the name can
115  * be an absolute path, or a relative path like ./filename or ../<filename>.
116  */
117  else if( wxFileName::FileExists( docname ) )
118  fullfilename = docname;
119  else
120  fullfilename = aPaths->FindValidPath( docname );
121 
122  wxString extension;
123 
124 #ifdef __WINDOWS__
125  extension = wxT( ".*" );
126 #endif
127 
128  if( wxIsWild( fullfilename ) )
129  {
130  fullfilename = wxFileSelector( _( "Documentation File" ), wxPathOnly( fullfilename ),
131  fullfilename, extension, wxFileSelectorDefaultWildcardStr,
132  wxFD_OPEN, aParent );
133 
134  if( fullfilename.IsEmpty() )
135  return false;
136  }
137 
138  if( !wxFileExists( fullfilename ) )
139  {
140  msg.Printf( _( "Documentation file '%s' not found." ), docname );
141  DisplayError( aParent, msg );
142  return false;
143  }
144 
145  wxFileName currentFileName( fullfilename );
146 
147  wxString file_ext = currentFileName.GetExt();
148 
149  if( file_ext.Lower() == wxT( "pdf" ) )
150  {
151  success = OpenPDF( fullfilename );
152  return success;
153  }
154 
155  /* Try to launch some browser (useful under linux) */
156  wxFileType* filetype;
157 
158  wxString type;
159  filetype = wxTheMimeTypesManager->GetFileTypeFromExtension( file_ext );
160 
161  if( !filetype ) // 2nd attempt.
162  {
163  mimeDatabase = new wxMimeTypesManager;
164  mimeDatabase->AddFallbacks( EDAfallbacks );
165  filetype = mimeDatabase->GetFileTypeFromExtension( file_ext );
166  delete mimeDatabase;
167  mimeDatabase = nullptr;
168  }
169 
170  if( filetype )
171  {
172  wxFileType::MessageParameters params( fullfilename, type );
173 
174  success = filetype->GetOpenCommand( &command, params );
175  delete filetype;
176 
177  if( success )
178  success = wxExecute( command );
179  }
180 
181  if( !success )
182  {
183  msg.Printf( _( "Unknown MIME type for documentation file '%s'" ), fullfilename );
184  DisplayError( aParent, msg );
185  }
186 
187  return success;
188 }
void DisplayError(wxWindow *aParent, const wxString &aText, int aDisplayTime)
Display an error or warning message box with aMessage.
Definition: confirm.cpp:279
#define WIN_STRING_DIR_SEP
Definition: gestfich.h:36
#define UNIX_STRING_DIR_SEP
Definition: gestfich.h:35
static wxMimeTypesManager * mimeDatabase
Definition: eda_doc.cpp:52
static const wxFileTypeInfo EDAfallbacks[]
Definition: eda_doc.cpp:53
const wxString ResolveUriByEnvVars(const wxString &aUri, PROJECT *aProject)
Replace any environment and/or text variables in file-path uris (leaving network-path URIs alone).
Definition: common.cpp:280
#define _(s)
bool OpenPDF(const wxString &file)
Run the PDF viewer and display a PDF file.
Definition: gestfich.cpp:159

References _, DisplayError(), EDAfallbacks, mimeDatabase, OpenPDF(), ResolveUriByEnvVars(), UNIX_STRING_DIR_SEP, and WIN_STRING_DIR_SEP.

Referenced by FIELDS_GRID_TRICKS::doPopupSelection(), FIELDS_EDITOR_GRID_TRICKS::doPopupSelection(), TEXT_BUTTON_URL::OnButtonClick(), EE_INSPECTION_TOOL::ShowDatasheet(), and COMMON_CONTROL::ShowHelp().

Variable Documentation

◆ EDAfallbacks

const wxFileTypeInfo EDAfallbacks[]
static
Initial value:
=
{
wxFileTypeInfo( wxT( "text/html" ),
wxT( "wxhtml %s" ),
wxT( "wxhtml %s" ),
wxT( "html document (from KiCad)" ),
wxT( "htm" ),
wxT( "html" ),wxNullPtr ),
wxFileTypeInfo( wxT( "application/sch" ),
wxT( "eeschema %s" ),
wxT( "eeschema -p %s" ),
wxT( "sch document (from KiCad)" ),
wxT( "sch" ),
wxT( "SCH" ), wxNullPtr ),
wxFileTypeInfo()
}

Definition at line 53 of file eda_doc.cpp.

Referenced by GetAssociatedDocument().

◆ mimeDatabase

wxMimeTypesManager* mimeDatabase
static

Definition at line 52 of file eda_doc.cpp.

Referenced by GetAssociatedDocument().