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