Skip to content

Instantly share code, notes, and snippets.

@dmatveev
Created April 3, 2012 21:26
Show Gist options
  • Save dmatveev/2295597 to your computer and use it in GitHub Desktop.
Save dmatveev/2295597 to your computer and use it in GitHub Desktop.
GFileMonitor sample application. Watches the specified path, registers events and prints it on the screen.
#include <glib.h>
#include <gio/gio.h>
#include <string.h>
static GMainLoop *gMainLoop = NULL;
char *
decode (GFileMonitorEvent ev)
{
char *fmt = g_malloc0 (1024);
int caret = 0;
#define dc(x) \
case G_FILE_MONITOR_EVENT_##x: \
strcat(fmt, #x); \
caret += strlen(#x); \
fmt[caret] = ':'; \
break;
switch (ev) {
dc(CHANGED);
dc(CHANGES_DONE_HINT);
dc(DELETED);
dc(CREATED);
dc(ATTRIBUTE_CHANGED);
dc(PRE_UNMOUNT);
dc(UNMOUNTED);
dc(MOVED);
}
#undef dc
return fmt;
}
void
callback (GFileMonitor *mon,
GFile *first,
GFile *second,
GFileMonitorEvent event,
gpointer udata)
{
char *msg = decode (event);
#define fn(x) ((x) ? g_file_get_basename (x) : "--")
g_printf ("Received event %s (code %d), first file \"%s\", second file \"%s\"\n",
msg,
event,
fn(first),
fn(second));
#undef fn
g_free (msg);
}
int
main (int argc, char *argv[])
{
GFile *file;
GFileMonitor *mon;
if (argc < 2) {
g_printf ("No!");
return 1;
}
g_type_init ();
file = g_file_new_for_path (argv[1]);
g_assert (file != NULL);
mon = g_file_monitor (file, G_FILE_MONITOR_SEND_MOVED, NULL, NULL);
g_assert (mon != NULL);
g_signal_connect (mon, "changed", G_CALLBACK (callback), NULL);
gMainLoop = g_main_loop_new (NULL, FALSE);
g_main_loop_run (gMainLoop);
g_object_unref (mon);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment