KiCad PCB EDA Suite
Loading...
Searching...
No Matches
jobs_output_archive.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) 2024 Mark Roszko <[email protected]>
5 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
6 *
7 * This program is free software: you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation, either version 3 of the License, or (at your
10 * option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * 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, see <https://www.gnu.org/licenses/>.
19 */
20
22#include <wx/filename.h>
23#include <wx/fs_zip.h>
24#include <wx/wfstream.h>
25#include <wx/zipstrm.h>
26#include <gestfich.h>
27#include <common.h>
29#include <kiplatform/io.h>
30
36
37
39{
40 if( m_outputPath.IsEmpty() )
41 return false;
42
43 return true;
44}
45
46
47bool JOBS_OUTPUT_ARCHIVE::HandleOutputs( const wxString& baseTempPath,
48 PROJECT* aProject,
49 const std::vector<wxString>& aPathsWithOverwriteDisallowed,
50 const std::vector<JOB_OUTPUT>& aOutputsToHandle,
51 std::optional<wxString>& aResolvedOutputPath )
52{
53 bool success = true;
54 aResolvedOutputPath.reset();
55
56 wxString outputPath = ResolveOutputPath( m_outputPath, aProject );
57
58 outputPath = EnsureFileExtension( outputPath, FILEEXT::ArchiveFileExtension );
59
60 // Ensure parent directory exists; matches the behavior of the folder output handler
61 // so that nested destination paths can be created on demand.
62 wxFileName outputFileName( outputPath );
63 wxString parentDir = outputFileName.GetPath();
64
65 if( !parentDir.IsEmpty() && !wxFileName::DirExists( parentDir ) )
66 {
67 if( !wxFileName::Mkdir( parentDir, wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL ) )
68 {
69 aResolvedOutputPath.reset();
70 return false;
71 }
72 }
73
74 wxFFileOutputStream ostream( outputPath );
75
76 if( !ostream.IsOk() )
77 {
78 aResolvedOutputPath.reset();
79 return false;
80 }
81
82 // Use a large I/O buffer to improve compatibility with cloud-synced folders.
83 // See KIPLATFORM::IO::CLOUD_SYNC_BUFFER_SIZE comment for details.
84 if( FILE* fp = ostream.GetFile()->fp() )
85 setvbuf( fp, nullptr, _IOFBF, KIPLATFORM::IO::CLOUD_SYNC_BUFFER_SIZE );
86
87 wxZipOutputStream zipstream( ostream, -1, wxConvUTF8 );
88 wxString errors;
89
90 if( !AddDirectoryToZip( zipstream, baseTempPath, errors ) )
91 success = false;
92
93 if( !zipstream.Close() )
94 success = false;
95
96 if( success )
97 aResolvedOutputPath = outputPath;
98 else
99 aResolvedOutputPath.reset();
100
101 return success;
102}
103
104
105void JOBS_OUTPUT_ARCHIVE::FromJson( const nlohmann::json& j )
106{
107 m_outputPath = j.value( "output_path", "" );
109}
110
111
112void JOBS_OUTPUT_ARCHIVE::ToJson( nlohmann::json& j ) const
113{
114 j["output_path"] = m_outputPath;
115 j["format"] = "zip";
116}
117
118
120{
121 return _( "Archive" );
122}
bool OutputPrecheck() override
Checks if the output process can proceed before doing anything else This can include user prompts.
void FromJson(const nlohmann::json &j) override
void ToJson(nlohmann::json &j) const override
wxString GetDefaultDescription() const override
bool HandleOutputs(const wxString &aBaseTempPath, PROJECT *aProject, const std::vector< wxString > &aPathsWithOverwriteDisallowed, const std::vector< JOB_OUTPUT > &aOutputsToHandle, std::optional< wxString > &aResolvedOutputPath) override
static wxString ResolveOutputPath(const wxString &aPath, const PROJECT *aProject)
Expand variables and a leading '~' in a destination path, then anchor a relative result at the projec...
Container for project specific data.
Definition project.h:63
wxString EnsureFileExtension(const wxString &aFilename, const wxString &aExtension)
It's annoying to throw up nag dialogs when the extension isn't right.
Definition common.cpp:880
#define _(s)
bool AddDirectoryToZip(wxZipOutputStream &aZip, const wxString &aSourceDir, wxString &aErrors, const wxString &aParentDir)
Add a directory and its contents to a zip file.
Definition gestfich.cpp:682
static const std::string ArchiveFileExtension
static constexpr size_t CLOUD_SYNC_BUFFER_SIZE
Buffer size for file I/O operations on cloud-synced folders.
Definition io.h:169
Definition of file extensions used in Kicad.