Commits

Alessio Caiazza committed d823f69

A working example

Comments (0)

Files changed (4)

+syntax:regexp
+^src/dtm$
+all clean dtm:
+	cd src && $(MAKE) $@
+
+.PHONY: all clean
+all: dtm
+
+dtm: dtm.vala
+	valac --pkg gio-2.0 dtm.vala
+
+clean:
+	-rm dtm
+
+.PHONY: all clean
+
+int main (string[] args) {
+
+    if (args.length != 3) {
+      stderr.printf("wrong parameters\n");
+      stderr.printf("Usage:\n%s file_to_watch command_to_execute\n", args[0]);
+      return -1;
+    }  
+             
+    var file = File.new_for_commandline_arg (args[1]);
+    
+    //if( file.query_exists())
+    try {
+      var monitor = file.monitor_file(FileMonitorFlags.NONE);
+      if(monitor == null)
+      {
+        stderr.printf("Failed opening monitor for %s", file.get_path());
+        return -2;
+      }
+      MainLoop main_loop = new MainLoop ();
+      
+      monitor.changed.connect(
+        (file, other_file, event_type) => {
+          stdout.printf("EVENTO: %s on %s\n", event_type.to_string(), file.get_path());
+          if(event_type == FileMonitorEvent.CHANGED)
+          {
+              try {
+                Process.spawn_command_line_async(args[2]);
+              } catch (SpawnError e) {
+                stderr.printf("Cannot execute %s.\n\t%s",
+                  args[3], e.message);
+              }
+          }
+          //main_loop.quit ();
+        });
+      
+      main_loop.run ();
+    } catch (GLib.IOError e) {
+      stderr.printf("IO Error: %s", e.message);
+      return -3;
+    }
+    
+    return 0;
+}