summaryrefslogtreecommitdiff
path: root/lib/astal/io/file.vala
diff options
context:
space:
mode:
authorkotontrion <[email protected]>2024-10-29 13:50:41 +0100
committerkotontrion <[email protected]>2024-10-29 13:50:41 +0100
commit57f20666e716fde56579b8aa638eed1264f793de (patch)
tree59b2ebbd770c80049cea4df82109d28f617675fe /lib/astal/io/file.vala
parent4d9ae88b0bab75779876d465f986791d052414ca (diff)
parent7e484188e7492ac7945c854bcc3f26cec1863c91 (diff)
Merge branch 'main' into feat/cava
Diffstat (limited to 'lib/astal/io/file.vala')
-rw-r--r--lib/astal/io/file.vala98
1 files changed, 98 insertions, 0 deletions
diff --git a/lib/astal/io/file.vala b/lib/astal/io/file.vala
new file mode 100644
index 0000000..57b6dc0
--- /dev/null
+++ b/lib/astal/io/file.vala
@@ -0,0 +1,98 @@
+namespace AstalIO {
+/**
+ * Read the contents of a file synchronously.
+ */
+public string read_file(string path) {
+ var str = "";
+ try {
+ FileUtils.get_contents(path, out str, null);
+ } catch (Error error) {
+ critical(error.message);
+ }
+ return str;
+}
+
+/**
+ * Read the contents of a file asynchronously.
+ */
+public async string read_file_async(string path) throws Error {
+ uint8[] content;
+ yield File.new_for_path(path).load_contents_async(null, out content, null);
+ return (string)content;
+}
+
+/**
+ * Write content to a file synchronously.
+ */
+public void write_file(string path, string content) {
+ try {
+ FileUtils.set_contents(path, content);
+ } catch (Error error) {
+ critical(error.message);
+ }
+}
+
+/**
+ * Write content to a file asynchronously.
+ */
+public async void write_file_async(string path, string content) throws Error {
+ yield File.new_for_path(path).replace_contents_async(
+ content.data,
+ null,
+ false,
+ FileCreateFlags.REPLACE_DESTINATION,
+ null,
+ null);
+}
+
+/**
+ * Monitor a file for changes. If the path is a directory, monitor it recursively.
+ * The callback will be called passed two parameters: the path of the file
+ * that changed and an [[email protected]] indicating the reason.
+ */
+public FileMonitor? monitor_file(string path, Closure callback) {
+ try {
+ var file = File.new_for_path(path);
+ var mon = file.monitor(FileMonitorFlags.NONE);
+
+ mon.changed.connect((file, _file, event) => {
+ var f = Value(Type.STRING);
+ var e = Value(Type.INT);
+ var ret = Value(Type.POINTER);
+
+ f.set_string(file.get_path());
+ e.set_int(event);
+
+ callback.invoke(ref ret, { f, e });
+ });
+
+ if (FileUtils.test(path, FileTest.IS_DIR)) {
+ var enumerator = file.enumerate_children("standard::*",
+ FileQueryInfoFlags.NONE, null);
+
+ var i = enumerator.next_file(null);
+ while (i != null) {
+ if (i.get_file_type() == FileType.DIRECTORY) {
+ var filepath = file.get_child(i.get_name()).get_path();
+ if (filepath != null) {
+ var m = monitor_file(path, callback);
+ mon.notify["cancelled"].connect(() => {
+ m.cancel();
+ });
+ }
+ }
+ i = enumerator.next_file(null);
+ }
+ }
+
+ mon.ref();
+ mon.notify["cancelled"].connect(() => {
+ mon.unref();
+ });
+ return mon;
+ } catch (Error error) {
+ critical(error.message);
+ return null;
+ }
+}
+}