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-2021 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 
25 #include <pgm_base.h>
26 #include <common.h>
27 #include <confirm.h>
28 #include <gestfich.h>
30 
31 #include <wx/mimetype.h>
32 #include <wx/filename.h>
33 #include <wx/uri.h>
34 #include <wx/filedlg.h>
35 
36 
38 {
39  SetPdfBrowserName( GetCommonSettings()->m_System.pdf_viewer_name );
41 }
42 
43 
45 {
48 }
49 
50 
51 // Mime type extensions (PDF files are not considered here)
52 static wxMimeTypesManager* mimeDatabase;
53 static const wxFileTypeInfo EDAfallbacks[] =
54 {
55  wxFileTypeInfo( wxT( "text/html" ),
56  wxT( "wxhtml %s" ),
57  wxT( "wxhtml %s" ),
58  wxT( "html document (from KiCad)" ),
59  wxT( "htm" ),
60  wxT( "html" ),wxNullPtr ),
61 
62  wxFileTypeInfo( wxT( "application/sch" ),
63  wxT( "eeschema %s" ),
64  wxT( "eeschema -p %s" ),
65  wxT( "sch document (from KiCad)" ),
66  wxT( "sch" ),
67  wxT( "SCH" ), wxNullPtr ),
68 
69  // must terminate the table with this!
70  wxFileTypeInfo()
71 };
72 
73 
74 bool GetAssociatedDocument( wxWindow* aParent, const wxString& aDocName, PROJECT* aProject )
75 {
76  SEARCH_STACK* aPaths = nullptr;
77  wxString docname;
78  wxString fullfilename;
79  wxString msg;
80  wxString command;
81  bool success = false;
82 
83 #if defined( EESCHEMA )
84  SEARCH_STACK* aPaths = aProject ? aProject->SchSearchS() : nullptr;
85 #endif
86 
87  // Is an internet url
88  static const std::vector<wxString> url_header =
89  {
90  wxT( "http:" ),
91  wxT( "https:" ),
92  wxT( "ftp:" ),
93  wxT( "www." ),
94  wxT( "file:" )
95  };
96 
97  // Replace before resolving as we might have a URL in a variable
98  docname = ResolveUriByEnvVars( aDocName, aProject );
99 
100  for( const wxString& proc : url_header)
101  {
102  if( docname.StartsWith( proc ) ) // looks like an internet url
103  {
104  wxURI uri( docname );
105  wxLaunchDefaultBrowser( uri.BuildURI() );
106  return true;
107  }
108  }
109 
110 #ifdef __WINDOWS__
111  docname.Replace( UNIX_STRING_DIR_SEP, WIN_STRING_DIR_SEP );
112 #else
113  docname.Replace( WIN_STRING_DIR_SEP, UNIX_STRING_DIR_SEP );
114 #endif
115 
116  /* Compute the full file name */
117  if( wxIsAbsolutePath( docname ) || aPaths == nullptr )
118  fullfilename = docname;
119  /* If the file exists, this is a trivial case: return the filename "as this". the name can
120  * be an absolute path, or a relative path like ./filename or ../<filename>.
121  */
122  else if( wxFileName::FileExists( docname ) )
123  fullfilename = docname;
124  else
125  fullfilename = aPaths->FindValidPath( docname );
126 
127  wxString extension;
128 
129 #ifdef __WINDOWS__
130  extension = wxT( ".*" );
131 #endif
132 
133  if( wxIsWild( fullfilename ) )
134  {
135  fullfilename = wxFileSelector( _( "Documentation File" ), wxPathOnly( fullfilename ),
136  fullfilename, extension, wxFileSelectorDefaultWildcardStr,
137  wxFD_OPEN, aParent );
138 
139  if( fullfilename.IsEmpty() )
140  return false;
141  }
142 
143  if( !wxFileExists( fullfilename ) )
144  {
145  msg.Printf( _( "Documentation file '%s' not found." ), docname );
146  DisplayError( aParent, msg );
147  return false;
148  }
149 
150  wxFileName currentFileName( fullfilename );
151 
152  wxString file_ext = currentFileName.GetExt();
153 
154  if( file_ext.Lower() == wxT( "pdf" ) )
155  {
156  success = OpenPDF( fullfilename );
157  return success;
158  }
159 
160  /* Try to launch some browser (useful under linux) */
161  wxFileType* filetype;
162 
163  wxString type;
164  filetype = wxTheMimeTypesManager->GetFileTypeFromExtension( file_ext );
165 
166  if( !filetype ) // 2nd attempt.
167  {
168  mimeDatabase = new wxMimeTypesManager;
169  mimeDatabase->AddFallbacks( EDAfallbacks );
170  filetype = mimeDatabase->GetFileTypeFromExtension( file_ext );
171  delete mimeDatabase;
172  mimeDatabase = nullptr;
173  }
174 
175  if( filetype )
176  {
177  wxFileType::MessageParameters params( fullfilename, type );
178 
179  success = filetype->GetOpenCommand( &command, params );
180  delete filetype;
181 
182  if( success )
183  success = wxExecute( command );
184  }
185 
186  if( !success )
187  {
188  msg.Printf( _( "Unknown MIME type for documentation file '%s'" ), fullfilename );
189  DisplayError( aParent, msg );
190  }
191 
192  return success;
193 }
virtual COMMON_SETTINGS * GetCommonSettings() const
Definition: pgm_base.cpp:381
void DisplayError(wxWindow *aParent, const wxString &aText, int aDisplayTime)
Display an error or warning message box with aMessage.
Definition: confirm.cpp:279
bool GetAssociatedDocument(wxWindow *aParent, const wxString &aDocName, PROJECT *aProject)
Open a document (file) with the suitable browser.
Definition: eda_doc.cpp:74
wxString FindValidPath(const wxString &aFileName) const
Definition: search_stack.h:70
#define WIN_STRING_DIR_SEP
Definition: gestfich.h:36
Container for project specific data.
Definition: project.h:62
This file is part of the common library.
#define UNIX_STRING_DIR_SEP
Definition: gestfich.h:35
Look for files in a number of paths.
Definition: search_stack.h:41
virtual void WritePdfBrowserInfos()
Save the PDF browser choice to the common configuration.
Definition: eda_doc.cpp:44
static wxMimeTypesManager * mimeDatabase
Definition: eda_doc.cpp:52
bool m_use_system_pdf_browser
Definition: pgm_base.h:320
static const wxFileTypeInfo EDAfallbacks[]
Definition: eda_doc.cpp:53
virtual void SetPdfBrowserName(const wxString &aFileName)
Definition: pgm_base.h:166
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:280
#define _(s)
virtual const wxString & GetPdfBrowserName() const
Definition: pgm_base.h:164
virtual void ReadPdfBrowserInfos()
Read the PDF browser choice from the common configuration.
Definition: eda_doc.cpp:37
see class PGM_BASE
The common library.
bool OpenPDF(const wxString &file)
Run the PDF viewer and display a PDF file.
Definition: gestfich.cpp:158