From 5f880bf11445c83b853b2425bb1a9f2646b53ced Mon Sep 17 00:00:00 2001 From: Kolan Sh Date: Tue, 16 Sep 2014 09:57:42 +0400 Subject: [PATCH] gio-async added --- vala/gio-async/gio-async.vala | 67 +++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 vala/gio-async/gio-async.vala diff --git a/vala/gio-async/gio-async.vala b/vala/gio-async/gio-async.vala new file mode 100644 index 0000000..751cf16 --- /dev/null +++ b/vala/gio-async/gio-async.vala @@ -0,0 +1,67 @@ +using Gtk; + +/** + * Loads the list of files in user's home directory and displays them + * in a GTK+ list view. + */ +class ASyncGIOSample : Window { + + private ListStore model; + + public ASyncGIOSample () { + + // Set up the window + set_default_size (300, 200); + this.destroy.connect (Gtk.main_quit); + + // Set up the list widget and its model + this.model = new ListStore (1, typeof (string)); + var list = new TreeView.with_model (this.model); + list.insert_column_with_attributes (-1, "Filename", + new CellRendererText (), "text", 0); + + // Put list widget into a scrollable area and add it to the window + var scroll = new ScrolledWindow (null, null); + scroll.set_policy (PolicyType.NEVER, PolicyType.AUTOMATIC); + scroll.add (list); + add (scroll); + + // start file listing process + list_directory.begin (); + } + + private async void list_directory () { + stdout.printf ("Start scanning home directory\n"); + var dir = File.new_for_path (Environment.get_home_dir ()); + try { + // asynchronous call, to get directory entries + var e = yield dir.enumerate_children_async (FileAttribute.STANDARD_NAME, + 0, Priority.DEFAULT); + while (true) { + // asynchronous call, to get entries so far + var files = yield e.next_files_async (10, Priority.DEFAULT); + if (files == null) { + break; + } + // append the files found so far to the list + foreach (var info in files) { + TreeIter iter; + this.model.append (out iter); + this.model.set (iter, 0, info.get_name ()); + } + } + } catch (Error err) { + stderr.printf ("Error: list_files failed: %s\n", err.message); + } + } + + static int main (string[] args) { + Gtk.init (ref args); + + var demo = new ASyncGIOSample (); + demo.show_all (); + + Gtk.main (); + return 0; + } +}