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

bool GetAssociatedDocument ( wxWindow *  aParent,
const wxString &  aDocName,
const wxPathList *  aPaths = NULL 
)

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

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.

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

Referenced by EDA_BASE_FRAME::GetKicadHelp(), LIB_EDIT_FRAME::OnViewEntryDoc(), LIB_VIEW_FRAME::onViewSymbolDocument(), SCH_EDIT_FRAME::Process_Special_Functions(), PCB_EDIT_FRAME::Process_Special_Functions(), DIALOG_EDIT_LIBENTRY_FIELDS_IN_LIB::showButtonHandler(), and DIALOG_EDIT_COMPONENT_IN_SCHEMATIC::showButtonHandler().

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  for( unsigned ii = 0; ii < DIM(url_header); ii++ )
107  {
108  if( aDocName.First( url_header[ii] ) == 0 ) // looks like an internet url
109  {
110  wxURI uri( aDocName );
111  wxLaunchDefaultBrowser( uri.BuildURI() );
112  return true;
113  }
114  }
115 
116  docname = aDocName;
117 
118 #ifdef __WINDOWS__
119  docname.Replace( UNIX_STRING_DIR_SEP, WIN_STRING_DIR_SEP );
120 #else
121  docname.Replace( WIN_STRING_DIR_SEP, UNIX_STRING_DIR_SEP );
122 #endif
123 
124 
125  /* Compute the full file name */
126  if( wxIsAbsolutePath( aDocName ) || aPaths == NULL)
127  fullfilename = aDocName;
128  /* If the file exists, this is a trivial case: return the filename
129  * "as this". the name can be an absolute path, or a relative path
130  * like ./filename or ../<filename>
131  */
132  else if( wxFileName::FileExists( aDocName ) )
133  fullfilename = aDocName;
134  else
135  {
136  fullfilename = aPaths->FindValidPath( aDocName );
137  }
138 
139  wxString mask( wxT( "*" ) ), extension;
140 
141 #ifdef __WINDOWS__
142  mask += wxT( ".*" );
143  extension = wxT( ".*" );
144 #endif
145 
146  if( wxIsWild( fullfilename ) )
147  {
148  fullfilename = EDA_FILE_SELECTOR( _( "Doc Files" ),
149  wxPathOnly( fullfilename ),
150  fullfilename,
151  extension,
152  mask,
153  aParent,
154  wxFD_OPEN,
155  true,
156  wxPoint( -1, -1 ) );
157  if( fullfilename.IsEmpty() )
158  return false;
159  }
160 
161  if( !wxFileExists( fullfilename ) )
162  {
163  msg.Printf( _( "Doc File \"%s\" not found" ), GetChars( aDocName ) );
164  DisplayError( aParent, msg );
165  return false;
166  }
167 
168  wxFileName currentFileName( fullfilename );
169 
170  wxString file_ext = currentFileName.GetExt();
171 
172  if( file_ext.Lower() == wxT( "pdf" ) )
173  {
174  success = OpenPDF( fullfilename );
175  return success;
176  }
177 
178  /* Try to launch some browser (useful under linux) */
179  wxFileType* filetype;
180 
181  wxString type;
182  filetype = wxTheMimeTypesManager->GetFileTypeFromExtension( file_ext );
183 
184  if( !filetype ) // 2nd attempt.
185  {
186  mimeDatabase = new wxMimeTypesManager;
187  mimeDatabase->AddFallbacks( EDAfallbacks );
188  filetype = mimeDatabase->GetFileTypeFromExtension( file_ext );
189  delete mimeDatabase;
190  mimeDatabase = NULL;
191  }
192 
193  if( filetype )
194  {
195  wxFileType::MessageParameters params( fullfilename, type );
196 
197  success = filetype->GetOpenCommand( &command, params );
198  delete filetype;
199 
200  if( success )
201  success = ProcessExecute( command );
202  }
203 
204  if( !success )
205  {
206  msg.Printf( _( "Unknown MIME type for doc file \"%s\"" ), GetChars( fullfilename ) );
207  DisplayError( aParent, msg );
208  }
209 
210  return success;
211 }
#define DIM(x)
of elements in an array
Definition: macros.h:98
int ProcessExecute(const wxString &aCommandLine, int aFlags, wxProcess *callback)
Run a command in a child process.
Definition: common.cpp:154
#define WIN_STRING_DIR_SEP
Definition: gestfich.h:44
#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
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:185
bool OpenPDF(const wxString &file)
Function OpenPDF run the PDF viewer and display a PDF file.
Definition: gestfich.cpp:345
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 214 of file eda_doc.cpp.

Referenced by PCB_BASE_FRAME::SelectFootprint().

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