From 6a8c41cd1d5e218d0dacffb836fdd7d4ec6333dd Mon Sep 17 00:00:00 2001 From: Aylur Date: Mon, 14 Oct 2024 16:01:36 +0000 Subject: feat: astal-io --- lib/astal/io/file.vala | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 lib/astal/io/file.vala (limited to 'lib/astal/io/file.vala') diff --git a/lib/astal/io/file.vala b/lib/astal/io/file.vala new file mode 100644 index 0000000..b2d480c --- /dev/null +++ b/lib/astal/io/file.vala @@ -0,0 +1,81 @@ +namespace AstalIO { +public string read_file(string path) { + var str = ""; + try { + FileUtils.get_contents(path, out str, null); + } catch (Error error) { + critical(error.message); + } + return str; +} + +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; +} + +public void write_file(string path, string content) { + try { + FileUtils.set_contents(path, content); + } catch (Error error) { + critical(error.message); + } +} + +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); +} + +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; + } +} +} -- cgit v1.2.3 From ecfbf082bfab22e34d8036896f51069ce0c18302 Mon Sep 17 00:00:00 2001 From: Aylur Date: Tue, 15 Oct 2024 20:58:49 +0000 Subject: docs: astal-io doc comments --- lib/astal/io/file.vala | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'lib/astal/io/file.vala') diff --git a/lib/astal/io/file.vala b/lib/astal/io/file.vala index b2d480c..e57f449 100644 --- a/lib/astal/io/file.vala +++ b/lib/astal/io/file.vala @@ -1,4 +1,7 @@ namespace AstalIO { +/** + * Read the contents of a file synchronously. + */ public string read_file(string path) { var str = ""; try { @@ -9,12 +12,18 @@ public string read_file(string path) { 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); @@ -23,6 +32,9 @@ public void write_file(string path, string content) { } } +/** + * 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, @@ -33,6 +45,11 @@ public async void write_file_async(string path, string content) throws Error { 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 [enum@GLib.FileMonitorEvent] indicating the reason. + */ public FileMonitor? monitor_file(string path, Closure callback) { try { var file = File.new_for_path(path); -- cgit v1.2.3 From 306e64998c1bf1fb997c1098ae92d6edfef31cd2 Mon Sep 17 00:00:00 2001 From: Aylur Date: Wed, 23 Oct 2024 20:37:32 +0000 Subject: docs: astal3 and io comments --- lib/astal/io/file.vala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/astal/io/file.vala') diff --git a/lib/astal/io/file.vala b/lib/astal/io/file.vala index e57f449..57b6dc0 100644 --- a/lib/astal/io/file.vala +++ b/lib/astal/io/file.vala @@ -48,7 +48,7 @@ public async void write_file_async(string path, string content) throws Error { /** * 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 [enum@GLib.FileMonitorEvent] indicating the reason. + * that changed and an [enum@Gio.FileMonitorEvent] indicating the reason. */ public FileMonitor? monitor_file(string path, Closure callback) { try { -- cgit v1.2.3