-
-
Save coldnew/33e230eca3ec70b24cbe to your computer and use it in GitHub Desktop.
GFileMonitor sample application. Watches the specified path, registers events and prints it on the screen.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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