@@ -8,7 +8,9 @@ const Me = ExtensionUtils.getCurrentExtension();
8
8
const Gettext = imports . gettext . domain ( "mediacontrols" ) ;
9
9
const _ = Gettext . gettext ;
10
10
11
- const { execCommunicate } = Me . imports . utils ;
11
+ Gio . _promisify ( Gio . File . prototype , "query_info_async" ) ;
12
+ Gio . _promisify ( Gio . File . prototype , "enumerate_children_async" ) ;
13
+ Gio . _promisify ( Gio . File . prototype , "delete_async" ) ;
12
14
13
15
function init ( ) {
14
16
ExtensionUtils . initTranslations ( "mediacontrols" ) ;
@@ -798,10 +800,7 @@ class AdwPrefs {
798
800
adwrow . add_suffix ( blacklistentry ) ;
799
801
adwrow . activatable_widget = blacklistentry ;
800
802
group2 . add ( adwrow ) ;
801
- const blacklistbuttonadd = Gtk . Button . new_from_icon_name (
802
- "list-add-symbolic" ,
803
- Gtk . IconSize . BUTTON || Gtk . IconSize . NORMAL
804
- ) ;
803
+ const blacklistbuttonadd = Gtk . Button . new_from_icon_name ( "list-add-symbolic" ) ;
805
804
blacklistbuttonadd . set_valign ( Gtk . Align . CENTER ) ;
806
805
adwrow . add_suffix ( blacklistbuttonadd ) ;
807
806
adwrow . activatable_widget = blacklistbuttonadd ;
@@ -846,29 +845,70 @@ class AdwPrefs {
846
845
}
847
846
848
847
async _getCacheSize ( ) {
849
- // Command: du -hs [data_directory]/media-controls | awk '{NF=1}1'
850
848
try {
851
- let dir = GLib . get_user_config_dir ( ) + "/media-controls" ;
852
- const result = await execCommunicate ( [ "/bin/bash" , "-c" , `du -hs ${ dir } | awk '{NF=1}1'` ] ) ;
853
- return result || "0K" ;
849
+ const path = GLib . get_user_config_dir ( ) + "/media-controls/cache" ;
850
+ const directory = Gio . File . new_for_path ( path ) ;
851
+ const iterator = await directory . enumerate_children_async (
852
+ "standard::*" ,
853
+ Gio . FileQueryInfoFlags . NOFOLLOW_SYMLINKS ,
854
+ GLib . PRIORITY_DEFAULT ,
855
+ null
856
+ ) ;
857
+
858
+ let sizeInBytes = 0 ;
859
+
860
+ for await ( const fileInfo of iterator ) {
861
+ const fileType = fileInfo . get_file_type ( ) ;
862
+ if ( fileType === Gio . FileType . REGULAR ) {
863
+ const fileSize = fileInfo . get_size ( ) ;
864
+ sizeInBytes += fileSize ;
865
+ }
866
+ }
867
+
868
+ return this . _bytesToSize ( sizeInBytes ) ;
854
869
} catch ( error ) {
855
870
logError ( error ) ;
856
871
}
857
872
}
858
873
859
874
async _clearcache ( widgetCacheSize , clearcachespinner ) {
860
- let dir = GLib . get_user_config_dir ( ) + "/media-controls" ;
861
875
try {
862
876
clearcachespinner . start ( ) ;
863
- await execCommunicate ( [ "rm" , "-r" , dir ] ) ;
877
+
878
+ const path = GLib . get_user_config_dir ( ) + "/media-controls/cache" ;
879
+ const directory = Gio . File . new_for_path ( path ) ;
880
+ const iterator = await directory . enumerate_children_async (
881
+ "standard::*" ,
882
+ Gio . FileQueryInfoFlags . NOFOLLOW_SYMLINKS ,
883
+ GLib . PRIORITY_DEFAULT ,
884
+ null
885
+ ) ;
886
+
887
+ const promises = [ ] ;
888
+
889
+ for await ( const fileInfo of iterator ) {
890
+ const file = iterator . get_child ( fileInfo ) ;
891
+ promises . push ( file . delete_async ( GLib . PRIORITY_DEFAULT , null ) ) ;
892
+ }
893
+
894
+ await Promise . all ( promises ) ;
895
+
864
896
widgetCacheSize . set_text ( await this . _getCacheSize ( ) ) ;
865
897
clearcachespinner . stop ( ) ;
866
898
} catch ( error ) {
899
+ logError ( error ) ;
867
900
widgetCacheSize . set_text ( _ ( "Failed to clear cache" ) ) ;
868
901
clearcachespinner . stop ( ) ;
869
902
}
870
903
}
871
904
905
+ _bytesToSize ( bytes ) {
906
+ const sizes = [ "Bytes" , "KB" , "MB" , "GB" , "TB" ] ;
907
+ if ( bytes === 0 ) return "0 Bytes" ;
908
+ const i = parseInt ( Math . floor ( Math . log ( bytes ) / Math . log ( 1024 ) ) ) ;
909
+ return Math . round ( bytes / Math . pow ( 1024 , i ) ) + " " + sizes [ i ] ;
910
+ }
911
+
872
912
_onclearcacheclicked ( widgetCacheSize , clearcachespinner ) {
873
913
this . _clearcache ( widgetCacheSize , clearcachespinner ) ;
874
914
}
0 commit comments