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, SEARCH_STACK* aPaths )
75 {
76  wxString docname;
77  wxString fullfilename;
78  wxString msg;
79  wxString command;
80  bool success = false;
81 
82  // Is an internet url
83  static const std::vector<wxString> url_header =
84  {
85  wxT( "http:" ),
86  wxT( "https:" ),
87  wxT( "ftp:" ),
88  wxT( "www." ),
89  wxT( "file:" )
90  };
91 
92  // Replace before resolving as we might have a URL in a variable
93  docname = ResolveUriByEnvVars( aDocName, aProject );
94 
95  for( const wxString& proc : url_header)
96  {
97  if( docname.StartsWith( proc ) ) // looks like an internet url
98  {
99  wxURI uri( docname );
100  wxLaunchDefaultBrowser( uri.BuildURI() );
101  return true;
102  }
103  }
104 
105 #ifdef __WINDOWS__
106  docname.Replace( UNIX_STRING_DIR_SEP, WIN_STRING_DIR_SEP );
107 #else
108  docname.Replace( WIN_STRING_DIR_SEP, UNIX_STRING_DIR_SEP );
109 #endif
110 
111  /* Compute the full file name */
112  if( wxIsAbsolutePath( docname ) || aPaths == nullptr )
113  fullfilename = docname;
114  /* If the file exists, this is a trivial case: return the filename "as this". the name can
115  * be an absolute path, or a relative path like ./filename or ../<filename>.
116  */
117  else if( wxFileName::FileExists( docname ) )
118  fullfilename = docname;
119  else
120  fullfilename = aPaths->FindValidPath( docname );
121 
122  wxString extension;
123 
124 #ifdef __WINDOWS__
125  extension = wxT( ".*" );
126 #endif
127 
128  if( wxIsWild( fullfilename ) )
129  {
130  fullfilename = wxFileSelector( _( "Documentation File" ), wxPathOnly( fullfilename ),
131  fullfilename, extension, wxFileSelectorDefaultWildcardStr,
132  wxFD_OPEN, aParent );
133 
134  if( fullfilename.IsEmpty() )
135  return false;
136  }
137 
138  if( !wxFileExists( fullfilename ) )
139  {
140  msg.Printf( _( "Documentation file '%s' not found." ), docname );
141  DisplayError( aParent, msg );
142  return false;
143  }
144 
145  wxFileName currentFileName( fullfilename );
146 
147  wxString file_ext = currentFileName.GetExt();
148 
149  if( file_ext.Lower() == wxT( "pdf" ) )
150  {
151  success = OpenPDF( fullfilename );
152  return success;
153  }
154 
155  /* Try to launch some browser (useful under linux) */
156  wxFileType* filetype;
157 
158  wxString type;
159  filetype = wxTheMimeTypesManager->GetFileTypeFromExtension( file_ext );
160 
161  if( !filetype ) // 2nd attempt.
162  {
163  mimeDatabase = new wxMimeTypesManager;
164  mimeDatabase->AddFallbacks( EDAfallbacks );
165  filetype = mimeDatabase->GetFileTypeFromExtension( file_ext );
166  delete mimeDatabase;
167  mimeDatabase = nullptr;
168  }
169 
170  if( filetype )
171  {
172  wxFileType::MessageParameters params( fullfilename, type );
173 
174  success = filetype->GetOpenCommand( &command, params );
175  delete filetype;
176 
177  if( success )
178  success = wxExecute( command );
179  }
180 
181  if( !success )
182  {
183  msg.Printf( _( "Unknown MIME type for documentation file '%s'" ), fullfilename );
184  DisplayError( aParent, msg );
185  }
186 
187  return success;
188 }
virtual COMMON_SETTINGS * GetCommonSettings() const
Definition: pgm_base.cpp:385
void DisplayError(wxWindow *aParent, const wxString &aText, int aDisplayTime)
Display an error or warning message box with aMessage.
Definition: confirm.cpp:279
#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
bool GetAssociatedDocument(wxWindow *aParent, const wxString &aDocName, PROJECT *aProject, SEARCH_STACK *aPaths)
Open a document (file) with the suitable browser.
Definition: eda_doc.cpp:74
static wxMimeTypesManager * mimeDatabase
Definition: eda_doc.cpp:52
bool m_use_system_pdf_browser
Definition: pgm_base.h:322
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:159