Monitoring directory changes with gnomevfs



Hi List,

I'm trying to use the directory monitoring API in gnomevfs, but I'm having difficulty understanding how it is supposed to be used. The documentation at http://library.gnome.org/devel/gnome-vfs-2.0/unstable/gnome-vfs-20-gnome-vfs-monitor.html doesn't have any examples and I haven't been able to find a simple example on the web. The API seems simple enough, but I'm not sure I understand how the callback mechanism is supposed to work. (This probably reflects a deeper lack of understanding of glib, but I'll persist...)

For example, I gather this program won't work because there is no GMainLoop in place to generate the callback:

#include <libgnomevfs/gnome-vfs.h>

int exit=0;

static void MonitorCallback(GnomeVFSMonitorHandle *handle, const gchar *monitor_uri, const gchar *info_uri, GnomeVFSMonitorEventType event_type, gpointer user_data)
{
    exit=1;
}

int main()
{
    if(!gnome_vfs_initialized())
        gnome_vfs_init();
    GnomeVFSMonitorHandle *h;
    gnome_vfs_monitor_add(&h, "/home/myusername", GNOME_VFS_MONITOR_DIRECTORY, MonitorCallback, NULL);
    while(!exit);
    gnome_vfs_monitor_cancel(h);
    return 0;
}

but something like this should work correctly?

#include <glib.h>
#include <libgnomevfs/gnome-vfs.h>

static void MonitorCallback(GnomeVFSMonitorHandle *handle, const gchar *monitor_uri, const gchar *info_uri, GnomeVFSMonitorEventType event_type, gpointer user_data)
{
    g_print("Monitor event received, exiting...\n");
    g_main_loop_quit((GMainLoop*)user_data);
}

int main()
{
    if(!gnome_vfs_initialized())
        gnome_vfs_init();
    GMainLoop *loop;
    loop = g_main_loop_new(NULL, FALSE);
    GnomeVFSMonitorHandle *h;
    gnome_vfs_monitor_add(&h, "/home/myusername", GNOME_VFS_MONITOR_DIRECTORY, MonitorCallback, loop);
    g_main_loop_run(loop);
    gnome_vfs_monitor_cancel(h);
    return 0;
}

Can someone can give me an idea of whether the above should work or what I would need to do to that simple example to get it working? Looking at the above I don't really understand how glib would know how to attach the MonitorCallback to the GMainLoop, which isn't even running yet (if I created and started another main loop inside the outer loop, would it also generate callbacks?).

thanks,
D


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]