KiCad PCB EDA Suite
eda_doc.h File Reference

This file is part of the common library. More...

Go to the source code of this file.

Functions

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

Detailed Description

This file is part of the common library.

See also
common.h

Definition in file eda_doc.h.

Function Documentation

◆ GetAssociatedDocument()

bool GetAssociatedDocument ( wxWindow *  aParent,
const wxString &  aDocName,
PROJECT aProject 
)

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).

Definition at line 74 of file eda_doc.cpp.

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

References _, DisplayError(), EDAfallbacks, SEARCH_STACK::FindValidPath(), 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().