KiCad PCB EDA Suite
kicad/files-io.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) 2004-2015 Jean-Pierre Charras
5  * Copyright (C) 2004-2020 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 
29 #include <wx/dir.h>
30 #include <wx/filedlg.h>
31 #include <wx/dirdlg.h>
32 
33 #include <confirm.h>
35 #include <kiway.h>
36 #include <pgm_kicad.h>
38 #include <reporter.h>
41 
42 #include "kicad_manager_frame.h"
43 
44 
45 void KICAD_MANAGER_FRAME::OnFileHistory( wxCommandEvent& event )
46 {
47  wxFileName projFileName = GetFileFromHistory( event.GetId(), _( "KiCad project file" ) );
48  if( !projFileName.FileExists() )
49  return;
50 
51  LoadProject( projFileName );
52 }
53 
54 
55 void KICAD_MANAGER_FRAME::OnClearFileHistory( wxCommandEvent& aEvent )
56 {
58 }
59 
60 
61 void KICAD_MANAGER_FRAME::OnUnarchiveFiles( wxCommandEvent& event )
62 {
63  wxFileName fn = Prj().GetProjectFullName();
64 
65  fn.SetExt( ArchiveFileExtension );
66 
67  wxFileDialog zipfiledlg( this, _( "Unzip Project" ), fn.GetPath(),
68  fn.GetFullName(), ZipFileWildcard(),
69  wxFD_OPEN | wxFD_FILE_MUST_EXIST );
70 
71  if( zipfiledlg.ShowModal() == wxID_CANCEL )
72  return;
73 
74  wxString msg = wxString::Format( _( "\nOpen \"%s\"\n" ), zipfiledlg.GetPath() );
75  PrintMsg( msg );
76 
77  wxDirDialog dirDlg( this, _( "Target Directory" ), fn.GetPath(), wxDD_DEFAULT_STYLE );
78 
79  if( dirDlg.ShowModal() == wxID_CANCEL )
80  return;
81 
82  wxString unzipDir = dirDlg.GetPath() + wxT( "/" );
83  msg.Printf( _( "Unzipping project in \"%s\"\n" ), unzipDir );
84  PrintMsg( msg );
85 
86  if( unzipDir == Prj().GetProjectPath() )
87  {
88  if( !Kiway().PlayersClose( false ) )
89  return;
90  }
91 
92  STATUSBAR_REPORTER reporter( GetStatusBar(), 1 );
93 
94  PROJECT_ARCHIVER archiver;
95 
96  archiver.Unarchive( zipfiledlg.GetPath(), unzipDir, reporter );
97 
98  if( unzipDir == Prj().GetProjectPath() )
99  {
100  wxString prjPath = Prj().GetProjectFullName();
101 
102  SETTINGS_MANAGER& mgr = Pgm().GetSettingsManager();
103 
104  mgr.UnloadProject( &Prj(), false );
105  mgr.LoadProject( prjPath );
106 
108  }
109 }
110 
111 
112 void KICAD_MANAGER_FRAME::OnArchiveFiles( wxCommandEvent& event )
113 {
114  wxFileName fileName = GetProjectFileName();
115 
116  fileName.SetExt( ArchiveFileExtension );
117 
118  wxFileDialog dlg( this, _( "Archive Project Files" ),
119  fileName.GetPath(), fileName.GetFullName(),
120  ZipFileWildcard(), wxFD_SAVE | wxFD_OVERWRITE_PROMPT );
121 
122  if( dlg.ShowModal() == wxID_CANCEL )
123  return;
124 
125  wxFileName zipFile = dlg.GetPath();
126 
127  wxString currdirname = fileName.GetPathWithSep();
128  wxDir dir( currdirname );
129 
130  if( !dir.IsOpened() ) // wxWidgets display a error message on issue.
131  return;
132 
133  STATUSBAR_REPORTER reporter( GetStatusBar(), 1 );
134  PROJECT_ARCHIVER archiver;
135 
136  archiver.Archive( currdirname, zipFile.GetFullPath(), reporter, true, true );
137 }
A wrapper for reporting to a specific text location in a statusbar.
Definition: reporter.h:280
void PrintMsg(const wxString &text)
bool Archive(const wxString &aSrcDir, const wxString &aDestFile, REPORTER &aReporter, bool aVerbose=true, bool aIncludeExtraFiles=false)
Creates an archive of the project.
KIWAY & Kiway() const
Return a reference to the KIWAY that this object has an opportunity to participate in.
Definition: kiway_holder.h:56
const wxString GetProjectFileName() const
void OnClearFileHistory(wxCommandEvent &aEvent)
This file is part of the common library.
wxString ZipFileWildcard()
KIWAY Kiway & Pgm(), KFCTL_STANDALONE
The global Program "get" accessor.
Definition: single_top.cpp:106
virtual const wxString GetProjectPath() const
Return the full path of the project.
Definition: project.cpp:123
wxString GetFileFromHistory(int cmdId, const wxString &type, FILE_HISTORY *aFileHistory=nullptr)
Fetches the file name from the file history list.
bool Unarchive(const wxString &aSrcFile, const wxString &aDestDir, REPORTER &aReporter)
Extracts an archive of the current project over existing files Warning: this will overwrite files in ...
void OnFileHistory(wxCommandEvent &event)
PROJECT & Prj() const
Return a reference to the PROJECT associated with this KIWAY.
Definition of file extensions used in Kicad.
virtual const wxString GetProjectFullName() const
Return the full path and name of the project.
Definition: project.cpp:117
void OnArchiveFiles(wxCommandEvent &event)
void ClearFileHistory(FILE_HISTORY *aFileHistory=nullptr)
Removes all files from the file history.
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200
bool LoadProject(const wxString &aFullPath, bool aSetActive=true)
Loads a project or sets up a new project with a specified path.
#define _(s)
Definition: 3d_actions.cpp:33
bool UnloadProject(PROJECT *aProject, bool aSave=true)
Saves, unloads and unregisters the given PROJECT.
void LoadProject(const wxFileName &aProjectFileName)
void OnUnarchiveFiles(wxCommandEvent &event)
const std::string ArchiveFileExtension