ftp://ftp.redhat.com/pub/redhat/linux/rawhide/SRPMS/SRPMS/gnome-vfs2-2.3.8-1.src.rpm
[gnome-vfs-httpcaptive.git] / test / test-channel.c
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
2 /* test-channel.c - Test for the `open_as_channel' functionality of the GNOME
3    Virtual File System.
4
5    Copyright (C) 1999 Free Software Foundation
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    Author: Ettore Perazzoli <ettore@comm2000.it> */
23
24 #include <config.h>
25
26 #include <glib/gconvert.h> /* workaround for bug in giochannel.h */
27 #include <glib/giochannel.h>
28 #include <glib/gmain.h>
29 #include <glib/gmessages.h>
30 #include <libgnomevfs/gnome-vfs-async-ops.h>
31 #include <libgnomevfs/gnome-vfs-init.h>
32 #include <stdio.h>
33
34 #define BUFFER_SIZE 4096
35
36 static GMainLoop *main_loop;
37
38 static gboolean
39 io_channel_callback (GIOChannel *source,
40                      GIOCondition condition,
41                      gpointer data)
42 {
43         gchar buffer[BUFFER_SIZE + 1];
44         gsize bytes_read;
45
46         printf ("\n\n************ IO Channel callback!\n");
47
48         if (condition & G_IO_IN) {
49                 g_io_channel_read_chars (source, buffer, sizeof (buffer) - 1, &bytes_read, NULL);
50                 buffer[bytes_read] = 0;
51                 printf ("---> Read %lu byte(s):\n%s\n\n(***END***)\n",
52                         (gulong)bytes_read, buffer);
53                 fflush (stdout);
54         }
55
56         if (condition & G_IO_NVAL) {
57                 g_warning ("channel_callback got NVAL condition.");
58                 return FALSE;
59         }
60
61         if (condition & G_IO_HUP) {
62                 printf ("\n----- EOF -----\n");
63                 fflush (stdout);
64                 g_io_channel_shutdown (source, FALSE, NULL);
65                 g_main_loop_quit (main_loop);
66                 return FALSE;
67         }
68
69         return TRUE;
70 }
71
72 static void open_callback (GnomeVFSAsyncHandle *handle,
73                            GIOChannel *channel,
74                            GnomeVFSResult result,
75                            gpointer data)
76 {
77         if (result != GNOME_VFS_OK) {
78                 printf ("Error opening: %s.\n",
79                         gnome_vfs_result_to_string (result));
80                 return;
81         }
82
83         printf ("Open successful, callback data `%s'.\n", (gchar *) data);
84         g_io_add_watch_full (channel,
85                              G_PRIORITY_HIGH,
86                              G_IO_IN | G_IO_NVAL | G_IO_HUP,
87                              io_channel_callback, handle,
88                              NULL);
89
90         g_io_channel_unref (channel);
91 }
92
93 \f
94 int
95 main (int argc, char **argv)
96 {
97         GnomeVFSAsyncHandle *handle;
98
99         if (argc < 2) {
100                 fprintf (stderr, "Usage: %s <uri>\n", argv[0]);
101                 return 1;
102         }
103
104         puts ("Initializing gnome-vfs...");
105         gnome_vfs_init ();
106
107         printf ("Starting open for `%s'...\n", argv[1]);
108         gnome_vfs_async_open_as_channel (&handle, argv[1],
109                                          GNOME_VFS_OPEN_READ,
110                                          BUFFER_SIZE,
111                                          0,
112                                          open_callback,
113                                          "open_callback");
114
115         puts ("GTK+ main loop running.");
116         main_loop = g_main_loop_new (NULL, TRUE);
117         g_main_loop_run (main_loop);
118         g_main_loop_unref (main_loop);
119
120         puts ("GTK+ main loop finished.");
121
122         puts ("All done");
123
124         while (1)
125                 ;
126
127         return 0;
128 }