ftp://ftp.redhat.com/pub/redhat/linux/rawhide/SRPMS/SRPMS/gnome-vfs2-2.3.8-1.src.rpm
[gnome-vfs-httpcaptive.git] / test / test-monitor.c
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
2 /* test-monitor.c - Test program for file monitoringu in the GNOME Virtual
3    File System.
4
5    Copyright (C) 2001 Ian McKellar
6
7    The Gnome Library is free software; you can redistribute it and/or
8    modify it under the terms of the GNU Library General Public License as
9    published by the Free Software Foundation; either version 2 of the
10    License, or (at your option) any later version.
11
12    The Gnome Library is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15    Library General Public License for more details.
16
17    You should have received a copy of the GNU Library General Public
18    License along with the Gnome Library; see the file COPYING.LIB.  If not,
19    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20    Boston, MA 02111-1307, USA.
21
22    Authors: 
23         Ian McKellar <yakk@yakk.net>
24  */
25
26 #include <libgnomevfs/gnome-vfs.h>
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <unistd.h>
31 #include <glib.h>
32
33 static GMainLoop *main_loop;
34
35 static gboolean
36 timeout_cb (gpointer data)
37 {
38         static int i = 0;
39
40         if (i == 0)
41         {
42                 unlink ("/tmp/test-monitor");
43                 g_spawn_command_line_sync ("touch /tmp/test-monitor",
44                                 NULL, NULL, NULL, NULL);
45         }
46
47         if (i == 1)
48         {
49                 unlink ("/tmp/test-monitor");
50         }
51
52         i++;
53
54         return TRUE;
55 }
56
57 static void
58 show_result (GnomeVFSResult result, const gchar *what, const gchar *text_uri)
59 {
60         fprintf (stderr, "%s `%s': %s\n",
61                  what, text_uri, gnome_vfs_result_to_string (result));
62         if (result != GNOME_VFS_OK)
63                 exit (1);
64 }
65
66 static void
67 callback (GnomeVFSMonitorHandle *handle,
68           const gchar *monitor_uri,
69           const gchar *info_uri,
70           GnomeVFSMonitorEventType event_type,
71           gpointer user_data) {
72         static int i = 0;
73
74         g_print ("Got a callback: ");
75         switch (event_type) {
76                 case GNOME_VFS_MONITOR_EVENT_CHANGED:
77                         g_print ("GNOME_VFS_MONITOR_EVENT_CHANGED");
78                         break;
79                 case GNOME_VFS_MONITOR_EVENT_DELETED:
80                         g_print ("GNOME_VFS_MONITOR_EVENT_DELETED");
81                         break;
82                 case GNOME_VFS_MONITOR_EVENT_STARTEXECUTING:
83                         g_print ("GNOME_VFS_MONITOR_EVENT_STARTEXECUTING");
84                         break;
85                 case GNOME_VFS_MONITOR_EVENT_STOPEXECUTING:
86                         g_print ("GNOME_VFS_MONITOR_EVENT_STOPEXECUTING");
87                         break;
88                 case GNOME_VFS_MONITOR_EVENT_CREATED:
89                         g_print ("GNOME_VFS_MONITOR_EVENT_CREATED");
90                         break;
91                 case GNOME_VFS_MONITOR_EVENT_METADATA_CHANGED:
92                         g_print ("GNOME_VFS_MONITOR_EVENT_METADATA_CHANGED");
93                         break;
94                 default:
95                         g_print ("Unknown monitor type, exiting...");
96                         exit (1);
97         }
98
99         g_print (" (%s)", info_uri);
100         g_print ("\n");
101         i++;
102         if (i >= 2)
103                 exit (0);
104 }
105
106 int
107 main (int argc, char **argv)
108 {
109         GnomeVFSResult    result;
110         gchar            *text_uri = "/tmp/";
111         GnomeVFSMonitorHandle *handle;
112
113         if (! gnome_vfs_init ()) {
114                 fprintf (stderr, "Cannot initialize gnome-vfs.\n");
115                 return 1;
116         }
117
118         if (argc == 2) {
119                 text_uri = argv[1];
120         }
121
122         result = gnome_vfs_monitor_add (&handle, text_uri,
123                         GNOME_VFS_MONITOR_DIRECTORY, callback, "user data");
124         printf ("handle is %p\n", handle);
125         show_result (result, "monitor_add", text_uri);
126
127         g_timeout_add (1000, timeout_cb, NULL);
128
129         if (result == GNOME_VFS_OK) {
130                 main_loop = g_main_loop_new (NULL, TRUE);
131                 g_main_loop_run (main_loop);
132                 g_main_loop_unref (main_loop);
133         }
134
135         g_free (text_uri);
136
137         return 0;
138 }