66using System . Windows ;
77using CommunityToolkit . Mvvm . Input ;
88using Flow . Launcher . Core ;
9- using Flow . Launcher . Core . Resource ;
109using Flow . Launcher . Infrastructure ;
1110using Flow . Launcher . Infrastructure . Logger ;
1211using Flow . Launcher . Infrastructure . UserSettings ;
@@ -16,6 +15,8 @@ namespace Flow.Launcher.SettingPages.ViewModels;
1615
1716public partial class SettingsPaneAboutViewModel : BaseModel
1817{
18+ private static readonly string ClassName = nameof ( SettingsPaneAboutViewModel ) ;
19+
1920 private readonly Settings _settings ;
2021 private readonly Updater _updater ;
2122
@@ -24,7 +25,16 @@ public string LogFolderSize
2425 get
2526 {
2627 var size = GetLogFiles ( ) . Sum ( file => file . Length ) ;
27- return $ "{ InternationalizationManager . Instance . GetTranslation ( "clearlogfolder" ) } ({ BytesToReadableString ( size ) } )";
28+ return $ "{ App . API . GetTranslation ( "clearlogfolder" ) } ({ BytesToReadableString ( size ) } )";
29+ }
30+ }
31+
32+ public string CacheFolderSize
33+ {
34+ get
35+ {
36+ var size = GetCacheFiles ( ) . Sum ( file => file . Length ) ;
37+ return $ "{ App . API . GetTranslation ( "clearcachefolder" ) } ({ BytesToReadableString ( size ) } )";
2838 }
2939 }
3040
@@ -42,7 +52,7 @@ public string LogFolderSize
4252 } ;
4353
4454 public string ActivatedTimes => string . Format (
45- InternationalizationManager . Instance . GetTranslation ( "about_activate_times" ) ,
55+ App . API . GetTranslation ( "about_activate_times" ) ,
4656 _settings . ActivateTimes
4757 ) ;
4858
@@ -88,14 +98,35 @@ private void OpenWelcomeWindow()
8898 private void AskClearLogFolderConfirmation ( )
8999 {
90100 var confirmResult = App . API . ShowMsgBox (
91- InternationalizationManager . Instance . GetTranslation ( "clearlogfolderMessage" ) ,
92- InternationalizationManager . Instance . GetTranslation ( "clearlogfolder" ) ,
101+ App . API . GetTranslation ( "clearlogfolderMessage" ) ,
102+ App . API . GetTranslation ( "clearlogfolder" ) ,
93103 MessageBoxButton . YesNo
94104 ) ;
95105
96106 if ( confirmResult == MessageBoxResult . Yes )
97107 {
98- ClearLogFolder ( ) ;
108+ if ( ! ClearLogFolder ( ) )
109+ {
110+ App . API . ShowMsgBox ( App . API . GetTranslation ( "clearfolderfailMessage" ) ) ;
111+ }
112+ }
113+ }
114+
115+ [ RelayCommand ]
116+ private void AskClearCacheFolderConfirmation ( )
117+ {
118+ var confirmResult = App . API . ShowMsgBox (
119+ App . API . GetTranslation ( "clearcachefolderMessage" ) ,
120+ App . API . GetTranslation ( "clearcachefolder" ) ,
121+ MessageBoxButton . YesNo
122+ ) ;
123+
124+ if ( confirmResult == MessageBoxResult . Yes )
125+ {
126+ if ( ! ClearCacheFolder ( ) )
127+ {
128+ App . API . ShowMsgBox ( App . API . GetTranslation ( "clearfolderfailMessage" ) ) ;
129+ }
99130 }
100131 }
101132
@@ -113,29 +144,54 @@ private void OpenParentOfSettingsFolder(object parameter)
113144 App . API . OpenDirectory ( parentFolderPath ) ;
114145 }
115146
116-
117147 [ RelayCommand ]
118148 private void OpenLogsFolder ( )
119149 {
120150 App . API . OpenDirectory ( GetLogDir ( Constant . Version ) . FullName ) ;
121151 }
122152
123153 [ RelayCommand ]
124- private Task UpdateApp ( ) => _updater . UpdateAppAsync ( false ) ;
154+ private Task UpdateAppAsync ( ) => _updater . UpdateAppAsync ( false ) ;
125155
126- private void ClearLogFolder ( )
156+ private bool ClearLogFolder ( )
127157 {
158+ var success = true ;
128159 var logDirectory = GetLogDir ( ) ;
129160 var logFiles = GetLogFiles ( ) ;
130161
131- logFiles . ForEach ( f => f . Delete ( ) ) ;
162+ logFiles . ForEach ( f =>
163+ {
164+ try
165+ {
166+ f . Delete ( ) ;
167+ }
168+ catch ( Exception e )
169+ {
170+ App . API . LogException ( ClassName , $ "Failed to delete log file: { f . Name } ", e ) ;
171+ success = false ;
172+ }
173+ } ) ;
132174
133175 logDirectory . EnumerateDirectories ( "*" , SearchOption . TopDirectoryOnly )
176+ // Do not clean log files of current version
134177 . Where ( dir => ! Constant . Version . Equals ( dir . Name ) )
135178 . ToList ( )
136- . ForEach ( dir => dir . Delete ( ) ) ;
179+ . ForEach ( dir =>
180+ {
181+ try
182+ {
183+ dir . Delete ( true ) ;
184+ }
185+ catch ( Exception e )
186+ {
187+ App . API . LogException ( ClassName , $ "Failed to delete log directory: { dir . Name } ", e ) ;
188+ success = false ;
189+ }
190+ } ) ;
137191
138192 OnPropertyChanged ( nameof ( LogFolderSize ) ) ;
193+
194+ return success ;
139195 }
140196
141197 private static DirectoryInfo GetLogDir ( string version = "" )
@@ -148,6 +204,55 @@ private static List<FileInfo> GetLogFiles(string version = "")
148204 return GetLogDir ( version ) . EnumerateFiles ( "*" , SearchOption . AllDirectories ) . ToList ( ) ;
149205 }
150206
207+ private bool ClearCacheFolder ( )
208+ {
209+ var success = true ;
210+ var cacheDirectory = GetCacheDir ( ) ;
211+ var cacheFiles = GetCacheFiles ( ) ;
212+
213+ cacheFiles . ForEach ( f =>
214+ {
215+ try
216+ {
217+ f . Delete ( ) ;
218+ }
219+ catch ( Exception e )
220+ {
221+ App . API . LogException ( ClassName , $ "Failed to delete cache file: { f . Name } ", e ) ;
222+ success = false ;
223+ }
224+ } ) ;
225+
226+ cacheDirectory . EnumerateDirectories ( "*" , SearchOption . TopDirectoryOnly )
227+ . ToList ( )
228+ . ForEach ( dir =>
229+ {
230+ try
231+ {
232+ dir . Delete ( true ) ;
233+ }
234+ catch ( Exception e )
235+ {
236+ App . API . LogException ( ClassName , $ "Failed to delete cache directory: { dir . Name } ", e ) ;
237+ success = false ;
238+ }
239+ } ) ;
240+
241+ OnPropertyChanged ( nameof ( CacheFolderSize ) ) ;
242+
243+ return success ;
244+ }
245+
246+ private static DirectoryInfo GetCacheDir ( )
247+ {
248+ return new DirectoryInfo ( DataLocation . CacheDirectory ) ;
249+ }
250+
251+ private static List < FileInfo > GetCacheFiles ( )
252+ {
253+ return GetCacheDir ( ) . EnumerateFiles ( "*" , SearchOption . AllDirectories ) . ToList ( ) ;
254+ }
255+
151256 private static string BytesToReadableString ( long bytes )
152257 {
153258 const int scale = 1024 ;
@@ -156,8 +261,7 @@ private static string BytesToReadableString(long bytes)
156261
157262 foreach ( string order in orders )
158263 {
159- if ( bytes > max )
160- return $ "{ decimal . Divide ( bytes , max ) : ##.##} { order } ";
264+ if ( bytes > max ) return $ "{ decimal . Divide ( bytes , max ) : ##.##} { order } ";
161265
162266 max /= scale ;
163267 }
0 commit comments