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 KeywordMatch (const wxString &aKeys, const wxString &aDatabase)
 Function KeywordMatch searches aKeyList for any words found in aDatabase. More...
 
bool GetAssociatedDocument (wxWindow *aParent, const wxString &aDocName, const wxPathList *aPaths=NULL)
 Function GetAssociatedDocument 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,
const wxPathList *  aPaths = NULL 
)

Function GetAssociatedDocument open a document (file) with the suitable browser.

Environmental variables are substituted before the document name is resolved for either browser or file

Parameters
aParent= main frame
aDocName= filename of file to open (Full filename or short filename) if aDocName begins with http: or ftp: or www. the default internet browser is launched
aPaths= a wxPathList to explore. if NULL or aDocName is a full filename, aPath is not used.

Definition at line 87 of file eda_doc.cpp.

91 {
92  wxString docname, fullfilename;
93  wxString msg;
94  wxString command;
95  bool success = false;
96 
97  // Is an internet url
98  static const wxChar* url_header[] = {
99  wxT( "http:" ),
100  wxT( "https:" ),
101  wxT( "ftp:" ),
102  wxT( "www." ),
103  wxT( "file:" )
104  };
105 
106  // Replace before resolving as we might have a URL in a variable
107  docname = ResolveUriByEnvVars( aDocName );
108 
109  for( unsigned ii = 0; ii < arrayDim(url_header); ii++ )
110  {
111  if( docname.First( url_header[ii] ) == 0 ) // looks like an internet url
112  {
113  wxURI uri( docname );
114  wxLaunchDefaultBrowser( uri.BuildURI() );
115  return true;
116  }
117  }
118 
119 #ifdef __WINDOWS__
120  docname.Replace( UNIX_STRING_DIR_SEP, WIN_STRING_DIR_SEP );
121 #else
122  docname.Replace( WIN_STRING_DIR_SEP, UNIX_STRING_DIR_SEP );
123 #endif
124 
125 
126  /* Compute the full file name */
127  if( wxIsAbsolutePath( docname ) || aPaths == NULL)
128  fullfilename = docname;
129  /* If the file exists, this is a trivial case: return the filename
130  * "as this". the name can be an absolute path, or a relative path
131  * like ./filename or ../<filename>
132  */
133  else if( wxFileName::FileExists( docname ) )
134  fullfilename = docname;
135  else
136  {
137  fullfilename = aPaths->FindValidPath( docname );
138  }
139 
140  wxString mask( wxT( "*" ) ), extension;
141 
142 #ifdef __WINDOWS__
143  mask += wxT( ".*" );
144  extension = wxT( ".*" );
145 #endif
146 
147  if( wxIsWild( fullfilename ) )
148  {
149  fullfilename = EDA_FILE_SELECTOR( _( "Doc Files" ),
150  wxPathOnly( fullfilename ),
151  fullfilename,
152  extension,
153  mask,
154  aParent,
155  wxFD_OPEN,
156  true,
157  wxPoint( -1, -1 ) );
158  if( fullfilename.IsEmpty() )
159  return false;
160  }
161 
162  if( !wxFileExists( fullfilename ) )
163  {
164  msg.Printf( _( "Doc File \"%s\" not found" ), GetChars( docname ) );
165  DisplayError( aParent, msg );
166  return false;
167  }
168 
169  wxFileName currentFileName( fullfilename );
170 
171  wxString file_ext = currentFileName.GetExt();
172 
173  if( file_ext.Lower() == wxT( "pdf" ) )
174  {
175  success = OpenPDF( fullfilename );
176  return success;
177  }
178 
179  /* Try to launch some browser (useful under linux) */
180  wxFileType* filetype;
181 
182  wxString type;
183  filetype = wxTheMimeTypesManager->GetFileTypeFromExtension( file_ext );
184 
185  if( !filetype ) // 2nd attempt.
186  {
187  mimeDatabase = new wxMimeTypesManager;
188  mimeDatabase->AddFallbacks( EDAfallbacks );
189  filetype = mimeDatabase->GetFileTypeFromExtension( file_ext );
190  delete mimeDatabase;
191  mimeDatabase = NULL;
192  }
193 
194  if( filetype )
195  {
196  wxFileType::MessageParameters params( fullfilename, type );
197 
198  success = filetype->GetOpenCommand( &command, params );
199  delete filetype;
200 
201  if( success )
202  success = ProcessExecute( command );
203  }
204 
205  if( !success )
206  {
207  msg.Printf( _( "Unknown MIME type for doc file \"%s\"" ), GetChars( fullfilename ) );
208  DisplayError( aParent, msg );
209  }
210 
211  return success;
212 }
int ProcessExecute(const wxString &aCommandLine, int aFlags, wxProcess *callback)
Run a command in a child process.
Definition: common.cpp:210
#define WIN_STRING_DIR_SEP
Definition: gestfich.h:44
const wxString ResolveUriByEnvVars(const wxString &aUri)
Replace any environment variables in file-path uris (leaving network-path URIs alone).
Definition: common.cpp:471
#define UNIX_STRING_DIR_SEP
Definition: gestfich.h:43
static wxMimeTypesManager * mimeDatabase
Definition: eda_doc.cpp:65
static const wxFileTypeInfo EDAfallbacks[]
Definition: eda_doc.cpp:66
wxString EDA_FILE_SELECTOR(const wxString &aTitle, const wxString &aPath, const wxString &aFileName, const wxString &aExtension, const wxString &aWildcard, wxWindow *aParent, int aStyle, const bool aKeepWorkingDirectory, const wxPoint &aPosition, wxString *aMruPath)
Function EDA_FILE_SELECTOR.
Definition: gestfich.cpp:82
constexpr std::size_t arrayDim(T const (&)[N]) noexcept
Definition: macros.h:99
static const wxChar * GetChars(const wxString &s)
Function GetChars returns a wxChar* to the actual wxChar* data within a wxString, and is helpful for ...
Definition: macros.h:92
void DisplayError(wxWindow *parent, const wxString &text, int displaytime)
Function DisplayError displays an error or warning message box with aMessage.
Definition: confirm.cpp:243
bool OpenPDF(const wxString &file)
Function OpenPDF run the PDF viewer and display a PDF file.
Definition: gestfich.cpp:345

