Delete 3D cache files with GUI settable number of days

KiCad stores 3D cache files but never gets rid of any of them.
This patch adds a new function called 'CleanCacheDir()'.  Any cache
files older than the passed 'number of days' parameter are deleted.

Fixes https://gitlab.com/kicad/code/kicad/-/issues/1928
This commit is contained in:
Peter Montgomery 2020-08-09 10:41:45 +00:00 committed by Ian McInerney
parent 5c3fddffa6
commit 4d460ec073
8 changed files with 302 additions and 11 deletions

View File

@ -57,6 +57,7 @@
#include <filename_resolver.h>
#include <pgm_base.h>
#include <project.h>
#include <settings/common_settings.h>
#include <settings/settings_manager.h>
@ -195,8 +196,18 @@ S3D_CACHE::S3D_CACHE()
S3D_CACHE::~S3D_CACHE()
{
COMMON_SETTINGS* commonSettings = Pgm().GetCommonSettings();
FlushCache();
// We'll delete ".3dc" cache files older than this many days
int clearCacheInterval = commonSettings->m_System.clear_3d_cache_interval;
// An interval of zero means the user doesn't want to ever clear the cache
if( clearCacheInterval > 0 )
CleanCacheDir( clearCacheInterval );
delete m_FNResolver;
delete m_Plugins;
}
@ -677,6 +688,46 @@ S3DMODEL* S3D_CACHE::GetModel( const wxString& aModelFileName )
return mp;
}
void S3D_CACHE::CleanCacheDir( int aNumDaysOld )
{
wxDir dir;
wxString fileSpec = wxT( "*.3dc" );
wxArrayString fileList; // Holds list of ".3dc" files found in cache directory
size_t numFilesFound = 0;
wxFileName thisFile;
wxDateTime lastAccess, thresholdDate;
wxDateSpan durationInDays;
// Calc the threshold date above which we delete cache files
durationInDays.SetDays( aNumDaysOld );
thresholdDate = wxDateTime::Now() - durationInDays;
// If the cache directory can be found and opened, then we'll try and clean it up
if( dir.Open( m_CacheDir ) )
{
thisFile.SetPath( m_CacheDir ); // Set the base path to the cache folder
// Get a list of all the ".3dc" files in the cache directory
numFilesFound = dir.GetAllFiles( m_CacheDir, &fileList, fileSpec );
for( unsigned int i = 0; i < numFilesFound; i++ )
{
// Completes path to specific file so we can get its "last access" date
thisFile.SetFullName( fileList[i] );
// Only get "last access" time to compare against. Don't need the other 2 timestamps.
if( thisFile.GetTimes( &lastAccess, nullptr, nullptr ) )
{
if( lastAccess.IsEarlierThan( thresholdDate ) )
{
// This file is older than the threshold so delete it
wxRemoveFile( thisFile.GetFullPath() );
}
}
}
}
}
S3D_CACHE* PROJECT::Get3DCacheManager( bool aUpdateProjDir )
{

View File

@ -183,6 +183,16 @@ public:
* @return is a pointer to the render data or NULL if not available
*/
S3DMODEL* GetModel( const wxString& aModelFileName );
/**
* Function Delete up old cache files in cache directory
*
* Deletes ".3dc" files in the cache directory that are older than
* "aNumDaysOld".
*
* @param aNumDaysOld is age threshold to delete ".3dc" cache files
*/
void CleanCacheDir( int aNumDaysOld );
};
#endif // CACHE_3D_H

View File

@ -101,6 +101,7 @@ bool PANEL_COMMON_SETTINGS::TransferDataFromWindow()
commonSettings->m_System.autosave_interval = m_SaveTime->GetValue() * 60;
commonSettings->m_System.file_history_size = m_fileHistorySize->GetValue();
commonSettings->m_System.clear_3d_cache_interval = m_Clear3DCacheFilesOlder->GetValue();
commonSettings->m_Graphics.opengl_aa_mode = m_antialiasing->GetSelection();
commonSettings->m_Graphics.cairo_aa_mode = m_antialiasingFallback->GetSelection();
@ -167,6 +168,8 @@ void PANEL_COMMON_SETTINGS::applySettingsToPanel( COMMON_SETTINGS& aSettings )
m_antialiasing->SetSelection( aSettings.m_Graphics.opengl_aa_mode );
m_antialiasingFallback->SetSelection( aSettings.m_Graphics.cairo_aa_mode );
m_Clear3DCacheFilesOlder->SetValue( aSettings.m_System.clear_3d_cache_interval );
int icon_scale_fourths = aSettings.m_Appearance.icon_scale;
if( icon_scale_fourths <= 0 )

