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, you may find one here:
18 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19 * or you may search the http://www.gnu.org website for the version 2 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
25#include "kicad_manager_frame.h"
26#include <kiway.h>
27#include <kiway_express.h>
31#include <gestfich.h>
32#include <confirm.h>
33#include <string_utils.h>
34#include <richio.h>
36
37
39 const wxString& aSrcProjectDirPath,
40 const wxString& aSrcProjectName,
41 const wxString& aNewProjectDirPath,
42 const wxString& aNewProjectName ) :
43 m_frame( aFrame ),
44 m_projectDirPath( aSrcProjectDirPath ),
45 m_projectName( aSrcProjectName ),
46 m_newProjectDirPath( aNewProjectDirPath ),
47 m_newProjectName( aNewProjectName )
48{
49}
50
51
52wxDirTraverseResult PROJECT_TREE_TRAVERSER::OnFile( const wxString& aSrcFilePath )
53{
54 // Recursion guard for a Save As to a location inside the source project.
55 if( aSrcFilePath.StartsWith( m_newProjectDirPath + wxFileName::GetPathSeparator() ) )
56 return wxDIR_CONTINUE;
57
58 wxFileName destFile( aSrcFilePath );
59 wxString ext = destFile.GetExt();
60 bool atRoot = destFile.GetPath() == m_projectDirPath;
61
65 {
66 wxString destPath = destFile.GetPath();
67
68 if( destPath.StartsWith( m_projectDirPath ) )
69 {
70 destPath.Replace( m_projectDirPath, m_newProjectDirPath, false );
71 destFile.SetPath( destPath );
72 }
73
74 if( destFile.GetName() == m_projectName )
75 {
76 destFile.SetName( m_newProjectName );
77
79 m_newProjectFile = destFile;
80 }
81
83 {
84 // All paths in the settings file are relative so we can just do a straight copy
85 KiCopyFile( aSrcFilePath, destFile.GetFullPath(), m_errors );
86 }
87 else if( ext == FILEEXT::ProjectFileExtension )
88 {
89 PROJECT_FILE projectFile( aSrcFilePath );
90 projectFile.LoadFromFile();
91 projectFile.SaveAs( destFile.GetPath(), destFile.GetName() );
92 }
94 {
95 PROJECT_LOCAL_SETTINGS projectLocalSettings( nullptr, aSrcFilePath );
96 projectLocalSettings.LoadFromFile();
97 projectLocalSettings.SaveAs( destFile.GetPath(), destFile.GetName() );
98 }
99 }
109 || destFile.GetName() == FILEEXT::SymbolLibraryTableFileName ) )
110 {
111 KIFACE* eeschema = m_frame->Kiway().KiFACE( KIWAY::FACE_SCH );
113 m_newProjectName, aSrcFilePath, m_errors );
114 }
115 else if( m_frame && ( ext == FILEEXT::KiCadPcbFileExtension
121 || destFile.GetName() == FILEEXT::FootprintLibraryTableFileName ) )
122 {
123 KIFACE* pcbnew = m_frame->Kiway().KiFACE( KIWAY::FACE_PCB );
125 m_newProjectName, aSrcFilePath, m_errors );
126 }
128 {
129 KIFACE* pleditor = m_frame->Kiway().KiFACE( KIWAY::FACE_PL_EDITOR );
131 m_newProjectName, aSrcFilePath, m_errors );
132 }
133 else if( m_frame && ( ext == FILEEXT::GerberJobFileExtension
136 {
137 KIFACE* gerbview = m_frame->Kiway().KiFACE( KIWAY::FACE_GERBVIEW );
139 m_newProjectName, aSrcFilePath, m_errors );
140 }
141 else if( destFile.GetName().StartsWith( FILEEXT::LockFilePrefix )
143 {
144 // Ignore lock files
145 }
146 else
147 {
148 // Everything we don't recognize just gets a straight copy.
149 wxString destPath = destFile.GetPathWithSep();
150 wxString destName = destFile.GetName();
151 wxUniChar pathSep = wxFileName::GetPathSeparator();
152
153 wxString srcProjectFootprintLib = pathSep + m_projectName + ".pretty" + pathSep;
154 wxString newProjectFootprintLib = pathSep + m_newProjectName + ".pretty" + pathSep;
155
156 if( destPath.StartsWith( m_projectDirPath ) )
157 destPath.Replace( m_projectDirPath, m_newProjectDirPath, false );
158
159 destPath.Replace( srcProjectFootprintLib, newProjectFootprintLib, true );
160
161 if( destName == m_projectName && ext != wxT( "zip" ) /* don't rename archives */ )
162 destFile.SetName( m_newProjectName );
163
164 destFile.SetPath( destPath );
165
166 KiCopyFile( aSrcFilePath, destFile.GetFullPath(), m_errors );
167 }
168
169 return wxDIR_CONTINUE;
170}
171
172
173wxDirTraverseResult PROJECT_TREE_TRAVERSER::OnDir( const wxString& aSrcDirPath )
174{
175 // Recursion guard for a Save As to a location inside the source project.
176 if( aSrcDirPath.StartsWith( m_newProjectDirPath ) )
177 return wxDIR_CONTINUE;
178
179 wxFileName destDir( aSrcDirPath );
180 wxString destDirPath = destDir.GetPathWithSep();
181 wxUniChar pathSep = wxFileName::GetPathSeparator();
182
183 if( destDirPath.StartsWith( m_projectDirPath + pathSep )
184 || destDirPath.StartsWith( m_projectDirPath + PROJECT_BACKUPS_DIR_SUFFIX ) )
185 {
186 destDirPath.Replace( m_projectDirPath, m_newProjectDirPath, false );
187 destDir.SetPath( destDirPath );
188 }
189
190 if( destDir.GetName() == m_projectName )
191 {
192 if( destDir.GetExt() == "pretty" )
193 destDir.SetName( m_newProjectName );
194#if 0
195 // WAYNE STAMBAUGH TODO:
196 // If we end up with a symbol equivalent to ".pretty" we'll want to handle it here....
197 else if( destDir.GetExt() == "sym_lib_dir_extension" )
198 destDir.SetName( m_newProjectName );
199#endif
200 }
201
202 if( !wxMkdir( destDir.GetFullPath() ) )
203 {
204 wxString msg;
205
206 if( !m_errors.empty() )
207 m_errors += "\n";
208
209 msg.Printf( _( "Cannot copy folder '%s'." ), destDir.GetFullPath() );
210 m_errors += msg;
211 }
212
213 return wxDIR_CONTINUE;
214}
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:298
@ FACE_PL_EDITOR
Definition kiway.h:302
@ FACE_PCB
pcbnew DSO
Definition kiway.h:299
@ FACE_GERBVIEW
Definition kiway.h:301
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:290
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:154
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:219
Definition of file extensions used in Kicad.