Skip to content

Commit b4473e5

Browse files
committed
use gjs clear cache instead of cli utilities
1 parent 6dd82ef commit b4473e5

File tree

4 files changed

+57
-60
lines changed

4 files changed

+57
-60
lines changed

build.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,9 @@ if [[ " ${PARAMS[*]} " =~ " -r " ]]; then
6161
glib-compile-schemas schemas/;
6262
copy;
6363
restart;
64+
fi
65+
66+
if [[ " ${PARAMS[*]} " =~ " -c " ]]; then
67+
glib-compile-schemas schemas/;
68+
copy;
6469
fi

metadata.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@
1010
],
1111
"url": "https://github.com/cliffniff/media-controls",
1212
"uuid": "mediacontrols@cliffniff.github.com",
13-
"version": 27
13+
"version": 29
1414
}

prefs.js

Lines changed: 51 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ const Me = ExtensionUtils.getCurrentExtension();
88
const Gettext = imports.gettext.domain("mediacontrols");
99
const _ = Gettext.gettext;
1010

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");
1214

1315
function init() {
1416
ExtensionUtils.initTranslations("mediacontrols");
@@ -798,10 +800,7 @@ class AdwPrefs {
798800
adwrow.add_suffix(blacklistentry);
799801
adwrow.activatable_widget = blacklistentry;
800802
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");
805804
blacklistbuttonadd.set_valign(Gtk.Align.CENTER);
806805
adwrow.add_suffix(blacklistbuttonadd);
807806
adwrow.activatable_widget = blacklistbuttonadd;
@@ -846,29 +845,70 @@ class AdwPrefs {
846845
}
847846

848847
async _getCacheSize() {
849-
// Command: du -hs [data_directory]/media-controls | awk '{NF=1}1'
850848
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);
854869
} catch (error) {
855870
logError(error);
856871
}
857872
}
858873

859874
async _clearcache(widgetCacheSize, clearcachespinner) {
860-
let dir = GLib.get_user_config_dir() + "/media-controls";
861875
try {
862876
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+
864896
widgetCacheSize.set_text(await this._getCacheSize());
865897
clearcachespinner.stop();
866898
} catch (error) {
899+
logError(error);
867900
widgetCacheSize.set_text(_("Failed to clear cache"));
868901
clearcachespinner.stop();
869902
}
870903
}
871904

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+
872912
_onclearcacheclicked(widgetCacheSize, clearcachespinner) {
873913
this._clearcache(widgetCacheSize, clearcachespinner);
874914
}

utils.js

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -108,51 +108,3 @@ var getRequest = (url) => {
108108
});
109109
});
110110
};
111-
112-
/**
113-
* Executes a shell command asynchronously
114-
* @param {Array<string>} argv array of arguments
115-
* @param {string?} input input to be given to the shell command
116-
* @param {boolean?} cancellable whether the operation is cancellable
117-
* @returns
118-
*/
119-
var execCommunicate = async (argv, input = null, cancellable = null) => {
120-
let cancelId = 0;
121-
let flags = Gio.SubprocessFlags.STDOUT_PIPE | Gio.SubprocessFlags.STDERR_PIPE;
122-
123-
if (input !== null) flags |= Gio.SubprocessFlags.STDIN_PIPE;
124-
125-
let proc = new Gio.Subprocess({
126-
argv: argv,
127-
flags: flags,
128-
});
129-
proc.init(cancellable);
130-
131-
if (cancellable instanceof Gio.Cancellable) {
132-
cancelId = cancellable.connect(() => proc.force_exit());
133-
}
134-
135-
return new Promise((resolve, reject) => {
136-
proc.communicate_utf8_async(input, null, (proc, res) => {
137-
try {
138-
let [, stdout, stderr] = proc.communicate_utf8_finish(res);
139-
let status = proc.get_exit_status();
140-
141-
if (status !== 0) {
142-
throw new Gio.IOErrorEnum({
143-
code: Gio.io_error_from_errno(status),
144-
message: stderr ? stderr.trim() : GLib.strerror(status),
145-
});
146-
}
147-
148-
resolve(stdout.trim());
149-
} catch (e) {
150-
reject(e);
151-
} finally {
152-
if (cancelId > 0) {
153-
cancellable.disconnect(cancelId);
154-
}
155-
}
156-
});
157-
});
158-
};

0 commit comments

Comments
 (0)