KiCad PCB EDA Suite
eda_doc.cpp File Reference
#include <fctsys.h>
#include <pgm_base.h>
#include <common.h>
#include <confirm.h>
#include <gestfich.h>
#include <wx/mimetype.h>
#include <wx/tokenzr.h>
#include <wx/filename.h>
#include <macros.h>

Go to the source code of this file.

Functions

bool GetAssociatedDocument (wxWindow *aParent, const wxString &aDocName, const wxPathList *aPaths)
 Function GetAssociatedDocument open a document (file) with the suitable browser. More...
 
int KeyWordOk (const wxString &KeyList, const wxString &Database)
 Function KeyWordOk searches aKeyList for any words found in aDatabase. More...
 

Variables

static wxMimeTypesManager * mimeDatabase
 
static const wxFileTypeInfo EDAfallbacks []
 

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 86 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(), and PCB_EDIT_FRAME::Process_Special_Functions().

90 {
91  wxString docname, fullfilename;
92  wxString msg;
93  wxString command;
94  bool success = false;
95 
96  // Is an internet url
97  static const wxChar* url_header[] = {
98  wxT( "http:" ),
99  wxT( "https:" ),
100  wxT( "ftp:" ),
101  wxT( "www." )
102  };
103 
104  for( unsigned ii = 0; ii < DIM(url_header); ii++ )
105  {
106  if( aDocName.First( url_header[ii] ) == 0 ) // looks like an internet url
107  {
108  wxLaunchDefaultBrowser( aDocName );
109  return true;
110  }
111  }
112 
113  docname = aDocName;
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( aDocName ) || aPaths == NULL)
124  fullfilename = aDocName;
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( aDocName ) )
130  fullfilename = aDocName;
131  else
132  {
133  fullfilename = aPaths->FindValidPath( aDocName );
134  }
135 
136  wxString mask( wxT( "*" ) ), extension;
137 
138 #ifdef __WINDOWS__
139  mask += wxT( ".*" );
140  extension = wxT( ".*" );
141 #endif
142 
143  if( wxIsWild( fullfilename ) )
144  {
145  fullfilename = EDA_FILE_SELECTOR( _( "Doc Files" ),
146  wxPathOnly( fullfilename ),
147  fullfilename,
148  extension,
149  mask,
150  aParent,
151  wxFD_OPEN,
152  true,
153  wxPoint( -1, -1 ) );
154  if( fullfilename.IsEmpty() )
155  return false;
156  }
157 
158  if( !wxFileExists( fullfilename ) )
159  {
160  msg.Printf( _( "Doc File '%s' not found" ), GetChars( aDocName ) );
161  DisplayError( aParent, msg );
162  return false;
163  }
164 
165  wxFileName currentFileName( fullfilename );
166 
167  wxString file_ext = currentFileName.GetExt();
168 
169  if( file_ext.Lower() == wxT( "pdf" ) )
170  {
171  success = OpenPDF( fullfilename );
172  return success;
173  }
174 
175  /* Try to launch some browser (useful under linux) */
176  wxFileType* filetype;
177 
178  wxString type;
179  filetype = wxTheMimeTypesManager->GetFileTypeFromExtension( file_ext );
180 
181  if( !filetype ) // 2nd attempt.
182  {
183  mimeDatabase = new wxMimeTypesManager;
184  mimeDatabase->AddFallbacks( EDAfallbacks );
185  filetype = mimeDatabase->GetFileTypeFromExtension( file_ext );
186  delete mimeDatabase;
187  mimeDatabase = NULL;
188  }
189 
190  if( filetype )
191  {
192  wxFileType::MessageParameters params( fullfilename, type );
193 
194  success = filetype->GetOpenCommand( &command, params );
195  delete filetype;
196 
197  if( success )
198  success = ProcessExecute( command );
199  }
200 
201  if( !success )
202  {
203  msg.Printf( _( "Unknown MIME type for doc file <%s>" ), GetChars( fullfilename ) );
204  DisplayError( aParent, msg );
205  }
206 
207  return success;
208 }
#define DIM(x)
of elements in an array
Definition: macros.h:98
int ProcessExecute(const wxString &aCommandLine, int aFlags, wxProcess *callback)
Function ProcessExecute runs a child process.
Definition: common.cpp:160
#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:64
static const wxFileTypeInfo EDAfallbacks[]
Definition: eda_doc.cpp:65
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:69
bool OpenPDF(const wxString &file)
Function OpenPDF run the PDF viewer and display a PDF file.
Definition: gestfich.cpp:345
int KeyWordOk ( const wxString &  aKeyList,
const wxString &  aDatabase 
)

Function KeyWordOk searches aKeyList for any words found in aDatabase.

Returns
0 if no keyword is found or 1 if keyword is found.

Definition at line 211 of file eda_doc.cpp.

Referenced by PCB_BASE_FRAME::SelectFootprint().

212 {
213  wxString KeysCopy, DataList;
214 
215  if( KeyList.IsEmpty() )
216  return 0;
217 
218  KeysCopy = KeyList; KeysCopy.MakeUpper();
219  DataList = Database; DataList.MakeUpper();
220 
221  wxStringTokenizer Token( KeysCopy, wxT( " \n\r" ) );
222 
223  while( Token.HasMoreTokens() )
224  {
225  wxString Key = Token.GetNextToken();
226 
227  // Search Key in Datalist:
228  wxStringTokenizer Data( DataList, wxT( " \n\r" ) );
229 
230  while( Data.HasMoreTokens() )
231  {
232  wxString word = Data.GetNextToken();
233 
234  if( word == Key )
235  return 1; // Key found !
236  }
237  }
238 
239  // keyword not found
240  return 0;
241 }

Variable Documentation

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

Referenced by GetAssociatedDocument().

wxMimeTypesManager* mimeDatabase
static

Definition at line 64 of file eda_doc.cpp.

Referenced by GetAssociatedDocument().