| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
#include <config.h> |
|---|
| 23 |
#include <glib/gi18n-lib.h> |
|---|
| 24 |
|
|---|
| 25 |
#ifndef _GNU_SOURCE |
|---|
| 26 |
#define _GNU_SOURCE |
|---|
| 27 |
#endif |
|---|
| 28 |
|
|---|
| 29 |
#include <string.h> |
|---|
| 30 |
|
|---|
| 31 |
#include <glib.h> |
|---|
| 32 |
#include <glib/gstdio.h> |
|---|
| 33 |
#include <stdlib.h> |
|---|
| 34 |
#include <gtk/gtk.h> |
|---|
| 35 |
|
|---|
| 36 |
#include <tny-stream.h> |
|---|
| 37 |
#include <tny-gtk-html-stream.h> |
|---|
| 38 |
|
|---|
| 39 |
static GObjectClass *parent_class = NULL; |
|---|
| 40 |
|
|---|
| 41 |
|
|---|
| 42 |
struct _TnyGtkHtmlStreamPriv { |
|---|
| 43 |
GtkHTMLStream *stream; |
|---|
| 44 |
}; |
|---|
| 45 |
|
|---|
| 46 |
#define TNY_GTK_HTML_STREAM_GET_PRIVATE(o) ((TnyGtkHtmlStream*)(o))->priv |
|---|
| 47 |
|
|---|
| 48 |
static ssize_t |
|---|
| 49 |
tny_gtk_html_stream_write_to_stream (TnyStream *self, TnyStream *output) |
|---|
| 50 |
{ |
|---|
| 51 |
char tmp_buf[4096]; |
|---|
| 52 |
ssize_t total = 0; |
|---|
| 53 |
ssize_t nb_read; |
|---|
| 54 |
ssize_t nb_written; |
|---|
| 55 |
|
|---|
| 56 |
g_assert (TNY_IS_STREAM (output)); |
|---|
| 57 |
|
|---|
| 58 |
while (G_LIKELY (!tny_stream_is_eos (self))) |
|---|
| 59 |
{ |
|---|
| 60 |
nb_read = tny_stream_read (self, tmp_buf, sizeof (tmp_buf)); |
|---|
| 61 |
if (G_UNLIKELY (nb_read < 0)) |
|---|
| 62 |
return -1; |
|---|
| 63 |
else if (G_LIKELY (nb_read > 0)) { |
|---|
| 64 |
nb_written = 0; |
|---|
| 65 |
|
|---|
| 66 |
while (G_LIKELY (nb_written < nb_read)) |
|---|
| 67 |
{ |
|---|
| 68 |
ssize_t len = tny_stream_write (output, tmp_buf + nb_written, |
|---|
| 69 |
nb_read - nb_written); |
|---|
| 70 |
if (G_UNLIKELY (len < 0)) |
|---|
| 71 |
return -1; |
|---|
| 72 |
nb_written += len; |
|---|
| 73 |
} |
|---|
| 74 |
total += nb_written; |
|---|
| 75 |
} |
|---|
| 76 |
} |
|---|
| 77 |
return total; |
|---|
| 78 |
} |
|---|
| 79 |
|
|---|
| 80 |
static ssize_t |
|---|
| 81 |
tny_gtk_html_stream_read (TnyStream *self, char *data, size_t n) |
|---|
| 82 |
{ |
|---|
| 83 |
return -1; |
|---|
| 84 |
} |
|---|
| 85 |
|
|---|
| 86 |
static gint |
|---|
| 87 |
tny_gtk_html_stream_reset (TnyStream *self) |
|---|
| 88 |
{ |
|---|
| 89 |
return 0; |
|---|
| 90 |
} |
|---|
| 91 |
|
|---|
| 92 |
static ssize_t |
|---|
| 93 |
tny_gtk_html_stream_write (TnyStream *self, const char *data, size_t n) |
|---|
| 94 |
{ |
|---|
| 95 |
TnyGtkHtmlStreamPriv *priv = TNY_GTK_HTML_STREAM_GET_PRIVATE (self); |
|---|
| 96 |
|
|---|
| 97 |
if (!priv->stream) { |
|---|
| 98 |
g_print ("modest: cannot write to closed stream\n"); |
|---|
| 99 |
return 0; |
|---|
| 100 |
} |
|---|
| 101 |
|
|---|
| 102 |
if (n == 0 || !data) |
|---|
| 103 |
return 0; |
|---|
| 104 |
|
|---|
| 105 |
gtk_html_stream_write (priv->stream, data, n); |
|---|
| 106 |
|
|---|
| 107 |
return (ssize_t) n; |
|---|
| 108 |
} |
|---|
| 109 |
|
|---|
| 110 |
static gint |
|---|
| 111 |
tny_gtk_html_stream_flush (TnyStream *self) |
|---|
| 112 |
{ |
|---|
| 113 |
return 0; |
|---|
| 114 |
} |
|---|
| 115 |
|
|---|
| 116 |
static gint |
|---|
| 117 |
tny_gtk_html_stream_close (TnyStream *self) |
|---|
| 118 |
{ |
|---|
| 119 |
TnyGtkHtmlStreamPriv *priv = TNY_GTK_HTML_STREAM_GET_PRIVATE (self); |
|---|
| 120 |
|
|---|
| 121 |
gtk_html_stream_close (priv->stream, GTK_HTML_STREAM_OK); |
|---|
| 122 |
priv->stream = NULL; |
|---|
| 123 |
|
|---|
| 124 |
return 0; |
|---|
| 125 |
} |
|---|
| 126 |
|
|---|
| 127 |
static gboolean |
|---|
| 128 |
tny_gtk_html_stream_is_eos (TnyStream *self) |
|---|
| 129 |
{ |
|---|
| 130 |
return TRUE; |
|---|
| 131 |
} |
|---|
| 132 |
|
|---|
| 133 |
|
|---|
| 134 |
|
|---|
| 135 |
|
|---|
| 136 |
|
|---|
| 137 |
|
|---|
| 138 |
|
|---|
| 139 |
|
|---|
| 140 |
|
|---|
| 141 |
|
|---|
| 142 |
|
|---|
| 143 |
TnyStream* |
|---|
| 144 |
tny_gtk_html_stream_new (GtkHTMLStream *stream) |
|---|
| 145 |
{ |
|---|
| 146 |
TnyGtkHtmlStream *self = g_object_new (TNY_TYPE_GTK_HTML_STREAM, NULL); |
|---|
| 147 |
TnyGtkHtmlStreamPriv *priv = TNY_GTK_HTML_STREAM_GET_PRIVATE (self); |
|---|
| 148 |
priv->stream = stream; |
|---|
| 149 |
return TNY_STREAM (self); |
|---|
| 150 |
} |
|---|
| 151 |
|
|---|
| 152 |
static void |
|---|
| 153 |
tny_gtk_html_stream_instance_init (GTypeInstance *instance, gpointer g_class) |
|---|
| 154 |
{ |
|---|
| 155 |
TnyGtkHtmlStream *self = (TnyGtkHtmlStream *)instance; |
|---|
| 156 |
TnyGtkHtmlStreamPriv *priv = g_slice_new (TnyGtkHtmlStreamPriv); |
|---|
| 157 |
priv->stream = NULL; |
|---|
| 158 |
self->priv = priv; |
|---|
| 159 |
return; |
|---|
| 160 |
} |
|---|
| 161 |
|
|---|
| 162 |
static void |
|---|
| 163 |
tny_gtk_html_stream_finalize (GObject *object) |
|---|
| 164 |
{ |
|---|
| 165 |
TnyGtkHtmlStream *self = (TnyGtkHtmlStream *)object; |
|---|
| 166 |
TnyGtkHtmlStreamPriv *priv = TNY_GTK_HTML_STREAM_GET_PRIVATE (self); |
|---|
| 167 |
priv->stream = NULL; |
|---|
| 168 |
g_slice_free (TnyGtkHtmlStreamPriv, priv); |
|---|
| 169 |
(*parent_class->finalize) (object); |
|---|
| 170 |
return; |
|---|
| 171 |
} |
|---|
| 172 |
|
|---|
| 173 |
static void |
|---|
| 174 |
tny_stream_init (gpointer g, gpointer iface_data) |
|---|
| 175 |
{ |
|---|
| 176 |
TnyStreamIface *klass = (TnyStreamIface *)g; |
|---|
| 177 |
|
|---|
| 178 |
klass->read= tny_gtk_html_stream_read; |
|---|
| 179 |
klass->write= tny_gtk_html_stream_write; |
|---|
| 180 |
klass->flush= tny_gtk_html_stream_flush; |
|---|
| 181 |
klass->close= tny_gtk_html_stream_close; |
|---|
| 182 |
klass->is_eos= tny_gtk_html_stream_is_eos; |
|---|
| 183 |
klass->reset= tny_gtk_html_stream_reset; |
|---|
| 184 |
klass->write_to_stream= tny_gtk_html_stream_write_to_stream; |
|---|
| 185 |
|
|---|
| 186 |
return; |
|---|
| 187 |
} |
|---|
| 188 |
|
|---|
| 189 |
static void |
|---|
| 190 |
tny_gtk_html_stream_class_init (TnyGtkHtmlStreamClass *class) |
|---|
| 191 |
{ |
|---|
| 192 |
GObjectClass *object_class; |
|---|
| 193 |
|
|---|
| 194 |
parent_class = g_type_class_peek_parent (class); |
|---|
| 195 |
object_class = (GObjectClass*) class; |
|---|
| 196 |
|
|---|
| 197 |
object_class->finalize = tny_gtk_html_stream_finalize; |
|---|
| 198 |
|
|---|
| 199 |
return; |
|---|
| 200 |
} |
|---|
| 201 |
|
|---|
| 202 |
static gpointer |
|---|
| 203 |
tny_gtk_html_stream_register_type (gpointer notused) |
|---|
| 204 |
{ |
|---|
| 205 |
GType type = 0; |
|---|
| 206 |
|
|---|
| 207 |
static const GTypeInfo info = |
|---|
| 208 |
{ |
|---|
| 209 |
sizeof (TnyGtkHtmlStreamClass), |
|---|
| 210 |
NULL, |
|---|
| 211 |
NULL, |
|---|
| 212 |
(GClassInitFunc) tny_gtk_html_stream_class_init, |
|---|
| 213 |
NULL, |
|---|
| 214 |
NULL, |
|---|
| 215 |
sizeof (TnyGtkHtmlStream), |
|---|
| 216 |
0, |
|---|
| 217 |
tny_gtk_html_stream_instance_init |
|---|
| 218 |
}; |
|---|
| 219 |
|
|---|
| 220 |
static const GInterfaceInfo tny_stream_info = |
|---|
| 221 |
{ |
|---|
| 222 |
(GInterfaceInitFunc) tny_stream_init, |
|---|
| 223 |
NULL, |
|---|
| 224 |
NULL |
|---|
| 225 |
}; |
|---|
| 226 |
|
|---|
| 227 |
type = g_type_register_static (G_TYPE_OBJECT, |
|---|
| 228 |
"TnyGtkHtmlStream", |
|---|
| 229 |
&info, 0); |
|---|
| 230 |
|
|---|
| 231 |
g_type_add_interface_static (type, TNY_TYPE_STREAM, |
|---|
| 232 |
&tny_stream_info); |
|---|
| 233 |
|
|---|
| 234 |
return GUINT_TO_POINTER (type); |
|---|
| 235 |
} |
|---|
| 236 |
|
|---|
| 237 |
GType |
|---|
| 238 |
tny_gtk_html_stream_get_type (void) |
|---|
| 239 |
{ |
|---|
| 240 |
static GOnce once = G_ONCE_INIT; |
|---|
| 241 |
g_once (&once, tny_gtk_html_stream_register_type, NULL); |
|---|
| 242 |
return GPOINTER_TO_UINT (once.retval); |
|---|
| 243 |
} |
|---|