KiCad PCB EDA Suite
Loading...
Searching...
No Matches
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)
52static wxMimeTypesManager* mimeDatabase;
53static 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
74bool 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 // Use wxWidgets to resolve any "." and ".." in the path
148 fullfilename = currentFileName.GetAbsolutePath();
149
150 wxString file_ext = currentFileName.GetExt();
151
152 if( file_ext.Lower() == wxT( "pdf" ) )
153 {
154 success = OpenPDF( fullfilename );
155 return success;
156 }
157
158 /* Try to launch some browser (useful under linux) */
159 wxFileType* filetype;
160
161 wxString type;
162 filetype = wxTheMimeTypesManager->GetFileTypeFromExtension( file_ext );
163
164 if( !filetype ) // 2nd attempt.
165 {
166 mimeDatabase = new wxMimeTypesManager;
167 mimeDatabase->AddFallbacks( EDAfallbacks );
168 filetype = mimeDatabase->GetFileTypeFromExtension( file_ext );
169 delete mimeDatabase;
170 mimeDatabase = nullptr;
171 }
172
173 if( filetype )
174 {
175 wxFileType::MessageParameters params( fullfilename, type );
176
177 success = filetype->GetOpenCommand( &command, params );
178 delete filetype;
179
180 if( success )
181 success = wxExecute( command );
182 }
183
184 if( !success )
185 {
186 msg.Printf( _( "Unknown MIME type for documentation file '%s'" ), fullfilename );
187 DisplayError( aParent, msg );
188 }
189
190 return success;
191}
virtual COMMON_SETTINGS * GetCommonSettings() const
Definition: pgm_base.cpp:621
virtual void SetPdfBrowserName(const wxString &aFileName)
Definition: pgm_base.h:173
virtual void ReadPdfBrowserInfos()
Read the PDF browser choice from the common configuration.
Definition: eda_doc.cpp:37
bool m_use_system_pdf_browser
Definition: pgm_base.h:381
virtual const wxString & GetPdfBrowserName() const
Definition: pgm_base.h:171
virtual void WritePdfBrowserInfos()
Save the PDF browser choice to the common configuration.
Definition: eda_doc.cpp:44
Container for project specific data.
Definition: project.h:64
Look for files in a number of paths.
Definition: search_stack.h:42
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:312
The common library.
void DisplayError(wxWindow *aParent, const wxString &aText, int aDisplayTime)
Display an error or warning message box with aMessage.
Definition: confirm.cpp:283
This file is part of the common library.
#define _(s)
static const wxFileTypeInfo EDAfallbacks[]
Definition: eda_doc.cpp:53
static wxMimeTypesManager * mimeDatabase
Definition: eda_doc.cpp:52
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
bool OpenPDF(const wxString &file)
Run the PDF viewer and display a PDF file.
Definition: gestfich.cpp:161
#define WIN_STRING_DIR_SEP
Definition: gestfich.h:36
#define UNIX_STRING_DIR_SEP
Definition: gestfich.h:35
see class PGM_BASE