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
61 {
62 wxString destPath = destFile.GetPath();
63
64 if( destPath.StartsWith( m_projectDirPath ) )
65 {
66 destPath.Replace( m_projectDirPath, m_newProjectDirPath, false );
67 destFile.SetPath( destPath );
68 }
69
70 if( destFile.GetName() == m_projectName )
71 {
72 destFile.SetName( m_newProjectName );
73
75 m_newProjectFile = destFile;
76 }
77
79 {
80 // All paths in the settings file are relative so we can just do a straight copy
81 KiCopyFile( aSrcFilePath, destFile.GetFullPath(), m_errors );
82 }
83 else if( ext == FILEEXT::ProjectFileExtension )
84 {
85 PROJECT_FILE projectFile( aSrcFilePath );
86 projectFile.LoadFromFile();
87 projectFile.SaveAs( destFile.GetPath(), destFile.GetName() );
88 }
90 {
91 PROJECT_LOCAL_SETTINGS projectLocalSettings( nullptr, aSrcFilePath );
92 projectLocalSettings.LoadFromFile();
93 projectLocalSettings.SaveAs( destFile.GetPath(), destFile.GetName() );
94 }
95 }
105 || destFile.GetName() == FILEEXT::SymbolLibraryTableFileName ) )
106 {
107 KIFACE* eeschema = m_frame->Kiway().KiFACE( KIWAY::FACE_SCH );
109 m_newProjectName, aSrcFilePath, m_errors );
110 }
111 else if( m_frame && ( ext == FILEEXT::KiCadPcbFileExtension
117 || destFile.GetName() == FILEEXT::FootprintLibraryTableFileName ) )
118 {
119 KIFACE* pcbnew = m_frame->Kiway().KiFACE( KIWAY::FACE_PCB );
121 m_newProjectName, aSrcFilePath, m_errors );
122 }
124 {
125 KIFACE* pleditor = m_frame->Kiway().KiFACE( KIWAY::FACE_PL_EDITOR );
127 m_newProjectName, aSrcFilePath, m_errors );
128 }
129 else if( m_frame && ( ext == FILEEXT::GerberJobFileExtension
132 {
133 KIFACE* gerbview = m_frame->Kiway().KiFACE( KIWAY::FACE_GERBVIEW );
135 m_newProjectName, aSrcFilePath, m_errors );
136 }
137 else if( destFile.GetName().StartsWith( FILEEXT::LockFilePrefix )
139 {
140 // Ignore lock files
141 }
142 else
143 {
144 // Everything we don't recognize just gets a straight copy.
145 wxString destPath = destFile.GetPathWithSep();
146 wxString destName = destFile.GetName();
147 wxUniChar pathSep = wxFileName::GetPathSeparator();
148
149 wxString srcProjectFootprintLib = pathSep + m_projectName + ".pretty" + pathSep;
150 wxString newProjectFootprintLib = pathSep + m_newProjectName + ".pretty" + pathSep;
151
152 if( destPath.StartsWith( m_projectDirPath ) )
153 destPath.Replace( m_projectDirPath, m_newProjectDirPath, false );
154
155 destPath.Replace( srcProjectFootprintLib, newProjectFootprintLib, true );
156
157 if( destName == m_projectName && ext != wxT( "zip" ) /* don't rename archives */ )
158 destFile.SetName( m_newProjectName );
159
160 destFile.SetPath( destPath );
161
162 KiCopyFile( aSrcFilePath, destFile.GetFullPath(), m_errors );
163 }
164
165 return wxDIR_CONTINUE;
166}
167
168
169wxDirTraverseResult PROJECT_TREE_TRAVERSER::OnDir( const wxString& aSrcDirPath )
170{
171 wxUniChar pathSep = wxFileName::GetPathSeparator();
172
173 // Skip the local-history git repository.
174 wxFileName srcDir = wxFileName::DirName( aSrcDirPath );
175 wxArrayString dirs = srcDir.GetDirs();
176
177 if( !dirs.IsEmpty() && dirs.Last() == wxS( ".history" ) )
178 return wxDIR_IGNORE;
179
180 // Recursion guard for a Save As to a location inside the source project.
181 if( ( aSrcDirPath + pathSep ).StartsWith( m_newProjectDirPath ) )
182 return wxDIR_IGNORE;
183
184 wxFileName destDir( aSrcDirPath );
185 wxString destDirPath = destDir.GetPathWithSep();
186
187 if( destDirPath.StartsWith( m_projectDirPath + pathSep )
188 || destDirPath.StartsWith( m_projectDirPath + PROJECT_BACKUPS_DIR_SUFFIX ) )
189 {
190 destDirPath.Replace( m_projectDirPath, m_newProjectDirPath, false );
191 destDir.SetPath( destDirPath );
192 }
193
194 if( destDir.GetName() == m_projectName )
195 {
196 if( destDir.GetExt() == "pretty" )
197 destDir.SetName( m_newProjectName );
198#if 0
199 // WAYNE STAMBAUGH TODO:
200 // If we end up with a symbol equivalent to ".pretty" we'll want to handle it here....
201 else if( destDir.GetExt() == "sym_lib_dir_extension" )
202 destDir.SetName( m_newProjectName );
203#endif
204 }
205
206 if( !wxMkdir( destDir.GetFullPath() ) )
207 {
208 wxString msg;
209
210 if( !m_errors.empty() )
211 m_errors += "\n";
212
213 msg.Printf( _( "Cannot copy folder '%s'." ), destDir.GetFullPath() );
214 m_errors += msg;
215 }
216
217 return wxDIR_CONTINUE;
218}
virtual bool LoadFromFile(const wxString &aDirectory="")
Loads the backing file from disk and then calls Load()
The main KiCad project manager frame.
@ FACE_SCH
eeschema DSO
Definition kiway.h:318
@ FACE_PL_EDITOR
Definition kiway.h:322
@ FACE_PCB
pcbnew DSO
Definition kiway.h:319
@ FACE_GERBVIEW
Definition kiway.h:321
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:307
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:152
virtual void SaveFileAs(const wxString &srcProjectBasePath, const wxString &srcProjectName, const wxString &newProjectBasePath, const wxString &newProjectName, const wxString &srcFilePath, wxString &aErrors)
Saving a file under a different name is delegated to the various KIFACEs because the project doesn't ...
Definition kiway.h:217
Definition of file extensions used in Kicad.