#include <glib.h>
#include <gio/gio.h>
/* gcc -g $(pkg-config --cflags glib-2.0 gio-2.0) test.c $(pkg-config --libs glib-2.0 gio-2.0) */
/* - Create an empty file
* - Trash it
* - See if trash::orig-path is set
* - Delete it from the trash & quit
*/
static void trash_changed (GFileMonitor *monitor,
GFile *file,
GFile *other_file,
GFileMonitorEvent event_type,
gpointer user_data)
{
GMainLoop *loop = (GMainLoop *) user_data;
const char * orig_path;
GFileInfo *info;
char * uri = g_file_get_uri (file);
switch (event_type)
{
case G_FILE_MONITOR_EVENT_CREATED:
info = g_file_query_info (file, "trash::orig-path",
G_FILE_QUERY_INFO_NONE, NULL, NULL);
orig_path = g_file_info_get_attribute_byte_string (info,
"trash::orig-path");
if (orig_path)
g_printf ("%s trashed from %s - bug not reproduced :D\n", uri, orig_path);
else
g_printf ("trash::orig-path is not set on %s - bug reproduced :(\n", uri);
g_file_delete (file, NULL, NULL);
g_object_unref (info);
break;
case G_FILE_MONITOR_EVENT_DELETED:
g_printf ("Cleaned up the trash - exiting.\n");
g_main_loop_quit (loop);
break;
}
}
void monitor_trash (GMainLoop *loop)
{
GFile *file;
GFileMonitor *monitor;
file = g_file_new_for_uri ("trash://");
monitor = g_file_monitor_directory (file,
G_FILE_MONITOR_NONE,
NULL,
NULL);
g_object_unref (file);
g_signal_connect (monitor,
"changed",
G_CALLBACK (trash_changed),
loop);
}
void trash_test_file ()
{
GFile *file = g_file_new_for_path ("test-file");
g_object_unref (g_file_create (file,
G_FILE_CREATE_NONE,
NULL, /* GCancellable */
NULL)); /* GError */
if (!g_file_trash (file, NULL, NULL))
g_error("Couldn't trash file");
g_object_unref (file);
}
void main ()
{
GMainLoop *loop;
loop = g_main_loop_new (NULL, TRUE);
monitor_trash (loop);
g_idle_add ((GSourceFunc) trash_test_file, NULL);
g_main_loop_run (loop);
}