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