References arrayDim(), DisplayError(), EDA_FILE_SELECTOR(), EDAfallbacks, GetChars(), mimeDatabase, OpenPDF(), ProcessExecute(), ResolveUriByEnvVars(), UNIX_STRING_DIR_SEP, and WIN_STRING_DIR_SEP.

Referenced by FIELDS_GRID_TRICKS::doPopupSelection(), FIELDS_EDITOR_GRID_TRICKS::doPopupSelection(), EDA_BASE_FRAME::GetKicadHelp(), TEXT_BUTTON_URL::OnButtonClick(), SCH_EDIT_FRAME::OnEditItem(), LIB_EDIT_FRAME::OnViewEntryDoc(), LIB_VIEW_FRAME::onViewSymbolDocument(), and PCB_EDIT_FRAME::Process_Special_Functions().

◆ KeywordMatch()

bool KeywordMatch ( const wxString &  aKeys,
const wxString &  aDatabase 
)

Function KeywordMatch searches aKeyList for any words found in aDatabase.

Returns
true if keyword is found.

Definition at line 215 of file eda_doc.cpp.

216 {
217  if( aKeys.IsEmpty() )
218  return false;
219 
220  wxStringTokenizer keyTokenizer( aKeys, wxT( ", \t\n\r" ), wxTOKEN_STRTOK );
221 
222  while( keyTokenizer.HasMoreTokens() )
223  {
224  wxString key = keyTokenizer.GetNextToken();
225 
226  // Search for key in aDatabase:
227  wxStringTokenizer dataTokenizer( aDatabase, wxT( ", \t\n\r" ), wxTOKEN_STRTOK );
228 
229  while( dataTokenizer.HasMoreTokens() )
230  {
231  if( dataTokenizer.GetNextToken() == key )
232  return true;
233  }
234  }
235 
236  // keyword not found
237  return false;
238 }