1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * Copyright (C) 2008, 2010 Red Hat, Inc.
4 * Copyright (C) 2010 Igalia, S.L.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public License
17 * along with this library; see the file COPYING.LIB. If not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
26 #include "soup-directory-input-stream.h"
28 #include <libsoup/soup.h>
32 #define INIT_STRING "<html><head><title>OMG!</title></head><body><table>"
33 #define EXIT_STRING "</table></html>"
35 G_DEFINE_TYPE (SoupDirectoryInputStream, soup_directory_input_stream, G_TYPE_INPUT_STREAM)
38 soup_directory_input_stream_parse_info (SoupDirectoryInputStream *stream,
44 char *escaped, *path, *xml_string;
46 if (!g_file_info_get_name (info))
49 s = g_file_info_get_display_name (info);
51 s = g_file_info_get_name (info);
52 /* FIXME: convert somehow? */
53 if (!g_utf8_validate (s, -1, NULL))
56 string = g_string_new ("<tr>");
58 xml_string = g_markup_escape_text (s, -1);
59 escaped = g_uri_escape_string (g_file_info_get_name (info), NULL, FALSE);
60 path = g_strconcat (stream->uri, "/", escaped, NULL);
62 g_string_append_printf (string, "<td><a href=\"%s\">%s</a></td>", path, xml_string);
65 g_string_append (string, "</tr>");
67 buffer = soup_buffer_new (SOUP_MEMORY_TAKE, string->str, string->len);
68 g_string_free (string, FALSE);
74 soup_directory_input_stream_read_next_file (SoupDirectoryInputStream *stream,
75 GCancellable *cancellable,
83 info = g_file_enumerator_next_file (stream->enumerator, cancellable, &err);
86 g_propagate_error (error, err);
88 } else if (!stream->done) {
90 return soup_buffer_new (SOUP_MEMORY_STATIC,
92 sizeof (EXIT_STRING));
98 buffer = soup_directory_input_stream_parse_info (stream, info);
99 g_object_unref (info);
100 } while (buffer == NULL);
106 soup_directory_input_stream_read (GInputStream *input,
109 GCancellable *cancellable,
112 SoupDirectoryInputStream *stream = SOUP_DIRECTORY_INPUT_STREAM (input);
115 for (total = 0; total < count; total += size) {
116 if (stream->buffer == NULL) {
117 stream->buffer = soup_directory_input_stream_read_next_file (stream, cancellable, error);
118 if (stream->buffer == NULL) {
119 /* FIXME: Is this correct or should we forward the error? */
121 g_clear_error (error);
126 size = MIN (stream->buffer->length, count - total);
127 memcpy ((char *)buffer + total, stream->buffer->data, size);
128 if (size == stream->buffer->length) {
129 soup_buffer_free (stream->buffer);
130 stream->buffer = NULL;
132 SoupBuffer *sub = soup_buffer_new_subbuffer (stream->buffer,
134 stream->buffer->length - size);
135 soup_buffer_free (stream->buffer);
136 stream->buffer = sub;
144 soup_directory_input_stream_close (GInputStream *input,
145 GCancellable *cancellable,
148 SoupDirectoryInputStream *stream = SOUP_DIRECTORY_INPUT_STREAM (input);
151 if (stream->buffer) {
152 soup_buffer_free (stream->buffer);
153 stream->buffer = NULL;
156 result = g_file_enumerator_close (stream->enumerator,
159 g_object_unref (stream->enumerator);
160 stream->enumerator = NULL;
162 g_free (stream->uri);
169 soup_directory_input_stream_class_init (SoupDirectoryInputStreamClass *stream_class)
171 GInputStreamClass *inputstream_class = G_INPUT_STREAM_CLASS (stream_class);
173 inputstream_class->read_fn = soup_directory_input_stream_read;
174 inputstream_class->close_fn = soup_directory_input_stream_close;
178 soup_directory_input_stream_init (SoupDirectoryInputStream *stream)
180 stream->buffer = soup_buffer_new (SOUP_MEMORY_STATIC,
182 sizeof (INIT_STRING));
186 soup_directory_input_stream_new (GFileEnumerator *enumerator,
189 GInputStream *stream;
191 g_return_val_if_fail (G_IS_FILE_ENUMERATOR (enumerator), NULL);
192 g_return_val_if_fail (uri != NULL, NULL);
194 stream = g_object_new (SOUP_TYPE_DIRECTORY_INPUT_STREAM, NULL);
196 SOUP_DIRECTORY_INPUT_STREAM (stream)->enumerator = g_object_ref (enumerator);
197 SOUP_DIRECTORY_INPUT_STREAM (stream)->uri = soup_uri_to_string (uri, FALSE);