dev/vala/gio-async/gio-async.vala

68 lines
2.1 KiB
Vala

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;
}
}