View File

@ -50,27 +50,46 @@ PANEL_COMMON_SETTINGS_BASE::PANEL_COMMON_SETTINGS_BASE( wxWindow* parent, wxWind
m_fileHistorySize = new wxSpinCtrl( this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxSP_ARROW_KEYS, 1, 100, 0 );
gbSizer1->Add( m_fileHistorySize, wxGBPosition( 1, 1 ), wxGBSpan( 1, 1 ), wxALIGN_CENTER_VERTICAL, 5 );
wxBoxSizer* bSizer5;
bSizer5 = new wxBoxSizer( wxHORIZONTAL );
m_Clear3DCacheFilesOlder = new wxSpinCtrl( this, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxSP_ARROW_KEYS, 0, 120, 30 );
m_Clear3DCacheFilesOlder->SetToolTip( _("3D cache files older than this are deleted.\nIf set to 0, cache clearing is disabled") );
bSizer5->Add( m_Clear3DCacheFilesOlder, 0, wxALIGN_CENTER_VERTICAL, 5 );
m_staticTextDays = new wxStaticText( this, wxID_ANY, _("Days"), wxDefaultPosition, wxDefaultSize, 0 );
m_staticTextDays->Wrap( -1 );
bSizer5->Add( m_staticTextDays, 0, wxALIGN_CENTER_VERTICAL|wxLEFT|wxRIGHT, 5 );
gbSizer1->Add( bSizer5, wxGBPosition( 2, 1 ), wxGBSpan( 1, 1 ), wxEXPAND, 5 );
wxStaticText* antialiasingLabel;
antialiasingLabel = new wxStaticText( this, wxID_ANY, _("Accelerated graphics:"), wxDefaultPosition, wxDefaultSize, 0 );
antialiasingLabel->Wrap( -1 );
gbSizer1->Add( antialiasingLabel, wxGBPosition( 3, 0 ), wxGBSpan( 1, 1 ), wxALIGN_CENTER_VERTICAL, 5 );
gbSizer1->Add( antialiasingLabel, wxGBPosition( 4, 0 ), wxGBSpan( 1, 1 ), wxALIGN_CENTER_VERTICAL, 5 );
wxString m_antialiasingChoices[] = { _("No Antialiasing"), _("Subpixel Antialiasing (High Quality)"), _("Subpixel Antialiasing (Ultra Quality)"), _("Supersampling (2x)"), _("Supersampling (4x)") };
int m_antialiasingNChoices = sizeof( m_antialiasingChoices ) / sizeof( wxString );
m_antialiasing = new wxChoice( this, wxID_ANY, wxDefaultPosition, wxDefaultSize, m_antialiasingNChoices, m_antialiasingChoices, 0 );
m_antialiasing->SetSelection( 0 );
gbSizer1->Add( m_antialiasing, wxGBPosition( 3, 1 ), wxGBSpan( 1, 2 ), wxALIGN_CENTER_VERTICAL|wxEXPAND, 5 );
gbSizer1->Add( m_antialiasing, wxGBPosition( 4, 1 ), wxGBSpan( 1, 2 ), wxALIGN_CENTER_VERTICAL|wxEXPAND, 5 );
wxStaticText* antialiasingLabel1;
antialiasingLabel1 = new wxStaticText( this, wxID_ANY, _("Fallback graphics:"), wxDefaultPosition, wxDefaultSize, 0 );
antialiasingLabel1->Wrap( -1 );
gbSizer1->Add( antialiasingLabel1, wxGBPosition( 4, 0 ), wxGBSpan( 1, 1 ), wxALIGN_CENTER_VERTICAL, 5 );
gbSizer1->Add( antialiasingLabel1, wxGBPosition( 6, 0 ), wxGBSpan( 1, 1 ), wxALIGN_CENTER_VERTICAL, 5 );
wxString m_antialiasingFallbackChoices[] = { _("No Antialiasing"), _("Fast Antialiasing"), _("Balanced Antialiasing"), _("High Quality Antialiasing") };
int m_antialiasingFallbackNChoices = sizeof( m_antialiasingFallbackChoices ) / sizeof( wxString );
m_antialiasingFallback = new wxChoice( this, wxID_ANY, wxDefaultPosition, wxDefaultSize, m_antialiasingFallbackNChoices, m_antialiasingFallbackChoices, 0 );
m_antialiasingFallback->SetSelection( 0 );
gbSizer1->Add( m_antialiasingFallback, wxGBPosition( 4, 1 ), wxGBSpan( 1, 2 ), wxALIGN_CENTER_VERTICAL|wxEXPAND, 5 );
gbSizer1->Add( m_antialiasingFallback, wxGBPosition( 6, 1 ), wxGBSpan( 1, 2 ), wxALIGN_CENTER_VERTICAL|wxEXPAND, 5 );
m_staticTextClear3DCache = new wxStaticText( this, wxID_ANY, _("3D cache file duration:"), wxDefaultPosition, wxDefaultSize, 0 );
m_staticTextClear3DCache->Wrap( -1 );
gbSizer1->Add( m_staticTextClear3DCache, wxGBPosition( 2, 0 ), wxGBSpan( 1, 1 ), wxALIGN_CENTER_VERTICAL|wxTOP, 5 );
gbSizer1->AddGrowableCol( 1 );
@ -265,7 +284,6 @@ PANEL_COMMON_SETTINGS_BASE::PANEL_COMMON_SETTINGS_BASE( wxWindow* parent, wxWind
this->SetSizer( bPanelSizer );
this->Layout();
bPanelSizer->Fit( this );
// Connect Events
m_textEditorBtn->Connect( wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler( PANEL_COMMON_SETTINGS_BASE::OnTextEditorClick ), NULL, this );

View File

@ -43,7 +43,7 @@
<property name="minimum_size"></property>
<property name="name">PANEL_COMMON_SETTINGS_BASE</property>
<property name="pos"></property>
<property name="size">-1,-1</property>
<property name="size">838,485</property>
<property name="subclass">RESETTABLE_PANEL; widgets/resettable_panel.h; Not forward_declare</property>
<property name="tooltip"></property>
<property name="window_extra_style"></property>
@ -408,12 +408,149 @@
<property name="window_style"></property>
</object>
</object>
<object class="gbsizeritem" expanded="1">
<property name="border">5</property>
<property name="colspan">1</property>
<property name="column">1</property>
<property name="flag">wxEXPAND</property>
<property name="row">2</property>
<property name="rowspan">1</property>
<object class="wxBoxSizer" expanded="1">
<property name="minimum_size"></property>
<property name="name">bSizer5</property>
<property name="orient">wxHORIZONTAL</property>
<property name="permission">none</property>
<object class="sizeritem" expanded="1">
<property name="border">5</property>
<property name="flag">wxALIGN_CENTER_VERTICAL</property>
<property name="proportion">0</property>
<object class="wxSpinCtrl" expanded="1">
<property name="BottomDockable">1</property>
<property name="LeftDockable">1</property>
<property name="RightDockable">1</property>
<property name="TopDockable">1</property>
<property name="aui_layer"></property>
<property name="aui_name"></property>
<property name="aui_position"></property>
<property name="aui_row"></property>
<property name="best_size"></property>
<property name="bg"></property>
<property name="caption"></property>
<property name="caption_visible">1</property>
<property name="center_pane">0</property>
<property name="close_button">1</property>
<property name="context_help"></property>
<property name="context_menu">1</property>
<property name="default_pane">0</property>
<property name="dock">Dock</property>
<property name="dock_fixed">0</property>
<property name="docking">Left</property>
<property name="enabled">1</property>
<property name="fg"></property>
<property name="floatable">1</property>
<property name="font"></property>
<property name="gripper">0</property>
<property name="hidden">0</property>
<property name="id">wxID_ANY</property>
<property name="initial">30</property>
<property name="max">120</property>
<property name="max_size"></property>
<property name="maximize_button">0</property>
<property name="maximum_size"></property>
<property name="min">0</property>
<property name="min_size"></property>
<property name="minimize_button">0</property>
<property name="minimum_size"></property>
<property name="moveable">1</property>
<property name="name">m_Clear3DCacheFilesOlder</property>
<property name="pane_border">1</property>
<property name="pane_position"></property>
<property name="pane_size"></property>
<property name="permission">protected</property>
<property name="pin_button">1</property>
<property name="pos"></property>
<property name="resize">Resizable</property>
<property name="show">1</property>
<property name="size"></property>
<property name="style">wxSP_ARROW_KEYS</property>
<property name="subclass">; ; forward_declare</property>
<property name="toolbar_pane">0</property>
<property name="tooltip">3D cache files older than this are deleted.&#x0A;If set to 0, cache clearing is disabled</property>
<property name="value"></property>
<property name="window_extra_style"></property>
<property name="window_name"></property>
<property name="window_style"></property>
</object>
</object>
<object class="sizeritem" expanded="1">
<property name="border">5</property>
<property name="flag">wxALIGN_CENTER_VERTICAL|wxLEFT|wxRIGHT</property>
<property name="proportion">0</property>
<object class="wxStaticText" expanded="1">
<property name="BottomDockable">1</property>
<property name="LeftDockable">1</property>
<property name="RightDockable">1</property>
<property name="TopDockable">1</property>
<property name="aui_layer"></property>
<property name="aui_name"></property>
<property name="aui_position"></property>
<property name="aui_row"></property>
<property name="best_size"></property>
<property name="bg"></property>
<property name="caption"></property>
<property name="caption_visible">1</property>
<property name="center_pane">0</property>
<property name="close_button">1</property>
<property name="context_help"></property>
<property name="context_menu">1</property>
<property name="default_pane">0</property>
<property name="dock">Dock</property>
<property name="dock_fixed">0</property>
<property name="docking">Left</property>
<property name="enabled">1</property>
<property name="fg"></property>
<property name="floatable">1</property>
<property name="font"></property>
<property name="gripper">0</property>
<property name="hidden">0</property>
<property name="id">wxID_ANY</property>
<property name="label">Days</property>
<property name="markup">0</property>
<property name="max_size"></property>
<property name="maximize_button">0</property>
<property name="maximum_size"></property>
<property name="min_size"></property>
<property name="minimize_button">0</property>
<property name="minimum_size"></property>
<property name="moveable">1</property>
<property name="name">m_staticTextDays</property>
<property name="pane_border">1</property>
<property name="pane_position"></property>
<property name="pane_size"></property>
<property name="permission">protected</property>
<property name="pin_button">1</property>
<property name="pos"></property>
<property name="resize">Resizable</property>
<property name="show">1</property>
<property name="size"></property>
<property name="style"></property>
<property name="subclass">; ; forward_declare</property>
<property name="toolbar_pane">0</property>
<property name="tooltip"></property>
<property name="window_extra_style"></property>
<property name="window_name"></property>
<property name="window_style"></property>
<property name="wrap">-1</property>
</object>
</object>
</object>
</object>
<object class="gbsizeritem" expanded="1">
<property name="border">5</property>
<property name="colspan">1</property>
<property name="column">0</property>
<property name="flag">wxALIGN_CENTER_VERTICAL</property>
<property name="row">3</property>
<property name="row">4</property>
<property name="rowspan">1</property>
<object class="wxStaticText" expanded="1">
<property name="BottomDockable">1</property>
@ -477,7 +614,7 @@
<property name="colspan">2</property>
<property name="column">1</property>
<property name="flag">wxALIGN_CENTER_VERTICAL|wxEXPAND</property>
<property name="row">3</property>
<property name="row">4</property>
<property name="rowspan">1</property>
<object class="wxChoice" expanded="1">
<property name="BottomDockable">1</property>
@ -544,7 +681,7 @@
<property name="colspan">1</property>
<property name="column">0</property>
<property name="flag">wxALIGN_CENTER_VERTICAL</property>
<property name="row">4</property>
<property name="row">6</property>
<property name="rowspan">1</property>
<object class="wxStaticText" expanded="1">
<property name="BottomDockable">1</property>
@ -608,7 +745,7 @@
<property name="colspan">2</property>
<property name="column">1</property>
<property name="flag">wxALIGN_CENTER_VERTICAL|wxEXPAND</property>
<property name="row">4</property>
<property name="row">6</property>
<property name="rowspan">1</property>
<object class="wxChoice" expanded="1">
<property name="BottomDockable">1</property>
@ -670,6 +807,70 @@
<property name="window_style"></property>
</object>
</object>
<object class="gbsizeritem" expanded="1">
<property name="border">5</property>
<property name="colspan">1</property>
<property name="column">0</property>
<property name="flag">wxALIGN_CENTER_VERTICAL|wxTOP</property>
<property name="row">2</property>
<property name="rowspan">1</property>
<object class="wxStaticText" expanded="1">
<property name="BottomDockable">1</property>
<property name="LeftDockable">1</property>
<property name="RightDockable">1</property>
<property name="TopDockable">1</property>
<property name="aui_layer"></property>
<property name="aui_name"></property>
<property name="aui_position"></property>
<property name="aui_row"></property>
<property name="best_size"></property>
<property name="bg"></property>
<property name="caption"></property>
<property name="caption_visible">1</property>
<property name="center_pane">0</property>
<property name="close_button">1</property>
<property name="context_help"></property>
<property name="context_menu">1</property>
<property name="default_pane">0</property>
<property name="dock">Dock</property>
<property name="dock_fixed">0</property>
<property name="docking">Left</property>
<property name="enabled">1</property>
<property name="fg"></property>
<property name="floatable">1</property>
<property name="font"></property>
<property name="gripper">0</property>
<property name="hidden">0</property>
<property name="id">wxID_ANY</property>
<property name="label">3D cache file duration:</property>
<property name="markup">0</property>
<property name="max_size"></property>
<property name="maximize_button">0</property>
<property name="maximum_size"></property>
<property name="min_size"></property>
<property name="minimize_button">0</property>
<property name="minimum_size"></property>
<property name="moveable">1</property>
<property name="name">m_staticTextClear3DCache</property>
<property name="pane_border">1</property>
<property name="pane_position"></property>
<property name="pane_size"></property>
<property name="permission">protected</property>
<property name="pin_button">1</property>
<property name="pos"></property>
<property name="resize">Resizable</property>
<property name="show">1</property>
<property name="size"></property>
<property name="style"></property>
<property name="subclass">; ; forward_declare</property>
<property name="toolbar_pane">0</property>
<property name="tooltip"></property>
<property name="window_extra_style"></property>
<property name="window_name"></property>
<property name="window_style"></property>
<property name="wrap">-1</property>
</object>
</object>
</object>
</object>
<object class="sizeritem" expanded="1">

View File

@ -48,8 +48,11 @@ class PANEL_COMMON_SETTINGS_BASE : public RESETTABLE_PANEL
wxSpinCtrl* m_SaveTime;
wxStaticText* m_staticTextFileHistorySize;
wxSpinCtrl* m_fileHistorySize;
wxSpinCtrl* m_Clear3DCacheFilesOlder;
wxStaticText* m_staticTextDays;
wxChoice* m_antialiasing;
wxChoice* m_antialiasingFallback;
wxStaticText* m_staticTextClear3DCache;
wxTextCtrl* m_textEditorPath;
wxBitmapButton* m_textEditorBtn;
wxRadioButton* m_defaultPDFViewer;
@ -90,7 +93,7 @@ class PANEL_COMMON_SETTINGS_BASE : public RESETTABLE_PANEL
public:
PANEL_COMMON_SETTINGS_BASE( wxWindow* parent, wxWindowID id = wxID_ANY, const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxSize( -1,-1 ), long style = wxTAB_TRAVERSAL, const wxString& name = wxEmptyString );
PANEL_COMMON_SETTINGS_BASE( wxWindow* parent, wxWindowID id = wxID_ANY, const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxSize( 838,485 ), long style = wxTAB_TRAVERSAL, const wxString& name = wxEmptyString );
~PANEL_COMMON_SETTINGS_BASE();
};

View File

@ -172,6 +172,10 @@ COMMON_SETTINGS::COMMON_SETTINGS() :
m_params.emplace_back( new PARAM<wxString>( "system.working_dir",
&m_System.working_dir, "" ) );
m_params.emplace_back( new PARAM<int>( "system.clear_3d_cache_interval",
&m_System.clear_3d_cache_interval, 30 ) );
}

View File

@ -89,6 +89,7 @@ public:
wxString pdf_viewer_name;
bool use_system_pdf_viewer;
wxString working_dir;
int clear_3d_cache_interval;
};
COMMON_SETTINGS();