KiCad PCB EDA Suite
Loading...
Searching...
No Matches
project_tree_traverser.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 The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
21#include "kicad_manager_frame.h"
22#include <kiway.h>
23#include <kiway_mail.h>
27#include <gestfich.h>
28#include <confirm.h>
29#include <string_utils.h>
30#include <richio.h>
32
33
35 const wxString& aSrcProjectDirPath,
36 const wxString& aSrcProjectName,
37 const wxString& aNewProjectDirPath,
38 const wxString& aNewProjectName ) :
39 m_frame( aFrame ),
40 m_projectDirPath( aSrcProjectDirPath ),
41 m_projectName( aSrcProjectName ),
42 m_newProjectDirPath( aNewProjectDirPath ),
43 m_newProjectName( aNewProjectName )
44{
45}
46
47
48wxDirTraverseResult PROJECT_TREE_TRAVERSER::OnFile( const wxString& aSrcFilePath )
49{
50 // Recursion guard for a Save As to a location inside the source project.
51 if( aSrcFilePath.StartsWith( m_newProjectDirPath ) )
52 return wxDIR_CONTINUE;
53
54 wxFileName destFile( aSrcFilePath );
55 wxString ext = destFile.GetExt();
56 bool atRoot = destFile.GetPath() == m_projectDirPath;
57
58 auto saveAs =
59 [&]( KIWAY::FACE_T aFace )
60 {
61 if( KIFACE* kiface = m_frame->Kiway().KiFACE( aFace ) )
62 {
64 aSrcFilePath, m_errors );
65 return;
66 }
67
68 if( !m_errors.empty() )
69 m_errors += "\n";
70
71 m_errors += wxString::Format( _( "Cannot copy file '%s'." ), aSrcFilePath );
72 };
73
77 {
78 wxString destPath = destFile.GetPath();
79
80 if( destPath.StartsWith( m_projectDirPath ) )
81 {
82 destPath.Replace( m_projectDirPath, m_newProjectDirPath, false );
83 destFile.SetPath( destPath );
84 }
85
86 if( destFile.GetName() == m_projectName )
87 {
88 destFile.SetName( m_newProjectName );
89
91 m_newProjectFile = destFile;
92 }
93
95 {
96 // All paths in the settings file are relative so we can just do a straight copy
97 KiCopyFile( aSrcFilePath, destFile.GetFullPath(), m_errors );
98 }
99 else if( ext == FILEEXT::ProjectFileExtension )
100 {
101 PROJECT_FILE projectFile( aSrcFilePath );
102 projectFile.LoadFromFile();
103 projectFile.SaveAs( destFile.GetPath(), destFile.GetName() );
104 }
106 {
107 PROJECT_LOCAL_SETTINGS projectLocalSettings( nullptr, aSrcFilePath );
108 projectLocalSettings.LoadFromFile();
109 projectLocalSettings.SaveAs( destFile.GetPath(), destFile.GetName() );
110 }
111 }
121 || destFile.GetName() == FILEEXT::SymbolLibraryTableFileName ) )
122 {
123 saveAs( KIWAY::FACE_SCH );
124 }
125 else if( m_frame && ( ext == FILEEXT::KiCadPcbFileExtension
131 || destFile.GetName() == FILEEXT::FootprintLibraryTableFileName ) )
132 {
133 saveAs( KIWAY::FACE_PCB );
134 }
136 {
137 saveAs( KIWAY::FACE_PL_EDITOR );
138 }
139 else if( m_frame && ( ext == FILEEXT::GerberJobFileExtension
142 {
143 saveAs( KIWAY::FACE_GERBVIEW );
144 }
145 else if( destFile.GetName().StartsWith( FILEEXT::LockFilePrefix )
147 {
148 // Ignore lock files
149 }
150 else
151 {
152 // Everything we don't recognize just gets a straight copy.
153 wxString destPath = destFile.GetPathWithSep();
154 wxString destName = destFile.GetName();
155 wxUniChar pathSep = wxFileName::GetPathSeparator();
156
157 wxString srcProjectFootprintLib = pathSep + m_projectName + ".pretty" + pathSep;
158 wxString newProjectFootprintLib = pathSep + m_newProjectName + ".pretty" + pathSep;
159
160 if( destPath.StartsWith( m_projectDirPath ) )
161 destPath.Replace( m_projectDirPath, m_newProjectDirPath, false );
162
163 destPath.Replace( srcProjectFootprintLib, newProjectFootprintLib, true );
164
165 if( destName == m_projectName && ext != wxT( "zip" ) /* don't rename archives */ )
166 destFile.SetName( m_newProjectName );
167
168 destFile.SetPath( destPath );
169
170 KiCopyFile( aSrcFilePath, destFile.GetFullPath(), m_errors );
171 }
172
173 return wxDIR_CONTINUE;
174}
175
176
177wxDirTraverseResult PROJECT_TREE_TRAVERSER::OnDir( const wxString& aSrcDirPath )
178{
179 wxUniChar pathSep = wxFileName::GetPathSeparator();
180
181 // Skip the local-history git repository.
182 wxFileName srcDir = wxFileName::DirName( aSrcDirPath );
183 wxArrayString dirs = srcDir.GetDirs();
184
185 if( !dirs.IsEmpty() && dirs.Last() == wxS( ".history" ) )
186 return wxDIR_IGNORE;
187
188 // Recursion guard for a Save As to a location inside the source project.
189 if( ( aSrcDirPath + pathSep ).StartsWith( m_newProjectDirPath ) )
190 return wxDIR_IGNORE;
191
192 wxFileName destDir( aSrcDirPath );
193 wxString destDirPath = destDir.GetPathWithSep();
194
195 if( destDirPath.StartsWith( m_projectDirPath + pathSep )
196 || destDirPath.StartsWith( m_projectDirPath + PROJECT_BACKUPS_DIR_SUFFIX ) )
197 {
198 destDirPath.Replace( m_projectDirPath, m_newProjectDirPath, false );
199 destDir.SetPath( destDirPath );
200 }
201
202 if( destDir.GetName() == m_projectName )
203 {
204 if( destDir.GetExt() == "pretty" )
205 destDir.SetName( m_newProjectName );
206#if 0
207 // WAYNE STAMBAUGH TODO:
208 // If we end up with a symbol equivalent to ".pretty" we'll want to handle it here....
209 else if( destDir.GetExt() == "sym_lib_dir_extension" )
210 destDir.SetName( m_newProjectName );
211#endif
212 }
213
214 if( !wxMkdir( destDir.GetFullPath() ) )
215 {
216 wxString msg;
217
218 if( !m_errors.empty() )
219 m_errors += "\n";
220
221 msg.Printf( _( "Cannot copy folder '%s'." ), destDir.GetFullPath() );
222 m_errors += msg;
223 }
224
225 return wxDIR_CONTINUE;
226}
virtual bool LoadFromFile(const wxString &aDirectory="")
Loads the backing file from disk and then calls Load()
The main KiCad project manager frame.
FACE_T
Known KIFACE implementations.
Definition kiway.h:354
@ FACE_SCH
eeschema DSO
Definition kiway.h:355
@ FACE_PL_EDITOR
Definition kiway.h:359
@ FACE_PCB
pcbnew DSO
Definition kiway.h:356
@ FACE_GERBVIEW
Definition kiway.h:358
The backing store for a PROJECT, in JSON format.
bool SaveAs(const wxString &aDirectory, const wxString &aFile)
bool LoadFromFile(const wxString &aDirectory="") override
Loads the backing file from disk and then calls Load()
The project local settings are things that are attached to a particular project, but also might be pa...
bool SaveAs(const wxString &aDirectory, const wxString &aFile)
KICAD_MANAGER_FRAME * m_frame
virtual wxDirTraverseResult OnFile(const wxString &aSrcFilePath) override
PROJECT_TREE_TRAVERSER(KICAD_MANAGER_FRAME *aFrame, const wxString &aSrcProjectDirPath, const wxString &aSrcProjectName, const wxString &aNewProjectDirPath, const wxString &aNewProjectName)
virtual wxDirTraverseResult OnDir(const wxString &aSrcDirPath) override
This file is part of the common library.
#define _(s)
void KiCopyFile(const wxString &aSrcPath, const wxString &aDestPath, wxString &aErrors)
Definition gestfich.cpp:416
static const std::string LegacySchematicFileExtension
static const std::string NetlistFileExtension
static const std::string SymbolLibraryTableFileName
static const std::string GerberJobFileExtension
static const std::string LockFileExtension
static const std::string ProjectFileExtension
static const std::string LegacyPcbFileExtension
static const std::string SchematicSymbolFileExtension
static const std::string LegacyProjectFileExtension
static const std::string ProjectLocalSettingsFileExtension
static const std::string KiCadSchematicFileExtension
static const std::string LegacySymbolLibFileExtension
static const std::string LockFilePrefix
static const std::string KiCadSymbolLibFileExtension
static const std::string FootprintLibraryTableFileName
static const std::string DrawingSheetFileExtension
static const std::string BackupFileSuffix
static const std::string LegacyFootprintLibPathExtension
static const std::string LegacySymbolDocumentFileExtension
static const std::string FootprintAssignmentFileExtension
static const std::string DrillFileExtension
static const std::string KiCadFootprintFileExtension
static const std::string KiCadPcbFileExtension
static bool IsGerberFileExtension(const wxString &ext)
#define PROJECT_BACKUPS_DIR_SUFFIX
Project settings path will be <projectname> + this.
Implement a participant in the KIWAY alchemy.
Definition kiway.h:153
IFACE KIFACE_BASE kiface("pcb_test_frame", KIWAY::FACE_PCB)
Definition of file extensions used in Kicad.