How can I monitor the ~/.local directory using Vala?

允我心安 提交于 2019-12-11 06:28:25

问题


I am trying to monitor the ~/.local directory according to the Vala documentation I can monitor the home correctly. but I can't monitor the the ~/.local.

initFileMonitor V1:

public void initFileMonitor(){
    try {
        string homePath = Environment.get_home_dir();
        string filePath = homePath + "/.local";
        File file = File.new_for_path(filePath);
        FileMonitor monitor = file.monitor_directory(FileMonitorFlags.NONE, null);

        print ("\nMonitoring: %s\n", file.get_path ());

        monitor.changed.connect ((src, dest, event) => {
            if (dest != null) {
                 print ("%s: %s, %s\n", event.to_string (), src.get_path (), dest.get_path ());
            } else {
                print ("%s: %s\n", event.to_string (), src.get_path ());
            }
        });
    } catch (Error err) {
        print ("Error: %s\n", err.message);
    }
}

terminal output(no error, no monitoring):

Monitoring: /home/srdr/.local

回答1:


Because the file monitor is stored in a local variable it is like other variables destroyed (or in GObject terms finalised/destructed) at the end of the function call

To ensure it lives long enough you should make it a field on a class, then the FileMonitor instance is 'owned' by an instance of that class rather than each call to a specific method

Runnable demo (valac demo.vala --pkg gio-2.0)

class FileMonitorDemo {
    private FileMonitor monitor;

    public void initFileMonitor() {
        var path = Path.build_filename(Environment.get_home_dir(), ".local");
        var file = File.new_for_path(path);
        try {
            monitor = file.monitor_directory(NONE);

            message ("Monitoring: %s", file.get_path ());

            monitor.changed.connect ((src, dest, event) => {
                if (dest != null) {
                    print ("%s: %s, %s\n", event.to_string (), src.get_path (), dest.get_path ());
                } else {
                    print ("%s: %s\n", event.to_string (), src.get_path ());
                }
            });
        } catch (Error err) {
            critical ("Error: %s\n", err.message);
        }
    }
}

void main () {
    var filemon = new FileMonitorDemo();
    filemon.initFileMonitor();
    new MainLoop ().run ();
}



回答2:


You need to actually run the monitor by creating a main loop and having it wait for events:

new MainLoop ().run ();


来源:https://stackoverflow.com/questions/51177265/how-can-i-monitor-the-local-directory-using-vala

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!