Skip to content

Commit

Permalink
use gjs clear cache instead of cli utilities
Browse files Browse the repository at this point in the history
  • Loading branch information
sakithb committed Oct 24, 2023
1 parent 6dd82ef commit b4473e5
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 60 deletions.
5 changes: 5 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,9 @@ if [[ " ${PARAMS[*]} " =~ " -r " ]]; then
glib-compile-schemas schemas/;
copy;
restart;
fi

if [[ " ${PARAMS[*]} " =~ " -c " ]]; then
glib-compile-schemas schemas/;
copy;
fi
2 changes: 1 addition & 1 deletion metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
],
"url": "https://github.com/cliffniff/media-controls",
"uuid": "[email protected]",
"version": 27
"version": 29
}
62 changes: 51 additions & 11 deletions prefs.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ const Me = ExtensionUtils.getCurrentExtension();
const Gettext = imports.gettext.domain("mediacontrols");
const _ = Gettext.gettext;

const { execCommunicate } = Me.imports.utils;
Gio._promisify(Gio.File.prototype, "query_info_async");
Gio._promisify(Gio.File.prototype, "enumerate_children_async");
Gio._promisify(Gio.File.prototype, "delete_async");

function init() {
ExtensionUtils.initTranslations("mediacontrols");
Expand Down Expand Up @@ -798,10 +800,7 @@ class AdwPrefs {
adwrow.add_suffix(blacklistentry);
adwrow.activatable_widget = blacklistentry;
group2.add(adwrow);
const blacklistbuttonadd = Gtk.Button.new_from_icon_name(
"list-add-symbolic",
Gtk.IconSize.BUTTON || Gtk.IconSize.NORMAL
);
const blacklistbuttonadd = Gtk.Button.new_from_icon_name("list-add-symbolic");
blacklistbuttonadd.set_valign(Gtk.Align.CENTER);
adwrow.add_suffix(blacklistbuttonadd);
adwrow.activatable_widget = blacklistbuttonadd;
Expand Down Expand Up @@ -846,29 +845,70 @@ class AdwPrefs {
}

async _getCacheSize() {
// Command: du -hs [data_directory]/media-controls | awk '{NF=1}1'
try {
let dir = GLib.get_user_config_dir() + "/media-controls";
const result = await execCommunicate(["/bin/bash", "-c", `du -hs ${dir} | awk '{NF=1}1'`]);
return result || "0K";
const path = GLib.get_user_config_dir() + "/media-controls/cache";
const directory = Gio.File.new_for_path(path);
const iterator = await directory.enumerate_children_async(
"standard::*",
Gio.FileQueryInfoFlags.NOFOLLOW_SYMLINKS,
GLib.PRIORITY_DEFAULT,
null
);

let sizeInBytes = 0;

for await (const fileInfo of iterator) {
const fileType = fileInfo.get_file_type();
if (fileType === Gio.FileType.REGULAR) {
const fileSize = fileInfo.get_size();
sizeInBytes += fileSize;
}
}

return this._bytesToSize(sizeInBytes);
} catch (error) {
logError(error);
}
}

async _clearcache(widgetCacheSize, clearcachespinner) {
let dir = GLib.get_user_config_dir() + "/media-controls";
try {
clearcachespinner.start();
await execCommunicate(["rm", "-r", dir]);

const path = GLib.get_user_config_dir() + "/media-controls/cache";
const directory = Gio.File.new_for_path(path);
const iterator = await directory.enumerate_children_async(
"standard::*",
Gio.FileQueryInfoFlags.NOFOLLOW_SYMLINKS,
GLib.PRIORITY_DEFAULT,
null
);

const promises = [];

for await (const fileInfo of iterator) {
const file = iterator.get_child(fileInfo);
promises.push(file.delete_async(GLib.PRIORITY_DEFAULT, null));
}

await Promise.all(promises);

widgetCacheSize.set_text(await this._getCacheSize());
clearcachespinner.stop();
} catch (error) {
logError(error);
widgetCacheSize.set_text(_("Failed to clear cache"));
clearcachespinner.stop();
}
}

_bytesToSize(bytes) {
const sizes = ["Bytes", "KB", "MB", "GB", "TB"];
if (bytes === 0) return "0 Bytes";
const i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)));
return Math.round(bytes / Math.pow(1024, i)) + " " + sizes[i];
}

_onclearcacheclicked(widgetCacheSize, clearcachespinner) {
this._clearcache(widgetCacheSize, clearcachespinner);
}
Expand Down
48 changes: 0 additions & 48 deletions utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,51 +108,3 @@ var getRequest = (url) => {
});
});
};

/**
* Executes a shell command asynchronously
* @param {Array<string>} argv array of arguments
* @param {string?} input input to be given to the shell command
* @param {boolean?} cancellable whether the operation is cancellable
* @returns
*/
var execCommunicate = async (argv, input = null, cancellable = null) => {
let cancelId = 0;
let flags = Gio.SubprocessFlags.STDOUT_PIPE | Gio.SubprocessFlags.STDERR_PIPE;

if (input !== null) flags |= Gio.SubprocessFlags.STDIN_PIPE;

let proc = new Gio.Subprocess({
argv: argv,
flags: flags,
});
proc.init(cancellable);

if (cancellable instanceof Gio.Cancellable) {
cancelId = cancellable.connect(() => proc.force_exit());
}

return new Promise((resolve, reject) => {
proc.communicate_utf8_async(input, null, (proc, res) => {
try {
let [, stdout, stderr] = proc.communicate_utf8_finish(res);
let status = proc.get_exit_status();

if (status !== 0) {
throw new Gio.IOErrorEnum({
code: Gio.io_error_from_errno(status),
message: stderr ? stderr.trim() : GLib.strerror(status),
});
}

resolve(stdout.trim());
} catch (e) {
reject(e);
} finally {
if (cancelId > 0) {
cancellable.disconnect(cancelId);
}
}
});
});
};

0 comments on commit b4473e5

Please sign in to comment.