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>
30 #include <kiway.h>
31 #include <pgm_kicad.h>
33 #include <reporter.h>
36 
37 #include "kicad_manager_frame.h"
38 
39 
40 void KICAD_MANAGER_FRAME::OnFileHistory( wxCommandEvent& event )
41 {
42  wxFileName projFileName = GetFileFromHistory( event.GetId(), _( "KiCad project file" ) );
43  if( !projFileName.FileExists() )
44  return;
45 
46  LoadProject( projFileName );
47 }
48 
49 
50 void KICAD_MANAGER_FRAME::OnClearFileHistory( wxCommandEvent& aEvent )
51 {
53 }
54 
55 
56 void KICAD_MANAGER_FRAME::OnUnarchiveFiles( wxCommandEvent& event )
57 {
58  wxFileName fn = Prj().GetProjectFullName();
59 
60  fn.SetExt( ArchiveFileExtension );
61 
62  wxFileDialog zipfiledlg( this, _( "Unzip Project" ), fn.GetPath(),
63  fn.GetFullName(), ZipFileWildcard(),
64  wxFD_OPEN | wxFD_FILE_MUST_EXIST );
65 
66  if( zipfiledlg.ShowModal() == wxID_CANCEL )
67  return;
68 
69  wxString msg = wxString::Format( _( "\nOpen \"%s\"\n" ), zipfiledlg.GetPath() );
70  PrintMsg( msg );
71 
72  wxDirDialog dirDlg( this, _( "Target Directory" ), fn.GetPath(), wxDD_DEFAULT_STYLE );
73 
74  if( dirDlg.ShowModal() == wxID_CANCEL )
75  return;
76 
77  wxString unzipDir = dirDlg.GetPath() + wxT( "/" );
78  msg.Printf( _( "Unzipping project in \"%s\"\n" ), unzipDir );
79  PrintMsg( msg );
80 
81  if( unzipDir == Prj().GetProjectPath() )
82  {
83  if( !Kiway().PlayersClose( false ) )
84  return;
85  }
86 
88 
89  PROJECT_ARCHIVER archiver;
90 
91  archiver.Unarchive( zipfiledlg.GetPath(), unzipDir, reporter );
92 
93  if( unzipDir == Prj().GetProjectPath() )
94  {
95  wxString prjPath = Prj().GetProjectFullName();
96 
97  SETTINGS_MANAGER& mgr = Pgm().GetSettingsManager();
98 
99  mgr.UnloadProject( &Prj(), false );
100  mgr.LoadProject( prjPath );
101 
103  }
104 }
105 
106 
107 void KICAD_MANAGER_FRAME::OnArchiveFiles( wxCommandEvent& event )
108 {
109  wxFileName fileName = GetProjectFileName();
110 
111  fileName.SetExt( ArchiveFileExtension );
112 
113  wxFileDialog dlg( this, _( "Archive Project Files" ),
114  fileName.GetPath(), fileName.GetFullName(),
115  ZipFileWildcard(), wxFD_SAVE | wxFD_OVERWRITE_PROMPT );
116 
117  if( dlg.ShowModal() == wxID_CANCEL )
118  return;
119 
120  wxFileName zipFile = dlg.GetPath();
121 
122  wxString currdirname = fileName.GetPathWithSep();
123  wxDir dir( currdirname );
124 
125  if( !dir.IsOpened() ) // wxWidgets display a error message on issue.
126  return;
127 
129 
130  PROJECT_ARCHIVER archiver;
131 
132  archiver.Archive( currdirname, zipFile.GetFullPath(), reporter );
133 }
KIWAY & Kiway() const
Function Kiway returns 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.
bool Archive(const wxString &aSrcDir, const wxString &aDestFile, REPORTER &aReporter, bool aVerbose=true)
Creates an archive of the project.
wxString ZipFileWildcard()
KIWAY Kiway & Pgm(), KFCTL_STANDALONE
The global Program "get" accessor.
Definition: single_top.cpp:102
VTBL_ENTRY const wxString GetProjectPath() const
Function GetProjectPath returns the full path of the project.
Definition: project.cpp:121
WX_TEXT_CTRL_REPORTER is wrapper for reporting to a wxTextCtrl object.
Definition: reporter.h:136
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)
void PrintMsg(const wxString &aText)
Displays aText in the text panel.
PROJECT & Prj() const
Function Prj returns a reference to the PROJECT "associated with" this KIWAY.
Definition of file extensions used in Kicad.
VTBL_ENTRY const wxString GetProjectFullName() const
Function GetProjectFullName returns the full path and name of the project.
Definition: project.cpp:115
void OnArchiveFiles(wxCommandEvent &event)
void ClearFileHistory(FILE_HISTORY *aFileHistory=nullptr)
Removes all files from the file history.
bool LoadProject(const wxString &aFullPath, bool aSetActive=true)
Loads a project or sets up a new project with a specified path.
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, CPTREE &aTree)
Function Format outputs a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:201
#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