28#include <wx/txtstrm.h>
29#include <wx/wfstream.h>
36#define SEP wxFileName::GetPathSeparator()
49 if( !wxFileName::DirExists(
m_basePath.GetPath() ) )
52 m_title =
_(
"Could open the template path!" ) + wxS(
" " ) + aPath;
54 else if( !wxFileName::DirExists(
m_metaPath.GetPath() ) )
57 m_title =
_(
"Couldn't open the meta information directory for this template!" ) + wxS(
" " ) +
63 m_title =
_(
"Couldn't find the meta HTML information file for this template!" );
82 virtual wxDirTraverseResult
OnFile(
const wxString& filename )
override
85 m_files.emplace_back( wxFileName( filename ) );
87 return wxDIR_CONTINUE;
90 virtual wxDirTraverseResult
OnDir(
const wxString& dirname )
override
93 m_files.emplace_back( wxFileName::DirName( dirname ) );
95 return wxDIR_CONTINUE;
106 std::vector<wxFileName> files;
110 dir.Traverse( sink );
140 std::vector< wxFileName >& aDestFiles )
142 std::vector< wxFileName > srcFiles =
GetFileList();
146 bool multipleProjectFilesFound =
false;
148 for( wxFileName& file : srcFiles )
152 if( !basename.IsEmpty() && basename != file.GetName() )
153 multipleProjectFilesFound =
true;
155 basename = file.GetName();
159 if( multipleProjectFilesFound )
162 for( wxFileName& srcFile : srcFiles )
165 wxFileName destFile = srcFile;
168 wxString
name = destFile.GetName();
169 name.Replace( basename, aNewProjectPath.GetName() );
170 destFile.SetName(
name );
173 wxString
path = destFile.GetPathWithSep();
174 path.Replace(
m_basePath.GetPathWithSep(), aNewProjectPath.GetPathWithSep() );
175 destFile.SetPath(
path );
177 aDestFiles.push_back( destFile );
180 return aDestFiles.size();
195 bool multipleProjectFilesFound =
false;
197 for( wxFileName& file : srcFiles )
201 if( !basename.IsEmpty() && basename != file.GetName() )
202 multipleProjectFilesFound =
true;
204 basename = file.GetName();
208 if( multipleProjectFilesFound )
211 for( wxFileName& srcFile : srcFiles )
214 wxFileName destFile = srcFile;
217 wxString currname = destFile.GetName();
223 else if( destFile.GetName().EndsWith(
"-cache" )
224 || destFile.GetName().EndsWith(
"-rescue" ) )
226 currname.Replace( basename, aNewProjectPath.GetName() );
228 else if( destFile.GetExt() ==
"dcm"
229 || destFile.GetExt() ==
"lib"
231 || destFile.GetPath().EndsWith(
".pretty" ) )
238 currname.Replace( basename, aNewProjectPath.GetName() );
241 destFile.SetName( currname );
245 wxString destpath = destFile.GetPathWithSep();
246 destpath.Replace(
m_basePath.GetPathWithSep(), aNewProjectPath.GetPathWithSep() );
249 if( !wxFileName::DirExists( destpath ) )
251 if( !wxFileName::Mkdir( destpath, 0777, wxPATH_MKDIR_FULL ) )
255 if( !aErrorMsg->empty() )
260 msg.Printf(
_(
"Cannot create folder '%s'." ), destpath );
268 destFile.SetPath( destpath );
270 if( srcFile.FileExists() && !wxCopyFile( srcFile.GetFullPath(), destFile.GetFullPath() ) )
274 if( !aErrorMsg->empty() )
279 msg.Printf(
_(
"Cannot copy file '%s'." ), destFile.GetFullPath() );
293 wxFFileInputStream input(
GetHtmlFile().GetFullPath() );
294 wxString separator( wxT(
"\x9" ) );
295 wxTextInputStream
text( input, separator, wxConvUTF8 );
304 while( input.IsOk() && !input.Eof() && !done )
306 wxString line =
text.ReadLine();
307 wxString upperline = line.Clone().Upper();
309 start = upperline.Find( wxT(
"<TITLE>" ) );
310 finish = upperline.Find( wxT(
"</TITLE>" ) );
311 int length = finish - start - 7;
314 if( start != wxNOT_FOUND )
316 if( finish != wxNOT_FOUND )
318 m_title = line( start + 7, length );
322 m_title = line.Mid( start + 7 );
329 if( finish != wxNOT_FOUND )
331 m_title += line.SubString( 0, finish );
337 m_title.Replace( wxT(
"\r" ), wxT(
" " ) );
338 m_title.Replace( wxT(
"\n" ), wxT(
" " ) );
virtual wxDirTraverseResult OnDir(const wxString &dirname) override
FILE_TRAVERSER(std::vector< wxFileName > &files, const wxString exclude)
virtual wxDirTraverseResult OnFile(const wxString &filename) override
std::vector< wxFileName > & m_files
wxBitmap * GetIcon()
Get the 64px^2 icon for the project template.
size_t GetDestinationFiles(const wxFileName &aNewProjectPath, std::vector< wxFileName > &aDestFiles)
Fetch the list of destination files to be copied when the new project is created.
wxFileName m_metaHtmlFile
PROJECT_TEMPLATE(const wxString &aPath)
Create a new project instance from aPath.
std::vector< wxFileName > GetFileList()
Get a vector list of filenames for the template.
~PROJECT_TEMPLATE()
Non-virtual destructor (so no derived classes)
wxFileName GetHtmlFile()
Get the full Html filename for the project template.
wxString * GetTitle()
Get the title of the project (extracted from the html title tag)
bool CreateProject(wxFileName &aNewProjectPath, wxString *aErrorMsg=nullptr)
Copies and renames all template files to create a new project.
wxFileName m_metaIconFile
wxString GetPrjDirName()
Get the dir name of the project template (i.e.
const std::string LegacyProjectFileExtension
const std::string ProjectFileExtension
const std::string DrawingSheetFileExtension
#define METAFILE_ICON
An optional png icon, exactly 64px x 64px which is used in the template selector if present.
#define METADIR
A directory which contains information about the project template and does not get copied.
#define METAFILE_INFO_HTML
A required html formatted file which contains information about the project template.
Definition of file extensions used in Kicad.