2012-04-16 14:56:01 +02:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
2015-09-23 11:26:50 +02:00
|
|
|
* Copyright (C) 2004-2015 Jean-Pierre Charras
|
2017-08-31 11:15:20 -04:00
|
|
|
* Copyright (C) 2004-2017 KiCad Developers, see change_log.txt for contributors.
|
2012-04-16 14:56:01 +02:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, you may find one here:
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 2 license,
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
2007-06-05 12:10:51 +00:00
|
|
|
|
2016-02-24 16:36:52 -05:00
|
|
|
/**
|
|
|
|
* @file kicad/files-io.cpp
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <wx/dir.h>
|
2007-06-05 12:10:51 +00:00
|
|
|
#include <wx/fs_zip.h>
|
2019-01-20 16:40:30 -08:00
|
|
|
#include <wx/uri.h>
|
2007-06-05 12:10:51 +00:00
|
|
|
#include <wx/wfstream.h>
|
2016-02-24 16:36:52 -05:00
|
|
|
#include <wx/zipstrm.h>
|
2007-06-05 12:10:51 +00:00
|
|
|
|
2012-01-22 22:33:36 -06:00
|
|
|
#include <confirm.h>
|
2016-02-24 16:36:52 -05:00
|
|
|
#include <kiway.h>
|
|
|
|
#include "pgm_kicad.h"
|
2017-11-11 19:31:38 -05:00
|
|
|
#include "wildcards_and_files_ext.h"
|
2016-02-24 16:36:52 -05:00
|
|
|
|
2019-06-09 11:10:19 +01:00
|
|
|
#include "kicad_manager_frame.h"
|
2007-06-05 12:10:51 +00:00
|
|
|
|
|
|
|
|
2019-03-27 16:46:51 +01:00
|
|
|
#define ZipFileExtension wxT( "zip" )
|
2007-06-05 12:10:51 +00:00
|
|
|
|
|
|
|
|
2011-04-17 15:54:17 +02:00
|
|
|
void KICAD_MANAGER_FRAME::OnFileHistory( wxCommandEvent& event )
|
2009-01-17 20:31:19 +00:00
|
|
|
{
|
2017-08-31 11:15:20 -04:00
|
|
|
wxFileName projFileName = GetFileFromHistory( event.GetId(), _( "KiCad project file" ),
|
|
|
|
&PgmTop().GetFileHistory() );
|
|
|
|
if( !projFileName.FileExists() )
|
|
|
|
return;
|
2009-01-17 20:31:19 +00:00
|
|
|
|
2017-08-31 11:15:20 -04:00
|
|
|
LoadProject( projFileName );
|
2009-01-17 20:31:19 +00:00
|
|
|
}
|
|
|
|
|
2014-07-07 14:49:14 -05:00
|
|
|
|
2011-04-17 15:54:17 +02:00
|
|
|
void KICAD_MANAGER_FRAME::OnUnarchiveFiles( wxCommandEvent& event )
|
2007-06-05 12:10:51 +00:00
|
|
|
{
|
2014-07-07 14:49:14 -05:00
|
|
|
wxFileName fn = GetProjectFileName();
|
|
|
|
|
2009-04-05 20:49:15 +00:00
|
|
|
fn.SetExt( ZipFileExtension );
|
2008-12-08 15:27:13 +00:00
|
|
|
|
2015-09-25 20:45:45 +02:00
|
|
|
wxFileDialog zipfiledlg( this, _( "Unzip Project" ), fn.GetPath(),
|
2017-11-11 19:31:38 -05:00
|
|
|
fn.GetFullName(), ZipFileWildcard(),
|
2017-08-31 11:15:20 -04:00
|
|
|
wxFD_OPEN | wxFD_FILE_MUST_EXIST );
|
2007-09-13 11:55:46 +00:00
|
|
|
|
2015-09-25 20:45:45 +02:00
|
|
|
if( zipfiledlg.ShowModal() == wxID_CANCEL )
|
2007-09-13 11:55:46 +00:00
|
|
|
return;
|
|
|
|
|
2017-12-15 12:37:46 +01:00
|
|
|
wxString msg = wxString::Format( _( "\nOpen \"%s\"\n" ), GetChars( zipfiledlg.GetPath() ) );
|
2013-04-09 19:49:01 +02:00
|
|
|
PrintMsg( msg );
|
2007-09-13 11:55:46 +00:00
|
|
|
|
2009-04-05 20:49:15 +00:00
|
|
|
wxDirDialog dirDlg( this, _( "Target Directory" ), fn.GetPath(),
|
|
|
|
wxDD_DEFAULT_STYLE | wxDD_DIR_MUST_EXIST );
|
|
|
|
|
|
|
|
if( dirDlg.ShowModal() == wxID_CANCEL )
|
2007-09-13 11:55:46 +00:00
|
|
|
return;
|
|
|
|
|
2015-09-25 20:45:45 +02:00
|
|
|
wxString unzipDir = dirDlg.GetPath() + wxT( "/" );
|
2017-12-15 12:37:46 +01:00
|
|
|
msg.Printf( _( "Unzipping project in \"%s\"\n" ), GetChars( unzipDir ) );
|
2013-04-09 19:49:01 +02:00
|
|
|
PrintMsg( msg );
|
2007-09-13 11:55:46 +00:00
|
|
|
|
|
|
|
wxFileSystem zipfilesys;
|
2014-07-07 14:49:14 -05:00
|
|
|
|
2007-09-13 11:55:46 +00:00
|
|
|
zipfilesys.AddHandler( new wxZipFSHandler );
|
2019-01-20 16:40:30 -08:00
|
|
|
auto path = wxURI( zipfiledlg.GetPath() + wxT( "#zip:" ) ).BuildURI();
|
|
|
|
zipfilesys.ChangePathTo( path, true );
|
2007-09-13 11:55:46 +00:00
|
|
|
|
|
|
|
wxFSFile* zipfile = NULL;
|
2019-01-20 16:40:30 -08:00
|
|
|
wxString localfilename = zipfilesys.FindFirst( wxFileSelectorDefaultWildcardStr, wxFILE );
|
2007-09-13 11:55:46 +00:00
|
|
|
|
|
|
|
while( !localfilename.IsEmpty() )
|
|
|
|
{
|
|
|
|
zipfile = zipfilesys.OpenFile( localfilename );
|
2014-07-07 14:49:14 -05:00
|
|
|
if( !zipfile )
|
2007-09-13 11:55:46 +00:00
|
|
|
{
|
|
|
|
DisplayError( this, wxT( "Zip file read error" ) );
|
|
|
|
break;
|
|
|
|
}
|
2008-12-08 15:27:13 +00:00
|
|
|
|
2015-09-25 20:45:45 +02:00
|
|
|
wxFileName uzfn = localfilename.AfterLast( ':' );
|
|
|
|
uzfn.MakeAbsolute( unzipDir );
|
|
|
|
wxString unzipfilename = uzfn.GetFullPath();
|
2008-12-08 15:27:13 +00:00
|
|
|
|
2017-12-15 12:37:46 +01:00
|
|
|
msg.Printf( _( "Extract file \"%s\"" ), GetChars( unzipfilename ) );
|
2013-04-09 19:49:01 +02:00
|
|
|
PrintMsg( msg );
|
2008-12-08 15:27:13 +00:00
|
|
|
|
2015-09-25 20:45:45 +02:00
|
|
|
wxInputStream* stream = zipfile->GetStream();
|
2008-12-08 15:27:13 +00:00
|
|
|
wxFFileOutputStream* ofile = new wxFFileOutputStream( unzipfilename );
|
2007-09-13 11:55:46 +00:00
|
|
|
|
|
|
|
if( ofile->Ok() )
|
|
|
|
{
|
|
|
|
ofile->Write( *stream );
|
|
|
|
PrintMsg( _( " OK\n" ) );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
PrintMsg( _( " *ERROR*\n" ) );
|
|
|
|
|
|
|
|
delete ofile;
|
|
|
|
delete zipfile;
|
Modular KiCad Blueprint Milestone B), major portions:
*) When kicad.exe closes a project, close any open KIFACEs so that they cannot
get disassociated from their true PROJECT.
*) Allow loading eeschema library editor from kicad.exe
*) Allow loading pcbnew library editor from kicad.exe
*) Rename LIB_COMPONENT to LIB_PART.
*) Add class PART_LIBS, and PART_LIB.
*) Make PART_LIBS non-global, i.e. PROJECT specific.
*) Implement "data on demand" for PART_LIBS
*) Implement "data on demand" for schematic SEARCH_STACK.
*) Use RSTRINGs to retain eeschema editor's notion of last library and part being edited.
*) Get rid of library search on every SCH_COMPONENT::Draw() call, instead use
a weak pointer.
*) Remove all chdir() calls so projects don't need to be CWD.
*) Romove APPEND support from OpenProjectFiles().
*) Make OpenProjectFiles() robust, even for creating new projects.
*) Load EESCHEMA colors in the KIWAY::OnKiwayStart() rather in window open,
and save them in the .eeschema config file, not in the project file.
*) Fix bug with wxDir() while accessing protected dirs in kicad.exe
*) Consolidate template copying into PROJECT class, not in kicad.exe source.
*) Generally untangle eeschema, making its libraries not global but rather
held in the PROJECT.
2014-08-13 15:28:54 -05:00
|
|
|
|
2007-09-13 11:55:46 +00:00
|
|
|
localfilename = zipfilesys.FindNext();
|
|
|
|
}
|
|
|
|
|
|
|
|
PrintMsg( wxT( "** end **\n" ) );
|
2018-02-16 14:20:14 +00:00
|
|
|
|
|
|
|
if( unzipDir == Prj().GetProjectPath() )
|
2019-06-09 14:12:44 +01:00
|
|
|
RefreshProjectTree();
|
2007-06-05 12:10:51 +00:00
|
|
|
}
|
|
|
|
|
2007-09-13 11:55:46 +00:00
|
|
|
|
2011-04-17 15:54:17 +02:00
|
|
|
void KICAD_MANAGER_FRAME::OnArchiveFiles( wxCommandEvent& event )
|
2007-06-05 12:10:51 +00:00
|
|
|
{
|
2014-07-06 23:12:04 -05:00
|
|
|
// List of file extensions to save.
|
2011-09-07 20:41:35 +02:00
|
|
|
static const wxChar* extentionList[] = {
|
2018-08-26 13:35:33 +02:00
|
|
|
wxT( "*.pro" ),
|
|
|
|
wxT( "*.sch" ), wxT( "*.lib" ), wxT( "*.dcm" ), // Schematic related files
|
|
|
|
wxT( "*.cmp" ),
|
|
|
|
wxT( "*.brd" ), wxT( "*.kicad_pcb" ), // Brd files
|
|
|
|
wxT( "*.mod" ), wxT( "*.kicad_mod" ), // fp files
|
2019-01-21 07:26:50 -08:00
|
|
|
wxT( "*.gb?" ), wxT( "*.gbrjob" ), // Gerber files
|
|
|
|
wxT( "*.gko" ), wxT( "*.gm1" ),
|
|
|
|
wxT( "*.gm2" ), wxT( "*.g?" ),
|
|
|
|
wxT( "*.gp1" ), wxT( "*.gp2" ),
|
|
|
|
wxT( "*.gpb" ), wxT( "*.gpt" ),
|
|
|
|
wxT( "*.gt?" ),
|
2019-03-27 16:46:51 +01:00
|
|
|
wxT( "*.pos" ), wxT( "*.drl" ), wxT( "*.nc" ), wxT( "*.xnc" ), // Fab files
|
2019-01-21 07:26:50 -08:00
|
|
|
wxT( "*.d356" ), wxT( "*.rpt" ),
|
|
|
|
wxT( "*.stp" ), wxT( "*.step" ), // 3d files
|
|
|
|
wxT( "*.wrl" ),
|
2018-08-26 13:35:33 +02:00
|
|
|
wxT( "*.net" ), wxT( "*.py" ),
|
|
|
|
wxT( "*.pdf" ), wxT( "*.txt" ), wxT( "*.kicad_wks" ),
|
2019-02-11 07:21:22 -08:00
|
|
|
wxT( "fp-lib-table" ), wxT( "sym-lib-table" )
|
2011-09-07 20:41:35 +02:00
|
|
|
};
|
|
|
|
|
2014-07-06 23:12:04 -05:00
|
|
|
wxString msg;
|
2014-07-07 14:49:14 -05:00
|
|
|
wxFileName fileName = GetProjectFileName();
|
2014-10-17 14:52:11 +02:00
|
|
|
wxString oldCwd = wxGetCwd();
|
2007-09-13 11:55:46 +00:00
|
|
|
|
2009-05-12 17:36:58 +00:00
|
|
|
fileName.SetExt( wxT( "zip" ) );
|
2007-09-13 11:55:46 +00:00
|
|
|
|
2009-04-05 20:49:15 +00:00
|
|
|
wxFileDialog dlg( this, _( "Archive Project Files" ),
|
|
|
|
fileName.GetPath(), fileName.GetFullName(),
|
2017-11-11 19:31:38 -05:00
|
|
|
ZipFileWildcard(), wxFD_SAVE | wxFD_OVERWRITE_PROMPT );
|
2007-09-13 11:55:46 +00:00
|
|
|
|
2009-04-05 20:49:15 +00:00
|
|
|
if( dlg.ShowModal() == wxID_CANCEL )
|
|
|
|
return;
|
2007-09-13 11:55:46 +00:00
|
|
|
|
2009-04-05 20:49:15 +00:00
|
|
|
wxFileName zip = dlg.GetPath();
|
2007-09-13 11:55:46 +00:00
|
|
|
|
2014-10-17 14:52:11 +02:00
|
|
|
wxString currdirname = fileName.GetPathWithSep();
|
2009-04-13 05:58:11 +00:00
|
|
|
wxDir dir( currdirname );
|
2008-12-08 15:27:13 +00:00
|
|
|
|
2017-07-24 09:20:44 +02:00
|
|
|
if( !dir.IsOpened() ) // wxWidgets display a error message on issue.
|
2009-04-05 20:49:15 +00:00
|
|
|
return;
|
2007-09-13 11:55:46 +00:00
|
|
|
|
2014-10-17 14:52:11 +02:00
|
|
|
wxSetWorkingDirectory( currdirname );
|
|
|
|
|
2011-09-07 20:41:35 +02:00
|
|
|
// Prepare the zip file
|
|
|
|
wxString zipfilename = zip.GetFullPath();
|
2008-12-08 15:27:13 +00:00
|
|
|
|
2015-09-25 15:38:09 -04:00
|
|
|
wxFFileOutputStream ostream( zipfilename );
|
2017-07-24 09:20:44 +02:00
|
|
|
|
|
|
|
if( !ostream.IsOk() ) // issue to create the file. Perhaps not writable dir
|
|
|
|
{
|
2017-12-15 12:37:46 +01:00
|
|
|
wxMessageBox( wxString::Format( _( "Unable to create zip archive file \"%s\"" ),
|
2017-08-31 11:15:20 -04:00
|
|
|
zipfilename ) );
|
2017-07-24 09:20:44 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-09-07 20:41:35 +02:00
|
|
|
wxZipOutputStream zipstream( ostream );
|
|
|
|
|
|
|
|
// Build list of filenames to put in zip archive
|
|
|
|
wxString currFilename;
|
2014-07-06 23:12:04 -05:00
|
|
|
|
2014-10-17 14:52:11 +02:00
|
|
|
wxArrayString files;
|
|
|
|
|
2019-01-06 08:43:12 -08:00
|
|
|
for( unsigned ii = 0; ii < arrayDim( extentionList ); ii++ )
|
2014-10-17 14:52:11 +02:00
|
|
|
wxDir::GetAllFiles( currdirname, &files, extentionList[ii] );
|
|
|
|
|
|
|
|
files.Sort();
|
|
|
|
|
|
|
|
int zipBytesCnt = 0;
|
|
|
|
|
|
|
|
for( unsigned ii = 0; ii < files.GetCount(); ii++ )
|
2009-04-05 20:49:15 +00:00
|
|
|
{
|
2014-10-17 14:52:11 +02:00
|
|
|
wxFileSystem fsfile;
|
2007-09-13 11:55:46 +00:00
|
|
|
|
2014-10-17 14:52:11 +02:00
|
|
|
wxFileName curr_fn( files[ii] );
|
|
|
|
curr_fn.MakeRelativeTo( currdirname );
|
|
|
|
currFilename = curr_fn.GetFullPath();
|
2014-07-06 23:12:04 -05:00
|
|
|
|
2017-12-15 12:37:46 +01:00
|
|
|
msg.Printf( _( "Archive file \"%s\"" ), GetChars( currFilename ) );
|
2014-10-17 14:52:11 +02:00
|
|
|
PrintMsg( msg );
|
|
|
|
|
|
|
|
// Read input file and add it to the zip file:
|
2015-09-25 15:38:09 -04:00
|
|
|
wxFSFile* infile = fsfile.OpenFile( currFilename );
|
2014-07-06 23:12:04 -05:00
|
|
|
|
2014-10-17 14:52:11 +02:00
|
|
|
if( infile )
|
|
|
|
{
|
|
|
|
zipstream.PutNextEntry( currFilename, infile->GetModificationTime() );
|
|
|
|
infile->GetStream()->Read( zipstream );
|
|
|
|
zipstream.CloseEntry();
|
|
|
|
int zippedsize = zipstream.GetSize() - zipBytesCnt;
|
|
|
|
zipBytesCnt = zipstream.GetSize();
|
|
|
|
PrintMsg( wxT(" ") );
|
2015-09-25 15:38:09 -04:00
|
|
|
msg.Printf( _( "(%lu bytes, compressed %d bytes)\n" ),
|
|
|
|
(unsigned long)infile->GetStream()->GetSize(), zippedsize );
|
2014-10-17 14:52:11 +02:00
|
|
|
PrintMsg( msg );
|
|
|
|
delete infile;
|
2007-09-13 11:55:46 +00:00
|
|
|
}
|
2014-10-17 14:52:11 +02:00
|
|
|
else
|
2015-09-25 15:38:09 -04:00
|
|
|
PrintMsg( _( " >>Error\n" ) );
|
2007-09-13 11:55:46 +00:00
|
|
|
}
|
2007-06-05 12:10:51 +00:00
|
|
|
|
2011-09-07 20:41:35 +02:00
|
|
|
zipBytesCnt = ostream.GetSize();
|
2014-10-17 14:52:11 +02:00
|
|
|
|
2011-09-07 20:41:35 +02:00
|
|
|
if( zipstream.Close() )
|
2007-09-13 11:55:46 +00:00
|
|
|
{
|
2017-12-15 12:37:46 +01:00
|
|
|
msg.Printf( _( "\nZip archive \"%s\" created (%d bytes)" ),
|
2011-09-07 20:41:35 +02:00
|
|
|
GetChars( zipfilename ), zipBytesCnt );
|
2009-05-12 17:36:58 +00:00
|
|
|
PrintMsg( msg );
|
2007-09-13 11:55:46 +00:00
|
|
|
PrintMsg( wxT( "\n** end **\n" ) );
|
|
|
|
}
|
|
|
|
else
|
2011-09-07 20:41:35 +02:00
|
|
|
{
|
2017-12-15 12:37:46 +01:00
|
|
|
msg.Printf( wxT( "Unable to create archive \"%s\", abort\n" ),
|
2015-09-25 15:38:09 -04:00
|
|
|
GetChars( zipfilename ) );
|
2011-09-07 20:41:35 +02:00
|
|
|
PrintMsg( msg );
|
|
|
|
}
|
2007-09-13 11:55:46 +00:00
|
|
|
|
2014-10-17 14:52:11 +02:00
|
|
|
wxSetWorkingDirectory( oldCwd );
|
2007-06-05 12:10:51 +00:00
|
|
|
}
|