KiCad PCB EDA Suite
eda_doc.cpp
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2014 Jean-Pierre Charras, jp.charras at wanadoo.fr
5  * Copyright (C) 2014-2017 KiCad Developers, see AUTHORS.txt for contributors.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, you may find one here:
19  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20  * or you may search the http://www.gnu.org website for the version 2 license,
21  * or you may write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24 
29 #include <fctsys.h>
30 #include <pgm_base.h>
31 #include <common.h>
32 #include <confirm.h>
33 #include <gestfich.h>
34 
35 #include <wx/mimetype.h>
36 #include <wx/tokenzr.h>
37 #include <wx/filename.h>
38 #include <macros.h>
39 
40 
42 {
43  wxASSERT( m_common_settings );
44 
45  wxString browser = m_common_settings->Read( wxT( "PdfBrowserName" ), wxEmptyString );
46  SetPdfBrowserName( browser );
47 
48  int tmp;
49  m_common_settings->Read( wxT( "UseSystemBrowser" ), &tmp, 0 );
50  m_use_system_pdf_browser = bool( tmp );
51 }
52 
53 
55 {
56  wxASSERT( m_common_settings );
57 
58  m_common_settings->Write( wxT( "PdfBrowserName" ), GetPdfBrowserName() );
59  m_common_settings->Write( wxT( "UseSystemBrowser" ), m_use_system_pdf_browser );
60 }
61 
62 
63 // Mime type extensions (PDF files are not considered here)
64 static wxMimeTypesManager* mimeDatabase;
65 static const wxFileTypeInfo EDAfallbacks[] =
66 {
67  wxFileTypeInfo( wxT( "text/html" ),
68  wxT( "wxhtml %s" ),
69  wxT( "wxhtml %s" ),
70  wxT( "html document (from KiCad)" ),
71  wxT( "htm" ),
72  wxT( "html" ),wxNullPtr ),
73 
74  wxFileTypeInfo( wxT( "application/sch" ),
75  wxT( "eeschema %s" ),
76  wxT( "eeschema -p %s" ),
77  wxT( "sch document (from KiCad)" ),
78  wxT( "sch" ),
79  wxT( "SCH" ), wxNullPtr ),
80 
81  // must terminate the table with this!
82  wxFileTypeInfo()
83 };
84 
85 
86 bool GetAssociatedDocument( wxWindow* aParent,
87  const wxString& aDocName,
88  const wxPathList* aPaths)
89 
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 }
209 
210 
211 int KeyWordOk( const wxString& KeyList, const wxString& Database )
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 }
#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
This file is part of the common library TODO brief description.
This file is part of the common library.
#define UNIX_STRING_DIR_SEP
Definition: gestfich.h:43
VTBL_ENTRY const wxString & GetPdfBrowserName() const
Definition: pgm_base.h:177
bool GetAssociatedDocument(wxWindow *aParent, const wxString &aDocName, const wxPathList *aPaths)
Function GetAssociatedDocument open a document (file) with the suitable browser.
Definition: eda_doc.cpp:86
VTBL_ENTRY void WritePdfBrowserInfos()
Function WritePdfBrowserInfos saves the PDF browser choice to the common configuration.
Definition: eda_doc.cpp:54
static wxMimeTypesManager * mimeDatabase
Definition: eda_doc.cpp:64
bool m_use_system_pdf_browser
true to use the selected PDF browser, if exists, or false to use the default
Definition: pgm_base.h:359
This file contains miscellaneous commonly used macros and functions.
static const wxFileTypeInfo EDAfallbacks[]
Definition: eda_doc.cpp:65
int KeyWordOk(const wxString &KeyList, const wxString &Database)
Function KeyWordOk searches aKeyList for any words found in aDatabase.
Definition: eda_doc.cpp:211
VTBL_ENTRY void SetPdfBrowserName(const wxString &aFileName)
Definition: pgm_base.h:179
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
wxConfigBase * m_common_settings
Configuration settings common to all KiCad program modules, like as in $HOME/.kicad_common.
Definition: pgm_base.h:344
VTBL_ENTRY void ReadPdfBrowserInfos()
Function ReadPdfBrowserInfos reads the PDF browser choice from the common configuration.
Definition: eda_doc.cpp:41
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
see class PGM_BASE
The common library.
void DisplayError(wxWindow *parent, const wxString &text, int displaytime)
Function DisplayError displays an error or warning message box with aMessage.
Definition: confirm.cpp:71
bool OpenPDF(const wxString &file)
Function OpenPDF run the PDF viewer and display a PDF file.
Definition: gestfich.cpp:345