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 <confirm.h>
31 #include <kiway.h>
32 #include <pgm_kicad.h>
34 #include <reporter.h>
37 
38 #include "kicad_manager_frame.h"
39 
40 
41 void KICAD_MANAGER_FRAME::OnFileHistory( wxCommandEvent& event )
42 {
43  wxFileName projFileName = GetFileFromHistory( event.GetId(), _( "KiCad project file" ) );
44  if( !projFileName.FileExists() )
45  return;
46 
47  LoadProject( projFileName );
48 }
49 
50 
51 void KICAD_MANAGER_FRAME::OnClearFileHistory( wxCommandEvent& aEvent )
52 {
54 }
55 
56 
57 void KICAD_MANAGER_FRAME::OnUnarchiveFiles( wxCommandEvent& event )
58 {
59  wxFileName fn = Prj().GetProjectFullName();
60 
61  fn.SetExt( ArchiveFileExtension );
62 
63  wxFileDialog zipfiledlg( this, _( "Unzip Project" ), fn.GetPath(),
64  fn.GetFullName(), ZipFileWildcard(),
65  wxFD_OPEN | wxFD_FILE_MUST_EXIST );
66 
67  if( zipfiledlg.ShowModal() == wxID_CANCEL )
68  return;
69 
70  wxString msg = wxString::Format( _( "\nOpen \"%s\"\n" ), zipfiledlg.GetPath() );
71  PrintMsg( msg );
72 
73  wxDirDialog dirDlg( this, _( "Target Directory" ), fn.GetPath(), wxDD_DEFAULT_STYLE );
74 
75  if( dirDlg.ShowModal() == wxID_CANCEL )
76  return;
77 
78  wxString unzipDir = dirDlg.GetPath() + wxT( "/" );
79  msg.Printf( _( "Unzipping project in \"%s\"\n" ), unzipDir );
80  PrintMsg( msg );
81 
82  if( unzipDir == Prj().GetProjectPath() )
83  {
84  if( !Kiway().PlayersClose( false ) )
85  return;
86  }
87 
88  STATUSBAR_REPORTER reporter( GetStatusBar(), 1 );
89 
90  PROJECT_ARCHIVER archiver;
91 
92  archiver.Unarchive( zipfiledlg.GetPath(), unzipDir, reporter );
93 
94  if( unzipDir == Prj().GetProjectPath() )
95  {
96  wxString prjPath = Prj().GetProjectFullName();
97 
98  SETTINGS_MANAGER& mgr = Pgm().GetSettingsManager();
99 
100  mgr.UnloadProject( &Prj(), false );
101  mgr.LoadProject( prjPath );
102 
104  }
105 }
106 
107 
108 void KICAD_MANAGER_FRAME::OnArchiveFiles( wxCommandEvent& event )
109 {
110  wxFileName fileName = GetProjectFileName();
111 
112  fileName.SetExt( ArchiveFileExtension );
113 
114  wxFileDialog dlg( this, _( "Archive Project Files" ),
115  fileName.GetPath(), fileName.GetFullName(),
116  ZipFileWildcard(), wxFD_SAVE | wxFD_OVERWRITE_PROMPT );
117 
118  if( dlg.ShowModal() == wxID_CANCEL )
119  return;
120 
121  wxFileName zipFile = dlg.GetPath();
122 
123  wxString currdirname = fileName.GetPathWithSep();
124  wxDir dir( currdirname );
125 
126  if( !dir.IsOpened() ) // wxWidgets display a error message on issue.
127  return;
128 
129  STATUSBAR_REPORTER reporter( GetStatusBar(), 1 );
130  PROJECT_ARCHIVER archiver;
131 
132  archiver.Archive( currdirname, zipFile.GetFullPath(), reporter, true, true );
133 }
A wrapper for reporting to a specific text location in a statusbar.
Definition: reporter.h:262
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.
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.
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