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, PROJECT *aProject)
 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,
PROJECT aProject 
)

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

Definition at line 80 of file eda_doc.cpp.

81 {
82  SEARCH_STACK* aPaths = nullptr;
83  wxString docname;
84  wxString fullfilename;
85  wxString msg;
86  wxString command;
87  bool success = false;
88 
89 #if defined(EESCHEMA)
90  SEARCH_STACK* aPaths = aProject ? aProject->SchSearchS() : nullptr;
91 #endif
92 
93  // Is an internet url
94  static const wxChar* url_header[] = {
95  wxT( "http:" ),
96  wxT( "https:" ),
97  wxT( "ftp:" ),
98  wxT( "www." ),
99  wxT( "file:" )
100  };
101 
102  // Replace before resolving as we might have a URL in a variable
103  docname = ResolveUriByEnvVars( aDocName, aProject );
104 
105  for( const wxString& proc : url_header)
106  {
107  if( docname.First( proc ) == 0 ) // looks like an internet url
108  {
109  wxURI uri( docname );
110  wxLaunchDefaultBrowser( uri.BuildURI() );
111  return true;
112  }
113  }
114 
115 #ifdef __WINDOWS__
116  docname.Replace( UNIX_STRING_DIR_SEP, WIN_STRING_DIR_SEP );
117 #else
118  docname.Replace( WIN_STRING_DIR_SEP, UNIX_STRING_DIR_SEP );
119 #endif
120 
121 
122  /* Compute the full file name */
123  if( wxIsAbsolutePath( docname ) || aPaths == NULL )
124  fullfilename = docname;
125  /* If the file exists, this is a trivial case: return the filename
126  * "as this". the name can be an absolute path, or a relative path
127  * like ./filename or ../<filename>
128  */
129  else if( wxFileName::FileExists( docname ) )
130  fullfilename = docname;
131  else
132  fullfilename = aPaths->FindValidPath( docname );
133 
134  wxString mask( wxT( "*" ) ), extension;
135 
136 #ifdef __WINDOWS__
137  mask += wxT( ".*" );
138  extension = wxT( ".*" );
139 #endif
140 
141  if( wxIsWild( fullfilename ) )
142  {
143  fullfilename = EDA_FILE_SELECTOR( _( "Doc Files" ),
144  wxPathOnly( fullfilename ),
145  fullfilename,
146  extension,
147  mask,
148  aParent,
149  wxFD_OPEN,
150  true,
151  wxPoint( -1, -1 ) );
152  if( fullfilename.IsEmpty() )
153  return false;
154  }
155 
156  if( !wxFileExists( fullfilename ) )
157  {
158  msg.Printf( _( "Doc File \"%s\" not found" ), GetChars( docname ) );
159  DisplayError( aParent, msg );
160  return false;
161  }
162 
163  wxFileName currentFileName( fullfilename );
164 
165  wxString file_ext = currentFileName.GetExt();
166 
167  if( file_ext.Lower() == wxT( "pdf" ) )
168  {
169  success = OpenPDF( fullfilename );
170  return success;
171  }
172 
173  /* Try to launch some browser (useful under linux) */
174  wxFileType* filetype;
175 
176  wxString type;
177  filetype = wxTheMimeTypesManager->GetFileTypeFromExtension( file_ext );
178 
179  if( !filetype ) // 2nd attempt.
180  {
181  mimeDatabase = new wxMimeTypesManager;
182  mimeDatabase->AddFallbacks( EDAfallbacks );
183  filetype = mimeDatabase->GetFileTypeFromExtension( file_ext );
184  delete mimeDatabase;
185  mimeDatabase = NULL;
186  }
187 
188  if( filetype )
189  {
190  wxFileType::MessageParameters params( fullfilename, type );
191 
192  success = filetype->GetOpenCommand( &command, params );
193  delete filetype;
194 
195  if( success )
196  success = ProcessExecute( command );
197  }
198 
199  if( !success )
200  {
201  msg.Printf( _( "Unknown MIME type for doc file \"%s\"" ), GetChars( fullfilename ) );
202  DisplayError( aParent, msg );
203  }
204 
205  return success;
206 }
void DisplayError(wxWindow *aParent, const wxString &aText, int aDisplayTime)
Display an error or warning message box with aMessage.
Definition: confirm.cpp:239
wxString FindValidPath(const wxString &aFileName) const
Definition: search_stack.h:73
int ProcessExecute(const wxString &aCommandLine, int aFlags, wxProcess *callback)
Run a command in a child process.
Definition: common.cpp:364
#define WIN_STRING_DIR_SEP
Definition: gestfich.h:44
#define UNIX_STRING_DIR_SEP
Definition: gestfich.h:43
SEARCH_STACK looks for files in a number of places.
Definition: search_stack.h:41
static wxMimeTypesManager * mimeDatabase
Definition: eda_doc.cpp:58
static const wxFileTypeInfo EDAfallbacks[]
Definition: eda_doc.cpp:59
#define NULL
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:52
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:587
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:153
#define _(s)
Definition: 3d_actions.cpp:33
bool OpenPDF(const wxString &file)
Function OpenPDF run the PDF viewer and display a PDF file.
Definition: gestfich.cpp:205

References _, DisplayError(), EDA_FILE_SELECTOR(), EDAfallbacks, SEARCH_STACK::FindValidPath(), GetChars(), mimeDatabase, NULL, OpenPDF(), ProcessExecute(), 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().

◆ 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 209 of file eda_doc.cpp.

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