I currently try to learn some vala. After reading the tutorial I wanted to port some apps written in java to vala.
Therefore I tried to write a application which monitors a folder for changes. This monitor should spawn as a seperate thread so the app can do other things will monitoring. Easy job in java.
After doing some research how to monitor (got a hit on stackoverflow btw) I ended up with the following code:
Runner:
public class App : Object
{
private const string FOLDER_TO_WATCH = "/home/chr/Work/vala-monitor-test/tmp/";
public App() {
GLib.MainLoop loop = new GLib.MainLoop();
stdout.printf("Folder: %s\n", FOLDER_TO_WATCH);
try {
var daemon = new MonitorDaemon( File.new_for_path( FOLDER_TO_WATCH ) );
unowned Thread<void*> t = Thread.create<void*>( daemon.watch, true );
}
catch (ThreadError err) {
stderr.printf("Error occured: %s", err.message);
}
catch (MonitorError err) {
stderr.printf("Error occured: %s", err.message);
}
stdout.printf("waiting..\n");
loop.run();
}
static int main( string[] args )
{
if (!Thread.supported ()) {
stderr.printf ("Cannot run without thread support.\n");
return 1;
}
new App();
return 0;
}
}
Monitor class:
errordomain MonitorError {
NOT_A_DIRECTORY
}
public class MonitorDaemon : Object {
// properties
public File watched_folder { get; construct set; }
// internal
private bool _stop = false;
public MonitorDaemon ( File folder_to_watch ) throws MonitorError {
if ( folder_to_watch.query_file_type ( FileQueryInfoFlags.NONE ) != FileType.DIRECTORY ) {
stderr.printf("Unable to watch: Not a directory\n");
throw new MonitorError.NOT_A_DIRECTORY("Not a directory");
}
this.watched_folder = folder_to_watch;
stdout.printf("Ready to watch %s\n", this.watched_folder.get_path() );
}
public void* watch() {
try {
FileMonitor monitor = this.watched_folder.monitor_directory ( FileMonitorFlags.NONE );
monitor.changed.connect( event_happened );
}
catch (IOError err) {
stderr.printf("Error occured: %s", err.message);
}
stdout.printf( "Started watching folder: %s\n", this.watched_folder.get_path() );
while (!this._stop) { }
stdout.printf( "Stopped watching folder: %s\n", this.watched_folder.get_path() );
return null;
}
public void stop() {
// cleanup..
this._stop = true;
}
private void event_happened ( File file, File? other_file, FileMonitorEvent event_type ) {
stdout.printf( "File '%s': %s\n", file.get_path(), event_type.to_string() );
}
}
When creating a thread for the method MonitorDaemon.watch the property set in the constructor is null again causing the following output on commandline:
Folder: /home/chr/Work/vala-monitor-test/tmp/
Ready to watch /home/chr/Work/vala-monitor-test/tmp/
waiting..
(process:7159): GLib-GIO-CRITICAL **: g_file_monitor_directory: assertion `G_IS_FILE (file)' failed
(process:7159): GLib-GObject-WARNING **: invalid (NULL) pointer instance
(process:7159): GLib-GObject-CRITICAL **: g_signal_connect_object: assertion `G_TYPE_CHECK_INSTANCE (instance)' failed
(process:7159): GLib-GIO-CRITICAL **: g_file_get_path: assertion `G_IS_FILE (file)' failed
Started watching folder: (null)
Vala is reference counted. When exiting the try{} block it gets unref'd then freed. You must keep the MonitorDaemon alive somewhere.