* modest-tny-stream-gtkhtml.[ch]:
[modest] / src / modest-tny-stream-gtkhtml.c
1 /* modest-tny-stream-gtkhtml.c */
2
3 /* insert (c)/licensing information) */
4
5 #include "modest-tny-stream-gtkhtml.h"
6 #include <gtkhtml/gtkhtml-stream.h>
7 #include <gtkhtml/gtkhtml-search.h>
8
9 /* 'private'/'protected' functions */
10 static void  modest_tny_stream_gtkhtml_class_init   (ModestTnyStreamGtkhtmlClass *klass);
11 static void  modest_tny_stream_gtkhtml_init         (ModestTnyStreamGtkhtml *obj);
12 static void  modest_tny_stream_gtkhtml_finalize     (GObject *obj);
13
14 static void  modest_tny_stream_gtkhml_iface_init (gpointer g_iface, gpointer iface_data);
15
16 /* list my signals */
17 enum {
18         /* MY_SIGNAL_1, */
19         /* MY_SIGNAL_2, */
20         LAST_SIGNAL
21 };
22
23 typedef struct _ModestTnyStreamGtkhtmlPrivate ModestTnyStreamGtkhtmlPrivate;
24 struct _ModestTnyStreamGtkhtmlPrivate {
25         GtkHTMLStream *stream;
26 };
27 #define MODEST_TNY_STREAM_GTKHTML_GET_PRIVATE(o)      (G_TYPE_INSTANCE_GET_PRIVATE((o), \
28                                                        MODEST_TYPE_TNY_STREAM_GTKHTML, \
29                                                        ModestTnyStreamGtkhtmlPrivate))
30 /* globals */
31 static GObjectClass *parent_class = NULL;
32
33 /* uncomment the following if you have defined any signals */
34 /* static guint signals[LAST_SIGNAL] = {0}; */
35
36 GType
37 modest_tny_stream_gtkhtml_get_type (void)
38 {
39         static GType my_type = 0;
40         if (!my_type) {
41                 static const GTypeInfo my_info = {
42                         sizeof(ModestTnyStreamGtkhtmlClass),
43                         NULL,           /* base init */
44                         NULL,           /* base finalize */
45                         (GClassInitFunc) modest_tny_stream_gtkhtml_class_init,
46                         NULL,           /* class finalize */
47                         NULL,           /* class data */
48                         sizeof(ModestTnyStreamGtkhtml),
49                         1,              /* n_preallocs */
50                         (GInstanceInitFunc) modest_tny_stream_gtkhtml_init,
51                 };
52
53                 static const GInterfaceInfo iface_info = {
54                         (GInterfaceInitFunc) modest_tny_stream_gtkhml_iface_init,
55                         NULL,         /* interface_finalize */
56                         NULL          /* interface_data */
57                 };
58
59                 my_type = g_type_register_static (G_TYPE_OBJECT,
60                                                   "ModestTnyStreamGtkhtml",
61                                                   &my_info, 0);
62
63                 g_type_add_interface_static (my_type, TNY_TYPE_STREAM_IFACE,
64                                              &iface_info);
65
66         }
67         return my_type;
68 }
69
70 static void
71 modest_tny_stream_gtkhtml_class_init (ModestTnyStreamGtkhtmlClass *klass)
72 {
73         GObjectClass *gobject_class;
74         gobject_class = (GObjectClass*) klass;
75
76         parent_class            = g_type_class_peek_parent (klass);
77         gobject_class->finalize = modest_tny_stream_gtkhtml_finalize;
78
79         g_type_class_add_private (gobject_class, sizeof(ModestTnyStreamGtkhtmlPrivate));
80 }
81
82 static void
83 modest_tny_stream_gtkhtml_init (ModestTnyStreamGtkhtml *obj)
84 {
85         ModestTnyStreamGtkhtmlPrivate *priv;
86         priv = MODEST_TNY_STREAM_GTKHTML_GET_PRIVATE(obj);
87
88         priv->stream  = NULL;
89 }
90
91 static void
92 modest_tny_stream_gtkhtml_finalize (GObject *obj)
93 {
94         ModestTnyStreamGtkhtmlPrivate *priv;
95
96         priv = MODEST_TNY_STREAM_GTKHTML_GET_PRIVATE(obj);
97         priv->stream = NULL;
98 }
99
100 GObject*
101 modest_tny_stream_gtkhtml_new (GtkHTMLStream *stream)
102 {
103         GObject *obj;
104         ModestTnyStreamGtkhtmlPrivate *priv;
105         
106         obj  = G_OBJECT(g_object_new(MODEST_TYPE_TNY_STREAM_GTKHTML, NULL));
107         priv = MODEST_TNY_STREAM_GTKHTML_GET_PRIVATE(obj);
108
109         g_return_val_if_fail (stream, NULL);
110         
111         priv->stream = stream;
112
113         return obj;
114 }
115
116
117 /* the rest are interface functions */
118
119
120 static ssize_t
121 gtkhtml_read (TnyStreamIface *self, char *buffer, size_t n)
122 {
123         return -1; /* we cannot read */
124 }
125
126
127 static ssize_t
128 gtkhtml_write (TnyStreamIface *self, const char *buffer, size_t n)
129 {
130         ModestTnyStreamGtkhtmlPrivate *priv;
131         
132         g_return_val_if_fail (self, 0);
133
134         priv = MODEST_TNY_STREAM_GTKHTML_GET_PRIVATE(self);
135         if (!priv->stream)
136                 g_warning ("cannot write to closed stream");
137         else
138                 gtk_html_stream_write (priv->stream, buffer, n);
139         
140         return n; /* hmmm */
141 }
142
143         
144 static gint
145 gtkhtml_flush (TnyStreamIface *self)
146 {
147         return 0;
148 }
149         
150
151 static gint
152 gtkhtml_close (TnyStreamIface *self)
153 {
154         ModestTnyStreamGtkhtmlPrivate *priv;
155         g_return_val_if_fail (self, 0);
156         priv = MODEST_TNY_STREAM_GTKHTML_GET_PRIVATE(self);
157         
158         gtk_html_stream_close   (priv->stream, GTK_HTML_STREAM_OK);
159         priv->stream = NULL;
160
161         return 0;
162 }
163
164
165 static gboolean
166 gtkhtml_eos (TnyStreamIface *self)
167 {
168         return TRUE;
169 }
170
171
172         
173 static gint
174 gtkhtml_reset (TnyStreamIface *self)
175 {
176         return 0;
177 }
178
179         
180 static ssize_t
181 gtkhtml_write_to_stream (TnyStreamIface *self, TnyStreamIface *output)
182 {
183         return 0;
184 }
185
186
187 static void
188 modest_tny_stream_gtkhml_iface_init (gpointer g_iface, gpointer iface_data)
189 {
190         TnyStreamIfaceClass *klass;
191         
192         g_return_if_fail (g_iface);
193
194         klass = (TnyStreamIfaceClass *)g_iface;
195         
196         klass->read_func  = gtkhtml_read;
197         klass->write_func = gtkhtml_write;
198         klass->flush_func = gtkhtml_flush;
199         klass->close_func = gtkhtml_close;
200         klass->eos_func   = gtkhtml_eos;
201         klass->reset_func = gtkhtml_reset;
202         klass->write_to_stream_func = gtkhtml_write_to_stream;
203 }