KiCad PCB EDA Suite
project_template.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) 2012 Brian Sidebotham <brian.sidebotham@gmail.com>
5  * Copyright (C) 1992-2012 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 
26 #include <wx/bitmap.h>
27 #include <wx/dir.h>
28 #include <wx/txtstrm.h>
29 #include <wx/wfstream.h>
30 
31 #include "project_template.h"
32 
33 
34 #define SEP() wxFileName::GetPathSeparator()
35 
36 
37 PROJECT_TEMPLATE::PROJECT_TEMPLATE( const wxString& aPath )
38 {
39  templateBasePath = wxFileName::DirName( aPath );
40  templateMetaPath = wxFileName::DirName( aPath + SEP() + METADIR );
41  templateMetaHtmlFile = wxFileName::FileName( aPath + SEP() + METADIR + SEP() +
43  templateMetaIconFile = wxFileName::FileName( aPath + SEP() + METADIR + SEP() + METAFILE_ICON );
44 
45  title = wxEmptyString;
46 
47  // Test the project template requirements to make sure aPath is a valid
48  // template structure
49  if( !wxFileName::DirExists( templateBasePath.GetPath() ) )
50  {
51  // Error, the path doesn't exist!
52  title = wxT( "Could open the template path! " + aPath );
53  }
54  else if( !wxFileName::DirExists( templateMetaPath.GetPath() ) )
55  {
56  // Error, the meta information directory doesn't exist!
57  title = wxT( "Couldn't open the meta information directory for this template! " +
58  templateMetaPath.GetPath() );
59  }
60  else if( !wxFileName::FileExists( templateMetaHtmlFile.GetFullPath() ) )
61  {
62  // Error, the meta information directory doesn't contain the informational html file!
63  title = wxT( "Cound't find the meta html information file for this template!" );
64  }
65 
66  // Try to load an icon
67  metaIcon = new wxBitmap( templateMetaIconFile.GetFullPath(), wxBITMAP_TYPE_PNG );
68 }
69 
70 std::vector<wxFileName> PROJECT_TEMPLATE::GetFileList()
71 {
72  std::vector<wxFileName> files;
73  wxString f = templateBasePath.GetPath();
74  wxArrayString allfiles;
75  wxFileName p;
76 
77  wxDir::GetAllFiles( f, &allfiles );
78 
79  // Create the vector and ignore all of the meta data files!
80  for( size_t i=0; i < allfiles.size(); i++ )
81  {
82  p = allfiles[i];
83 
84  // Files that are in the meta directory must not be included
85  if( !p.GetPath().StartsWith( templateMetaPath.GetPath() ) )
86  files.push_back(allfiles[i]);
87  }
88 
89  return files;
90 }
91 
92 
94 {
95  return templateBasePath.GetDirs()[ templateBasePath.GetDirCount()-1 ];
96 }
97 
98 
100 {
101 
102 }
103 
104 
106 {
107  return templateMetaHtmlFile;
108 }
109 
110 
112 {
113  return metaIcon;
114 }
115 
116 
117 bool PROJECT_TEMPLATE::CreateProject( wxFileName& aNewProjectPath )
118 {
119  // CreateProject copy the files from template to the new project folder
120  // and rename files which have the same name as the template .pro file
121  bool result = true;
122 
123  std::vector<wxFileName> srcFiles = GetFileList();
124 
125  // Find the template file name base. this is the name of the .pro templte file
126  wxString basename;
127  for( size_t i=0; i < srcFiles.size(); i++ )
128  {
129  if( srcFiles[i].GetExt() == wxT( "pro" ) )
130  {
131  basename = srcFiles[i].GetName();
132  break;
133  }
134  }
135 
136  for( size_t i=0; i < srcFiles.size(); i++ )
137  {
138  // Replace the template path
139  wxFileName destination = srcFiles[i];
140 
141  // Replace the template filename with the project filename for the new project creation
142  wxString currname = destination.GetName();
143  currname.Replace( basename, aNewProjectPath.GetName() );
144  destination.SetName( currname );
145 
146  // Replace the template path with the project path for the new project creation
147  // but keep the sub directory name, if exists
148  wxString destpath = destination.GetPathWithSep();
149  destpath.Replace( templateBasePath.GetPathWithSep(), aNewProjectPath.GetPathWithSep() );
150 
151  // Check to see if the path already exists, if not attempt to create it here. Don't worry
152  // about error checking, if the path isn't created the file copy will fail anyway
153 
154  if( !wxFileName::DirExists( destpath ) )
155  wxFileName::Mkdir( destpath, 0777, wxPATH_MKDIR_FULL );
156 
157  destination.SetPath( destpath );
158 
159  wxString srcFile = srcFiles[i].GetFullPath();
160  wxString dstFile = destination.GetFullPath();
161 
162  if( !wxCopyFile( srcFile, dstFile ) )
163  {
164  result = false;
165  break;
166  }
167  }
168 
169  return result;
170 }
171 
172 
174 {
175  wxFileInputStream input( GetHtmlFile().GetFullPath() );
176  wxString separator( wxT( "\x9" ) );
177  wxTextInputStream text( input, separator, wxConvUTF8 );
178 
179  /* Open HTML file and get the text between the title tags */
180  if( title == wxEmptyString )
181  {
182  int start = 0;
183  int finish = 0;
184  bool done = false;
185 
186  while( input.IsOk() && !input.Eof() && !done )
187  {
188  wxString line = text.ReadLine();
189 
190  start = line.Find( wxT( "<title>" ) );
191  if( start == wxNOT_FOUND )
192  start = line.Find( wxT( "<TITLE>" ) );
193 
194  finish = line.Find( wxT( "</title>" ) );
195  if( finish == wxNOT_FOUND )
196  finish = line.Find( wxT( "</TITLE>" ) );
197 
198  // find the opening tag
199  if( start != wxNOT_FOUND )
200  {
201  if( finish != wxNOT_FOUND )
202  {
203  title = line.SubString( start + 7, finish );
204  }
205  else
206  {
207  title = line.SubString( start + 7, line.Len() - 1 );
208  done = true;
209  }
210  }
211  else
212  {
213  if( finish != wxNOT_FOUND )
214  {
215  title += line.SubString( 0, finish );
216  done = true;
217  }
218  else
219  {
220  title += line;
221  }
222  }
223 
224  // Remove line endings
225  title.Replace( wxT( "\r" ), wxT( "" ) );
226  title.Replace( wxT( "\n" ), wxT( "" ) );
227  }
228  }
229 
230  return &title;
231 }
std::vector< wxFileName > GetFileList()
Get a vector list of filenames for the template.
wxFileName templateMetaHtmlFile
#define METADIR
A directory which contains information about the project template and does not get copied...
wxFileName GetHtmlFile()
Get the full Html filename for the project template.
wxFileName templateBasePath
#define METAFILE_INFO_HTML
A required html formatted file which contains information about the project template.
#define SEP()
wxFileName templateMetaIconFile
PROJECT_TEMPLATE(const wxString &aPath)
Create a new project instance from aPath.
#define METAFILE_ICON
An optional png icon, exactly 64px x 64px which is used in the template selector if present...
wxString * GetTitle()
Get the title of the project (extracted from the html title tag)
wxString GetPrjDirName()
Get the dir name of the project template (i.e.
bool CreateProject(wxFileName &aNewProjectPath)
Copies and renames all template files to create a new project.
wxFileName templateMetaPath
~PROJECT_TEMPLATE()
Non-virtual destructor (so no dervied classes)
wxBitmap * GetIcon()
Get the 64px^2 icon for the project template.